Re: [(known) BUG] DELETE/UPDATE more than one row in partitioned foreign table

From: Ajit Awekar <ajitpostgres(at)gmail(dot)com>
To: Manu <manuelreyesbravo(at)gmail(dot)com>
Cc: Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com>, Etsuro Fujita <etsuro(dot)fujita(at)gmail(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, shihao zhong <zhong950419(at)gmail(dot)com>, Nikita Malakhov <hukutoc(at)gmail(dot)com>, Jehan-Guillaume de Rorthais <jgdr(at)dalibo(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org, rhaas(at)postgresql(dot)org
Subject: Re: [(known) BUG] DELETE/UPDATE more than one row in partitioned foreign table
Date: 2026-11-03 09:59:02
Message-ID: CAER375NOcbQyDGxfRDM0wuRmJx8j5TBmkXtC0QY0j2Doye=HSA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Jakub, Manu,

Thanks for the reviews. Attached is v5, split into two patches as suggested:

- v5-0001: planner support for an FDW-injected row-identity
pseudo-column (costsize.c, initsplan.c, setrefs.c, relnode.c, ruleutils.c).
It has no effect unless an FDW uses it.
- v5-0002: the postgres_fdw part (deparse, executor, tests, docs). It depends
on 0001.

Changes since v4:

1. Whole-row regression. The statement

DELETE FROM fbig WHERE fbig.* IS NULL AND random() <= 1;

now returns without error, as on master. Manu's diagnosis was right:
the retyped qual Var no longer equal()ed the row-typed entry in
fdw_scan_tlist, so a second whole-row entry was appended and
setrefs.c took the first one. All whole-row entries are now typed
RECORD and folded into one.

2. Foreign table as a partition. A whole-row qual in DELETE or
UPDATE, through a local parent or directly on the partition,
failed with "record type has not been registered". Retyped Vars
in quals are now wrapped in a RelabelType back to their original
row type. EXPLAIN shows (t.*)::t IS NULL in such filters;
results are unchanged.

3. The Assert(fdwroutine != NULL) checks are now runtime errors.

I added regression tests for 1 and 2. Manu's case_i_whole_row.sql
(i1-i8) runs without errors on v5, with the same results as master.

Request a review.

Thanks & Best Regards,
Ajit

On Thu, 29 Oct 2026 at 00:02, Manu <manuelreyesbravo(at)gmail(dot)com> wrote:
>
> Hi Jakub,
>
> On Wed, Oct 28, 2026 at 10:12 AM Jakub Wartak
> <jakub(dot)wartak(at)enterprisedb(dot)com> wrote:
> > postgres=# DELETE FROM fbig WHERE fbig.* IS NULL AND random() <= 1;
> > ERROR: attribute 3 of type record has wrong type
>
> Confirmed with V4 on master 126976a8ffc and fcfd2456078, and V3
> already had it; master gives no error. A DELETE whose local qual uses
> the whole row fails the same way without random() and on a foreign
> partition. UPDATE works on a plain foreign table, but on a
> foreign partition it fails with "record type has not been registered".
>
> The ForeignScan's fdw_scan_tlist gets two whole-row entries, one typed
> fbig and one RECORD: normalize_wholerow_vartype() retypes the qual's
> Var (postgres_fdw.c:1515 with V4), so it no longer equal()s the one
> from the reltarget and is appended (:1538). setrefs.c matches on
> varno/varattno only and takes the first, the fbig one. The comment at
> :1507 assumes core's RECORD Var is already there, which is true only
> for UPDATE or with row DELETE triggers; with an AFTER DELETE row
> trigger the RECORD entry comes first and the DELETE on the plain
> foreign table works (on the partition it then fails like UPDATE).
>
> Ajit, I'll set the entry to Ready for Committer once v5 covers this
> and passes the eight cases plus the attached case_i_whole_row.sql.txt.
>
> Regards,
> Manu

Attachment Content-Type Size
v5-0001-Allow-FDWs-to-add-row-identity-pseudo-columns-to-.patch application/octet-stream 9.1 KB
v5-0002-postgres_fdw-Identify-remote-rows-by-ctid-and-tab.patch application/octet-stream 104.6 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexander Pyhalov 2026-11-03 10:04:17 Re: Asynchronous MergeAppend
Previous Message Peter Eisentraut 2026-11-03 09:57:42 Re: Clean up code comments for pg_wchar_tbl