PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_restore.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <sys/stat.h>
#include "common/string.h"
#include "connectdb.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, int num)
 
static int read_one_statement (StringInfo inBuf, FILE *pfile)
 
static int restore_all_databases (PGconn *conn, const char *dumpdirpath, SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers)
 
static int process_global_sql_commands (PGconn *conn, const char *dumpdirpath, const char *outfile)
 
static void copy_or_print_global_file (const char *outfile, FILE *pfile)
 
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 *dumpdirpath, SimplePtrList *dbname_oid_list)
 
int main (int argc, char **argv)
 

Typedef Documentation

◆ DbOidName

typedef struct DbOidName DbOidName

Function Documentation

◆ copy_or_print_global_file()

static void copy_or_print_global_file ( const char *  outfile,
FILE *  pfile 
)
static

Definition at line 1379 of file pg_restore.c.

1380{
1381 char out_file_path[MAXPGPATH];
1382 FILE *OPF;
1383 int c;
1384
1385 /* "-" is used for stdout. */
1386 if (strcmp(outfile, "-") == 0)
1387 OPF = stdout;
1388 else
1389 {
1390 snprintf(out_file_path, MAXPGPATH, "%s", outfile);
1391 OPF = fopen(out_file_path, PG_BINARY_W);
1392
1393 if (OPF == NULL)
1394 {
1395 fclose(pfile);
1396 pg_fatal("could not open file: \"%s\"", outfile);
1397 }
1398 }
1399
1400 /* Append global.dat into output file or print to stdout. */
1401 while ((c = fgetc(pfile)) != EOF)
1402 fputc(c, OPF);
1403
1404 fclose(pfile);
1405
1406 /* Close output file. */
1407 if (strcmp(outfile, "-") != 0)
1408 fclose(OPF);
1409}
#define PG_BINARY_W
Definition: c.h:1247
#define pg_fatal(...)
#define MAXPGPATH
static FILE * OPF
Definition: pg_dumpall.c:122
static char * outfile
#define snprintf
Definition: port.h:239
char * c

References MAXPGPATH, OPF, outfile, PG_BINARY_W, pg_fatal, snprintf, and generate_unaccent_rules::stdout.

Referenced by process_global_sql_commands().

◆ file_exists_in_directory()

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

Definition at line 842 of file pg_restore.c.

843{
844 struct stat st;
845 char buf[MAXPGPATH];
846
847 if (snprintf(buf, MAXPGPATH, "%s/%s", dir, filename) >= MAXPGPATH)
848 pg_fatal("directory name too long: \"%s\"", dir);
849
850 return (stat(buf, &st) == 0 && S_ISREG(st.st_mode));
851}
static char * filename
Definition: pg_dumpall.c:123
static char * buf
Definition: pg_test_fsync.c:72
#define stat
Definition: win32_port.h:274
#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 *  dumpdirpath,
SimplePtrList dbname_oid_list 
)
static

Definition at line 1038 of file pg_restore.c.

1039{
1040 StringInfoData linebuf;
1041 FILE *pfile;
1042 char map_file_path[MAXPGPATH];
1043 int count = 0;
1044
1045
1046 /*
1047 * If there is only global.dat file in dump, then return from here as
1048 * there is no database to restore.
1049 */
1050 if (!file_exists_in_directory(dumpdirpath, "map.dat"))
1051 {
1052 pg_log_info("database restoring is skipped as \"map.dat\" is not present in \"%s\"", dumpdirpath);
1053 return 0;
1054 }
1055
1056 snprintf(map_file_path, MAXPGPATH, "%s/map.dat", dumpdirpath);
1057
1058 /* Open map.dat file. */
1059 pfile = fopen(map_file_path, PG_BINARY_R);
1060
1061 if (pfile == NULL)
1062 pg_fatal("could not open \"%s\": %m", map_file_path);
1063
1064 initStringInfo(&linebuf);
1065
1066 /* Append all the dbname/db_oid combinations to the list. */
1067 while (pg_get_line_buf(pfile, &linebuf))
1068 {
1069 Oid db_oid = InvalidOid;
1070 char *dbname;
1071 DbOidName *dbidname;
1072 int namelen;
1073 char *p = linebuf.data;
1074
1075 /* Extract dboid. */
1076 while (isdigit((unsigned char) *p))
1077 p++;
1078 if (p > linebuf.data && *p == ' ')
1079 {
1080 sscanf(linebuf.data, "%u", &db_oid);
1081 p++;
1082 }
1083
1084 /* dbname is the rest of the line */
1085 dbname = p;
1086 namelen = strlen(dbname);
1087
1088 /* Report error and exit if the file has any corrupted data. */
1089 if (!OidIsValid(db_oid) || namelen <= 1)
1090 pg_fatal("invalid entry in \"%s\" at line: %d", map_file_path,
1091 count + 1);
1092
1093 pg_log_info("found database \"%s\" (OID: %u) in \"%s\"",
1094 dbname, db_oid, map_file_path);
1095
1096 dbidname = pg_malloc(offsetof(DbOidName, str) + namelen + 1);
1097 dbidname->oid = db_oid;
1098 strlcpy(dbidname->str, dbname, namelen);
1099
1100 simple_ptr_list_append(dbname_oid_list, dbidname);
1101 count++;
1102 }
1103
1104 /* Close map.dat file. */
1105 fclose(pfile);
1106
1107 return count;
1108}
#define PG_BINARY_R
Definition: c.h:1246
#define OidIsValid(objectId)
Definition: c.h:746
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
const char * str
#define pg_log_info(...)
Definition: logging.h:124
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:842
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
void simple_ptr_list_append(SimplePtrList *list, void *ptr)
Definition: simple_list.c:162
char * dbname
Definition: streamutil.c:49
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
char str[FLEXIBLE_ARRAY_MEMBER]
Definition: pg_restore.c:81

References StringInfoData::data, dbname, file_exists_in_directory(), initStringInfo(), InvalidOid, MAXPGPATH, DbOidName::oid, OidIsValid, PG_BINARY_R, pg_fatal, pg_get_line_buf(), pg_log_info, pg_malloc(), simple_ptr_list_append(), snprintf, DbOidName::str, 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 939 of file pg_restore.c.

942{
943 int count_db = 0;
944 PQExpBuffer query;
945 PGresult *res;
946
947 query = createPQExpBuffer();
948
949 if (!conn)
950 pg_log_info("considering PATTERN as NAME for --exclude-database option as no db connection while doing pg_restore.");
951
952 /*
953 * Process one by one all dbnames and if specified to skip restoring, then
954 * remove dbname from list.
955 */
956 for (SimplePtrListCell *db_cell = dbname_oid_list->head;
957 db_cell; db_cell = db_cell->next)
958 {
959 DbOidName *dbidname = (DbOidName *) db_cell->ptr;
960 bool skip_db_restore = false;
962
963 appendStringLiteralConn(db_lit, dbidname->str, conn);
964
965 for (SimpleStringListCell *pat_cell = db_exclude_patterns.head; pat_cell; pat_cell = pat_cell->next)
966 {
967 /*
968 * If there is an exact match then we don't need to try a pattern
969 * match
970 */
971 if (pg_strcasecmp(dbidname->str, pat_cell->val) == 0)
972 skip_db_restore = true;
973 /* Otherwise, try a pattern match if there is a connection */
974 else if (conn)
975 {
976 int dotcnt;
977
978 appendPQExpBufferStr(query, "SELECT 1 ");
979 processSQLNamePattern(conn, query, pat_cell->val, false,
980 false, NULL, db_lit->data,
981 NULL, NULL, NULL, &dotcnt);
982
983 if (dotcnt > 0)
984 {
985 pg_log_error("improper qualified name (too many dotted names): %s",
986 dbidname->str);
987 PQfinish(conn);
988 exit_nicely(1);
989 }
990
991 res = executeQuery(conn, query->data);
992
993 if ((PQresultStatus(res) == PGRES_TUPLES_OK) && PQntuples(res))
994 {
995 skip_db_restore = true;
996 pg_log_info("database \"%s\" matches exclude pattern: \"%s\"", dbidname->str, pat_cell->val);
997 }
998
999 PQclear(res);
1000 resetPQExpBuffer(query);
1001 }
1002
1003 if (skip_db_restore)
1004 break;
1005 }
1006
1007 destroyPQExpBuffer(db_lit);
1008
1009 /*
1010 * Mark db to be skipped or increment the counter of dbs to be
1011 * restored
1012 */
1013 if (skip_db_restore)
1014 {
1015 pg_log_info("excluding database \"%s\"", dbidname->str);
1016 dbidname->oid = InvalidOid;
1017 }
1018 else
1019 {
1020 count_db++;
1021 }
1022 }
1023
1024 destroyPQExpBuffer(query);
1025
1026 return count_db;
1027}
PGresult * executeQuery(PGconn *conn, const char *query)
Definition: connectdb.c:278
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5290
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
void PQclear(PGresult *res)
Definition: fe-exec.c:721
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:128
#define pg_log_error(...)
Definition: logging.h:106
void exit_nicely(int code)
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
PGconn * conn
Definition: streamutil.c:52
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:446
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)
struct SimplePtrListCell * next
Definition: simple_list.h:48
SimplePtrListCell * head
Definition: simple_list.h:54
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42

References appendPQExpBufferStr(), appendStringLiteralConn(), conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), exit_nicely(), SimpleStringList::head, SimplePtrList::head, InvalidOid, SimpleStringListCell::next, SimplePtrListCell::next, DbOidName::oid, pg_log_error, pg_log_info, pg_strcasecmp(), PGRES_TUPLES_OK, PQclear(), PQfinish(), PQntuples(), PQresultStatus(), processSQLNamePattern(), resetPQExpBuffer(), and DbOidName::str.

Referenced by restore_all_databases().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 86 of file pg_restore.c.

87{
89 int c;
90 int numWorkers = 1;
91 char *inputFileSpec;
92 bool data_only = false;
93 bool schema_only = false;
94 int n_errors = 0;
95 bool globals_only = false;
96 SimpleStringList db_exclude_patterns = {NULL, NULL};
97 static int disable_triggers = 0;
98 static int enable_row_security = 0;
99 static int if_exists = 0;
100 static int no_data_for_failed_tables = 0;
101 static int outputNoTableAm = 0;
102 static int outputNoTablespaces = 0;
103 static int use_setsessauth = 0;
104 static int no_comments = 0;
105 static int no_data = 0;
106 static int no_policies = 0;
107 static int no_publications = 0;
108 static int no_schema = 0;
109 static int no_security_labels = 0;
110 static int no_statistics = 0;
111 static int no_subscriptions = 0;
112 static int strict_names = 0;
113 static int statistics_only = 0;
114 static int with_data = 0;
115 static int with_schema = 0;
116 static int with_statistics = 0;
117
118 struct option cmdopts[] = {
119 {"clean", 0, NULL, 'c'},
120 {"create", 0, NULL, 'C'},
121 {"data-only", 0, NULL, 'a'},
122 {"globals-only", 0, NULL, 'g'},
123 {"dbname", 1, NULL, 'd'},
124 {"exit-on-error", 0, NULL, 'e'},
125 {"exclude-schema", 1, NULL, 'N'},
126 {"file", 1, NULL, 'f'},
127 {"format", 1, NULL, 'F'},
128 {"function", 1, NULL, 'P'},
129 {"host", 1, NULL, 'h'},
130 {"index", 1, NULL, 'I'},
131 {"jobs", 1, NULL, 'j'},
132 {"list", 0, NULL, 'l'},
133 {"no-privileges", 0, NULL, 'x'},
134 {"no-acl", 0, NULL, 'x'},
135 {"no-owner", 0, NULL, 'O'},
136 {"no-reconnect", 0, NULL, 'R'},
137 {"port", 1, NULL, 'p'},
138 {"no-password", 0, NULL, 'w'},
139 {"password", 0, NULL, 'W'},
140 {"schema", 1, NULL, 'n'},
141 {"schema-only", 0, NULL, 's'},
142 {"superuser", 1, NULL, 'S'},
143 {"table", 1, NULL, 't'},
144 {"trigger", 1, NULL, 'T'},
145 {"use-list", 1, NULL, 'L'},
146 {"username", 1, NULL, 'U'},
147 {"verbose", 0, NULL, 'v'},
148 {"single-transaction", 0, NULL, '1'},
149
150 /*
151 * the following options don't have an equivalent short option letter
152 */
153 {"disable-triggers", no_argument, &disable_triggers, 1},
154 {"enable-row-security", no_argument, &enable_row_security, 1},
155 {"if-exists", no_argument, &if_exists, 1},
156 {"no-data-for-failed-tables", no_argument, &no_data_for_failed_tables, 1},
157 {"no-table-access-method", no_argument, &outputNoTableAm, 1},
158 {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
159 {"role", required_argument, NULL, 2},
160 {"section", required_argument, NULL, 3},
161 {"strict-names", no_argument, &strict_names, 1},
162 {"transaction-size", required_argument, NULL, 5},
163 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
164 {"no-comments", no_argument, &no_comments, 1},
165 {"no-data", no_argument, &no_data, 1},
166 {"no-policies", no_argument, &no_policies, 1},
167 {"no-publications", no_argument, &no_publications, 1},
168 {"no-schema", no_argument, &no_schema, 1},
169 {"no-security-labels", no_argument, &no_security_labels, 1},
170 {"no-subscriptions", no_argument, &no_subscriptions, 1},
171 {"no-statistics", no_argument, &no_statistics, 1},
172 {"with-data", no_argument, &with_data, 1},
173 {"with-schema", no_argument, &with_schema, 1},
174 {"with-statistics", no_argument, &with_statistics, 1},
175 {"statistics-only", no_argument, &statistics_only, 1},
176 {"filter", required_argument, NULL, 4},
177 {"exclude-database", required_argument, NULL, 6},
178
179 {NULL, 0, NULL, 0}
180 };
181
182 pg_logging_init(argv[0]);
184 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
185
187
189
190 progname = get_progname(argv[0]);
191
192 if (argc > 1)
193 {
194 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
195 {
197 exit_nicely(0);
198 }
199 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
200 {
201 puts("pg_restore (PostgreSQL) " PG_VERSION);
202 exit_nicely(0);
203 }
204 }
205
206 while ((c = getopt_long(argc, argv, "acCd:ef:F:gh:I:j:lL:n:N:Op:P:RsS:t:T:U:vwWx1",
207 cmdopts, NULL)) != -1)
208 {
209 switch (c)
210 {
211 case 'a': /* Dump data only */
212 data_only = true;
213 break;
214 case 'c': /* clean (i.e., drop) schema prior to create */
215 opts->dropSchema = 1;
216 break;
217 case 'C':
218 opts->createDB = 1;
219 break;
220 case 'd':
221 opts->cparams.dbname = pg_strdup(optarg);
222 break;
223 case 'e':
224 opts->exit_on_error = true;
225 break;
226 case 'f': /* output file name */
227 opts->filename = pg_strdup(optarg);
228 break;
229 case 'F':
230 if (strlen(optarg) != 0)
231 opts->formatName = pg_strdup(optarg);
232 break;
233 case 'g':
234 /* restore only global.dat file from directory */
235 globals_only = true;
236 break;
237 case 'h':
238 if (strlen(optarg) != 0)
239 opts->cparams.pghost = pg_strdup(optarg);
240 break;
241 case 'j': /* number of restore jobs */
242 if (!option_parse_int(optarg, "-j/--jobs", 1,
244 &numWorkers))
245 exit(1);
246 break;
247
248 case 'l': /* Dump the TOC summary */
249 opts->tocSummary = 1;
250 break;
251
252 case 'L': /* input TOC summary file name */
253 opts->tocFile = pg_strdup(optarg);
254 break;
255
256 case 'n': /* Dump data for this schema only */
257 simple_string_list_append(&opts->schemaNames, optarg);
258 break;
259
260 case 'N': /* Do not dump data for this schema */
261 simple_string_list_append(&opts->schemaExcludeNames, optarg);
262 break;
263
264 case 'O':
265 opts->noOwner = 1;
266 break;
267
268 case 'p':
269 if (strlen(optarg) != 0)
270 opts->cparams.pgport = pg_strdup(optarg);
271 break;
272 case 'R':
273 /* no-op, still accepted for backwards compatibility */
274 break;
275 case 'P': /* Function */
276 opts->selTypes = 1;
277 opts->selFunction = 1;
278 simple_string_list_append(&opts->functionNames, optarg);
279 break;
280 case 'I': /* Index */
281 opts->selTypes = 1;
282 opts->selIndex = 1;
284 break;
285 case 'T': /* Trigger */
286 opts->selTypes = 1;
287 opts->selTrigger = 1;
288 simple_string_list_append(&opts->triggerNames, optarg);
289 break;
290 case 's': /* dump schema only */
291 schema_only = true;
292 break;
293 case 'S': /* Superuser username */
294 if (strlen(optarg) != 0)
295 opts->superuser = pg_strdup(optarg);
296 break;
297 case 't': /* Dump specified table(s) only */
298 opts->selTypes = 1;
299 opts->selTable = 1;
301 break;
302
303 case 'U':
304 opts->cparams.username = pg_strdup(optarg);
305 break;
306
307 case 'v': /* verbose */
308 opts->verbose = 1;
310 break;
311
312 case 'w':
313 opts->cparams.promptPassword = TRI_NO;
314 break;
315
316 case 'W':
317 opts->cparams.promptPassword = TRI_YES;
318 break;
319
320 case 'x': /* skip ACL dump */
321 opts->aclsSkip = 1;
322 break;
323
324 case '1': /* Restore data in a single transaction */
325 opts->single_txn = true;
326 opts->exit_on_error = true;
327 break;
328
329 case 0:
330
331 /*
332 * This covers the long options without a short equivalent.
333 */
334 break;
335
336 case 2: /* SET ROLE */
337 opts->use_role = pg_strdup(optarg);
338 break;
339
340 case 3: /* section */
341 set_dump_section(optarg, &(opts->dumpSections));
342 break;
343
344 case 4: /* filter */
346 break;
347
348 case 5: /* transaction-size */
349 if (!option_parse_int(optarg, "--transaction-size",
350 1, INT_MAX,
351 &opts->txn_size))
352 exit(1);
353 opts->exit_on_error = true;
354 break;
355 case 6: /* database patterns to skip */
356 simple_string_list_append(&db_exclude_patterns, optarg);
357 break;
358
359 default:
360 /* getopt_long already emitted a complaint */
361 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
362 exit_nicely(1);
363 }
364 }
365
366 /* Get file name from command line */
367 if (optind < argc)
368 inputFileSpec = argv[optind++];
369 else
370 inputFileSpec = NULL;
371
372 /* Complain if any arguments remain */
373 if (optind < argc)
374 {
375 pg_log_error("too many command-line arguments (first is \"%s\")",
376 argv[optind]);
377 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
378 exit_nicely(1);
379 }
380
381 /* Complain if neither -f nor -d was specified (except if dumping TOC) */
382 if (!opts->cparams.dbname && !opts->filename && !opts->tocSummary)
383 pg_fatal("one of -d/--dbname and -f/--file must be specified");
384
385 if (db_exclude_patterns.head != NULL && globals_only)
386 {
387 pg_log_error("option --exclude-database cannot be used together with -g/--globals-only");
388 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
389 exit_nicely(1);
390 }
391
392 /* Should get at most one of -d and -f, else user is confused */
393 if (opts->cparams.dbname)
394 {
395 if (opts->filename)
396 {
397 pg_log_error("options -d/--dbname and -f/--file cannot be used together");
398 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
399 exit_nicely(1);
400 }
401 opts->useDB = 1;
402 }
403
404 /* reject conflicting "-only" options */
405 if (data_only && schema_only)
406 pg_fatal("options -s/--schema-only and -a/--data-only cannot be used together");
407 if (schema_only && statistics_only)
408 pg_fatal("options -s/--schema-only and --statistics-only cannot be used together");
409 if (data_only && statistics_only)
410 pg_fatal("options -a/--data-only and --statistics-only cannot be used together");
411
412 /* reject conflicting "-only" and "no-" options */
413 if (data_only && no_data)
414 pg_fatal("options -a/--data-only and --no-data cannot be used together");
415 if (schema_only && no_schema)
416 pg_fatal("options -s/--schema-only and --no-schema cannot be used together");
418 pg_fatal("options --statistics-only and --no-statistics cannot be used together");
419
420 /* reject conflicting "with-" and "no-" options */
421 if (with_data && no_data)
422 pg_fatal("options --with-data and --no-data cannot be used together");
423 if (with_schema && no_schema)
424 pg_fatal("options --with-schema and --no-schema cannot be used together");
426 pg_fatal("options --with-statistics and --no-statistics cannot be used together");
427
428 if (data_only && opts->dropSchema)
429 pg_fatal("options -c/--clean and -a/--data-only cannot be used together");
430
431 if (opts->single_txn && opts->txn_size > 0)
432 pg_fatal("options -1/--single-transaction and --transaction-size cannot be used together");
433
434 /*
435 * -C is not compatible with -1, because we can't create a database inside
436 * a transaction block.
437 */
438 if (opts->createDB && opts->single_txn)
439 pg_fatal("options -C/--create and -1/--single-transaction cannot be used together");
440
441 /* Can't do single-txn mode with multiple connections */
442 if (opts->single_txn && numWorkers > 1)
443 pg_fatal("cannot specify both --single-transaction and multiple jobs");
444
445 /*
446 * Set derivative flags. An "-only" option may be overridden by an
447 * explicit "with-" option; e.g. "--schema-only --with-statistics" will
448 * include schema and statistics. Other ambiguous or nonsensical
449 * combinations, e.g. "--schema-only --no-schema", will have already
450 * caused an error in one of the checks above.
451 */
452 opts->dumpData = ((opts->dumpData && !schema_only && !statistics_only) ||
453 (data_only || with_data)) && !no_data;
454 opts->dumpSchema = ((opts->dumpSchema && !data_only && !statistics_only) ||
455 (schema_only || with_schema)) && !no_schema;
456 opts->dumpStatistics = ((opts->dumpStatistics && !schema_only && !data_only) ||
458
459 opts->disable_triggers = disable_triggers;
460 opts->enable_row_security = enable_row_security;
461 opts->noDataForFailedTables = no_data_for_failed_tables;
462 opts->noTableAm = outputNoTableAm;
463 opts->noTablespace = outputNoTablespaces;
464 opts->use_setsessauth = use_setsessauth;
465 opts->no_comments = no_comments;
466 opts->no_policies = no_policies;
467 opts->no_publications = no_publications;
468 opts->no_security_labels = no_security_labels;
469 opts->no_subscriptions = no_subscriptions;
470
471 if (if_exists && !opts->dropSchema)
472 pg_fatal("option --if-exists requires option -c/--clean");
473 opts->if_exists = if_exists;
475
476 if (opts->formatName)
477 {
478 if (pg_strcasecmp(opts->formatName, "c") == 0 ||
479 pg_strcasecmp(opts->formatName, "custom") == 0)
480 opts->format = archCustom;
481 else if (pg_strcasecmp(opts->formatName, "d") == 0 ||
482 pg_strcasecmp(opts->formatName, "directory") == 0)
483 opts->format = archDirectory;
484 else if (pg_strcasecmp(opts->formatName, "t") == 0 ||
485 pg_strcasecmp(opts->formatName, "tar") == 0)
486 opts->format = archTar;
487 else if (pg_strcasecmp(opts->formatName, "p") == 0 ||
488 pg_strcasecmp(opts->formatName, "plain") == 0)
489 {
490 /* recognize this for consistency with pg_dump */
491 pg_fatal("archive format \"%s\" is not supported; please use psql",
492 opts->formatName);
493 }
494 else
495 pg_fatal("unrecognized archive format \"%s\"; please specify \"c\", \"d\", or \"t\"",
496 opts->formatName);
497 }
498
499 /*
500 * If toc.dat file is not present in the current path, then check for
501 * global.dat. If global.dat file is present, then restore all the
502 * databases from map.dat (if it exists), but skip restoring those
503 * matching --exclude-database patterns.
504 */
505 if (inputFileSpec != NULL && !file_exists_in_directory(inputFileSpec, "toc.dat") &&
506 file_exists_in_directory(inputFileSpec, "global.dat"))
507 {
508 PGconn *conn = NULL; /* Connection to restore global sql
509 * commands. */
510
511 /*
512 * Can only use --list or --use-list options with a single database
513 * dump.
514 */
515 if (opts->tocSummary)
516 pg_fatal("option -l/--list cannot be used when restoring an archive created by pg_dumpall");
517 else if (opts->tocFile)
518 pg_fatal("option -L/--use-list cannot be used when restoring an archive created by pg_dumpall");
519
520 /*
521 * To restore from a pg_dumpall archive, -C (create database) option
522 * must be specified unless we are only restoring globals.
523 */
524 if (!globals_only && opts->createDB != 1)
525 {
526 pg_log_error("-C/--create option should be specified when restoring an archive created by pg_dumpall");
527 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
528 pg_log_error_hint("Individual databases can be restored using their specific archives.");
529 exit_nicely(1);
530 }
531
532 /*
533 * Connect to the database to execute global sql commands from
534 * global.dat file.
535 */
536 if (opts->cparams.dbname)
537 {
538 conn = ConnectDatabase(opts->cparams.dbname, NULL, opts->cparams.pghost,
539 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
540 false, progname, NULL, NULL, NULL, NULL);
541
542
543 if (!conn)
544 pg_fatal("could not connect to database \"%s\"", opts->cparams.dbname);
545 }
546
547 /* If globals-only, then return from here. */
548 if (globals_only)
549 {
550 /*
551 * Open global.dat file and execute/append all the global sql
552 * commands.
553 */
554 n_errors = process_global_sql_commands(conn, inputFileSpec,
555 opts->filename);
556
557 if (conn)
558 PQfinish(conn);
559
560 pg_log_info("database restoring skipped as -g/--globals-only option was specified");
561 }
562 else
563 {
564 /* Now restore all the databases from map.dat */
565 n_errors = restore_all_databases(conn, inputFileSpec, db_exclude_patterns,
566 opts, numWorkers);
567 }
568
569 /* Free db pattern list. */
570 simple_string_list_destroy(&db_exclude_patterns);
571 }
572 else /* process if global.dat file does not exist. */
573 {
574 if (db_exclude_patterns.head != NULL)
575 pg_fatal("option --exclude-database can be used only when restoring an archive created by pg_dumpall");
576
577 if (globals_only)
578 pg_fatal("option -g/--globals-only can be used only when restoring an archive created by pg_dumpall");
579
580 n_errors = restore_one_database(inputFileSpec, opts, numWorkers, false, 0);
581 }
582
583 /* Done, print a summary of ignored errors during restore. */
584 if (n_errors)
585 {
586 pg_log_warning("errors ignored on restore: %d", n_errors);
587 return 1;
588 }
589
590 return 0;
591}
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:1185
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
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
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
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)
Definition: option_utils.c:50
static AmcheckOptions opts
Definition: pg_amcheck.c:112
RestoreOptions * NewRestoreOptions(void)
@ 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)
static int strict_names
Definition: pg_dump.c:152
static int if_exists
Definition: pg_dumpall.c:94
static int statistics_only
Definition: pg_dumpall.c:116
static int no_policies
Definition: pg_dumpall.c:100
static int disable_triggers
Definition: pg_dumpall.c:93
static int with_data
Definition: pg_dumpall.c:110
static int no_comments
Definition: pg_dumpall.c:99
static int no_publications
Definition: pg_dumpall.c:101
static int with_schema
Definition: pg_dumpall.c:111
static int no_security_labels
Definition: pg_dumpall.c:102
static int no_statistics
Definition: pg_dumpall.c:105
static int use_setsessauth
Definition: pg_dumpall.c:98
static int no_data
Definition: pg_dumpall.c:103
static int no_schema
Definition: pg_dumpall.c:104
static int no_subscriptions
Definition: pg_dumpall.c:106
static int with_statistics
Definition: pg_dumpall.c:112
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
static void usage(const char *progname)
Definition: pg_restore.c:654
static int restore_all_databases(PGconn *conn, const char *dumpdirpath, SimpleStringList db_exclude_patterns, RestoreOptions *opts, int numWorkers)
Definition: pg_restore.c:1122
static void read_restore_filters(const char *filename, RestoreOptions *opts)
Definition: pg_restore.c:744
static int process_global_sql_commands(PGconn *conn, const char *dumpdirpath, const char *outfile)
Definition: pg_restore.c:1303
static int restore_one_database(const char *inputFileSpec, RestoreOptions *opts, int numWorkers, bool append_data, int num)
Definition: pg_restore.c:601
#define pg_log_warning(...)
Definition: pgfnames.c:24
const char * get_progname(const char *argv0)
Definition: path.c:652
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
void simple_string_list_destroy(SimpleStringList *list)
Definition: simple_list.c:125
bool strict_names
Definition: pg_amcheck.c:61
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37

References archCustom, archDirectory, archTar, conn, ConnectDatabase(), disable_triggers, exit_nicely(), file_exists_in_directory(), get_progname(), getopt_long(), SimpleStringList::head, if_exists, init_parallel_dump_utils(), 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_MAX_JOBS, pg_strcasecmp(), pg_strdup(), PG_TEXTDOMAIN, PQfinish(), process_global_sql_commands(), progname, read_restore_filters(), required_argument, restore_all_databases(), restore_one_database(), set_dump_section(), set_pglocale_pgservice(), simple_string_list_append(), simple_string_list_destroy(), statistics_only, AmcheckOptions::strict_names, strict_names, TRI_DEFAULT, TRI_NO, TRI_YES, usage(), use_setsessauth, AmcheckOptions::verbose, with_data, with_schema, and with_statistics.

◆ process_global_sql_commands()

static int process_global_sql_commands ( PGconn conn,
const char *  dumpdirpath,
const char *  outfile 
)
static

Definition at line 1303 of file pg_restore.c.

1304{
1305 char global_file_path[MAXPGPATH];
1306 PGresult *result;
1307 StringInfoData sqlstatement,
1308 user_create;
1309 FILE *pfile;
1310 int n_errors = 0;
1311
1312 snprintf(global_file_path, MAXPGPATH, "%s/global.dat", dumpdirpath);
1313
1314 /* Open global.dat file. */
1315 pfile = fopen(global_file_path, PG_BINARY_R);
1316
1317 if (pfile == NULL)
1318 pg_fatal("could not open \"%s\": %m", global_file_path);
1319
1320 /*
1321 * If outfile is given, then just copy all global.dat file data into
1322 * outfile.
1323 */
1324 if (outfile)
1325 {
1327 return 0;
1328 }
1329
1330 /* Init sqlstatement to append commands. */
1331 initStringInfo(&sqlstatement);
1332
1333 /* creation statement for our current role */
1334 initStringInfo(&user_create);
1335 appendStringInfoString(&user_create, "CREATE ROLE ");
1336 /* should use fmtId here, but we don't know the encoding */
1337 appendStringInfoString(&user_create, PQuser(conn));
1338 appendStringInfoChar(&user_create, ';');
1339
1340 /* Process file till EOF and execute sql statements. */
1341 while (read_one_statement(&sqlstatement, pfile) != EOF)
1342 {
1343 /* don't try to create the role we are connected as */
1344 if (strstr(sqlstatement.data, user_create.data))
1345 continue;
1346
1347 pg_log_info("executing query: %s", sqlstatement.data);
1348 result = PQexec(conn, sqlstatement.data);
1349
1350 switch (PQresultStatus(result))
1351 {
1352 case PGRES_COMMAND_OK:
1353 case PGRES_TUPLES_OK:
1354 case PGRES_EMPTY_QUERY:
1355 break;
1356 default:
1357 n_errors++;
1358 pg_log_error("could not execute query: \"%s\" \nCommand was: \"%s\"", PQerrorMessage(conn), sqlstatement.data);
1359 }
1360 PQclear(result);
1361 }
1362
1363 /* Print a summary of ignored errors during global.dat. */
1364 if (n_errors)
1365 pg_log_warning("ignored %d errors in \"%s\"", n_errors, global_file_path);
1366
1367 fclose(pfile);
1368
1369 return n_errors;
1370}
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7463
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7619
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:124
static void copy_or_print_global_file(const char *outfile, FILE *pfile)
Definition: pg_restore.c:1379
static int read_one_statement(StringInfo inBuf, FILE *pfile)
Definition: pg_restore.c:863
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242

References appendStringInfoChar(), appendStringInfoString(), conn, copy_or_print_global_file(), StringInfoData::data, initStringInfo(), MAXPGPATH, outfile, PG_BINARY_R, pg_fatal, pg_log_error, pg_log_info, pg_log_warning, PGRES_COMMAND_OK, PGRES_EMPTY_QUERY, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQresultStatus(), PQuser(), read_one_statement(), and snprintf.

Referenced by main(), and restore_all_databases().

◆ read_one_statement()

static int read_one_statement ( StringInfo  inBuf,
FILE *  pfile 
)
static

Definition at line 863 of file pg_restore.c.

864{
865 int c; /* character read from getc() */
866 int m;
867
869
870 initStringInfo(&q);
871
872 resetStringInfo(inBuf);
873
874 /*
875 * Read characters until EOF or the appropriate delimiter is seen.
876 */
877 while ((c = fgetc(pfile)) != EOF)
878 {
879 if (c != '\'' && c != '"' && c != '\n' && c != ';')
880 {
881 appendStringInfoChar(inBuf, (char) c);
882 while ((c = fgetc(pfile)) != EOF)
883 {
884 if (c != '\'' && c != '"' && c != ';' && c != '\n')
885 appendStringInfoChar(inBuf, (char) c);
886 else
887 break;
888 }
889 }
890
891 if (c == '\'' || c == '"')
892 {
893 appendStringInfoChar(&q, (char) c);
894 m = c;
895
896 while ((c = fgetc(pfile)) != EOF)
897 {
898 appendStringInfoChar(&q, (char) c);
899
900 if (c == m)
901 {
903 resetStringInfo(&q);
904 break;
905 }
906 }
907 }
908
909 if (c == ';')
910 {
911 appendStringInfoChar(inBuf, (char) ';');
912 break;
913 }
914
915 if (c == '\n')
916 appendStringInfoChar(inBuf, (char) '\n');
917 }
918
919 pg_free(q.data);
920
921 /* No input before EOF signal means time to quit. */
922 if (c == EOF && inBuf->len == 0)
923 return EOF;
924
925 /* return something that's not EOF */
926 return 'Q';
927}
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:126

References appendStringInfoChar(), appendStringInfoString(), StringInfoData::data, initStringInfo(), StringInfoData::len, pg_free(), and resetStringInfo().

Referenced by process_global_sql_commands().

◆ read_restore_filters()

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

Definition at line 744 of file pg_restore.c.

745{
746 FilterStateData fstate;
747 char *objname;
748 FilterCommandType comtype;
749 FilterObjectType objtype;
750
752
753 while (filter_read_item(&fstate, &objname, &comtype, &objtype))
754 {
755 if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
756 {
757 switch (objtype)
758 {
760 break;
767 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
768 "include",
769 filter_object_type_name(objtype));
770 exit_nicely(1);
771
773 opts->selTypes = 1;
774 opts->selFunction = 1;
775 simple_string_list_append(&opts->functionNames, objname);
776 break;
778 opts->selTypes = 1;
779 opts->selIndex = 1;
780 simple_string_list_append(&opts->indexNames, objname);
781 break;
783 simple_string_list_append(&opts->schemaNames, objname);
784 break;
786 opts->selTypes = 1;
787 opts->selTable = 1;
788 simple_string_list_append(&opts->tableNames, objname);
789 break;
791 opts->selTypes = 1;
792 opts->selTrigger = 1;
793 simple_string_list_append(&opts->triggerNames, objname);
794 break;
795 }
796 }
797 else if (comtype == FILTER_COMMAND_TYPE_EXCLUDE)
798 {
799 switch (objtype)
800 {
802 break;
813 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
814 "exclude",
815 filter_object_type_name(objtype));
816 exit_nicely(1);
817
819 simple_string_list_append(&opts->schemaExcludeNames, objname);
820 break;
821 }
822 }
823 else
824 {
827 }
828
829 if (objname)
830 free(objname);
831 }
832
833 filter_free(&fstate);
834}
#define _(x)
Definition: elog.c:91
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:389
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
Assert(PointerIsAligned(start, uint64))
#define free(a)
Definition: header.h:65

References _, Assert(), exit_nicely(), 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 ( PGconn conn,
const char *  dumpdirpath,
SimpleStringList  db_exclude_patterns,
RestoreOptions opts,
int  numWorkers 
)
static

Definition at line 1122 of file pg_restore.c.

1125{
1126 SimplePtrList dbname_oid_list = {NULL, NULL};
1127 int num_db_restore = 0;
1128 int num_total_db;
1129 int n_errors_total;
1130 int count = 0;
1131 char *connected_db = NULL;
1132 bool dumpData = opts->dumpData;
1133 bool dumpSchema = opts->dumpSchema;
1134 bool dumpStatistics = opts->dumpSchema;
1135
1136 /* Save db name to reuse it for all the database. */
1137 if (opts->cparams.dbname)
1138 connected_db = opts->cparams.dbname;
1139
1140 num_total_db = get_dbname_oid_list_from_mfile(dumpdirpath, &dbname_oid_list);
1141
1142 /* If map.dat has no entries, return after processing global.dat */
1143 if (dbname_oid_list.head == NULL)
1144 return process_global_sql_commands(conn, dumpdirpath, opts->filename);
1145
1146 pg_log_info("found %d database names in \"map.dat\"", num_total_db);
1147
1148 if (!conn)
1149 {
1150 pg_log_info("trying to connect database \"postgres\"");
1151
1152 conn = ConnectDatabase("postgres", NULL, opts->cparams.pghost,
1153 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1154 false, progname, NULL, NULL, NULL, NULL);
1155
1156 /* Try with template1. */
1157 if (!conn)
1158 {
1159 pg_log_info("trying to connect database \"template1\"");
1160
1161 conn = ConnectDatabase("template1", NULL, opts->cparams.pghost,
1162 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1163 false, progname, NULL, NULL, NULL, NULL);
1164 }
1165 }
1166
1167 /*
1168 * filter the db list according to the exclude patterns
1169 */
1170 num_db_restore = get_dbnames_list_to_restore(conn, &dbname_oid_list,
1171 db_exclude_patterns);
1172
1173 /* Open global.dat file and execute/append all the global sql commands. */
1174 n_errors_total = process_global_sql_commands(conn, dumpdirpath, opts->filename);
1175
1176 /* Close the db connection as we are done with globals and patterns. */
1177 if (conn)
1178 PQfinish(conn);
1179
1180 /* Exit if no db needs to be restored. */
1181 if (dbname_oid_list.head == NULL || num_db_restore == 0)
1182 {
1183 pg_log_info("no database needs to restore out of %d databases", num_total_db);
1184 return n_errors_total;
1185 }
1186
1187 pg_log_info("need to restore %d databases out of %d databases", num_db_restore, num_total_db);
1188
1189 /*
1190 * We have a list of databases to restore after processing the
1191 * exclude-database switch(es). Now we can restore them one by one.
1192 */
1193 for (SimplePtrListCell *db_cell = dbname_oid_list.head;
1194 db_cell; db_cell = db_cell->next)
1195 {
1196 DbOidName *dbidname = (DbOidName *) db_cell->ptr;
1197 char subdirpath[MAXPGPATH];
1198 char subdirdbpath[MAXPGPATH];
1199 char dbfilename[MAXPGPATH];
1200 int n_errors;
1201
1202 /* ignore dbs marked for skipping */
1203 if (dbidname->oid == InvalidOid)
1204 continue;
1205
1206 /*
1207 * We need to reset override_dbname so that objects can be restored
1208 * into an already created database. (used with -d/--dbname option)
1209 */
1210 if (opts->cparams.override_dbname)
1211 {
1212 pfree(opts->cparams.override_dbname);
1213 opts->cparams.override_dbname = NULL;
1214 }
1215
1216 snprintf(subdirdbpath, MAXPGPATH, "%s/databases", dumpdirpath);
1217
1218 /*
1219 * Look for the database dump file/dir. If there is an {oid}.tar or
1220 * {oid}.dmp file, use it. Otherwise try to use a directory called
1221 * {oid}
1222 */
1223 snprintf(dbfilename, MAXPGPATH, "%u.tar", dbidname->oid);
1224 if (file_exists_in_directory(subdirdbpath, dbfilename))
1225 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.tar", dumpdirpath, dbidname->oid);
1226 else
1227 {
1228 snprintf(dbfilename, MAXPGPATH, "%u.dmp", dbidname->oid);
1229
1230 if (file_exists_in_directory(subdirdbpath, dbfilename))
1231 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u.dmp", dumpdirpath, dbidname->oid);
1232 else
1233 snprintf(subdirpath, MAXPGPATH, "%s/databases/%u", dumpdirpath, dbidname->oid);
1234 }
1235
1236 pg_log_info("restoring database \"%s\"", dbidname->str);
1237
1238 /* If database is already created, then don't set createDB flag. */
1239 if (opts->cparams.dbname)
1240 {
1241 PGconn *test_conn;
1242
1243 test_conn = ConnectDatabase(dbidname->str, NULL, opts->cparams.pghost,
1244 opts->cparams.pgport, opts->cparams.username, TRI_DEFAULT,
1245 false, progname, NULL, NULL, NULL, NULL);
1246 if (test_conn)
1247 {
1248 PQfinish(test_conn);
1249
1250 /* Use already created database for connection. */
1251 opts->createDB = 0;
1252 opts->cparams.dbname = dbidname->str;
1253 }
1254 else
1255 {
1256 /* we'll have to create it */
1257 opts->createDB = 1;
1258 opts->cparams.dbname = connected_db;
1259 }
1260 }
1261
1262 /*
1263 * Reset flags - might have been reset in pg_backup_archiver.c by the
1264 * previous restore.
1265 */
1266 opts->dumpData = dumpData;
1267 opts->dumpSchema = dumpSchema;
1268 opts->dumpStatistics = dumpStatistics;
1269
1270 /* Restore the single database. */
1271 n_errors = restore_one_database(subdirpath, opts, numWorkers, true, count);
1272
1273 /* Print a summary of ignored errors during single database restore. */
1274 if (n_errors)
1275 {
1276 n_errors_total += n_errors;
1277 pg_log_warning("errors ignored on database \"%s\" restore: %d", dbidname->str, n_errors);
1278 }
1279
1280 count++;
1281 }
1282
1283 /* Log number of processed databases. */
1284 pg_log_info("number of restored databases is %d", num_db_restore);
1285
1286 /* Free dbname and dboid list. */
1287 simple_ptr_list_destroy(&dbname_oid_list);
1288
1289 return n_errors_total;
1290}
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void pfree(void *pointer)
Definition: mcxt.c:2150
static int get_dbnames_list_to_restore(PGconn *conn, SimplePtrList *dbname_oid_list, SimpleStringList db_exclude_patterns)
Definition: pg_restore.c:939
static int get_dbname_oid_list_from_mfile(const char *dumpdirpath, SimplePtrList *dbname_oid_list)
Definition: pg_restore.c:1038
void simple_ptr_list_destroy(SimplePtrList *list)
Definition: simple_list.c:181

References conn, ConnectDatabase(), file_exists_in_directory(), get_dbname_oid_list_from_mfile(), get_dbnames_list_to_restore(), SimplePtrList::head, if(), InvalidOid, MAXPGPATH, SimplePtrListCell::next, DbOidName::oid, opts, pfree(), pg_log_info, pg_log_warning, PQfinish(), process_global_sql_commands(), progname, restore_one_database(), simple_ptr_list_destroy(), snprintf, DbOidName::str, and TRI_DEFAULT.

Referenced by main().

◆ restore_one_database()

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

Definition at line 601 of file pg_restore.c.

603{
604 Archive *AH;
605 int n_errors;
606
607 AH = OpenArchive(inputFileSpec, opts->format);
608
609 SetArchiveOptions(AH, NULL, opts);
610
611 /*
612 * We don't have a connection yet but that doesn't matter. The connection
613 * is initialized to NULL and if we terminate through exit_nicely() while
614 * it's still NULL, the cleanup function will just be a no-op. If we are
615 * restoring multiple databases, then only update AX handle for cleanup as
616 * the previous entry was already in the array and we had closed previous
617 * connection, so we can use the same array slot.
618 */
619 if (!append_data || num == 0)
621 else
623
624 /* Let the archiver know how noisy to be */
625 AH->verbose = opts->verbose;
626
627 /*
628 * Whether to keep submitting sql commands as "pg_restore ... | psql ... "
629 */
630 AH->exit_on_error = opts->exit_on_error;
631
632 if (opts->tocFile)
633 SortTocFromFile(AH);
634
635 AH->numWorkers = numWorkers;
636
637 if (opts->tocSummary)
638 PrintTOCSummary(AH);
639 else
640 {
643 }
644
645 n_errors = AH->n_errors;
646
647 /* AH may be freed in CloseArchive? */
648 CloseArchive(AH);
649
650 return n_errors;
651}
void replace_on_exit_close_archive(Archive *AHX)
Definition: parallel.c:341
void on_exit_close_archive(Archive *AHX)
Definition: parallel.c:330
static size_t append_data(char *buf, size_t size, size_t nmemb, void *userdata)
void ProcessArchiveRestoreOptions(Archive *AHX)
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)
bool exit_on_error
Definition: pg_backup.h:247
int n_errors
Definition: pg_backup.h:248
int numWorkers
Definition: pg_backup.h:235
int verbose
Definition: pg_backup.h:227

References append_data(), CloseArchive(), Archive::exit_on_error, 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 654 of file pg_restore.c.

655{
656 printf(_("%s restores a PostgreSQL database from an archive created by pg_dump or pg_dumpall.\n"
657 "If the archive is created by pg_dumpall, then restores multiple databases also.\n\n"), progname);
658 printf(_("Usage:\n"));
659 printf(_(" %s [OPTION]... [FILE]\n"), progname);
660
661 printf(_("\nGeneral options:\n"));
662 printf(_(" -d, --dbname=NAME connect to database name\n"));
663 printf(_(" -f, --file=FILENAME output file name (- for stdout)\n"));
664 printf(_(" -F, --format=c|d|t backup file format (should be automatic)\n"));
665 printf(_(" -l, --list print summarized TOC of the archive\n"));
666 printf(_(" -v, --verbose verbose mode\n"));
667 printf(_(" -V, --version output version information, then exit\n"));
668 printf(_(" -?, --help show this help, then exit\n"));
669
670 printf(_("\nOptions controlling the restore:\n"));
671 printf(_(" -a, --data-only restore only the data, no schema\n"));
672 printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
673 printf(_(" -C, --create create the target database\n"));
674 printf(_(" -e, --exit-on-error exit on error, default is to continue\n"));
675 printf(_(" -g, --globals-only restore only global objects, no databases\n"));
676 printf(_(" -I, --index=NAME restore named index\n"));
677 printf(_(" -j, --jobs=NUM use this many parallel jobs to restore\n"));
678 printf(_(" -L, --use-list=FILENAME use table of contents from this file for\n"
679 " selecting/ordering output\n"));
680 printf(_(" -n, --schema=NAME restore only objects in this schema\n"));
681 printf(_(" -N, --exclude-schema=NAME do not restore objects in this schema\n"));
682 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
683 printf(_(" -P, --function=NAME(args) restore named function\n"));
684 printf(_(" -s, --schema-only restore only the schema, no data\n"));
685 printf(_(" -S, --superuser=NAME superuser user name to use for disabling triggers\n"));
686 printf(_(" -t, --table=NAME restore named relation (table, view, etc.)\n"));
687 printf(_(" -T, --trigger=NAME restore named trigger\n"));
688 printf(_(" --exclude-database=PATTERN exclude databases whose name matches with pattern\n"));
689 printf(_(" -x, --no-privileges skip restoration of access privileges (grant/revoke)\n"));
690 printf(_(" -1, --single-transaction restore as a single transaction\n"));
691 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
692 printf(_(" --enable-row-security enable row security\n"));
693 printf(_(" --filter=FILENAME restore or skip objects based on expressions\n"
694 " in FILENAME\n"));
695 printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
696 printf(_(" --no-comments do not restore comment commands\n"));
697 printf(_(" --no-data do not restore data\n"));
698 printf(_(" --no-data-for-failed-tables do not restore data of tables that could not be\n"
699 " created\n"));
700 printf(_(" --no-policies do not restore row security policies\n"));
701 printf(_(" --no-publications do not restore publications\n"));
702 printf(_(" --no-schema do not restore schema\n"));
703 printf(_(" --no-security-labels do not restore security labels\n"));
704 printf(_(" --no-statistics do not restore statistics\n"));
705 printf(_(" --no-subscriptions do not restore subscriptions\n"));
706 printf(_(" --no-table-access-method do not restore table access methods\n"));
707 printf(_(" --no-tablespaces do not restore tablespace assignments\n"));
708 printf(_(" --section=SECTION restore named section (pre-data, data, or post-data)\n"));
709 printf(_(" --statistics-only restore only the statistics, not schema or data\n"));
710 printf(_(" --strict-names require table and/or schema include patterns to\n"
711 " match at least one entity each\n"));
712 printf(_(" --transaction-size=N commit after every N objects\n"));
713 printf(_(" --use-set-session-authorization\n"
714 " use SET SESSION AUTHORIZATION commands instead of\n"
715 " ALTER OWNER commands to set ownership\n"));
716 printf(_(" --with-data dump the data\n"));
717 printf(_(" --with-schema dump the schema\n"));
718 printf(_(" --with-statistics dump the statistics\n"));
719
720 printf(_("\nConnection options:\n"));
721 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
722 printf(_(" -p, --port=PORT database server port number\n"));
723 printf(_(" -U, --username=NAME connect as specified database user\n"));
724 printf(_(" -w, --no-password never prompt for password\n"));
725 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
726 printf(_(" --role=ROLENAME do SET ROLE before restore\n"));
727
728 printf(_("\n"
729 "The options -I, -n, -N, -P, -t, -T, --section, and --exclude-database can be combined\n"
730 "and specified multiple times to select multiple objects.\n"));
731 printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
732 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
733 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
734}
#define printf(...)
Definition: port.h:245

References _, printf, and progname.

Referenced by main().