From a4950fa5e5a684315b0ac2bbe0c7ecc602cb34b6 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Tue, 3 Nov 2026 13:34:05 +1300 Subject: [PATCH v2 2/2] Allow postgres_fdw to pushdown LIMIT with empty sort_pathkeys There is no need to consider the shippability of query_pathkeys here. If sort_pathkeys is NIL due to all PathKeys being redundant for the ORDER BY clause then query_pathkeys can be set to setop_pathkeys, and those may have non-shippable PathKeys. There's no need to consider those when processing UPPERREL_FINAL in postgresGetForeignUpperPaths(). 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 -- =================================================================== -- 2.53.0