| From: | Tender Wang <tndrwang(at)gmail(dot)com> |
|---|---|
| To: | 303677365(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Cc: | Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com> |
| Subject: | Re: BUG #19912: MERGE NOT MATCHED BY SOURCE sees stale source values after concurrent update (EPQ) |
| Date: | 2026-11-18 01:42:41 |
| Message-ID: | CAHewXNkTvQD=A6T8QDvW3objACWyy8WGo0+UurWTX61po8W8yg@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
PG Bug reporting form <noreply(at)postgresql(dot)org> 于2026年11月18日周三 02:28写道:
> **Root cause:**
>
> In ExecMergeMatched() (src/backend/executor/nodeModifyTable.c), when the
> target tuple was concurrently updated (TM_Updated), the join quals are
> re-evaluated with the updated target tuple via EvalPlanQual, but neither
> context->planSlot nor econtext->ecxt_innertuple is refreshed from the EPQ
> re-evaluation result. When the re-check concludes the row no longer matches
> and the executor transitions to the NOT MATCHED BY SOURCE action, the
> action's projection (ExecProject) and RETURNING (ExecProcessReturning(...,
> context->planSlot)) therefore read the stale source tuple from the original
> plan slot. Regular UPDATE/DELETE pass the EPQ tuple back to callers via
> *epqreturnslot and build the new tuple from it; MERGE's MATCHED→NMBS
> transition has no equivalent refresh.
>
Thanks for the detailed report and analysis. I can reproduce the issue on
current master.
The diagnosis is correct. When EvalPlanQual determines that a concurrently
updated target tuple no longer matches the source tuple, it returns an EPQ
tuple with the source side correctly set to NULL. ExecMergeMatched() then
switches to the NOT MATCHED BY SOURCE action list, but the action quals and
projections continue to use the original plan slot. RETURNING also receives
that original slot, so all of them can observe stale source values.
The attached patch uses the EPQ slot for the NOT MATCHED BY SOURCE action
quals, projection, and RETURNING. It leaves context->planSlot unchanged,
because a subsequent NOT MATCHED BY TARGET action still needs the original
source tuple.
All regression and isolation tests pass.
CC'ing Dean in case he has any thoughts.
--
Tender Wang
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Use-EPQ-tuple-for-MERGE-NOT-MATCHED-BY-SOURCE-action.patch | text/plain | 3.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Henson Choi | 2026-11-18 01:05:20 | Re: BUG #19911: Window aggregate over ROWS n FOLLOWING: "unexpected end of tuplestore" after spill |