From d0e8fcf4684adf44bf05ae228590afd5bdc52089 Mon Sep 17 00:00:00 2001 From: jian he Date: Mon, 3 Feb 2025 17:08:35 +0800 Subject: [PATCH v14 1/1] pg_restore dump global objects at least one database needs to be restored call execute_global_sql_commands only when 0 < num_db_restore < MAX_ON_EXIT_NICELY. and other coesmetic changes. --- src/bin/pg_dump/pg_restore.c | 52 +++++++++++++++++------------------- 1 file changed, 25 insertions(+), 27 deletions(-) diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 42c4fe3ce2..8bd8a1f6da 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -71,8 +71,8 @@ typedef struct SimpleDatabaseOidList SimpleDatabaseOidListCell *tail; } SimpleDatabaseOidList; -static void -simple_db_oid_list_append(SimpleDatabaseOidList *list, Oid db_oid, const char *dbname); +static void simple_db_oid_list_append(SimpleDatabaseOidList *list, + Oid db_oid, const char *dbname); static void usage(const char *progname); static void read_restore_filters(const char *filename, RestoreOptions *opts); @@ -495,17 +495,15 @@ main(int argc, char **argv) pg_fatal("could not connect to database \"%s\"", opts->cparams.dbname); } - /* - * Open global.dat file and execute/append all the global sql - * commands. - */ - execute_global_sql_commands(conn, inputFileSpec, opts->filename); - /* If globals-only, then return from here. */ if (globals_only) { - if (conn) - PQfinish(conn); + /* + * Open global.dat file and execute/append all the global sql + * commands. + */ + execute_global_sql_commands(conn, inputFileSpec, opts->filename); + pg_log_info("databases restoring is skipped as -g/--globals-only option is specified"); } else @@ -515,10 +513,12 @@ main(int argc, char **argv) db_exclude_patterns, opts, numWorkers); + /* Free db pattern list. */ + simple_string_full_list_delete(&db_exclude_patterns); } - /* Free db pattern list. */ - simple_string_full_list_delete(&db_exclude_patterns); + if (conn) + PQfinish(conn); return exit_code; } @@ -988,7 +988,7 @@ get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimpleDatabaseOidList *d while((fgets(line, MAXPGPATH, pfile)) != NULL) { Oid db_oid = InvalidOid; - char db_oid_str[MAXPGPATH + 1]; + char db_oid_str[MAXPGPATH + 1] = {'\0'}; char dbname[MAXPGPATH + 1] = {'\0'}; /* Extract dboid. */ @@ -1078,16 +1078,12 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath, num_db_restore = filter_dbnames_for_restore(conn, &dbname_oid_list, db_exclude_patterns); - /* Close the db connection as we are done with globals and patterns. */ - if (conn) - PQfinish(conn); - - /* Exit if no db needs to be restored. */ - if (dbname_oid_list.head == NULL) - return 0; - pg_log_info("needs to restore %d databases out of %d databases", num_db_restore, num_total_db); + /* Exit if no db needs to be restored. */ + if (dbname_oid_list.head == NULL) + return 0; + /* * To restore multiple databases, -C (create database) option should be specified * or all databases should be created before pg_restore. @@ -1099,11 +1095,13 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath, if (num_db_restore > MAX_ON_EXIT_NICELY) { simple_db_oid_full_list_delete(&dbname_oid_list); - pg_fatal("cound not restore more than %d databases by single pg_restore, here total db:%d", + pg_fatal("cound not restore more than %d databases by single pg_restore, here total database:%d", MAX_ON_EXIT_NICELY, num_db_restore); } + execute_global_sql_commands(conn, dumpdirpath, opts->filename); + /* * XXX: TODO till now, we made a list of databases, those needs to be restored * after skipping names of exclude-database. Now we can launch parallel @@ -1153,8 +1151,8 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath, * * This will open global.dat file and will execute all global sql commands one * by one statement. - * Semicolon is considered as statement terminator. If outfile is passed, then - * this will copy all sql commands into outfile rather then executing them. + * Semicolon is considered as statement terminator. If outfile is not NULL, then + * we copy all sql commands into outfile rather then executing them. */ static void execute_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *outfile) @@ -1242,7 +1240,7 @@ copy_global_file_to_out_file(const char *outfile, FILE *pfile) */ static void simple_db_oid_list_append(SimpleDatabaseOidList *list, Oid db_oid, - const char *dbname) + const char *dbname) { SimpleDatabaseOidListCell *cell; @@ -1310,8 +1308,8 @@ simple_string_full_list_delete(SimpleStringList *list) */ static void simple_db_oid_list_delete(SimpleDatabaseOidList *list, - SimpleDatabaseOidListCell *cell, - SimpleDatabaseOidListCell *prev) + SimpleDatabaseOidListCell *cell, + SimpleDatabaseOidListCell *prev) { if (prev == NULL) { -- 2.34.1