From ee9ce96211e80347e364f480dc19229284a76258 Mon Sep 17 00:00:00 2001
From: Baji Shaik <baji.pgdev@gmail.com>
Date: Thu, 22 Oct 2026 18:04:40 -0500
Subject: [PATCH] Improve CREATE PUBLICATION error for a standalone
 table/schema name

preprocess_pubobj_list() reports a generic "invalid publication
object list" / "One of TABLE or TABLES IN SCHEMA must be specified
before a standalone table or schema name" whenever the first object
in a FOR list has no TABLE or TABLES IN SCHEMA prefix.  This is the
error a user gets from, among other things, a natural first attempt
at publishing sequences or a schema's tables by name, e.g.
CREATE PUBLICATION p FOR SEQUENCE s1 or FOR SCHEMA myschema, since
SEQUENCE and SCHEMA are not reserved keywords here and are parsed as
a standalone object name instead.

Include the offending name in the detail message when we have one,
and add a hint listing the valid publication object kinds, so the
error points at what to fix instead of only saying something is
wrong:

    ERROR:  invalid publication object list
    DETAIL:  One of TABLE or TABLES IN SCHEMA must be specified before
             standalone table or schema name "sequence".
    HINT:  Valid publication objects are TABLE, TABLES IN SCHEMA, ALL
           TABLES, and ALL SEQUENCES.

The name isn't always available (e.g. FOR CURRENT_SCHEMA, or a
qualified or wildcard table reference); those cases keep the
original detail wording, since pubobj->name is unset for them, but
still get the same hint.

Author: Baji Shaik <baji.pgdev@gmail.com>
---
 src/backend/parser/gram.y                 | 22 +++++++++++++++++-----
 src/test/regress/expected/publication.out | 17 +++++++++++++++++
 src/test/regress/sql/publication.sql      |  6 ++++++
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index 0563453fe24..3b2436a6b9d 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -20022,11 +20022,23 @@ preprocess_pubobj_list(List *pubobjspec_list, core_yyscan_t yyscanner)
 
 	pubobj = (PublicationObjSpec *) linitial(pubobjspec_list);
 	if (pubobj->pubobjtype == PUBLICATIONOBJ_CONTINUATION)
-		ereport(ERROR,
-				errcode(ERRCODE_SYNTAX_ERROR),
-				errmsg("invalid publication object list"),
-				errdetail("One of TABLE or TABLES IN SCHEMA must be specified before a standalone table or schema name."),
-				parser_errposition(pubobj->location));
+	{
+		if (pubobj->name)
+			ereport(ERROR,
+					errcode(ERRCODE_SYNTAX_ERROR),
+					errmsg("invalid publication object list"),
+					errdetail("One of TABLE or TABLES IN SCHEMA must be specified before standalone table or schema name \"%s\".",
+							  pubobj->name),
+					errhint("Valid publication objects are TABLE, TABLES IN SCHEMA, ALL TABLES, and ALL SEQUENCES."),
+					parser_errposition(pubobj->location));
+		else
+			ereport(ERROR,
+					errcode(ERRCODE_SYNTAX_ERROR),
+					errmsg("invalid publication object list"),
+					errdetail("One of TABLE or TABLES IN SCHEMA must be specified before a standalone table or schema name."),
+					errhint("Valid publication objects are TABLE, TABLES IN SCHEMA, ALL TABLES, and ALL SEQUENCES."),
+					parser_errposition(pubobj->location));
+	}
 
 	foreach(cell, pubobjspec_list)
 	{
diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out
index b55da39fde5..d7502fd6de1 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -1854,11 +1854,27 @@ ERROR:  invalid publication object list
 LINE 1: CREATE PUBLICATION testpub_forschema1 FOR CURRENT_SCHEMA;
                                                   ^
 DETAIL:  One of TABLE or TABLES IN SCHEMA must be specified before a standalone table or schema name.
+HINT:  Valid publication objects are TABLE, TABLES IN SCHEMA, ALL TABLES, and ALL SEQUENCES.
 -- check create publication on CURRENT_SCHEMA along with FOR TABLE
 CREATE PUBLICATION testpub_forschema1 FOR TABLE CURRENT_SCHEMA;
 ERROR:  syntax error at or near "CURRENT_SCHEMA"
 LINE 1: CREATE PUBLICATION testpub_forschema1 FOR TABLE CURRENT_SCHE...
                                                         ^
+-- check create publication error detail names the standalone word when the
+-- parser has one, e.g. a natural but nonexistent attempt at publishing
+-- sequences or a schema's tables by name
+CREATE PUBLICATION testpub_forseq FOR SEQUENCE s1;
+ERROR:  invalid publication object list
+LINE 1: CREATE PUBLICATION testpub_forseq FOR SEQUENCE s1;
+                                              ^
+DETAIL:  One of TABLE or TABLES IN SCHEMA must be specified before standalone table or schema name "sequence".
+HINT:  Valid publication objects are TABLE, TABLES IN SCHEMA, ALL TABLES, and ALL SEQUENCES.
+CREATE PUBLICATION testpub_forschema2 FOR SCHEMA pub_test1;
+ERROR:  invalid publication object list
+LINE 1: CREATE PUBLICATION testpub_forschema2 FOR SCHEMA pub_test1;
+                                                  ^
+DETAIL:  One of TABLE or TABLES IN SCHEMA must be specified before standalone table or schema name "schema".
+HINT:  Valid publication objects are TABLE, TABLES IN SCHEMA, ALL TABLES, and ALL SEQUENCES.
 -- check create publication on a schema that does not exist
 CREATE PUBLICATION testpub_forschema FOR TABLES IN SCHEMA non_existent_schema;
 ERROR:  schema "non_existent_schema" does not exist
@@ -2133,6 +2149,7 @@ ERROR:  invalid publication object list
 LINE 1: CREATE PUBLICATION testpub_error FOR pub_test2.tbl1;
                                              ^
 DETAIL:  One of TABLE or TABLES IN SCHEMA must be specified before a standalone table or schema name.
+HINT:  Valid publication objects are TABLE, TABLES IN SCHEMA, ALL TABLES, and ALL SEQUENCES.
 DROP VIEW testpub_view;
 DROP PUBLICATION testpub_default;
 DROP PUBLICATION testpub_ins_trunct;
diff --git a/src/test/regress/sql/publication.sql b/src/test/regress/sql/publication.sql
index e3dbb2bc57c..4ca3c5d88f4 100644
--- a/src/test/regress/sql/publication.sql
+++ b/src/test/regress/sql/publication.sql
@@ -1169,6 +1169,12 @@ CREATE PUBLICATION testpub_forschema1 FOR CURRENT_SCHEMA;
 -- check create publication on CURRENT_SCHEMA along with FOR TABLE
 CREATE PUBLICATION testpub_forschema1 FOR TABLE CURRENT_SCHEMA;
 
+-- check create publication error detail names the standalone word when the
+-- parser has one, e.g. a natural but nonexistent attempt at publishing
+-- sequences or a schema's tables by name
+CREATE PUBLICATION testpub_forseq FOR SEQUENCE s1;
+CREATE PUBLICATION testpub_forschema2 FOR SCHEMA pub_test1;
+
 -- check create publication on a schema that does not exist
 CREATE PUBLICATION testpub_forschema FOR TABLES IN SCHEMA non_existent_schema;
 
-- 
2.54.0 (Apple Git-157)

