Fix RETURNING side effects for FOR PORTION OF leftover rows
Hi,
Dean raised a comment on my patch [1]/messages/by-id/6C34A987-AC50-4477-BD71-2D4AFEE1A589@gmail.com 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:
```
evantest=# create sequence s;
CREATE SEQUENCE
evantest=# create table t (r int4range, val int);
CREATE TABLE
evantest=# insert into t values ('[1,4)', 0);
INSERT 0 1
evantest=# update t for portion of r from 2 to 3 set val=1 returning nextval('s') as n;
n
---
3
(1 row)
UPDATE 1
evantest=# select last_value from s;
last_value
------------
3
(1 row)
```
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.
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.
See the attached patch for details.
[1]: /messages/by-id/6C34A987-AC50-4477-BD71-2D4AFEE1A589@gmail.com
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
v1-0001-Avoid-RETURNING-side-effects-for-FOR-PORTION-OF-l.patchapplication/octet-stream; name=v1-0001-Avoid-RETURNING-side-effects-for-FOR-PORTION-OF-l.patch; x-unix-mode=0644Download+40-16
On Thu, 16 Jul 2026 at 07:20, Chao Li <li.evan.chao@gmail.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
On Jul 20, 2026, at 16:56, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
On Thu, 16 Jul 2026 at 07:20, Chao Li <li.evan.chao@gmail.com> 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/
Attachments:
v2-0001-Avoid-RETURNING-side-effects-for-FOR-PORTION-OF-l.patchapplication/octet-stream; name=v2-0001-Avoid-RETURNING-side-effects-for-FOR-PORTION-OF-l.patch; x-unix-mode=0644Download+54-13
On Mon, Jul 20, 2026 at 6:17 AM Chao Li <li.evan.chao@gmail.com> 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
On Mon, 20 Jul 2026 at 17:21, Paul A Jungwirth
<pj@illuminatedcomputing.com> wrote:
On Mon, Jul 20, 2026 at 6:17 AM Chao Li <li.evan.chao@gmail.com> wrote:
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!
Sorry for the delay getting back to this.
The fix looks good to me, and the point about cross-partition updates
is a good one.
Attached is a v3 patch, in which I reworked the comments a bit, and
moved the logic of the new check to the place it's actually used. I
also changed the tests to use a NOTICE-raising function instead of a
sequence, since that makes it clearer which rows are/were being
processed by the RETURNING clause.
Regards,
Dean
Attachments:
v3-0001-Avoid-RETURNING-side-effects-for-FOR-PORTION-OF-l.patchtext/x-patch; charset=US-ASCII; name=v3-0001-Avoid-RETURNING-side-effects-for-FOR-PORTION-OF-l.patchDownload+52-13
On Sat, Jul 25, 2026 at 3:16 AM Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
The fix looks good to me, and the point about cross-partition updates
is a good one.Attached is a v3 patch, in which I reworked the comments a bit, and
moved the logic of the new check to the place it's actually used. I
also changed the tests to use a NOTICE-raising function instead of a
sequence, since that makes it clearer which rows are/were being
processed by the RETURNING clause.
This all seems good. Using NOTICE does seem a little nicer.
The commit message has a small typo:
. . . so that the auxiliary INSERT of a
cross-partition UPDATE with a FOR PORTION OF clause still process
<-- s/b processes
RETURNING normally.
I tried to break the fix by using a CTE, but (as expected) that wasn't
a problem, since the outer query has a different executor node. I
don't think my tests need to be included in the commit, but if you
want them, here they are:
+-- The RETURNING list is not evaluated for the leftovers when the
+-- UPDATE ... FOR PORTION OF is in a CTE either, whether the outer query
+-- is a SELECT ...
+INSERT INTO for_portion_of_test (id, valid_at, name) VALUES
+ ('[12,13)', '[2018-01-01,2020-01-01)', 'twelve');
+WITH updated AS (
+ UPDATE for_portion_of_test
+ FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-05-01'
+ SET name = 'twelve^1'
+ WHERE id = '[12,13)'
+ RETURNING *, fpo_returning_row(for_portion_of_test::text)
+)
+SELECT * FROM updated;
+SELECT * FROM for_portion_of_test WHERE id = '[12,13)' ORDER BY id, valid_at;
+
+-- ... or an INSERT:
+CREATE TABLE fpo_returning_log (id int4range, valid_at daterange,
name text, r text);
+INSERT INTO for_portion_of_test (id, valid_at, name) VALUES
+ ('[13,14)', '[2018-01-01,2020-01-01)', 'thirteen');
+WITH updated AS (
+ UPDATE for_portion_of_test
+ FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-05-01'
+ SET name = 'thirteen^1'
+ WHERE id = '[13,14)'
+ RETURNING *, fpo_returning_row(for_portion_of_test::text)
+)
+INSERT INTO fpo_returning_log SELECT * FROM updated;
+SELECT * FROM fpo_returning_log ORDER BY id, valid_at;
+SELECT * FROM for_portion_of_test WHERE id = '[13,14)' ORDER BY id, valid_at;
+
+-- clean up:
+DROP TABLE fpo_returning_log;
+DELETE FROM for_portion_of_test WHERE id IN ('[12,13)', '[13,14)');
Yours,
--
Paul ~{:-)
pj@illuminatedcomputing.com
On Jul 26, 2026, at 03:30, Paul A Jungwirth <pj@illuminatedcomputing.com> wrote:
On Sat, Jul 25, 2026 at 3:16 AM Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
The fix looks good to me, and the point about cross-partition updates
is a good one.Attached is a v3 patch, in which I reworked the comments a bit, and
moved the logic of the new check to the place it's actually used. I
also changed the tests to use a NOTICE-raising function instead of a
sequence, since that makes it clearer which rows are/were being
processed by the RETURNING clause.This all seems good. Using NOTICE does seem a little nicer.
The commit message has a small typo:
. . . so that the auxiliary INSERT of a
cross-partition UPDATE with a FOR PORTION OF clause still process
<-- s/b processes
RETURNING normally.
+1
Otherwise, v2 looks good to me.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
On Mon, 27 Jul 2026 at 02:49, Chao Li <li.evan.chao@gmail.com> wrote:
On Jul 26, 2026, at 03:30, Paul A Jungwirth <pj@illuminatedcomputing.com> wrote:
This all seems good. Using NOTICE does seem a little nicer.
The commit message has a small typo:
+1
OK, pushed and back-patched. Thank you both.
Regards,
Dean
On Jul 28, 2026, at 17:07, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
On Mon, 27 Jul 2026 at 02:49, Chao Li <li.evan.chao@gmail.com> wrote:
On Jul 26, 2026, at 03:30, Paul A Jungwirth <pj@illuminatedcomputing.com> wrote:
This all seems good. Using NOTICE does seem a little nicer.
The commit message has a small typo:
+1
OK, pushed and back-patched. Thank you both.
Regards,
Dean
Thank you very much for taking care of this patch.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/