pgsql: Fix bugs in RETURNING in cross-partition UPDATE cases. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix bugs in RETURNING in cross-partition UPDATE cases.
Date
Msg-id E1lZbXj-0003gl-IE@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix bugs in RETURNING in cross-partition UPDATE cases.

If the source and destination partitions don't have identical
rowtypes (for example, one has dropped columns the other lacks),
then the planSlot contents will be different because of that.
If the query has a RETURNING list that tries to return resjunk
columns out of the planSlot, that is columns from tables that
were joined to the target table, we'd get errors or wrong answers.
That's because we used the RETURNING list generated for the
destination partition, which expects a planSlot matching that
partition's subplan.

The most practical fix seems to be to convert the updated destination
tuple back to the source partition's rowtype, and then apply the
RETURNING list generated for the source partition.  This avoids making
fragile assumptions about whether the per-subpartition subplans
generated all the resjunk columns in the same order.

This has been broken since v11 introduced cross-partition UPDATE.
The lack of field complaints shows that non-identical partitions
aren't a common case; therefore, don't stress too hard about
making the conversion efficient.

There's no such bug in HEAD, because commit 86dc90056 got rid of
per-target-relation variance in the contents of the planSlot.
Hence, patch v11-v13 only.

Amit Langote and Etsuro Fujita, small changes by me

Discussion: https://postgr.es/m/CA+HiwqE_UK1jTSNrjb8mpTdivzd3dum6mK--xqKq0Y9VmfwWQA@mail.gmail.com

Branch
------
REL_13_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/a71cfc56bf6013e3ea1d673acaf73fe7ebbd6bf3

Modified Files
--------------
src/backend/executor/nodeModifyTable.c | 88 +++++++++++++++++++++++++++++-----
src/test/regress/expected/update.out   | 40 ++++++++++++++++
src/test/regress/sql/update.sql        | 25 ++++++++++
3 files changed, 141 insertions(+), 12 deletions(-)


pgsql-committers by date:

Previous
From: Andrew Dunstan
Date:
Subject: pgsql: Make PostgresNode version aware
Next
From: Tom Lane
Date:
Subject: pgsql: Doc: document the tie-breaking behavior of the round() function.