Skip to content

Commit 4b56bb4

Browse files
pg_upgrade: Move live_check variable to user_opts.
At the moment, pg_upgrade stores whether it is doing a "live check" (i.e., the user specified --check and the old server is still running) in a local variable scoped to main(). This live_check variable is passed to several functions. To further complicate matters, a few call sites provide a hard-coded "false" as the live_check argument. Specifically, this is done when calling these functions for the new cluster, for which any live-check-only paths won't apply. This commit moves the live_check variable to the global user_opts variable, which stores information about the options the user specified on the command line. This allows us to remove the live_check parameter from several functions. For the functions with callers that provide a hard-coded "false" as the live_check argument (e.g., get_control_data()), we verify the given cluster is the old cluster before taking any live-check-only paths. This small refactoring effort helps simplify some proposed changes that would parallelize many of pg_upgrade's once-in-each-database tasks using libpq's asynchronous APIs. By removing the live_check parameter, we can more easily convert the functions to callbacks for the new parallel system. Reviewed-by: Daniel Gustafsson Discussion: https://postgr.es/m/20240516211638.GA1688936%40nathanxps13
1 parent 5d1d8b3 commit 4b56bb4

File tree

6 files changed

+41
-42
lines changed

6 files changed

+41
-42
lines changed

src/bin/pg_upgrade/check.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void check_for_new_tablespace_dir(void);
2929
static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster);
3030
static void check_new_cluster_logical_replication_slots(void);
3131
static void check_new_cluster_subscription_configuration(void);
32-
static void check_old_cluster_for_valid_slots(bool live_check);
32+
static void check_old_cluster_for_valid_slots(void);
3333
static void check_old_cluster_subscription_state(void);
3434

3535
/*
@@ -555,9 +555,9 @@ fix_path_separator(char *path)
555555
}
556556

557557
void
558-
output_check_banner(bool live_check)
558+
output_check_banner(void)
559559
{
560-
if (user_opts.check && live_check)
560+
if (user_opts.live_check)
561561
{
562562
pg_log(PG_REPORT,
563563
"Performing Consistency Checks on Old Live Server\n"
@@ -573,18 +573,18 @@ output_check_banner(bool live_check)
573573

574574

575575
void
576-
check_and_dump_old_cluster(bool live_check)
576+
check_and_dump_old_cluster(void)
577577
{
578578
/* -- OLD -- */
579579

580-
if (!live_check)
580+
if (!user_opts.live_check)
581581
start_postmaster(&old_cluster, true);
582582

583583
/*
584584
* Extract a list of databases, tables, and logical replication slots from
585585
* the old cluster.
586586
*/
587-
get_db_rel_and_slot_infos(&old_cluster, live_check);
587+
get_db_rel_and_slot_infos(&old_cluster);
588588

589589
init_tablespaces();
590590

@@ -605,7 +605,7 @@ check_and_dump_old_cluster(bool live_check)
605605
* Logical replication slots can be migrated since PG17. See comments
606606
* atop get_old_cluster_logical_slot_infos().
607607
*/
608-
check_old_cluster_for_valid_slots(live_check);
608+
check_old_cluster_for_valid_slots();
609609

610610
/*
611611
* Subscriptions and their dependencies can be migrated since PG17.
@@ -669,15 +669,15 @@ check_and_dump_old_cluster(bool live_check)
669669
if (!user_opts.check)
670670
generate_old_dump();
671671

672-
if (!live_check)
672+
if (!user_opts.live_check)
673673
stop_postmaster(false);
674674
}
675675

676676

677677
void
678678
check_new_cluster(void)
679679
{
680-
get_db_rel_and_slot_infos(&new_cluster, false);
680+
get_db_rel_and_slot_infos(&new_cluster);
681681

682682
check_new_cluster_is_empty();
683683

@@ -828,14 +828,14 @@ check_cluster_versions(void)
828828

829829

830830
void
831-
check_cluster_compatibility(bool live_check)
831+
check_cluster_compatibility(void)
832832
{
833833
/* get/check pg_control data of servers */
834-
get_control_data(&old_cluster, live_check);
835-
get_control_data(&new_cluster, false);
834+
get_control_data(&old_cluster);
835+
get_control_data(&new_cluster);
836836
check_control_data(&old_cluster.controldata, &new_cluster.controldata);
837837

838-
if (live_check && old_cluster.port == new_cluster.port)
838+
if (user_opts.live_check && old_cluster.port == new_cluster.port)
839839
pg_fatal("When checking a live server, "
840840
"the old and new port numbers must be different.");
841841
}
@@ -1838,7 +1838,7 @@ check_new_cluster_subscription_configuration(void)
18381838
* before shutdown.
18391839
*/
18401840
static void
1841-
check_old_cluster_for_valid_slots(bool live_check)
1841+
check_old_cluster_for_valid_slots(void)
18421842
{
18431843
char output_path[MAXPGPATH];
18441844
FILE *script = NULL;
@@ -1877,7 +1877,7 @@ check_old_cluster_for_valid_slots(bool live_check)
18771877
* Note: This can be satisfied only when the old cluster has been
18781878
* shut down, so we skip this for live checks.
18791879
*/
1880-
if (!live_check && !slot->caught_up)
1880+
if (!user_opts.live_check && !slot->caught_up)
18811881
{
18821882
if (script == NULL &&
18831883
(script = fopen_priv(output_path, "w")) == NULL)

src/bin/pg_upgrade/controldata.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* return valid xid data for a running server.
3434
*/
3535
void
36-
get_control_data(ClusterInfo *cluster, bool live_check)
36+
get_control_data(ClusterInfo *cluster)
3737
{
3838
char cmd[MAXPGPATH];
3939
char bufin[MAX_STRING];
@@ -76,6 +76,7 @@ get_control_data(ClusterInfo *cluster, bool live_check)
7676
uint32 segno = 0;
7777
char *resetwal_bin;
7878
int rc;
79+
bool live_check = (cluster == &old_cluster && user_opts.live_check);
7980

8081
/*
8182
* Because we test the pg_resetwal output as strings, it has to be in

src/bin/pg_upgrade/info.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static void free_rel_infos(RelInfoArr *rel_arr);
2727
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);
30-
static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check);
30+
static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo);
3131

3232

3333
/*
@@ -272,11 +272,9 @@ report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
272272
*
273273
* higher level routine to generate dbinfos for the database running
274274
* on the given "port". Assumes that server is already running.
275-
*
276-
* live_check would be used only when the target is the old cluster.
277275
*/
278276
void
279-
get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check)
277+
get_db_rel_and_slot_infos(ClusterInfo *cluster)
280278
{
281279
int dbnum;
282280

@@ -293,7 +291,7 @@ get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check)
293291
get_rel_infos(cluster, pDbInfo);
294292

295293
if (cluster == &old_cluster)
296-
get_old_cluster_logical_slot_infos(pDbInfo, live_check);
294+
get_old_cluster_logical_slot_infos(pDbInfo);
297295
}
298296

299297
if (cluster == &old_cluster)
@@ -637,7 +635,7 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
637635
* are included.
638636
*/
639637
static void
640-
get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check)
638+
get_old_cluster_logical_slot_infos(DbInfo *dbinfo)
641639
{
642640
PGconn *conn;
643641
PGresult *res;
@@ -673,7 +671,7 @@ get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check)
673671
"WHERE slot_type = 'logical' AND "
674672
"database = current_database() AND "
675673
"temporary IS FALSE;",
676-
live_check ? "FALSE" :
674+
user_opts.live_check ? "FALSE" :
677675
"(CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
678676
"ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
679677
"END)");

src/bin/pg_upgrade/option.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ adjust_data_dir(ClusterInfo *cluster)
470470
* directory.
471471
*/
472472
void
473-
get_sock_dir(ClusterInfo *cluster, bool live_check)
473+
get_sock_dir(ClusterInfo *cluster)
474474
{
475475
#if !defined(WIN32)
476-
if (!live_check)
476+
if (!user_opts.live_check || cluster == &new_cluster)
477477
cluster->sockdir = user_opts.socketdir;
478478
else
479479
{

src/bin/pg_upgrade/pg_upgrade.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static void create_new_objects(void);
6565
static void copy_xact_xlog_xid(void);
6666
static void set_frozenxids(bool minmxid_only);
6767
static void make_outputdirs(char *pgdata);
68-
static void setup(char *argv0, bool *live_check);
68+
static void setup(char *argv0);
6969
static void create_logical_replication_slots(void);
7070

7171
ClusterInfo old_cluster,
@@ -88,7 +88,6 @@ int
8888
main(int argc, char **argv)
8989
{
9090
char *deletion_script_file_name = NULL;
91-
bool live_check = false;
9291

9392
/*
9493
* pg_upgrade doesn't currently use common/logging.c, but initialize it
@@ -123,18 +122,18 @@ main(int argc, char **argv)
123122
*/
124123
make_outputdirs(new_cluster.pgdata);
125124

126-
setup(argv[0], &live_check);
125+
setup(argv[0]);
127126

128-
output_check_banner(live_check);
127+
output_check_banner();
129128

130129
check_cluster_versions();
131130

132-
get_sock_dir(&old_cluster, live_check);
133-
get_sock_dir(&new_cluster, false);
131+
get_sock_dir(&old_cluster);
132+
get_sock_dir(&new_cluster);
134133

135-
check_cluster_compatibility(live_check);
134+
check_cluster_compatibility();
136135

137-
check_and_dump_old_cluster(live_check);
136+
check_and_dump_old_cluster();
138137

139138

140139
/* -- NEW -- */
@@ -331,7 +330,7 @@ make_outputdirs(char *pgdata)
331330

332331

333332
static void
334-
setup(char *argv0, bool *live_check)
333+
setup(char *argv0)
335334
{
336335
/*
337336
* make sure the user has a clean environment, otherwise, we may confuse
@@ -378,7 +377,7 @@ setup(char *argv0, bool *live_check)
378377
pg_fatal("There seems to be a postmaster servicing the old cluster.\n"
379378
"Please shutdown that postmaster and try again.");
380379
else
381-
*live_check = true;
380+
user_opts.live_check = true;
382381
}
383382
}
384383

@@ -660,7 +659,7 @@ create_new_objects(void)
660659
set_frozenxids(true);
661660

662661
/* update new_cluster info now that we have objects in the databases */
663-
get_db_rel_and_slot_infos(&new_cluster, false);
662+
get_db_rel_and_slot_infos(&new_cluster);
664663
}
665664

666665
/*

src/bin/pg_upgrade/pg_upgrade.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ typedef struct
321321
typedef struct
322322
{
323323
bool check; /* check clusters only, don't change any data */
324+
bool live_check; /* check clusters only, old server is running */
324325
bool do_sync; /* flush changes to disk */
325326
transferMode transfer_mode; /* copy files or link them? */
326327
int jobs; /* number of processes/threads to use */
@@ -365,20 +366,20 @@ extern OSInfo os_info;
365366

366367
/* check.c */
367368

368-
void output_check_banner(bool live_check);
369-
void check_and_dump_old_cluster(bool live_check);
369+
void output_check_banner(void);
370+
void check_and_dump_old_cluster(void);
370371
void check_new_cluster(void);
371372
void report_clusters_compatible(void);
372373
void issue_warnings_and_set_wal_level(void);
373374
void output_completion_banner(char *deletion_script_file_name);
374375
void check_cluster_versions(void);
375-
void check_cluster_compatibility(bool live_check);
376+
void check_cluster_compatibility(void);
376377
void create_script_for_old_cluster_deletion(char **deletion_script_file_name);
377378

378379

379380
/* controldata.c */
380381

381-
void get_control_data(ClusterInfo *cluster, bool live_check);
382+
void get_control_data(ClusterInfo *cluster);
382383
void check_control_data(ControlData *oldctrl, ControlData *newctrl);
383384
void disable_old_cluster(void);
384385

@@ -427,15 +428,15 @@ void check_loadable_libraries(void);
427428
FileNameMap *gen_db_file_maps(DbInfo *old_db,
428429
DbInfo *new_db, int *nmaps, const char *old_pgdata,
429430
const char *new_pgdata);
430-
void get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check);
431+
void get_db_rel_and_slot_infos(ClusterInfo *cluster);
431432
int count_old_cluster_logical_slots(void);
432433
void get_subscription_count(ClusterInfo *cluster);
433434

434435
/* option.c */
435436

436437
void parseCommandLine(int argc, char *argv[]);
437438
void adjust_data_dir(ClusterInfo *cluster);
438-
void get_sock_dir(ClusterInfo *cluster, bool live_check);
439+
void get_sock_dir(ClusterInfo *cluster);
439440

440441
/* relfilenumber.c */
441442

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