Skip to content

Commit 3706cc9

Browse files
committed
Fix calculation of which GENERATED columns need to be updated.
We were identifying the updatable generated columns of inheritance children by transposing the calculation made for their parent. However, there's nothing that says a traditional-inheritance child can't have generated columns that aren't there in its parent, or that have different dependencies than are in the parent's expression. (At present it seems that we don't enforce that for partitioning either, which is likely wrong to some degree or other; but the case clearly needs to be handled with traditional inheritance.) Hence, drop the very-klugy-anyway "extraUpdatedCols" RTE field in favor of identifying which generated columns depend on updated columns during executor startup. In HEAD we can remove extraUpdatedCols altogether; in back branches, it's still there but always empty. Another difference between the HEAD and back-branch versions of this patch is that in HEAD we can add the new bitmap field to ResultRelInfo, but that would cause an ABI break in back branches. Like 4b3e379, add a List field at the end of struct EState instead. Back-patch to v13. The bogus calculation is also being made in v12, but it doesn't have the same visible effect because we don't use it to decide which generated columns to recalculate; as a consequence of which the patch doesn't apply easily. I think that there might still be a demonstrable bug associated with trigger firing conditions, but that's such a weird corner-case usage that I'm content to leave it unfixed in v12. Amit Langote and Tom Lane Discussion: https://postgr.es/m/CA+HiwqFshLKNvQUd1DgwJ-7tsTp=dwv7KZqXC4j2wYBV1aCDUA@mail.gmail.com Discussion: https://postgr.es/m/2793383.1672944799@sss.pgh.pa.us
1 parent aa26980 commit 3706cc9

File tree

14 files changed

+330
-126
lines changed

14 files changed

+330
-126
lines changed

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "optimizer/appendinfo.h"
3232
#include "optimizer/clauses.h"
3333
#include "optimizer/cost.h"
34+
#include "optimizer/inherit.h"
3435
#include "optimizer/optimizer.h"
3536
#include "optimizer/pathnode.h"
3637
#include "optimizer/paths.h"
@@ -1813,7 +1814,8 @@ postgresPlanForeignModify(PlannerInfo *root,
18131814
else if (operation == CMD_UPDATE)
18141815
{
18151816
int col;
1816-
Bitmapset *allUpdatedCols = bms_union(rte->updatedCols, rte->extraUpdatedCols);
1817+
RelOptInfo *rel = find_base_rel(root, resultRelation);
1818+
Bitmapset *allUpdatedCols = get_rel_all_updated_cols(root, rel);
18171819

18181820
col = -1;
18191821
while ((col = bms_next_member(allUpdatedCols, col)) >= 0)

src/backend/executor/execUtils.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ CreateExecutorState(void)
132132
estate->es_insert_pending_result_relations = NIL;
133133
estate->es_insert_pending_modifytables = NIL;
134134

135+
estate->es_resultrelinfo_extra = NIL;
136+
135137
estate->es_param_list_info = NULL;
136138
estate->es_param_exec_vals = NULL;
137139

@@ -1323,26 +1325,25 @@ ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate)
13231325
Bitmapset *
13241326
ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate)
13251327
{
1326-
/* see ExecGetInsertedCols() */
1327-
if (relinfo->ri_RangeTableIndex != 0)
1328-
{
1329-
RangeTblEntry *rte = exec_rt_fetch(relinfo->ri_RangeTableIndex, estate);
1328+
Relation rel = relinfo->ri_RelationDesc;
1329+
TupleDesc tupdesc = RelationGetDescr(rel);
13301330

1331-
return rte->extraUpdatedCols;
1332-
}
1333-
else if (relinfo->ri_RootResultRelInfo)
1331+
if (tupdesc->constr && tupdesc->constr->has_generated_stored)
13341332
{
1335-
ResultRelInfo *rootRelInfo = relinfo->ri_RootResultRelInfo;
1336-
RangeTblEntry *rte = exec_rt_fetch(rootRelInfo->ri_RangeTableIndex, estate);
1333+
ListCell *lc;
13371334

1338-
if (relinfo->ri_RootToPartitionMap != NULL)
1339-
return execute_attr_map_cols(relinfo->ri_RootToPartitionMap->attrMap,
1340-
rte->extraUpdatedCols);
1341-
else
1342-
return rte->extraUpdatedCols;
1335+
/* Assert that ExecInitStoredGenerated has been called. */
1336+
Assert(relinfo->ri_GeneratedExprs != NULL);
1337+
foreach(lc, estate->es_resultrelinfo_extra)
1338+
{
1339+
ResultRelInfoExtra *rextra = (ResultRelInfoExtra *) lfirst(lc);
1340+
1341+
if (rextra->rinfo == relinfo)
1342+
return rextra->ri_extraUpdatedCols;
1343+
}
1344+
Assert(false); /* shouldn't get here */
13431345
}
1344-
else
1345-
return NULL;
1346+
return NULL;
13461347
}
13471348

13481349
/* Return columns being updated, including generated columns */

src/backend/executor/nodeModifyTable.c

Lines changed: 115 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "foreign/fdwapi.h"
5555
#include "miscadmin.h"
5656
#include "nodes/nodeFuncs.h"
57+
#include "optimizer/optimizer.h"
5758
#include "rewrite/rewriteHandler.h"
5859
#include "storage/bufmgr.h"
5960
#include "storage/lmgr.h"
@@ -353,69 +354,128 @@ ExecCheckTIDVisible(EState *estate,
353354
}
354355

355356
/*
356-
* Compute stored generated columns for a tuple
357+
* Initialize to compute stored generated columns for a tuple
358+
*
359+
* This fills the resultRelInfo's ri_GeneratedExprs field and makes an
360+
* associated ResultRelInfoExtra struct to hold ri_extraUpdatedCols.
361+
* (Currently, ri_extraUpdatedCols is consulted only in UPDATE, but we might
362+
* as well fill it for INSERT too.)
357363
*/
358-
void
359-
ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo,
360-
EState *estate, TupleTableSlot *slot,
361-
CmdType cmdtype)
364+
static void
365+
ExecInitStoredGenerated(ResultRelInfo *resultRelInfo,
366+
EState *estate,
367+
CmdType cmdtype)
362368
{
363369
Relation rel = resultRelInfo->ri_RelationDesc;
364370
TupleDesc tupdesc = RelationGetDescr(rel);
365371
int natts = tupdesc->natts;
372+
Bitmapset *updatedCols;
373+
ResultRelInfoExtra *rextra;
366374
MemoryContext oldContext;
367-
Datum *values;
368-
bool *nulls;
369375

370-
Assert(tupdesc->constr && tupdesc->constr->has_generated_stored);
376+
/* Don't call twice */
377+
Assert(resultRelInfo->ri_GeneratedExprs == NULL);
378+
379+
/* Nothing to do if no generated columns */
380+
if (!(tupdesc->constr && tupdesc->constr->has_generated_stored))
381+
return;
371382

372383
/*
373-
* If first time through for this result relation, build expression
374-
* nodetrees for rel's stored generation expressions. Keep them in the
375-
* per-query memory context so they'll survive throughout the query.
384+
* In an UPDATE, we can skip computing any generated columns that do not
385+
* depend on any UPDATE target column. But if there is a BEFORE ROW
386+
* UPDATE trigger, we cannot skip because the trigger might change more
387+
* columns.
376388
*/
377-
if (resultRelInfo->ri_GeneratedExprs == NULL)
378-
{
379-
oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
389+
if (cmdtype == CMD_UPDATE &&
390+
!(rel->trigdesc && rel->trigdesc->trig_update_before_row))
391+
updatedCols = ExecGetUpdatedCols(resultRelInfo, estate);
392+
else
393+
updatedCols = NULL;
380394

381-
resultRelInfo->ri_GeneratedExprs =
382-
(ExprState **) palloc(natts * sizeof(ExprState *));
383-
resultRelInfo->ri_NumGeneratedNeeded = 0;
395+
/*
396+
* Make sure these data structures are built in the per-query memory
397+
* context so they'll survive throughout the query.
398+
*/
399+
oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
400+
401+
resultRelInfo->ri_GeneratedExprs =
402+
(ExprState **) palloc0(natts * sizeof(ExprState *));
403+
resultRelInfo->ri_NumGeneratedNeeded = 0;
404+
405+
rextra = palloc_object(ResultRelInfoExtra);
406+
rextra->rinfo = resultRelInfo;
407+
rextra->ri_extraUpdatedCols = NULL;
408+
estate->es_resultrelinfo_extra = lappend(estate->es_resultrelinfo_extra,
409+
rextra);
384410

385-
for (int i = 0; i < natts; i++)
411+
for (int i = 0; i < natts; i++)
412+
{
413+
if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
386414
{
387-
if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
388-
{
389-
Expr *expr;
415+
Expr *expr;
390416

391-
/*
392-
* If it's an update and the current column was not marked as
393-
* being updated, then we can skip the computation. But if
394-
* there is a BEFORE ROW UPDATE trigger, we cannot skip
395-
* because the trigger might affect additional columns.
396-
*/
397-
if (cmdtype == CMD_UPDATE &&
398-
!(rel->trigdesc && rel->trigdesc->trig_update_before_row) &&
399-
!bms_is_member(i + 1 - FirstLowInvalidHeapAttributeNumber,
400-
ExecGetExtraUpdatedCols(resultRelInfo, estate)))
401-
{
402-
resultRelInfo->ri_GeneratedExprs[i] = NULL;
403-
continue;
404-
}
417+
/* Fetch the GENERATED AS expression tree */
418+
expr = (Expr *) build_column_default(rel, i + 1);
419+
if (expr == NULL)
420+
elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
421+
i + 1, RelationGetRelationName(rel));
422+
423+
/*
424+
* If it's an update with a known set of update target columns,
425+
* see if we can skip the computation.
426+
*/
427+
if (updatedCols)
428+
{
429+
Bitmapset *attrs_used = NULL;
405430

406-
expr = (Expr *) build_column_default(rel, i + 1);
407-
if (expr == NULL)
408-
elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
409-
i + 1, RelationGetRelationName(rel));
431+
pull_varattnos((Node *) expr, 1, &attrs_used);
410432

411-
resultRelInfo->ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
412-
resultRelInfo->ri_NumGeneratedNeeded++;
433+
if (!bms_overlap(updatedCols, attrs_used))
434+
continue; /* need not update this column */
413435
}
414-
}
415436

416-
MemoryContextSwitchTo(oldContext);
437+
/* No luck, so prepare the expression for execution */
438+
resultRelInfo->ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
439+
resultRelInfo->ri_NumGeneratedNeeded++;
440+
441+
/* And mark this column in rextra->ri_extraUpdatedCols */
442+
rextra->ri_extraUpdatedCols =
443+
bms_add_member(rextra->ri_extraUpdatedCols,
444+
i + 1 - FirstLowInvalidHeapAttributeNumber);
445+
}
417446
}
418447

448+
MemoryContextSwitchTo(oldContext);
449+
}
450+
451+
/*
452+
* Compute stored generated columns for a tuple
453+
*/
454+
void
455+
ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo,
456+
EState *estate, TupleTableSlot *slot,
457+
CmdType cmdtype)
458+
{
459+
Relation rel = resultRelInfo->ri_RelationDesc;
460+
TupleDesc tupdesc = RelationGetDescr(rel);
461+
int natts = tupdesc->natts;
462+
ExprContext *econtext = GetPerTupleExprContext(estate);
463+
MemoryContext oldContext;
464+
Datum *values;
465+
bool *nulls;
466+
467+
/* We should not be called unless this is true */
468+
Assert(tupdesc->constr && tupdesc->constr->has_generated_stored);
469+
470+
/*
471+
* For relations named directly in the query, ExecInitStoredGenerated
472+
* should have been called already; but this might not have happened yet
473+
* for a partition child rel. Also, it's convenient for outside callers
474+
* to not have to call ExecInitStoredGenerated explicitly.
475+
*/
476+
if (resultRelInfo->ri_GeneratedExprs == NULL)
477+
ExecInitStoredGenerated(resultRelInfo, estate, cmdtype);
478+
419479
/*
420480
* If no generated columns have been affected by this change, then skip
421481
* the rest.
@@ -435,14 +495,13 @@ ExecComputeStoredGenerated(ResultRelInfo *resultRelInfo,
435495
{
436496
Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
437497

438-
if (attr->attgenerated == ATTRIBUTE_GENERATED_STORED &&
439-
resultRelInfo->ri_GeneratedExprs[i])
498+
if (resultRelInfo->ri_GeneratedExprs[i])
440499
{
441-
ExprContext *econtext;
442500
Datum val;
443501
bool isnull;
444502

445-
econtext = GetPerTupleExprContext(estate);
503+
Assert(attr->attgenerated == ATTRIBUTE_GENERATED_STORED);
504+
446505
econtext->ecxt_scantuple = slot;
447506

448507
val = ExecEvalExpr(resultRelInfo->ri_GeneratedExprs[i], econtext, &isnull);
@@ -4088,6 +4147,15 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
40884147
elog(ERROR, "could not find junk wholerow column");
40894148
}
40904149
}
4150+
4151+
/*
4152+
* For INSERT and UPDATE, prepare to evaluate any generated columns.
4153+
* We must do this now, even if we never insert or update any rows,
4154+
* because we have to fill resultRelInfo->ri_extraUpdatedCols for
4155+
* possible use by the trigger machinery.
4156+
*/
4157+
if (operation == CMD_INSERT || operation == CMD_UPDATE)
4158+
ExecInitStoredGenerated(resultRelInfo, estate, operation);
40914159
}
40924160

40934161
/*

0 commit comments

Comments
 (0)