postgres.git / summary / log / commit / refs

commit    b6178c942ea7a85f72beb6a775d2793062a7ff8e
Author:   David Rowley <drowley@postgresql.org>
Date:     Wed Nov 04 03:23:34 2026 +0000

    Allow LIMIT pushdown in postgres_fdw with empty ORDER BY pathkeys
    
    When all of the ORDER BY clause's PathKeys are redundant due to consts in
    all of the pathkeys' EquivalenceClasses, sort_pathkeys is NIL, but
    query_pathkeys may then get set to setop_pathkeys, which may not be
    shippable.  add_foreign_ordered_paths() previously used the shippability
    of query_pathkeys to decide whether the UPPERREL_ORDERED step was safe to
    push down, which needlessly prevented the subsequent UPPERREL_FINAL step
    from pushing down the LIMIT.  No sort is needed in this case, so just mark
    the ordered step as pushdown-safe.
    
    Author: David Rowley <dgrowleyml@gmail.com>
    Reviewed-by: Robins Tharakan <tharakan@gmail.com>
    Discussion: https://postgr.es/m/CAEP4nAwnSefmi4w+89XbHQ0f-0OixCYUJo9kVcbXSYL8L4rEPg@mail.gmail.com


contrib/postgres_fdw/expected/postgres_fdw.out | 10 ++++++++++ contrib/postgres_fdw/postgres_fdw.c | 14 ++++++++++---- contrib/postgres_fdw/sql/postgres_fdw.sql | 4 ++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index fa5844aa9cd..b946a6416a0 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -1408,6 +1408,16 @@ SELECT * FROM ( Remote SQL: SELECT "C 1", c2 FROM "S 1"."T 1" WHERE (("C 1" = 0)) (14 rows) +-- Ensure the LIMIT clause is pushed to the foreign server +EXPLAIN (VERBOSE, COSTS OFF) +SELECT c1, c2 FROM ft7 WHERE c1 = 0 ORDER BY c1 LIMIT 1; + QUERY PLAN +------------------------------------------------------------------------------- + Foreign Scan on public.ft7 + Output: c1, c2 + Remote SQL: SELECT c1, c2 FROM "S 1"."T 4" WHERE ((c1 = 0)) LIMIT 1::bigint +(3 rows) + -- =================================================================== -- JOIN queries -- =================================================================== diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index 1acf506f6fa..7621f679a99 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -8277,11 +8277,17 @@ add_foreign_ordered_paths(PlannerInfo *root, RelOptInfo *input_rel, if (input_rel->reloptkind == RELOPT_BASEREL || input_rel->reloptkind == RELOPT_JOINREL) { - Assert(root->query_pathkeys == root->sort_pathkeys || - root->sort_pathkeys == NIL); + /* mark pushdown as safe when no sort is required for the ORDER BY */ + if (root->sort_pathkeys == NIL) + fpinfo->pushdown_safe = true; + else + { + Assert(root->query_pathkeys == root->sort_pathkeys); + + /* Safe to push down if the query_pathkeys are safe to push down */ + fpinfo->pushdown_safe = ifpinfo->qp_is_pushdown_safe; + } - /* Safe to push down if the query_pathkeys are safe to push down */ - fpinfo->pushdown_safe = ifpinfo->qp_is_pushdown_safe; return; } diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 058caac503f..906d76a4ff3 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -551,6 +551,10 @@ SELECT * FROM ( (SELECT c1, c2 FROM ft1 WHERE c1 = 0 ORDER BY c1) ) a; +-- Ensure the LIMIT clause is pushed to the foreign server +EXPLAIN (VERBOSE, COSTS OFF) +SELECT c1, c2 FROM ft7 WHERE c1 = 0 ORDER BY c1 LIMIT 1; + -- =================================================================== -- JOIN queries -- =================================================================== [parent: 52b41da0be54]