v2-0001-fix-new-collist-validation-msg.patch

application/octet-stream

Filename: v2-0001-fix-new-collist-validation-msg.patch
Type: application/octet-stream
Part: 0
Message: Re: BUG #18558: ALTER PUBLICATION fails with unhelpful error on attempt to use system column

Patch

Format: format-patch
Series: patch v2-0001
Subject: fix-new-collist-validation-msg
File+
src/backend/catalog/pg_publication.c 1 4
src/backend/commands/publicationcmds.c 9 8
src/include/catalog/pg_publication.h 2 0
src/test/regress/expected/publication.out 2 0
src/test/regress/sql/publication.sql 1 0
From 4cde2fa96adfd40d942e6261b34b8c958ffb9e58 Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Mon, 29 Jul 2024 15:12:25 +1000
Subject: [PATCH v2] fix-new-collist-validation-msg

---
 src/backend/catalog/pg_publication.c      |  5 +----
 src/backend/commands/publicationcmds.c    | 17 +++++++++--------
 src/include/catalog/pg_publication.h      |  2 ++
 src/test/regress/expected/publication.out |  2 ++
 src/test/regress/sql/publication.sql      |  1 +
 5 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 0602398..1d45572 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -48,9 +48,6 @@ typedef struct
 								 * table. */
 } published_rel;
 
-static void publication_translate_columns(Relation targetrel, List *columns,
-										  int *natts, AttrNumber **attrs);
-
 /*
  * Check if relation can be in given publication and throws appropriate
  * error if not.
@@ -498,7 +495,7 @@ compare_int16(const void *a, const void *b)
  * FirstLowInvalidHeapAttributeNumber; system columns are forbidden so this
  * is okay.
  */
-static void
+void
 publication_translate_columns(Relation targetrel, List *columns,
 							  int *natts, AttrNumber **attrs)
 {
diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c
index 6ea7099..e293dda 100644
--- a/src/backend/commands/publicationcmds.c
+++ b/src/backend/commands/publicationcmds.c
@@ -1181,15 +1181,16 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup,
 				 */
 				if (newpubrel->columns)
 				{
-					ListCell   *lc;
+					AttrNumber *attarray = NULL;
+					int			natts = 0;
 
-					foreach(lc, newpubrel->columns)
-					{
-						char	   *colname = strVal(lfirst(lc));
-						AttrNumber	attnum = get_attnum(newrelid, colname);
+					/* First, validate the new column list. */
+					publication_translate_columns(newpubrel->relation, newpubrel->columns,
+												  &natts, &attarray);
 
-						newcolumns = bms_add_member(newcolumns, attnum);
-					}
+					newcolumns = pub_collist_to_bitmapset(NULL,
+														  PointerGetDatum(buildint2vector(attarray, natts)),
+														  NULL);
 				}
 
 				/*
@@ -1199,7 +1200,7 @@ AlterPublicationTables(AlterPublicationStmt *stmt, HeapTuple tup,
 				 * expressions also match. Same for the column list. Drop the
 				 * rest.
 				 */
-				if (RelationGetRelid(newpubrel->relation) == oldrelid)
+				if (newrelid == oldrelid)
 				{
 					if (equal(oldrelwhereclause, newpubrel->whereClause) &&
 						bms_equal(oldcolumns, newcolumns))
diff --git a/src/include/catalog/pg_publication.h b/src/include/catalog/pg_publication.h
index 2f1b6ab..596f2cc 100644
--- a/src/include/catalog/pg_publication.h
+++ b/src/include/catalog/pg_publication.h
@@ -157,5 +157,7 @@ extern ObjectAddress publication_add_schema(Oid pubid, Oid schemaid,
 
 extern Bitmapset *pub_collist_to_bitmapset(Bitmapset *columns, Datum pubcols,
 										   MemoryContext mcxt);
+extern void publication_translate_columns(Relation targetrel, List *columns,
+										  int *natts, AttrNumber **attrs);
 
 #endif							/* PG_PUBLICATION_H */
diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out
index 30b6371..04a7795 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -693,6 +693,8 @@ ERROR:  cannot use generated column "d" in publication column list
 -- error: system attributes "ctid" not allowed in column list
 ALTER PUBLICATION testpub_fortable ADD TABLE testpub_tbl5 (a, ctid);
 ERROR:  cannot use system column "ctid" in publication column list
+ALTER PUBLICATION testpub_fortable SET TABLE testpub_tbl1 (id, ctid);
+ERROR:  cannot use system column "ctid" in publication column list
 -- ok
 ALTER PUBLICATION testpub_fortable ADD TABLE testpub_tbl5 (a, c);
 ALTER TABLE testpub_tbl5 DROP COLUMN c;		-- no dice
diff --git a/src/test/regress/sql/publication.sql b/src/test/regress/sql/publication.sql
index 479d4f3..c379901 100644
--- a/src/test/regress/sql/publication.sql
+++ b/src/test/regress/sql/publication.sql
@@ -417,6 +417,7 @@ ALTER PUBLICATION testpub_fortable DROP TABLE testpub_tbl5;
 ALTER PUBLICATION testpub_fortable ADD TABLE testpub_tbl5 (a, d);
 -- error: system attributes "ctid" not allowed in column list
 ALTER PUBLICATION testpub_fortable ADD TABLE testpub_tbl5 (a, ctid);
+ALTER PUBLICATION testpub_fortable SET TABLE testpub_tbl1 (id, ctid);
 -- ok
 ALTER PUBLICATION testpub_fortable ADD TABLE testpub_tbl5 (a, c);
 ALTER TABLE testpub_tbl5 DROP COLUMN c;		-- no dice
-- 
1.8.3.1