Skip to content

Commit 364509a

Browse files
pg_upgrade: Retrieve subscription count more efficiently.
Presently, pg_upgrade obtains the number of subscriptions in the to-be-upgraded cluster by first querying pg_subscription in every database for the number of subscriptions in only that database. Then, in count_old_cluster_subscriptions(), it adds all the values collected in the first step. This is expensive, especially when there are many databases. Fortunately, there is a better way to retrieve the subscription count. Since pg_subscription is a shared catalog, we only need to connect to a single database and query it once. This commit modifies pg_upgrade to use that approach, which also allows us to trim several lines of code. In passing, move the call to get_db_subscription_count(), which has been renamed to get_subscription_count(), from get_db_rel_and_slot_infos() to the dedicated >= v17 section in check_and_dump_old_cluster(). We may be able to make similar improvements to get_old_cluster_logical_slot_infos(), but that is left as a future exercise. Reviewed-by: Michael Paquier, Amit Kapila Discussion: https://postgr.es/m/ZprQJv_TxccN3tkr%40nathan Backpatch-through: 17
1 parent 9f21482 commit 364509a

File tree

3 files changed

+15
-53
lines changed

3 files changed

+15
-53
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,10 @@ check_and_dump_old_cluster(bool live_check)
609609

610610
/*
611611
* Subscriptions and their dependencies can be migrated since PG17.
612-
* See comments atop get_db_subscription_count().
612+
* Before that the logical slots are not upgraded, so we will not be
613+
* able to upgrade the logical replication clusters completely.
613614
*/
615+
get_subscription_count(&old_cluster);
614616
check_old_cluster_subscription_state();
615617
}
616618

@@ -1797,17 +1799,14 @@ check_new_cluster_subscription_configuration(void)
17971799
{
17981800
PGresult *res;
17991801
PGconn *conn;
1800-
int nsubs_on_old;
18011802
int max_replication_slots;
18021803

18031804
/* Subscriptions and their dependencies can be migrated since PG17. */
18041805
if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700)
18051806
return;
18061807

1807-
nsubs_on_old = count_old_cluster_subscriptions();
1808-
18091808
/* Quick return if there are no subscriptions to be migrated. */
1810-
if (nsubs_on_old == 0)
1809+
if (old_cluster.nsubs == 0)
18111810
return;
18121811

18131812
prep_status("Checking for new cluster configuration for subscriptions");
@@ -1821,10 +1820,10 @@ check_new_cluster_subscription_configuration(void)
18211820
pg_fatal("could not determine parameter settings on new cluster");
18221821

18231822
max_replication_slots = atoi(PQgetvalue(res, 0, 0));
1824-
if (nsubs_on_old > max_replication_slots)
1823+
if (old_cluster.nsubs > max_replication_slots)
18251824
pg_fatal("\"max_replication_slots\" (%d) must be greater than or equal to the number of "
18261825
"subscriptions (%d) on the old cluster",
1827-
max_replication_slots, nsubs_on_old);
1826+
max_replication_slots, old_cluster.nsubs);
18281827

18291828
PQclear(res);
18301829
PQfinish(conn);

src/bin/pg_upgrade/info.c

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ static void print_db_infos(DbInfoArr *db_arr);
2828
static void print_rel_infos(RelInfoArr *rel_arr);
2929
static void print_slot_infos(LogicalSlotInfoArr *slot_arr);
3030
static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check);
31-
static void get_db_subscription_count(DbInfo *dbinfo);
3231

3332

3433
/*
@@ -293,15 +292,8 @@ get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check)
293292

294293
get_rel_infos(cluster, pDbInfo);
295294

296-
/*
297-
* Retrieve the logical replication slots infos and the subscriptions
298-
* count for the old cluster.
299-
*/
300295
if (cluster == &old_cluster)
301-
{
302296
get_old_cluster_logical_slot_infos(pDbInfo, live_check);
303-
get_db_subscription_count(pDbInfo);
304-
}
305297
}
306298

307299
if (cluster == &old_cluster)
@@ -748,54 +740,25 @@ count_old_cluster_logical_slots(void)
748740
}
749741

750742
/*
751-
* get_db_subscription_count()
752-
*
753-
* Gets the number of subscriptions in the database referred to by "dbinfo".
743+
* get_subscription_count()
754744
*
755-
* Note: This function will not do anything if the old cluster is pre-PG17.
756-
* This is because before that the logical slots are not upgraded, so we will
757-
* not be able to upgrade the logical replication clusters completely.
745+
* Gets the number of subscriptions in the cluster.
758746
*/
759-
static void
760-
get_db_subscription_count(DbInfo *dbinfo)
747+
void
748+
get_subscription_count(ClusterInfo *cluster)
761749
{
762750
PGconn *conn;
763751
PGresult *res;
764752

765-
/* Subscriptions can be migrated since PG17. */
766-
if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700)
767-
return;
768-
769-
conn = connectToServer(&old_cluster, dbinfo->db_name);
753+
conn = connectToServer(cluster, "template1");
770754
res = executeQueryOrDie(conn, "SELECT count(*) "
771-
"FROM pg_catalog.pg_subscription WHERE subdbid = %u",
772-
dbinfo->db_oid);
773-
dbinfo->nsubs = atoi(PQgetvalue(res, 0, 0));
755+
"FROM pg_catalog.pg_subscription");
756+
cluster->nsubs = atoi(PQgetvalue(res, 0, 0));
774757

775758
PQclear(res);
776759
PQfinish(conn);
777760
}
778761

779-
/*
780-
* count_old_cluster_subscriptions()
781-
*
782-
* Returns the number of subscriptions for all databases.
783-
*
784-
* Note: this function always returns 0 if the old_cluster is PG16 and prior
785-
* because we gather subscriptions only for cluster versions greater than or
786-
* equal to PG17. See get_db_subscription_count().
787-
*/
788-
int
789-
count_old_cluster_subscriptions(void)
790-
{
791-
int nsubs = 0;
792-
793-
for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
794-
nsubs += old_cluster.dbarr.dbs[dbnum].nsubs;
795-
796-
return nsubs;
797-
}
798-
799762
static void
800763
free_db_and_rel_infos(DbInfoArr *db_arr)
801764
{

src/bin/pg_upgrade/pg_upgrade.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ typedef struct
197197
* path */
198198
RelInfoArr rel_arr; /* array of all user relinfos */
199199
LogicalSlotInfoArr slot_arr; /* array of all LogicalSlotInfo */
200-
int nsubs; /* number of subscriptions */
201200
} DbInfo;
202201

203202
/*
@@ -296,6 +295,7 @@ typedef struct
296295
char major_version_str[64]; /* string PG_VERSION of cluster */
297296
uint32 bin_version; /* version returned from pg_ctl */
298297
const char *tablespace_suffix; /* directory specification */
298+
int nsubs; /* number of subscriptions */
299299
} ClusterInfo;
300300

301301

@@ -430,7 +430,7 @@ FileNameMap *gen_db_file_maps(DbInfo *old_db,
430430
const char *new_pgdata);
431431
void get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check);
432432
int count_old_cluster_logical_slots(void);
433-
int count_old_cluster_subscriptions(void);
433+
void get_subscription_count(ClusterInfo *cluster);
434434

435435
/* option.c */
436436

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy