Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vul63-004z1y-2x for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Feb 2026 05:36:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vul61-00H8Xi-19 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Feb 2026 05:36:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vul60-00H8XZ-32 for pgsql-hackers@lists.postgresql.org; Tue, 24 Feb 2026 05:36:33 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vul5x-00000000z1Z-30op for pgsql-hackers@lists.postgresql.org; Tue, 24 Feb 2026 05:36:32 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-38708180241so39897821fa.1 for ; Mon, 23 Feb 2026 21:36:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1771911388; cv=none; d=google.com; s=arc-20240605; b=EfWPM5EalygOe+4gZX99btzxmwaYz8HDSshf1c1eQJcrUzZPJfIupFDN9Ibt8ZQn4N aUajap+Gmejqp0kwRCvgxPo7shNnfThLxGshJY6bTLQ3Y/7QVbrgbfSFnTy95KFepDib zCgk1K9VC2apw8+4qmXujwsPf64UwDZ7eVY8dzHYEl8fhTlIDO78yqDR3GzDJtQfWP6i qiNvuo3JNrUZ468GA01hvN8MINeH9QiVvMK8Hrzg/tQVSO3y3U9hoxH8jeP4PezH9Okd u5ze38wkI4uEiAfJJ3hO+L3hioEHfcXr3Egou91vJtKtuG/OJDMIljUBeVuXiNIx1yVL M5xA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=CyBr2vQp8Dxc3IoXHp4iP5fPcln8/DidVnb7sV+4HjQ=; fh=ylvfFqCDOjpHFqx3AqvwpOUzCxzh4XtVo8H8LCXzIPw=; b=J32rnOvDnGbmylTcKM8U3cNdRZvwVLosx+yQZ1WYKWgNqWAOnej8YbSlbotOZDdZtj lhb3u7hfBppd4d5L3pvnIkybo8vrH9UbYvHuRQbv3EHFkLyHmDer1jolKIjpthLK3Q0e jaZCMk+kylQLuJtmF5gfnpUEIxqeotDMllYK9Rb6hOwQr//edlyEX+HYD0Y1TPEsS4L6 HlJhlGCFmu97i9d4tv05o4SdcSzS4QfMnrolvylaYoZTx1i5CwHe5muUQCeM1FS5LmXG FDoQ/+02DJ+fEfiyO1AazRMrK70yBu/LFfPpOnmCnaLkxMR5XdZqW9DSkabZ+XMaLMbs iwzg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771911388; x=1772516188; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=CyBr2vQp8Dxc3IoXHp4iP5fPcln8/DidVnb7sV+4HjQ=; b=nrenTbkCVl8rfc6RxhubLckXt/21GQ1DCkZl+x8FZG1xYL9Nmf+6Sl3ysIDIzawecW HPwpYZcML53nfeYvJuXnClsgbSv6iGa/LunvhozCql37iI1o+iZ1z0XgsrPaSS81YWA8 AUSHigJGljDANR4oToJM/sagPO5NZsxuCXywQ4ma5GInF0mgmcX6XXgqyZHfbqIvTDKT TDLmQHhjAtprww557y5isAn1rMwZ57DsOX7bB7Q30QfV43gyB4/XjDpeJIxWf8N1r0He aItxXlANjAcp+1ZiK0Eskq3KF8O87ZGF0FP9IPgx0QQlIwlGCcYeLc+XgVrgHUZ12kiH SAaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771911388; x=1772516188; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=CyBr2vQp8Dxc3IoXHp4iP5fPcln8/DidVnb7sV+4HjQ=; b=EAZwBmde6lJKMDKBlrJ0cSxc7qE4NwgrWn7TEtDAkOYjnFgYHETiMCWNwbOdSOuBAh RLJQoQKmLTpHMOeKvddd52WmP9gXrSKugH8GGdYde8199l4K8ixKysS4HnGlIgZF+WQc 1D/27HfeyygItv5wZC+IDiU2hcjFqTYnstg+rTHDCQZHwNHm8lFoSlfR2rR4ziV3olgf jxtKEamSLCkHELNRHFKrz8L/ScQCm5pAsln+U27MRq/6R4YIfILUpA/z4kmCwykifalS LLY9TR+BwXwCHqlUK9FyUQPfLp91diH+y9L033nbcCKX+xbiAUSxYoJExh30weAHIH+t +RFw== X-Forwarded-Encrypted: i=1; AJvYcCW+c7+o48TCcjr3wGv05W9WZPDRAPigbmYMrSXrABIJx7yhwRjNpJZf0qJmlER+N0uTg9cWyGpqzb7dL3wk@lists.postgresql.org X-Gm-Message-State: AOJu0YxHXE7aJLbQcgsxBYsPZAHyyGYGjuy1Om4cBUYqx6dm+YRRk8m3 hSCSFjxMcrGmSufcLJc0PxE1bzsGYLDOyiJWgzf7HE7RDSH0CldJUuQEleg4l8Cc8EU87h4bNg2 Ta09iHe8qM2Ovar1CGLbn2Qel1GECXKE= X-Gm-Gg: ATEYQzwZo//TG1Rc13HL++5fJRv31g3vNQZN2MXzgHYKLESNHEVeeoTG7UxtZxF4ZSY wgO9w8VK8Iuy/z/Eb1rD+daK4al8+tFWWkEhjqdfbZVVVHd4O33Y4tWO/PrLMqUENZVQhQfWzw5 9GbZEWZ4txD3LF0LiGT+3ZJFMIAKk/SN13TCm5LVGvKj1DQQSscqka0iVsVx/cGw7gBMOshPx43 EzPkrDM/p/tmMQ0ZDbURLojqHgJCzLf4h6uzySBVXoJHcbXyfjCxZLZRXsPTg6DBPXNM96GVWM+ m6z+5YDYO4hdyZjgm4HNgqk7lFbJmg4A5IcnJ1qGz7j059R+FnOwSWrP2UXsPO8ndYK/hxfG X-Received: by 2002:a2e:be1a:0:b0:383:24fe:4eaf with SMTP id 38308e7fff4ca-389a5de4d48mr28565251fa.30.1771911387765; Mon, 23 Feb 2026 21:36:27 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Amit Kapila Date: Tue, 24 Feb 2026 11:06:15 +0530 X-Gm-Features: AaiRm53NwTO1dEo1Aak5e-d0tUBCFlzS-FMVt219pJhdVQfIJterO8aOhNuuFBo Message-ID: Subject: Re: [PATCH] Support automatic sequence replication To: "Hayato Kuroda (Fujitsu)" Cc: Ajin Cherian , shveta malik , Ashutosh Sharma , PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Feb 23, 2026 at 6:56=E2=80=AFAM Hayato Kuroda (Fujitsu) wrote: > > Thanks for updating the patch. Here are my comments. > > 01. start_sequence_sync() > ``` > /* Need to start transaction for cache lookup */ > StartTransactionCommand(); > ``` > > Here, we must check additional parameter changes, such as conninfo and pa= sswordrequired. > Also, it's inefficient because transactions are started each time. > > Can we re-use maybe_reread_subscription() here? Some parameters do not ta= ke > effect for the sequence sync worker, but it is OK to exit even if they > are changed. If we use the function, no need to include "storage/ipc.h". > I also think it is better to restart the sequencesync worker on subscription parameter change and probably it is okay to use maybe_reread_subscription() but need to be careful to avoid anything apply_worker specific in that function. BTW, what happens now without this change when apply_worker stops due to parameter change, does sequencesync worker continue? If so, we should make it restart as we are doing for apply worker. Also, shouldn't we need to invoke AcceptInvalidationMessages() as we are doing in apply worker when not in a remote transaction? I think it will be required to get local_sequence definition changes , if any. > 02. match_previous_words > > No need to remove "REFRESH SEQUENCES" anymore. > > 03. CopySeqResult > ``` > COPYSEQ_NOWORK, > ``` > > It describes why the copying is skipped. How about "COPYSEQ_NO_DRIFT"? > +1. > 04. LogicalRepSyncSequences() > > ``` > oldctx =3D MemoryContextSwitchTo(ApplyContext); > > seq =3D palloc0_object(LogicalRepSequenceInfo); > seq->localrelid =3D subrel->srrelid; > seq->nspname =3D get_namespace_name(RelationGetNamespace(sequence_rel))= ; > seq->seqname =3D pstrdup(RelationGetRelationName(sequence_rel)); > seq->relstate =3D relstate; > seqinfos =3D lappend(seqinfos, seq); > > MemoryContextSwitchTo(oldctx); > ``` > > ISTM they are palloc'd but not pfree'd. > They are freed, see following code. But it seems nspname and seqname should be freed separately for each sequence element and each sequence element also needs to be freed independently. + /* Clean up */ + list_free(seqinfos); > Since the sequencesync worker now has a long lifetime, we must take care = of the > memory allocation/freeing more carefully. How about introducing per-inter= action > memory context like ApplyMessageContext? > Yeah, I feel that would be a better approach than retail pfree especially because it is also required at other places in sequencesync worker (see places where currently ApplyContext is used in sequencesync worker). I think it would be better if we name this new context as SequenceSyncContext. Few other miscellaneous comments =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D 1. - Refreshing Out-of-Sync Sequences - - Subscriber sequence values will become out of sync as the publisher - advances them. - - - To detect this, compare the - pg_subscription_rel.srsublsn - on the subscriber with the page_lsn obtaine= d - from the pg_get_sequence_data - function for the sequence on the publisher. Then run - - ALTER SUBSCRIPTION ... REFRESH SEQUENCES to - re-synchronize if necessary. - + Out-of-Sync Sequences Each sequence caches a block of values (typically 32) in memory befor= e @@ -1961,16 +1941,6 @@ Publications: ------------+-----------+------------ 610 | t | 0/017CEDF8 (1 row) - - - - The difference between the sequence page LSNs on the publisher and the - sequence page LSNs on the subscriber indicates that the sequences are = out - of sync. Re-synchronize all sequences known to the subscriber using - - ALTER SUBSCRIPTION ... REFRESH SEQUENCES. - -/* sub # */ ALTER SUBSCRIPTION sub1 REFRESH SEQUENCES; ... ... - - - Incremental sequence changes are not replicated. Although the data i= n - serial or identity columns backed by sequences will be replicated as = part - of the table, the sequences themselves do not replicate ongoing chang= es. - On the subscriber, a sequence will retain the last value it synchroni= zed - from the publisher. If the subscriber is used as a read-only database= , - then this should typically not be a problem. If, however, some kind = of - switchover or failover to the subscriber database is intended, then t= he - sequences would need to be updated to the latest values, either by - executing - ALTER SUBSCRIPTION ... REFRESH SEQUENCES - or by copying the current data from the publisher (perhaps using - pg_dump) or by determining a sufficiently high val= ue - from the tables themselves. - - - I think this can still happen after this patch but chances are much lower, so we need some of this before upgrade. We should reword it accordingly. Similarly check other parts of the doc you removed. 2. - "logical replication synchronization for subscription \"%s\", sequence \"%s.%s\" has finished", + "logical replication sync for subscription \"%s\", sequence \"%s.%s\" has been updated", Is there a need to shorten synchronization to sync in above message? 3. elog(DEBUG1, - "logical replication sequence synchronization for subscription \"%s\" - batch #%d =3D %d attempted, %d succeeded, %d mismatched, %d insufficient permission, %d missing from publisher, %d skipped", - MySubscription->name, - (cur_batch_base_index / MAX_SEQUENCES_SYNC_PER_BATCH) + 1, - batch_size, batch_succeeded_count, batch_mismatched_count, - batch_insuffperm_count, batch_missing_count, batch_skipped_count); + "logical replication sequence synchronization for subscription \"%s\" - batch #%d =3D %d attempted, %d succeeded, %d mismatched, %d insufficient permission, %d missing from publisher, %d skipped, %d no drift", + MySubscription->name, + (cur_batch_base_index / MAX_SEQUENCES_SYNC_PER_BATCH) + 1, + batch_size, batch_succeeded_count, batch_mismatched_count, + batch_insuffperm_count, batch_missing_count, batch_skipped_count, batch_no_drift); Here, the formatting of message is incorrect. 4. table_states_not_ready =3D NIL; - if (!IsTransactionState()) Spurious line removal. 5. MemoryContextSwitchTo(oldctx); - + list_free_deep(seq_states); /* * Does the subscription have tables? * @@ -260,7 +253,6 @@ FetchRelationStates(bool *has_pending_subtables, */ has_subtables =3D (table_states_not_ready !=3D NIL) || HasSubscriptionTables(MySubscription->oid); - /* * If the subscription relation cache has been invalidated since we * entered this routine, we still use and return the relations we just @@ -271,10 +263,8 @@ FetchRelationStates(bool *has_pending_subtables, if (relation_states_validity =3D=3D SYNC_RELATIONS_STATE_REBUILD_STARTED) relation_states_validity =3D SYNC_RELATIONS_STATE_VALID; } - if (has_pending_subtables) *has_pending_subtables =3D has_subtables; - if (has_pending_subsequences) ... ... } + if (rc & WL_TIMEOUT) All above places have spurious line removals and additions which made code harder to understand. 6. else if (Matches("ALTER", "SUBSCRIPTION", MatchAny)) COMPLETE_WITH("CONNECTION", "ENABLE", "DISABLE", "OWNER TO", - "RENAME TO", "REFRESH PUBLICATION", "REFRESH SEQUENCES", - "SET", "SKIP (", "ADD PUBLICATION", "DROP PUBLICATION"); + "RENAME TO", "REFRESH PUBLICATION", "SET", "SKIP (", + "ADD PUBLICATION", "DROP PUBLICATION"); unrelated change. 7. + else + { + + /* + * Double the sleep time, but not beyond + * the maximum allowable value. + */ + sleep_ms =3D Min(sleep_ms * 2, SEQSYNC_MAX_SLEEP_MS); Comments are not properly aligned. --=20 With Regards, Amit Kapila.