Subject: Re: Restrict publishing of partitioned table with a foreign table as partition

From: Sergey Tatarintsev <[email-masked]>
Date: Mon, 7 Apr 2025 11:13:04 +0700

07.04.2025 03:27, Álvaro Herrera пишет:
> On 2025-Apr-01, Shlok Kyal wrote:
>
>> I have modified the comment in create_publication.sgml and also added
>> comment in the restrictions section of logical-replication.sgml.
>> I have also added a more detailed explanation in comment of
>> 'check_foreign_tables'
>>
>> I have attached the updated v11 patch.
> Sadly I don't have time to describe the changes proposed here right now,
> but I'll do that early tomorrow.  (Some minor changes are still needed,
> particularly the comments to publication_check_foreign_parts which are
> mostly unchanged from what your patch has.  I'll do another review round
> tomorrow.)
>
Hello!

I looked at the latest patch again and found one more place for 
list_free(). Also look at additional test case:

diff --git a/src/backend/catalog/pg_publication.c 
b/src/backend/catalog/pg_publication.c index 51e463c112b..7fcc191feb9 
100644 --- a/src/backend/catalog/pg_publication.c +++ 
b/src/backend/catalog/pg_publication.c @@ -442,6 +442,7 @@ 
GetPubPartitionOptionRelations(List *result, PublicationPartOpt 
pub_partopt,                         result = lappend_oid(result, 
partOid);                 } +               list_free(all_parts); 
         }         else                 result = lappend_oid(result, 
relid); diff --git a/src/test/regress/sql/publication.sql 
b/src/test/regress/sql/publication.sql index 49c9d98b668..e56aebc397a 
100644 --- a/src/test/regress/sql/publication.sql +++ 
b/src/test/regress/sql/publication.sql @@ -1296,6 +1296,14 @@ SELECT 
pubname, tablename FROM pg_publication_tables WHERE schemaname in 
('sch3'  -- foreign partition  ALTER PUBLICATION pub1 SET 
(publish_via_partition_root); +CREATE SCHEMA sch5; +CREATE SCHEMA sch6; 
+CREATE TABLE sch6.tmain(id int) PARTITION BY RANGE(id); +CREATE TABLE 
sch5.part1 PARTITION OF sch6.tmain FOR VALUES FROM (0) TO (10) PARTITION 
BY RANGE(id); +CREATE FOREIGN TABLE sch6.part2 PARTITION OF sch5.part1 
FOR VALUES FROM (0) TO (5) SERVER fdw_server; +CREATE PUBLICATION pub4 
FOR TABLES IN SCHEMA sch5 WITH (publish_via_partition_root); +SELECT 
pubname, tablename FROM pg_publication_tables WHERE pubname = 'pub4' 
ORDER BY pubname, tablename; +  DROP PUBLICATION pub1;  DROP PUBLICATION 
pub2;  DROP PUBLICATION pub3;

I think this is a wrong assumption:

ScanKeyInit(&key[keycount++], Anum_pg_class_relispartition, 
BTEqualStrategyNumber, F_BOOLEQ, BoolGetDatum(false));

In this case sch5.part1 is partitioned table, but it also partition of 
table in different schema

-- 
With best regards,
Sergey Tatarintsev,
PostgresPro

---

From: Álvaro Herrera <[email-masked]>
Date: Mon, 7 Apr 2025 14:30:59 +0200

On 2025-Apr-07, Sergey Tatarintsev wrote:

> In this case sch5.part1 is partitioned table, but it also partition of table
> in different schema

Yeah, this looks ill-considered.

After looking at this code some more, I'm withdrawing myself from here.
(I don't mean just this patch in particular, I mean the logical
replication code as a whole.)

Thanks,

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"La persona que no quería pecar / estaba obligada a sentarse
 en duras y empinadas sillas    / desprovistas, por cierto
 de blandos atenuantes"                          (Patricio Vogel)

---

From: Shlok Kyal <[email-masked]>
Date: Mon, 28 Apr 2025 18:21:46 +0530

On Mon, 7 Apr 2025 at 09:43, Sergey Tatarintsev
<[email-masked]> wrote:
> I looked at the latest patch again and found one more place for list_free(). Also look at additional test case:
>
> diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c index 51e463c112b..7fcc191feb9 100644 --- a/src/backend/catalog/pg_publication.c +++ b/src/backend/catalog/pg_publication.c @@ -442,6 +442,7 @@ GetPubPartitionOptionRelations(List *result, PublicationPartOpt pub_partopt,                           result = lappend_oid(result, partOid);                 } +               list_free(all_parts);         }         else                 result = lappend_oid(result, relid); diff --git a/src/test/regress/sql/publication.sql b/src/test/regress/sql/publication.sql index 49c9d98b668..e56aebc397a 100644 --- a/src/test/regress/sql/publication.sql +++ b/src/test/regress/sql/publication.sql @@ -1296,6 +1296,14 @@ SELECT pubname, tablename FROM pg_publication_tables WHERE schemaname in ('sch3'  -- foreign partition  ALTER PUBLICATION pub1 SET (publish_via_partition_root);   +CREATE SCHEMA sch5; +CREATE SCHEMA sch6; +CREATE TABLE sch6.tmain(id int) PARTITION BY RANGE(id); +CREATE TABLE sch5.part1 PARTITION OF sch6.tmain FOR VALUES FROM (0) TO (10) PARTITION BY RANGE(id); +CREATE FOREIGN TABLE sch6.part2 PARTITION OF sch5.part1 FOR VALUES FROM (0) TO (5) SERVER fdw_server; +CREATE PUBLICATION pub4 FOR TABLES IN SCHEMA sch5 WITH (publish_via_partition_root); +SELECT pubname, tablename FROM pg_publication_tables WHERE pubname = 'pub4' ORDER BY pubname, tablename; +  DROP PUBLICATION pub1;  DROP PUBLICATION pub2;  DROP PUBLICATION pub3;
>
> ScanKeyInit(&key[keycount++], Anum_pg_class_relispartition, BTEqualStrategyNumber, F_BOOLEQ, BoolGetDatum(false));
>
> In this case sch5.part1 is partitioned table, but it also partition of table in different schema
>

Hi Sergey,

Thanks for reviewing the patch. I have addressed the comment in the
latest patch shared in [1].

[1]: https://www.postgresql.org/message-id/CANhcyEW0QMiJXMqpPFRHni-q0Rm4R0hpZ0LdaqA%3DF3wvDUU6sQ%40mail.gmail.com

Thanks and Regards,
Shlok Kyal