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

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Paul A Jungwirth <pj(at)illuminatedcomputing(dot)com>
Subject: Re: Fix RETURNING side effects for FOR PORTION OF leftover rows
Date: 2026-07-20 08:56:55
Message-ID: CAEZATCXPv0hQiOj6xr+OocVb9EK2VAmAfei0+_xoXW1jCNNXFA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 16 Jul 2026 at 07:20, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Rafia Sabih 2026-07-20 09:16:32 Re: postgres_fdw: Emit message when batch_size is reduced
Previous Message Nisha Moond 2026-07-20 08:22:53 Re: Support EXCEPT for TABLES IN SCHEMA publications