From 8a2a4f94e4c1f35e77179e6de0d4f3f2d1c7cf1f Mon Sep 17 00:00:00 2001
From: Sergey Tatarintsev <s.tatarintsev@postgrespro.ru>
Date: Fri, 31 Jan 2025 11:57:18 +0700
Subject: [PATCH] Tests: check tables which was really published via 'FOR ALL
 TABLES' and 'FOR TABLES IN SCHEMA'

---
 src/backend/catalog/pg_publication.c      |  1 +
 src/test/regress/expected/publication.out | 26 ++++++++++++++++++++++-
 src/test/regress/sql/publication.sql      | 21 ++++++++++++++++--
 3 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c
index 071b428bc0..b7be142bd0 100644
--- a/src/backend/catalog/pg_publication.c
+++ b/src/backend/catalog/pg_publication.c
@@ -1372,6 +1372,7 @@ check_partrel_has_foreign_table(Form_pg_class relform, Oid *foreign_tbl_relid)
 			if (has_foreign_tbl)
 				break;
 		}
+		list_free(relids);
 	}
 
 	return has_foreign_tbl;
diff --git a/src/test/regress/expected/publication.out b/src/test/regress/expected/publication.out
index b973f41e94..20dfe5c6d0 100644
--- a/src/test/regress/expected/publication.out
+++ b/src/test/regress/expected/publication.out
@@ -1886,6 +1886,11 @@ DROP PUBLICATION pub2;
 DROP TABLE gencols;
 RESET client_min_messages;
 -- ======================================================
+-- We don't want to see previously created tables, so we'll create an empty database
+SELECT current_database() as prev_dbname
+\gset
+CREATE DATABASE publication_test_db;
+\c publication_test_db
 -- Test when foreign table is a partition of a partitioned table on which
 -- publication is created
 SET client_min_messages = 'ERROR';
@@ -1922,8 +1927,27 @@ ALTER TABLE sch3.tmain ATTACH PARTITION sch3.part3_2 FOR VALUES FROM (15) TO (20
 ERROR:  cannot attach foreign table "part3_2" to a published table
 DROP PUBLICATION pub1;
 DROP SCHEMA sch3 CASCADE;
-DROP SERVER fdw_server;
+CREATE SCHEMA sch4;
+CREATE TABLE sch4.ltable(id int) PARTITION BY RANGE(id);
+CREATE TABLE sch4.ltable_part PARTITION OF sch4.ltable FOR VALUES FROM (0) TO (5);
+CREATE FOREIGN TABLE sch4.ftable(id int) SERVER fdw_server;
+CREATE PUBLICATION pub1 FOR TABLES IN SCHEMA sch4;
+CREATE PUBLICATION pub2 FOR TABLES IN SCHEMA sch4 WITH (publish_via_partition_root);
+CREATE PUBLICATION pub3 FOR ALL TABLES;
+SELECT pubname, tablename FROM pg_publication_tables ORDER BY pubname, tablename;
+ pubname |  tablename  
+---------+-------------
+ pub1    | ltable_part
+ pub2    | ltable
+ pub3    | ltable_part
+(3 rows)
+
+DROP PUBLICATION pub1, pub2;
+DROP SCHEMA sch4 CASCADE;
+DROP SERVER fdw_server CASCADE;
 DROP FOREIGN DATA WRAPPER test_fdw;
+\c :prev_dbname
+DROP DATABASE publication_test_db;
 RESET SESSION AUTHORIZATION;
 DROP ROLE regress_publication_user, regress_publication_user2;
 DROP ROLE regress_publication_user_dummy;
diff --git a/src/test/regress/sql/publication.sql b/src/test/regress/sql/publication.sql
index b9811033dc..b24c1e90c7 100644
--- a/src/test/regress/sql/publication.sql
+++ b/src/test/regress/sql/publication.sql
@@ -1188,6 +1188,11 @@ DROP TABLE gencols;
 RESET client_min_messages;
 -- ======================================================
 
+-- We don't want to see previously created tables, so we'll create an empty database
+SELECT current_database() as prev_dbname
+\gset
+CREATE DATABASE publication_test_db;
+\c publication_test_db
 -- Test when foreign table is a partition of a partitioned table on which
 -- publication is created
 SET client_min_messages = 'ERROR';
@@ -1220,9 +1225,21 @@ ALTER TABLE sch3.tmain ATTACH PARTITION sch3.part3_2 FOR VALUES FROM (15) TO (20
 
 DROP PUBLICATION pub1;
 DROP SCHEMA sch3 CASCADE;
-DROP SERVER fdw_server;
-DROP FOREIGN DATA WRAPPER test_fdw;
 
+CREATE SCHEMA sch4;
+CREATE TABLE sch4.ltable(id int) PARTITION BY RANGE(id);
+CREATE TABLE sch4.ltable_part PARTITION OF sch4.ltable FOR VALUES FROM (0) TO (5);
+CREATE FOREIGN TABLE sch4.ftable(id int) SERVER fdw_server;
+CREATE PUBLICATION pub1 FOR TABLES IN SCHEMA sch4;
+CREATE PUBLICATION pub2 FOR TABLES IN SCHEMA sch4 WITH (publish_via_partition_root);
+CREATE PUBLICATION pub3 FOR ALL TABLES;
+SELECT pubname, tablename FROM pg_publication_tables ORDER BY pubname, tablename;
+DROP PUBLICATION pub1, pub2;
+DROP SCHEMA sch4 CASCADE;
+DROP SERVER fdw_server CASCADE;
+DROP FOREIGN DATA WRAPPER test_fdw;
+\c :prev_dbname
+DROP DATABASE publication_test_db;
 RESET SESSION AUTHORIZATION;
 DROP ROLE regress_publication_user, regress_publication_user2;
 DROP ROLE regress_publication_user_dummy;
-- 
2.43.0

