Re: Fix stale source values in MERGE RETURNING after a cross-partition update

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Tender Wang <tndrwang(at)gmail(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix stale source values in MERGE RETURNING after a cross-partition update
Date: 2026-11-22 11:32:31
Message-ID: CAEZATCX9qyA_0Sx0y54A9rLcGHjpngT6EBem6ZVRVWRrkGUeAQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sun, 22 Nov 2026 at 07:46, Tender Wang <tndrwang(at)gmail(dot)com> wrote:
>
> 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.

Ugh yes, that's annoying. Well spotted.

> 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.

Hmm, I don't like that solution because it's somewhat delocalised,
which I think makes it fragile if new code is added using
context->planSlot, instead of your new returningSlot.

I think a better (more localised) solution is to have
ExecMergeMatched() swap out context->planSlot while it executes the
NOT MATCHED BY SOURCE action. That feels more robust, because it
removes any chance of any code called while executing the action
seeing the old stale source values. Something like v2, attached.

Also, I don't think merge-match-recheck is the right place to test
this, since it's not about rechecking AND conditions. I've added new
tests to merge-not-matched, based on the ones added before for bug
#19912, testing both left and full joins.

(I didn't remove your tests from v2 yet, because I wanted to confirm
that they also passed with the new code.)

Regards,
Dean

Attachment Content-Type Size
v2-Fix-stale-MERGE-RETURNING-source-values-after-a-cros.patch text/x-patch 13.1 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message ZizhuanLiu X-MAN 2026-11-22 12:31:16 Optimize IS NOT DISTINCT FROM to equality when possible
Previous Message Pavlo Sukhanov 2026-11-22 10:06:09 Re: Python/pytest test framework take two