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.94.2) (envelope-from ) id 1sWLbo-00EcFr-KD for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 19:55:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sWLbm-00FoKZ-Cn for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 19:55:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sWLbl-00FoGF-UP for pgsql-hackers@lists.postgresql.org; Tue, 23 Jul 2024 19:55:38 +0000 Received: from mail-io1-xd2b.google.com ([2607:f8b0:4864:20::d2b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sWLbf-0015L4-I5 for pgsql-hackers@lists.postgresql.org; Tue, 23 Jul 2024 19:55:37 +0000 Received: by mail-io1-xd2b.google.com with SMTP id ca18e2360f4ac-8076cee8088so209388939f.0 for ; Tue, 23 Jul 2024 12:55:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721764531; x=1722369331; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=10aA8A3ORju3dYc1opT3mCmN4eOLTiC18Tgu6QXlGCQ=; b=RFSZkZXVHsIJdPTTlFf04m9UOPU7QiKV83BZzkPVih4BeH0Edm3/MoOptkrFzYUjV/ lHqvFi/YxQeJMjKL6NMekIEpBCmRGs7jUPjclJO7UCMj1wgpOxARho21baM280H9qa+8 FY3MUa7iVHYgqojA/jvYorCU5RKWi1irDwotJ9W0prp4vzeByqaefOcaKBQvUmtIppF1 03TTpa/G6u6lFCLz9KXqmPRpM/2t6Ostb5Nf7VDvx/iZfsvc4KmBHWOCdwb8nRA5FqPq 5J/N4G9yiHaEQ4m+bKjF4d44IQ5jMKs/LuwTVINyERbzEn/7ydgTcLuWO4cAh1gXgU9w BQjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721764531; x=1722369331; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=10aA8A3ORju3dYc1opT3mCmN4eOLTiC18Tgu6QXlGCQ=; b=aX/mJBDh7jrHh6PpeFWCNIaYxBik1n91d0SJEDQpVWBeZ6jJCwBphYIOwDIwaP8Qr7 JskmEk5Mm0W/fVW9CMbgVSB5FHgMUvSgKeIyLbEkFPiYo8WFmuL3wQ6IZr2mYEk9sAXD KCPq1vwcNk9H879YQzFYmh+euMIhq3De2wQ5YYq6xyHK0xjOL7UfdM2TZ2mQG0tbKsGu 5kWVHoDOB6CuNdxXjAmcPSQT83zKZJ8opbb4J+UxHx/FX5tFdJplTR6oS3Rw0nyiaEKs 1WCbzgXhRaJK7Pvf8cPqyjSkG4SpAFMuKKCDCiepremLIwvpJRP3WSoVSGi7AF1f+sXs OiVA== X-Forwarded-Encrypted: i=1; AJvYcCW3BM3WcJd5E197SidQuax59abXQVwp4S39TH6sum1Y+Y94ZYXDvboXhhKVA1cey+v0Cszpcw1vP7OqNXTBtcEf9B4hfZncBmlQ7791oWuqB9j2 X-Gm-Message-State: AOJu0YzGU9L90O2EVUvw7Eem4MeL5fgB2fV/plextLM8qW0ZzU9S57JX vilnl624IBI8ZLRG0esEEs4dWo/4rEgtrGvTGfFf4+DHxZCWHY61 X-Google-Smtp-Source: AGHT+IHyh6BTAI7JIhNSaqy9ZqJLR3IO8LdUYy4tqpCJYzeLnra3mksHmwsvkcFPTBfUQF+h6/kBpw== X-Received: by 2002:a05:6602:6b0e:b0:803:5e55:ecb2 with SMTP id ca18e2360f4ac-81f715dec26mr17473439f.0.1721764530624; Tue, 23 Jul 2024 12:55:30 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4c28c6b3ba6sm47573173.10.2024.07.23.12.55.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jul 2024 12:55:30 -0700 (PDT) Date: Tue, 23 Jul 2024 14:55:28 -0500 From: Nathan Bossart To: Amit Kapila Cc: Michael Paquier , vignesh C , "Hayato Kuroda (Fujitsu)" , Justin Pryzby , Masahiko Sawada , Peter Smith , Julien Rouhaud , "pgsql-hackers@lists.postgresql.org" Subject: Re: pg_upgrade and logical replication Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="+ZdBOq27BGnFZ0o9" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --+ZdBOq27BGnFZ0o9 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Jul 23, 2024 at 09:05:05AM +0530, Amit Kapila wrote: > Right, the other option would be to move it to the place where we call > check_old_cluster_for_valid_slots(), etc. Initially, it was kept in > the specific function (get_db_rel_and_slot_infos) as we were > mainlining the count at the per-database level but now as we are > changing that I am not sure if calling it from the same place is a > good idea. But OTOH, it is okay to keep it at the place where we > retrieve the required information from the old cluster. I moved it to where you suggested. > One minor point is the comment atop get_subscription_count() still > refers to the function name as get_db_subscription_count(). Oops, fixed. -- nathan --+ZdBOq27BGnFZ0o9 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="v3-0001-pg_upgrade-retrieve-subscription-count-more-effic.patch" From 19831c5a2869f949e73564abea8a36858b39bcd1 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Sat, 20 Jul 2024 21:01:29 -0500 Subject: [PATCH v3 1/1] pg_upgrade: retrieve subscription count more efficiently --- src/bin/pg_upgrade/check.c | 13 ++++----- src/bin/pg_upgrade/info.c | 51 +++++---------------------------- src/bin/pg_upgrade/pg_upgrade.h | 4 +-- 3 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c index 27924159d6..51e30a2f23 100644 --- a/src/bin/pg_upgrade/check.c +++ b/src/bin/pg_upgrade/check.c @@ -609,8 +609,10 @@ check_and_dump_old_cluster(bool live_check) /* * Subscriptions and their dependencies can be migrated since PG17. - * See comments atop get_db_subscription_count(). + * Before that the logical slots are not upgraded, so we will not be + * able to upgrade the logical replication clusters completely. */ + get_subscription_count(&old_cluster); check_old_cluster_subscription_state(); } @@ -1797,17 +1799,14 @@ check_new_cluster_subscription_configuration(void) { PGresult *res; PGconn *conn; - int nsubs_on_old; int max_replication_slots; /* Subscriptions and their dependencies can be migrated since PG17. */ if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700) return; - nsubs_on_old = count_old_cluster_subscriptions(); - /* Quick return if there are no subscriptions to be migrated. */ - if (nsubs_on_old == 0) + if (old_cluster.nsubs == 0) return; prep_status("Checking for new cluster configuration for subscriptions"); @@ -1821,10 +1820,10 @@ check_new_cluster_subscription_configuration(void) pg_fatal("could not determine parameter settings on new cluster"); max_replication_slots = atoi(PQgetvalue(res, 0, 0)); - if (nsubs_on_old > max_replication_slots) + if (old_cluster.nsubs > max_replication_slots) pg_fatal("\"max_replication_slots\" (%d) must be greater than or equal to the number of " "subscriptions (%d) on the old cluster", - max_replication_slots, nsubs_on_old); + max_replication_slots, old_cluster.nsubs); PQclear(res); PQfinish(conn); diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c index 95c22a7200..c07a69b63e 100644 --- a/src/bin/pg_upgrade/info.c +++ b/src/bin/pg_upgrade/info.c @@ -28,7 +28,6 @@ static void print_db_infos(DbInfoArr *db_arr); static void print_rel_infos(RelInfoArr *rel_arr); static void print_slot_infos(LogicalSlotInfoArr *slot_arr); static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check); -static void get_db_subscription_count(DbInfo *dbinfo); /* @@ -293,15 +292,8 @@ get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check) get_rel_infos(cluster, pDbInfo); - /* - * Retrieve the logical replication slots infos and the subscriptions - * count for the old cluster. - */ if (cluster == &old_cluster) - { get_old_cluster_logical_slot_infos(pDbInfo, live_check); - get_db_subscription_count(pDbInfo); - } } if (cluster == &old_cluster) @@ -748,54 +740,25 @@ count_old_cluster_logical_slots(void) } /* - * get_db_subscription_count() - * - * Gets the number of subscriptions in the database referred to by "dbinfo". + * get_subscription_count() * - * Note: This function will not do anything if the old cluster is pre-PG17. - * This is because before that the logical slots are not upgraded, so we will - * not be able to upgrade the logical replication clusters completely. + * Gets the number of subscriptions in the cluster. */ -static void -get_db_subscription_count(DbInfo *dbinfo) +void +get_subscription_count(ClusterInfo *cluster) { PGconn *conn; PGresult *res; - /* Subscriptions can be migrated since PG17. */ - if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700) - return; - - conn = connectToServer(&old_cluster, dbinfo->db_name); + conn = connectToServer(cluster, "template1"); res = executeQueryOrDie(conn, "SELECT count(*) " - "FROM pg_catalog.pg_subscription WHERE subdbid = %u", - dbinfo->db_oid); - dbinfo->nsubs = atoi(PQgetvalue(res, 0, 0)); + "FROM pg_catalog.pg_subscription"); + cluster->nsubs = atoi(PQgetvalue(res, 0, 0)); PQclear(res); PQfinish(conn); } -/* - * count_old_cluster_subscriptions() - * - * Returns the number of subscriptions for all databases. - * - * Note: this function always returns 0 if the old_cluster is PG16 and prior - * because we gather subscriptions only for cluster versions greater than or - * equal to PG17. See get_db_subscription_count(). - */ -int -count_old_cluster_subscriptions(void) -{ - int nsubs = 0; - - for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++) - nsubs += old_cluster.dbarr.dbs[dbnum].nsubs; - - return nsubs; -} - static void free_db_and_rel_infos(DbInfoArr *db_arr) { diff --git a/src/bin/pg_upgrade/pg_upgrade.h b/src/bin/pg_upgrade/pg_upgrade.h index 8afe240bdf..e2b99b49fa 100644 --- a/src/bin/pg_upgrade/pg_upgrade.h +++ b/src/bin/pg_upgrade/pg_upgrade.h @@ -197,7 +197,6 @@ typedef struct * path */ RelInfoArr rel_arr; /* array of all user relinfos */ LogicalSlotInfoArr slot_arr; /* array of all LogicalSlotInfo */ - int nsubs; /* number of subscriptions */ } DbInfo; /* @@ -296,6 +295,7 @@ typedef struct char major_version_str[64]; /* string PG_VERSION of cluster */ uint32 bin_version; /* version returned from pg_ctl */ const char *tablespace_suffix; /* directory specification */ + int nsubs; /* number of subscriptions */ } ClusterInfo; @@ -430,7 +430,7 @@ FileNameMap *gen_db_file_maps(DbInfo *old_db, const char *new_pgdata); void get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check); int count_old_cluster_logical_slots(void); -int count_old_cluster_subscriptions(void); +void get_subscription_count(ClusterInfo *cluster); /* option.c */ -- 2.39.3 (Apple Git-146) --+ZdBOq27BGnFZ0o9--