PS_NITPICKS_20240625_V100002.txt

text/plain

Filename: PS_NITPICKS_20240625_V100002.txt
Type: text/plain
Part: 0
Message: Re: Pgoutput not capturing the generated columns
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index b3fde6a..1bca40c 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -725,7 +725,7 @@ make_copy_attnamelist(LogicalRepRelMapEntry *rel, bool *remotegenlist)
 			* name as a non-generated column in the corresponding
 			* publication table.
 			*/
-			if(!remotegenlist[attnum])
+			if (!remotegenlist[attnum])
 				ereport(ERROR,
 						(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
 						 errmsg("logical replication target relation \"%s.%s\" has a generated column \"%s\" "
@@ -742,11 +742,12 @@ make_copy_attnamelist(LogicalRepRelMapEntry *rel, bool *remotegenlist)
 	}
 
 	/*
-	 * Construct column list for COPY.
+	 * Construct column list for COPY, excluding columns that are
+	 * subscription table generated columns.
 	 */
 	for (int i = 0; i < rel->remoterel.natts; i++)
 	{
-		if(!gencollist[i])
+		if (!gencollist[i])
 			attnamelist = lappend(attnamelist,
 								  makeString(rel->remoterel.attnames[i]));
 	}
@@ -1231,11 +1232,14 @@ copy_table(Relation rel)
 	else
 	{
 		/*
-		 * For non-tables and tables with row filters and when
-		 * 'include_generated_columns' is specified as 'true', we need to do
-		 * COPY (SELECT ...), as normal COPY of generated column is not
-		 * supported. For tables with any row filters, build a SELECT query
-		 * with OR'ed row filters for COPY.
+		 * For non-tables and tables with row filters, we need to do COPY
+		 * (SELECT ...), but we can't just do SELECT * because we need to not
+		 * copy generated columns. For tables with any row filters, build a
+		 * SELECT query with OR'ed row filters for COPY.
+		 *
+		 * We also need to use this same COPY (SELECT ...) syntax when
+		 * 'include_generated_columns' is specified as true, because copy
+		 * of generated columns is not supported by the normal COPY.
 		 */
 		int i = 0;
 
diff --git a/src/test/subscription/t/011_generated.pl b/src/test/subscription/t/011_generated.pl
index c47eaf5..0a3026c 100644
--- a/src/test/subscription/t/011_generated.pl
+++ b/src/test/subscription/t/011_generated.pl
@@ -63,8 +63,8 @@ $node_publisher->safe_psql('postgres', "CREATE TABLE tab5 (a int, b int)");
 $node_subscriber->safe_psql('postgres',
 	"CREATE TABLE tab5 (a int, b int GENERATED ALWAYS AS (a * 22) STORED)");
 
-# tab6: publisher-side generated col 'b' and 'c' --> subscriber-side non-generated col 'b', and generated-col 'c'
-# columns on subscriber in different order
+# tab6: publisher-side generated col 'b' and 'c' --> subscriber-side non-generated col 'b', and generated-col 'c',
+# where columns on the subscriber are in a different order
 $node_publisher->safe_psql('postgres',
 	"CREATE TABLE tab6 (a int, b int GENERATED ALWAYS AS (a * 2) STORED, c int GENERATED ALWAYS AS (a * 2) STORED)");