Fill in extraUpdatedCols in logical replication
authorPeter Eisentraut
Mon, 17 Feb 2020 14:19:58 +0000 (15:19 +0100)
committerPeter Eisentraut
Mon, 17 Feb 2020 14:21:12 +0000 (15:21 +0100)
The extraUpdatedCols field of the target RTE records which generated
columns are affected by an update.  This is used in a variety of
places, including per-column triggers and foreign data wrappers.  When
an update was initiated by a logical replication subscription, this
field was not filled in, so such an update would not affect generated
columns in a way that is consistent with normal updates.  To fix,
factor out some code from analyze.c to fill in extraUpdatedCols in the
logical replication worker as well.

Reviewed-by: Pavel Stehule
Discussion: https://www.postgresql.org/message-id/flat/b05e781a-fa16-6b52-6738-761181204567@2ndquadrant.com

src/backend/parser/analyze.c
src/backend/replication/logical/worker.c
src/include/parser/analyze.h

index 345a8e619778cf9746e6195c57c7c9dcf2591845..ecd6a8bae7da35d7514d54a6dd0ae5a41b1d654e 100644 (file)
@@ -2346,10 +2346,18 @@ transformUpdateTargetList(ParseState *pstate, List *origTlist)
    if (orig_tl != NULL)
        elog(ERROR, "UPDATE target count mismatch --- internal error");
 
-   /*
-    * Record in extraUpdatedCols generated columns referencing updated base
-    * columns.
-    */
+   fill_extraUpdatedCols(target_rte, tupdesc);
+
+   return tlist;
+}
+
+/*
+ * Record in extraUpdatedCols generated columns referencing updated base
+ * columns.
+ */
+void
+fill_extraUpdatedCols(RangeTblEntry *target_rte, TupleDesc tupdesc)
+{
    if (tupdesc->constr &&
        tupdesc->constr->has_generated_stored)
    {
@@ -2371,8 +2379,6 @@ transformUpdateTargetList(ParseState *pstate, List *origTlist)
                                                              defval.adnum - FirstLowInvalidHeapAttributeNumber);
        }
    }
-
-   return tlist;
 }
 
 /*
index a0384126399505414e8b1d302d7d8961264d4d29..790a28fc77e47c7024b8605448311aaa4325fb5b 100644 (file)
@@ -42,6 +42,7 @@
 #include "miscadmin.h"
 #include "nodes/makefuncs.h"
 #include "optimizer/optimizer.h"
+#include "parser/analyze.h"
 #include "parser/parse_relation.h"
 #include "pgstat.h"
 #include "postmaster/bgworker.h"
@@ -736,6 +737,8 @@ apply_handle_update(StringInfo s)
                                                     i + 1 - FirstLowInvalidHeapAttributeNumber);
    }
 
+   fill_extraUpdatedCols(target_rte, RelationGetDescr(rel->localrel));
+
    PushActiveSnapshot(GetTransactionSnapshot());
    ExecOpenIndices(estate->es_result_relation_info, false);
 
index cb1d96bc35d9d80c3f53eb98fe61fc05618ef90a..b3bad9716406666e9ee52e5f4298ddce90b6dda8 100644 (file)
@@ -46,4 +46,6 @@ extern void applyLockingClause(Query *qry, Index rtindex,
 extern List *BuildOnConflictExcludedTargetlist(Relation targetrel,
                                               Index exclRelIndex);
 
+extern void fill_extraUpdatedCols(RangeTblEntry *target_rte, TupleDesc tupdesc);
+
 #endif                         /* ANALYZE_H */