PS_NITPICKS_20240626_V100003.txt

text/plain

Filename: PS_NITPICKS_20240626_V100003.txt
Type: text/plain
Part: 0
Message: Re: Pgoutput not capturing the generated columns
diff --git a/contrib/test_decoding/test_decoding.c b/contrib/test_decoding/test_decoding.c
index abce99a..db584c8 100644
--- a/contrib/test_decoding/test_decoding.c
+++ b/contrib/test_decoding/test_decoding.c
@@ -557,14 +557,19 @@ tuple_to_stringinfo(StringInfo s, TupleDesc tupdesc, HeapTuple tuple,
 		if (attr->attisdropped)
 			continue;
 
-		/*
-		 * Don't print virtual generated column. Don't print stored
-		 * generated column if 'include_generated_columns' is false.
-		 *
-		 * TODO: can use ATTRIBUTE_GENERATED_VIRTUAL to simpilfy
-		 */
-		if (attr->attgenerated && (attr->attgenerated != ATTRIBUTE_GENERATED_STORED || !include_generated_columns))
-			continue;
+		if (attr->attgenerated)
+		{
+			/*
+			 * Don't print generated columns when
+			 * 'include_generated_columns' is false.
+			 */
+			if (!include_generated_columns)
+				continue;
+
+			/* Don't print generated columns unless they are STORED. */
+			if (attr->attgenerated != ATTRIBUTE_GENERATED_STORED)
+				continue;
+		}
 
 		/*
 		 * Don't print system columns, oid will already have been printed if
diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index e5e5aef..935ba81 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -521,6 +521,7 @@ publication_translate_columns(Relation targetrel, List *columns,
 	{
 		char	   *colname = strVal(lfirst(lc));
 		AttrNumber	attnum = get_attnum(RelationGetRelid(targetrel), colname);
+		Form_pg_attribute att;
 
 		if (attnum == InvalidAttrNumber)
 			ereport(ERROR,
@@ -534,11 +535,8 @@ publication_translate_columns(Relation targetrel, List *columns,
 					errmsg("cannot use system column \"%s\" in publication column list",
 						   colname));
 
-		/*
-		 * TODO: simplify the expression
-		 */
-		if (TupleDescAttr(tupdesc, attnum - 1)->attgenerated &&
-			TupleDescAttr(tupdesc, attnum - 1)->attgenerated != ATTRIBUTE_GENERATED_STORED)
+		att = TupleDescAttr(tupdesc, attnum - 1);
+		if (att->attgenerated && att->attgenerated != ATTRIBUTE_GENERATED_STORED)
 			ereport(ERROR,
 					errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
 					errmsg("cannot use virtual generated column \"%s\" in publication column list",
@@ -1236,7 +1234,10 @@ pg_get_publication_tables(PG_FUNCTION_ARGS)
 			{
 				Form_pg_attribute att = TupleDescAttr(desc, i);
 
-				if (att->attisdropped || (att->attgenerated && att->attgenerated != ATTRIBUTE_GENERATED_STORED))
+				if (att->attisdropped)
+					continue;
+
+				if (att->attgenerated && att->attgenerated != ATTRIBUTE_GENERATED_STORED)
 					continue;
 
 				attnums[nattnums++] = att->attnum;
diff --git a/src/test/subscription/t/011_generated.pl b/src/test/subscription/t/011_generated.pl
index 361d9f3..edcb149 100644
--- a/src/test/subscription/t/011_generated.pl
+++ b/src/test/subscription/t/011_generated.pl
@@ -39,7 +39,7 @@ $node_subscriber->safe_psql('postgres',
 	"CREATE TABLE tab2 (a int, b int)"
 );
 
-# publisher-side tab3 has stored generated col 'b' but subscriber-side tab2 has DIFFERENT COMPUTATION generated col 'b'.
+# publisher-side tab3 has stored generated col 'b' but subscriber-side tab2 has DIFFERENT COMPUTATION stored generated col 'b'.
 $node_publisher->safe_psql('postgres',
 	"CREATE TABLE tab3 (a int, b int GENERATED ALWAYS AS (a + 10) STORED)"
 );
@@ -48,7 +48,7 @@ $node_subscriber->safe_psql('postgres',
 	"CREATE TABLE tab3 (a int, b int GENERATED ALWAYS AS (a + 20) STORED)"
 );
 
-# tab4: publisher-side stored generated col 'b' and 'c' --> subscriber-side non-generated col 'b', and stored generated-col 'c'
+# tab4: publisher-side stored generated cols 'b' and 'c' --> subscriber-side non-generated col 'b', and stored generated col 'c'
 $node_publisher->safe_psql('postgres',
 	"CREATE TABLE tab4 (a int , b int GENERATED ALWAYS AS (a * 2) STORED, c int GENERATED ALWAYS AS (a * 2) STORED)"
 );
@@ -63,7 +63,7 @@ $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 stored generated col 'b' and 'c' --> subscriber-side non-generated col 'b', and stored generated-col 'c'
+# tab6: publisher-side stored generated cols 'b' and 'c' --> subscriber-side non-generated col 'b', and stored generated-col 'c'
 # columns on subscriber in 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)");
@@ -184,7 +184,7 @@ $node_publisher->safe_psql('postgres', "INSERT INTO tab6 VALUES (4), (5)");
 
 $node_publisher->wait_for_catchup('sub6');
 
-# stored gen-col 'b' and 'c' in publisher replicating to NOT gen-col 'b' and gen-col 'c' on subscriber
+# stored gen-cols 'b' and 'c' in publisher replicating to NOT gen-col 'b' and stored gen-col 'c' on subscriber
 # order of column is different on subscriber
 $result =
   $node_subscriber->safe_psql('postgres', "SELECT a, b, c FROM tab6 ORDER BY a");