From 45c32cd0c7079531b539e1be37719ac3332bc8a8 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignesh21@gmail.com>
Date: Thu, 7 May 2026 10:51:03 +0530
Subject: [PATCH v5 2/2] Include schema-qualified names in non-EXCEPT
 publication error messages.

Previously, some error messages in check_publication_add_relation()
only reported the relation name when a table could not be added to a
publication. This could be ambiguous in databases where the same
relation name exists in multiple schemas.

The handling for EXCEPT clause related error messages was addressed
in a previous patch. This patch extends the same improvement to the
remaining non-EXCEPT cases by using schema-qualified names in error
messages for CREATE PUBLICATION and ALTER PUBLICATION commands.
---
 contrib/postgres_fdw/expected/postgres_fdw.out |  2 +-
 src/backend/catalog/pg_publication.c           | 17 +++++------------
 src/test/regress/expected/publication.out      | 10 +++++-----
 3 files changed, 11 insertions(+), 18 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 6a59804e7f6..15ca3d90ebf 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -68,25 +68,18 @@ static void
 check_publication_add_relation(PublicationRelInfo *pri)
 {
 	Relation	targetrel = pri->relation;
-	const char *relname;
 	const char *errormsg;
 
 	if (pri->except)
-	{
-		relname = get_relation_qualified_name(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)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg(errormsg, relname),
+				 errmsg(errormsg, get_relation_qualified_name(targetrel)),
 				 errdetail("This operation is not supported for individual partitions.")));
 
 	/* Must be a regular or partitioned table */
@@ -94,26 +87,26 @@ check_publication_add_relation(PublicationRelInfo *pri)
 		RelationGetForm(targetrel)->relkind != RELKIND_PARTITIONED_TABLE)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg(errormsg, relname),
+				 errmsg(errormsg, get_relation_qualified_name(targetrel)),
 				 errdetail_relkind_not_supported(RelationGetForm(targetrel)->relkind)));
 
 	/* Can't be system table */
 	if (IsCatalogRelation(targetrel))
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg(errormsg, relname),
+				 errmsg(errormsg, get_relation_qualified_name(targetrel)),
 				 errdetail("This operation is not supported for system tables.")));
 
 	/* UNLOGGED and TEMP relations cannot be part of publication. */
 	if (targetrel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg(errormsg, relname),
+				 errmsg(errormsg, get_relation_qualified_name(targetrel)),
 				 errdetail("This operation is not supported for temporary tables.")));
 	else if (targetrel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg(errormsg, relname),
+				 errmsg(errormsg, get_relation_qualified_name(targetrel)),
 				 errdetail("This operation is not supported for unlogged tables.")));
 }
 
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

