Skip to content

Commit 23b75dd

Browse files
committed
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 8bf6ec3 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 fe9e658 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
1 parent afa122e commit 23b75dd

File tree

5 files changed

+204
-160
lines changed

5 files changed

+204
-160
lines changed

src/backend/executor/nodeModifyTable.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,15 @@ ExecUpdate(ModifyTableState *mtstate,
11931193
bool partition_constraint_failed;
11941194
bool update_indexes;
11951195

1196+
/*
1197+
* If we generate a new candidate tuple after EvalPlanQual testing, we
1198+
* must loop back here to try again. (We don't need to redo triggers,
1199+
* however. If there are any BEFORE triggers then trigger.c will have
1200+
* done table_tuple_lock to lock the correct tuple, so there's no need
1201+
* to do them again.)
1202+
*/
1203+
lreplace:
1204+
11961205
/*
11971206
* Constraints and GENERATED expressions might reference the tableoid
11981207
* column, so (re-)initialize tts_tableOid before evaluating them.
@@ -1206,17 +1215,6 @@ ExecUpdate(ModifyTableState *mtstate,
12061215
resultRelationDesc->rd_att->constr->has_generated_stored)
12071216
ExecComputeStoredGenerated(estate, slot);
12081217

1209-
/*
1210-
* Check any RLS UPDATE WITH CHECK policies
1211-
*
1212-
* If we generate a new candidate tuple after EvalPlanQual testing, we
1213-
* must loop back here and recheck any RLS policies and constraints.
1214-
* (We don't need to redo triggers, however. If there are any BEFORE
1215-
* triggers then trigger.c will have done table_tuple_lock to lock the
1216-
* correct tuple, so there's no need to do them again.)
1217-
*/
1218-
lreplace:;
1219-
12201218
/* ensure slot is independent, consider e.g. EPQ */
12211219
ExecMaterializeSlot(slot);
12221220

@@ -1231,6 +1229,7 @@ lreplace:;
12311229
resultRelInfo->ri_PartitionCheck &&
12321230
!ExecPartitionCheck(resultRelInfo, slot, estate, false);
12331231

1232+
/* Check any RLS UPDATE WITH CHECK policies */
12341233
if (!partition_constraint_failed &&
12351234
resultRelInfo->ri_WithCheckOptions != NIL)
12361235
{

0 commit comments

Comments
 (0)