diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index 729ffc9e12..1e00fedacd 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -319,7 +319,7 @@ extern Archive *CreateArchive(const char *FileSpec, const ArchiveFormat fmt, DataDirSyncMethod sync_method); /* The --list option */ -extern void PrintTOCSummary(Archive *AHX, bool append_data); +extern void PrintTOCSummary(Archive *AHX); extern RestoreOptions *NewRestoreOptions(void); diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 32d645728a..144d97d0f4 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -1275,7 +1275,7 @@ ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, /* Public */ void -PrintTOCSummary(Archive *AHX, bool append_data) +PrintTOCSummary(Archive *AHX) { ArchiveHandle *AH = (ArchiveHandle *) AHX; RestoreOptions *ropt = AH->public.ropt; @@ -1291,7 +1291,7 @@ PrintTOCSummary(Archive *AHX, bool append_data) sav = SaveOutput(AH); if (ropt->filename) - SetOutput(AH, ropt->filename, out_compression_spec, append_data); + SetOutput(AH, ropt->filename, out_compression_spec, false); if (strftime(stamp_str, sizeof(stamp_str), PGDUMP_STRFTIME_FMT, localtime(&AH->createDate)) == 0) diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 0b6e974380..b75e4f56f3 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -1567,7 +1567,7 @@ dumpDatabases(PGconn *conn, ArchiveFormat archDumpFormat) /* Create a subdirectory with 'databases' name under main directory. */ if (mkdir(db_subdir, 0755) != 0) - pg_log_error("could not create subdirectory \"%s\": %m", db_subdir); + pg_fatal("could not create subdirectory \"%s\": %m", db_subdir); snprintf(map_file_path, MAXPGPATH, "%s/map.dat", filename); diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index af7d815a77..6dd82f08f6 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -121,7 +121,7 @@ main(int argc, char **argv) static int strict_names = 0; bool data_only = false; bool schema_only = false; - bool globals_only = false; + bool globals_only = false; SimpleStringList db_exclude_patterns = {NULL, NULL}; struct option cmdopts[] = { @@ -391,6 +391,13 @@ main(int argc, char **argv) exit_nicely(1); } + if (opts->tocSummary && globals_only) + { + pg_log_error("option -l/--list cannot be used together with -g/--globals-only"); + pg_log_error_hint("Try \"%s --help\" for more information.", progname); + exit_nicely(1); + } + /* Should get at most one of -d and -f, else user is confused */ if (opts->cparams.dbname) { @@ -481,12 +488,6 @@ main(int argc, char **argv) PGconn *conn = NULL; /* Connection to restore global sql commands. */ int exit_code = 0; - /* - * User is suggested to use single database dump for --list option. - */ - if (opts->tocSummary) - pg_fatal("option -l/--list cannot be used when using dump of pg_dumpall"); - /* * Connect to database to execute global sql commands from * global.dat file. @@ -531,6 +532,9 @@ main(int argc, char **argv) } } + if(inputFileSpec != NULL && globals_only) + pg_fatal("could not specify --globals-only when restoring a single database"); + return restoreOneDatabase(inputFileSpec, opts, numWorkers, false, NULL); } /* @@ -571,7 +575,7 @@ restoreOneDatabase(const char *inputFileSpec, RestoreOptions *opts, AH->numWorkers = numWorkers; if (opts->tocSummary) - PrintTOCSummary(AH, append_data); + PrintTOCSummary(AH); else { ProcessArchiveRestoreOptions(AH); @@ -1090,8 +1094,11 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath, * To restore multiple databases, -C (create database) option should be specified * or all databases should be created before pg_restore. */ - if (opts->createDB != 1) - pg_log_info("restoring dump of pg_dumpall without -C option, there might be multiple databases in directory."); + if (opts->createDB != 1 && num_db_restore > 1) + pg_log_info("restoring multiple databases without -C option."); + + if (opts->tocSummary && num_db_restore > 1) + pg_fatal("option -l/--list cannot be used when restoring multiple databases"); /* TODO: MAX_ON_EXIT_NICELY is 100 now... max AH handle register on exit .*/ if (num_db_restore > MAX_ON_EXIT_NICELY) @@ -1103,7 +1110,8 @@ restoreAllDatabases(PGconn *conn, const char *dumpdirpath, } /* Open global.dat file and execute/append all the global sql commands. */ - process_global_sql_commands(conn, dumpdirpath, opts->filename); + if (!opts->tocSummary) + process_global_sql_commands(conn, dumpdirpath, opts->filename); /* Close the db connection as we are done with globals and patterns. */ if (conn) @@ -1223,8 +1231,7 @@ process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *o /* * copy_global_file_to_out_file * - * This will copy global.dat file into out file. If "-" is used as outfile, - * then print commands to the stdout. + * This will copy global.dat file into outfile. */ static void copy_global_file_to_out_file(const char *outfile, FILE *pfile) @@ -1233,19 +1240,13 @@ copy_global_file_to_out_file(const char *outfile, FILE *pfile) FILE *OPF; int c; - /* "-" is used for stdout. */ - if (strcmp(outfile, "-") == 0) - OPF = stdout; - else - { - snprintf(out_file_path, MAXPGPATH, "%s", outfile); - OPF = fopen(out_file_path, PG_BINARY_W); + snprintf(out_file_path, MAXPGPATH, "%s", outfile); + OPF = fopen(out_file_path, PG_BINARY_W); - if (OPF == NULL) - { - fclose(pfile); - pg_fatal("could not open file: \"%s\"", outfile); - } + if (OPF == NULL) + { + fclose(pfile); + pg_fatal("could not open file: \"%s\"", outfile); } /* Now append global.dat into out file. */ @@ -1253,10 +1254,7 @@ copy_global_file_to_out_file(const char *outfile, FILE *pfile) fputc(c, OPF); fclose(pfile); - - /* Close out file. */ - if (strcmp(outfile, "-") != 0) - fclose(OPF); + fclose(OPF); } /* diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl index de41ec06d8..8bb9edc5b5 100755 --- a/src/bin/pg_dump/t/001_basic.pl +++ b/src/bin/pg_dump/t/001_basic.pl @@ -224,6 +224,11 @@ command_fails_like( qr/\Qpg_restore: error: option --exclude-database cannot be used together with -g\/--globals-only\E/, 'pg_restore: option --exclude-database cannot be used together with -g/--globals-only'); +command_fails_like( + [ 'pg_restore', '--list', '--globals-only', '-d', 'xxx' ], + qr/\Qpg_restore: error: option -l\/--list cannot be used together with -g\/--globals-only\E/, + 'pg_restore: option -l/--list cannot be used together with -g/--globals-only'); + # also fails for -r and -t, but it seems pointless to add more tests for those. command_fails_like( [ 'pg_dumpall', '--exclude-database=foo', '--globals-only' ],