From 0753ae11f1fe2f9f5a1529ceeb74c071f47ea670 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignesh21@gmail.com>
Date: Mon, 13 Jul 2026 14:49:20 +0530
Subject: [PATCH v2 1/3] Reject sequence synchronization against pre-PG19
 publishers

Sequence synchronization relies on pg_get_sequence_data(), which was
added in PostgreSQL 19.  Previously, requesting sequence sync against
an older publisher (via CREATE SUBSCRIPTION, ALTER SUBSCRIPTION ...
REFRESH SEQUENCES, or a sequencesync worker) would either reset the
affected sequences to INIT state and have the worker repeatedly fail
with a confusing "invalid query response" error.

Check the publisher's server version up front in both
AlterSubscription_refresh_seq() and copy_sequences(), and error out
immediately with a clear diagnosis when it predates PostgreSQL 19.

Also document the PostgreSQL 19 publisher requirement for sequence
replication in the logical replication documentation and in
ALTER SUBSCRIPTION ... REFRESH SEQUENCES.
---
 doc/src/sgml/logical-replication.sgml          | 18 +++++++++++++++++-
 doc/src/sgml/ref/alter_subscription.sgml       |  6 ++++++
 src/backend/commands/subscriptioncmds.c        | 14 ++++++++++++++
 src/backend/replication/logical/sequencesync.c | 13 +++++++++++++
 4 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml
index 690598bff98..274ba8b447a 100644
--- a/doc/src/sgml/logical-replication.sgml
+++ b/doc/src/sgml/logical-replication.sgml
@@ -1772,6 +1772,13 @@ Included in publications:
  <sect1 id="logical-replication-sequences">
   <title>Replicating Sequences</title>
 
+  <note>
+   <para>
+    Sequence synchronization requires the publisher to be running
+    <productname>PostgreSQL</productname> 19 or later.
+   </para>
+  </note>
+
   <para>
    To synchronize sequences from a publisher to a subscriber, first publish
    them using <link linkend="sql-createpublication-params-for-all-sequences">
@@ -2368,7 +2375,16 @@ CONTEXT:  processing remote data for replication origin "pg_16395" during "INSER
      <command>ALTER SUBSCRIPTION ... REFRESH SEQUENCES</command></link>
      or by copying the current data from the publisher (perhaps using
      <command>pg_dump</command>) or by determining a sufficiently high value
-     from the tables themselves.
+     from the tables themselves.  Note that
+     <link linkend="sql-altersubscription-params-refresh-sequences">
+     <command>ALTER SUBSCRIPTION ... REFRESH SEQUENCES</command></link> only
+     re-synchronizes sequences that are already known to the subscription
+     (see <xref linkend="logical-replication-sequences"/>); in particular, it
+     requires the publisher to be running <productname>PostgreSQL</productname>
+     19 or later. Before relying on it to prepare for a switchover or
+     failover, confirm that the publisher's version supports sequence
+     replication and that the sequences of interest are already known to the
+     subscription.
     </para>
    </listitem>
 
diff --git a/doc/src/sgml/ref/alter_subscription.sgml b/doc/src/sgml/ref/alter_subscription.sgml
index 8d64744375a..6fc3e07a2d5 100644
--- a/doc/src/sgml/ref/alter_subscription.sgml
+++ b/doc/src/sgml/ref/alter_subscription.sgml
@@ -245,6 +245,12 @@ ALTER SUBSCRIPTION <replaceable class="parameter">name</replaceable> RENAME TO <
       sequences are subscribed. Run <literal>REFRESH PUBLICATION</literal>
       first if the publication's set of sequences has changed.
      </para>
+     <note>
+      <para>
+       Sequence replication requires the publisher to be running
+       <productname>PostgreSQL</productname> 19 or later.
+      </para>
+     </note>
      <para>
       See <xref linkend="sequence-definition-mismatches"/> for
       recommendations on how to handle any warnings about sequence definition
diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c
index 4292e7fb8f4..e2a1abfb103 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -1380,6 +1380,20 @@ AlterSubscription_refresh_seq(Subscription *sub)
 	{
 		List	   *subrel_states;
 
+		/*
+		 * Sequence synchronization relies on pg_get_sequence_data(), which
+		 * is only available since PostgreSQL 19.  Fail immediately with a
+		 * clear diagnosis instead of resetting the sequences to INIT state
+		 * and letting a sequencesync worker repeatedly fail trying to fetch
+		 * sequence data with a query that can never succeed against this
+		 * publisher.
+		 */
+		if (walrcv_server_version(wrconn) < 190000)
+			ereport(ERROR,
+					errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+					errmsg("cannot synchronize sequences for subscription \"%s\" because the publisher is running a version earlier than PostgreSQL 19",
+						   sub->name));
+
 		check_publications_origin_sequences(wrconn, sub->publications, true,
 											sub->origin, NULL, 0, sub->name);
 
diff --git a/src/backend/replication/logical/sequencesync.c b/src/backend/replication/logical/sequencesync.c
index 770fa5de10b..98a21b34ed7 100644
--- a/src/backend/replication/logical/sequencesync.c
+++ b/src/backend/replication/logical/sequencesync.c
@@ -435,6 +435,19 @@ copy_sequences(WalReceiverConn *conn)
 	StringInfoData cmd;
 	MemoryContext oldctx;
 
+	/*
+	 * Sequence synchronization relies on pg_get_sequence_data(), which is
+	 * only available since PostgreSQL 19.  Fail with a clear diagnosis
+	 * instead of sending a query that the publisher cannot execute (or
+	 * cannot even parse), which would otherwise make this worker exit with
+	 * a confusing "invalid query response" error.
+	 */
+	if (walrcv_server_version(conn) < 190000)
+		ereport(ERROR,
+				errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+				errmsg("cannot synchronize sequences for subscription \"%s\" because the publisher is running a version earlier than PostgreSQL 19",
+					   MySubscription->name));
+
 	initStringInfo(&seqstr);
 	initStringInfo(&cmd);
 
-- 
2.50.1 (Apple Git-155)

