Re: Fix RETURNING side effects for FOR PORTION OF leftover rows

Поиск
Список
Период
Сортировка
Искать

Re: Fix RETURNING side effects for FOR PORTION OF leftover rows

От:
Dean Rasheed <dean.a.rasheed@gmail.com>
Дата:
On Thu, 16 Jul 2026 at 07:20, Chao Li  wrote:
>
> Dean raised a comment on my patch [1] about RETURNING processing for UPDATE ... FOR PORTION OF. While addressing that comment, I found a new problem: UPDATE/DELETE ... FOR PORTION OF currently processes RETURNING for leftover rows as well, which seems wrong because it can cause unexpected side effects.
>
> Here is a simple repro using a sequence:
>
> The UPDATE FOR PORTION OF command generates two leftover rows, and both cause RETURNING to be processed. As a result, the sequence is advanced three times, which seems unexpected from the user's perspective.

Yes, I wondered whether that could happen. We should definitely fix
this. Apart from anything else, it's not good that it can execute
user-defined functions on the leftover rows, which haven't been
checked against RLS SELECT policies.

> To fix the problem, since leftover rows are inserted by calling ExecInsert(), and ExecInsert() is a local static function, I think a simple and safe solution is to add a new parameter to ExecInsert() to indicate whether to process RETURNING for this insert. Then UPDATE/DELETE FOR PORTION OF can bypass RETURNING processing for leftover rows.

I don't particularly like adding another argument to ExecInsert(). I
think it can work out from its existing arguments whether it's part of
a FOR PORTION OF query (by looking at mtstate).

Regards,
Dean


Re: Fix RETURNING side effects for FOR PORTION OF leftover rows

От:
Chao Li <li.evan.chao@gmail.com>
Дата:
Вложения:


> On Jul 20, 2026, at 16:56, Dean Rasheed  wrote:
> 
> On Thu, 16 Jul 2026 at 07:20, Chao Li  wrote:
> 
>> To fix the problem, since leftover rows are inserted by calling ExecInsert(), and ExecInsert() is a local static function, I think a simple and safe solution is to add a new parameter to ExecInsert() to indicate whether to process RETURNING for this insert. Then UPDATE/DELETE FOR PORTION OF can bypass RETURNING processing for leftover rows.
> 
> I don't particularly like adding another argument to ExecInsert(). I
> think it can work out from its existing arguments whether it's part of
> a FOR PORTION OF query (by looking at mtstate).
> 

Okay. We cannot simply check node->forPortionOf to bypass RETURNING processing, because an FPO update on a partitioned table may move a row to a different partition. In that case, ExecInsert() is also called to insert the row into the new partition.

However, in that case, mtstate->operation is still CMD_UPDATE, while for leftover rows, mtstate->operation is temporarily changed to CMD_INSERT. So we can check both node->forPortionOf and mtstate->operation.

PFA v2 with the new implementation. I tried to run check-world and it passed.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/




Re: Fix RETURNING side effects for FOR PORTION OF leftover rows

От:
Paul A Jungwirth <pj@illuminatedcomputing.com>
Дата:
On Mon, Jul 20, 2026 at 6:17 AM Chao Li  wrote:
>
> Okay. We cannot simply check node->forPortionOf to bypass RETURNING processing, because an FPO update on a partitioned table may move a row to a different partition. In that case, ExecInsert() is also called to insert the row into the new partition.
>
> However, in that case, mtstate->operation is still CMD_UPDATE, while for leftover rows, mtstate->operation is temporarily changed to CMD_INSERT. So we can check both node->forPortionOf and mtstate->operation.
>
> PFA v2 with the new implementation. I tried to run check-world and it passed.

Nice catch. I took a look at the v2 patch. It looks like a good fix to
me. I'm happy that we're testing cross-partition updates as well.
Thanks for the quick fix!

Yours,

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com


FAQ