diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2023-03-06 18:31:16 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2023-03-06 18:31:16 -0500 |
commit | 1e05ea51d327635ce56caab56cc47e70716e081f (patch) | |
tree | 98ac5c7e6a06cb4fe71627fbf6050c7c13a212d4 /src/backend/executor/nodeModifyTable.c | |
parent | e9051ecd58e9f171c5c92f28c1bbd8f42fa243c1 (diff) | |
download | postgresql-1e05ea51d327635ce56caab56cc47e70716e081f.tar.gz postgresql-1e05ea51d327635ce56caab56cc47e70716e081f.zip |
Fix some more cases of missed GENERATED-column updates.
If UPDATE is forced to retry after an EvalPlanQual check, it neglected
to repeat GENERATED-column computations, even though those might well
have changed since we're dealing with a different tuple than before.
Fixing this is mostly a matter of looping back a bit further when
we retry. In v15 and HEAD that's most easily done by altering the API
of ExecUpdateAct so that it includes computing GENERATED expressions.
Also, if an UPDATE in a partitioned table turns into a cross-partition
INSERT operation, we failed to recompute GENERATED columns. That's a
bug since 8bf6ec3ba allowed partitions to have different generation
expressions; although it seems to have no ill effects before that.
Fixing this is messier because we can now have situations where the same
query needs both the UPDATE-aligned set of GENERATED columns and the
INSERT-aligned set, and it's unclear which set will be generated first
(else we could hack things by forcing the INSERT-aligned set to be
generated, which is indeed how fe9e658f4 made it work for MERGE).
The best fix seems to be to build and store separate sets of expressions
for the INSERT and UPDATE cases. That would create ABI issues in the
back branches, but so far it seems we can leave this alone in the back
branches.
Per bug #17823 from Hisahiro Kauchi. The first part of this affects all
branches back to v12 where GENERATED columns were added.
Discussion: https://postgr.es/m/17823-b64909cf7d63de84@postgresql.org
Diffstat (limited to 'src/backend/executor/nodeModifyTable.c')
-rw-r--r-- | src/backend/executor/nodeModifyTable.c | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 07805542466..55c430c9ec5 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -1814,6 +1814,15 @@ ExecUpdate(ModifyTableState *mtstate, bool update_indexes; /* + * If we generate a new candidate tuple after EvalPlanQual testing, we + * must loop back here to try again. (We don't need to redo triggers, + * however. If there are any BEFORE triggers then trigger.c will have + * done table_tuple_lock to lock the correct tuple, so there's no need + * to do them again.) + */ +lreplace: + + /* * Constraints and GENERATED expressions might reference the tableoid * column, so (re-)initialize tts_tableOid before evaluating them. */ @@ -1827,17 +1836,6 @@ ExecUpdate(ModifyTableState *mtstate, ExecComputeStoredGenerated(resultRelInfo, estate, slot, CMD_UPDATE); - /* - * Check any RLS UPDATE WITH CHECK policies - * - * If we generate a new candidate tuple after EvalPlanQual testing, we - * must loop back here and recheck any RLS policies and constraints. - * (We don't need to redo triggers, however. If there are any BEFORE - * triggers then trigger.c will have done table_tuple_lock to lock the - * correct tuple, so there's no need to do them again.) - */ -lreplace:; - /* ensure slot is independent, consider e.g. EPQ */ ExecMaterializeSlot(slot); @@ -1852,6 +1850,7 @@ lreplace:; resultRelationDesc->rd_rel->relispartition && !ExecPartitionCheck(resultRelInfo, slot, estate, false); + /* Check any RLS UPDATE WITH CHECK policies */ if (!partition_constraint_failed && resultRelInfo->ri_WithCheckOptions != NIL) { @@ -2996,9 +2995,8 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) /* * For INSERT and UPDATE, prepare to evaluate any generated columns. - * We must do this now, even if we never insert or update any rows, - * because we have to fill resultRelInfo->ri_extraUpdatedCols for - * possible use by the trigger machinery. + * (This is probably not necessary any longer, but we'll refrain from + * changing it in back branches, in case extension code expects it.) */ if (operation == CMD_INSERT || operation == CMD_UPDATE) ExecInitStoredGenerated(resultRelInfo, estate, operation); |