BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual

Started by PG Bug reporting form6 days ago6 messagesbugs

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19931
Logged by: Robins Tharakan
Email address: tharakan@gmail.com
PostgreSQL version: 17.0
Operating system: Debian 13 (x86_64)
Description:

A SELECT ... FOR UPDATE over a join of postgres_fdw foreign tables
ignores a qual that reads no table, such as now() IS NULL, when
postgres_fdw pushes the join down. The query below returns (1); it
should return no rows, as it does without FOR UPDATE and with the local
tables in place of the foreign ones.

The plan has no gating Result for the qual:

Foreign Scan
Relations: (ft1) INNER JOIN (ft2)

Without FOR UPDATE it has one:

Result
One-Time Filter: (now() IS NULL)
-> Foreign Scan
Relations: (ft1) INNER JOIN (ft2)

It returns the wrong rows on master (9b5d4e33317) through REL_17_STABLE.
REL_16_STABLE and older return no rows: 6f80a8d9c13, back-patched to
them, stopped FDWs from replacing a join that has pseudoconstant quals,
and 9e9931d2bf4, on master only, allowed it again.

When the join also has a condition that postgres_fdw evaluates locally,
and an ORDER BY on a whole row keeps the row locking local, such a
query fails at planning time with "variable not found in subplan target
list" instead; that is reported separately.

Commit
======
Started: 9e9931d2bf4 (2023-08-15)
Affects: master through to 17.0

It doesn't seem that fix for bug #19353 [1] helped here.

SQL
===
CREATE EXTENSION postgres_fdw;
DO $$
BEGIN
EXECUTE format('CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (dbname %L, port %L)',
current_database(), current_setting('port'));
END
$$;
CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (b int);
INSERT INTO t1 VALUES (1);
INSERT INTO t2 VALUES (1);
CREATE FOREIGN TABLE ft1 (a int) SERVER loopback OPTIONS (table_name 't1');
CREATE FOREIGN TABLE ft2 (b int) SERVER loopback OPTIONS (table_name 't2');
ANALYZE ft1, ft2;

SELECT ft1.a FROM ft1 JOIN ft2 ON ft1.a = ft2.b
WHERE now() IS NULL
FOR UPDATE;

Found using SQLsmith.
-
Robins Tharakan
Databricks

Reference:
1.
/messages/by-id/CAPXdwCyBn_DMSgv=Jpo=0DzZ=eHUZ3i1Po7KnSt0tF6nK7ZSug@mail.gmail.com

#2Nitin Motiani
nitinmotiani@google.com
In reply to: PG Bug reporting form (#1)
Re: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual

On Mon, Nov 23, 2026 at 2:10 PM PG Bug reporting form
<noreply@postgresql.org> wrote:

It returns the wrong rows on master (9b5d4e33317) through REL_17_STABLE.
REL_16_STABLE and older return no rows: 6f80a8d9c13, back-patched to
them, stopped FDWs from replacing a join that has pseudoconstant quals,
and 9e9931d2bf4, on master only, allowed it again.

Thanks for the report. I was able to reproduce this bug. The fix seems
small so I'm attaching a patch. It fixes this issue by passing
fdw_restrictinfo of the join path to the final path which can then use
it to gate on the clauses like now() is null.

Please take a look.

Thanks & Regards,
Nitin Motiani
Google

Attachments:

t259655_2
v1-0001-postgres_fdw-Fix-FOR-UPDATE-SHARE-over-a-join-ign.patchapplication/x-patch; name=v1-0001-postgres_fdw-Fix-FOR-UPDATE-SHARE-over-a-join-ign.patchDownload+62-6
#3Matheus Alcantara
matheusssilv97@gmail.com
In reply to: Nitin Motiani (#2)
Re: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual

On Mon Nov 23, 2026 at 8:40 AM -03, Nitin Motiani wrote:

On Mon, Nov 23, 2026 at 2:10 PM PG Bug reporting form
<noreply@postgresql.org> wrote:

It returns the wrong rows on master (9b5d4e33317) through REL_17_STABLE.
REL_16_STABLE and older return no rows: 6f80a8d9c13, back-patched to
them, stopped FDWs from replacing a join that has pseudoconstant quals,
and 9e9931d2bf4, on master only, allowed it again.

Thanks for the report. I was able to reproduce this bug. The fix seems
small so I'm attaching a patch. It fixes this issue by passing
fdw_restrictinfo of the join path to the final path which can then use
it to gate on the clauses like now() is null.

Please take a look.

Thanks for the patch!

I've reviewed and tested it, and I think it's the right fix. Since
9e9931d2bf4, create_scan_plan() gets the gating quals for a join rel
from fdw_restrictinfo. The final path keeps the join rel as its parent,
so it has to carry that list too.

I tested a few variants on top of the reported query: FOR UPDATE with an
ORDER BY that's pushed down, a 3-way join, and FOR SHARE OF. All of them
returned wrong rows on master and are correct with the fix, so the patch
looks good to me. It also applies cleanly on REL_17_STABLE and
REL_18_STABLE.

A small comment: I think that the changes on add_foreign_final_paths()
can be simplified into:

+  ForeignPath *fpath;
+
+  if (IsA(path, ProjectionPath))
+          fpath = (ForeignPath *) ((ProjectionPath *) path)->subpath;
+  else
+          fpath = (ForeignPath *) path;

And then use fpath->fdw_restrictinfo on create_foreign_upper_path().

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

#4Nitin Motiani
nitinmotiani@google.com
In reply to: Matheus Alcantara (#3)
Re: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual

Thanks for the review, Matheus. I have created a commitfest entry for
this here https://commitfest.postgresql.org/patch/7737.

On Tue, Nov 24, 2026 at 10:03 PM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:

A small comment: I think that the changes on add_foreign_final_paths()
can be simplified into:

+  ForeignPath *fpath;
+
+  if (IsA(path, ProjectionPath))
+          fpath = (ForeignPath *) ((ProjectionPath *) path)->subpath;
+  else
+          fpath = (ForeignPath *) path;

And then use fpath->fdw_restrictinfo on create_foreign_upper_path().

--

Have made this change. Also added a test for a 3-way join. Attaching
the v2 patch. Please take a look.

Thanks & Regards,
Nitin Motiani
Google

Attachments:

t259655_4
v2-0001-postgres_fdw-Fix-FOR-UPDATE-SHARE-over-a-join-ign.patchapplication/x-patch; name=v2-0001-postgres_fdw-Fix-FOR-UPDATE-SHARE-over-a-join-ign.patchDownload+83-3
#5Kirill Reshke
reshkekirill@gmail.com
In reply to: Nitin Motiani (#4)
Re: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual

On Fri, 27 Nov 2026 at 12:17, Nitin Motiani <nitinmotiani@google.com> wrote:

Thanks for the review, Matheus. I have created a commitfest entry for
this here https://commitfest.postgresql.org/patch/7737.

On Tue, Nov 24, 2026 at 10:03 PM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:

A small comment: I think that the changes on add_foreign_final_paths()
can be simplified into:

+  ForeignPath *fpath;
+
+  if (IsA(path, ProjectionPath))
+          fpath = (ForeignPath *) ((ProjectionPath *) path)->subpath;
+  else
+          fpath = (ForeignPath *) path;

And then use fpath->fdw_restrictinfo on create_foreign_upper_path().

--

Have made this change. Also added a test for a 3-way join. Attaching
the v2 patch. Please take a look.

Thanks & Regards,
Nitin Motiani
Google

Hi Nitin,

Looks like there is one more case in add_foreign_final_paths() with no
needs no row locking. A generic plan for a LEFT JOIN with LIMIT 1 can
loose a bool parameter from the WHERE clause: example:

SET plan_cache_mode = force_generic_plan;
PREPARE q(boolean) AS
SELECT 42 FROM ft1 LEFT JOIN ft2 ON ft1.a = ft2.b
WHERE $1 LIMIT 1;
EXECUTE q(false);

This returned one row containing 42. I expected zero rows. A NULL
argument also returned one row. The same query over the local tables
returned zero rows for both arguments and one row for true.

Looks like this diff will do

--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -1456,6 +1456,15 @@ postgresGetForeignPlan(PlannerInfo *root,
  remote_exprs = extract_actual_clauses(fpinfo->remote_conds, false);
  local_exprs = extract_actual_clauses(fpinfo->local_conds, false);
+ /*
+ * A pushed-down LIMIT on a join has no local gating Result.  Keep
+ * shippable pseudoconstant WHERE clauses below the remote LIMIT.
+ */
+ if (has_limit && IS_JOIN_REL(foreignrel))
+ remote_exprs = list_concat(remote_exprs,
+   extract_actual_clauses(fpinfo->remote_conds,
+  true));
+
  /*
  * We leave fdw_recheck_quals empty in this case, since we never need
  * to apply EPQ recheck clauses.  In the case of a joinrel, EPQ
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql
b/contrib/postgres_fdw/sql/postgres_fdw.sql
index b71771a..76d260e 100644

But I didn't have time to fully review yet

--
Best regards,
Kirill Reshke

#6Nitin Motiani
nitinmotiani@google.com
In reply to: Kirill Reshke (#5)
Re: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual

On Fri, Nov 27, 2026 at 2:02 PM Kirill Reshke <reshkekirill@gmail.com> wrote:

Looks like there is one more case in add_foreign_final_paths() with no
needs no row locking. A generic plan for a LEFT JOIN with LIMIT 1 can
loose a bool parameter from the WHERE clause: example:

SET plan_cache_mode = force_generic_plan;
PREPARE q(boolean) AS
SELECT 42 FROM ft1 LEFT JOIN ft2 ON ft1.a = ft2.b
WHERE $1 LIMIT 1;
EXECUTE q(false);

Thanks for catching this, Kiril. I was able to reproduce it too. I had
originally tested limit case with inner joins which happened to be
fine because their pushed-down were sent as a part of join class to
the remote, but for outer joins postgresGetForeignPlan() omits
pseudoconstant remote_conds.

This returned one row containing 42. I expected zero rows. A NULL

argument also returned one row. The same query over the local tables
returned zero rows for both arguments and one row for true.

Looks like this diff will do

--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -1456,6 +1456,15 @@ postgresGetForeignPlan(PlannerInfo *root,
remote_exprs = extract_actual_clauses(fpinfo->remote_conds, false);
local_exprs = extract_actual_clauses(fpinfo->local_conds, false);
+ /*
+ * A pushed-down LIMIT on a join has no local gating Result.  Keep
+ * shippable pseudoconstant WHERE clauses below the remote LIMIT.
+ */
+ if (has_limit && IS_JOIN_REL(foreignrel))
+ remote_exprs = list_concat(remote_exprs,
+   extract_actual_clauses(fpinfo->remote_conds,
+  true));
+
/*
* We leave fdw_recheck_quals empty in this case, since we never need
* to apply EPQ recheck clauses.  In the case of a joinrel, EPQ
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql
b/contrib/postgres_fdw/sql/postgres_fdw.sql
index b71771a..76d260e 100644

This should work but I opted to pass the join's restriction list to
the final path, as v2 does for the FOR UPDATE case. This way we have
the same local gating for both the cases. I'm attaching v3 with this
change. Please take a look.

Regards,
Nitin Motiani
Google

Attachments:

t259655_6
v3-0001-postgres_fdw-Fix-pseudoconstant-quals-being-ignor.patchapplication/x-patch; name=v3-0001-postgres_fdw-Fix-pseudoconstant-quals-being-ignor.patchDownload+172-4