From 589fd95c9fe96ad2e38db51c1b4238017ba41ce3 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignesh21@gmail.com>
Date: Fri, 8 May 2026 11:17:03 +0530
Subject: [PATCH v7] Use schema-qualified names in publication add relation
 error messages.

Error messages in check_publication_add_relation() previously reported
only the relation name when a table in a non-EXCEPT clause could not
be processed. This could be ambiguous when relations with the same name
exist in multiple schemas. Use schema-qualified names instead, making
these error messages more consistent with other messages that reference
relations.
---
 contrib/postgres_fdw/expected/postgres_fdw.out |  2 +-
 src/backend/catalog/pg_publication.c           |  8 +-------
 src/test/regress/expected/publication.out      | 10 +++++-----
 3 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index aaffcf31271..8960190c3ac 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -288,7 +288,7 @@ DROP SUBSCRIPTION regress_pgfdw_subscription;
 -- test error case for create publication on foreign table
 -- ===================================================================
 CREATE PUBLICATION testpub_ftbl FOR TABLE ft1;  -- should fail
-ERROR:  cannot add relation "ft1" to publication
+ERROR:  cannot add relation "public.ft1" to publication
 DETAIL:  This operation is not supported for foreign tables.
 -- ===================================================================
 -- simple queries
diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 5c457d9aca8..c6ed7699bd1 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -56,19 +56,13 @@ static void
 check_publication_add_relation(PublicationRelInfo *pri)
 {
 	Relation	targetrel = pri->relation;
-	const char *relname;
+	const char *relname = RelationGetQualifiedRelationName(targetrel);
 	const char *errormsg;
 
 	if (pri->except)
-	{
-		relname = RelationGetQualifiedRelationName(targetrel);
 		errormsg = gettext_noop("cannot specify relation \"%s\" in the publication EXCEPT clause");
-	}
 	else
-	{
-		relname = RelationGetRelationName(targetrel);
 		errormsg = gettext_noop("cannot add relation \"%s\" to publication");
-	}
 
 	/* If in EXCEPT clause, must be root partitioned table */
 	if (pri->except && targetrel->rd_rel->relispartition)
diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out
index 29e54b214a0..826310f657e 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -1499,23 +1499,23 @@ UPDATE testpub_tbl4 set a = 3;
 DROP TABLE testpub_tbl4;
 -- fail - view
 CREATE PUBLICATION testpub_fortbl FOR TABLE testpub_view;
-ERROR:  cannot add relation "testpub_view" to publication
+ERROR:  cannot add relation "public.testpub_view" to publication
 DETAIL:  This operation is not supported for views.
 CREATE TEMPORARY TABLE testpub_temptbl(a int);
 -- fail - temporary table
 CREATE PUBLICATION testpub_fortemptbl FOR TABLE testpub_temptbl;
-ERROR:  cannot add relation "testpub_temptbl" to publication
+ERROR:  cannot add relation "pg_temp.testpub_temptbl" to publication
 DETAIL:  This operation is not supported for temporary tables.
 DROP TABLE testpub_temptbl;
 CREATE UNLOGGED TABLE testpub_unloggedtbl(a int);
 -- fail - unlogged table
 CREATE PUBLICATION testpub_forunloggedtbl FOR TABLE testpub_unloggedtbl;
-ERROR:  cannot add relation "testpub_unloggedtbl" to publication
+ERROR:  cannot add relation "public.testpub_unloggedtbl" to publication
 DETAIL:  This operation is not supported for unlogged tables.
 DROP TABLE testpub_unloggedtbl;
 -- fail - system table
 CREATE PUBLICATION testpub_forsystemtbl FOR TABLE pg_publication;
-ERROR:  cannot add relation "pg_publication" to publication
+ERROR:  cannot add relation "pg_catalog.pg_publication" to publication
 DETAIL:  This operation is not supported for system tables.
 SET client_min_messages = 'ERROR';
 CREATE PUBLICATION testpub_fortbl FOR TABLE testpub_tbl1, pub_test.testpub_nopk;
@@ -1537,7 +1537,7 @@ Tables:
 
 -- fail - view
 ALTER PUBLICATION testpub_default ADD TABLE testpub_view;
-ERROR:  cannot add relation "testpub_view" to publication
+ERROR:  cannot add relation "public.testpub_view" to publication
 DETAIL:  This operation is not supported for views.
 ALTER PUBLICATION testpub_default ADD TABLE testpub_tbl1;
 ALTER PUBLICATION testpub_default SET TABLE testpub_tbl1;
-- 
2.43.0

