pgsql: Fix incorrect Assert in add_foreign_ordered_paths

From: David Rowley <drowley(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix incorrect Assert in add_foreign_ordered_paths
Date: 2026-11-04 02:58:13
Message-ID: E1xORCY-000000006NU-0cbA@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix incorrect Assert in add_foreign_ordered_paths

12933dc60 (originally 66c0185a3) added setop_pathkeys for providing
presorted inputs for set operation from child queries when that query
doesn't have any other pathkey requirements.

add_foreign_ordered_paths() assumed that if the query had an ORDER BY
that the query_pathkeys would be equal to sort_pathkeys. That's not
true when query_pathkeys is set to setop_pathkeys, which can happen
for set-op child queries with an ORDER BY when all the sort_pathkeys are
redundant pathkeys. (See standard_qp_callback()).

Here we relax the Assert to allow for sort_pathkeys to be empty. In
this case it is technically ok to mark pushdown_safe as true so that
postgresGetForeignUpperPaths() allows add_foreign_final_paths() to
generate paths with the LIMIT clause pushed down. We don't adjust that
here as that could cause plan changes in the backbranches. Let's just
fix things enough so the Assert doesn't fail and do a proper fix in
master.

Backpatch to 17 where the setop_pathkeys field was added.

Reported-by: Robins Tharakan <tharakan(at)gmail(dot)com>
Author: David Rowley <dgrowleyml(at)gmail(dot)com>
Reviewed-by: Robins Tharakan <tharakan(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAEP4nAwnSefmi4w+89XbHQ0f-0OixCYUJo9kVcbXSYL8L4rEPg@mail.gmail.com
Backpatch-through: 17

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/52b41da0be54f5df6708f95dc8bda73b8ae7c563

Modified Files
--------------
contrib/postgres_fdw/expected/postgres_fdw.out | 26 ++++++++++++++++++++++++++
contrib/postgres_fdw/postgres_fdw.c | 16 ++++++++--------
contrib/postgres_fdw/sql/postgres_fdw.sql | 9 +++++++++
3 files changed, 43 insertions(+), 8 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message David Rowley 2026-11-04 02:58:24 pgsql: Fix incorrect Assert in add_foreign_ordered_paths
Previous Message Fujii Masao 2026-11-04 02:22:02 pgsql: Fix possible index corruption in pg_trgm GiST indexes