From b15995d2cdb408256afbb80977548aba92fde224 Mon Sep 17 00:00:00 2001
From: Dilip Kumar <dilipkumarb@google.com>
Date: Tue, 28 Apr 2026 11:11:57 +0530
Subject: [PATCH v29 1/4] Include schema-qualified names in publication error
 messages.

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

This patch updates these error messages to use schema-qualified names,
improving the clarity of error reporting for CREATE PUBLICATION and
ALTER PUBLICATION commands.
---
 .../postgres_fdw/expected/postgres_fdw.out    |  2 +-
 src/backend/catalog/pg_publication.c          | 34 ++++++++++++++++---
 src/test/regress/expected/publication.out     | 12 +++----
 3 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index 10e87acabef..4cc7f367a6e 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 a43d385c605..ea5b1874e8d 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -48,6 +48,30 @@ typedef struct
 								 * table. */
 } published_rel;
 
+static char *get_qualified_relname(Relation rel);
+
+/*
+ * Get a palloc'd string containing the schema-qualified name of the relation.
+ */
+static char *
+get_qualified_relname(Relation rel)
+{
+	char	   *relname;
+	char	   *nspname;
+	char	   *result;
+
+	relname = RelationGetRelationName(rel);
+
+	nspname = get_namespace_name_or_temp(RelationGetNamespace(rel));
+	if (!nspname)
+		elog(ERROR, "cache lookup failed for namespace %u",
+			 RelationGetNamespace(rel));
+
+	result = quote_qualified_identifier(nspname, relname);
+
+	return result;
+}
+
 /*
  * Check if relation can be in given publication and throws appropriate
  * error if not.
@@ -67,7 +91,7 @@ check_publication_add_relation(PublicationRelInfo *pri)
 	if (pri->except && targetrel->rd_rel->relispartition)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg(errormsg, RelationGetRelationName(targetrel)),
+				 errmsg(errormsg, get_qualified_relname(targetrel)),
 				 errdetail("This operation is not supported for individual partitions.")));
 
 	/* Must be a regular or partitioned table */
@@ -75,26 +99,26 @@ check_publication_add_relation(PublicationRelInfo *pri)
 		RelationGetForm(targetrel)->relkind != RELKIND_PARTITIONED_TABLE)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg(errormsg, RelationGetRelationName(targetrel)),
+				 errmsg(errormsg, get_qualified_relname(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, RelationGetRelationName(targetrel)),
+				 errmsg(errormsg, get_qualified_relname(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, RelationGetRelationName(targetrel)),
+				 errmsg(errormsg, get_qualified_relname(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, RelationGetRelationName(targetrel)),
+				 errmsg(errormsg, get_qualified_relname(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 0345f6c5e47..826310f657e 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -458,7 +458,7 @@ Excluded from publications:
 Number of partitions: 1 (Use \d+ to list them.)
 
 CREATE PUBLICATION testpub9 FOR ALL TABLES EXCEPT (TABLE testpub_part1);
-ERROR:  cannot specify relation "testpub_part1" in the publication EXCEPT clause
+ERROR:  cannot specify relation "public.testpub_part1" 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
@@ -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.49.0

