PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_restore.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <sys/stat.h>
#include "common/string.h"
#include "connectdb.h"
#include "dumputils.h"
#include "fe_utils/option_utils.h"
#include "fe_utils/string_utils.h"
#include "filter.h"
#include "getopt_long.h"
#include "parallel.h"
#include "pg_backup_utils.h"
Include dependency graph for pg_restore.c:

Go to the source code of this file.

Data Structures

struct  DbOidName
 

Typedefs

typedef struct DbOidName DbOidName
 

Functions

static void usage (const char *progname)
 
static void read_restore_filters (const char *filename, RestoreOptions *opts)
 
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 restore_global_objects (const char *inputFileSpec, RestoreOptions *opts)
 
static int restore_all_databases (const char *inputFileSpec, SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers)
 
static int get_dbnames_list_to_restore (PGconn *conn, SimplePtrList *dbname_oid_list, SimpleStringList db_exclude_patterns)
 
static int get_dbname_oid_list_from_mfile (const char *dumpdirpatharg, SimplePtrList *dbname_oid_list)
 
int main (int argc, char **argv)
 

Typedef Documentation

◆ DbOidName

Function Documentation

◆ file_exists_in_directory()

static bool file_exists_in_directory ( const char dir,
const char filename 
)
static

Definition at line 978 of file pg_restore.c.

979{
980 struct stat st;
981 char buf[MAXPGPATH];
982
983 if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH)
984 pg_fatal("directory name too long: \"%s\"", dir);
985
986 return (stat(buf, &st) == 0 && S_ISREG(st.st_mode));
987}
#define pg_fatal(...)
#define MAXPGPATH
static char * filename
Definition pg_dumpall.c:133
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define snprintf
Definition port.h:260
#define stat
Definition win32_port.h:74
#define S_ISREG(m)
Definition win32_port.h:318

References buf, filename, MAXPGPATH, pg_fatal, S_ISREG, snprintf, stat::st_mode, and stat.

Referenced by get_dbname_oid_list_from_mfile(), main(), and restore_all_databases().

◆ get_dbname_oid_list_from_mfile()

static int get_dbname_oid_list_from_mfile ( const char dumpdirpatharg,
SimplePtrList dbname_oid_list 
)
static

Definition at line 1096 of file pg_restore.c.

1097{
1099 FILE *pfile;
1101 int count = 0;
1102 int len;
1104
1105 /*
1106 * If there is no map.dat file in the dump, then return from here as there
1107 * is no database to restore.
1108 */
1109 if (!file_exists_in_directory(dumpdirpath, "map.dat"))
1110 {
1111 pg_log_info("database restoring is skipped because file \"%s\" does not exist in directory \"%s\"", "map.dat", dumpdirpath);
1112 return 0;
1113 }
1114
1116
1117 /* Trim slash from directory name. */
1118 while (len > 1 && dumpdirpath[len - 1] == '/')
1119 {
1120 dumpdirpath[len - 1] = '\0';
1121 len--;
1122 }
1123
1125
1126 /* Open map.dat file. */
1128
1129 if (pfile == NULL)
1130 pg_fatal("could not open file \"%s\": %m", map_file_path);
1131
1133
1134 /* Append all the dbname/db_oid combinations to the list. */
1135 while (pg_get_line_buf(pfile, &linebuf))
1136 {
1137 Oid db_oid = InvalidOid;
1138 char *dbname;
1140 int namelen;
1141 char *p = linebuf.data;
1142
1143 /* look for the dboid. */
1144 while (isdigit((unsigned char) *p))
1145 p++;
1146
1147 /* ignore lines that don't begin with a digit */
1148 if (p == linebuf.data)
1149 continue;
1150
1151 if (*p == ' ')
1152 {
1153 sscanf(linebuf.data, "%u", &db_oid);
1154 p++;
1155 }
1156
1157 /* dbname is the rest of the line */
1158 dbname = p;
1159 namelen = strlen(dbname);
1160
1161 /* Strip trailing newline */
1162 if (namelen > 0 && dbname[namelen - 1] == '\n')
1163 dbname[--namelen] = '\0';
1164
1165 /* Report error and exit if the file has any corrupted data. */
1166 if (!OidIsValid(db_oid) || namelen < 1)
1167 pg_fatal("invalid entry in file \"%s\" on line %d", map_file_path,
1168 count + 1);
1169
1170 dbidname = pg_malloc(offsetof(DbOidName, str) + namelen + 1);
1171 dbidname->oid = db_oid;
1172 strlcpy(dbidname->str, dbname, namelen + 1);
1173
1174 pg_log_info("found database \"%s\" (OID: %u) in file \"%s\"",
1175 dbidname->str, db_oid, map_file_path);
1176
1178 count++;
1179 }
1180
1181 /* Close map.dat file. */
1182 fclose(pfile);
1183
1184 pfree(linebuf.data);
1185
1186 return count;
1187}
#define PG_BINARY_R
Definition c.h:1339
#define OidIsValid(objectId)
Definition c.h:821
void * pg_malloc(size_t size)
Definition fe_memutils.c:47
const char * str
#define pg_log_info(...)
Definition logging.h:124
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
const void size_t len
bool pg_get_line_buf(FILE *stream, StringInfo buf)
Definition pg_get_line.c:95
static bool file_exists_in_directory(const char *dir, const char *filename)
Definition pg_restore.c:978
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
#define InvalidOid
unsigned int Oid
static int fb(int x)
void simple_ptr_list_append(SimplePtrList *list, void *ptr)
char * dbname
Definition streamutil.c:49
void initStringInfo(StringInfo str)
Definition stringinfo.c:97

References dbname, fb(), file_exists_in_directory(), initStringInfo(), InvalidOid, len, MAXPGPATH, OidIsValid, pfree(), PG_BINARY_R, pg_fatal, pg_get_line_buf(), pg_log_info, pg_malloc(), pstrdup(), simple_ptr_list_append(), snprintf, str, and strlcpy().

Referenced by restore_all_databases().

◆ get_dbnames_list_to_restore()

static int get_dbnames_list_to_restore ( PGconn conn,
SimplePtrList dbname_oid_list,
SimpleStringList  db_exclude_patterns 
)
static

Definition at line 999 of file pg_restore.c.

1002{
1003 int count_db = 0;
1004 PQExpBuffer query;
1006 PGresult *res;
1007
1008 query = createPQExpBuffer();
1010
1011 /*
1012 * Process one by one all dbnames and if specified to skip restoring, then
1013 * remove dbname from list.
1014 */
1016 db_cell; db_cell = db_cell->next)
1017 {
1018 DbOidName *dbidname = (DbOidName *) db_cell->ptr;
1019 bool skip_db_restore = false;
1020
1021 resetPQExpBuffer(query);
1023
1025
1027 {
1028 /*
1029 * If there is an exact match then we don't need to try a pattern
1030 * match
1031 */
1032 if (pg_strcasecmp(dbidname->str, pat_cell->val) == 0)
1033 skip_db_restore = true;
1034 /* Otherwise, try a pattern match if there is a connection */
1035 else
1036 {
1037 int dotcnt;
1038
1039 appendPQExpBufferStr(query, "SELECT 1 ");
1040 processSQLNamePattern(conn, query, pat_cell->val, false,
1041 false, NULL, db_lit->data,
1042 NULL, NULL, NULL, &dotcnt);
1043
1044 if (dotcnt > 0)
1045 {
1046 pg_log_error("improper qualified name (too many dotted names): %s",
1047 dbidname->str);
1048 PQfinish(conn);
1049 exit_nicely(1);
1050 }
1051
1052 res = executeQuery(conn, query->data);
1053
1054 if (PQntuples(res))
1055 {
1056 skip_db_restore = true;
1057 pg_log_info("database name \"%s\" matches --exclude-database pattern \"%s\"", dbidname->str, pat_cell->val);
1058 }
1059
1060 PQclear(res);
1061 resetPQExpBuffer(query);
1062 }
1063
1064 if (skip_db_restore)
1065 break;
1066 }
1067
1068 /*
1069 * Mark db to be skipped or increment the counter of dbs to be
1070 * restored
1071 */
1072 if (skip_db_restore)
1073 {
1074 pg_log_info("excluding database \"%s\"", dbidname->str);
1075 dbidname->oid = InvalidOid;
1076 }
1077 else
1078 count_db++;
1079 }
1080
1081 destroyPQExpBuffer(query);
1083
1084 return count_db;
1085}
PGresult * executeQuery(PGconn *conn, const char *query)
Definition connectdb.c:278
void PQfinish(PGconn *conn)
#define PQclear
#define PQntuples
#define pg_log_error(...)
Definition logging.h:106
void exit_nicely(int code)
int pg_strcasecmp(const char *s1, const char *s2)
PQExpBuffer createPQExpBuffer(void)
Definition pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
PGconn * conn
Definition streamutil.c:52
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)

References appendPQExpBufferStr(), appendStringLiteralConn(), conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), exit_nicely(), fb(), InvalidOid, pg_log_error, pg_log_info, pg_strcasecmp(), PQclear, PQfinish(), PQntuples, processSQLNamePattern(), and resetPQExpBuffer().

Referenced by restore_all_databases().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 85 of file pg_restore.c.

86{
88 int c;
89 int numWorkers = 1;
90 char *inputFileSpec;
91 bool data_only = false;
92 bool schema_only = false;
93 int n_errors = 0;
94 bool globals_only = false;
96 static int disable_triggers = 0;
97 static int enable_row_security = 0;
98 static int if_exists = 0;
99 static int no_data_for_failed_tables = 0;
100 static int outputNoTableAm = 0;
101 static int outputNoTablespaces = 0;
102 static int use_setsessauth = 0;
103 static int no_comments = 0;
104 static int no_data = 0;
105 static int no_policies = 0;
106 static int no_publications = 0;
107 static int no_schema = 0;
108 static int no_security_labels = 0;
109 static int no_statistics = 0;
110 static int no_globals = 0;
111 static int no_subscriptions = 0;
112 static int strict_names = 0;
113 static int statistics_only = 0;
114 static int with_statistics = 0;
115
116 struct option cmdopts[] = {
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'},
147
148 /*
149 * the following options don't have an equivalent short option letter
150 */
151 {"disable-triggers", no_argument, &disable_triggers, 1},
152 {"enable-row-security", no_argument, &enable_row_security, 1},
153 {"if-exists", no_argument, &if_exists, 1},
154 {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
155 {"no-table-access-method", no_argument, &outputNoTableAm, 1},
156 {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
157 {"role", required_argument, NULL, 2},
158 {"section", required_argument, NULL, 3},
159 {"strict-names", no_argument, &strict_names, 1},
160 {"transaction-size", required_argument, NULL, 5},
161 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
162 {"no-comments", no_argument, &no_comments, 1},
163 {"no-data", no_argument, &no_data, 1},
164 {"no-policies", no_argument, &no_policies, 1},
165 {"no-publications", no_argument, &no_publications, 1},
166 {"no-schema", no_argument, &no_schema, 1},
167 {"no-security-labels", no_argument, &no_security_labels, 1},
168 {"no-globals", no_argument, &no_globals, 1},
169 {"no-subscriptions", no_argument, &no_subscriptions, 1},
170 {"no-statistics", no_argument, &no_statistics, 1},
171 {"statistics", no_argument, &with_statistics, 1},
172 {"statistics-only", no_argument, &statistics_only, 1},
173 {"filter", required_argument, NULL, 4},
174 {"restrict-key", required_argument, NULL, 6},
175 {"exclude-database", required_argument, NULL, 7},
176
177 {NULL, 0, NULL, 0}
178 };
179
180 pg_logging_init(argv[0]);
182 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
183
185
187
188 progname = get_progname(argv[0]);
189
190 if (argc > 1)
191 {
192 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
193 {
195 exit_nicely(0);
196 }
197 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
198 {
199 puts("pg_restore (PostgreSQL) " PG_VERSION);
200 exit_nicely(0);
201 }
202 }
203
204 while ((c = getopt_long(argc, argv, "acCd:ef:F:gh:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
205 cmdopts, NULL)) != -1)
206 {
207 switch (c)
208 {
209 case 'a': /* Dump data only */
210 data_only = true;
211 break;
212 case 'c': /* clean (i.e., drop) schema prior to create */
213 opts->dropSchema = 1;
214 break;
215 case 'C':
216 opts->createDB = 1;
217 break;
218 case 'd':
219 opts->cparams.dbname = pg_strdup(optarg);
220 break;
221 case 'e':
222 opts->exit_on_error = true;
223 break;
224 case 'f': /* output file name */
225 opts->filename = pg_strdup(optarg);
226 break;
227 case 'F':
228 if (strlen(optarg) != 0)
229 opts->formatName = pg_strdup(optarg);
230 break;
231 case 'g':
232 /* restore only global sql commands. */
233 globals_only = true;
234 break;
235 case 'h':
236 if (strlen(optarg) != 0)
237 opts->cparams.pghost = pg_strdup(optarg);
238 break;
239 case 'j': /* number of restore jobs */
240 if (!option_parse_int(optarg, "-j/--jobs", 1,
242 &numWorkers))
243 exit(1);
244 break;
245
246 case 'l': /* Dump the TOC summary */
247 opts->tocSummary = 1;
248 break;
249
250 case 'L': /* input TOC summary file name */
251 opts->tocFile = pg_strdup(optarg);
252 break;
253
254 case 'n': /* Dump data for this schema only */
255 simple_string_list_append(&opts->schemaNames, optarg);
256 break;
257
258 case 'N': /* Do not dump data for this schema */
259 simple_string_list_append(&opts->schemaExcludeNames, optarg);
260 break;
261
262 case 'O':
263 opts->noOwner = 1;
264 break;
265
266 case 'p':
267 if (strlen(optarg) != 0)
268 opts->cparams.pgport = pg_strdup(optarg);
269 break;
270 case 'R':
271 /* no-op, still accepted for backwards compatibility */
272 break;
273 case 'P': /* Function */
274 opts->selTypes = 1;
275 opts->selFunction = 1;
276 simple_string_list_append(&opts->functionNames, optarg);
277 break;
278 case 'I': /* Index */
279 opts->selTypes = 1;
280 opts->selIndex = 1;
282 break;
283 case 'T': /* Trigger */
284 opts->selTypes = 1;
285 opts->selTrigger = 1;
286 simple_string_list_append(&opts->triggerNames, optarg);
287 break;
288 case 's': /* dump schema only */
289 schema_only = true;
290 break;
291 case 'S': /* Superuser username */
292 if (strlen(optarg) != 0)
293 opts->superuser = pg_strdup(optarg);
294 break;
295 case 't': /* Dump specified table(s) only */
296 opts->selTypes = 1;
297 opts->selTable = 1;
299 break;
300
301 case 'U':
302 opts->cparams.username = pg_strdup(optarg);
303 break;
304
305 case 'v': /* verbose */
306 opts->verbose = 1;
308 break;
309
310 case 'w':
311 opts->cparams.promptPassword = TRI_NO;
312 break;
313
314 case 'W':
315 opts->cparams.promptPassword = TRI_YES;
316 break;
317
318 case 'x': /* skip ACL dump */
319 opts->aclsSkip = 1;
320 break;
321
322 case '1': /* Restore data in a single transaction */
323 opts->single_txn = true;
324 opts->exit_on_error = true;
325 break;
326
327 case 0:
328
329 /*
330 * This covers the long options without a short equivalent.
331 */
332 break;
333
334 case 2: /* SET ROLE */
335 opts->use_role = pg_strdup(optarg);
336 break;
337
338 case 3: /* section */
339 set_dump_section(optarg, &(opts->dumpSections));
340 break;
341
342 case 4: /* filter */
344 break;
345
346 case 5: /* transaction-size */
347 if (!option_parse_int(optarg, "--transaction-size",
348 1, INT_MAX,
349 &opts->txn_size))
350 exit(1);
351 opts->exit_on_error = true;
352 break;
353
354 case 6:
355 opts->restrict_key = pg_strdup(optarg);
356 break;
357
358 case 7: /* database patterns to skip */
360 break;
361
362 default:
363 /* getopt_long already emitted a complaint */
364 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
365 exit_nicely(1);
366 }
367 }
368
369 /* Get file name from command line */
370 if (optind < argc)
371 inputFileSpec = argv[optind++];
372 else
374
375 /* Complain if any arguments remain */
376 if (optind < argc)
377 {
378 pg_log_error("too many command-line arguments (first is \"%s\")",
379 argv[optind]);
380 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
381 exit_nicely(1);
382 }
383
384 /* Complain if neither -f nor -d was specified (except if dumping TOC) */
385 if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
386 pg_fatal("one of -d/--dbname and -f/--file must be specified");
387
389 {
390 pg_log_error("option %s cannot be used together with %s",
391 "--exclude-database", "-g/--globals-only");
392 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
393 exit_nicely(1);
394 }
395
396 /* Should get at most one of -d and -f, else user is confused */
397 if (opts->cparams.dbname)
398 {
399 if (opts->filename)
400 {
401 pg_log_error("options %s and %s cannot be used together",
402 "-d/--dbname", "-f/--file");
403 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
404 exit_nicely(1);
405 }
406
407 if (opts->restrict_key)
408 pg_fatal("options %s and %s cannot be used together",
409 "-d/--dbname", "--restrict-key");
410
411 opts->useDB = 1;
412 }
413 else
414 {
415 /*
416 * If you don't provide a restrict key, one will be appointed for you.
417 */
418 if (!opts->restrict_key)
419 opts->restrict_key = generate_restrict_key();
420 if (!opts->restrict_key)
421 pg_fatal("could not generate restrict key");
422 if (!valid_restrict_key(opts->restrict_key))
423 pg_fatal("invalid restrict key");
424 }
425
426 /* reject conflicting "-only" options */
427 if (data_only && schema_only)
428 pg_fatal("options %s and %s cannot be used together",
429 "-s/--schema-only", "-a/--data-only");
431 pg_fatal("options %s and %s cannot be used together",
432 "-s/--schema-only", "--statistics-only");
434 pg_fatal("options %s and %s cannot be used together",
435 "-a/--data-only", "--statistics-only");
436
437 /* reject conflicting "-only" and "no-" options */
438 if (data_only && no_data)
439 pg_fatal("options %s and %s cannot be used together",
440 "-a/--data-only", "--no-data");
441 if (schema_only && no_schema)
442 pg_fatal("options %s and %s cannot be used together",
443 "-s/--schema-only", "--no-schema");
445 pg_fatal("options %s and %s cannot be used together",
446 "--statistics-only", "--no-statistics");
447
448 /* reject conflicting "no-" options */
450 pg_fatal("options %s and %s cannot be used together",
451 "--statistics", "--no-statistics");
452
453 /* reject conflicting "only-" options */
455 pg_fatal("options %s and %s cannot be used together",
456 "-a/--data-only", "--statistics");
458 pg_fatal("options %s and %s cannot be used together",
459 "-s/--schema-only", "--statistics");
460
461 if (data_only && opts->dropSchema)
462 pg_fatal("options %s and %s cannot be used together",
463 "-c/--clean", "-a/--data-only");
464
465 if (opts->single_txn && opts->txn_size > 0)
466 pg_fatal("options %s and %s cannot be used together",
467 "-1/--single-transaction", "--transaction-size");
468
469 if (opts->single_txn && globals_only)
470 pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
471 "--single-transaction", "-g/--globals-only");
472
473 if (opts->txn_size && globals_only)
474 pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
475 "--transaction-size", "-g/--globals-only");
476
477 if (opts->exit_on_error && globals_only)
478 pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
479 "--exit-on-error", "-g/--globals-only");
480
481 if (data_only && globals_only)
482 pg_fatal("options %s and %s cannot be used together",
483 "-a/--data-only", "-g/--globals-only");
485 pg_fatal("options %s and %s cannot be used together",
486 "-s/--schema-only", "-g/--globals-only");
488 pg_fatal("options %s and %s cannot be used together",
489 "--statistics-only", "-g/--globals-only");
491 pg_fatal("options %s and %s cannot be used together",
492 "--statistics", "-g/--globals-only");
493
495 pg_fatal("options %s and %s cannot be used together",
496 "--no-globals", "-g/--globals-only");
497
498 /*
499 * -C is not compatible with -1, because we can't create a database inside
500 * a transaction block.
501 */
502 if (opts->createDB && opts->single_txn)
503 pg_fatal("options %s and %s cannot be used together",
504 "-C/--create", "-1/--single-transaction");
505
506 /* Can't do single-txn mode with multiple connections */
507 if (opts->single_txn && numWorkers > 1)
508 pg_fatal("cannot specify both --single-transaction and multiple jobs");
509
510 /*
511 * Set derivative flags. Ambiguous or nonsensical combinations, e.g.
512 * "--schema-only --no-schema", will have already caused an error in one
513 * of the checks above.
514 */
515 opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
516 data_only) && !no_data;
517 opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
519 opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
521
522 opts->disable_triggers = disable_triggers;
523 opts->enable_row_security = enable_row_security;
524 opts->noDataForFailedTables = no_data_for_failed_tables;
525 opts->noTableAm = outputNoTableAm;
526 opts->noTablespace = outputNoTablespaces;
527 opts->use_setsessauth = use_setsessauth;
528 opts->no_comments = no_comments;
529 opts->no_policies = no_policies;
530 opts->no_publications = no_publications;
531 opts->no_security_labels = no_security_labels;
532 opts->no_subscriptions = no_subscriptions;
533
534 if (if_exists && !opts->dropSchema)
535 pg_fatal("option %s requires option %s",
536 "--if-exists", "-c/--clean");
537 opts->if_exists = if_exists;
539
540 if (opts->formatName)
541 {
542 if (pg_strcasecmp(opts->formatName, "c") == 0 ||
543 pg_strcasecmp(opts->formatName, "custom") == 0)
544 opts->format = archCustom;
545 else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
546 pg_strcasecmp(opts->formatName, "directory") == 0)
547 opts->format = archDirectory;
548 else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
549 pg_strcasecmp(opts->formatName, "tar") == 0)
550 opts->format = archTar;
551 else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
552 pg_strcasecmp(opts->formatName, "plain") == 0)
553 {
554 /* recognize this for consistency with pg_dump */
555 pg_fatal("archive format \"%s\" is not supported; please use psql",
556 opts->formatName);
557 }
558 else
559 pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
560 opts->formatName);
561 }
562
563 /*
564 * If toc.glo file is present, then restore all the databases from
565 * map.dat, but skip restoring those matching --exclude-database patterns.
566 */
567 if (inputFileSpec != NULL &&
569 {
572
573 opts->format = archUnknown;
574
576
577 /*
578 * Can only use --list or --use-list options with a single database
579 * dump.
580 */
581 if (opts->tocSummary)
582 pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
583 "-l/--list");
584 if (opts->tocFile)
585 pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
586 "-L/--use-list");
587
588 if (opts->strict_names)
589 pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
590 "--strict-names");
591 if (globals_only && opts->dropSchema)
592 pg_fatal("options %s and %s cannot be used together when restoring an archive created by pg_dumpall",
593 "--clean", "-g/--globals-only");
594
595 /*
596 * For pg_dumpall archives, --clean implies --if-exists since global
597 * objects may not exist in the target cluster.
598 */
599 if (opts->dropSchema && !opts->if_exists)
600 {
601 opts->if_exists = 1;
602 pg_log_info("--if-exists is implied by --clean for pg_dumpall archives");
603 }
604
605 if (no_schema)
606 pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
607 "--no-schema");
608
609 if (data_only)
610 pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
611 "-a/--data-only");
612
613 if (statistics_only)
614 pg_fatal("option %s cannot be used when restoring an archive created by pg_dumpall",
615 "--statistics-only");
616
617 if (!(opts->dumpSections & DUMP_PRE_DATA))
618 pg_fatal("option %s cannot exclude %s when restoring a pg_dumpall archive",
619 "--section", "--pre-data");
620
621 /*
622 * To restore from a pg_dumpall archive, -C (create database) option
623 * must be specified unless we are only restoring globals or we are
624 * skipping globals.
625 */
626 if (!no_globals && !globals_only && opts->createDB != 1)
627 {
628 pg_log_error("option %s must be specified when restoring an archive created by pg_dumpall",
629 "-C/--create");
630 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
631 pg_log_error_hint("Individual databases can be restored using their specific archives.");
632 exit_nicely(1);
633 }
634
635 /*
636 * Restore global objects, even if --exclude-database results in zero
637 * databases to process. If 'globals-only' is set, exit immediately.
638 */
640
641 if (!no_globals)
643 else
644 pg_log_info("skipping restore of global objects because %s was specified",
645 "--no-globals");
646
647 if (globals_only)
648 pg_log_info("database restoring skipped because option %s was specified",
649 "-g/--globals-only");
650 else
651 {
652 /* Now restore all the databases from map.dat */
654 opts, numWorkers);
655 }
656
657 /* Free db pattern list. */
659 }
660 else
661 {
662 if (db_exclude_patterns.head != NULL)
663 {
665 pg_fatal("option %s can be used only when restoring an archive created by pg_dumpall",
666 "--exclude-database");
667 }
668
669 if (globals_only)
670 pg_fatal("option %s can be used only when restoring an archive created by pg_dumpall",
671 "-g/--globals-only");
672
673 /* Process if toc.glo file does not exist. */
674 n_errors = restore_one_database(inputFileSpec, opts, numWorkers, false);
675 }
676
677 /* Done, print a summary of ignored errors during restore. */
678 if (n_errors)
679 {
680 pg_log_warning("errors ignored on restore: %d", n_errors);
681 return 1;
682 }
683
684 return 0;
685}
void init_parallel_dump_utils(void)
Definition parallel.c:238
#define PG_MAX_JOBS
Definition parallel.h:48
#define PG_TEXTDOMAIN(domain)
Definition c.h:1266
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition exec.c:430
char * generate_restrict_key(void)
Definition dumputils.c:973
bool valid_restrict_key(const char *restrict_key)
Definition dumputils.c:997
char * pg_strdup(const char *in)
Definition fe_memutils.c:85
#define pg_malloc0_object(type)
Definition fe_memutils.h:51
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition getopt_long.c:60
#define no_argument
Definition getopt_long.h:25
#define required_argument
Definition getopt_long.h:26
void pg_logging_increase_verbosity(void)
Definition logging.c:185
void pg_logging_init(const char *argv0)
Definition logging.c:83
void pg_logging_set_level(enum pg_log_level new_level)
Definition logging.c:176
#define pg_log_error_hint(...)
Definition logging.h:112
@ PG_LOG_WARNING
Definition logging.h:38
const char * progname
Definition main.c:44
bool option_parse_int(const char *optarg, const char *optname, int min_range, int max_range, int *result)
static AmcheckOptions opts
Definition pg_amcheck.c:112
static void usage(void)
RestoreOptions * NewRestoreOptions(void)
@ archUnknown
Definition pg_backup.h:41
@ archTar
Definition pg_backup.h:43
@ archCustom
Definition pg_backup.h:42
@ archDirectory
Definition pg_backup.h:45
void set_dump_section(const char *arg, int *dumpSections)
#define DUMP_PRE_DATA
static int strict_names
Definition pg_dump.c:157
static int if_exists
Definition pg_dumpall.c:105
static int statistics_only
Definition pg_dumpall.c:125
static int no_policies
Definition pg_dumpall.c:111
static int disable_triggers
Definition pg_dumpall.c:104
static int no_comments
Definition pg_dumpall.c:110
static int no_publications
Definition pg_dumpall.c:112
static int no_security_labels
Definition pg_dumpall.c:113
static int no_statistics
Definition pg_dumpall.c:116
static int use_setsessauth
Definition pg_dumpall.c:109
static int no_data
Definition pg_dumpall.c:114
static int no_schema
Definition pg_dumpall.c:115
static int no_subscriptions
Definition pg_dumpall.c:117
static int with_statistics
Definition pg_dumpall.c:121
PGDLLIMPORT int optind
Definition getopt.c:51
PGDLLIMPORT char * optarg
Definition getopt.c:53
static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts, int numWorkers, bool append_data)
Definition pg_restore.c:738
static int restore_global_objects(const char *inputFileSpec, RestoreOptions *opts)
Definition pg_restore.c:693
static void read_restore_filters(const char *filename, RestoreOptions *opts)
Definition pg_restore.c:880
static int restore_all_databases(const char *inputFileSpec, SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers)
#define pg_log_warning(...)
Definition pgfnames.c:24
const char * get_progname(const char *argv0)
Definition path.c:652
char * c
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition simple_list.c:63
void simple_string_list_destroy(SimpleStringList *list)
@ TRI_YES
Definition vacuumlo.c:38
@ TRI_NO
Definition vacuumlo.c:37

References archCustom, archDirectory, archTar, archUnknown, disable_triggers, DUMP_PRE_DATA, exit_nicely(), fb(), file_exists_in_directory(), generate_restrict_key(), get_progname(), getopt_long(), if_exists, init_parallel_dump_utils(), MAXPGPATH, NewRestoreOptions(), no_argument, no_comments, no_data, no_policies, no_publications, no_schema, no_security_labels, no_statistics, no_subscriptions, optarg, optind, option_parse_int(), opts, pg_fatal, pg_log_error, pg_log_error_hint, pg_log_info, pg_log_warning, PG_LOG_WARNING, pg_logging_increase_verbosity(), pg_logging_init(), pg_logging_set_level(), pg_malloc0_object, PG_MAX_JOBS, pg_strcasecmp(), pg_strdup(), PG_TEXTDOMAIN, progname, read_restore_filters(), required_argument, restore_all_databases(), restore_global_objects(), restore_one_database(), set_dump_section(), set_pglocale_pgservice(), simple_string_list_append(), simple_string_list_destroy(), snprintf, statistics_only, AmcheckOptions::strict_names, strict_names, TRI_NO, TRI_YES, usage(), use_setsessauth, valid_restrict_key(), AmcheckOptions::verbose, and with_statistics.

◆ read_restore_filters()

static void read_restore_filters ( const char filename,
RestoreOptions opts 
)
static

Definition at line 880 of file pg_restore.c.

881{
883 char *objname;
885 FilterObjectType objtype;
886
888
889 while (filter_read_item(&fstate, &objname, &comtype, &objtype))
890 {
892 {
893 switch (objtype)
894 {
896 break;
903 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
904 "include",
905 filter_object_type_name(objtype));
906 exit_nicely(1);
907
909 opts->selTypes = 1;
910 opts->selFunction = 1;
911 simple_string_list_append(&opts->functionNames, objname);
912 break;
914 opts->selTypes = 1;
915 opts->selIndex = 1;
916 simple_string_list_append(&opts->indexNames, objname);
917 break;
919 simple_string_list_append(&opts->schemaNames, objname);
920 break;
922 opts->selTypes = 1;
923 opts->selTable = 1;
924 simple_string_list_append(&opts->tableNames, objname);
925 break;
927 opts->selTypes = 1;
928 opts->selTrigger = 1;
929 simple_string_list_append(&opts->triggerNames, objname);
930 break;
931 }
932 }
934 {
935 switch (objtype)
936 {
938 break;
949 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
950 "exclude",
951 filter_object_type_name(objtype));
952 exit_nicely(1);
953
955 simple_string_list_append(&opts->schemaExcludeNames, objname);
956 break;
957 }
958 }
959 else
960 {
963 }
964
965 if (objname)
966 free(objname);
967 }
968
970}
#define Assert(condition)
Definition c.h:906
#define _(x)
Definition elog.c:95
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
Definition filter.c:36
void filter_free(FilterStateData *fstate)
Definition filter.c:60
const char * filter_object_type_name(FilterObjectType fot)
Definition filter.c:82
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
Definition filter.c:392
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
Definition filter.c:154
FilterObjectType
Definition filter.h:48
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
Definition filter.h:51
@ FILTER_OBJECT_TYPE_SCHEMA
Definition filter.h:57
@ FILTER_OBJECT_TYPE_INDEX
Definition filter.h:56
@ FILTER_OBJECT_TYPE_TRIGGER
Definition filter.h:60
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
Definition filter.h:54
@ FILTER_OBJECT_TYPE_DATABASE
Definition filter.h:52
@ FILTER_OBJECT_TYPE_FUNCTION
Definition filter.h:55
@ FILTER_OBJECT_TYPE_TABLE_DATA
Definition filter.h:50
@ FILTER_OBJECT_TYPE_NONE
Definition filter.h:49
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
Definition filter.h:59
@ FILTER_OBJECT_TYPE_EXTENSION
Definition filter.h:53
@ FILTER_OBJECT_TYPE_TABLE
Definition filter.h:58
FilterCommandType
Definition filter.h:38
@ FILTER_COMMAND_TYPE_NONE
Definition filter.h:39
@ FILTER_COMMAND_TYPE_EXCLUDE
Definition filter.h:41
@ FILTER_COMMAND_TYPE_INCLUDE
Definition filter.h:40
#define free(a)

References _, Assert, exit_nicely(), fb(), filename, FILTER_COMMAND_TYPE_EXCLUDE, FILTER_COMMAND_TYPE_INCLUDE, FILTER_COMMAND_TYPE_NONE, filter_free(), filter_init(), FILTER_OBJECT_TYPE_DATABASE, FILTER_OBJECT_TYPE_EXTENSION, FILTER_OBJECT_TYPE_FOREIGN_DATA, FILTER_OBJECT_TYPE_FUNCTION, FILTER_OBJECT_TYPE_INDEX, filter_object_type_name(), FILTER_OBJECT_TYPE_NONE, FILTER_OBJECT_TYPE_SCHEMA, FILTER_OBJECT_TYPE_TABLE, FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN, FILTER_OBJECT_TYPE_TABLE_DATA, FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN, FILTER_OBJECT_TYPE_TRIGGER, filter_read_item(), free, opts, pg_log_filter_error(), and simple_string_list_append().

Referenced by main().

◆ restore_all_databases()

static int restore_all_databases ( const char inputFileSpec,
SimpleStringList  db_exclude_patterns,
RestoreOptions opts,
int  numWorkers 
)
static

Definition at line 1201 of file pg_restore.c.

1204{
1206 int num_db_restore = 0;
1207 int num_total_db;
1208 int n_errors_total = 0;
1209 char *connected_db = NULL;
1210 PGconn *conn = NULL;
1213
1215
1216 /* Save db name to reuse it for all the database. */
1217 if (opts->cparams.dbname)
1218 connected_db = opts->cparams.dbname;
1219
1221
1222 pg_log_info(ngettext("found %d database name in \"%s\"",
1223 "found %d database names in \"%s\"",
1224 num_total_db),
1225 num_total_db, "map.dat");
1226
1227 /*
1228 * If exclude-patterns is given, connect to the database to process them.
1229 */
1230 if (db_exclude_patterns.head != NULL)
1231 {
1232 if (opts->cparams.dbname)
1233 {
1234 conn = ConnectDatabase(opts->cparams.dbname, NULL, opts->cparams.pghost,
1235 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1236 false, progname, NULL, NULL, NULL, NULL);
1237
1238 if (!conn)
1239 pg_fatal("could not connect to database \"%s\"", opts->cparams.dbname);
1240 }
1241
1242 if (!conn)
1243 {
1244 pg_log_info("trying to connect to database \"%s\"", "postgres");
1245
1246 conn = ConnectDatabase("postgres", NULL, opts->cparams.pghost,
1247 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1248 false, progname, NULL, NULL, NULL, NULL);
1249
1250 /* Try with template1. */
1251 if (!conn)
1252 {
1253 pg_log_info("trying to connect to database \"%s\"", "template1");
1254
1255 conn = ConnectDatabase("template1", NULL, opts->cparams.pghost,
1256 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1257 false, progname, NULL, NULL, NULL, NULL);
1258 if (!conn)
1259 {
1260 pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
1261 "Please specify an alternative database.");
1262 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
1263 exit_nicely(1);
1264 }
1265 }
1266 }
1267
1268 /* Filter the db list according to the exclude patterns. */
1271 PQfinish(conn);
1272 }
1273 else
1275
1276 /* Exit if no db needs to be restored. */
1277 if (num_db_restore == 0)
1278 {
1279 pg_log_info(ngettext("no database needs restoring out of %d database",
1280 "no database needs restoring out of %d databases", num_total_db),
1281 num_total_db);
1284 return 0;
1285 }
1286
1287 pg_log_info("need to restore %d databases out of %d databases", num_db_restore, num_total_db);
1288
1289 /*
1290 * We have a list of databases to restore after processing the
1291 * exclude-database switch(es). Now we can restore them one by one.
1292 */
1294 db_cell; db_cell = db_cell->next)
1295 {
1296 DbOidName *dbidname = (DbOidName *) db_cell->ptr;
1297 char subdirpath[MAXPGPATH];
1298 char subdirdbpath[MAXPGPATH];
1299 char dbfilename[MAXPGPATH];
1300 int n_errors;
1301
1302 /* ignore dbs marked for skipping */
1303 if (dbidname->oid == InvalidOid)
1304 continue;
1305
1306 /*
1307 * Since pg_backup_archiver.c may modify RestoreOptions during the
1308 * previous restore, we must provide a fresh copy of the original
1309 * "opts" for each call to restore_one_database.
1310 */
1312
1313 /*
1314 * We need to reset override_dbname so that objects can be restored
1315 * into an already created database. (used with -d/--dbname option)
1316 */
1317 if (tmpopts->cparams.override_dbname)
1318 {
1319 pfree(tmpopts->cparams.override_dbname);
1320 tmpopts->cparams.override_dbname = NULL;
1321 }
1322
1323 snprintf(subdirdbpath, MAXPGPATH, "%s/databases", inputFileSpec);
1324
1325 /*
1326 * Look for the database dump file/dir. If there is an {oid}.tar or
1327 * {oid}.dmp file, use it. Otherwise try to use a directory called
1328 * {oid}
1329 */
1330 snprintf(dbfilename, MAXPGPATH, "%u.tar", dbidname->oid);
1332 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.tar", inputFileSpec, dbidname->oid);
1333 else
1334 {
1335 snprintf(dbfilename, MAXPGPATH, "%u.dmp", dbidname->oid);
1336
1338 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.dmp", inputFileSpec, dbidname->oid);
1339 else
1340 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u", inputFileSpec, dbidname->oid);
1341 }
1342
1343 pg_log_info("restoring database \"%s\"", dbidname->str);
1344
1345 /* If database is already created, then don't set createDB flag. */
1346 if (tmpopts->cparams.dbname)
1347 {
1349
1350 test_conn = ConnectDatabase(dbidname->str, NULL, tmpopts->cparams.pghost,
1351 tmpopts->cparams.pgport, tmpopts->cparams.username, TRI_DEFAULT,
1352 false, progname, NULL, NULL, NULL, NULL);
1353 if (test_conn)
1354 {
1356
1357 /* Use already created database for connection. */
1358 tmpopts->createDB = 0;
1359 tmpopts->cparams.dbname = dbidname->str;
1360 }
1361 else
1362 {
1363 if (!tmpopts->createDB)
1364 {
1365 pg_log_info("skipping restore of database \"%s\": database does not exist and %s was not specified",
1366 dbidname->str, "-C/--create");
1367 continue;
1368 }
1369
1370 /* We'll have to create it */
1371 tmpopts->createDB = 1;
1372 tmpopts->cparams.dbname = connected_db;
1373 }
1374 }
1375
1376 /* Restore the single database. */
1377 n_errors = restore_one_database(subdirpath, tmpopts, numWorkers, true);
1378
1379 n_errors_total += n_errors;
1380
1381 /* Print a summary of ignored errors during single database restore. */
1382 if (n_errors)
1383 pg_log_warning("errors ignored on database \"%s\" restore: %d", dbidname->str, n_errors);
1384 }
1385
1386 /* Log number of processed databases. */
1387 pg_log_info("number of restored databases is %d", num_db_restore);
1388
1389 /* Free dbname and dboid list. */
1391
1394
1395 return n_errors_total;
1396}
#define ngettext(s, p, n)
Definition c.h:1233
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)
Definition connectdb.c:40
void pg_free(void *ptr)
static int get_dbnames_list_to_restore(PGconn *conn, SimplePtrList *dbname_oid_list, SimpleStringList db_exclude_patterns)
Definition pg_restore.c:999
static int get_dbname_oid_list_from_mfile(const char *dumpdirpatharg, SimplePtrList *dbname_oid_list)
void simple_ptr_list_destroy(SimplePtrList *list)
@ TRI_DEFAULT
Definition vacuumlo.c:36

References conn, ConnectDatabase(), exit_nicely(), fb(), file_exists_in_directory(), get_dbname_oid_list_from_mfile(), get_dbnames_list_to_restore(), InvalidOid, MAXPGPATH, ngettext, opts, pfree(), pg_fatal, pg_free(), pg_log_error, pg_log_error_hint, pg_log_info, pg_log_warning, pg_malloc0_object, PQfinish(), progname, restore_one_database(), simple_ptr_list_destroy(), snprintf, and TRI_DEFAULT.

Referenced by main().

◆ restore_global_objects()

static int restore_global_objects ( const char inputFileSpec,
RestoreOptions opts 
)
static

Definition at line 693 of file pg_restore.c.

694{
695 Archive *AH;
696 int nerror = 0;
697
698 /* Set format as custom so that toc.glo file can be read. */
699 opts->format = archCustom;
700 opts->txn_size = 0;
701
702 AH = OpenArchive(inputFileSpec, opts->format);
703
705
707
708 /* Let the archiver know how noisy to be */
709 AH->verbose = opts->verbose;
710
711 /* Don't output TOC entry comments when restoring globals */
712 ((ArchiveHandle *) AH)->noTocComments = 1;
713
714 AH->exit_on_error = false;
715
716 /* Parallel execution is not supported for global object restoration. */
717 AH->numWorkers = 1;
718
720 RestoreArchive(AH, false);
721
722 nerror = AH->n_errors;
723
724 /* AH may be freed in CloseArchive? */
725 CloseArchive(AH);
726
727 return nerror;
728}
void on_exit_close_archive(Archive *AHX)
Definition parallel.c:330
void ProcessArchiveRestoreOptions(Archive *AHX)
Archive * OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
void RestoreArchive(Archive *AHX, bool append_data)
void CloseArchive(Archive *AHX)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
bool exit_on_error
Definition pg_backup.h:252
int n_errors
Definition pg_backup.h:253
int numWorkers
Definition pg_backup.h:240
int verbose
Definition pg_backup.h:232

References archCustom, CloseArchive(), Archive::exit_on_error, fb(), Archive::n_errors, Archive::numWorkers, on_exit_close_archive(), OpenArchive(), opts, ProcessArchiveRestoreOptions(), RestoreArchive(), SetArchiveOptions(), AmcheckOptions::verbose, and Archive::verbose.

Referenced by main().

◆ restore_one_database()

static int restore_one_database ( const char inputFileSpec,
RestoreOptions opts,
int  numWorkers,
bool  append_data 
)
static

Definition at line 738 of file pg_restore.c.

740{
741 Archive *AH;
742 int n_errors;
743
744 AH = OpenArchive(inputFileSpec, opts->format);
745
747
748 /*
749 * We don't have a connection yet but that doesn't matter. The connection
750 * is initialized to NULL and if we terminate through exit_nicely() while
751 * it's still NULL, the cleanup function will just be a no-op. If we are
752 * restoring multiple databases, then only update AX handle for cleanup as
753 * the previous entry was already in the array and we had closed previous
754 * connection, so we can use the same array slot.
755 */
756 if (!append_data)
758 else
760
761 /* Let the archiver know how noisy to be */
762 AH->verbose = opts->verbose;
763
764 /*
765 * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
766 */
767 AH->exit_on_error = opts->exit_on_error;
768
769 if (opts->tocFile)
770 SortTocFromFile(AH);
771
772 AH->numWorkers = numWorkers;
773
774 if (opts->tocSummary)
775 PrintTOCSummary(AH);
776 else
777 {
780 }
781
782 n_errors = AH->n_errors;
783
784 /* AH may be freed in CloseArchive? */
785 CloseArchive(AH);
786
787 return n_errors;
788}
void replace_on_exit_close_archive(Archive *AHX)
Definition parallel.c:345
static size_t append_data(char *buf, size_t size, size_t nmemb, void *userdata)
void SortTocFromFile(Archive *AHX)
void PrintTOCSummary(Archive *AHX)

References append_data(), CloseArchive(), Archive::exit_on_error, fb(), Archive::n_errors, Archive::numWorkers, on_exit_close_archive(), OpenArchive(), opts, PrintTOCSummary(), ProcessArchiveRestoreOptions(), replace_on_exit_close_archive(), RestoreArchive(), SetArchiveOptions(), SortTocFromFile(), AmcheckOptions::verbose, and Archive::verbose.

Referenced by main(), and restore_all_databases().

◆ usage()

static void usage ( const char progname)
static

Definition at line 791 of file pg_restore.c.

792{
793 printf(_("%s restores PostgreSQL databases from archives created by pg_dump or pg_dumpall.\n\n"), progname);
794 printf(_("Usage:\n"));
795 printf(_(" %s [OPTION]... [FILE]\n"), progname);
796
797 printf(_("\nGeneral options:\n"));
798 printf(_(" -d, --dbname=NAME connect to database name\n"));
799 printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
800 printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
801 printf(_(" -l, --list print summarized TOC of the archive\n"));
802 printf(_(" -v, --verbose verbose mode\n"));
803 printf(_(" -V, --version output version information, then exit\n"));
804 printf(_(" -?, --help show this help, then exit\n"));
805
806 printf(_("\nOptions controlling the restore:\n"));
807 printf(_(" -a, --data-only restore only the data, no schema\n"));
808 printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
809 printf(_(" -C, --create create the target database\n"));
810 printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
811 printf(_(" -g, --globals-only restore only global objects, no databases\n"));
812 printf(_(" -I, --index=NAME restore named index\n"));
813 printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
814 printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
815 " selecting/ordering output\n"));
816 printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
817 printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
818 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
819 printf(_(" -P, --function=NAME(args) restore named function\n"));
820 printf(_(" -s, --schema-only restore only the schema, no data\n"));
821 printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
822 printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
823 printf(_(" -T, --trigger=NAME restore named trigger\n"));
824 printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
825 printf(_(" -1, --single-transaction restore as a single transaction\n"));
826 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
827 printf(_(" --enable-row-security enable row security\n"));
828 printf(_(" --exclude-database=PATTERN do not restore the specified database(s)\n"));
829 printf(_(" --filter=FILENAME restore or skip objects based on expressions\n"
830 " in FILENAME\n"));
831 printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
832 printf(_(" --no-comments do not restore comment commands\n"));
833 printf(_(" --no-data do not restore data\n"));
834 printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
835 " created\n"));
836 printf(_(" --no-policies do not restore row security policies\n"));
837 printf(_(" --no-publications do not restore publications\n"));
838 printf(_(" --no-schema do not restore schema\n"));
839 printf(_(" --no-security-labels do not restore security labels\n"));
840 printf(_(" --no-statistics do not restore statistics\n"));
841 printf(_(" --no-subscriptions do not restore subscriptions\n"));
842 printf(_(" --no-globals do not restore global objects (roles and tablespaces)\n"));
843 printf(_(" --no-table-access-method do not restore table access methods\n"));
844 printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
845 printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
846 printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
847 printf(_(" --statistics restore the statistics\n"));
848 printf(_(" --statistics-only restore only the statistics, not schema or data\n"));
849 printf(_(" --strict-names require table and/or schema include patterns to\n"
850 " match at least one entity each\n"));
851 printf(_(" --transaction-size=N commit after every N objects\n"));
852 printf(_(" --use-set-session-authorization\n"
853 " use SET SESSION AUTHORIZATION commands instead of\n"
854 " ALTER OWNER commands to set ownership\n"));
855
856 printf(_("\nConnection options:\n"));
857 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
858 printf(_(" -p, --port=PORT database server port number\n"));
859 printf(_(" -U, --username=NAME connect as specified database user\n"));
860 printf(_(" -w, --no-password never prompt for password\n"));
861 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
862 printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
863
864 printf(_("\n"
865 "The options -I, -n, -N, -P, -t, -T, --section, and --exclude-database can be\n"
866 "combined and specified multiple times to select multiple objects.\n"));
867 printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
868 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
869 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
870}
#define printf(...)
Definition port.h:266

References _, fb(), printf, and progname.