97 static int enable_row_security = 0;
100 static int outputNoTableAm = 0;
101 static int outputNoTablespaces = 0;
117 {
"clean", 0,
NULL,
'c'},
118 {
"create", 0,
NULL,
'C'},
119 {
"data-only", 0,
NULL,
'a'},
120 {
"globals-only", 0,
NULL,
'g'},
121 {
"dbname", 1,
NULL,
'd'},
122 {
"exit-on-error", 0,
NULL,
'e'},
123 {
"exclude-schema", 1,
NULL,
'N'},
124 {
"file", 1,
NULL,
'f'},
125 {
"format", 1,
NULL,
'F'},
126 {
"function", 1,
NULL,
'P'},
127 {
"host", 1,
NULL,
'h'},
128 {
"index", 1,
NULL,
'I'},
129 {
"jobs", 1,
NULL,
'j'},
130 {
"list", 0,
NULL,
'l'},
131 {
"no-privileges", 0,
NULL,
'x'},
132 {
"no-acl", 0,
NULL,
'x'},
133 {
"no-owner", 0,
NULL,
'O'},
134 {
"no-reconnect", 0,
NULL,
'R'},
135 {
"port", 1,
NULL,
'p'},
136 {
"no-password", 0,
NULL,
'w'},
137 {
"password", 0,
NULL,
'W'},
138 {
"schema", 1,
NULL,
'n'},
139 {
"schema-only", 0,
NULL,
's'},
140 {
"superuser", 1,
NULL,
'S'},
141 {
"table", 1,
NULL,
't'},
142 {
"trigger", 1,
NULL,
'T'},
143 {
"use-list", 1,
NULL,
'L'},
144 {
"username", 1,
NULL,
'U'},
145 {
"verbose", 0,
NULL,
'v'},
146 {
"single-transaction", 0,
NULL,
'1'},
152 {
"enable-row-security",
no_argument, &enable_row_security, 1},
155 {
"no-table-access-method",
no_argument, &outputNoTableAm, 1},
156 {
"no-tablespaces",
no_argument, &outputNoTablespaces, 1},
192 if (
strcmp(argv[1],
"--help") == 0 ||
strcmp(argv[1],
"-?") == 0)
197 if (
strcmp(argv[1],
"--version") == 0 ||
strcmp(argv[1],
"-V") == 0)
204 while ((
c =
getopt_long(argc, argv,
"acCd:ef:F:gh:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
213 opts->dropSchema = 1;
222 opts->exit_on_error =
true;
245 opts->tocSummary = 1;
272 opts->selFunction = 1;
282 opts->selTrigger = 1;
320 opts->single_txn =
true;
321 opts->exit_on_error =
true;
348 opts->exit_on_error =
true;
375 pg_log_error(
"too many command-line arguments (first is \"%s\")",
382 if (!
opts->cparams.dbname && !
opts->filename && !
opts->tocSummary)
383 pg_fatal(
"one of -d/--dbname and -f/--file must be specified");
391 opts->filename,
"-f/--file");
395 opts->restrict_key,
"--restrict-key");
397 if (
opts->cparams.dbname)
404 if (!
opts->restrict_key)
406 if (!
opts->restrict_key)
407 pg_fatal(
"could not generate restrict key");
447 opts->single_txn,
"-1/--single-transaction",
448 opts->txn_size,
"--transaction-size");
459 opts->single_txn,
"-1/--single-transaction");
462 if (
opts->single_txn && numWorkers > 1)
463 pg_fatal(
"cannot specify both --single-transaction and multiple jobs");
478 opts->enable_row_security = enable_row_security;
480 opts->noTableAm = outputNoTableAm;
481 opts->noTablespace = outputNoTablespaces;
490 pg_fatal(
"option %s requires option %s",
491 "--if-exists",
"-c/--clean");
495 if (
opts->formatName)
510 pg_fatal(
"archive format \"%s\" is not supported; please use psql",
514 pg_fatal(
"unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
536 if (
opts->tocSummary)
537 pg_fatal(
"option %s cannot be used when restoring an archive created by pg_dumpall",
540 pg_fatal(
"option %s cannot be used when restoring an archive created by pg_dumpall",
544 pg_fatal(
"option %s cannot be used when restoring an archive created by pg_dumpall",
547 pg_fatal(
"options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
548 "--clean",
"-g/--globals-only");
554 if (
opts->dropSchema && !
opts->if_exists)
557 pg_log_info(
"--if-exists is implied by --clean for pg_dumpall archives");
561 pg_fatal(
"option %s cannot be used when restoring an archive created by pg_dumpall",
565 pg_fatal(
"option %s cannot be used when restoring an archive created by pg_dumpall",
569 pg_fatal(
"option %s cannot be used when restoring an archive created by pg_dumpall",
570 "--statistics-only");
573 pg_fatal(
"option %s cannot exclude %s when restoring a pg_dumpall archive",
574 "--section",
"--pre-data");
583 pg_log_error(
"option %s must be specified when restoring an archive created by pg_dumpall",
586 pg_log_error_hint(
"Individual databases can be restored using their specific archives.");
599 pg_log_info(
"skipping restore of global objects because %s was specified",
603 pg_log_info(
"database restoring skipped because option %s was specified",
604 "-g/--globals-only");
620 pg_fatal(
"option %s can be used only when restoring an archive created by pg_dumpall",
621 "--exclude-database");
625 pg_fatal(
"option %s can be used only when restoring an archive created by pg_dumpall",
626 "-g/--globals-only");
729 if (
opts->tocSummary)
748 printf(
_(
"%s restores PostgreSQL databases from archives created by pg_dump or pg_dumpall.\n\n"),
progname);
752 printf(
_(
"\nGeneral options:\n"));
753 printf(
_(
" -d, --dbname=NAME connect to database name\n"));
754 printf(
_(
" -f, --file=FILENAME output file name (- for stdout)\n"));
755 printf(
_(
" -F, --format=c|d|t backup file format (should be automatic)\n"));
756 printf(
_(
" -l, --list print summarized TOC of the archive\n"));
757 printf(
_(
" -v, --verbose verbose mode\n"));
758 printf(
_(
" -V, --version output version information, then exit\n"));
759 printf(
_(
" -?, --help show this help, then exit\n"));
761 printf(
_(
"\nOptions controlling the restore:\n"));
762 printf(
_(
" -a, --data-only restore only the data, no schema\n"));
763 printf(
_(
" -c, --clean clean (drop) database objects before recreating\n"));
764 printf(
_(
" -C, --create create the target database\n"));
765 printf(
_(
" -e, --exit-on-error exit on error, default is to continue\n"));
766 printf(
_(
" -g, --globals-only restore only global objects, no databases\n"));
767 printf(
_(
" -I, --index=NAME restore named index\n"));
768 printf(
_(
" -j, --jobs=NUM use this many parallel jobs to restore\n"));
769 printf(
_(
" -L, --use-list=FILENAME use table of contents from this file for\n"
770 " selecting/ordering output\n"));
771 printf(
_(
" -n, --schema=NAME restore only objects in this schema\n"));
772 printf(
_(
" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
773 printf(
_(
" -O, --no-owner skip restoration of object ownership\n"));
774 printf(
_(
" -P, --function=NAME(args) restore named function\n"));
775 printf(
_(
" -s, --schema-only restore only the schema, no data\n"));
776 printf(
_(
" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
777 printf(
_(
" -t, --table=NAME restore named relation (table, view, etc.)\n"));
778 printf(
_(
" -T, --trigger=NAME restore named trigger\n"));
779 printf(
_(
" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
780 printf(
_(
" -1, --single-transaction restore as a single transaction\n"));
781 printf(
_(
" --disable-triggers disable triggers during data-only restore\n"));
782 printf(
_(
" --enable-row-security enable row security\n"));
783 printf(
_(
" --exclude-database=PATTERN do not restore the specified database(s)\n"));
784 printf(
_(
" --filter=FILENAME restore or skip objects based on expressions\n"
786 printf(
_(
" --if-exists use IF EXISTS when dropping objects\n"));
787 printf(
_(
" --no-comments do not restore comment commands\n"));
788 printf(
_(
" --no-data do not restore data\n"));
789 printf(
_(
" --no-data-for-failed-tables do not restore data of tables that could not be\n"
791 printf(
_(
" --no-policies do not restore row security policies\n"));
792 printf(
_(
" --no-publications do not restore publications\n"));
793 printf(
_(
" --no-schema do not restore schema\n"));
794 printf(
_(
" --no-security-labels do not restore security labels\n"));
795 printf(
_(
" --no-statistics do not restore statistics\n"));
796 printf(
_(
" --no-subscriptions do not restore subscriptions\n"));
797 printf(
_(
" --no-globals do not restore global objects (roles and tablespaces)\n"));
798 printf(
_(
" --no-table-access-method do not restore table access methods\n"));
799 printf(
_(
" --no-tablespaces do not restore tablespace assignments\n"));
800 printf(
_(
" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
801 printf(
_(
" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
802 printf(
_(
" --statistics restore the statistics\n"));
803 printf(
_(
" --statistics-only restore only the statistics, not schema or data\n"));
804 printf(
_(
" --strict-names require table and/or schema include patterns to\n"
805 " match at least one entity each\n"));
806 printf(
_(
" --transaction-size=N commit after every N objects\n"));
807 printf(
_(
" --use-set-session-authorization\n"
808 " use SET SESSION AUTHORIZATION commands instead of\n"
809 " ALTER OWNER commands to set ownership\n"));
811 printf(
_(
"\nConnection options:\n"));
812 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
813 printf(
_(
" -p, --port=PORT database server port number\n"));
814 printf(
_(
" -U, --username=NAME connect as specified database user\n"));
815 printf(
_(
" -w, --no-password never prompt for password\n"));
816 printf(
_(
" -W, --password force password prompt (should happen automatically)\n"));
817 printf(
_(
" --role=ROLENAME do SET ROLE before restore\n"));
820 "The options -I, -n, -N, -P, -t, -T, --section, and --exclude-database can be\n"
821 "combined and specified multiple times to select multiple objects.\n"));
822 printf(
_(
"\nIf no input file name is supplied, then standard input is used.\n\n"));
865 opts->selFunction = 1;
883 opts->selTrigger = 1;
939 pg_fatal(
"directory name too long: \"%s\"", dir);
1001 pg_log_error(
"improper qualified name (too many dotted names): %s",
1065 pg_log_info(
"database restoring is skipped because file \"%s\" does not exist in directory \"%s\"",
"map.dat",
dumpdirpath);
1089 while (
isdigit((
unsigned char) *p))
1107 if (namelen > 0 &&
dbname[namelen - 1] ==
'\n')
1108 dbname[--namelen] =
'\0';
1119 pg_log_info(
"found database \"%s\" (OID: %u) in file \"%s\"",
1162 if (
opts->cparams.dbname)
1168 "found %d database names in \"%s\"",
1177 if (
opts->cparams.dbname)
1184 pg_fatal(
"could not connect to database \"%s\"",
opts->cparams.dbname);
1189 pg_log_info(
"trying to connect to database \"%s\"",
"postgres");
1198 pg_log_info(
"trying to connect to database \"%s\"",
"template1");
1205 pg_log_error(
"could not connect to databases \"postgres\" or \"template1\"\n"
1206 "Please specify an alternative database.");
1225 "no database needs restoring out of %d databases",
num_total_db),
1300 pg_log_info(
"skipping restore of database \"%s\": database does not exist and %s was not specified",
void replace_on_exit_close_archive(Archive *AHX)
void on_exit_close_archive(Archive *AHX)
void init_parallel_dump_utils(void)
#define ngettext(s, p, n)
#define Assert(condition)
#define PG_TEXTDOMAIN(domain)
#define FLEXIBLE_ARRAY_MEMBER
#define OidIsValid(objectId)
void set_pglocale_pgservice(const char *argv0, const char *app)
PGresult * executeQuery(PGconn *conn, const char *query)
PGconn * ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error, const char *progname, const char **connstr, int *server_version, char *password, char *override_dbname)
char * generate_restrict_key(void)
bool valid_restrict_key(const char *restrict_key)
void PQfinish(PGconn *conn)
void * pg_malloc(size_t size)
char * pg_strdup(const char *in)
#define pg_malloc0_object(type)
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
void filter_free(FilterStateData *fstate)
const char * filter_object_type_name(FilterObjectType fot)
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
@ FILTER_OBJECT_TYPE_SCHEMA
@ FILTER_OBJECT_TYPE_INDEX
@ FILTER_OBJECT_TYPE_TRIGGER
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
@ FILTER_OBJECT_TYPE_DATABASE
@ FILTER_OBJECT_TYPE_FUNCTION
@ FILTER_OBJECT_TYPE_TABLE_DATA
@ FILTER_OBJECT_TYPE_NONE
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
@ FILTER_OBJECT_TYPE_EXTENSION
@ FILTER_OBJECT_TYPE_TABLE
@ FILTER_COMMAND_TYPE_NONE
@ FILTER_COMMAND_TYPE_EXCLUDE
@ FILTER_COMMAND_TYPE_INCLUDE
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_increase_verbosity(void)
void pg_logging_init(const char *argv0)
void pg_logging_set_level(enum pg_log_level new_level)
#define pg_log_error(...)
#define pg_log_error_hint(...)
void pfree(void *pointer)
static size_t append_data(char *buf, size_t size, size_t nmemb, void *userdata)
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
#define check_mut_excl_opts(set, opt,...)
static AmcheckOptions opts
void ProcessArchiveRestoreOptions(Archive *AHX)
RestoreOptions * NewRestoreOptions(void)
Archive * OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
void RestoreArchive(Archive *AHX, bool append_data)
void CloseArchive(Archive *AHX)
void SortTocFromFile(Archive *AHX)
void PrintTOCSummary(Archive *AHX)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
void exit_nicely(int code)
void set_dump_section(const char *arg, int *dumpSections)
static int statistics_only
static int disable_triggers
static int no_publications
static int no_security_labels
static int use_setsessauth
static int no_subscriptions
static int with_statistics
bool pg_get_line_buf(FILE *stream, StringInfo buf)
PGDLLIMPORT char * optarg
static bool file_exists_in_directory(const char *dir, const char *filename)
static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts, int numWorkers, bool append_data)
static int get_dbnames_list_to_restore(PGconn *conn, SimplePtrList *dbname_oid_list, SimpleStringList db_exclude_patterns)
static int restore_global_objects(const char *inputFileSpec, RestoreOptions *opts)
static void read_restore_filters(const char *filename, RestoreOptions *opts)
static int get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimplePtrList *dbname_oid_list)
static int restore_all_databases(const char *inputFileSpec, SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers)
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define pg_log_warning(...)
int pg_strcasecmp(const char *s1, const char *s2)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
PQExpBuffer createPQExpBuffer(void)
void resetPQExpBuffer(PQExpBuffer str)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void simple_ptr_list_destroy(SimplePtrList *list)
void simple_string_list_append(SimpleStringList *list, const char *val)
void simple_string_list_destroy(SimpleStringList *list)
void simple_ptr_list_append(SimplePtrList *list, void *ptr)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
void initStringInfo(StringInfo str)
char str[FLEXIBLE_ARRAY_MEMBER]