From ebdd745f836f92de4e23d1f1579b8b2206408b81 Mon Sep 17 00:00:00 2001
From: Manu <manuelreyesbravo@gmail.com>
Date: Wed, 16 Sep 2026 09:36:24 -0300
Subject: [PATCH v2 on top of v5 2/2] Don't quote the relation name twice in
 EXCEPT clause errors

Commit a49b9cfd72d made check_publication_add_relation() report the
relation of an EXCEPT clause with RelationGetQualifiedRelationName(),
which quotes identifiers when needed, inside a message that already puts
the name in quotes.  A name that needs quoting came out quoted twice:

    ERROR:  cannot specify relation "public."testpub Part2"" in the publication EXCEPT clause

Build the qualified name without identifier quoting instead, which is
the "\"%s.%s\"" form used elsewhere in the backend.  The message text,
and so its translations, is unchanged.  Add a test with such a name.
---
 src/backend/catalog/pg_publication.c      | 8 +++++++-
 src/test/regress/expected/publication.out | 7 ++++++-
 src/test/regress/sql/publication.sql      | 5 ++++-
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 6b752c4c738..72ac5c8db3e 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -62,7 +62,13 @@ check_publication_add_relation(PublicationRelInfo *pri)
 
 	if (pri->except)
 	{
-		relname = RelationGetQualifiedRelationName(targetrel);
+		/*
+		 * The message already quotes the name, so qualify it without the
+		 * identifier quoting that RelationGetQualifiedRelationName() adds.
+		 */
+		relname = psprintf("%s.%s",
+						   get_namespace_name(RelationGetNamespace(targetrel)),
+						   RelationGetRelationName(targetrel));
 		errormsg = gettext_noop("cannot specify relation \"%s\" in the publication EXCEPT clause");
 	}
 	else
diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out
index 3013e405422..6e4dec1d422 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -514,6 +514,11 @@ Number of partitions: 1 (Use \d+ to list them.)
 CREATE PUBLICATION testpub9 FOR ALL TABLES EXCEPT (TABLE testpub_part1);
 ERROR:  cannot specify relation "public.testpub_part1" in the publication EXCEPT clause
 DETAIL:  This operation is not supported for individual partitions.
+-- A name that needs quoting must not be quoted twice in the message.
+CREATE TABLE "testpub Part2" PARTITION OF testpub_root FOR VALUES FROM (100) TO (200);
+CREATE PUBLICATION testpub9 FOR ALL TABLES EXCEPT (TABLE "testpub Part2");
+ERROR:  cannot specify relation "public.testpub Part2" in the publication EXCEPT clause
+DETAIL:  This operation is not supported for individual partitions.
 CREATE TABLE tab_main (a int) PARTITION BY RANGE(a);
 -- Attaching a partition is not allowed if the partitioned table appears in a
 -- publication's EXCEPT clause.
@@ -522,7 +527,7 @@ ERROR:  cannot attach table "testpub_root" as partition because it is referenced
 DETAIL:  The publication EXCEPT clause cannot contain tables that are partitions.
 HINT:  Change the publication's EXCEPT clause using ALTER PUBLICATION ... SET ALL TABLES.
 RESET client_min_messages;
-DROP TABLE testpub_root, testpub_part1, tab_main;
+DROP TABLE testpub_root, testpub_part1, "testpub Part2", tab_main;
 DROP PUBLICATION testpub8;
 --- Tests for publications with SEQUENCES
 CREATE SEQUENCE regress_pub_seq0;
diff --git a/src/test/regress/sql/publication.sql b/src/test/regress/sql/publication.sql
index 074482605d9..e3dbb2bc57c 100644
--- a/src/test/regress/sql/publication.sql
+++ b/src/test/regress/sql/publication.sql
@@ -249,6 +249,9 @@ CREATE PUBLICATION testpub8 FOR ALL TABLES EXCEPT (TABLE testpub_root);
 \d testpub_part1
 \d testpub_root
 CREATE PUBLICATION testpub9 FOR ALL TABLES EXCEPT (TABLE testpub_part1);
+-- A name that needs quoting must not be quoted twice in the message.
+CREATE TABLE "testpub Part2" PARTITION OF testpub_root FOR VALUES FROM (100) TO (200);
+CREATE PUBLICATION testpub9 FOR ALL TABLES EXCEPT (TABLE "testpub Part2");
 
 CREATE TABLE tab_main (a int) PARTITION BY RANGE(a);
 -- Attaching a partition is not allowed if the partitioned table appears in a
@@ -256,7 +259,7 @@ CREATE TABLE tab_main (a int) PARTITION BY RANGE(a);
 ALTER TABLE tab_main ATTACH PARTITION testpub_root FOR VALUES FROM (0) TO (200);
 
 RESET client_min_messages;
-DROP TABLE testpub_root, testpub_part1, tab_main;
+DROP TABLE testpub_root, testpub_part1, "testpub Part2", tab_main;
 DROP PUBLICATION testpub8;
 
 --- Tests for publications with SEQUENCES
-- 
2.55.0

