Re: Logical Replication of sequences

From: vignesh C <vignesh21(at)gmail(dot)com>
To: shveta malik <shveta(dot)malik(at)gmail(dot)com>
Cc: Nisha Moond <nisha(dot)moond412(at)gmail(dot)com>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Peter Smith <smithpb2250(at)gmail(dot)com>, "Hayato Kuroda (Fujitsu)" <kuroda(dot)hayato(at)fujitsu(dot)com>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com>, Peter Eisentraut <peter(at)eisentraut(dot)org>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Euler Taveira <euler(at)eulerto(dot)com>, Michael Paquier <michael(at)paquier(dot)xyz>, "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com>, "Jonathan S(dot) Katz" <jkatz(at)postgresql(dot)org>
Subject: Re: Logical Replication of sequences
Date: 2025-07-04 10:23:37
Message-ID: CALDaNm1wDWEjoEF1ZPGnGjwXOAeU6L5xicykT6=3JLYhizDcew@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, 1 Jul 2025 at 15:20, shveta malik <shveta(dot)malik(at)gmail(dot)com> wrote:
> On Mon, Jun 30, 2025 at 3:21 PM Nisha Moond <nisha(dot)moond412(at)gmail(dot)com> wrote:
> >
> > Please find the attached v20250630 patch set addressing above comments
> > and other comments in [1],[2],[3] and [4].
>
> Thanks for the patches. I am still in process of reviewing it but
> please find few comments:
>
> 1)
> + if (pset.sversion >= 180000)
> + appendPQExpBuffer(&buf,
> + ",\n puballsequences AS \"%s\"",
> + gettext_noop("All sequences"));
>
> The server version check throughout the patch can be modified to 19000
> as a new branch is created now.

Modified

> 2)
> + bool all_pub; /* Special publication for all tables,
> + * sequecnes */
>
> a) Typo: sequecnes --> sequences

Modified

> b) It is not clear from the comment that when will it be true? Will it
> be set when either of all-tables or all-sequences is given or does it
> need both?

Updated the comments

> 3)
> postgres=# create publication pub1 for all sequences WITH ( PUBLISH='delete');
> CREATE PUBLICATION
> postgres=# create publication pub2 for all tables, sequences WITH
> (PUBLISH='update');
> CREATE PUBLICATION
>
> For the first command, 'WITH ( publication_parameter..' is useless.
> For the second command, it is applicable only for 'all tables'.
>
> a) I am not sure if we even allow WITH in the first command?
> b) In the second command, even if we allow it, there should be some
> sort of NOTICE informing that it is applicable only to 'TABLES'.
>
> Thoughts?
>
> Also we allowed altering publication_parameter for all-sequences publication:
>
> postgres=# alter publication pub1 set (publish='insert,update');
> ALTER PUBLICATION
>
> c) Should this be restricted as well? Thoughts?

We have documented that the parameter of with clause is not applicable
for sequences. I feel that all the above statements are ok with the
documentation mentioned.

Regarding the comments from [1].
> 5)
> In LogicalRepSyncSequences, why are we allocating it in a permanent
> memory context?
>
> + /* Allocate the tracking info in a permanent memory context. */
> + oldctx = MemoryContextSwitchTo(CacheMemoryContext);
> + foreach_ptr(SubscriptionRelState, seq_state, sequences)
> + {
> + SubscriptionRelState *rstate = palloc(sizeof(SubscriptionRelState));
> +
> + memcpy(rstate, seq_state, sizeof(SubscriptionRelState));
> + sequences_not_synced = lappend(sequences_not_synced, rstate);
> + }
> + MemoryContextSwitchTo(oldctx);
>
> Same for 'seq_info' allocation.

When we are in between a transaction we will be using
TopTransactionContext. We can palloc() in TopTransactionContext and
safely use that memory throughout the transaction. But we cannot
cannot access memory allocated in TopTransactionContext after
CommitTransaction() finishes, because TopTransactionContext is
explicitly reset (or deleted) at the end of the transaction.
This is the reason we have to use CacheMemoryContext here.

The rest of the comments are fixed.

Also one of the pending comment from [2] is fixed.
> 4. Since we are not adding sequences in the list 'sub_remove_rels',
> should we only palloc for (the count of no. of tables)? Is it worth
> the effort?
> /*
> * Rels that we want to remove from subscription and drop any slots
> * and origins corresponding to them.
> */
> sub_remove_rels = palloc(subrel_count * sizeof(SubRemoveRels));

The attached v20250704 version patch has the changes for the same.
[1] - https://www.postgresql.org/message-id/CAJpy0uD%2B7UtDs9%3DCx03BAckMPDdW7C6ifGF_Lc54B8iH6RNXWQ%40mail.gmail.com
[2] - https://www.postgresql.org/message-id/CANhcyEWKhHWFzpdAF6czbwq76NRDNCecDqQNtN6Bomn26mqHFw%40mail.gmail.com

Regards,
Vignesh

Attachment Content-Type Size
v20250704-0001-Introduce-pg_sequence_state-function-for-e.patch application/octet-stream 7.3 KB
v20250704-0005-New-worker-for-sequence-synchronization-du.patch application/octet-stream 74.1 KB
v20250704-0004-Introduce-REFRESH-PUBLICATION-SEQUENCES-fo.patch application/octet-stream 43.1 KB
v20250704-0003-Reorganize-tablesync-Code-and-Introduce-sy.patch application/octet-stream 23.0 KB
v20250704-0002-Introduce-ALL-SEQUENCES-support-for-Postgr.patch application/octet-stream 104.8 KB
v20250704-0006-Documentation-for-sequence-synchronization.patch application/octet-stream 33.3 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2025-07-04 10:54:53 Re: Remove unused wait_event_info parameter in FileStartReadV()
Previous Message Aleksander Alekseev 2025-07-04 10:16:02 Re: Remove unused wait_event_info parameter in FileStartReadV()