From 056b8772c1ca265b05d76134dc39e8706ec85ff9 Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Wed, 21 Oct 2026 08:28:34 +1100
Subject: [PATCH v5] Error if same table has ONLY mismatch.

Added a new error:
ERROR:  conflicting ONLY specifications for table "%s"

Patched code now rejects conflicting ONLY specifications when the same
non-partitioned table is explicitly listed more than once.

e.g.

CREATE PUBLICATION pub FOR TABLE parent, TABLE ONLY parent; -- ERROR, conflict
CREATE PUBLICATION pub FOR TABLE ONLY parent, TABLE parent; -- ERROR, conflict

CREATE PUBLICATION pub FOR TABLE plain, TABLE ONLY plain; -- ERROR, conflict
CREATE PUBLICATION pub FOR TABLE ONLY plain, TABLE plain; -- ERROR, conflict

~

ONLY has "no effect" for partitioned tables, so conflicting ONLY-ness of the
same partitioned table is ignored, same as before:

CREATE PUBLICATION pub FOR TABLE part_root, TABLE ONLY part_root; -- OK
CREATE PUBLICATION pub FOR TABLE ONLY part_root, TABLE part_root; -- OK

~

Author: Peter Smith <smithpb2250@gmail.com>

Reviewed-by: shveta malik <shveta.malik@gmail.com>
Reviewed-by: Takeshi Ideriha <iderihatakeshi@gmail.com>
Reviewed-by: kedar anavardekar <kedar.anavardekar@gmail.com>

Discussion: https://www.postgresql.org/message-id/flat/19bc00e8-ed58-488d-a727-5bca2445b795%40gmail.com#b78a125644c7448a4771f261366a11f9
---
 src/backend/commands/publicationcmds.c    |  2 +-
 src/test/regress/expected/publication.out | 10 +++++++---
 src/test/regress/sql/publication.sql      |  8 +++++---
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/backend/commands/publicationcmds.c b/src/backend/commands/publicationcmds.c
index 9dfc965b4fb..2e23eb44518 100644
--- a/src/backend/commands/publicationcmds.c
+++ b/src/backend/commands/publicationcmds.c
@@ -1896,7 +1896,7 @@ OpenTableList(List *tables)
 			 * tables should be included.  Plain "foo, foo" (agreeing on
 			 * ONLY-ness) remains a silently-tolerated no-op, as before.
 			 *
-			 * Check is just for explcitly mentioned tables to avoid falsely
+			 * Check is just for explicitly mentioned tables to avoid falsely
 			 * giving errors for implicitly inherited child tables.
 			 *
 			 * Mismatched ONLY-ness remains silently-tolerated for
diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out
index c79fe48b346..299c7d21b12 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -475,7 +475,7 @@ Except tables:
     "public.testpub_tbl_parent"
 
 -- Specifying same table twice is OK when the mentions agree on ONLY-ness.
-CREATE TABLE tbl_plain(a int);;
+CREATE TABLE tbl_plain(a int);
 CREATE PUBLICATION testpub_dup1 FOR TABLE
   testpub_tbl_parent, testpub_tbl_parent,
   testpub_tbl_child, testpub_tbl_child,
@@ -519,10 +519,14 @@ CREATE PUBLICATION testpub_conflict1 FOR TABLE testpub_tbl_parent, ONLY testpub_
 ERROR:  conflicting ONLY specifications for table "testpub_tbl_parent"
 CREATE PUBLICATION testpub_conflict2 FOR TABLE ONLY testpub_tbl_parent, testpub_tbl_parent;
 ERROR:  conflicting ONLY specifications for table "testpub_tbl_parent"
-CREATE PUBLICATION testpub_conflict3 FOR TABLE ONLY testpub_tbl_child, testpub_tbl_child;
+CREATE PUBLICATION testpub_conflict3 FOR TABLE testpub_tbl_child, ONLY testpub_tbl_child;
 ERROR:  conflicting ONLY specifications for table "testpub_tbl_child"
-CREATE PUBLICATION testpub_conflict4 FOR TABLE testpub_tbl_child, ONLY testpub_tbl_child;
+CREATE PUBLICATION testpub_conflict4 FOR TABLE ONLY testpub_tbl_child, testpub_tbl_child;
 ERROR:  conflicting ONLY specifications for table "testpub_tbl_child"
+CREATE PUBLICATION testpub_conflict5 FOR TABLE tbl_plain, ONLY tbl_plain;
+ERROR:  conflicting ONLY specifications for table "tbl_plain"
+CREATE PUBLICATION testpub_conflict6 FOR TABLE ONLY tbl_plain, tbl_plain;
+ERROR:  conflicting ONLY specifications for table "tbl_plain"
 ALTER PUBLICATION testpub_dup3 SET ALL TABLES EXCEPT(TABLE ONLY testpub_tbl_parent, testpub_tbl_parent);
 ERROR:  conflicting ONLY specifications for table "testpub_tbl_parent"
 SET client_min_messages = 'ERROR';
diff --git a/src/test/regress/sql/publication.sql b/src/test/regress/sql/publication.sql
index e60ac52563f..8e701ff205c 100644
--- a/src/test/regress/sql/publication.sql
+++ b/src/test/regress/sql/publication.sql
@@ -235,7 +235,7 @@ CREATE PUBLICATION testpub7 FOR ALL TABLES EXCEPT (TABLE ONLY testpub_tbl_parent
 \dRp+ testpub7
 
 -- Specifying same table twice is OK when the mentions agree on ONLY-ness.
-CREATE TABLE tbl_plain(a int);;
+CREATE TABLE tbl_plain(a int);
 CREATE PUBLICATION testpub_dup1 FOR TABLE
   testpub_tbl_parent, testpub_tbl_parent,
   testpub_tbl_child, testpub_tbl_child,
@@ -260,8 +260,10 @@ ALTER PUBLICATION testpub_dup3 SET ALL TABLES EXCEPT (TABLE
 RESET client_min_messages;
 CREATE PUBLICATION testpub_conflict1 FOR TABLE testpub_tbl_parent, ONLY testpub_tbl_parent;
 CREATE PUBLICATION testpub_conflict2 FOR TABLE ONLY testpub_tbl_parent, testpub_tbl_parent;
-CREATE PUBLICATION testpub_conflict3 FOR TABLE ONLY testpub_tbl_child, testpub_tbl_child;
-CREATE PUBLICATION testpub_conflict4 FOR TABLE testpub_tbl_child, ONLY testpub_tbl_child;
+CREATE PUBLICATION testpub_conflict3 FOR TABLE testpub_tbl_child, ONLY testpub_tbl_child;
+CREATE PUBLICATION testpub_conflict4 FOR TABLE ONLY testpub_tbl_child, testpub_tbl_child;
+CREATE PUBLICATION testpub_conflict5 FOR TABLE tbl_plain, ONLY tbl_plain;
+CREATE PUBLICATION testpub_conflict6 FOR TABLE ONLY tbl_plain, tbl_plain;
 ALTER PUBLICATION testpub_dup3 SET ALL TABLES EXCEPT(TABLE ONLY testpub_tbl_parent, testpub_tbl_parent);
 
 SET client_min_messages = 'ERROR';
-- 
2.47.3

