Re: Proposal: Cascade REPLICA IDENTITY changes to leaf partitions - Mailing list pgsql-hackers

From Chao Li
Subject Re: Proposal: Cascade REPLICA IDENTITY changes to leaf partitions
Date
Msg-id 742252CE-7C28-4FE5-A0C1-8E059DE26A58@gmail.com
Whole thread Raw
In response to Re: Proposal: Cascade REPLICA IDENTITY changes to leaf partitions  (Amit Kapila <amit.kapila16@gmail.com>)
Responses Re: Proposal: Cascade REPLICA IDENTITY changes to leaf partitions
List pgsql-hackers

> On Dec 11, 2025, at 20:43, Amit Kapila <amit.kapila16@gmail.com> wrote:
>
> On Thu, Dec 11, 2025 at 2:46 PM Chao Li <li.evan.chao@gmail.com> wrote:
>>
>> Hi,
>> While working with logical replication and partitioned tables, I noticed an inconsistency between how publications
treatpartitions and how "ALTER TABLE ... REPLICA IDENTITY" behaves. 
>>
>> When a publication is created on a partitioned table, e.g.:
>> ```
>> CREATE PUBLICATION pub FOR TABLE parent;
>> ```
>>
>> PostgreSQL automatically includes all leaf partitions of the table in the publication. This matches the user’s
expectationthat a partitioned table behaves as a single logical entity. 
>>
>> However, if the user then runs:
>> ```
>> ALTER TABLE parent REPLICA IDENTITY FULL;
>> ```
>> only the parent table’s relreplident is updated. None of the leaf partitions inherit this change, even though the
parentitself has no storage and its replication identity plays no role in logical replication. Logical decoding always
operateson the leaf partitions, and their replication identities determine whether UPDATE/DELETE can be replicated
safely.
>>
>> This gap leads to several problems:
>>
>> * The parent table’s replica identity is effectively irrelevant during logical replication, since it never stores
tuplesor produces WAL. 
>>
>
> When we use row filters, if publish_via_partition_root option of
> publication is true, the root partitioned table's row filter is used.
> I think this would then refer RI of partitioned table for validity of
> row filter. Please see docs [1] (There can be a case where a
> subscription combines multiple publications. If a partitioned table is
> published by any subscribed publications which set
> publish_via_partition_root = true, changes on this partitioned table
> (or on its partitions) will be published using the identity and schema
> of this partitioned table rather than that of the individual
> partitions. This parameter also affects how row filters and column
> lists are chosen for partitions; see below for details.) for more
> details.
>
> I have not tested it but you can once try to see how it behaves.
>
> The other point is what if one of the partition already has RI defined
> to a different value than what is defined for parent table?
>
> [1] - https://www.postgresql.org/docs/devel/sql-createpublication.html
>
> --
> With Regards,
> Amit Kapila.

Hi Amit,

Thanks for pointing out that my assumption of “RI of parent is not used” is not always true.

I agree that automatic-cascade will introduce a lot of complexities. To ensure the backward-compatibility, how about to
extendthe ALTER TABLE syntax like: 

```
ALTER TABLE <root> REPLICA IDENTITY <type> [CASCADE | FORCE CASCADE]
```

So, that the current syntax will behave the same as usual, and

With CASCADE
============
1. Root's RI updated
2. All children (including middle partitioned tables and leaf tables) RI updated unless 3
3. If any child’s RI is different from the root's RI, fail out, no change happens

With CASCADE FORCE
===================
1. Root's RI updated
2. All children (including middle partitioned tables and leaf tables) RI updated, prints a warning message when a
child’sRI is different from root’s RI 

"ALTER TABLE parent REPLICA IDENTITY” is a PG specific syntax, so the change won’t break the SQL standard. And
“CASCADE”is known keyword that has been used in many SQL commands. 

I can see the usefulness of “CASCADE” when a partitioned table has many partitions. A single command will be able to
updateall partitions’ RI. 

What do you think?

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/







pgsql-hackers by date:

Previous
From: shveta malik
Date:
Subject: Re: Proposal: Conflict log history table for Logical Replication
Next
From: Dilip Kumar
Date:
Subject: Re: Proposal: Conflict log history table for Logical Replication