Fix calculation of which GENERATED columns need to be updated.
authorTom Lane
Thu, 5 Jan 2023 19:12:17 +0000 (14:12 -0500)
committerTom Lane
Thu, 5 Jan 2023 19:12:17 +0000 (14:12 -0500)
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 4b3e37993, 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

14 files changed:
contrib/postgres_fdw/postgres_fdw.c
src/backend/executor/execUtils.c
src/backend/executor/nodeModifyTable.c
src/backend/optimizer/util/inherit.c
src/backend/optimizer/util/plancat.c
src/backend/replication/logical/worker.c
src/backend/rewrite/rewriteHandler.c
src/include/nodes/execnodes.h
src/include/nodes/parsenodes.h
src/include/optimizer/inherit.h
src/include/optimizer/plancat.h
src/include/rewrite/rewriteHandler.h
src/test/regress/expected/generated.out
src/test/regress/sql/generated.sql

index 3b0145207b065c4f92601b673a0359b952ec8529..e8bcfec22ba2472ccfec286ddef0c74a625bba8a 100644 (file)
@@ -29,6 +29,7 @@
 #include "nodes/nodeFuncs.h"
 #include "optimizer/clauses.h"
 #include "optimizer/cost.h"
+#include "optimizer/inherit.h"
 #include "optimizer/optimizer.h"
 #include "optimizer/pathnode.h"
 #include "optimizer/paths.h"
@@ -1697,7 +1698,8 @@ postgresPlanForeignModify(PlannerInfo *root,
    else if (operation == CMD_UPDATE)
    {
        int         col;
-       Bitmapset  *allUpdatedCols = bms_union(rte->updatedCols, rte->extraUpdatedCols);
+       RelOptInfo *rel = find_base_rel(root, resultRelation);
+       Bitmapset  *allUpdatedCols = get_rel_all_updated_cols(root, rel);
 
        col = -1;
        while ((col = bms_next_member(allUpdatedCols, col)) >= 0)
index fe4925f506e579ac15dea81b7e62902399fee61a..5b04b0803f3288bf7d3663165b41bb11bb9db02f 100644 (file)
@@ -135,6 +135,8 @@ CreateExecutorState(void)
 
    estate->es_trig_target_relations = NIL;
 
+   estate->es_resultrelinfo_extra = NIL;
+
    estate->es_param_list_info = NULL;
    estate->es_param_exec_vals = NULL;
 
@@ -1276,27 +1278,25 @@ ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate)
 Bitmapset *
 ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate)
 {
-   /* see ExecGetInsertedCols() */
-   if (relinfo->ri_RangeTableIndex != 0)
-   {
-       RangeTblEntry *rte = exec_rt_fetch(relinfo->ri_RangeTableIndex, estate);
+   Relation    rel = relinfo->ri_RelationDesc;
+   TupleDesc   tupdesc = RelationGetDescr(rel);
 
-       return rte->extraUpdatedCols;
-   }
-   else if (relinfo->ri_RootResultRelInfo)
+   if (tupdesc->constr && tupdesc->constr->has_generated_stored)
    {
-       ResultRelInfo *rootRelInfo = relinfo->ri_RootResultRelInfo;
-       RangeTblEntry *rte = exec_rt_fetch(rootRelInfo->ri_RangeTableIndex, estate);
-       PartitionRoutingInfo *partrouteinfo = relinfo->ri_PartitionInfo;
+       ListCell   *lc;
 
-       if (partrouteinfo->pi_RootToPartitionMap != NULL)
-           return execute_attr_map_cols(partrouteinfo->pi_RootToPartitionMap->attrMap,
-                                        rte->extraUpdatedCols);
-       else
-           return rte->extraUpdatedCols;
+       /* Assert that ExecInitStoredGenerated has been called. */
+       Assert(relinfo->ri_GeneratedExprs != NULL);
+       foreach(lc, estate->es_resultrelinfo_extra)
+       {
+           ResultRelInfoExtra *rextra = (ResultRelInfoExtra *) lfirst(lc);
+
+           if (rextra->rinfo == relinfo)
+               return rextra->ri_extraUpdatedCols;
+       }
+       Assert(false);          /* shouldn't get here */
    }
-   else
-       return NULL;
+   return NULL;
 }
 
 /* Return columns being updated, including generated columns */
index 52c48afe3ffc55b4771383a9bc8fb0f6871a1a0d..74ef7ce5d9c27530443a532f8a6f4506b41b5706 100644 (file)
@@ -49,6 +49,7 @@
 #include "foreign/fdwapi.h"
 #include "miscadmin.h"
 #include "nodes/nodeFuncs.h"
+#include "optimizer/optimizer.h"
 #include "rewrite/rewriteHandler.h"
 #include "storage/bufmgr.h"
 #include "storage/lmgr.h"
@@ -249,68 +250,127 @@ ExecCheckTIDVisible(EState *estate,
 }
 
 /*
- * Compute stored generated columns for a tuple
+ * Initialize to compute stored generated columns for a tuple
+ *
+ * This fills the resultRelInfo's ri_GeneratedExprs field and makes an
+ * associated ResultRelInfoExtra struct to hold ri_extraUpdatedCols.
+ * (Currently, ri_extraUpdatedCols is consulted only in UPDATE, but we might
+ * as well fill it for INSERT too.)
  */
-void
-ExecComputeStoredGenerated(EState *estate, TupleTableSlot *slot, CmdType cmdtype)
+static void
+ExecInitStoredGenerated(ResultRelInfo *resultRelInfo,
+                       EState *estate,
+                       CmdType cmdtype)
 {
-   ResultRelInfo *resultRelInfo = estate->es_result_relation_info;
    Relation    rel = resultRelInfo->ri_RelationDesc;
    TupleDesc   tupdesc = RelationGetDescr(rel);
    int         natts = tupdesc->natts;
+   Bitmapset  *updatedCols;
+   ResultRelInfoExtra *rextra;
    MemoryContext oldContext;
-   Datum      *values;
-   bool       *nulls;
 
-   Assert(tupdesc->constr && tupdesc->constr->has_generated_stored);
+   /* Don't call twice */
+   Assert(resultRelInfo->ri_GeneratedExprs == NULL);
+
+   /* Nothing to do if no generated columns */
+   if (!(tupdesc->constr && tupdesc->constr->has_generated_stored))
+       return;
 
    /*
-    * If first time through for this result relation, build expression
-    * nodetrees for rel's stored generation expressions.  Keep them in the
-    * per-query memory context so they'll survive throughout the query.
+    * In an UPDATE, we can skip computing any generated columns that do not
+    * depend on any UPDATE target column.  But if there is a BEFORE ROW
+    * UPDATE trigger, we cannot skip because the trigger might change more
+    * columns.
     */
-   if (resultRelInfo->ri_GeneratedExprs == NULL)
-   {
-       oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
+   if (cmdtype == CMD_UPDATE &&
+       !(rel->trigdesc && rel->trigdesc->trig_update_before_row))
+       updatedCols = ExecGetUpdatedCols(resultRelInfo, estate);
+   else
+       updatedCols = NULL;
 
-       resultRelInfo->ri_GeneratedExprs =
-           (ExprState **) palloc(natts * sizeof(ExprState *));
-       resultRelInfo->ri_NumGeneratedNeeded = 0;
+   /*
+    * Make sure these data structures are built in the per-query memory
+    * context so they'll survive throughout the query.
+    */
+   oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
+
+   resultRelInfo->ri_GeneratedExprs =
+       (ExprState **) palloc0(natts * sizeof(ExprState *));
+   resultRelInfo->ri_NumGeneratedNeeded = 0;
+
+   rextra = palloc_object(ResultRelInfoExtra);
+   rextra->rinfo = resultRelInfo;
+   rextra->ri_extraUpdatedCols = NULL;
+   estate->es_resultrelinfo_extra = lappend(estate->es_resultrelinfo_extra,
+                                            rextra);
 
-       for (int i = 0; i < natts; i++)
+   for (int i = 0; i < natts; i++)
+   {
+       if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
        {
-           if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
-           {
-               Expr       *expr;
+           Expr       *expr;
 
-               /*
-                * If it's an update and the current column was not marked as
-                * being updated, then we can skip the computation.  But if
-                * there is a BEFORE ROW UPDATE trigger, we cannot skip
-                * because the trigger might affect additional columns.
-                */
-               if (cmdtype == CMD_UPDATE &&
-                   !(rel->trigdesc && rel->trigdesc->trig_update_before_row) &&
-                   !bms_is_member(i + 1 - FirstLowInvalidHeapAttributeNumber,
-                                  ExecGetExtraUpdatedCols(resultRelInfo, estate)))
-               {
-                   resultRelInfo->ri_GeneratedExprs[i] = NULL;
-                   continue;
-               }
+           /* Fetch the GENERATED AS expression tree */
+           expr = (Expr *) build_column_default(rel, i + 1);
+           if (expr == NULL)
+               elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
+                    i + 1, RelationGetRelationName(rel));
+
+           /*
+            * If it's an update with a known set of update target columns,
+            * see if we can skip the computation.
+            */
+           if (updatedCols)
+           {
+               Bitmapset  *attrs_used = NULL;
 
-               expr = (Expr *) build_column_default(rel, i + 1);
-               if (expr == NULL)
-                   elog(ERROR, "no generation expression found for column number %d of table \"%s\"",
-                        i + 1, RelationGetRelationName(rel));
+               pull_varattnos((Node *) expr, 1, &attrs_used);
 
-               resultRelInfo->ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
-               resultRelInfo->ri_NumGeneratedNeeded++;
+               if (!bms_overlap(updatedCols, attrs_used))
+                   continue;   /* need not update this column */
            }
-       }
 
-       MemoryContextSwitchTo(oldContext);
+           /* No luck, so prepare the expression for execution */
+           resultRelInfo->ri_GeneratedExprs[i] = ExecPrepareExpr(expr, estate);
+           resultRelInfo->ri_NumGeneratedNeeded++;
+
+           /* And mark this column in rextra->ri_extraUpdatedCols */
+           rextra->ri_extraUpdatedCols =
+               bms_add_member(rextra->ri_extraUpdatedCols,
+                              i + 1 - FirstLowInvalidHeapAttributeNumber);
+       }
    }
 
+   MemoryContextSwitchTo(oldContext);
+}
+
+/*
+ * Compute stored generated columns for a tuple
+ */
+void
+ExecComputeStoredGenerated(EState *estate, TupleTableSlot *slot, CmdType cmdtype)
+{
+   ResultRelInfo *resultRelInfo = estate->es_result_relation_info;
+   Relation    rel = resultRelInfo->ri_RelationDesc;
+   TupleDesc   tupdesc = RelationGetDescr(rel);
+   int         natts = tupdesc->natts;
+   ExprContext *econtext = GetPerTupleExprContext(estate);
+   MemoryContext oldContext;
+   Datum      *values;
+   bool       *nulls;
+
+   /* We should not be called unless this is true */
+   Assert(tupdesc->constr && tupdesc->constr->has_generated_stored);
+
+   /*
+    * For relations named directly in the query, ExecInitStoredGenerated
+    * should have been called already; but this might not have happened yet
+    * for a partition child rel.  Also, it's convenient for outside callers
+    * to not have to call ExecInitStoredGenerated explicitly.
+    */
+   if (resultRelInfo->ri_GeneratedExprs == NULL)
+       ExecInitStoredGenerated(resultRelInfo, estate, cmdtype);
+
    /*
     * If no generated columns have been affected by this change, then skip
     * the rest.
@@ -330,14 +390,13 @@ ExecComputeStoredGenerated(EState *estate, TupleTableSlot *slot, CmdType cmdtype
    {
        Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
 
-       if (attr->attgenerated == ATTRIBUTE_GENERATED_STORED &&
-           resultRelInfo->ri_GeneratedExprs[i])
+       if (resultRelInfo->ri_GeneratedExprs[i])
        {
-           ExprContext *econtext;
            Datum       val;
            bool        isnull;
 
-           econtext = GetPerTupleExprContext(estate);
+           Assert(attr->attgenerated == ATTRIBUTE_GENERATED_STORED);
+
            econtext->ecxt_scantuple = slot;
 
            val = ExecEvalExpr(resultRelInfo->ri_GeneratedExprs[i], econtext, &isnull);
@@ -2488,6 +2547,15 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
                                                             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.
+        */
+       if (operation == CMD_INSERT || operation == CMD_UPDATE)
+           ExecInitStoredGenerated(resultRelInfo, estate, operation);
+
        resultRelInfo++;
        i++;
    }
index d5f1e125a9382a66a56c8b16fc629ad8a0b19644..bd1eba2ff79c92899489679cc91c382ead40c2b8 100644 (file)
@@ -25,6 +25,7 @@
 #include "optimizer/inherit.h"
 #include "optimizer/optimizer.h"
 #include "optimizer/pathnode.h"
+#include "optimizer/plancat.h"
 #include "optimizer/planmain.h"
 #include "optimizer/planner.h"
 #include "optimizer/prep.h"
@@ -50,6 +51,10 @@ static void expand_single_inheritance_child(PlannerInfo *root,
                                            Index *childRTindex_p);
 static Bitmapset *translate_col_privs(const Bitmapset *parent_privs,
                                      List *translated_vars);
+static Bitmapset *translate_col_privs_multilevel(PlannerInfo *root,
+                                                RelOptInfo *rel,
+                                                RelOptInfo *parent_rel,
+                                                Bitmapset *parent_cols);
 static void expand_appendrel_subquery(PlannerInfo *root, RelOptInfo *rel,
                                      RangeTblEntry *rte, Index rti);
 
@@ -332,11 +337,6 @@ expand_partitioned_rtentry(PlannerInfo *root, RelOptInfo *relinfo,
        root->partColsUpdated =
            has_partition_attrs(parentrel, parentrte->updatedCols, NULL);
 
-   /*
-    * There shouldn't be any generated columns in the partition key.
-    */
-   Assert(!has_partition_attrs(parentrel, parentrte->extraUpdatedCols, NULL));
-
    /* Nothing further to do here if there are no partitions. */
    if (partdesc->nparts == 0)
        return;
@@ -555,15 +555,12 @@ expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte,
                                                     appinfo->translated_vars);
        childrte->updatedCols = translate_col_privs(parentrte->updatedCols,
                                                    appinfo->translated_vars);
-       childrte->extraUpdatedCols = translate_col_privs(parentrte->extraUpdatedCols,
-                                                        appinfo->translated_vars);
    }
    else
    {
        childrte->selectedCols = bms_copy(parentrte->selectedCols);
        childrte->insertedCols = bms_copy(parentrte->insertedCols);
        childrte->updatedCols = bms_copy(parentrte->updatedCols);
-       childrte->extraUpdatedCols = bms_copy(parentrte->extraUpdatedCols);
    }
 
    /*
@@ -607,6 +604,52 @@ expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte,
    }
 }
 
+/*
+ * get_rel_all_updated_cols
+ *         Returns the set of columns of a given "simple" relation that are
+ *         updated by this query.
+ */
+Bitmapset *
+get_rel_all_updated_cols(PlannerInfo *root, RelOptInfo *rel)
+{
+   Index       relid;
+   RangeTblEntry *rte;
+   Bitmapset  *updatedCols,
+              *extraUpdatedCols;
+
+   Assert(root->parse->commandType == CMD_UPDATE);
+   Assert(IS_SIMPLE_REL(rel));
+
+   /*
+    * We obtain updatedCols for the query's result relation.  Then, if
+    * necessary, we map it to the column numbers of the relation for which
+    * they were requested.
+    */
+   relid = root->parse->resultRelation;
+   rte = planner_rt_fetch(relid, root);
+
+   updatedCols = rte->updatedCols;
+
+   if (rel->relid != relid)
+   {
+       RelOptInfo *top_parent_rel = find_base_rel(root, relid);
+
+       Assert(IS_OTHER_REL(rel));
+
+       updatedCols = translate_col_privs_multilevel(root, rel, top_parent_rel,
+                                                    updatedCols);
+   }
+
+   /*
+    * Now we must check to see if there are any generated columns that depend
+    * on the updatedCols, and add them to the result.
+    */
+   extraUpdatedCols = get_dependent_generated_columns(root, rel->relid,
+                                                      updatedCols);
+
+   return bms_union(updatedCols, extraUpdatedCols);
+}
+
 /*
  * translate_col_privs
  *   Translate a bitmapset representing per-column privileges from the
@@ -659,6 +702,44 @@ translate_col_privs(const Bitmapset *parent_privs,
    return child_privs;
 }
 
+/*
+ * translate_col_privs_multilevel
+ *     Recursively translates the column numbers contained in 'parent_cols'
+ *     to the column numbers of a descendant relation given by 'rel'
+ *
+ * Note that because this is based on translate_col_privs, it will expand
+ * a whole-row reference into all inherited columns.  This is not an issue
+ * for current usages, but beware.
+ */
+static Bitmapset *
+translate_col_privs_multilevel(PlannerInfo *root, RelOptInfo *rel,
+                              RelOptInfo *parent_rel,
+                              Bitmapset *parent_cols)
+{
+   AppendRelInfo *appinfo;
+
+   /* Fast path for easy case. */
+   if (parent_cols == NULL)
+       return NULL;
+
+   Assert(root->append_rel_array != NULL);
+   appinfo = root->append_rel_array[rel->relid];
+   Assert(appinfo != NULL);
+
+   /* Recurse if immediate parent is not the top parent. */
+   if (appinfo->parent_relid != parent_rel->relid)
+   {
+       RelOptInfo *next_parent = find_base_rel(root, appinfo->parent_relid);
+
+       parent_cols = translate_col_privs_multilevel(root, next_parent,
+                                                    parent_rel,
+                                                    parent_cols);
+   }
+
+   /* Now translate for this child. */
+   return translate_col_privs(parent_cols, appinfo->translated_vars);
+}
+
 /*
  * expand_appendrel_subquery
  *     Add "other rel" RelOptInfos for the children of an appendrel baserel
index 063c6fa8942467b738855ed30ed0ca0983dd613f..f7ce44cb22a0c0fb0393fc9654a14b775e904a90 100644 (file)
@@ -2115,6 +2115,11 @@ has_row_triggers(PlannerInfo *root, Index rti, CmdType event)
    return result;
 }
 
+/*
+ * has_stored_generated_columns
+ *
+ * Does table identified by RTI have any STORED GENERATED columns?
+ */
 bool
 has_stored_generated_columns(PlannerInfo *root, Index rti)
 {
@@ -2134,6 +2139,57 @@ has_stored_generated_columns(PlannerInfo *root, Index rti)
    return result;
 }
 
+/*
+ * get_dependent_generated_columns
+ *
+ * Get the column numbers of any STORED GENERATED columns of the relation
+ * that depend on any column listed in target_cols.  Both the input and
+ * result bitmapsets contain column numbers offset by
+ * FirstLowInvalidHeapAttributeNumber.
+ */
+Bitmapset *
+get_dependent_generated_columns(PlannerInfo *root, Index rti,
+                               Bitmapset *target_cols)
+{
+   Bitmapset  *dependentCols = NULL;
+   RangeTblEntry *rte = planner_rt_fetch(rti, root);
+   Relation    relation;
+   TupleDesc   tupdesc;
+   TupleConstr *constr;
+
+   /* Assume we already have adequate lock */
+   relation = table_open(rte->relid, NoLock);
+
+   tupdesc = RelationGetDescr(relation);
+   constr = tupdesc->constr;
+
+   if (constr && constr->has_generated_stored)
+   {
+       for (int i = 0; i < constr->num_defval; i++)
+       {
+           AttrDefault *defval = &constr->defval[i];
+           Node       *expr;
+           Bitmapset  *attrs_used = NULL;
+
+           /* skip if not generated column */
+           if (!TupleDescAttr(tupdesc, defval->adnum - 1)->attgenerated)
+               continue;
+
+           /* identify columns this generated column depends on */
+           expr = stringToNode(defval->adbin);
+           pull_varattnos(expr, 1, &attrs_used);
+
+           if (bms_overlap(target_cols, attrs_used))
+               dependentCols = bms_add_member(dependentCols,
+                                              defval->adnum - FirstLowInvalidHeapAttributeNumber);
+       }
+   }
+
+   table_close(relation, NoLock);
+
+   return dependentCols;
+}
+
 /*
  * set_relation_partition_info
  *
index 0cd61d04a806d52af80df6e0417bd061fabb1b30..a0e64301a94cfbbafc53c4573992935ec214d7ab 100644 (file)
@@ -830,9 +830,6 @@ apply_handle_update(StringInfo s)
        }
    }
 
-   /* Also populate extraUpdatedCols, in case we have generated columns */
-   fill_extraUpdatedCols(target_rte, rel->localrel);
-
    /* Build the search tuple. */
    oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
    slot_store_cstrings(remoteslot, rel,
index d8a98cb7e4395bdf4ced711383505b36d20b7fea..593c533b449aa7e266312d51d7c524e228cca506 100644 (file)
@@ -1574,43 +1574,6 @@ rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte,
    }
 }
 
-/*
- * Record in target_rte->extraUpdatedCols the indexes of any generated columns
- * that depend on any columns mentioned in target_rte->updatedCols.
- */
-void
-fill_extraUpdatedCols(RangeTblEntry *target_rte, Relation target_relation)
-{
-   TupleDesc   tupdesc = RelationGetDescr(target_relation);
-   TupleConstr *constr = tupdesc->constr;
-
-   target_rte->extraUpdatedCols = NULL;
-
-   if (constr && constr->has_generated_stored)
-   {
-       for (int i = 0; i < constr->num_defval; i++)
-       {
-           AttrDefault *defval = &constr->defval[i];
-           Node       *expr;
-           Bitmapset  *attrs_used = NULL;
-
-           /* skip if not generated column */
-           if (!TupleDescAttr(tupdesc, defval->adnum - 1)->attgenerated)
-               continue;
-
-           /* identify columns this generated column depends on */
-           expr = stringToNode(defval->adbin);
-           pull_varattnos(expr, 1, &attrs_used);
-
-           if (bms_overlap(target_rte->updatedCols, attrs_used))
-               target_rte->extraUpdatedCols =
-                   bms_add_member(target_rte->extraUpdatedCols,
-                                  defval->adnum - FirstLowInvalidHeapAttributeNumber);
-       }
-   }
-}
-
-
 /*
  * matchLocks -
  *   match the list of locks and returns the matching rules
@@ -3746,9 +3709,6 @@ RewriteQuery(Query *parsetree, List *rewrite_events, int orig_rt_length)
                                    parsetree->override,
                                    rt_entry_relation,
                                    parsetree->resultRelation);
-
-           /* Also populate extraUpdatedCols (for generated columns) */
-           fill_extraUpdatedCols(rt_entry, rt_entry_relation);
        }
        else if (event == CMD_DELETE)
        {
index a9a45f566c4836485003ead3e03e78dcfeb20ab6..caf0d41f0bb3b470b543f689c0b044d6d4505010 100644 (file)
@@ -501,6 +501,19 @@ typedef struct ResultRelInfo
    struct CopyMultiInsertBuffer *ri_CopyMultiInsertBuffer;
 } ResultRelInfo;
 
+/*
+ * To avoid an ABI-breaking change in the size of ResultRelInfo in back
+ * branches, we create one of these for each result relation for which we've
+ * computed extraUpdatedCols, and store it in EState.es_resultrelinfo_extra.
+ */
+typedef struct ResultRelInfoExtra
+{
+   ResultRelInfo *rinfo;       /* owning ResultRelInfo */
+
+   /* For INSERT/UPDATE, attnums of generated columns to be computed */
+   Bitmapset  *ri_extraUpdatedCols;
+} ResultRelInfoExtra;
+
 /* ----------------
  *   EState information
  *
@@ -608,6 +621,9 @@ typedef struct EState
    int         es_jit_flags;
    struct JitContext *es_jit;
    struct JitInstrumentation *es_jit_worker_instr;
+
+   /* List of ResultRelInfoExtra structs (see above) */
+   List       *es_resultrelinfo_extra;
 } EState;
 
 
index 943848782f40e853e35bda5809d1357d2d75ebd6..9cb915275ed8573180ba14c69c1603ce97ca3566 100644 (file)
@@ -942,14 +942,7 @@ typedef struct PartitionCmd
  *   which triggers to fire and in FDWs to know which changed columns they
  *   need to ship off.
  *
- *   Generated columns that are caused to be updated by an update to a base
- *   column are listed in extraUpdatedCols.  This is not considered for
- *   permission checking, but it is useful in those places that want to know
- *   the full set of columns being updated as opposed to only the ones the
- *   user explicitly mentioned in the query.  (There is currently no need for
- *   an extraInsertedCols, but it could exist.)  Note that extraUpdatedCols
- *   is populated during query rewrite, NOT in the parser, since generated
- *   columns could be added after a rule has been parsed and stored.
+ *   extraUpdatedCols is no longer used or maintained; it's always empty.
  *
  *   securityQuals is a list of security barrier quals (boolean expressions),
  *   to be tested in the listed order before returning a row from the
index 0ba61326528d0d9bc7c715638fcd6518ba4997b7..1fe59db39eefb3b0a7f9d5239d1859c0944fcb5d 100644 (file)
@@ -20,6 +20,8 @@
 extern void expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel,
                                     RangeTblEntry *rte, Index rti);
 
+extern Bitmapset *get_rel_all_updated_cols(PlannerInfo *root, RelOptInfo *rel);
+
 extern bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
                                  RelOptInfo *childrel, RangeTblEntry *childRTE,
                                  AppendRelInfo *appinfo);
index c29a7091ec04a8e871af19075be72bc8062962a8..5ef93331d7e0274da57c93c69ad65fee0328353b 100644 (file)
@@ -74,4 +74,7 @@ extern bool has_row_triggers(PlannerInfo *root, Index rti, CmdType event);
 
 extern bool has_stored_generated_columns(PlannerInfo *root, Index rti);
 
+extern Bitmapset *get_dependent_generated_columns(PlannerInfo *root, Index rti,
+                                                 Bitmapset *target_cols);
+
 #endif                         /* PLANCAT_H */
index a18211f4a23cef0566d25a62e791459969a5e696..eb2e7b1768ac8a1e468e093cbfdee6b2fef97b2f 100644 (file)
@@ -26,9 +26,6 @@ extern Node *build_column_default(Relation rel, int attrno);
 extern void rewriteTargetListUD(Query *parsetree, RangeTblEntry *target_rte,
                                Relation target_relation);
 
-extern void fill_extraUpdatedCols(RangeTblEntry *target_rte,
-                                 Relation target_relation);
-
 extern Query *get_view_query(Relation view);
 extern const char *view_query_is_auto_updatable(Query *viewquery,
                                                bool check_cols);
index 6a6b77677f71045e0757cf8d82786c02662006e4..8d5f91b43dfabd7a06ab34f9c9d1bdf3e85ce681 100644 (file)
@@ -296,6 +296,25 @@ CREATE TABLE gtest1_2 () INHERITS (gtest0, gtesty);  -- error
 NOTICE:  merging multiple inherited definitions of column "b"
 ERROR:  inherited column "b" has a generation conflict
 DROP TABLE gtesty;
+-- test correct handling of GENERATED column that's only in child
+CREATE TABLE gtestp (f1 int);
+CREATE TABLE gtestc (f2 int GENERATED ALWAYS AS (f1+1) STORED) INHERITS(gtestp);
+INSERT INTO gtestc values(42);
+TABLE gtestc;
+ f1 | f2 
+----+----
+ 42 | 43
+(1 row)
+
+UPDATE gtestp SET f1 = f1 * 10;
+TABLE gtestc;
+ f1  | f2  
+-----+-----
+ 420 | 421
+(1 row)
+
+DROP TABLE gtestp CASCADE;
+NOTICE:  drop cascades to table gtestc
 -- test stored update
 CREATE TABLE gtest3 (a int, b int GENERATED ALWAYS AS (a * 3) STORED);
 INSERT INTO gtest3 (a) VALUES (1), (2), (3), (NULL);
index 58a7d4104fc4d6081f909270947d738b5bc06bee..133147cebbd67a342769fa1f0c74363f04ecf19a 100644 (file)
@@ -132,6 +132,15 @@ CREATE TABLE gtesty (x int, b int DEFAULT 55);
 CREATE TABLE gtest1_2 () INHERITS (gtest0, gtesty);  -- error
 DROP TABLE gtesty;
 
+-- test correct handling of GENERATED column that's only in child
+CREATE TABLE gtestp (f1 int);
+CREATE TABLE gtestc (f2 int GENERATED ALWAYS AS (f1+1) STORED) INHERITS(gtestp);
+INSERT INTO gtestc values(42);
+TABLE gtestc;
+UPDATE gtestp SET f1 = f1 * 10;
+TABLE gtestc;
+DROP TABLE gtestp CASCADE;
+
 -- test stored update
 CREATE TABLE gtest3 (a int, b int GENERATED ALWAYS AS (a * 3) STORED);
 INSERT INTO gtest3 (a) VALUES (1), (2), (3), (NULL);