• Etsuro Fujita's avatar
    postgres_fdw: Fix costing of pre-sorted foreign paths with local stats. · 08d2d58a
    Etsuro Fujita authored
    Commit aa09cd24 modified estimate_path_cost_size() so that it reuses
    cached costs of a basic foreign path for a given foreign-base/join
    relation when costing pre-sorted foreign paths for that relation, but it
    incorrectly re-computed retrieved_rows, an estimated number of rows
    fetched from the remote side, which is needed for costing both the basic
    and pre-sorted foreign paths.  To fix, handle retrieved_rows the same way
    as the cached costs: store in that relation's fpinfo the retrieved_rows
    estimate computed for costing the basic foreign path, and reuse it when
    costing the pre-sorted foreign paths.  Also, reuse the rows/width
    estimates stored in that relation's fpinfo when costing the pre-sorted
    foreign paths, to make the code consistent.
    
    In commit ffab494a, to extend the costing mentioned above to the
    foreign-grouping case, I made a change to add_foreign_grouping_paths() to
    store in a given foreign-grouped relation's RelOptInfo the rows estimate
    for that relation for reuse, but this patch makes that change unnecessary
    since we already store the row estimate in that relation's fpinfo, which
    this patch reuses when costing a foreign path for that relation with the
    sortClause ordering; remove that change.
    
    In passing, fix thinko in commit 7012b132: in estimate_path_cost_size(),
    the width estimate for a given foreign-grouped relation to be stored in
    that relation's fpinfo was reset incorrectly when costing a basic foreign
    path for that relation with local stats.
    
    Apply the patch to HEAD only to avoid destabilizing existing plan choices.
    
    Author: Etsuro Fujita
    Discussion: https://postgr.es/m/CAPmGK17jaJLPDEkgnP2VmkOg=5wT8YQ1CqssU8JRpZ_NSE+dqQ@mail.gmail.com
    08d2d58a
postgres_fdw.h 6.95 KB