From bcb3c9ca6e47748f7a5134db9b19e55909677a22 Mon Sep 17 00:00:00 2001 From: jian he Date: Mon, 3 Feb 2025 20:48:27 +0800 Subject: [PATCH v14 1/1] fix pg_restore --list option and handle invoke global objects execution call execute_global_sql_commands only when 0 < num_db_restore < MAX_ON_EXIT_NICELY. and other coesmetic changes. also make pg_restore --list no need database connection, per complain from https://postgr.es/m/CACJufxHUDGWe=2ZukvMfuwEcSK8CsVYm=9+rtPnrW7CRCfoCsw@mail.gmail.com now the logic is: if num_db_restore value is ok (0 < num_db_restore < MAX_ON_EXIT_NICELY) and we didn't specify --list option then call execute_global_sql_commands --- src/bin/pg_dump/pg_restore.c | 54 ++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 42c4fe3ce2..eb20079cb8 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,15 @@ 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); } + /* print out summary, don't need execute the global objects related statement */ + if (!opts->tocSummary) + 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 +1153,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 +1242,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 +1310,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