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

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: tharakan(at)gmail(dot)com
Subject: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual
Date: 2026-11-22 21:15:36
Message-ID: 19931-28ecec0cb4b7fc39@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19931
Logged by: Robins Tharakan
Email address: tharakan(at)gmail(dot)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.
https://www.postgresql.org/message-id/flat/CAPXdwCyBn_DMSgv%3DJpo%3D0DzZ%3DeHUZ3i1Po7KnSt0tF6nK7ZSug%40mail.gmail.com#329859d7a078160def40ceef7aa657bf

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2026-11-23 01:06:02 Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill
Previous Message Raja Sai pranav 2026-11-22 19:10:50 Re: BUG #19930: amcheck might report btree index corruption after recovery of a mid-deletion state