| From: | Tender Wang <tndrwang(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Cc: | Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> |
| Subject: | Fix stale source values in MERGE RETURNING after a cross-partition update |
| Date: | 2026-11-22 07:45:48 |
| Message-ID: | CAHewXNkZb5rBnen7ALsXX3d5JgdpzUjsNY9pqbxub3N-+Nb=yQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
I found a follow-up case for #19912.
When a concurrent update causes MERGE to switch from MATCHED to NOT
MATCHED BY SOURCE, ExecMergeMatched() builds an all-NULL plan slot for
the action.
If that action moves the target row to another partition, ExecInsert()
evaluates RETURNING using context->planSlot, which still contains the
source values from the original match.
How to reproduce:
CREATE TABLE tp (k int, id int) PARTITION BY RANGE (id);
CREATE TABLE tp1 PARTITION OF tp FOR VALUES FROM (1) TO (10);
CREATE TABLE tp2 PARTITION OF tp FOR VALUES FROM (10) TO (20);
CREATE TABLE src (sid int);
INSERT INTO tp VALUES (5, 5);
INSERT INTO src VALUES (5);
session 1
BEGIN;
UPDATE tp SET k = 99 WHERE id = 5;
session 2
MERGE INTO tp t USING src s ON t.k = s.sid
WHEN MATCHED THEN UPDATE SET k = t.k
WHEN NOT MATCHED BY SOURCE THEN UPDATE SET id = 15
RETURNING merge_action(), s.sid, t.k, t.id;
session1
commit;
The attached patch adds an optional returningSlot to ModifyTableContext.
ExecMergeMatched() sets it to the current action’s plan slot before
calling ExecUpdateAct() and clears it when the call returns.
This lets both update paths use the same slot for RETURNING and
preserves the original plan slot for any subsequent WHEN NOT MATCHED
BY TARGET action.
ExecModifyTable() initializes the override to NULL, so ExecInsert()
uses context->planSlot whenever no override is set.
CC’ing Dean since this follows up on the fix for #19912.
--
Tender Wang
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Fix-stale-MERGE-RETURNING-source-values-after-a-cros.patch | text/plain | 5.9 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Chengpeng Yan | 2026-11-22 07:47:16 | Re: Simplify the uniqueness proof for self-join elimination |
| Previous Message | Xuneng Zhou | 2026-11-22 07:01:20 | Re: FATAL: could not send end-of-streaming message to primary: no COPY in progress |