Hi,

On Tue, Apr 18, 2023 at 01:40:51AM +0000, Hayato Kuroda (Fujitsu) wrote:
>
> I found a cfbot failure on macOS [1]. According to the log,
> "SELECT count(*) FROM t2" was executed before synchronization was done.
>
> ```
> [09:24:21.018](0.132s) not ok 18 - Table t2 should now have 3 rows on the new 
> subscriber
> ```
>
> With the patch present, wait_for_catchup() is executed after REFRESH, but
> it may not be sufficient because it does not check pg_subscription_rel.
> wait_for_subscription_sync() seems better for the purpose.

Fixed, thanks!

v5 attached with all previously mentioned fixes.
>From c6755ea3318220dc41bc315cc7acce4954e9b252 Mon Sep 17 00:00:00 2001
From: Julien Rouhaud <julien.rouh...@free.fr>
Date: Wed, 22 Feb 2023 09:19:32 +0800
Subject: [PATCH v5] Optionally preserve the full subscription's state during
 pg_upgrade

Previously, only the subscription metadata information was preserved.  Without
the list of relations and their state it's impossible to re-enable the
subscriptions without missing some records as the list of relations can only be
refreshed after enabling the subscription (and therefore starting the apply
worker).  Even if we added a way to refresh the subscription while enabling a
publication, we still wouldn't know which relations are new on the publication
side, and therefore should be fully synced, and which shouldn't.

Similarly, the subscription's replication origin are needed to ensure
that we don't replicate anything twice.

To fix this problem, this patch teaches pg_dump in binary upgrade mode to emit
additional ALTER SUBSCRIPTION subcommands that will restore the content of
pg_subscription_rel, and also provides an additional LSN parameter for CREATE
SUBSCRIPTION to restore the underlying replication origin remote LSN.  The new
ALTER SUBSCRIPTION subcommand and the new LSN parameter are not exposed to
users and only accepted in binary upgrade mode.

The new ALTER SUBSCRIPTION subcommand has the following syntax:

ALTER SUBSCRIPTION name ADD TABLE (relid = XYZ, state = 'x' [, lsn = 'X/Y'])

The relation is identified by its oid, as it's preserved during pg_upgrade.
The lsn is optional, and defaults to NULL / InvalidXLogRecPtr if not provided.
Explicitly passing InvalidXLogRecPtr (0/0) is however not allowed.

This mode is optional and not enabled by default.  A new
--preserve-subscription-state option is added to pg_upgrade to use it.  For
now, pg_upgrade will check that all the subscription have a valid replication
origin remote_lsn, and that all underlying relations are in 'r' (ready) state,
and will error out if that's not the case, logging the reason for the failure.

Author: Julien Rouhaud
Reviewed-by: FIXME
Discussion: https://postgr.es/m/20230217075433.u5mjly4d5cr4hcfe@jrouhaud
---
 doc/src/sgml/ref/pgupgrade.sgml          |  23 +++
 src/backend/commands/subscriptioncmds.c  |  75 +++++++-
 src/backend/parser/gram.y                |  11 ++
 src/bin/pg_dump/common.c                 |  22 +++
 src/bin/pg_dump/pg_backup.h              |   2 +
 src/bin/pg_dump/pg_dump.c                | 136 +++++++++++++-
 src/bin/pg_dump/pg_dump.h                |  15 ++
 src/bin/pg_upgrade/check.c               |  81 +++++++++
 src/bin/pg_upgrade/dump.c                |   3 +-
 src/bin/pg_upgrade/meson.build           |   1 +
 src/bin/pg_upgrade/option.c              |   6 +
 src/bin/pg_upgrade/pg_upgrade.h          |   1 +
 src/bin/pg_upgrade/t/003_subscription.pl | 220 +++++++++++++++++++++++
 src/include/nodes/parsenodes.h           |   3 +-
 src/tools/pgindent/typedefs.list         |   1 +
 15 files changed, 595 insertions(+), 5 deletions(-)
 create mode 100644 src/bin/pg_upgrade/t/003_subscription.pl

diff --git a/doc/src/sgml/ref/pgupgrade.sgml b/doc/src/sgml/ref/pgupgrade.sgml
index 7816b4c685..6af790c986 100644
--- a/doc/src/sgml/ref/pgupgrade.sgml
+++ b/doc/src/sgml/ref/pgupgrade.sgml
@@ -240,6 +240,29 @@ PostgreSQL documentation
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--preserve-subscription-state</option></term>
+      <listitem>
+       <para>
+        Fully preserve the logical subscription state if any.  That includes
+        the underlying replication origin with their remote LSN and the list of
+        relations in each subscription so that replication can be simply
+        resumed if the subscriptions are reactivated.
+       </para>
+       <para>
+        If this option isn't used, it is up to the user to reactivate the
+        subscriptions in a suitable way; see the subscription part in <xref
+        linkend="pg-dump-notes"/> for more information.
+       </para>
+       <para>
+        If this option is used and any of the subscription on the old cluster
+        has an unknown <varname>remote_lsn</varname> (0/0), or has any relation
+        in a state different from <literal>r</literal> (ready), the
+        <application>pg_upgrade</application> run will error.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>-?</option></term>
       <term><option>--help</option></term>
diff --git a/src/backend/commands/subscriptioncmds.c 
b/src/backend/commands/subscriptioncmds.c
index 56eafbff10..657db3791e 100644
--- a/src/backend/commands/subscriptioncmds.c
+++ b/src/backend/commands/subscriptioncmds.c
@@ -71,6 +71,8 @@
 #define SUBOPT_RUN_AS_OWNER                    0x00001000
 #define SUBOPT_LSN                                     0x00002000
 #define SUBOPT_ORIGIN                          0x00004000
+#define SUBOPT_RELID                           0x00008000
+#define SUBOPT_STATE                           0x00010000
 
 /* check if the 'val' has 'bits' set */
 #define IsSet(val, bits)  (((val) & (bits)) == (bits))
@@ -97,6 +99,8 @@ typedef struct SubOpts
        bool            runasowner;
        char       *origin;
        XLogRecPtr      lsn;
+       Oid                     relid;
+       char            state;
 } SubOpts;
 
 static List *fetch_table_list(WalReceiverConn *wrconn, List *publications);
@@ -353,6 +357,46 @@ parse_subscription_options(ParseState *pstate, List 
*stmt_options,
                        opts->specified_opts |= SUBOPT_LSN;
                        opts->lsn = lsn;
                }
+               else if (IsSet(supported_opts, SUBOPT_RELID) &&
+                                strcmp(defel->defname, "relid") == 0)
+               {
+                       Oid                     relid = defGetObjectId(defel);
+
+                       Assert(IsBinaryUpgrade);
+
+                       if (IsSet(opts->specified_opts, SUBOPT_RELID))
+                               errorConflictingDefElem(defel, pstate);
+
+                       if (!OidIsValid(relid))
+                       {
+                               char       *rel_str = defGetString(defel);
+
+                               ereport(ERROR,
+                                               
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                                errmsg("invalid relation 
identifier used: %s", rel_str)));
+                       }
+
+                       opts->specified_opts |= SUBOPT_RELID;
+                       opts->relid = relid;
+               }
+               else if (IsSet(supported_opts, SUBOPT_STATE) &&
+                                strcmp(defel->defname, "state") == 0)
+               {
+                       char       *state_str = defGetString(defel);
+
+                       Assert(IsBinaryUpgrade);
+
+                       if (IsSet(opts->specified_opts, SUBOPT_STATE))
+                               errorConflictingDefElem(defel, pstate);
+
+                       if (strlen(state_str) != 1)
+                               ereport(ERROR,
+                                               
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                                errmsg("invalid relation 
state: %s", state_str)));
+
+                       opts->specified_opts |= SUBOPT_STATE;
+                       opts->state = defGetString(defel)[0];
+               }
                else
                        ereport(ERROR,
                                        (errcode(ERRCODE_SYNTAX_ERROR),
@@ -580,6 +624,7 @@ CreateSubscription(ParseState *pstate, 
CreateSubscriptionStmt *stmt,
        bits32          supported_opts;
        SubOpts         opts = {0};
        AclResult       aclresult;
+       RepOriginId originid;
 
        /*
         * Parse and check options.
@@ -592,6 +637,8 @@ CreateSubscription(ParseState *pstate, 
CreateSubscriptionStmt *stmt,
                                          SUBOPT_STREAMING | 
SUBOPT_TWOPHASE_COMMIT |
                                          SUBOPT_DISABLE_ON_ERR | 
SUBOPT_PASSWORD_REQUIRED |
                                          SUBOPT_RUN_AS_OWNER | SUBOPT_ORIGIN);
+       if (IsBinaryUpgrade)
+               supported_opts |= SUBOPT_LSN;
        parse_subscription_options(pstate, stmt->options, supported_opts, 
&opts);
 
        /*
@@ -718,7 +765,12 @@ CreateSubscription(ParseState *pstate, 
CreateSubscriptionStmt *stmt,
        recordDependencyOnOwner(SubscriptionRelationId, subid, owner);
 
        ReplicationOriginNameForLogicalRep(subid, InvalidOid, originname, 
sizeof(originname));
-       replorigin_create(originname);
+       originid = replorigin_create(originname);
+
+       if (IsBinaryUpgrade && IsSet(opts.specified_opts, SUBOPT_LSN))
+               replorigin_advance(originid, opts.lsn, InvalidXLogRecPtr,
+                                                  false /* backward */ ,
+                                                  false /* WAL log */ );
 
        /*
         * Connect to remote side to execute requested commands and fetch table
@@ -1428,6 +1480,27 @@ AlterSubscription(ParseState *pstate, 
AlterSubscriptionStmt *stmt,
                                break;
                        }
 
+               case ALTER_SUBSCRIPTION_ADD_TABLE:
+                       {
+                               if (!IsBinaryUpgrade)
+                                       ereport(ERROR,
+                                                       
(errcode(ERRCODE_SYNTAX_ERROR)),
+                                                       errmsg("ALTER 
SUBSCRIPTION ... ADD TABLE is not supported"));
+
+                               supported_opts = SUBOPT_RELID | SUBOPT_STATE | 
SUBOPT_LSN;
+                               parse_subscription_options(pstate, 
stmt->options,
+                                                                               
   supported_opts, &opts);
+
+                               /* relid and state should always be provided. */
+                               Assert(IsSet(opts.specified_opts, 
SUBOPT_RELID));
+                               Assert(IsSet(opts.specified_opts, 
SUBOPT_STATE));
+
+                               AddSubscriptionRelState(subid, opts.relid, 
opts.state,
+                                                                               
opts.lsn);
+
+                               break;
+                       }
+
                default:
                        elog(ERROR, "unrecognized ALTER SUBSCRIPTION kind %d",
                                 stmt->kind);
diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y
index acf6cf4866..0432bf2cb4 100644
--- a/src/backend/parser/gram.y
+++ b/src/backend/parser/gram.y
@@ -10695,6 +10695,17 @@ AlterSubscriptionStmt:
                                        n->options = $5;
                                        $$ = (Node *) n;
                                }
+                       /* for binary upgrade only */
+                       | ALTER SUBSCRIPTION name ADD_P TABLE definition
+                               {
+                                       AlterSubscriptionStmt *n =
+                                               makeNode(AlterSubscriptionStmt);
+
+                                       n->kind = ALTER_SUBSCRIPTION_ADD_TABLE;
+                                       n->subname = $3;
+                                       n->options = $6;
+                                       $$ = (Node *) n;
+                               }
                ;
 
 /*****************************************************************************
diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c
index 5d988986ed..29d2cc7cee 100644
--- a/src/bin/pg_dump/common.c
+++ b/src/bin/pg_dump/common.c
@@ -24,6 +24,7 @@
 #include "catalog/pg_operator_d.h"
 #include "catalog/pg_proc_d.h"
 #include "catalog/pg_publication_d.h"
+#include "catalog/pg_subscription_d.h"
 #include "catalog/pg_type_d.h"
 #include "common/hashfn.h"
 #include "fe_utils/string_utils.h"
@@ -264,6 +265,9 @@ getSchemaData(Archive *fout, int *numTablesPtr)
        pg_log_info("reading subscriptions");
        getSubscriptions(fout);
 
+       pg_log_info("reading subscription membership of tables");
+       getSubscriptionTables(fout);
+
        free(inhinfo);                          /* not needed any longer */
 
        *numTablesPtr = numTables;
@@ -974,6 +978,24 @@ findPublicationByOid(Oid oid)
        return (PublicationInfo *) dobj;
 }
 
+/*
+ * findSubscriptionByOid
+ *       finds the DumpableObject for the subscription with the given oid
+ *       returns NULL if not found
+ */
+SubscriptionInfo *
+findSubscriptionByOid(Oid oid)
+{
+       CatalogId       catId;
+       DumpableObject *dobj;
+
+       catId.tableoid = SubscriptionRelationId;
+       catId.oid = oid;
+       dobj = findObjectByCatalogId(catId);
+       Assert(dobj == NULL || dobj->objType == DO_SUBSCRIPTION);
+       return (SubscriptionInfo *) dobj;
+}
+
 
 /*
  * recordExtensionMembership
diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h
index aba780ef4b..8c82657e76 100644
--- a/src/bin/pg_dump/pg_backup.h
+++ b/src/bin/pg_dump/pg_backup.h
@@ -200,6 +200,8 @@ typedef struct _dumpOptions
 
        int                     sequence_data;  /* dump sequence data even in 
schema-only mode */
        int                     do_nothing;
+
+       int             preserve_subscriptions;
 } DumpOptions;
 
 /*
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 058244cd17..a5336acb5b 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -431,6 +431,7 @@ main(int argc, char **argv)
                {"table-and-children", required_argument, NULL, 12},
                {"exclude-table-and-children", required_argument, NULL, 13},
                {"exclude-table-data-and-children", required_argument, NULL, 
14},
+               {"preserve-subscription-state", no_argument, 
&dopt.preserve_subscriptions, 1},
 
                {NULL, 0, NULL, 0}
        };
@@ -714,6 +715,10 @@ main(int argc, char **argv)
        if (dopt.do_nothing && dopt.dump_inserts == 0)
                pg_fatal("option --on-conflict-do-nothing requires option 
--inserts, --rows-per-insert, or --column-inserts");
 
+       /* --preserve-subscription-state requires --binary-upgrade */
+       if (dopt.preserve_subscriptions && !dopt.binary_upgrade)
+               pg_fatal("option --preserve-subscription-state requires option 
--binary-upgrade");
+
        /* Identify archive format to emit */
        archiveFormat = parseArchiveFormat(format, &archiveMode);
 
@@ -4585,6 +4590,92 @@ is_superuser(Archive *fout)
        return false;
 }
 
+/*
+ * getSubscriptionTables
+ *       get information about the given subscription's relations
+ */
+void
+getSubscriptionTables(Archive *fout)
+{
+       SubscriptionInfo *subinfo;
+       SubRelInfo *rels = NULL;
+       PQExpBuffer query;
+       PGresult   *res;
+       int                     i_srsubid;
+       int                     i_srrelid;
+       int                     i_srsubstate;
+       int                     i_srsublsn;
+       int                     i_nrels;
+       int                     i,
+                               cur_rel = 0,
+                               ntups,
+                               last_srsubid = InvalidOid;
+
+       if (!fout->dopt->binary_upgrade || !fout->dopt->preserve_subscriptions 
||
+               fout->remoteVersion < 100000)
+       {
+               return;
+       }
+
+       query = createPQExpBuffer();
+
+       appendPQExpBuffer(query, "SELECT srsubid, srrelid, srsubstate, 
srsublsn,"
+                                         " count(*) OVER (PARTITION BY 
srsubid) AS nrels"
+                                         " FROM pg_subscription_rel"
+                                         " ORDER BY srsubid");
+
+       res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
+
+       ntups = PQntuples(res);
+
+       if (ntups == 0)
+               goto cleanup;
+
+       /*
+        * Get subscription relation fields.
+        */
+       i_srsubid = PQfnumber(res, "srsubid");
+       i_srrelid = PQfnumber(res, "srrelid");
+       i_srsubstate = PQfnumber(res, "srsubstate");
+       i_srsublsn = PQfnumber(res, "srsublsn");
+       i_nrels = PQfnumber(res, "nrels");
+
+       for (i = 0; i < ntups; i++)
+       {
+               int                     cur_srsubid = atooid(PQgetvalue(res, i, 
i_srsubid));
+
+               /*
+                * If we switched to a new subscription, setup the necessary 
fields in
+                * the SubscriptionInfo and reset the cur_rel counter.
+                */
+               if (cur_srsubid != last_srsubid)
+               {
+                       int                     nrels;
+
+                       subinfo = findSubscriptionByOid(cur_srsubid);
+
+                       nrels = atooid(PQgetvalue(res, i, i_nrels));
+                       rels = pg_malloc(nrels * sizeof(SubRelInfo));
+
+                       subinfo->subrels = rels;
+                       subinfo->nrels = nrels;
+
+                       last_srsubid = cur_srsubid;
+                       cur_rel = 0;
+               }
+
+               rels[cur_rel].srrelid = atooid(PQgetvalue(res, i, i_srrelid));
+               rels[cur_rel].srsubstate = PQgetvalue(res, i, i_srsubstate)[0];
+               rels[cur_rel].srsublsn = pg_strdup(PQgetvalue(res, i, 
i_srsublsn));
+
+               cur_rel++;
+       }
+
+cleanup:
+       PQclear(res);
+       destroyPQExpBuffer(query);
+}
+
 /*
  * getSubscriptions
  *       get information about subscriptions
@@ -4610,6 +4701,7 @@ getSubscriptions(Archive *fout)
        int                     i_subpublications;
        int                     i_subbinary;
        int                     i_subpasswordrequired;
+       int                     i_suboriginremotelsn;
        int                     i,
                                ntups;
 
@@ -4664,15 +4756,19 @@ getSubscriptions(Archive *fout)
        if (fout->remoteVersion >= 160000)
                appendPQExpBufferStr(query,
                                                         " s.suborigin,\n"
-                                                        " 
s.subpasswordrequired\n");
+                                                        " 
s.subpasswordrequired,\n");
        else
                appendPQExpBuffer(query,
                                                  " '%s' AS suborigin,\n"
-                                                 " 't' AS 
subpasswordrequired\n",
+                                                 " 't' AS 
subpasswordrequired,\n",
                                                  LOGICALREP_ORIGIN_ANY);
 
+       appendPQExpBufferStr(query, "o.remote_lsn\n");
+
        appendPQExpBufferStr(query,
                                                 "FROM pg_subscription s\n"
+                                                "LEFT JOIN 
pg_replication_origin_status o \n"
+                                                "    ON o.external_id = 'pg_' 
|| s.oid::text \n"
                                                 "WHERE s.subdbid = (SELECT oid 
FROM pg_database\n"
                                                 "                   WHERE 
datname = current_database())");
 
@@ -4698,6 +4794,7 @@ getSubscriptions(Archive *fout)
        i_subdisableonerr = PQfnumber(res, "subdisableonerr");
        i_suborigin = PQfnumber(res, "suborigin");
        i_subpasswordrequired = PQfnumber(res, "subpasswordrequired");
+       i_suboriginremotelsn = PQfnumber(res, "remote_lsn");
 
        subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo));
 
@@ -4730,6 +4827,18 @@ getSubscriptions(Archive *fout)
                subinfo[i].suborigin = pg_strdup(PQgetvalue(res, i, 
i_suborigin));
                subinfo[i].subpasswordrequired =
                        pg_strdup(PQgetvalue(res, i, i_subpasswordrequired));
+               if (PQgetisnull(res, i, i_suboriginremotelsn))
+                       subinfo[i].suboriginremotelsn = NULL;
+               else
+                       subinfo[i].suboriginremotelsn =
+                               pg_strdup(PQgetvalue(res, i, 
i_suboriginremotelsn));
+
+               /*
+                * For now assume there's no relation associated with the
+                * subscription. Later code might update this field and allocate
+                * subrels as needed.
+                */
+               subinfo[i].nrels = 0;
 
                /* Decide whether we want to dump it */
                selectDumpableObject(&(subinfo[i].dobj), fout);
@@ -4814,9 +4923,31 @@ dumpSubscription(Archive *fout, const SubscriptionInfo 
*subinfo)
        if (strcmp(subinfo->subpasswordrequired, "t") != 0)
                appendPQExpBuffer(query, ", password_required = false");
 
+       if (dopt->binary_upgrade && dopt->preserve_subscriptions &&
+               subinfo->suboriginremotelsn)
+       {
+               appendPQExpBuffer(query, ", lsn = '%s'", 
subinfo->suboriginremotelsn);
+       }
+
        appendPQExpBufferStr(query, ");\n");
 
        if (subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
+       {
+               for (i = 0; i < subinfo->nrels; i++)
+               {
+                       appendPQExpBuffer(query, "\nALTER SUBSCRIPTION %s ADD 
TABLE "
+                                                         "(relid = %u, state = 
'%c'",
+                                                         qsubname,
+                                                         
subinfo->subrels[i].srrelid,
+                                                         
subinfo->subrels[i].srsubstate);
+
+                       if (subinfo->subrels[i].srsublsn[0] != '\0')
+                               appendPQExpBuffer(query, ", LSN = '%s'",
+                                                                 
subinfo->subrels[i].srsublsn);
+
+                       appendPQExpBufferStr(query, ");");
+               }
+
                ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
                                         ARCHIVE_OPTS(.tag = subinfo->dobj.name,
                                                                  .owner = 
subinfo->rolname,
@@ -4824,6 +4955,7 @@ dumpSubscription(Archive *fout, const SubscriptionInfo 
*subinfo)
                                                                  .section = 
SECTION_POST_DATA,
                                                                  .createStmt = 
query->data,
                                                                  .dropStmt = 
delq->data));
+       }
 
        if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
                dumpComment(fout, "SUBSCRIPTION", qsubname,
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index ed6ce41ad7..b9a39655c6 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -647,6 +647,16 @@ typedef struct _PublicationSchemaInfo
        NamespaceInfo *pubschema;
 } PublicationSchemaInfo;
 
+/*
+ * The SubRelInfo struct is used to represent a subscription relation.
+ */
+typedef struct _SubRelInfo
+{
+       Oid                     srrelid;
+       char            srsubstate;
+       char       *srsublsn;
+} SubRelInfo;
+
 /*
  * The SubscriptionInfo struct is used to represent subscription.
  */
@@ -664,6 +674,9 @@ typedef struct _SubscriptionInfo
        char       *subsynccommit;
        char       *subpublications;
        char       *subpasswordrequired;
+       char       *suboriginremotelsn;
+       int                     nrels;
+       SubRelInfo *subrels;
 } SubscriptionInfo;
 
 /*
@@ -690,6 +703,7 @@ extern CollInfo *findCollationByOid(Oid oid);
 extern NamespaceInfo *findNamespaceByOid(Oid oid);
 extern ExtensionInfo *findExtensionByOid(Oid oid);
 extern PublicationInfo *findPublicationByOid(Oid oid);
+extern SubscriptionInfo *findSubscriptionByOid(Oid oid);
 
 extern void recordExtensionMembership(CatalogId catId, ExtensionInfo *ext);
 extern ExtensionInfo *findOwningExtension(CatalogId catalogId);
@@ -749,5 +763,6 @@ extern void getPublicationNamespaces(Archive *fout);
 extern void getPublicationTables(Archive *fout, TableInfo tblinfo[],
                                                                 int numTables);
 extern void getSubscriptions(Archive *fout);
+extern void getSubscriptionTables(Archive *fout);
 
 #endif                                                 /* PG_DUMP_H */
diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c
index fea159689e..e5dc0bd3c2 100644
--- a/src/bin/pg_upgrade/check.c
+++ b/src/bin/pg_upgrade/check.c
@@ -20,6 +20,7 @@ static void check_is_install_user(ClusterInfo *cluster);
 static void check_proper_datallowconn(ClusterInfo *cluster);
 static void check_for_prepared_transactions(ClusterInfo *cluster);
 static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
+static void check_for_subscription_state(ClusterInfo *cluster);
 static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
 static void check_for_incompatible_polymorphics(ClusterInfo *cluster);
 static void check_for_tables_with_oids(ClusterInfo *cluster);
@@ -104,6 +105,13 @@ check_and_dump_old_cluster(bool live_check)
        check_for_reg_data_type_usage(&old_cluster);
        check_for_isn_and_int8_passing_mismatch(&old_cluster);
 
+       /* PG 10 introduced subscriptions. */
+       if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1000 &&
+               user_opts.preserve_subscriptions)
+       {
+               check_for_subscription_state(&old_cluster);
+       }
+
        /*
         * PG 16 increased the size of the 'aclitem' type, which breaks the 
on-disk
         * format for existing data.
@@ -785,6 +793,79 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo 
*cluster)
                check_ok();
 }
 
+/*
+ * check_for_subscription_state()
+ *
+ * Verify that all subscriptions have a valid remote_lsn and don't contain
+ * any table in srsubstate different than ready ('r').
+ */
+static void
+check_for_subscription_state(ClusterInfo *cluster)
+{
+       int                     dbnum;
+       bool            is_error = false;
+
+       Assert(user_opts.preserve_subscriptions);
+
+       prep_status("Checking for subscription state");
+
+       for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
+       {
+               PGresult   *res;
+               DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
+               PGconn     *conn = connectToServer(cluster, active_db->db_name);
+
+               /* We need to check for pg_replication_origin_status only once. 
*/
+               if (dbnum == 0)
+               {
+                       int                     ntup;
+
+                       res = executeQueryOrDie(conn,
+                                                                       "SELECT 
s.subname "
+                                                                       "FROM 
pg_catalog.pg_subscription s "
+                                                                       "LEFT 
JOIN pg_catalog.pg_replication_origin_status os"
+                                                                       "  ON 
os.external_id = 'pg_' || s.oid "
+                                                                       "WHERE 
coalesce(remote_lsn, '0/0') = '0/0'");
+
+                       ntup = PQntuples(res);
+                       for (int i = 0; i < ntup; i++)
+                       {
+                               is_error = true;
+                               pg_log(PG_WARNING,
+                                          "\nWARNING:  subscription \"%s\" has 
an invalid remote_lsn",
+                                          PQgetvalue(res, 0, 0));
+                       }
+                       PQclear(res);
+               }
+
+               res = executeQueryOrDie(conn,
+                                                               "SELECT 
count(0) "
+                                                               "FROM 
pg_catalog.pg_subscription_rel "
+                                                               "WHERE 
srsubstate != 'r'");
+
+               if (PQntuples(res) != 1)
+                       pg_fatal("could not determine the number of non-ready 
subscription relations");
+
+               if (strcmp(PQgetvalue(res, 0, 0), "0") != 0)
+               {
+                       is_error = true;
+                       pg_log(PG_WARNING,
+                                  "\nWARNING: database \"%s\" has %s 
subscription "
+                                  "relations(s) in non-ready state", 
active_db->db_name,
+                                  PQgetvalue(res, 0, 0));
+               }
+
+               PQclear(res);
+               PQfinish(conn);
+       }
+
+       if (is_error)
+               pg_fatal("--preserve-subscription-state is incompatible with "
+                                "subscription relations in non-ready state");
+
+       check_ok();
+}
+
 /*
  * Verify that no user defined postfix operators exist.
  */
diff --git a/src/bin/pg_upgrade/dump.c b/src/bin/pg_upgrade/dump.c
index 6c8c82dca8..9284576af7 100644
--- a/src/bin/pg_upgrade/dump.c
+++ b/src/bin/pg_upgrade/dump.c
@@ -53,9 +53,10 @@ generate_old_dump(void)
 
                parallel_exec_prog(log_file_name, NULL,
                                                   "\"%s/pg_dump\" %s 
--schema-only --quote-all-identifiers "
-                                                  "--binary-upgrade 
--format=custom %s --file=\"%s/%s\" %s",
+                                                  "--binary-upgrade 
--format=custom %s %s --file=\"%s/%s\" %s",
                                                   new_cluster.bindir, 
cluster_conn_opts(&old_cluster),
                                                   log_opts.verbose ? 
"--verbose" : "",
+                                                  
user_opts.preserve_subscriptions ? "--preserve-subscription-state" : "",
                                                   log_opts.dumpdir,
                                                   sql_file_name, 
escaped_connstr.data);
 
diff --git a/src/bin/pg_upgrade/meson.build b/src/bin/pg_upgrade/meson.build
index 12a97f84e2..9ea25dec70 100644
--- a/src/bin/pg_upgrade/meson.build
+++ b/src/bin/pg_upgrade/meson.build
@@ -42,6 +42,7 @@ tests += {
     'tests': [
       't/001_basic.pl',
       't/002_pg_upgrade.pl',
+      't/003_subscription.pl',
     ],
     'test_kwargs': {'priority': 40}, # pg_upgrade tests are slow
   },
diff --git a/src/bin/pg_upgrade/option.c b/src/bin/pg_upgrade/option.c
index 8869b6b60d..afed9ac5ce 100644
--- a/src/bin/pg_upgrade/option.c
+++ b/src/bin/pg_upgrade/option.c
@@ -57,6 +57,7 @@ parseCommandLine(int argc, char *argv[])
                {"verbose", no_argument, NULL, 'v'},
                {"clone", no_argument, NULL, 1},
                {"copy", no_argument, NULL, 2},
+               {"preserve-subscription-state", no_argument, NULL, 3},
 
                {NULL, 0, NULL, 0}
        };
@@ -199,6 +200,10 @@ parseCommandLine(int argc, char *argv[])
                                user_opts.transfer_mode = TRANSFER_MODE_COPY;
                                break;
 
+                       case 3:
+                               user_opts.preserve_subscriptions = true;
+                               break;
+
                        default:
                                fprintf(stderr, _("Try \"%s --help\" for more 
information.\n"),
                                                os_info.progname);
@@ -289,6 +294,7 @@ usage(void)
        printf(_("  -V, --version                 display version information, 
then exit\n"));
        printf(_("  --clone                       clone instead of copying 
files to new cluster\n"));
        printf(_("  --copy                        copy files to new cluster 
(default)\n"));
+       printf(_("  --preserve-subscription-state preserve the subscription 
state fully\n"));
        printf(_("  -?, --help                    show this help, then 
exit\n"));
        printf(_("\n"
                         "Before running pg_upgrade you must:\n"
diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h
index 3eea0139c7..131fd9a56e 100644
--- a/src/bin/pg_upgrade/pg_upgrade.h
+++ b/src/bin/pg_upgrade/pg_upgrade.h
@@ -304,6 +304,7 @@ typedef struct
        transferMode transfer_mode; /* copy files or link them? */
        int                     jobs;                   /* number of 
processes/threads to use */
        char       *socketdir;          /* directory to use for Unix sockets */
+       bool            preserve_subscriptions; /* fully transfer subscription 
state */
 } UserOpts;
 
 typedef struct
diff --git a/src/bin/pg_upgrade/t/003_subscription.pl 
b/src/bin/pg_upgrade/t/003_subscription.pl
new file mode 100644
index 0000000000..053077150c
--- /dev/null
+++ b/src/bin/pg_upgrade/t/003_subscription.pl
@@ -0,0 +1,220 @@
+# Copyright (c) 2022-2023, PostgreSQL Global Development Group
+
+# Test for pg_upgrade of logical subscription
+use strict;
+use warnings;
+
+use Cwd qw(abs_path);
+use File::Basename qw(dirname);
+use File::Compare;
+use File::Find qw(find);
+use File::Path qw(rmtree);
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use PostgreSQL::Test::AdjustUpgrade;
+use Test::More;
+
+# Can be changed to test the other modes.
+my $mode = $ENV{PG_TEST_PG_UPGRADE_MODE} || '--copy';
+
+# Initialize publisher node
+my $publisher = PostgreSQL::Test::Cluster->new('publisher');
+$publisher->init(allows_streaming => 'logical');
+$publisher->start;
+
+# Initialize the old subscriber node
+my $old_sub = PostgreSQL::Test::Cluster->new('old_sub');
+$old_sub->init;
+$old_sub->start;
+
+# Initialize the new subscriber
+my $new_sub = PostgreSQL::Test::Cluster->new('new_sub');
+$new_sub->init;
+my $bindir = $new_sub->config_data('--bindir');
+
+sub insert_line
+{
+       my $payload = shift;
+
+       foreach("t1", "t2")
+       {
+               $publisher->safe_psql('postgres',
+                       "INSERT INTO " . $_ . " (val) VALUES('$payload')");
+       }
+}
+
+# Initial setup
+foreach ("t1", "t2")
+{
+       $publisher->safe_psql('postgres',
+               "CREATE TABLE " . $_ . " (id serial, val text)");
+       $old_sub->safe_psql('postgres',
+               "CREATE TABLE " . $_ . " (id serial, val text)");
+}
+insert_line('before initial sync');
+
+# Setup logical replication, replicating only 1 table
+my $connstr = $publisher->connstr . ' dbname=postgres';
+
+$publisher->safe_psql('postgres',
+       "CREATE PUBLICATION pub FOR TABLE t1");
+
+$old_sub->safe_psql('postgres',
+       "CREATE SUBSCRIPTION sub CONNECTION '$connstr' PUBLICATION pub");
+
+# Wait for the catchup, as we need the subscription rel in ready state
+$old_sub->wait_for_subscription_sync($publisher, 'sub');
+
+# Check that pg_upgrade refuses to run if there's a subscription without a 
valid
+# remote_lsn.
+#
+# Replication origin's remote_lsn isn't set if no data is replicated after the
+# initial sync.
+command_fails(
+       [
+               'pg_upgrade', '--no-sync',        '-d', $old_sub->data_dir,
+               '-D',         $new_sub->data_dir, '-b', $bindir,
+               '-B',         $bindir,            '-s', $new_sub->host,
+               '-p',         $old_sub->port,     '-P', $new_sub->port,
+               $mode,
+               '--preserve-subscription-state',
+               '--check',
+       ],
+       'run of pg_upgrade --check for old instance with invalid remote_lsn');
+ok(-d $new_sub->data_dir . "/pg_upgrade_output.d",
+       "pg_upgrade_output.d/ not removed after pg_upgrade failure");
+rmtree($new_sub->data_dir . "/pg_upgrade_output.d");
+
+# Make sure the replication origin is set
+insert_line('after initial sync');
+$old_sub->wait_for_subscription_sync($publisher, 'sub');
+
+my $result = $old_sub->safe_psql('postgres',
+    "SELECT COUNT(*) FROM pg_subscription_rel WHERE srsubstate != 'r'");
+is ($result, qq(0), "All tables in pg_subscription_rel should be in ready 
state");
+
+# Check the number of rows for each table on each server
+$result = $publisher->safe_psql('postgres',
+       "SELECT count(*) FROM t1");
+is ($result, qq(2), "Table t1 should have 2 rows on the publisher");
+$result = $publisher->safe_psql('postgres',
+       "SELECT count(*) FROM t2");
+is ($result, qq(2), "Table t2 should have 2 rows on the publisher");
+$result = $old_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t1");
+is ($result, qq(2), "Table t1 should have 2 rows on the old subscriber");
+$result = $old_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t2");
+is ($result, qq(0), "Table t2 should have 0 rows on the old subscriber");
+
+# Check that pg_upgrade refuses to run if there's a subscription with tables in
+# a state different than 'r' (ready).
+$old_sub->safe_psql('postgres',
+    "ALTER SUBSCRIPTION sub DISABLE");
+$old_sub->safe_psql('postgres',
+       "UPDATE pg_subscription_rel
+               SET srsubstate = 'i' WHERE srsubstate = 'r'");
+
+command_fails(
+       [
+               'pg_upgrade', '--no-sync',        '-d', $old_sub->data_dir,
+               '-D',         $new_sub->data_dir, '-b', $bindir,
+               '-B',         $bindir,            '-s', $new_sub->host,
+               '-p',         $old_sub->port,     '-P', $new_sub->port,
+               $mode,
+               '--preserve-subscription-state',
+               '--check',
+       ],
+       'run of pg_upgrade --check for old instance with incorrect sub rel');
+ok(-d $new_sub->data_dir . "/pg_upgrade_output.d",
+       "pg_upgrade_output.d/ not removed after pg_upgrade failure");
+rmtree($new_sub->data_dir . "/pg_upgrade_output.d");
+
+# Check that pg_upgrade doesn't detect any problem once all the subscription's
+# relation are in 'r' (ready) state.
+$old_sub->safe_psql('postgres',
+       "UPDATE pg_subscription_rel
+               SET srsubstate = 'r' WHERE srsubstate = 'i'");
+
+command_ok(
+       [
+               'pg_upgrade', '--no-sync',        '-d', $old_sub->data_dir,
+               '-D',         $new_sub->data_dir, '-b', $bindir,
+               '-B',         $bindir,            '-s', $new_sub->host,
+               '-p',         $old_sub->port,     '-P', $new_sub->port,
+               $mode,
+               '--preserve-subscription-state',
+               '--check',
+       ],
+       'run of pg_upgrade --check for old instance with correct sub rel');
+
+# Stop the old subscriber, insert a row in each table while it's down and add
+# t2 to the publication
+my $remote_lsn = $old_sub->safe_psql('postgres',
+       "SELECT remote_lsn FROM pg_replication_origin_status");
+$old_sub->stop;
+
+insert_line('while old_sub is down');
+
+# Run pg_upgrade
+command_ok(
+       [
+               'pg_upgrade', '--no-sync',        '-d', $old_sub->data_dir,
+               '-D',         $new_sub->data_dir, '-b', $bindir,
+               '-B',         $bindir,            '-s', $new_sub->host,
+               '-p',         $old_sub->port,     '-P', $new_sub->port,
+               $mode,
+               '--preserve-subscription-state',
+       ],
+       'run of pg_upgrade for new sub');
+ok( !-d $new_sub->data_dir . "/pg_upgrade_output.d",
+       "pg_upgrade_output.d/ removed after pg_upgrade success");
+$publisher->safe_psql('postgres',
+       "ALTER PUBLICATION pub ADD TABLE t2");
+
+$new_sub->start;
+
+# Subscription relations and replication origin remote_lsn should be preserved
+$result = $new_sub->safe_psql('postgres',
+    "SELECT count(*) FROM pg_subscription_rel");
+is ($result, qq(1), "There should be 1 row in pg_subscription_rel");
+
+$result = $new_sub->safe_psql('postgres',
+    "SELECT remote_lsn FROM pg_replication_origin_status");
+is ($result, qq($remote_lsn), "remote_lsn should have been preserved");
+
+# There should be no new replicated rows before enabling the subscription
+$result = $new_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t1");
+is ($result, qq(2), "Table t1 should still have 2 rows on the new subscriber");
+$result = $new_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t2");
+is ($result, qq(0), "Table t2 should still have 0 rows on the new subscriber");
+
+# Enable the subscription
+$new_sub->safe_psql('postgres',
+       "ALTER SUBSCRIPTION sub ENABLE");
+
+$publisher->wait_for_catchup('sub');
+
+# Rows on t1 should have been replicated, while nothing should happen for t2
+$result = $new_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t1");
+is ($result, qq(3), "Table t1 should now have 3 rows on the new subscriber");
+$result = $new_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t2");
+is ($result, qq(0), "Table t2 should still have 0 rows on the new subscriber");
+
+# Refresh the subscription, only the missing row on t2 should be replicated
+$new_sub->safe_psql('postgres',
+       "ALTER SUBSCRIPTION sub REFRESH PUBLICATION");
+$new_sub->wait_for_subscription_sync($publisher, 'sub');
+$result = $new_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t1");
+is ($result, qq(3), "Table t1 should still have 3 rows on the new subscriber");
+$result = $new_sub->safe_psql('postgres',
+       "SELECT count(*) FROM t2");
+is ($result, qq(3), "Table t2 should now have 3 rows on the new subscriber");
+
+done_testing();
diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h
index cc7b32b279..0ec85ceda2 100644
--- a/src/include/nodes/parsenodes.h
+++ b/src/include/nodes/parsenodes.h
@@ -4028,7 +4028,8 @@ typedef enum AlterSubscriptionType
        ALTER_SUBSCRIPTION_DROP_PUBLICATION,
        ALTER_SUBSCRIPTION_REFRESH,
        ALTER_SUBSCRIPTION_ENABLED,
-       ALTER_SUBSCRIPTION_SKIP
+       ALTER_SUBSCRIPTION_SKIP,
+       ALTER_SUBSCRIPTION_ADD_TABLE
 } AlterSubscriptionType;
 
 typedef struct AlterSubscriptionStmt
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index b4058b88c3..ad13521447 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -2647,6 +2647,7 @@ SubqueryScan
 SubqueryScanPath
 SubqueryScanState
 SubqueryScanStatus
+SubRelInfo
 SubscriptExecSetup
 SubscriptExecSteps
 SubscriptRoutines
-- 
2.37.0

Reply via email to