Make new GENERATED-expressions code more bulletproof.
authorTom Lane
Sun, 15 Jan 2023 19:06:46 +0000 (14:06 -0500)
committerTom Lane
Sun, 15 Jan 2023 19:06:46 +0000 (14:06 -0500)
In commit 8bf6ec3ba I assumed that no code path could reach
ExecGetExtraUpdatedCols without having gone through
ExecInitStoredGenerated.  That turns out not to be the case in
logical replication: if there's an ON UPDATE trigger on the target
table, trigger.c will call this code before anybody has set up its
generated columns.  Having seen that, I don't have a lot of faith in
there not being other such paths.  ExecGetExtraUpdatedCols can call
ExecInitStoredGenerated for itself, as long as we are willing to
assume that it is only called in CMD_UPDATE operations, which on
the whole seems like a safer leap of faith.

Per report from Vitaly Davydov.

Discussion: https://postgr.es/m/d259d69652b8c2ff50e14cda3c236c7f@postgrespro.ru

src/backend/executor/execUtils.c
src/backend/executor/nodeModifyTable.c
src/include/executor/nodeModifyTable.h
src/test/subscription/t/011_generated.pl

index 5b04b0803f3288bf7d3663165b41bb11bb9db02f..568ee34e00c346a49eebdaf46bb7aeaa8826acd2 100644 (file)
@@ -52,6 +52,7 @@
 #include "access/transam.h"
 #include "executor/executor.h"
 #include "executor/execPartition.h"
+#include "executor/nodeModifyTable.h"
 #include "jit/jit.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
@@ -1285,8 +1286,9 @@ ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate)
    {
        ListCell   *lc;
 
-       /* Assert that ExecInitStoredGenerated has been called. */
-       Assert(relinfo->ri_GeneratedExprs != NULL);
+       /* In some code paths we can reach here before initializing the info */
+       if (relinfo->ri_GeneratedExprs == NULL)
+           ExecInitStoredGenerated(relinfo, estate, CMD_UPDATE);
        foreach(lc, estate->es_resultrelinfo_extra)
        {
            ResultRelInfoExtra *rextra = (ResultRelInfoExtra *) lfirst(lc);
index 74ef7ce5d9c27530443a532f8a6f4506b41b5706..eac9291149e2016ffb58fc1a8c4bc023804469a2 100644 (file)
@@ -257,7 +257,7 @@ ExecCheckTIDVisible(EState *estate,
  * (Currently, ri_extraUpdatedCols is consulted only in UPDATE, but we might
  * as well fill it for INSERT too.)
  */
-static void
+void
 ExecInitStoredGenerated(ResultRelInfo *resultRelInfo,
                        EState *estate,
                        CmdType cmdtype)
index 4ec4ebdabc13e81cac9fb3d04e313fc0a89a2247..4c84a269f7628eb098813c3ab195fa039c5df99d 100644 (file)
 
 #include "nodes/execnodes.h"
 
+extern void ExecInitStoredGenerated(ResultRelInfo *resultRelInfo,
+                                   EState *estate,
+                                   CmdType cmdtype);
+
 extern void ExecComputeStoredGenerated(EState *estate, TupleTableSlot *slot, CmdType cmdtype);
 
 extern ModifyTableState *ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags);
index e4b3b41db7fdef168b4c72835dd1803c5d76f95b..351634246bedd97d60efdfa8051c89a9e02cadd9 100644 (file)
@@ -3,7 +3,7 @@ use strict;
 use warnings;
 use PostgresNode;
 use TestLib;
-use Test::More tests => 2;
+use Test::More tests => 3;
 
 # setup
 
@@ -22,7 +22,7 @@ $node_publisher->safe_psql('postgres',
 );
 
 $node_subscriber->safe_psql('postgres',
-   "CREATE TABLE tab1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 22) STORED)"
+   "CREATE TABLE tab1 (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 22) STORED, c int)"
 );
 
 # data for initial sync
@@ -52,9 +52,43 @@ $node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 6 WHERE a = 5");
 
 $node_publisher->wait_for_catchup('sub1');
 
-$result = $node_subscriber->safe_psql('postgres', "SELECT a, b FROM tab1");
-is( $result, qq(1|22
-2|44
-3|66
-4|88
-6|132), 'generated columns replicated');
+$result = $node_subscriber->safe_psql('postgres', "SELECT * FROM tab1");
+is( $result, qq(1|22|
+2|44|
+3|66|
+4|88|
+6|132|), 'generated columns replicated');
+
+# try it with a subscriber-side trigger
+
+$node_subscriber->safe_psql(
+   'postgres', q{
+CREATE FUNCTION tab1_trigger_func() RETURNS trigger
+LANGUAGE plpgsql AS $$
+BEGIN
+  NEW.c := NEW.a + 10;
+  RETURN NEW;
+END $$;
+
+CREATE TRIGGER test1 BEFORE INSERT OR UPDATE ON tab1
+  FOR EACH ROW
+  EXECUTE PROCEDURE tab1_trigger_func();
+
+ALTER TABLE tab1 ENABLE REPLICA TRIGGER test1;
+});
+
+$node_publisher->safe_psql('postgres', "INSERT INTO tab1 VALUES (7), (8)");
+
+$node_publisher->safe_psql('postgres', "UPDATE tab1 SET a = 9 WHERE a = 7");
+
+$node_publisher->wait_for_catchup('sub1');
+
+$result =
+  $node_subscriber->safe_psql('postgres', "SELECT * FROM tab1 ORDER BY 1");
+is( $result, qq(1|22|
+2|44|
+3|66|
+4|88|
+6|132|
+8|176|18
+9|198|19), 'generated columns replicated with trigger');