PostgreSQL Source Code  git master
pg_dump.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pg_dump.c
4  * pg_dump is a utility for dumping out a postgres database
5  * into a script file.
6  *
7  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * pg_dump will read the system catalogs in a database and dump out a
11  * script that reproduces the schema in terms of SQL that is understood
12  * by PostgreSQL
13  *
14  * Note that pg_dump runs in a transaction-snapshot mode transaction,
15  * so it sees a consistent snapshot of the database including system
16  * catalogs. However, it relies in part on various specialized backend
17  * functions like pg_get_indexdef(), and those things tend to look at
18  * the currently committed state. So it is possible to get 'cache
19  * lookup failed' error if someone performs DDL changes while a dump is
20  * happening. The window for this sort of thing is from the acquisition
21  * of the transaction snapshot to getSchemaData() (when pg_dump acquires
22  * AccessShareLock on every table it intends to dump). It isn't very large,
23  * but it can happen.
24  *
25  * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
26  *
27  * IDENTIFICATION
28  * src/bin/pg_dump/pg_dump.c
29  *
30  *-------------------------------------------------------------------------
31  */
32 #include "postgres_fe.h"
33 
34 #include <unistd.h>
35 #include <ctype.h>
36 #ifdef HAVE_TERMIOS_H
37 #include <termios.h>
38 #endif
39 
40 #include "getopt_long.h"
41 
42 #include "access/attnum.h"
43 #include "access/sysattr.h"
44 #include "access/transam.h"
45 #include "catalog/pg_aggregate_d.h"
46 #include "catalog/pg_am_d.h"
47 #include "catalog/pg_attribute_d.h"
48 #include "catalog/pg_cast_d.h"
49 #include "catalog/pg_class_d.h"
50 #include "catalog/pg_default_acl_d.h"
51 #include "catalog/pg_largeobject_d.h"
52 #include "catalog/pg_largeobject_metadata_d.h"
53 #include "catalog/pg_proc_d.h"
54 #include "catalog/pg_trigger_d.h"
55 #include "catalog/pg_type_d.h"
56 #include "libpq/libpq-fs.h"
57 #include "storage/block.h"
58 
59 #include "dumputils.h"
60 #include "parallel.h"
61 #include "pg_backup_db.h"
62 #include "pg_backup_utils.h"
63 #include "pg_dump.h"
64 #include "fe_utils/connect.h"
65 #include "fe_utils/string_utils.h"
66 
67 
68 typedef struct
69 {
70  const char *descr; /* comment for an object */
71  Oid classoid; /* object class (catalog OID) */
72  Oid objoid; /* object OID */
73  int objsubid; /* subobject (table column #) */
74 } CommentItem;
75 
76 typedef struct
77 {
78  const char *provider; /* label provider of this security label */
79  const char *label; /* security label for an object */
80  Oid classoid; /* object class (catalog OID) */
81  Oid objoid; /* object OID */
82  int objsubid; /* subobject (table column #) */
83 } SecLabelItem;
84 
85 typedef enum OidOptions
86 {
88  zeroAsAny = 2,
91 } OidOptions;
92 
93 /* global decls */
94 bool g_verbose; /* User wants verbose narration of our
95  * activities. */
96 static bool dosync = true; /* Issue fsync() to make dump durable on disk. */
97 
98 /* subquery used to convert user ID (eg, datdba) to user name */
99 static const char *username_subquery;
100 
101 /*
102  * For 8.0 and earlier servers, pulled from pg_database, for 8.1+ we use
103  * FirstNormalObjectId - 1.
104  */
105 static Oid g_last_builtin_oid; /* value of the last builtin oid */
106 
107 /* The specified names/patterns should to match at least one entity */
108 static int strict_names = 0;
109 
110 /*
111  * Object inclusion/exclusion lists
112  *
113  * The string lists record the patterns given by command-line switches,
114  * which we then convert to lists of OIDs of matching objects.
115  */
117 static SimpleOidList schema_include_oids = {NULL, NULL};
119 static SimpleOidList schema_exclude_oids = {NULL, NULL};
120 
122 static SimpleOidList table_include_oids = {NULL, NULL};
124 static SimpleOidList table_exclude_oids = {NULL, NULL};
126 static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
127 
128 
129 char g_opaque_type[10]; /* name for the opaque type */
130 
131 /* placeholders for the delimiters for comments */
133 char g_comment_end[10];
134 
135 static const CatalogId nilCatalogId = {0, 0};
136 
137 /*
138  * Macro for producing quoted, schema-qualified name of a dumpable object.
139  */
140 #define fmtQualifiedDumpable(obj) \
141  fmtQualifiedId((obj)->dobj.namespace->dobj.name, \
142  (obj)->dobj.name)
143 
144 static void help(const char *progname);
145 static void setup_connection(Archive *AH,
146  const char *dumpencoding, const char *dumpsnapshot,
147  char *use_role);
148 static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
149 static void expand_schema_name_patterns(Archive *fout,
150  SimpleStringList *patterns,
151  SimpleOidList *oids,
152  bool strict_names);
153 static void expand_table_name_patterns(Archive *fout,
154  SimpleStringList *patterns,
155  SimpleOidList *oids,
156  bool strict_names);
157 static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid);
158 static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
159 static void refreshMatViewData(Archive *fout, TableDataInfo *tdinfo);
160 static void guessConstraintInheritance(TableInfo *tblinfo, int numTables);
161 static void dumpComment(Archive *fout, const char *type, const char *name,
162  const char *namespace, const char *owner,
163  CatalogId catalogId, int subid, DumpId dumpId);
164 static int findComments(Archive *fout, Oid classoid, Oid objoid,
165  CommentItem **items);
166 static int collectComments(Archive *fout, CommentItem **items);
167 static void dumpSecLabel(Archive *fout, const char *type, const char *name,
168  const char *namespace, const char *owner,
169  CatalogId catalogId, int subid, DumpId dumpId);
170 static int findSecLabels(Archive *fout, Oid classoid, Oid objoid,
171  SecLabelItem **items);
172 static int collectSecLabels(Archive *fout, SecLabelItem **items);
173 static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
174 static void dumpNamespace(Archive *fout, NamespaceInfo *nspinfo);
175 static void dumpExtension(Archive *fout, ExtensionInfo *extinfo);
176 static void dumpType(Archive *fout, TypeInfo *tyinfo);
177 static void dumpBaseType(Archive *fout, TypeInfo *tyinfo);
178 static void dumpEnumType(Archive *fout, TypeInfo *tyinfo);
179 static void dumpRangeType(Archive *fout, TypeInfo *tyinfo);
180 static void dumpUndefinedType(Archive *fout, TypeInfo *tyinfo);
181 static void dumpDomain(Archive *fout, TypeInfo *tyinfo);
182 static void dumpCompositeType(Archive *fout, TypeInfo *tyinfo);
183 static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo);
184 static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
185 static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
186 static void dumpFunc(Archive *fout, FuncInfo *finfo);
187 static void dumpCast(Archive *fout, CastInfo *cast);
188 static void dumpTransform(Archive *fout, TransformInfo *transform);
189 static void dumpOpr(Archive *fout, OprInfo *oprinfo);
190 static void dumpAccessMethod(Archive *fout, AccessMethodInfo *oprinfo);
191 static void dumpOpclass(Archive *fout, OpclassInfo *opcinfo);
192 static void dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo);
193 static void dumpCollation(Archive *fout, CollInfo *collinfo);
194 static void dumpConversion(Archive *fout, ConvInfo *convinfo);
195 static void dumpRule(Archive *fout, RuleInfo *rinfo);
196 static void dumpAgg(Archive *fout, AggInfo *agginfo);
197 static void dumpTrigger(Archive *fout, TriggerInfo *tginfo);
198 static void dumpEventTrigger(Archive *fout, EventTriggerInfo *evtinfo);
199 static void dumpTable(Archive *fout, TableInfo *tbinfo);
200 static void dumpTableSchema(Archive *fout, TableInfo *tbinfo);
201 static void dumpAttrDef(Archive *fout, AttrDefInfo *adinfo);
202 static void dumpSequence(Archive *fout, TableInfo *tbinfo);
203 static void dumpSequenceData(Archive *fout, TableDataInfo *tdinfo);
204 static void dumpIndex(Archive *fout, IndxInfo *indxinfo);
205 static void dumpIndexAttach(Archive *fout, IndexAttachInfo *attachinfo);
206 static void dumpStatisticsExt(Archive *fout, StatsExtInfo *statsextinfo);
207 static void dumpConstraint(Archive *fout, ConstraintInfo *coninfo);
208 static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo);
209 static void dumpTSParser(Archive *fout, TSParserInfo *prsinfo);
210 static void dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo);
211 static void dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo);
212 static void dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo);
213 static void dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo);
214 static void dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo);
215 static void dumpUserMappings(Archive *fout,
216  const char *servername, const char *namespace,
217  const char *owner, CatalogId catalogId, DumpId dumpId);
218 static void dumpDefaultACL(Archive *fout, DefaultACLInfo *daclinfo);
219 
220 static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
221  const char *type, const char *name, const char *subname,
222  const char *nspname, const char *owner,
223  const char *acls, const char *racls,
224  const char *initacls, const char *initracls);
225 
226 static void getDependencies(Archive *fout);
227 static void BuildArchiveDependencies(Archive *fout);
229  DumpId **dependencies, int *nDeps, int *allocDeps);
230 
232 static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
233  DumpableObject *boundaryObjs);
234 
235 static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
236 static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind);
237 static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids);
238 static void buildMatViewRefreshDependencies(Archive *fout);
239 static void getTableDataFKConstraints(void);
240 static char *format_function_arguments(FuncInfo *finfo, char *funcargs,
241  bool is_agg);
242 static char *format_function_arguments_old(Archive *fout,
243  FuncInfo *finfo, int nallargs,
244  char **allargtypes,
245  char **argmodes,
246  char **argnames);
247 static char *format_function_signature(Archive *fout,
248  FuncInfo *finfo, bool honor_quotes);
249 static char *convertRegProcReference(Archive *fout,
250  const char *proc);
251 static char *getFormattedOperatorName(Archive *fout, const char *oproid);
252 static char *convertTSFunction(Archive *fout, Oid funcOid);
253 static Oid findLastBuiltinOid_V71(Archive *fout);
254 static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
255 static void getBlobs(Archive *fout);
256 static void dumpBlob(Archive *fout, BlobInfo *binfo);
257 static int dumpBlobs(Archive *fout, void *arg);
258 static void dumpPolicy(Archive *fout, PolicyInfo *polinfo);
259 static void dumpPublication(Archive *fout, PublicationInfo *pubinfo);
260 static void dumpPublicationTable(Archive *fout, PublicationRelInfo *pubrinfo);
261 static void dumpSubscription(Archive *fout, SubscriptionInfo *subinfo);
262 static void dumpDatabase(Archive *AH);
263 static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
264  const char *dbname, Oid dboid);
265 static void dumpEncoding(Archive *AH);
266 static void dumpStdStrings(Archive *AH);
267 static void dumpSearchPath(Archive *AH);
269  PQExpBuffer upgrade_buffer,
270  Oid pg_type_oid,
271  bool force_array_type);
273  PQExpBuffer upgrade_buffer, Oid pg_rel_oid);
274 static void binary_upgrade_set_pg_class_oids(Archive *fout,
275  PQExpBuffer upgrade_buffer,
276  Oid pg_class_oid, bool is_index);
277 static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
278  DumpableObject *dobj,
279  const char *objtype,
280  const char *objname,
281  const char *objnamespace);
282 static const char *getAttrName(int attrnum, TableInfo *tblInfo);
283 static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
284 static bool nonemptyReloptions(const char *reloptions);
285 static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
286  const char *prefix, Archive *fout);
287 static char *get_synchronized_snapshot(Archive *fout);
288 static void setupDumpWorker(Archive *AHX);
289 static TableInfo *getRootTableInfo(TableInfo *tbinfo);
290 
291 
292 int
293 main(int argc, char **argv)
294 {
295  int c;
296  const char *filename = NULL;
297  const char *format = "p";
298  TableInfo *tblinfo;
299  int numTables;
300  DumpableObject **dobjs;
301  int numObjs;
302  DumpableObject *boundaryObjs;
303  int i;
304  int optindex;
305  RestoreOptions *ropt;
306  Archive *fout; /* the script file */
307  const char *dumpencoding = NULL;
308  const char *dumpsnapshot = NULL;
309  char *use_role = NULL;
310  int numWorkers = 1;
311  trivalue prompt_password = TRI_DEFAULT;
312  int compressLevel = -1;
313  int plainText = 0;
314  ArchiveFormat archiveFormat = archUnknown;
315  ArchiveMode archiveMode;
316 
317  static DumpOptions dopt;
318 
319  static struct option long_options[] = {
320  {"data-only", no_argument, NULL, 'a'},
321  {"blobs", no_argument, NULL, 'b'},
322  {"no-blobs", no_argument, NULL, 'B'},
323  {"clean", no_argument, NULL, 'c'},
324  {"create", no_argument, NULL, 'C'},
325  {"dbname", required_argument, NULL, 'd'},
326  {"file", required_argument, NULL, 'f'},
327  {"format", required_argument, NULL, 'F'},
328  {"host", required_argument, NULL, 'h'},
329  {"jobs", 1, NULL, 'j'},
330  {"no-reconnect", no_argument, NULL, 'R'},
331  {"oids", no_argument, NULL, 'o'},
332  {"no-owner", no_argument, NULL, 'O'},
333  {"port", required_argument, NULL, 'p'},
334  {"schema", required_argument, NULL, 'n'},
335  {"exclude-schema", required_argument, NULL, 'N'},
336  {"schema-only", no_argument, NULL, 's'},
337  {"superuser", required_argument, NULL, 'S'},
338  {"table", required_argument, NULL, 't'},
339  {"exclude-table", required_argument, NULL, 'T'},
340  {"no-password", no_argument, NULL, 'w'},
341  {"password", no_argument, NULL, 'W'},
342  {"username", required_argument, NULL, 'U'},
343  {"verbose", no_argument, NULL, 'v'},
344  {"no-privileges", no_argument, NULL, 'x'},
345  {"no-acl", no_argument, NULL, 'x'},
346  {"compress", required_argument, NULL, 'Z'},
347  {"encoding", required_argument, NULL, 'E'},
348  {"help", no_argument, NULL, '?'},
349  {"version", no_argument, NULL, 'V'},
350 
351  /*
352  * the following options don't have an equivalent short option letter
353  */
354  {"attribute-inserts", no_argument, &dopt.column_inserts, 1},
355  {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1},
356  {"column-inserts", no_argument, &dopt.column_inserts, 1},
357  {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1},
358  {"disable-triggers", no_argument, &dopt.disable_triggers, 1},
359  {"enable-row-security", no_argument, &dopt.enable_row_security, 1},
360  {"exclude-table-data", required_argument, NULL, 4},
361  {"if-exists", no_argument, &dopt.if_exists, 1},
362  {"inserts", no_argument, &dopt.dump_inserts, 1},
363  {"lock-wait-timeout", required_argument, NULL, 2},
364  {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
365  {"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
366  {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
367  {"role", required_argument, NULL, 3},
368  {"section", required_argument, NULL, 5},
369  {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
370  {"snapshot", required_argument, NULL, 6},
371  {"strict-names", no_argument, &strict_names, 1},
372  {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1},
373  {"no-comments", no_argument, &dopt.no_comments, 1},
374  {"no-publications", no_argument, &dopt.no_publications, 1},
375  {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
376  {"no-synchronized-snapshots", no_argument, &dopt.no_synchronized_snapshots, 1},
377  {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
378  {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
379  {"no-sync", no_argument, NULL, 7},
380  {"on-conflict-do-nothing", no_argument, &dopt.do_nothing, 1},
381 
382  {NULL, 0, NULL, 0}
383  };
384 
385  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
386 
387  /*
388  * Initialize what we need for parallel execution, especially for thread
389  * support on Windows.
390  */
392 
393  g_verbose = false;
394 
395  strcpy(g_comment_start, "-- ");
396  g_comment_end[0] = '\0';
397  strcpy(g_opaque_type, "opaque");
398 
399  progname = get_progname(argv[0]);
400 
401  if (argc > 1)
402  {
403  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
404  {
405  help(progname);
406  exit_nicely(0);
407  }
408  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
409  {
410  puts("pg_dump (PostgreSQL) " PG_VERSION);
411  exit_nicely(0);
412  }
413  }
414 
415  InitDumpOptions(&dopt);
416 
417  while ((c = getopt_long(argc, argv, "abBcCd:E:f:F:h:j:n:N:oOp:RsS:t:T:U:vwWxZ:",
418  long_options, &optindex)) != -1)
419  {
420  switch (c)
421  {
422  case 'a': /* Dump data only */
423  dopt.dataOnly = true;
424  break;
425 
426  case 'b': /* Dump blobs */
427  dopt.outputBlobs = true;
428  break;
429 
430  case 'B': /* Don't dump blobs */
431  dopt.dontOutputBlobs = true;
432  break;
433 
434  case 'c': /* clean (i.e., drop) schema prior to create */
435  dopt.outputClean = 1;
436  break;
437 
438  case 'C': /* Create DB */
439  dopt.outputCreateDB = 1;
440  break;
441 
442  case 'd': /* database name */
443  dopt.dbname = pg_strdup(optarg);
444  break;
445 
446  case 'E': /* Dump encoding */
447  dumpencoding = pg_strdup(optarg);
448  break;
449 
450  case 'f':
451  filename = pg_strdup(optarg);
452  break;
453 
454  case 'F':
455  format = pg_strdup(optarg);
456  break;
457 
458  case 'h': /* server host */
459  dopt.pghost = pg_strdup(optarg);
460  break;
461 
462  case 'j': /* number of dump jobs */
463  numWorkers = atoi(optarg);
464  break;
465 
466  case 'n': /* include schema(s) */
467  simple_string_list_append(&schema_include_patterns, optarg);
468  dopt.include_everything = false;
469  break;
470 
471  case 'N': /* exclude schema(s) */
472  simple_string_list_append(&schema_exclude_patterns, optarg);
473  break;
474 
475  case 'o': /* Dump oids */
476  dopt.oids = true;
477  break;
478 
479  case 'O': /* Don't reconnect to match owner */
480  dopt.outputNoOwner = 1;
481  break;
482 
483  case 'p': /* server port */
484  dopt.pgport = pg_strdup(optarg);
485  break;
486 
487  case 'R':
488  /* no-op, still accepted for backwards compatibility */
489  break;
490 
491  case 's': /* dump schema only */
492  dopt.schemaOnly = true;
493  break;
494 
495  case 'S': /* Username for superuser in plain text output */
497  break;
498 
499  case 't': /* include table(s) */
500  simple_string_list_append(&table_include_patterns, optarg);
501  dopt.include_everything = false;
502  break;
503 
504  case 'T': /* exclude table(s) */
505  simple_string_list_append(&table_exclude_patterns, optarg);
506  break;
507 
508  case 'U':
509  dopt.username = pg_strdup(optarg);
510  break;
511 
512  case 'v': /* verbose */
513  g_verbose = true;
514  break;
515 
516  case 'w':
517  prompt_password = TRI_NO;
518  break;
519 
520  case 'W':
521  prompt_password = TRI_YES;
522  break;
523 
524  case 'x': /* skip ACL dump */
525  dopt.aclsSkip = true;
526  break;
527 
528  case 'Z': /* Compression Level */
529  compressLevel = atoi(optarg);
530  if (compressLevel < 0 || compressLevel > 9)
531  {
532  write_msg(NULL, "compression level must be in range 0..9\n");
533  exit_nicely(1);
534  }
535  break;
536 
537  case 0:
538  /* This covers the long options. */
539  break;
540 
541  case 2: /* lock-wait-timeout */
543  break;
544 
545  case 3: /* SET ROLE */
546  use_role = pg_strdup(optarg);
547  break;
548 
549  case 4: /* exclude table(s) data */
550  simple_string_list_append(&tabledata_exclude_patterns, optarg);
551  break;
552 
553  case 5: /* section */
555  break;
556 
557  case 6: /* snapshot */
558  dumpsnapshot = pg_strdup(optarg);
559  break;
560 
561  case 7: /* no-sync */
562  dosync = false;
563  break;
564 
565  default:
566  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
567  exit_nicely(1);
568  }
569  }
570 
571  /*
572  * Non-option argument specifies database name as long as it wasn't
573  * already specified with -d / --dbname
574  */
575  if (optind < argc && dopt.dbname == NULL)
576  dopt.dbname = argv[optind++];
577 
578  /* Complain if any arguments remain */
579  if (optind < argc)
580  {
581  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
582  progname, argv[optind]);
583  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
584  progname);
585  exit_nicely(1);
586  }
587 
588  /* --column-inserts implies --inserts */
589  if (dopt.column_inserts)
590  dopt.dump_inserts = 1;
591 
592  /*
593  * Binary upgrade mode implies dumping sequence data even in schema-only
594  * mode. This is not exposed as a separate option, but kept separate
595  * internally for clarity.
596  */
597  if (dopt.binary_upgrade)
598  dopt.sequence_data = 1;
599 
600  if (dopt.dataOnly && dopt.schemaOnly)
601  {
602  write_msg(NULL, "options -s/--schema-only and -a/--data-only cannot be used together\n");
603  exit_nicely(1);
604  }
605 
606  if (dopt.dataOnly && dopt.outputClean)
607  {
608  write_msg(NULL, "options -c/--clean and -a/--data-only cannot be used together\n");
609  exit_nicely(1);
610  }
611 
612  if (dopt.dump_inserts && dopt.oids)
613  {
614  write_msg(NULL, "options --inserts/--column-inserts and -o/--oids cannot be used together\n");
615  write_msg(NULL, "(The INSERT command cannot set OIDs.)\n");
616  exit_nicely(1);
617  }
618 
619  if (dopt.if_exists && !dopt.outputClean)
620  exit_horribly(NULL, "option --if-exists requires option -c/--clean\n");
621 
622  if (dopt.do_nothing && !(dopt.dump_inserts || dopt.column_inserts))
623  exit_horribly(NULL, "option --on-conflict-do-nothing requires option --inserts or --column-inserts\n");
624 
625  /* Identify archive format to emit */
626  archiveFormat = parseArchiveFormat(format, &archiveMode);
627 
628  /* archiveFormat specific setup */
629  if (archiveFormat == archNull)
630  plainText = 1;
631 
632  /* Custom and directory formats are compressed by default, others not */
633  if (compressLevel == -1)
634  {
635 #ifdef HAVE_LIBZ
636  if (archiveFormat == archCustom || archiveFormat == archDirectory)
637  compressLevel = Z_DEFAULT_COMPRESSION;
638  else
639 #endif
640  compressLevel = 0;
641  }
642 
643 #ifndef HAVE_LIBZ
644  if (compressLevel != 0)
645  write_msg(NULL, "WARNING: requested compression not available in this "
646  "installation -- archive will be uncompressed\n");
647  compressLevel = 0;
648 #endif
649 
650  /*
651  * If emitting an archive format, we always want to emit a DATABASE item,
652  * in case --create is specified at pg_restore time.
653  */
654  if (!plainText)
655  dopt.outputCreateDB = 1;
656 
657  /*
658  * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually)
659  * parallel jobs because that's the maximum limit for the
660  * WaitForMultipleObjects() call.
661  */
662  if (numWorkers <= 0
663 #ifdef WIN32
664  || numWorkers > MAXIMUM_WAIT_OBJECTS
665 #endif
666  )
667  exit_horribly(NULL, "invalid number of parallel jobs\n");
668 
669  /* Parallel backup only in the directory archive format so far */
670  if (archiveFormat != archDirectory && numWorkers > 1)
671  exit_horribly(NULL, "parallel backup only supported by the directory format\n");
672 
673  /* Open the output file */
674  fout = CreateArchive(filename, archiveFormat, compressLevel, dosync,
675  archiveMode, setupDumpWorker);
676 
677  /* Make dump options accessible right away */
678  SetArchiveOptions(fout, &dopt, NULL);
679 
680  /* Register the cleanup hook */
681  on_exit_close_archive(fout);
682 
683  /* Let the archiver know how noisy to be */
684  fout->verbose = g_verbose;
685 
686  /*
687  * We allow the server to be back to 8.0, and up to any minor release of
688  * our own major version. (See also version check in pg_dumpall.c.)
689  */
690  fout->minRemoteVersion = 80000;
691  fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
692 
693  fout->numWorkers = numWorkers;
694 
695  /*
696  * Open the database using the Archiver, so it knows about it. Errors mean
697  * death.
698  */
699  ConnectDatabase(fout, dopt.dbname, dopt.pghost, dopt.pgport, dopt.username, prompt_password);
700  setup_connection(fout, dumpencoding, dumpsnapshot, use_role);
701 
702  /*
703  * Disable security label support if server version < v9.1.x (prevents
704  * access to nonexistent pg_seclabel catalog)
705  */
706  if (fout->remoteVersion < 90100)
707  dopt.no_security_labels = 1;
708 
709  /*
710  * On hot standbys, never try to dump unlogged table data, since it will
711  * just throw an error.
712  */
713  if (fout->isStandby)
714  dopt.no_unlogged_table_data = true;
715 
716  /* Select the appropriate subquery to convert user IDs to names */
717  if (fout->remoteVersion >= 80100)
718  username_subquery = "SELECT rolname FROM pg_catalog.pg_roles WHERE oid =";
719  else
720  username_subquery = "SELECT usename FROM pg_catalog.pg_user WHERE usesysid =";
721 
722  /* check the version for the synchronized snapshots feature */
723  if (numWorkers > 1 && fout->remoteVersion < 90200
724  && !dopt.no_synchronized_snapshots)
725  exit_horribly(NULL,
726  "Synchronized snapshots are not supported by this server version.\n"
727  "Run with --no-synchronized-snapshots instead if you do not need\n"
728  "synchronized snapshots.\n");
729 
730  /* check the version when a snapshot is explicitly specified by user */
731  if (dumpsnapshot && fout->remoteVersion < 90200)
732  exit_horribly(NULL,
733  "Exported snapshots are not supported by this server version.\n");
734 
735  /*
736  * Find the last built-in OID, if needed (prior to 8.1)
737  *
738  * With 8.1 and above, we can just use FirstNormalObjectId - 1.
739  */
740  if (fout->remoteVersion < 80100)
742  else
744 
745  if (g_verbose)
746  write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
747 
748  /* Expand schema selection patterns into OID lists */
749  if (schema_include_patterns.head != NULL)
750  {
751  expand_schema_name_patterns(fout, &schema_include_patterns,
752  &schema_include_oids,
753  strict_names);
754  if (schema_include_oids.head == NULL)
755  exit_horribly(NULL, "no matching schemas were found\n");
756  }
757  expand_schema_name_patterns(fout, &schema_exclude_patterns,
758  &schema_exclude_oids,
759  false);
760  /* non-matching exclusion patterns aren't an error */
761 
762  /* Expand table selection patterns into OID lists */
763  if (table_include_patterns.head != NULL)
764  {
765  expand_table_name_patterns(fout, &table_include_patterns,
766  &table_include_oids,
767  strict_names);
768  if (table_include_oids.head == NULL)
769  exit_horribly(NULL, "no matching tables were found\n");
770  }
771  expand_table_name_patterns(fout, &table_exclude_patterns,
772  &table_exclude_oids,
773  false);
774 
775  expand_table_name_patterns(fout, &tabledata_exclude_patterns,
776  &tabledata_exclude_oids,
777  false);
778 
779  /* non-matching exclusion patterns aren't an error */
780 
781  /*
782  * Dumping blobs is the default for dumps where an inclusion switch is not
783  * used (an "include everything" dump). -B can be used to exclude blobs
784  * from those dumps. -b can be used to include blobs even when an
785  * inclusion switch is used.
786  *
787  * -s means "schema only" and blobs are data, not schema, so we never
788  * include blobs when -s is used.
789  */
790  if (dopt.include_everything && !dopt.schemaOnly && !dopt.dontOutputBlobs)
791  dopt.outputBlobs = true;
792 
793  /*
794  * Now scan the database and create DumpableObject structs for all the
795  * objects we intend to dump.
796  */
797  tblinfo = getSchemaData(fout, &numTables);
798 
799  if (fout->remoteVersion < 80400)
800  guessConstraintInheritance(tblinfo, numTables);
801 
802  if (!dopt.schemaOnly)
803  {
804  getTableData(&dopt, tblinfo, numTables, dopt.oids, 0);
806  if (dopt.dataOnly)
808  }
809 
810  if (dopt.schemaOnly && dopt.sequence_data)
811  getTableData(&dopt, tblinfo, numTables, dopt.oids, RELKIND_SEQUENCE);
812 
813  /*
814  * In binary-upgrade mode, we do not have to worry about the actual blob
815  * data or the associated metadata that resides in the pg_largeobject and
816  * pg_largeobject_metadata tables, respectively.
817  *
818  * However, we do need to collect blob information as there may be
819  * comments or other information on blobs that we do need to dump out.
820  */
821  if (dopt.outputBlobs || dopt.binary_upgrade)
822  getBlobs(fout);
823 
824  /*
825  * Collect dependency data to assist in ordering the objects.
826  */
827  getDependencies(fout);
828 
829  /* Lastly, create dummy objects to represent the section boundaries */
830  boundaryObjs = createBoundaryObjects();
831 
832  /* Get pointers to all the known DumpableObjects */
833  getDumpableObjects(&dobjs, &numObjs);
834 
835  /*
836  * Add dummy dependencies to enforce the dump section ordering.
837  */
838  addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
839 
840  /*
841  * Sort the objects into a safe dump order (no forward references).
842  *
843  * We rely on dependency information to help us determine a safe order, so
844  * the initial sort is mostly for cosmetic purposes: we sort by name to
845  * ensure that logically identical schemas will dump identically.
846  */
847  sortDumpableObjectsByTypeName(dobjs, numObjs);
848 
849  sortDumpableObjects(dobjs, numObjs,
850  boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
851 
852  /*
853  * Create archive TOC entries for all the objects to be dumped, in a safe
854  * order.
855  */
856 
857  /* First the special ENCODING, STDSTRINGS, and SEARCHPATH entries. */
858  dumpEncoding(fout);
859  dumpStdStrings(fout);
860  dumpSearchPath(fout);
861 
862  /* The database items are always next, unless we don't want them at all */
863  if (dopt.outputCreateDB)
864  dumpDatabase(fout);
865 
866  /* Now the rearrangeable objects. */
867  for (i = 0; i < numObjs; i++)
868  dumpDumpableObject(fout, dobjs[i]);
869 
870  /*
871  * Set up options info to ensure we dump what we want.
872  */
873  ropt = NewRestoreOptions();
874  ropt->filename = filename;
875 
876  /* if you change this list, see dumpOptionsFromRestoreOptions */
877  ropt->dropSchema = dopt.outputClean;
878  ropt->dataOnly = dopt.dataOnly;
879  ropt->schemaOnly = dopt.schemaOnly;
880  ropt->if_exists = dopt.if_exists;
881  ropt->column_inserts = dopt.column_inserts;
882  ropt->dumpSections = dopt.dumpSections;
883  ropt->aclsSkip = dopt.aclsSkip;
884  ropt->superuser = dopt.outputSuperuser;
885  ropt->createDB = dopt.outputCreateDB;
886  ropt->noOwner = dopt.outputNoOwner;
887  ropt->noTablespace = dopt.outputNoTablespaces;
888  ropt->disable_triggers = dopt.disable_triggers;
889  ropt->use_setsessauth = dopt.use_setsessauth;
891  ropt->dump_inserts = dopt.dump_inserts;
892  ropt->no_comments = dopt.no_comments;
893  ropt->no_publications = dopt.no_publications;
895  ropt->no_subscriptions = dopt.no_subscriptions;
896  ropt->lockWaitTimeout = dopt.lockWaitTimeout;
899  ropt->sequence_data = dopt.sequence_data;
900  ropt->binary_upgrade = dopt.binary_upgrade;
901 
902  if (compressLevel == -1)
903  ropt->compression = 0;
904  else
905  ropt->compression = compressLevel;
906 
907  ropt->suppressDumpWarnings = true; /* We've already shown them */
908 
909  SetArchiveOptions(fout, &dopt, ropt);
910 
911  /* Mark which entries should be output */
913 
914  /*
915  * The archive's TOC entries are now marked as to which ones will actually
916  * be output, so we can set up their dependency lists properly. This isn't
917  * necessary for plain-text output, though.
918  */
919  if (!plainText)
921 
922  /*
923  * And finally we can do the actual output.
924  *
925  * Note: for non-plain-text output formats, the output file is written
926  * inside CloseArchive(). This is, um, bizarre; but not worth changing
927  * right now.
928  */
929  if (plainText)
930  RestoreArchive(fout);
931 
932  CloseArchive(fout);
933 
934  exit_nicely(0);
935 }
936 
937 
938 static void
939 help(const char *progname)
940 {
941  printf(_("%s dumps a database as a text file or to other formats.\n\n"), progname);
942  printf(_("Usage:\n"));
943  printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
944 
945  printf(_("\nGeneral options:\n"));
946  printf(_(" -f, --file=FILENAME output file or directory name\n"));
947  printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
948  " plain text (default))\n"));
949  printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n"));
950  printf(_(" -v, --verbose verbose mode\n"));
951  printf(_(" -V, --version output version information, then exit\n"));
952  printf(_(" -Z, --compress=0-9 compression level for compressed formats\n"));
953  printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
954  printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
955  printf(_(" -?, --help show this help, then exit\n"));
956 
957  printf(_("\nOptions controlling the output content:\n"));
958  printf(_(" -a, --data-only dump only the data, not the schema\n"));
959  printf(_(" -b, --blobs include large objects in dump\n"));
960  printf(_(" -B, --no-blobs exclude large objects in dump\n"));
961  printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
962  printf(_(" -C, --create include commands to create database in dump\n"));
963  printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
964  printf(_(" -n, --schema=SCHEMA dump the named schema(s) only\n"));
965  printf(_(" -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)\n"));
966  printf(_(" -o, --oids include OIDs in dump\n"));
967  printf(_(" -O, --no-owner skip restoration of object ownership in\n"
968  " plain-text format\n"));
969  printf(_(" -s, --schema-only dump only the schema, no data\n"));
970  printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n"));
971  printf(_(" -t, --table=TABLE dump the named table(s) only\n"));
972  printf(_(" -T, --exclude-table=TABLE do NOT dump the named table(s)\n"));
973  printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
974  printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
975  printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
976  printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
977  printf(_(" --disable-triggers disable triggers during data-only restore\n"));
978  printf(_(" --enable-row-security enable row security (dump only content user has\n"
979  " access to)\n"));
980  printf(_(" --exclude-table-data=TABLE do NOT dump data for the named table(s)\n"));
981  printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
982  printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
983  printf(_(" --load-via-partition-root load partitions via the root table\n"));
984  printf(_(" --no-comments do not dump comments\n"));
985  printf(_(" --no-publications do not dump publications\n"));
986  printf(_(" --no-security-labels do not dump security label assignments\n"));
987  printf(_(" --no-subscriptions do not dump subscriptions\n"));
988  printf(_(" --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs\n"));
989  printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
990  printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
991  printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
992  printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
993  printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
994  printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
995  printf(_(" --snapshot=SNAPSHOT use given snapshot for the dump\n"));
996  printf(_(" --strict-names require table and/or schema include patterns to\n"
997  " match at least one entity each\n"));
998  printf(_(" --use-set-session-authorization\n"
999  " use SET SESSION AUTHORIZATION commands instead of\n"
1000  " ALTER OWNER commands to set ownership\n"));
1001 
1002  printf(_("\nConnection options:\n"));
1003  printf(_(" -d, --dbname=DBNAME database to dump\n"));
1004  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
1005  printf(_(" -p, --port=PORT database server port number\n"));
1006  printf(_(" -U, --username=NAME connect as specified database user\n"));
1007  printf(_(" -w, --no-password never prompt for password\n"));
1008  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
1009  printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
1010 
1011  printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
1012  "variable value is used.\n\n"));
1013  printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
1014 }
1015 
1016 static void
1017 setup_connection(Archive *AH, const char *dumpencoding,
1018  const char *dumpsnapshot, char *use_role)
1019 {
1020  DumpOptions *dopt = AH->dopt;
1021  PGconn *conn = GetConnection(AH);
1022  const char *std_strings;
1023 
1025 
1026  /*
1027  * Set the client encoding if requested.
1028  */
1029  if (dumpencoding)
1030  {
1031  if (PQsetClientEncoding(conn, dumpencoding) < 0)
1032  exit_horribly(NULL, "invalid client encoding \"%s\" specified\n",
1033  dumpencoding);
1034  }
1035 
1036  /*
1037  * Get the active encoding and the standard_conforming_strings setting, so
1038  * we know how to escape strings.
1039  */
1040  AH->encoding = PQclientEncoding(conn);
1041 
1042  std_strings = PQparameterStatus(conn, "standard_conforming_strings");
1043  AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);
1044 
1045  /*
1046  * Set the role if requested. In a parallel dump worker, we'll be passed
1047  * use_role == NULL, but AH->use_role is already set (if user specified it
1048  * originally) and we should use that.
1049  */
1050  if (!use_role && AH->use_role)
1051  use_role = AH->use_role;
1052 
1053  /* Set the role if requested */
1054  if (use_role && AH->remoteVersion >= 80100)
1055  {
1056  PQExpBuffer query = createPQExpBuffer();
1057 
1058  appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
1059  ExecuteSqlStatement(AH, query->data);
1060  destroyPQExpBuffer(query);
1061 
1062  /* save it for possible later use by parallel workers */
1063  if (!AH->use_role)
1064  AH->use_role = pg_strdup(use_role);
1065  }
1066 
1067  /* Set the datestyle to ISO to ensure the dump's portability */
1068  ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
1069 
1070  /* Likewise, avoid using sql_standard intervalstyle */
1071  if (AH->remoteVersion >= 80400)
1072  ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
1073 
1074  /*
1075  * Set extra_float_digits so that we can dump float data exactly (given
1076  * correctly implemented float I/O code, anyway)
1077  */
1078  if (AH->remoteVersion >= 90000)
1079  ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
1080  else
1081  ExecuteSqlStatement(AH, "SET extra_float_digits TO 2");
1082 
1083  /*
1084  * If synchronized scanning is supported, disable it, to prevent
1085  * unpredictable changes in row ordering across a dump and reload.
1086  */
1087  if (AH->remoteVersion >= 80300)
1088  ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
1089 
1090  /*
1091  * Disable timeouts if supported.
1092  */
1093  ExecuteSqlStatement(AH, "SET statement_timeout = 0");
1094  if (AH->remoteVersion >= 90300)
1095  ExecuteSqlStatement(AH, "SET lock_timeout = 0");
1096  if (AH->remoteVersion >= 90600)
1097  ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
1098 
1099  /*
1100  * Quote all identifiers, if requested.
1101  */
1102  if (quote_all_identifiers && AH->remoteVersion >= 90100)
1103  ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1104 
1105  /*
1106  * Adjust row-security mode, if supported.
1107  */
1108  if (AH->remoteVersion >= 90500)
1109  {
1110  if (dopt->enable_row_security)
1111  ExecuteSqlStatement(AH, "SET row_security = on");
1112  else
1113  ExecuteSqlStatement(AH, "SET row_security = off");
1114  }
1115 
1116  /*
1117  * Start transaction-snapshot mode transaction to dump consistent data.
1118  */
1119  ExecuteSqlStatement(AH, "BEGIN");
1120  if (AH->remoteVersion >= 90100)
1121  {
1122  /*
1123  * To support the combination of serializable_deferrable with the jobs
1124  * option we use REPEATABLE READ for the worker connections that are
1125  * passed a snapshot. As long as the snapshot is acquired in a
1126  * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a
1127  * REPEATABLE READ transaction provides the appropriate integrity
1128  * guarantees. This is a kluge, but safe for back-patching.
1129  */
1130  if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
1132  "SET TRANSACTION ISOLATION LEVEL "
1133  "SERIALIZABLE, READ ONLY, DEFERRABLE");
1134  else
1136  "SET TRANSACTION ISOLATION LEVEL "
1137  "REPEATABLE READ, READ ONLY");
1138  }
1139  else
1140  {
1142  "SET TRANSACTION ISOLATION LEVEL "
1143  "SERIALIZABLE, READ ONLY");
1144  }
1145 
1146  /*
1147  * If user specified a snapshot to use, select that. In a parallel dump
1148  * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id
1149  * is already set (if the server can handle it) and we should use that.
1150  */
1151  if (dumpsnapshot)
1152  AH->sync_snapshot_id = pg_strdup(dumpsnapshot);
1153 
1154  if (AH->sync_snapshot_id)
1155  {
1156  PQExpBuffer query = createPQExpBuffer();
1157 
1158  appendPQExpBuffer(query, "SET TRANSACTION SNAPSHOT ");
1159  appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1160  ExecuteSqlStatement(AH, query->data);
1161  destroyPQExpBuffer(query);
1162  }
1163  else if (AH->numWorkers > 1 &&
1164  AH->remoteVersion >= 90200 &&
1166  {
1167  if (AH->isStandby && AH->remoteVersion < 100000)
1168  exit_horribly(NULL,
1169  "Synchronized snapshots on standby servers are not supported by this server version.\n"
1170  "Run with --no-synchronized-snapshots instead if you do not need\n"
1171  "synchronized snapshots.\n");
1172 
1173 
1175  }
1176 }
1177 
1178 /* Set up connection for a parallel worker process */
1179 static void
1181 {
1182  /*
1183  * We want to re-select all the same values the master connection is
1184  * using. We'll have inherited directly-usable values in
1185  * AH->sync_snapshot_id and AH->use_role, but we need to translate the
1186  * inherited encoding value back to a string to pass to setup_connection.
1187  */
1188  setup_connection(AH,
1190  NULL,
1191  NULL);
1192 }
1193 
1194 static char *
1196 {
1197  char *query = "SELECT pg_catalog.pg_export_snapshot()";
1198  char *result;
1199  PGresult *res;
1200 
1201  res = ExecuteSqlQueryForSingleRow(fout, query);
1202  result = pg_strdup(PQgetvalue(res, 0, 0));
1203  PQclear(res);
1204 
1205  return result;
1206 }
1207 
1208 static ArchiveFormat
1210 {
1211  ArchiveFormat archiveFormat;
1212 
1213  *mode = archModeWrite;
1214 
1215  if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1216  {
1217  /* This is used by pg_dumpall, and is not documented */
1218  archiveFormat = archNull;
1219  *mode = archModeAppend;
1220  }
1221  else if (pg_strcasecmp(format, "c") == 0)
1222  archiveFormat = archCustom;
1223  else if (pg_strcasecmp(format, "custom") == 0)
1224  archiveFormat = archCustom;
1225  else if (pg_strcasecmp(format, "d") == 0)
1226  archiveFormat = archDirectory;
1227  else if (pg_strcasecmp(format, "directory") == 0)
1228  archiveFormat = archDirectory;
1229  else if (pg_strcasecmp(format, "p") == 0)
1230  archiveFormat = archNull;
1231  else if (pg_strcasecmp(format, "plain") == 0)
1232  archiveFormat = archNull;
1233  else if (pg_strcasecmp(format, "t") == 0)
1234  archiveFormat = archTar;
1235  else if (pg_strcasecmp(format, "tar") == 0)
1236  archiveFormat = archTar;
1237  else
1238  exit_horribly(NULL, "invalid output format \"%s\" specified\n", format);
1239  return archiveFormat;
1240 }
1241 
1242 /*
1243  * Find the OIDs of all schemas matching the given list of patterns,
1244  * and append them to the given OID list.
1245  */
1246 static void
1248  SimpleStringList *patterns,
1249  SimpleOidList *oids,
1250  bool strict_names)
1251 {
1252  PQExpBuffer query;
1253  PGresult *res;
1254  SimpleStringListCell *cell;
1255  int i;
1256 
1257  if (patterns->head == NULL)
1258  return; /* nothing to do */
1259 
1260  query = createPQExpBuffer();
1261 
1262  /*
1263  * The loop below runs multiple SELECTs might sometimes result in
1264  * duplicate entries in the OID list, but we don't care.
1265  */
1266 
1267  for (cell = patterns->head; cell; cell = cell->next)
1268  {
1269  appendPQExpBuffer(query,
1270  "SELECT oid FROM pg_catalog.pg_namespace n\n");
1271  processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1272  false, NULL, "n.nspname", NULL, NULL);
1273 
1274  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1275  if (strict_names && PQntuples(res) == 0)
1276  exit_horribly(NULL, "no matching schemas were found for pattern \"%s\"\n", cell->val);
1277 
1278  for (i = 0; i < PQntuples(res); i++)
1279  {
1280  simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1281  }
1282 
1283  PQclear(res);
1284  resetPQExpBuffer(query);
1285  }
1286 
1287  destroyPQExpBuffer(query);
1288 }
1289 
1290 /*
1291  * Find the OIDs of all tables matching the given list of patterns,
1292  * and append them to the given OID list.
1293  */
1294 static void
1296  SimpleStringList *patterns, SimpleOidList *oids,
1297  bool strict_names)
1298 {
1299  PQExpBuffer query;
1300  PGresult *res;
1301  SimpleStringListCell *cell;
1302  int i;
1303 
1304  if (patterns->head == NULL)
1305  return; /* nothing to do */
1306 
1307  query = createPQExpBuffer();
1308 
1309  /*
1310  * this might sometimes result in duplicate entries in the OID list, but
1311  * we don't care.
1312  */
1313 
1314  for (cell = patterns->head; cell; cell = cell->next)
1315  {
1316  /*
1317  * Query must remain ABSOLUTELY devoid of unqualified names. This
1318  * would be unnecessary given a pg_table_is_visible() variant taking a
1319  * search_path argument.
1320  */
1321  appendPQExpBuffer(query,
1322  "SELECT c.oid"
1323  "\nFROM pg_catalog.pg_class c"
1324  "\n LEFT JOIN pg_catalog.pg_namespace n"
1325  "\n ON n.oid OPERATOR(pg_catalog.=) c.relnamespace"
1326  "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY"
1327  "\n (array['%c', '%c', '%c', '%c', '%c', '%c'])\n",
1328  RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1329  RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
1330  RELKIND_PARTITIONED_TABLE);
1331  processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1332  false, "n.nspname", "c.relname", NULL,
1333  "pg_catalog.pg_table_is_visible(c.oid)");
1334 
1335  ExecuteSqlStatement(fout, "RESET search_path");
1336  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1339  if (strict_names && PQntuples(res) == 0)
1340  exit_horribly(NULL, "no matching tables were found for pattern \"%s\"\n", cell->val);
1341 
1342  for (i = 0; i < PQntuples(res); i++)
1343  {
1344  simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1345  }
1346 
1347  PQclear(res);
1348  resetPQExpBuffer(query);
1349  }
1350 
1351  destroyPQExpBuffer(query);
1352 }
1353 
1354 /*
1355  * checkExtensionMembership
1356  * Determine whether object is an extension member, and if so,
1357  * record an appropriate dependency and set the object's dump flag.
1358  *
1359  * It's important to call this for each object that could be an extension
1360  * member. Generally, we integrate this with determining the object's
1361  * to-be-dumped-ness, since extension membership overrides other rules for that.
1362  *
1363  * Returns true if object is an extension member, else false.
1364  */
1365 static bool
1367 {
1368  ExtensionInfo *ext = findOwningExtension(dobj->catId);
1369 
1370  if (ext == NULL)
1371  return false;
1372 
1373  dobj->ext_member = true;
1374 
1375  /* Record dependency so that getDependencies needn't deal with that */
1376  addObjectDependency(dobj, ext->dobj.dumpId);
1377 
1378  /*
1379  * In 9.6 and above, mark the member object to have any non-initial ACL,
1380  * policies, and security labels dumped.
1381  *
1382  * Note that any initial ACLs (see pg_init_privs) will be removed when we
1383  * extract the information about the object. We don't provide support for
1384  * initial policies and security labels and it seems unlikely for those to
1385  * ever exist, but we may have to revisit this later.
1386  *
1387  * Prior to 9.6, we do not include any extension member components.
1388  *
1389  * In binary upgrades, we still dump all components of the members
1390  * individually, since the idea is to exactly reproduce the database
1391  * contents rather than replace the extension contents with something
1392  * different.
1393  */
1394  if (fout->dopt->binary_upgrade)
1395  dobj->dump = ext->dobj.dump;
1396  else
1397  {
1398  if (fout->remoteVersion < 90600)
1399  dobj->dump = DUMP_COMPONENT_NONE;
1400  else
1401  dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1404  }
1405 
1406  return true;
1407 }
1408 
1409 /*
1410  * selectDumpableNamespace: policy-setting subroutine
1411  * Mark a namespace as to be dumped or not
1412  */
1413 static void
1415 {
1416  /*
1417  * If specific tables are being dumped, do not dump any complete
1418  * namespaces. If specific namespaces are being dumped, dump just those
1419  * namespaces. Otherwise, dump all non-system namespaces.
1420  */
1421  if (table_include_oids.head != NULL)
1422  nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1423  else if (schema_include_oids.head != NULL)
1424  nsinfo->dobj.dump_contains = nsinfo->dobj.dump =
1425  simple_oid_list_member(&schema_include_oids,
1426  nsinfo->dobj.catId.oid) ?
1428  else if (fout->remoteVersion >= 90600 &&
1429  strcmp(nsinfo->dobj.name, "pg_catalog") == 0)
1430  {
1431  /*
1432  * In 9.6 and above, we dump out any ACLs defined in pg_catalog, if
1433  * they are interesting (and not the original ACLs which were set at
1434  * initdb time, see pg_init_privs).
1435  */
1436  nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
1437  }
1438  else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
1439  strcmp(nsinfo->dobj.name, "information_schema") == 0)
1440  {
1441  /* Other system schemas don't get dumped */
1442  nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1443  }
1444  else if (strcmp(nsinfo->dobj.name, "public") == 0)
1445  {
1446  /*
1447  * The public schema is a strange beast that sits in a sort of
1448  * no-mans-land between being a system object and a user object. We
1449  * don't want to dump creation or comment commands for it, because
1450  * that complicates matters for non-superuser use of pg_dump. But we
1451  * should dump any ACL changes that have occurred for it, and of
1452  * course we should dump contained objects.
1453  */
1454  nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
1456  }
1457  else
1458  nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
1459 
1460  /*
1461  * In any case, a namespace can be excluded by an exclusion switch
1462  */
1463  if (nsinfo->dobj.dump_contains &&
1464  simple_oid_list_member(&schema_exclude_oids,
1465  nsinfo->dobj.catId.oid))
1466  nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1467 
1468  /*
1469  * If the schema belongs to an extension, allow extension membership to
1470  * override the dump decision for the schema itself. However, this does
1471  * not change dump_contains, so this won't change what we do with objects
1472  * within the schema. (If they belong to the extension, they'll get
1473  * suppressed by it, otherwise not.)
1474  */
1475  (void) checkExtensionMembership(&nsinfo->dobj, fout);
1476 }
1477 
1478 /*
1479  * selectDumpableTable: policy-setting subroutine
1480  * Mark a table as to be dumped or not
1481  */
1482 static void
1484 {
1485  if (checkExtensionMembership(&tbinfo->dobj, fout))
1486  return; /* extension membership overrides all else */
1487 
1488  /*
1489  * If specific tables are being dumped, dump just those tables; else, dump
1490  * according to the parent namespace's dump flag.
1491  */
1492  if (table_include_oids.head != NULL)
1493  tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
1494  tbinfo->dobj.catId.oid) ?
1496  else
1497  tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains;
1498 
1499  /*
1500  * In any case, a table can be excluded by an exclusion switch
1501  */
1502  if (tbinfo->dobj.dump &&
1503  simple_oid_list_member(&table_exclude_oids,
1504  tbinfo->dobj.catId.oid))
1505  tbinfo->dobj.dump = DUMP_COMPONENT_NONE;
1506 }
1507 
1508 /*
1509  * selectDumpableType: policy-setting subroutine
1510  * Mark a type as to be dumped or not
1511  *
1512  * If it's a table's rowtype or an autogenerated array type, we also apply a
1513  * special type code to facilitate sorting into the desired order. (We don't
1514  * want to consider those to be ordinary types because that would bring tables
1515  * up into the datatype part of the dump order.) We still set the object's
1516  * dump flag; that's not going to cause the dummy type to be dumped, but we
1517  * need it so that casts involving such types will be dumped correctly -- see
1518  * dumpCast. This means the flag should be set the same as for the underlying
1519  * object (the table or base type).
1520  */
1521 static void
1523 {
1524  /* skip complex types, except for standalone composite types */
1525  if (OidIsValid(tyinfo->typrelid) &&
1526  tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
1527  {
1528  TableInfo *tytable = findTableByOid(tyinfo->typrelid);
1529 
1530  tyinfo->dobj.objType = DO_DUMMY_TYPE;
1531  if (tytable != NULL)
1532  tyinfo->dobj.dump = tytable->dobj.dump;
1533  else
1534  tyinfo->dobj.dump = DUMP_COMPONENT_NONE;
1535  return;
1536  }
1537 
1538  /* skip auto-generated array types */
1539  if (tyinfo->isArray)
1540  {
1541  tyinfo->dobj.objType = DO_DUMMY_TYPE;
1542 
1543  /*
1544  * Fall through to set the dump flag; we assume that the subsequent
1545  * rules will do the same thing as they would for the array's base
1546  * type. (We cannot reliably look up the base type here, since
1547  * getTypes may not have processed it yet.)
1548  */
1549  }
1550 
1551  if (checkExtensionMembership(&tyinfo->dobj, fout))
1552  return; /* extension membership overrides all else */
1553 
1554  /* Dump based on if the contents of the namespace are being dumped */
1555  tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains;
1556 }
1557 
1558 /*
1559  * selectDumpableDefaultACL: policy-setting subroutine
1560  * Mark a default ACL as to be dumped or not
1561  *
1562  * For per-schema default ACLs, dump if the schema is to be dumped.
1563  * Otherwise dump if we are dumping "everything". Note that dataOnly
1564  * and aclsSkip are checked separately.
1565  */
1566 static void
1568 {
1569  /* Default ACLs can't be extension members */
1570 
1571  if (dinfo->dobj.namespace)
1572  /* default ACLs are considered part of the namespace */
1573  dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains;
1574  else
1575  dinfo->dobj.dump = dopt->include_everything ?
1577 }
1578 
1579 /*
1580  * selectDumpableCast: policy-setting subroutine
1581  * Mark a cast as to be dumped or not
1582  *
1583  * Casts do not belong to any particular namespace (since they haven't got
1584  * names), nor do they have identifiable owners. To distinguish user-defined
1585  * casts from built-in ones, we must resort to checking whether the cast's
1586  * OID is in the range reserved for initdb.
1587  */
1588 static void
1590 {
1591  if (checkExtensionMembership(&cast->dobj, fout))
1592  return; /* extension membership overrides all else */
1593 
1594  /*
1595  * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
1596  * support ACLs currently.
1597  */
1598  if (cast->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1599  cast->dobj.dump = DUMP_COMPONENT_NONE;
1600  else
1601  cast->dobj.dump = fout->dopt->include_everything ?
1603 }
1604 
1605 /*
1606  * selectDumpableProcLang: policy-setting subroutine
1607  * Mark a procedural language as to be dumped or not
1608  *
1609  * Procedural languages do not belong to any particular namespace. To
1610  * identify built-in languages, we must resort to checking whether the
1611  * language's OID is in the range reserved for initdb.
1612  */
1613 static void
1615 {
1616  if (checkExtensionMembership(&plang->dobj, fout))
1617  return; /* extension membership overrides all else */
1618 
1619  /*
1620  * Only include procedural languages when we are dumping everything.
1621  *
1622  * For from-initdb procedural languages, only include ACLs, as we do for
1623  * the pg_catalog namespace. We need this because procedural languages do
1624  * not live in any namespace.
1625  */
1626  if (!fout->dopt->include_everything)
1627  plang->dobj.dump = DUMP_COMPONENT_NONE;
1628  else
1629  {
1630  if (plang->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1631  plang->dobj.dump = fout->remoteVersion < 90600 ?
1633  else
1634  plang->dobj.dump = DUMP_COMPONENT_ALL;
1635  }
1636 }
1637 
1638 /*
1639  * selectDumpableAccessMethod: policy-setting subroutine
1640  * Mark an access method as to be dumped or not
1641  *
1642  * Access methods do not belong to any particular namespace. To identify
1643  * built-in access methods, we must resort to checking whether the
1644  * method's OID is in the range reserved for initdb.
1645  */
1646 static void
1648 {
1649  if (checkExtensionMembership(&method->dobj, fout))
1650  return; /* extension membership overrides all else */
1651 
1652  /*
1653  * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
1654  * they do not support ACLs currently.
1655  */
1656  if (method->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1657  method->dobj.dump = DUMP_COMPONENT_NONE;
1658  else
1659  method->dobj.dump = fout->dopt->include_everything ?
1661 }
1662 
1663 /*
1664  * selectDumpableExtension: policy-setting subroutine
1665  * Mark an extension as to be dumped or not
1666  *
1667  * Built-in extensions should be skipped except for checking ACLs, since we
1668  * assume those will already be installed in the target database. We identify
1669  * such extensions by their having OIDs in the range reserved for initdb.
1670  * We dump all user-added extensions by default, or none of them if
1671  * include_everything is false (i.e., a --schema or --table switch was given).
1672  */
1673 static void
1675 {
1676  /*
1677  * Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to
1678  * change permissions on their member objects, if they wish to, and have
1679  * those changes preserved.
1680  */
1681  if (extinfo->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1682  extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
1683  else
1684  extinfo->dobj.dump = extinfo->dobj.dump_contains =
1687 }
1688 
1689 /*
1690  * selectDumpablePublicationTable: policy-setting subroutine
1691  * Mark a publication table as to be dumped or not
1692  *
1693  * Publication tables have schemas, but those are ignored in decision making,
1694  * because publications are only dumped when we are dumping everything.
1695  */
1696 static void
1698 {
1699  if (checkExtensionMembership(dobj, fout))
1700  return; /* extension membership overrides all else */
1701 
1702  dobj->dump = fout->dopt->include_everything ?
1704 }
1705 
1706 /*
1707  * selectDumpableObject: policy-setting subroutine
1708  * Mark a generic dumpable object as to be dumped or not
1709  *
1710  * Use this only for object types without a special-case routine above.
1711  */
1712 static void
1714 {
1715  if (checkExtensionMembership(dobj, fout))
1716  return; /* extension membership overrides all else */
1717 
1718  /*
1719  * Default policy is to dump if parent namespace is dumpable, or for
1720  * non-namespace-associated items, dump if we're dumping "everything".
1721  */
1722  if (dobj->namespace)
1723  dobj->dump = dobj->namespace->dobj.dump_contains;
1724  else
1725  dobj->dump = fout->dopt->include_everything ?
1727 }
1728 
1729 /*
1730  * Dump a table's contents for loading using the COPY command
1731  * - this routine is called by the Archiver when it wants the table
1732  * to be dumped.
1733  */
1734 
1735 static int
1736 dumpTableData_copy(Archive *fout, void *dcontext)
1737 {
1738  TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1739  TableInfo *tbinfo = tdinfo->tdtable;
1740  const char *classname = tbinfo->dobj.name;
1741  const bool hasoids = tbinfo->hasoids;
1742  const bool oids = tdinfo->oids;
1744 
1745  /*
1746  * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
1747  * which uses it already.
1748  */
1749  PQExpBuffer clistBuf = createPQExpBuffer();
1750  PGconn *conn = GetConnection(fout);
1751  PGresult *res;
1752  int ret;
1753  char *copybuf;
1754  const char *column_list;
1755 
1756  if (g_verbose)
1757  write_msg(NULL, "dumping contents of table \"%s.%s\"\n",
1758  tbinfo->dobj.namespace->dobj.name, classname);
1759 
1760  /*
1761  * Specify the column list explicitly so that we have no possibility of
1762  * retrieving data in the wrong column order. (The default column
1763  * ordering of COPY will not be what we want in certain corner cases
1764  * involving ADD COLUMN and inheritance.)
1765  */
1766  column_list = fmtCopyColumnList(tbinfo, clistBuf);
1767 
1768  if (oids && hasoids)
1769  {
1770  appendPQExpBuffer(q, "COPY %s %s WITH OIDS TO stdout;",
1771  fmtQualifiedDumpable(tbinfo),
1772  column_list);
1773  }
1774  else if (tdinfo->filtercond)
1775  {
1776  /* Note: this syntax is only supported in 8.2 and up */
1777  appendPQExpBufferStr(q, "COPY (SELECT ");
1778  /* klugery to get rid of parens in column list */
1779  if (strlen(column_list) > 2)
1780  {
1781  appendPQExpBufferStr(q, column_list + 1);
1782  q->data[q->len - 1] = ' ';
1783  }
1784  else
1785  appendPQExpBufferStr(q, "* ");
1786  appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
1787  fmtQualifiedDumpable(tbinfo),
1788  tdinfo->filtercond);
1789  }
1790  else
1791  {
1792  appendPQExpBuffer(q, "COPY %s %s TO stdout;",
1793  fmtQualifiedDumpable(tbinfo),
1794  column_list);
1795  }
1796  res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
1797  PQclear(res);
1798  destroyPQExpBuffer(clistBuf);
1799 
1800  for (;;)
1801  {
1802  ret = PQgetCopyData(conn, &copybuf, 0);
1803 
1804  if (ret < 0)
1805  break; /* done or error */
1806 
1807  if (copybuf)
1808  {
1809  WriteData(fout, copybuf, ret);
1810  PQfreemem(copybuf);
1811  }
1812 
1813  /* ----------
1814  * THROTTLE:
1815  *
1816  * There was considerable discussion in late July, 2000 regarding
1817  * slowing down pg_dump when backing up large tables. Users with both
1818  * slow & fast (multi-processor) machines experienced performance
1819  * degradation when doing a backup.
1820  *
1821  * Initial attempts based on sleeping for a number of ms for each ms
1822  * of work were deemed too complex, then a simple 'sleep in each loop'
1823  * implementation was suggested. The latter failed because the loop
1824  * was too tight. Finally, the following was implemented:
1825  *
1826  * If throttle is non-zero, then
1827  * See how long since the last sleep.
1828  * Work out how long to sleep (based on ratio).
1829  * If sleep is more than 100ms, then
1830  * sleep
1831  * reset timer
1832  * EndIf
1833  * EndIf
1834  *
1835  * where the throttle value was the number of ms to sleep per ms of
1836  * work. The calculation was done in each loop.
1837  *
1838  * Most of the hard work is done in the backend, and this solution
1839  * still did not work particularly well: on slow machines, the ratio
1840  * was 50:1, and on medium paced machines, 1:1, and on fast
1841  * multi-processor machines, it had little or no effect, for reasons
1842  * that were unclear.
1843  *
1844  * Further discussion ensued, and the proposal was dropped.
1845  *
1846  * For those people who want this feature, it can be implemented using
1847  * gettimeofday in each loop, calculating the time since last sleep,
1848  * multiplying that by the sleep ratio, then if the result is more
1849  * than a preset 'minimum sleep time' (say 100ms), call the 'select'
1850  * function to sleep for a subsecond period ie.
1851  *
1852  * select(0, NULL, NULL, NULL, &tvi);
1853  *
1854  * This will return after the interval specified in the structure tvi.
1855  * Finally, call gettimeofday again to save the 'last sleep time'.
1856  * ----------
1857  */
1858  }
1859  archprintf(fout, "\\.\n\n\n");
1860 
1861  if (ret == -2)
1862  {
1863  /* copy data transfer failed */
1864  write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.\n", classname);
1865  write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1866  write_msg(NULL, "The command was: %s\n", q->data);
1867  exit_nicely(1);
1868  }
1869 
1870  /* Check command status and return to normal libpq state */
1871  res = PQgetResult(conn);
1872  if (PQresultStatus(res) != PGRES_COMMAND_OK)
1873  {
1874  write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetResult() failed.\n", classname);
1875  write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1876  write_msg(NULL, "The command was: %s\n", q->data);
1877  exit_nicely(1);
1878  }
1879  PQclear(res);
1880 
1881  /* Do this to ensure we've pumped libpq back to idle state */
1882  if (PQgetResult(conn) != NULL)
1883  write_msg(NULL, "WARNING: unexpected extra results during COPY of table \"%s\"\n",
1884  classname);
1885 
1886  destroyPQExpBuffer(q);
1887  return 1;
1888 }
1889 
1890 /*
1891  * Dump table data using INSERT commands.
1892  *
1893  * Caution: when we restore from an archive file direct to database, the
1894  * INSERT commands emitted by this function have to be parsed by
1895  * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments,
1896  * E'' strings, or dollar-quoted strings. So don't emit anything like that.
1897  */
1898 static int
1899 dumpTableData_insert(Archive *fout, void *dcontext)
1900 {
1901  TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1902  TableInfo *tbinfo = tdinfo->tdtable;
1903  DumpOptions *dopt = fout->dopt;
1905  PQExpBuffer insertStmt = NULL;
1906  PGresult *res;
1907  int tuple;
1908  int nfields;
1909  int field;
1910 
1911  appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
1912  "SELECT * FROM ONLY %s",
1913  fmtQualifiedDumpable(tbinfo));
1914  if (tdinfo->filtercond)
1915  appendPQExpBuffer(q, " %s", tdinfo->filtercond);
1916 
1917  ExecuteSqlStatement(fout, q->data);
1918 
1919  while (1)
1920  {
1921  res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
1922  PGRES_TUPLES_OK);
1923  nfields = PQnfields(res);
1924  for (tuple = 0; tuple < PQntuples(res); tuple++)
1925  {
1926  /*
1927  * First time through, we build as much of the INSERT statement as
1928  * possible in "insertStmt", which we can then just print for each
1929  * line. If the table happens to have zero columns then this will
1930  * be a complete statement, otherwise it will end in "VALUES(" and
1931  * be ready to have the row's column values appended.
1932  */
1933  if (insertStmt == NULL)
1934  {
1935  TableInfo *targettab;
1936 
1937  insertStmt = createPQExpBuffer();
1938 
1939  /*
1940  * When load-via-partition-root is set, get the root table
1941  * name for the partition table, so that we can reload data
1942  * through the root table.
1943  */
1944  if (dopt->load_via_partition_root && tbinfo->ispartition)
1945  targettab = getRootTableInfo(tbinfo);
1946  else
1947  targettab = tbinfo;
1948 
1949  appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
1950  fmtQualifiedDumpable(targettab));
1951 
1952  /* corner case for zero-column table */
1953  if (nfields == 0)
1954  {
1955  appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n");
1956  }
1957  else
1958  {
1959  /* append the list of column names if required */
1960  if (dopt->column_inserts)
1961  {
1962  appendPQExpBufferChar(insertStmt, '(');
1963  for (field = 0; field < nfields; field++)
1964  {
1965  if (field > 0)
1966  appendPQExpBufferStr(insertStmt, ", ");
1967  appendPQExpBufferStr(insertStmt,
1968  fmtId(PQfname(res, field)));
1969  }
1970  appendPQExpBufferStr(insertStmt, ") ");
1971  }
1972 
1973  if (tbinfo->needs_override)
1974  appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE ");
1975 
1976  appendPQExpBufferStr(insertStmt, "VALUES (");
1977  }
1978  }
1979 
1980  archputs(insertStmt->data, fout);
1981 
1982  /* if it is zero-column table then we're done */
1983  if (nfields == 0)
1984  continue;
1985 
1986  for (field = 0; field < nfields; field++)
1987  {
1988  if (field > 0)
1989  archputs(", ", fout);
1990  if (PQgetisnull(res, tuple, field))
1991  {
1992  archputs("NULL", fout);
1993  continue;
1994  }
1995 
1996  /* XXX This code is partially duplicated in ruleutils.c */
1997  switch (PQftype(res, field))
1998  {
1999  case INT2OID:
2000  case INT4OID:
2001  case INT8OID:
2002  case OIDOID:
2003  case FLOAT4OID:
2004  case FLOAT8OID:
2005  case NUMERICOID:
2006  {
2007  /*
2008  * These types are printed without quotes unless
2009  * they contain values that aren't accepted by the
2010  * scanner unquoted (e.g., 'NaN'). Note that
2011  * strtod() and friends might accept NaN, so we
2012  * can't use that to test.
2013  *
2014  * In reality we only need to defend against
2015  * infinity and NaN, so we need not get too crazy
2016  * about pattern matching here.
2017  */
2018  const char *s = PQgetvalue(res, tuple, field);
2019 
2020  if (strspn(s, "0123456789 +-eE.") == strlen(s))
2021  archputs(s, fout);
2022  else
2023  archprintf(fout, "'%s'", s);
2024  }
2025  break;
2026 
2027  case BITOID:
2028  case VARBITOID:
2029  archprintf(fout, "B'%s'",
2030  PQgetvalue(res, tuple, field));
2031  break;
2032 
2033  case BOOLOID:
2034  if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
2035  archputs("true", fout);
2036  else
2037  archputs("false", fout);
2038  break;
2039 
2040  default:
2041  /* All other types are printed as string literals. */
2042  resetPQExpBuffer(q);
2044  PQgetvalue(res, tuple, field),
2045  fout);
2046  archputs(q->data, fout);
2047  break;
2048  }
2049  }
2050 
2051  if (!dopt->do_nothing)
2052  archputs(");\n", fout);
2053  else
2054  archputs(") ON CONFLICT DO NOTHING;\n", fout);
2055  }
2056 
2057  if (PQntuples(res) <= 0)
2058  {
2059  PQclear(res);
2060  break;
2061  }
2062  PQclear(res);
2063  }
2064 
2065  archputs("\n\n", fout);
2066 
2067  ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
2068 
2069  destroyPQExpBuffer(q);
2070  if (insertStmt != NULL)
2071  destroyPQExpBuffer(insertStmt);
2072 
2073  return 1;
2074 }
2075 
2076 /*
2077  * getRootTableInfo:
2078  * get the root TableInfo for the given partition table.
2079  */
2080 static TableInfo *
2082 {
2083  TableInfo *parentTbinfo;
2084 
2085  Assert(tbinfo->ispartition);
2086  Assert(tbinfo->numParents == 1);
2087 
2088  parentTbinfo = tbinfo->parents[0];
2089  while (parentTbinfo->ispartition)
2090  {
2091  Assert(parentTbinfo->numParents == 1);
2092  parentTbinfo = parentTbinfo->parents[0];
2093  }
2094 
2095  return parentTbinfo;
2096 }
2097 
2098 /*
2099  * dumpTableData -
2100  * dump the contents of a single table
2101  *
2102  * Actually, this just makes an ArchiveEntry for the table contents.
2103  */
2104 static void
2106 {
2107  DumpOptions *dopt = fout->dopt;
2108  TableInfo *tbinfo = tdinfo->tdtable;
2109  PQExpBuffer copyBuf = createPQExpBuffer();
2110  PQExpBuffer clistBuf = createPQExpBuffer();
2111  DataDumperPtr dumpFn;
2112  char *copyStmt;
2113  const char *copyFrom;
2114 
2115  if (!dopt->dump_inserts)
2116  {
2117  /* Dump/restore using COPY */
2118  dumpFn = dumpTableData_copy;
2119 
2120  /*
2121  * When load-via-partition-root is set, get the root table name for
2122  * the partition table, so that we can reload data through the root
2123  * table.
2124  */
2125  if (dopt->load_via_partition_root && tbinfo->ispartition)
2126  {
2127  TableInfo *parentTbinfo;
2128 
2129  parentTbinfo = getRootTableInfo(tbinfo);
2130  copyFrom = fmtQualifiedDumpable(parentTbinfo);
2131  }
2132  else
2133  copyFrom = fmtQualifiedDumpable(tbinfo);
2134 
2135  /* must use 2 steps here 'cause fmtId is nonreentrant */
2136  appendPQExpBuffer(copyBuf, "COPY %s ",
2137  copyFrom);
2138  appendPQExpBuffer(copyBuf, "%s %sFROM stdin;\n",
2139  fmtCopyColumnList(tbinfo, clistBuf),
2140  (tdinfo->oids && tbinfo->hasoids) ? "WITH OIDS " : "");
2141  copyStmt = copyBuf->data;
2142  }
2143  else
2144  {
2145  /* Restore using INSERT */
2146  dumpFn = dumpTableData_insert;
2147  copyStmt = NULL;
2148  }
2149 
2150  /*
2151  * Note: although the TableDataInfo is a full DumpableObject, we treat its
2152  * dependency on its table as "special" and pass it to ArchiveEntry now.
2153  * See comments for BuildArchiveDependencies.
2154  */
2155  if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2156  {
2157  TocEntry *te;
2158 
2159  te = ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
2160  tbinfo->dobj.name, tbinfo->dobj.namespace->dobj.name,
2161  NULL, tbinfo->rolname,
2162  false, "TABLE DATA", SECTION_DATA,
2163  "", "", copyStmt,
2164  &(tbinfo->dobj.dumpId), 1,
2165  dumpFn, tdinfo);
2166 
2167  /*
2168  * Set the TocEntry's dataLength in case we are doing a parallel dump
2169  * and want to order dump jobs by table size. We choose to measure
2170  * dataLength in table pages during dump, so no scaling is needed.
2171  * However, relpages is declared as "integer" in pg_class, and hence
2172  * also in TableInfo, but it's really BlockNumber a/k/a unsigned int.
2173  * Cast so that we get the right interpretation of table sizes
2174  * exceeding INT_MAX pages.
2175  */
2176  te->dataLength = (BlockNumber) tbinfo->relpages;
2177  }
2178 
2179  destroyPQExpBuffer(copyBuf);
2180  destroyPQExpBuffer(clistBuf);
2181 }
2182 
2183 /*
2184  * refreshMatViewData -
2185  * load or refresh the contents of a single materialized view
2186  *
2187  * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
2188  * statement.
2189  */
2190 static void
2192 {
2193  TableInfo *tbinfo = tdinfo->tdtable;
2194  PQExpBuffer q;
2195 
2196  /* If the materialized view is not flagged as populated, skip this. */
2197  if (!tbinfo->relispopulated)
2198  return;
2199 
2200  q = createPQExpBuffer();
2201 
2202  appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
2203  fmtQualifiedDumpable(tbinfo));
2204 
2205  if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2206  ArchiveEntry(fout,
2207  tdinfo->dobj.catId, /* catalog ID */
2208  tdinfo->dobj.dumpId, /* dump ID */
2209  tbinfo->dobj.name, /* Name */
2210  tbinfo->dobj.namespace->dobj.name, /* Namespace */
2211  NULL, /* Tablespace */
2212  tbinfo->rolname, /* Owner */
2213  false, /* with oids */
2214  "MATERIALIZED VIEW DATA", /* Desc */
2215  SECTION_POST_DATA, /* Section */
2216  q->data, /* Create */
2217  "", /* Del */
2218  NULL, /* Copy */
2219  tdinfo->dobj.dependencies, /* Deps */
2220  tdinfo->dobj.nDeps, /* # Deps */
2221  NULL, /* Dumper */
2222  NULL); /* Dumper Arg */
2223 
2224  destroyPQExpBuffer(q);
2225 }
2226 
2227 /*
2228  * getTableData -
2229  * set up dumpable objects representing the contents of tables
2230  */
2231 static void
2232 getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind)
2233 {
2234  int i;
2235 
2236  for (i = 0; i < numTables; i++)
2237  {
2238  if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
2239  (!relkind || tblinfo[i].relkind == relkind))
2240  makeTableDataInfo(dopt, &(tblinfo[i]), oids);
2241  }
2242 }
2243 
2244 /*
2245  * Make a dumpable object for the data of this specific table
2246  *
2247  * Note: we make a TableDataInfo if and only if we are going to dump the
2248  * table data; the "dump" flag in such objects isn't used.
2249  */
2250 static void
2251 makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids)
2252 {
2253  TableDataInfo *tdinfo;
2254 
2255  /*
2256  * Nothing to do if we already decided to dump the table. This will
2257  * happen for "config" tables.
2258  */
2259  if (tbinfo->dataObj != NULL)
2260  return;
2261 
2262  /* Skip VIEWs (no data to dump) */
2263  if (tbinfo->relkind == RELKIND_VIEW)
2264  return;
2265  /* Skip FOREIGN TABLEs (no data to dump) */
2266  if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2267  return;
2268  /* Skip partitioned tables (data in partitions) */
2269  if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
2270  return;
2271 
2272  /* Don't dump data in unlogged tables, if so requested */
2273  if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
2274  dopt->no_unlogged_table_data)
2275  return;
2276 
2277  /* Check that the data is not explicitly excluded */
2278  if (simple_oid_list_member(&tabledata_exclude_oids,
2279  tbinfo->dobj.catId.oid))
2280  return;
2281 
2282  /* OK, let's dump it */
2283  tdinfo = (TableDataInfo *) pg_malloc(sizeof(TableDataInfo));
2284 
2285  if (tbinfo->relkind == RELKIND_MATVIEW)
2286  tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
2287  else if (tbinfo->relkind == RELKIND_SEQUENCE)
2288  tdinfo->dobj.objType = DO_SEQUENCE_SET;
2289  else
2290  tdinfo->dobj.objType = DO_TABLE_DATA;
2291 
2292  /*
2293  * Note: use tableoid 0 so that this object won't be mistaken for
2294  * something that pg_depend entries apply to.
2295  */
2296  tdinfo->dobj.catId.tableoid = 0;
2297  tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
2298  AssignDumpId(&tdinfo->dobj);
2299  tdinfo->dobj.name = tbinfo->dobj.name;
2300  tdinfo->dobj.namespace = tbinfo->dobj.namespace;
2301  tdinfo->tdtable = tbinfo;
2302  tdinfo->oids = oids;
2303  tdinfo->filtercond = NULL; /* might get set later */
2304  addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
2305 
2306  tbinfo->dataObj = tdinfo;
2307 }
2308 
2309 /*
2310  * The refresh for a materialized view must be dependent on the refresh for
2311  * any materialized view that this one is dependent on.
2312  *
2313  * This must be called after all the objects are created, but before they are
2314  * sorted.
2315  */
2316 static void
2318 {
2319  PQExpBuffer query;
2320  PGresult *res;
2321  int ntups,
2322  i;
2323  int i_classid,
2324  i_objid,
2325  i_refobjid;
2326 
2327  /* No Mat Views before 9.3. */
2328  if (fout->remoteVersion < 90300)
2329  return;
2330 
2331  query = createPQExpBuffer();
2332 
2333  appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2334  "( "
2335  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2336  "FROM pg_depend d1 "
2337  "JOIN pg_class c1 ON c1.oid = d1.objid "
2338  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2339  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2340  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2341  "AND d2.objid = r1.oid "
2342  "AND d2.refobjid <> d1.objid "
2343  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2344  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2345  CppAsString2(RELKIND_VIEW) ") "
2346  "WHERE d1.classid = 'pg_class'::regclass "
2347  "UNION "
2348  "SELECT w.objid, d3.refobjid, c3.relkind "
2349  "FROM w "
2350  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2351  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2352  "AND d3.objid = r3.oid "
2353  "AND d3.refobjid <> w.refobjid "
2354  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2355  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2356  CppAsString2(RELKIND_VIEW) ") "
2357  ") "
2358  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2359  "FROM w "
2360  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2361 
2362  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2363 
2364  ntups = PQntuples(res);
2365 
2366  i_classid = PQfnumber(res, "classid");
2367  i_objid = PQfnumber(res, "objid");
2368  i_refobjid = PQfnumber(res, "refobjid");
2369 
2370  for (i = 0; i < ntups; i++)
2371  {
2372  CatalogId objId;
2373  CatalogId refobjId;
2374  DumpableObject *dobj;
2375  DumpableObject *refdobj;
2376  TableInfo *tbinfo;
2377  TableInfo *reftbinfo;
2378 
2379  objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2380  objId.oid = atooid(PQgetvalue(res, i, i_objid));
2381  refobjId.tableoid = objId.tableoid;
2382  refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2383 
2384  dobj = findObjectByCatalogId(objId);
2385  if (dobj == NULL)
2386  continue;
2387 
2388  Assert(dobj->objType == DO_TABLE);
2389  tbinfo = (TableInfo *) dobj;
2390  Assert(tbinfo->relkind == RELKIND_MATVIEW);
2391  dobj = (DumpableObject *) tbinfo->dataObj;
2392  if (dobj == NULL)
2393  continue;
2394  Assert(dobj->objType == DO_REFRESH_MATVIEW);
2395 
2396  refdobj = findObjectByCatalogId(refobjId);
2397  if (refdobj == NULL)
2398  continue;
2399 
2400  Assert(refdobj->objType == DO_TABLE);
2401  reftbinfo = (TableInfo *) refdobj;
2402  Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2403  refdobj = (DumpableObject *) reftbinfo->dataObj;
2404  if (refdobj == NULL)
2405  continue;
2406  Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2407 
2408  addObjectDependency(dobj, refdobj->dumpId);
2409 
2410  if (!reftbinfo->relispopulated)
2411  tbinfo->relispopulated = false;
2412  }
2413 
2414  PQclear(res);
2415 
2416  destroyPQExpBuffer(query);
2417 }
2418 
2419 /*
2420  * getTableDataFKConstraints -
2421  * add dump-order dependencies reflecting foreign key constraints
2422  *
2423  * This code is executed only in a data-only dump --- in schema+data dumps
2424  * we handle foreign key issues by not creating the FK constraints until
2425  * after the data is loaded. In a data-only dump, however, we want to
2426  * order the table data objects in such a way that a table's referenced
2427  * tables are restored first. (In the presence of circular references or
2428  * self-references this may be impossible; we'll detect and complain about
2429  * that during the dependency sorting step.)
2430  */
2431 static void
2433 {
2434  DumpableObject **dobjs;
2435  int numObjs;
2436  int i;
2437 
2438  /* Search through all the dumpable objects for FK constraints */
2439  getDumpableObjects(&dobjs, &numObjs);
2440  for (i = 0; i < numObjs; i++)
2441  {
2442  if (dobjs[i]->objType == DO_FK_CONSTRAINT)
2443  {
2444  ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
2445  TableInfo *ftable;
2446 
2447  /* Not interesting unless both tables are to be dumped */
2448  if (cinfo->contable == NULL ||
2449  cinfo->contable->dataObj == NULL)
2450  continue;
2451  ftable = findTableByOid(cinfo->confrelid);
2452  if (ftable == NULL ||
2453  ftable->dataObj == NULL)
2454  continue;
2455 
2456  /*
2457  * Okay, make referencing table's TABLE_DATA object depend on the
2458  * referenced table's TABLE_DATA object.
2459  */
2461  ftable->dataObj->dobj.dumpId);
2462  }
2463  }
2464  free(dobjs);
2465 }
2466 
2467 
2468 /*
2469  * guessConstraintInheritance:
2470  * In pre-8.4 databases, we can't tell for certain which constraints
2471  * are inherited. We assume a CHECK constraint is inherited if its name
2472  * matches the name of any constraint in the parent. Originally this code
2473  * tried to compare the expression texts, but that can fail for various
2474  * reasons --- for example, if the parent and child tables are in different
2475  * schemas, reverse-listing of function calls may produce different text
2476  * (schema-qualified or not) depending on search path.
2477  *
2478  * In 8.4 and up we can rely on the conislocal field to decide which
2479  * constraints must be dumped; much safer.
2480  *
2481  * This function assumes all conislocal flags were initialized to true.
2482  * It clears the flag on anything that seems to be inherited.
2483  */
2484 static void
2486 {
2487  int i,
2488  j,
2489  k;
2490 
2491  for (i = 0; i < numTables; i++)
2492  {
2493  TableInfo *tbinfo = &(tblinfo[i]);
2494  int numParents;
2495  TableInfo **parents;
2496  TableInfo *parent;
2497 
2498  /* Sequences and views never have parents */
2499  if (tbinfo->relkind == RELKIND_SEQUENCE ||
2500  tbinfo->relkind == RELKIND_VIEW)
2501  continue;
2502 
2503  /* Don't bother computing anything for non-target tables, either */
2504  if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
2505  continue;
2506 
2507  numParents = tbinfo->numParents;
2508  parents = tbinfo->parents;
2509 
2510  if (numParents == 0)
2511  continue; /* nothing to see here, move along */
2512 
2513  /* scan for inherited CHECK constraints */
2514  for (j = 0; j < tbinfo->ncheck; j++)
2515  {
2516  ConstraintInfo *constr;
2517 
2518  constr = &(tbinfo->checkexprs[j]);
2519 
2520  for (k = 0; k < numParents; k++)
2521  {
2522  int l;
2523 
2524  parent = parents[k];
2525  for (l = 0; l < parent->ncheck; l++)
2526  {
2527  ConstraintInfo *pconstr = &(parent->checkexprs[l]);
2528 
2529  if (strcmp(pconstr->dobj.name, constr->dobj.name) == 0)
2530  {
2531  constr->conislocal = false;
2532  break;
2533  }
2534  }
2535  if (!constr->conislocal)
2536  break;
2537  }
2538  }
2539  }
2540 }
2541 
2542 
2543 /*
2544  * dumpDatabase:
2545  * dump the database definition
2546  */
2547 static void
2549 {
2550  DumpOptions *dopt = fout->dopt;
2551  PQExpBuffer dbQry = createPQExpBuffer();
2552  PQExpBuffer delQry = createPQExpBuffer();
2553  PQExpBuffer creaQry = createPQExpBuffer();
2554  PQExpBuffer labelq = createPQExpBuffer();
2555  PGconn *conn = GetConnection(fout);
2556  PGresult *res;
2557  int i_tableoid,
2558  i_oid,
2559  i_datname,
2560  i_dba,
2561  i_encoding,
2562  i_collate,
2563  i_ctype,
2564  i_frozenxid,
2565  i_minmxid,
2566  i_datacl,
2567  i_rdatacl,
2568  i_datistemplate,
2569  i_datconnlimit,
2570  i_tablespace;
2571  CatalogId dbCatId;
2572  DumpId dbDumpId;
2573  const char *datname,
2574  *dba,
2575  *encoding,
2576  *collate,
2577  *ctype,
2578  *datacl,
2579  *rdatacl,
2580  *datistemplate,
2581  *datconnlimit,
2582  *tablespace;
2583  uint32 frozenxid,
2584  minmxid;
2585  char *qdatname;
2586 
2587  if (g_verbose)
2588  write_msg(NULL, "saving database definition\n");
2589 
2590  /* Fetch the database-level properties for this database */
2591  if (fout->remoteVersion >= 90600)
2592  {
2593  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2594  "(%s datdba) AS dba, "
2595  "pg_encoding_to_char(encoding) AS encoding, "
2596  "datcollate, datctype, datfrozenxid, datminmxid, "
2597  "(SELECT array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
2598  " SELECT unnest(coalesce(datacl,acldefault('d',datdba))) AS acl "
2599  " EXCEPT SELECT unnest(acldefault('d',datdba))) as datacls)"
2600  " AS datacl, "
2601  "(SELECT array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
2602  " SELECT unnest(acldefault('d',datdba)) AS acl "
2603  " EXCEPT SELECT unnest(coalesce(datacl,acldefault('d',datdba)))) as rdatacls)"
2604  " AS rdatacl, "
2605  "datistemplate, datconnlimit, "
2606  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2607  "shobj_description(oid, 'pg_database') AS description "
2608 
2609  "FROM pg_database "
2610  "WHERE datname = current_database()",
2612  }
2613  else if (fout->remoteVersion >= 90300)
2614  {
2615  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2616  "(%s datdba) AS dba, "
2617  "pg_encoding_to_char(encoding) AS encoding, "
2618  "datcollate, datctype, datfrozenxid, datminmxid, "
2619  "datacl, '' as rdatacl, datistemplate, datconnlimit, "
2620  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2621  "shobj_description(oid, 'pg_database') AS description "
2622 
2623  "FROM pg_database "
2624  "WHERE datname = current_database()",
2626  }
2627  else if (fout->remoteVersion >= 80400)
2628  {
2629  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2630  "(%s datdba) AS dba, "
2631  "pg_encoding_to_char(encoding) AS encoding, "
2632  "datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
2633  "datacl, '' as rdatacl, datistemplate, datconnlimit, "
2634  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2635  "shobj_description(oid, 'pg_database') AS description "
2636 
2637  "FROM pg_database "
2638  "WHERE datname = current_database()",
2640  }
2641  else if (fout->remoteVersion >= 80200)
2642  {
2643  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2644  "(%s datdba) AS dba, "
2645  "pg_encoding_to_char(encoding) AS encoding, "
2646  "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2647  "datacl, '' as rdatacl, datistemplate, datconnlimit, "
2648  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2649  "shobj_description(oid, 'pg_database') AS description "
2650 
2651  "FROM pg_database "
2652  "WHERE datname = current_database()",
2654  }
2655  else
2656  {
2657  appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2658  "(%s datdba) AS dba, "
2659  "pg_encoding_to_char(encoding) AS encoding, "
2660  "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2661  "datacl, '' as rdatacl, datistemplate, "
2662  "-1 as datconnlimit, "
2663  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
2664  "FROM pg_database "
2665  "WHERE datname = current_database()",
2667  }
2668 
2669  res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
2670 
2671  i_tableoid = PQfnumber(res, "tableoid");
2672  i_oid = PQfnumber(res, "oid");
2673  i_datname = PQfnumber(res, "datname");
2674  i_dba = PQfnumber(res, "dba");
2675  i_encoding = PQfnumber(res, "encoding");
2676  i_collate = PQfnumber(res, "datcollate");
2677  i_ctype = PQfnumber(res, "datctype");
2678  i_frozenxid = PQfnumber(res, "datfrozenxid");
2679  i_minmxid = PQfnumber(res, "datminmxid");
2680  i_datacl = PQfnumber(res, "datacl");
2681  i_rdatacl = PQfnumber(res, "rdatacl");
2682  i_datistemplate = PQfnumber(res, "datistemplate");
2683  i_datconnlimit = PQfnumber(res, "datconnlimit");
2684  i_tablespace = PQfnumber(res, "tablespace");
2685 
2686  dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
2687  dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2688  datname = PQgetvalue(res, 0, i_datname);
2689  dba = PQgetvalue(res, 0, i_dba);
2690  encoding = PQgetvalue(res, 0, i_encoding);
2691  collate = PQgetvalue(res, 0, i_collate);
2692  ctype = PQgetvalue(res, 0, i_ctype);
2693  frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
2694  minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
2695  datacl = PQgetvalue(res, 0, i_datacl);
2696  rdatacl = PQgetvalue(res, 0, i_rdatacl);
2697  datistemplate = PQgetvalue(res, 0, i_datistemplate);
2698  datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
2699  tablespace = PQgetvalue(res, 0, i_tablespace);
2700 
2701  qdatname = pg_strdup(fmtId(datname));
2702 
2703  /*
2704  * Prepare the CREATE DATABASE command. We must specify encoding, locale,
2705  * and tablespace since those can't be altered later. Other DB properties
2706  * are left to the DATABASE PROPERTIES entry, so that they can be applied
2707  * after reconnecting to the target DB.
2708  */
2709  appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
2710  qdatname);
2711  if (strlen(encoding) > 0)
2712  {
2713  appendPQExpBufferStr(creaQry, " ENCODING = ");
2714  appendStringLiteralAH(creaQry, encoding, fout);
2715  }
2716  if (strlen(collate) > 0)
2717  {
2718  appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
2719  appendStringLiteralAH(creaQry, collate, fout);
2720  }
2721  if (strlen(ctype) > 0)
2722  {
2723  appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
2724  appendStringLiteralAH(creaQry, ctype, fout);
2725  }
2726 
2727  /*
2728  * Note: looking at dopt->outputNoTablespaces here is completely the wrong
2729  * thing; the decision whether to specify a tablespace should be left till
2730  * pg_restore, so that pg_restore --no-tablespaces applies. Ideally we'd
2731  * label the DATABASE entry with the tablespace and let the normal
2732  * tablespace selection logic work ... but CREATE DATABASE doesn't pay
2733  * attention to default_tablespace, so that won't work.
2734  */
2735  if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
2736  !dopt->outputNoTablespaces)
2737  appendPQExpBuffer(creaQry, " TABLESPACE = %s",
2738  fmtId(tablespace));
2739  appendPQExpBufferStr(creaQry, ";\n");
2740 
2741  appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
2742  qdatname);
2743 
2744  dbDumpId = createDumpId();
2745 
2746  ArchiveEntry(fout,
2747  dbCatId, /* catalog ID */
2748  dbDumpId, /* dump ID */
2749  datname, /* Name */
2750  NULL, /* Namespace */
2751  NULL, /* Tablespace */
2752  dba, /* Owner */
2753  false, /* with oids */
2754  "DATABASE", /* Desc */
2755  SECTION_PRE_DATA, /* Section */
2756  creaQry->data, /* Create */
2757  delQry->data, /* Del */
2758  NULL, /* Copy */
2759  NULL, /* Deps */
2760  0, /* # Deps */
2761  NULL, /* Dumper */
2762  NULL); /* Dumper Arg */
2763 
2764  /* Compute correct tag for archive entry */
2765  appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
2766 
2767  /* Dump DB comment if any */
2768  if (fout->remoteVersion >= 80200)
2769  {
2770  /*
2771  * 8.2 and up keep comments on shared objects in a shared table, so we
2772  * cannot use the dumpComment() code used for other database objects.
2773  * Be careful that the ArchiveEntry parameters match that function.
2774  */
2775  char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
2776 
2777  if (comment && *comment && !dopt->no_comments)
2778  {
2779  resetPQExpBuffer(dbQry);
2780 
2781  /*
2782  * Generates warning when loaded into a differently-named
2783  * database.
2784  */
2785  appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
2786  appendStringLiteralAH(dbQry, comment, fout);
2787  appendPQExpBufferStr(dbQry, ";\n");
2788 
2789  ArchiveEntry(fout, nilCatalogId, createDumpId(),
2790  labelq->data, NULL, NULL, dba,
2791  false, "COMMENT", SECTION_NONE,
2792  dbQry->data, "", NULL,
2793  &(dbDumpId), 1,
2794  NULL, NULL);
2795  }
2796  }
2797  else
2798  {
2799  dumpComment(fout, "DATABASE", qdatname, NULL, dba,
2800  dbCatId, 0, dbDumpId);
2801  }
2802 
2803  /* Dump DB security label, if enabled */
2804  if (!dopt->no_security_labels && fout->remoteVersion >= 90200)
2805  {
2806  PGresult *shres;
2807  PQExpBuffer seclabelQry;
2808 
2809  seclabelQry = createPQExpBuffer();
2810 
2811  buildShSecLabelQuery(conn, "pg_database", dbCatId.oid, seclabelQry);
2812  shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
2813  resetPQExpBuffer(seclabelQry);
2814  emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
2815  if (seclabelQry->len > 0)
2816  ArchiveEntry(fout, nilCatalogId, createDumpId(),
2817  labelq->data, NULL, NULL, dba,
2818  false, "SECURITY LABEL", SECTION_NONE,
2819  seclabelQry->data, "", NULL,
2820  &(dbDumpId), 1,
2821  NULL, NULL);
2822  destroyPQExpBuffer(seclabelQry);
2823  PQclear(shres);
2824  }
2825 
2826  /*
2827  * Dump ACL if any. Note that we do not support initial privileges
2828  * (pg_init_privs) on databases.
2829  */
2830  dumpACL(fout, dbCatId, dbDumpId, "DATABASE",
2831  qdatname, NULL, NULL,
2832  dba, datacl, rdatacl, "", "");
2833 
2834  /*
2835  * Now construct a DATABASE PROPERTIES archive entry to restore any
2836  * non-default database-level properties. (The reason this must be
2837  * separate is that we cannot put any additional commands into the TOC
2838  * entry that has CREATE DATABASE. pg_restore would execute such a group
2839  * in an implicit transaction block, and the backend won't allow CREATE
2840  * DATABASE in that context.)
2841  */
2842  resetPQExpBuffer(creaQry);
2843  resetPQExpBuffer(delQry);
2844 
2845  if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
2846  appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
2847  qdatname, datconnlimit);
2848 
2849  if (strcmp(datistemplate, "t") == 0)
2850  {
2851  appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
2852  qdatname);
2853 
2854  /*
2855  * The backend won't accept DROP DATABASE on a template database. We
2856  * can deal with that by removing the template marking before the DROP
2857  * gets issued. We'd prefer to use ALTER DATABASE IF EXISTS here, but
2858  * since no such command is currently supported, fake it with a direct
2859  * UPDATE on pg_database.
2860  */
2861  appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
2862  "SET datistemplate = false WHERE datname = ");
2863  appendStringLiteralAH(delQry, datname, fout);
2864  appendPQExpBufferStr(delQry, ";\n");
2865  }
2866 
2867  /* Add database-specific SET options */
2868  dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
2869 
2870  /*
2871  * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
2872  * entry, too, for lack of a better place.
2873  */
2874  if (dopt->binary_upgrade)
2875  {
2876  appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
2877  appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
2878  "SET datfrozenxid = '%u', datminmxid = '%u'\n"
2879  "WHERE datname = ",
2880  frozenxid, minmxid);
2881  appendStringLiteralAH(creaQry, datname, fout);
2882  appendPQExpBufferStr(creaQry, ";\n");
2883  }
2884 
2885  if (creaQry->len > 0)
2886  ArchiveEntry(fout, nilCatalogId, createDumpId(),
2887  datname, NULL, NULL, dba,
2888  false, "DATABASE PROPERTIES", SECTION_PRE_DATA,
2889  creaQry->data, delQry->data, NULL,
2890  &(dbDumpId), 1,
2891  NULL, NULL);
2892 
2893  /*
2894  * pg_largeobject and pg_largeobject_metadata come from the old system
2895  * intact, so set their relfrozenxids and relminmxids.
2896  */
2897  if (dopt->binary_upgrade)
2898  {
2899  PGresult *lo_res;
2900  PQExpBuffer loFrozenQry = createPQExpBuffer();
2901  PQExpBuffer loOutQry = createPQExpBuffer();
2902  int i_relfrozenxid,
2903  i_relminmxid;
2904 
2905  /*
2906  * pg_largeobject
2907  */
2908  if (fout->remoteVersion >= 90300)
2909  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid\n"
2910  "FROM pg_catalog.pg_class\n"
2911  "WHERE oid = %u;\n",
2912  LargeObjectRelationId);
2913  else
2914  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid\n"
2915  "FROM pg_catalog.pg_class\n"
2916  "WHERE oid = %u;\n",
2917  LargeObjectRelationId);
2918 
2919  lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2920 
2921  i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2922  i_relminmxid = PQfnumber(lo_res, "relminmxid");
2923 
2924  appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
2925  appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2926  "SET relfrozenxid = '%u', relminmxid = '%u'\n"
2927  "WHERE oid = %u;\n",
2928  atooid(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2929  atooid(PQgetvalue(lo_res, 0, i_relminmxid)),
2930  LargeObjectRelationId);
2931  ArchiveEntry(fout, nilCatalogId, createDumpId(),
2932  "pg_largeobject", NULL, NULL, "",
2933  false, "pg_largeobject", SECTION_PRE_DATA,
2934  loOutQry->data, "", NULL,
2935  NULL, 0,
2936  NULL, NULL);
2937 
2938  PQclear(lo_res);
2939 
2940  /*
2941  * pg_largeobject_metadata
2942  */
2943  if (fout->remoteVersion >= 90000)
2944  {
2945  resetPQExpBuffer(loFrozenQry);
2946  resetPQExpBuffer(loOutQry);
2947 
2948  if (fout->remoteVersion >= 90300)
2949  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid\n"
2950  "FROM pg_catalog.pg_class\n"
2951  "WHERE oid = %u;\n",
2952  LargeObjectMetadataRelationId);
2953  else
2954  appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid\n"
2955  "FROM pg_catalog.pg_class\n"
2956  "WHERE oid = %u;\n",
2957  LargeObjectMetadataRelationId);
2958 
2959  lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2960 
2961  i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2962  i_relminmxid = PQfnumber(lo_res, "relminmxid");
2963 
2964  appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n");
2965  appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2966  "SET relfrozenxid = '%u', relminmxid = '%u'\n"
2967  "WHERE oid = %u;\n",
2968  atooid(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2969  atooid(PQgetvalue(lo_res, 0, i_relminmxid)),
2970  LargeObjectMetadataRelationId);
2971  ArchiveEntry(fout, nilCatalogId, createDumpId(),
2972  "pg_largeobject_metadata", NULL, NULL, "",
2973  false, "pg_largeobject_metadata", SECTION_PRE_DATA,
2974  loOutQry->data, "", NULL,
2975  NULL, 0,
2976  NULL, NULL);
2977 
2978  PQclear(lo_res);
2979  }
2980 
2981  destroyPQExpBuffer(loFrozenQry);
2982  destroyPQExpBuffer(loOutQry);
2983  }
2984 
2985  PQclear(res);
2986 
2987  free(qdatname);
2988  destroyPQExpBuffer(dbQry);
2989  destroyPQExpBuffer(delQry);
2990  destroyPQExpBuffer(creaQry);
2991  destroyPQExpBuffer(labelq);
2992 }
2993 
2994 /*
2995  * Collect any database-specific or role-and-database-specific SET options
2996  * for this database, and append them to outbuf.
2997  */
2998 static void
3000  const char *dbname, Oid dboid)
3001 {
3002  PGconn *conn = GetConnection(AH);
3004  PGresult *res;
3005  int count = 1;
3006 
3007  /*
3008  * First collect database-specific options. Pre-8.4 server versions lack
3009  * unnest(), so we do this the hard way by querying once per subscript.
3010  */
3011  for (;;)
3012  {
3013  if (AH->remoteVersion >= 90000)
3014  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting "
3015  "WHERE setrole = 0 AND setdatabase = '%u'::oid",
3016  count, dboid);
3017  else
3018  printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE oid = '%u'::oid", count, dboid);
3019 
3020  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3021 
3022  if (PQntuples(res) == 1 &&
3023  !PQgetisnull(res, 0, 0))
3024  {
3025  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
3026  "DATABASE", dbname, NULL, NULL,
3027  outbuf);
3028  PQclear(res);
3029  count++;
3030  }
3031  else
3032  {
3033  PQclear(res);
3034  break;
3035  }
3036  }
3037 
3038  /* Now look for role-and-database-specific options */
3039  if (AH->remoteVersion >= 90000)
3040  {
3041  /* Here we can assume we have unnest() */
3042  printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3043  "FROM pg_db_role_setting s, pg_roles r "
3044  "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3045  dboid);
3046 
3047  res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3048 
3049  if (PQntuples(res) > 0)
3050  {
3051  int i;
3052 
3053  for (i = 0; i < PQntuples(res); i++)
3054  makeAlterConfigCommand(conn, PQgetvalue(res, i, 1),
3055  "ROLE", PQgetvalue(res, i, 0),
3056  "DATABASE", dbname,
3057  outbuf);
3058  }
3059 
3060  PQclear(res);
3061  }
3062 
3063  destroyPQExpBuffer(buf);
3064 }
3065 
3066 /*
3067  * dumpEncoding: put the correct encoding into the archive
3068  */
3069 static void
3071 {
3072  const char *encname = pg_encoding_to_char(AH->encoding);
3074 
3075  if (g_verbose)
3076  write_msg(NULL, "saving encoding = %s\n", encname);
3077 
3078  appendPQExpBufferStr(qry, "SET client_encoding = ");
3079  appendStringLiteralAH(qry, encname, AH);
3080  appendPQExpBufferStr(qry, ";\n");
3081 
3082  ArchiveEntry(AH, nilCatalogId, createDumpId(),
3083  "ENCODING", NULL, NULL, "",
3084  false, "ENCODING", SECTION_PRE_DATA,
3085  qry->data, "", NULL,
3086  NULL, 0,
3087  NULL, NULL);
3088 
3089  destroyPQExpBuffer(qry);
3090 }
3091 
3092 
3093 /*
3094  * dumpStdStrings: put the correct escape string behavior into the archive
3095  */
3096 static void
3098 {
3099  const char *stdstrings = AH->std_strings ? "on" : "off";
3101 
3102  if (g_verbose)
3103  write_msg(NULL, "saving standard_conforming_strings = %s\n",
3104  stdstrings);
3105 
3106  appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
3107  stdstrings);
3108 
3109  ArchiveEntry(AH, nilCatalogId, createDumpId(),
3110  "STDSTRINGS", NULL, NULL, "",
3111  false, "STDSTRINGS", SECTION_PRE_DATA,
3112  qry->data, "", NULL,
3113  NULL, 0,
3114  NULL, NULL);
3115 
3116  destroyPQExpBuffer(qry);
3117 }
3118 
3119 /*
3120  * dumpSearchPath: record the active search_path in the archive
3121  */
3122 static void
3124 {
3126  PQExpBuffer path = createPQExpBuffer();
3127  PGresult *res;
3128  char **schemanames = NULL;
3129  int nschemanames = 0;
3130  int i;
3131 
3132  /*
3133  * We use the result of current_schemas(), not the search_path GUC,
3134  * because that might contain wildcards such as "$user", which won't
3135  * necessarily have the same value during restore. Also, this way avoids
3136  * listing schemas that may appear in search_path but not actually exist,
3137  * which seems like a prudent exclusion.
3138  */
3139  res = ExecuteSqlQueryForSingleRow(AH,
3140  "SELECT pg_catalog.current_schemas(false)");
3141 
3142  if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames))
3143  exit_horribly(NULL, "could not parse result of current_schemas()\n");
3144 
3145  /*
3146  * We use set_config(), not a simple "SET search_path" command, because
3147  * the latter has less-clean behavior if the search path is empty. While
3148  * that's likely to get fixed at some point, it seems like a good idea to
3149  * be as backwards-compatible as possible in what we put into archives.
3150  */
3151  for (i = 0; i < nschemanames; i++)
3152  {
3153  if (i > 0)
3154  appendPQExpBufferStr(path, ", ");
3155  appendPQExpBufferStr(path, fmtId(schemanames[i]));
3156  }
3157 
3158  appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', ");
3159  appendStringLiteralAH(qry, path->data, AH);
3160  appendPQExpBufferStr(qry, ", false);\n");
3161 
3162  if (g_verbose)
3163  write_msg(NULL, "saving search_path = %s\n", path->data);
3164 
3165  ArchiveEntry(AH, nilCatalogId, createDumpId(),
3166  "SEARCHPATH", NULL, NULL, "",
3167  false, "SEARCHPATH", SECTION_PRE_DATA,
3168  qry->data, "", NULL,
3169  NULL, 0,
3170  NULL, NULL);
3171 
3172  /* Also save it in AH->searchpath, in case we're doing plain text dump */
3173  AH->searchpath = pg_strdup(qry->data);
3174 
3175  if (schemanames)
3176  free(schemanames);
3177  PQclear(res);
3178  destroyPQExpBuffer(qry);
3179  destroyPQExpBuffer(path);
3180 }
3181 
3182 
3183 /*
3184  * getBlobs:
3185  * Collect schema-level data about large objects
3186  */
3187 static void
3189 {
3190  DumpOptions *dopt = fout->dopt;
3191  PQExpBuffer blobQry = createPQExpBuffer();
3192  BlobInfo *binfo;
3193  DumpableObject *bdata;
3194  PGresult *res;
3195  int ntups;
3196  int i;
3197  int i_oid;
3198  int i_lomowner;
3199  int i_lomacl;
3200  int i_rlomacl;
3201  int i_initlomacl;
3202  int i_initrlomacl;
3203 
3204  /* Verbose message */
3205  if (g_verbose)
3206  write_msg(NULL, "reading large objects\n");
3207 
3208  /* Fetch BLOB OIDs, and owner/ACL data if >= 9.0 */
3209  if (fout->remoteVersion >= 90600)
3210  {
3211  PQExpBuffer acl_subquery = createPQExpBuffer();
3212  PQExpBuffer racl_subquery = createPQExpBuffer();
3213  PQExpBuffer init_acl_subquery = createPQExpBuffer();
3214  PQExpBuffer init_racl_subquery = createPQExpBuffer();
3215 
3216  buildACLQueries(acl_subquery, racl_subquery, init_acl_subquery,
3217  init_racl_subquery, "l.lomacl", "l.lomowner", "'L'",
3218  dopt->binary_upgrade);
3219 
3220  appendPQExpBuffer(blobQry,
3221  "SELECT l.oid, (%s l.lomowner) AS rolname, "
3222  "%s AS lomacl, "
3223  "%s AS rlomacl, "
3224  "%s AS initlomacl, "
3225  "%s AS initrlomacl "
3226  "FROM pg_largeobject_metadata l "
3227  "LEFT JOIN pg_init_privs pip ON "
3228  "(l.oid = pip.objoid "
3229  "AND pip.classoid = 'pg_largeobject'::regclass "
3230  "AND pip.objsubid = 0) ",
3232  acl_subquery->data,
3233  racl_subquery->data,
3234  init_acl_subquery->data,
3235  init_racl_subquery->data);
3236 
3237  destroyPQExpBuffer(acl_subquery);
3238  destroyPQExpBuffer(racl_subquery);
3239  destroyPQExpBuffer(init_acl_subquery);
3240  destroyPQExpBuffer(init_racl_subquery);
3241  }
3242  else if (fout->remoteVersion >= 90000)
3243  appendPQExpBuffer(blobQry,
3244  "SELECT oid, (%s lomowner) AS rolname, lomacl, "
3245  "NULL AS rlomacl, NULL AS initlomacl, "
3246  "NULL AS initrlomacl "
3247  " FROM pg_largeobject_metadata",
3249  else
3250  appendPQExpBufferStr(blobQry,
3251  "SELECT DISTINCT loid AS oid, "
3252  "NULL::name AS rolname, NULL::oid AS lomacl, "
3253  "NULL::oid AS rlomacl, NULL::oid AS initlomacl, "
3254  "NULL::oid AS initrlomacl "
3255  " FROM pg_largeobject");
3256 
3257  res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK);
3258 
3259  i_oid = PQfnumber(res, "oid");
3260  i_lomowner = PQfnumber(res, "rolname");
3261  i_lomacl = PQfnumber(res, "lomacl");
3262  i_rlomacl = PQfnumber(res, "rlomacl");
3263  i_initlomacl = PQfnumber(res, "initlomacl");
3264  i_initrlomacl = PQfnumber(res, "initrlomacl");
3265 
3266  ntups = PQntuples(res);
3267 
3268  /*
3269  * Each large object has its own BLOB archive entry.
3270  */
3271  binfo = (BlobInfo *) pg_malloc(ntups * sizeof(BlobInfo));
3272 
3273  for (i = 0; i < ntups; i++)
3274  {
3275  binfo[i].dobj.objType = DO_BLOB;
3276  binfo[i].dobj.catId.tableoid = LargeObjectRelationId;
3277  binfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3278  AssignDumpId(&binfo[i].dobj);
3279 
3280  binfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oid));
3281  binfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_lomowner));
3282  binfo[i].blobacl = pg_strdup(PQgetvalue(res, i, i_lomacl));
3283  binfo[i].rblobacl = pg_strdup(PQgetvalue(res, i, i_rlomacl));
3284  binfo[i].initblobacl = pg_strdup(PQgetvalue(res, i, i_initlomacl));
3285  binfo[i].initrblobacl = pg_strdup(PQgetvalue(res, i, i_initrlomacl));
3286 
3287  if (PQgetisnull(res, i, i_lomacl) &&
3288  PQgetisnull(res, i, i_rlomacl) &&
3289  PQgetisnull(res, i, i_initlomacl) &&
3290  PQgetisnull(res, i, i_initrlomacl))
3291  binfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
3292 
3293  /*
3294  * In binary-upgrade mode for blobs, we do *not* dump out the data or
3295  * the ACLs, should any exist. The data and ACL (if any) will be
3296  * copied by pg_upgrade, which simply copies the pg_largeobject and
3297  * pg_largeobject_metadata tables.
3298  *
3299  * We *do* dump out the definition of the blob because we need that to
3300  * make the restoration of the comments, and anything else, work since
3301  * pg_upgrade copies the files behind pg_largeobject and
3302  * pg_largeobject_metadata after the dump is restored.
3303  */
3304  if (dopt->binary_upgrade)
3306  }
3307 
3308  /*
3309  * If we have any large objects, a "BLOBS" archive entry is needed. This
3310  * is just a placeholder for sorting; it carries no data now.
3311  */
3312  if (ntups > 0)
3313  {
3314  bdata = (DumpableObject *) pg_malloc(sizeof(DumpableObject));
3315  bdata->objType = DO_BLOB_DATA;
3316  bdata->catId = nilCatalogId;
3317  AssignDumpId(bdata);
3318  bdata->name = pg_strdup("BLOBS");
3319  }
3320 
3321  PQclear(res);
3322  destroyPQExpBuffer(blobQry);
3323 }
3324 
3325 /*
3326  * dumpBlob
3327  *
3328  * dump the definition (metadata) of the given large object
3329  */
3330 static void
3331 dumpBlob(Archive *fout, BlobInfo *binfo)
3332 {
3333  PQExpBuffer cquery = createPQExpBuffer();
3334  PQExpBuffer dquery = createPQExpBuffer();
3335 
3336  appendPQExpBuffer(cquery,
3337  "SELECT pg_catalog.lo_create('%s');\n",
3338  binfo->dobj.name);
3339 
3340  appendPQExpBuffer(dquery,
3341  "SELECT pg_catalog.lo_unlink('%s');\n",
3342  binfo->dobj.name);
3343 
3344  if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3345  ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3346  binfo->dobj.name,
3347  NULL, NULL,
3348  binfo->rolname, false,
3349  "BLOB", SECTION_PRE_DATA,
3350  cquery->data, dquery->data, NULL,
3351  NULL, 0,
3352  NULL, NULL);
3353 
3354  /* Dump comment if any */
3355  if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3356  dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3357  NULL, binfo->rolname,
3358  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3359 
3360  /* Dump security label if any */
3361  if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3362  dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3363  NULL, binfo->rolname,
3364  binfo->dobj.catId, 0, binfo->dobj.dumpId);
3365 
3366  /* Dump ACL if any */
3367  if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3368  dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
3369  binfo->dobj.name, NULL,
3370  NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3371  binfo->initblobacl, binfo->initrblobacl);
3372 
3373  destroyPQExpBuffer(cquery);
3374  destroyPQExpBuffer(dquery);
3375 }
3376 
3377 /*
3378  * dumpBlobs:
3379  * dump the data contents of all large objects
3380  */
3381 static int
3382 dumpBlobs(Archive *fout, void *arg)
3383 {
3384  const char *blobQry;
3385  const char *blobFetchQry;
3386  PGconn *conn = GetConnection(fout);
3387  PGresult *res;
3388  char buf[LOBBUFSIZE];
3389  int ntups;
3390  int i;
3391  int cnt;
3392 
3393  if (g_verbose)
3394  write_msg(NULL, "saving large objects\n");
3395 
3396  /*
3397  * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
3398  * the already-in-memory dumpable objects instead...
3399  */
3400  if (fout->remoteVersion >= 90000)
3401  blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_largeobject_metadata";
3402  else
3403  blobQry = "DECLARE bloboid CURSOR FOR SELECT DISTINCT loid FROM pg_largeobject";
3404 
3405  ExecuteSqlStatement(fout, blobQry);
3406 
3407  /* Command to fetch from cursor */
3408  blobFetchQry = "FETCH 1000 IN bloboid";
3409 
3410  do
3411  {
3412  /* Do a fetch */
3413  res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3414 
3415  /* Process the tuples, if any */
3416  ntups = PQntuples(res);
3417  for (i = 0; i < ntups; i++)
3418  {
3419  Oid blobOid;
3420  int loFd;
3421 
3422  blobOid = atooid(PQgetvalue(res, i, 0));
3423  /* Open the BLOB */
3424  loFd = lo_open(conn, blobOid, INV_READ);
3425  if (loFd == -1)
3426  exit_horribly(NULL, "could not open large object %u: %s",
3427  blobOid, PQerrorMessage(conn));
3428 
3429  StartBlob(fout, blobOid);
3430 
3431  /* Now read it in chunks, sending data to archive */
3432  do
3433  {
3434  cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3435  if (cnt < 0)
3436  exit_horribly(NULL, "error reading large object %u: %s",
3437  blobOid, PQerrorMessage(conn));
3438 
3439  WriteData(fout, buf, cnt);
3440  } while (cnt > 0);
3441 
3442  lo_close(conn, loFd);
3443 
3444  EndBlob(fout, blobOid);
3445  }
3446 
3447  PQclear(res);
3448  } while (ntups > 0);
3449 
3450  return 1;
3451 }
3452 
3453 /*
3454  * getPolicies
3455  * get information about policies on a dumpable table.
3456  */
3457 void
3458 getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
3459 {
3460  PQExpBuffer query;
3461  PGresult *res;
3462  PolicyInfo *polinfo;
3463  int i_oid;
3464  int i_tableoid;
3465  int i_polname;
3466  int i_polcmd;
3467  int i_polpermissive;
3468  int i_polroles;
3469  int i_polqual;
3470  int i_polwithcheck;
3471  int i,
3472  j,
3473  ntups;
3474 
3475  if (fout->remoteVersion < 90500)
3476  return;
3477 
3478  query = createPQExpBuffer();
3479 
3480  for (i = 0; i < numTables; i++)
3481  {
3482  TableInfo *tbinfo = &tblinfo[i];
3483 
3484  /* Ignore row security on tables not to be dumped */
3485  if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
3486  continue;
3487 
3488  if (g_verbose)
3489  write_msg(NULL, "reading row security enabled for table \"%s.%s\"\n",
3490  tbinfo->dobj.namespace->dobj.name,
3491  tbinfo->dobj.name);
3492 
3493  /*
3494  * Get row security enabled information for the table. We represent
3495  * RLS being enabled on a table by creating a PolicyInfo object with
3496  * null polname.
3497  */
3498  if (tbinfo->rowsec)
3499  {
3500  /*
3501  * Note: use tableoid 0 so that this object won't be mistaken for
3502  * something that pg_depend entries apply to.
3503  */
3504  polinfo = pg_malloc(sizeof(PolicyInfo));
3505  polinfo->dobj.objType = DO_POLICY;
3506  polinfo->dobj.catId.tableoid = 0;
3507  polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
3508  AssignDumpId(&polinfo->dobj);
3509  polinfo->dobj.namespace = tbinfo->dobj.namespace;
3510  polinfo->dobj.name = pg_strdup(tbinfo->dobj.name);
3511  polinfo->poltable = tbinfo;
3512  polinfo->polname = NULL;
3513  polinfo->polcmd = '\0';
3514  polinfo->polpermissive = 0;
3515  polinfo->polroles = NULL;
3516  polinfo->polqual = NULL;
3517  polinfo->polwithcheck = NULL;
3518  }
3519 
3520  if (g_verbose)
3521  write_msg(NULL, "reading policies for table \"%s.%s\"\n",
3522  tbinfo->dobj.namespace->dobj.name,
3523  tbinfo->dobj.name);
3524 
3525  resetPQExpBuffer(query);
3526 
3527  /* Get the policies for the table. */
3528  if (fout->remoteVersion >= 100000)
3529  appendPQExpBuffer(query,
3530  "SELECT oid, tableoid, pol.polname, pol.polcmd, pol.polpermissive, "
3531  "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3532  " pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
3533  "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3534  "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3535  "FROM pg_catalog.pg_policy pol "
3536  "WHERE polrelid = '%u'",
3537  tbinfo->dobj.catId.oid);
3538  else
3539  appendPQExpBuffer(query,
3540  "SELECT oid, tableoid, pol.polname, pol.polcmd, 't' as polpermissive, "
3541  "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3542  " pg_catalog.array_to_string(ARRAY(SELECT pg_catalog.quote_ident(rolname) from pg_catalog.pg_roles WHERE oid = ANY(pol.polroles)), ', ') END AS polroles, "
3543  "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3544  "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3545  "FROM pg_catalog.pg_policy pol "
3546  "WHERE polrelid = '%u'",
3547  tbinfo->dobj.catId.oid);
3548  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3549 
3550  ntups = PQntuples(res);
3551 
3552  if (ntups == 0)
3553  {
3554  /*
3555  * No explicit policies to handle (only the default-deny policy,
3556  * which is handled as part of the table definition). Clean up
3557  * and return.
3558  */
3559  PQclear(res);
3560  continue;
3561  }
3562 
3563  i_oid = PQfnumber(res, "oid");
3564  i_tableoid = PQfnumber(res, "tableoid");
3565  i_polname = PQfnumber(res, "polname");
3566  i_polcmd = PQfnumber(res, "polcmd");
3567  i_polpermissive = PQfnumber(res, "polpermissive");
3568  i_polroles = PQfnumber(res, "polroles");
3569  i_polqual = PQfnumber(res, "polqual");
3570  i_polwithcheck = PQfnumber(res, "polwithcheck");
3571 
3572  polinfo = pg_malloc(ntups * sizeof(PolicyInfo));
3573 
3574  for (j = 0; j < ntups; j++)
3575  {
3576  polinfo[j].dobj.objType = DO_POLICY;
3577  polinfo[j].dobj.catId.tableoid =
3578  atooid(PQgetvalue(res, j, i_tableoid));
3579  polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
3580  AssignDumpId(&polinfo[j].dobj);
3581  polinfo[j].dobj.namespace = tbinfo->dobj.namespace;
3582  polinfo[j].poltable = tbinfo;
3583  polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname));
3584  polinfo[j].dobj.name = pg_strdup(polinfo[j].polname);
3585 
3586  polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd));
3587  polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't';
3588 
3589  if (PQgetisnull(res, j, i_polroles))
3590  polinfo[j].polroles = NULL;
3591  else
3592  polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles));
3593 
3594  if (PQgetisnull(res, j, i_polqual))
3595  polinfo[j].polqual = NULL;
3596  else
3597  polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual));
3598 
3599  if (PQgetisnull(res, j, i_polwithcheck))
3600  polinfo[j].polwithcheck = NULL;
3601  else
3602  polinfo[j].polwithcheck
3603  = pg_strdup(PQgetvalue(res, j, i_polwithcheck));
3604  }
3605  PQclear(res);
3606  }
3607  destroyPQExpBuffer(query);
3608 }
3609 
3610 /*
3611  * dumpPolicy
3612  * dump the definition of the given policy
3613  */
3614 static void
3616 {
3617  DumpOptions *dopt = fout->dopt;
3618  TableInfo *tbinfo = polinfo->poltable;
3619  PQExpBuffer query;
3620  PQExpBuffer delqry;
3621  const char *cmd;
3622  char *tag;
3623 
3624  if (dopt->dataOnly)
3625  return;
3626 
3627  /*
3628  * If polname is NULL, then this record is just indicating that ROW LEVEL
3629  * SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE
3630  * ROW LEVEL SECURITY.
3631  */
3632  if (polinfo->polname == NULL)
3633  {
3634  query = createPQExpBuffer();
3635 
3636  appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;",
3637  fmtQualifiedDumpable(tbinfo));
3638 
3639  /*
3640  * We must emit the ROW SECURITY object's dependency on its table
3641  * explicitly, because it will not match anything in pg_depend (unlike
3642  * the case for other PolicyInfo objects).
3643  */
3644  if (polinfo->dobj.dump & DUMP_COMPONENT_POLICY)
3645  ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
3646  polinfo->dobj.name,
3647  polinfo->dobj.namespace->dobj.name,
3648  NULL,
3649  tbinfo->rolname, false,
3650  "ROW SECURITY", SECTION_POST_DATA,
3651  query->data, "", NULL,
3652  &(tbinfo->dobj.dumpId), 1,
3653  NULL, NULL);
3654 
3655  destroyPQExpBuffer(query);
3656  return;
3657  }
3658 
3659  if (polinfo->polcmd == '*')
3660  cmd = "";
3661  else if (polinfo->polcmd == 'r')
3662  cmd = " FOR SELECT";
3663  else if (polinfo->polcmd == 'a')
3664  cmd = " FOR INSERT";
3665  else if (polinfo->polcmd == 'w')
3666  cmd = " FOR UPDATE";
3667  else if (polinfo->polcmd == 'd')
3668  cmd = " FOR DELETE";
3669  else
3670  {
3671  write_msg(NULL, "unexpected policy command type: %c\n",
3672  polinfo->polcmd);
3673  exit_nicely(1);
3674  }
3675 
3676  query = createPQExpBuffer();
3677  delqry = createPQExpBuffer();
3678 
3679  appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname));
3680 
3681  appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo),
3682  !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd);
3683 
3684  if (polinfo->polroles != NULL)
3685  appendPQExpBuffer(query, " TO %s", polinfo->polroles);
3686 
3687  if (polinfo->polqual != NULL)
3688  appendPQExpBuffer(query, " USING (%s)", polinfo->polqual);
3689 
3690  if (polinfo->polwithcheck != NULL)
3691  appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck);
3692 
3693  appendPQExpBuffer(query, ";\n");
3694 
3695  appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname));
3696  appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo));
3697 
3698  tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
3699 
3700  if (polinfo->dobj.dump & DUMP_COMPONENT_POLICY)
3701  ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
3702  tag,
3703  polinfo->dobj.namespace->dobj.name,
3704  NULL,
3705  tbinfo->rolname, false,
3706  "POLICY", SECTION_POST_DATA,
3707  query->data, delqry->data, NULL,
3708  NULL, 0,
3709  NULL, NULL);
3710 
3711  free(tag);
3712  destroyPQExpBuffer(query);
3713  destroyPQExpBuffer(delqry);
3714 }
3715 
3716 /*
3717  * getPublications
3718  * get information about publications
3719  */
3720 void
3722 {
3723  DumpOptions *dopt = fout->dopt;
3724  PQExpBuffer query;
3725  PGresult *res;
3726  PublicationInfo *pubinfo;
3727  int i_tableoid;
3728  int i_oid;
3729  int i_pubname;
3730  int i_rolname;
3731  int i_puballtables;
3732  int i_pubinsert;
3733  int i_pubupdate;
3734  int i_pubdelete;
3735  int i_pubtruncate;
3736  int i,
3737  ntups;
3738 
3739  if (dopt->no_publications || fout->remoteVersion < 100000)
3740  return;
3741 
3742  query = createPQExpBuffer();
3743 
3744  resetPQExpBuffer(query);
3745 
3746  /* Get the publications. */
3747  if (fout->remoteVersion >= 110000)
3748  appendPQExpBuffer(query,
3749  "SELECT p.tableoid, p.oid, p.pubname, "
3750  "(%s p.pubowner) AS rolname, "
3751  "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate "
3752  "FROM pg_publication p",
3754  else
3755  appendPQExpBuffer(query,
3756  "SELECT p.tableoid, p.oid, p.pubname, "
3757  "(%s p.pubowner) AS rolname, "
3758  "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, false AS pubtruncate "
3759  "FROM pg_publication p",
3761 
3762  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3763 
3764  ntups = PQntuples(res);
3765 
3766  i_tableoid = PQfnumber(res, "tableoid");
3767  i_oid = PQfnumber(res, "oid");
3768  i_pubname = PQfnumber(res, "pubname");
3769  i_rolname = PQfnumber(res, "rolname");
3770  i_puballtables = PQfnumber(res, "puballtables");
3771  i_pubinsert = PQfnumber(res, "pubinsert");
3772  i_pubupdate = PQfnumber(res, "pubupdate");
3773  i_pubdelete = PQfnumber(res, "pubdelete");
3774  i_pubtruncate = PQfnumber(res, "pubtruncate");
3775 
3776  pubinfo = pg_malloc(ntups * sizeof(PublicationInfo));
3777 
3778  for (i = 0; i < ntups; i++)
3779  {
3780  pubinfo[i].dobj.objType = DO_PUBLICATION;
3781  pubinfo[i].dobj.catId.tableoid =
3782  atooid(PQgetvalue(res, i, i_tableoid));
3783  pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3784  AssignDumpId(&pubinfo[i].dobj);
3785  pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname));
3786  pubinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3787  pubinfo[i].puballtables =
3788  (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0);
3789  pubinfo[i].pubinsert =
3790  (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0);
3791  pubinfo[i].pubupdate =
3792  (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0);
3793  pubinfo[i].pubdelete =
3794  (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
3795  pubinfo[i].pubtruncate =
3796  (strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0);
3797 
3798  if (strlen(pubinfo[i].rolname) == 0)
3799  write_msg(NULL, "WARNING: owner of publication \"%s\" appears to be invalid\n",
3800  pubinfo[i].dobj.name);
3801 
3802  /* Decide whether we want to dump it */
3803  selectDumpableObject(&(pubinfo[i].dobj), fout);
3804  }
3805  PQclear(res);
3806 
3807  destroyPQExpBuffer(query);
3808 }
3809 
3810 /*
3811  * dumpPublication
3812  * dump the definition of the given publication
3813  */
3814 static void
3816 {
3817  PQExpBuffer delq;
3818  PQExpBuffer query;
3819  char *qpubname;
3820  bool first = true;
3821 
3822  if (!(pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3823  return;
3824 
3825  delq = createPQExpBuffer();
3826  query = createPQExpBuffer();
3827 
3828  qpubname = pg_strdup(fmtId(pubinfo->dobj.name));
3829 
3830  appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n",
3831  qpubname);
3832 
3833  appendPQExpBuffer(query, "CREATE PUBLICATION %s",
3834  qpubname);
3835 
3836  if (pubinfo->puballtables)
3837  appendPQExpBufferStr(query, " FOR ALL TABLES");
3838 
3839  appendPQExpBufferStr(query, " WITH (publish = '");
3840  if (pubinfo->pubinsert)
3841  {
3842  appendPQExpBufferStr(query, "insert");
3843  first = false;
3844  }
3845 
3846  if (pubinfo->pubupdate)
3847  {
3848  if (!first)
3849  appendPQExpBufferStr(query, ", ");
3850 
3851  appendPQExpBufferStr(query, "update");
3852  first = false;
3853  }
3854 
3855  if (pubinfo->pubdelete)
3856  {
3857  if (!first)
3858  appendPQExpBufferStr(query, ", ");
3859 
3860  appendPQExpBufferStr(query, "delete");
3861  first = false;
3862  }
3863 
3864  if (pubinfo->pubtruncate)
3865  {
3866  if (!first)
3867  appendPQExpBufferStr(query, ", ");
3868 
3869  appendPQExpBufferStr(query, "truncate");
3870  first = false;
3871  }
3872 
3873  appendPQExpBufferStr(query, "');\n");
3874 
3875  ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
3876  pubinfo->dobj.name,
3877  NULL,
3878  NULL,
3879  pubinfo->rolname, false,
3880  "PUBLICATION", SECTION_POST_DATA,
3881  query->data, delq->data, NULL,
3882  NULL, 0,
3883  NULL, NULL);
3884 
3885  if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3886  dumpComment(fout, "PUBLICATION", qpubname,
3887  NULL, pubinfo->rolname,
3888  pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
3889 
3890  if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3891  dumpSecLabel(fout, "PUBLICATION", qpubname,
3892  NULL, pubinfo->rolname,
3893  pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
3894 
3895  destroyPQExpBuffer(delq);
3896  destroyPQExpBuffer(query);
3897  free(qpubname);
3898 }
3899 
3900 /*
3901  * getPublicationTables
3902  * get information about publication membership for dumpable tables.
3903  */
3904 void
3906 {
3907  PQExpBuffer query;
3908  PGresult *res;
3909  PublicationRelInfo *pubrinfo;
3910  DumpOptions *dopt = fout->dopt;
3911  int i_tableoid;
3912  int i_oid;
3913  int i_pubname;
3914  int i,
3915  j,
3916  ntups;
3917 
3918  if (dopt->no_publications || fout->remoteVersion < 100000)
3919  return;
3920 
3921  query = createPQExpBuffer();
3922 
3923  for (i = 0; i < numTables; i++)
3924  {
3925  TableInfo *tbinfo = &tblinfo[i];
3926 
3927  /* Only plain tables can be aded to publications. */
3928  if (tbinfo->relkind != RELKIND_RELATION)
3929  continue;
3930 
3931  /*
3932  * Ignore publication membership of tables whose definitions are not
3933  * to be dumped.
3934  */
3935  if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3936  continue;
3937 
3938  if (g_verbose)
3939  write_msg(NULL, "reading publication membership for table \"%s.%s\"\n",
3940  tbinfo->dobj.namespace->dobj.name,
3941  tbinfo->dobj.name);
3942 
3943  resetPQExpBuffer(query);
3944 
3945  /* Get the publication membership for the table. */
3946  appendPQExpBuffer(query,
3947  "SELECT pr.tableoid, pr.oid, p.pubname "
3948  "FROM pg_publication_rel pr, pg_publication p "
3949  "WHERE pr.prrelid = '%u'"
3950  " AND p.oid = pr.prpubid",
3951  tbinfo->dobj.catId.oid);
3952  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3953 
3954  ntups = PQntuples(res);
3955 
3956  if (ntups == 0)
3957  {
3958  /*
3959  * Table is not member of any publications. Clean up and return.
3960  */
3961  PQclear(res);
3962  continue;
3963  }
3964 
3965  i_tableoid = PQfnumber(res, "tableoid");
3966  i_oid = PQfnumber(res, "oid");
3967  i_pubname = PQfnumber(res, "pubname");
3968 
3969  pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo));
3970 
3971  for (j = 0; j < ntups; j++)
3972  {
3973  pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
3974  pubrinfo[j].dobj.catId.tableoid =
3975  atooid(PQgetvalue(res, j, i_tableoid));
3976  pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
3977  AssignDumpId(&pubrinfo[j].dobj);
3978  pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
3979  pubrinfo[j].dobj.name = tbinfo->dobj.name;
3980  pubrinfo[j].pubname = pg_strdup(PQgetvalue(res, j, i_pubname));
3981  pubrinfo[j].pubtable = tbinfo;
3982 
3983  /* Decide whether we want to dump it */
3984  selectDumpablePublicationTable(&(pubrinfo[j].dobj), fout);
3985  }
3986  PQclear(res);
3987  }
3988  destroyPQExpBuffer(query);
3989 }
3990 
3991 /*
3992  * dumpPublicationTable
3993  * dump the definition of the given publication table mapping
3994  */
3995 static void
3997 {
3998  TableInfo *tbinfo = pubrinfo->pubtable;
3999  PQExpBuffer query;
4000  char *tag;
4001 
4002  if (!(pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
4003  return;
4004 
4005  tag = psprintf("%s %s", pubrinfo->pubname, tbinfo->dobj.name);
4006 
4007  query = createPQExpBuffer();
4008 
4009  appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
4010  fmtId(pubrinfo->pubname));
4011  appendPQExpBuffer(query, " %s;\n",
4012  fmtQualifiedDumpable(tbinfo));
4013 
4014  /*
4015  * There is no point in creating drop query as drop query as the drop is
4016  * done by table drop.
4017  */
4018  ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
4019  tag,
4020  tbinfo->dobj.namespace->dobj.name,
4021  NULL,
4022  "", false,
4023  "PUBLICATION TABLE", SECTION_POST_DATA,
4024  query->data, "", NULL,
4025  NULL, 0,
4026  NULL, NULL);
4027 
4028  free(tag);
4029  destroyPQExpBuffer(query);
4030 }
4031 
4032 /*
4033  * Is the currently connected user a superuser?
4034  */
4035 static bool
4037 {
4038  ArchiveHandle *AH = (ArchiveHandle *) fout;
4039  const char *val;
4040 
4041  val = PQparameterStatus(AH->connection, "is_superuser");
4042 
4043  if (val && strcmp(val, "on") == 0)
4044  return true;
4045 
4046  return false;
4047 }
4048 
4049 /*
4050  * getSubscriptions
4051  * get information about subscriptions
4052  */
4053 void
4055 {
4056  DumpOptions *dopt = fout->dopt;
4057  PQExpBuffer query;
4058  PGresult *res;
4059  SubscriptionInfo *subinfo;
4060  int i_tableoid;
4061  int i_oid;
4062  int i_subname;
4063  int i_rolname;
4064  int i_subconninfo;
4065  int i_subslotname;
4066  int i_subsynccommit;
4067  int i_subpublications;
4068  int i,
4069  ntups;
4070 
4071  if (dopt->no_subscriptions || fout->remoteVersion < 100000)
4072  return;
4073 
4074  if (!is_superuser(fout))
4075  {
4076  int n;
4077 
4078  res = ExecuteSqlQuery(fout,
4079  "SELECT count(*) FROM pg_subscription "
4080  "WHERE subdbid = (SELECT oid FROM pg_database"
4081  " WHERE datname = current_database())",
4082  PGRES_TUPLES_OK);
4083  n = atoi(PQgetvalue(res, 0, 0));
4084  if (n > 0)
4085  write_msg(NULL, "WARNING: subscriptions not dumped because current user is not a superuser\n");
4086  PQclear(res);
4087  return;
4088  }
4089 
4090  query = createPQExpBuffer();
4091 
4092  resetPQExpBuffer(query);
4093 
4094  /* Get the subscriptions in current database. */
4095  appendPQExpBuffer(query,
4096  "SELECT s.tableoid, s.oid, s.subname,"
4097  "(%s s.subowner) AS rolname, "
4098  " s.subconninfo, s.subslotname, s.subsynccommit, "
4099  " s.subpublications "
4100  "FROM pg_subscription s "
4101  "WHERE s.subdbid = (SELECT oid FROM pg_database"
4102  " WHERE datname = current_database())",
4104  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4105 
4106  ntups = PQntuples(res);
4107 
4108  i_tableoid = PQfnumber(res, "tableoid");
4109  i_oid = PQfnumber(res, "oid");
4110  i_subname = PQfnumber(res, "subname");
4111  i_rolname = PQfnumber(res, "rolname");
4112  i_subconninfo = PQfnumber(res, "subconninfo");
4113  i_subslotname = PQfnumber(res, "subslotname");
4114  i_subsynccommit = PQfnumber(res, "subsynccommit");
4115  i_subpublications = PQfnumber(res, "subpublications");
4116 
4117  subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo));
4118 
4119  for (i = 0; i < ntups; i++)
4120  {
4121  subinfo[i].dobj.objType = DO_SUBSCRIPTION;
4122  subinfo[i].dobj.catId.tableoid =
4123  atooid(PQgetvalue(res, i, i_tableoid));
4124  subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4125  AssignDumpId(&subinfo[i].dobj);
4126  subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
4127  subinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4128  subinfo[i].subconninfo = pg_strdup(PQgetvalue(res, i, i_subconninfo));
4129  if (PQgetisnull(res, i, i_subslotname))
4130  subinfo[i].subslotname = NULL;
4131  else
4132  subinfo[i].subslotname = pg_strdup(PQgetvalue(res, i, i_subslotname));
4133  subinfo[i].subsynccommit =
4134  pg_strdup(PQgetvalue(res, i, i_subsynccommit));
4135  subinfo[i].subpublications =
4136  pg_strdup(PQgetvalue(res, i, i_subpublications));
4137 
4138  if (strlen(subinfo[i].rolname) == 0)
4139  write_msg(NULL, "WARNING: owner of subscription \"%s\" appears to be invalid\n",
4140  subinfo[i].dobj.name);
4141 
4142  /* Decide whether we want to dump it */
4143  selectDumpableObject(&(subinfo[i].dobj), fout);
4144  }
4145  PQclear(res);
4146 
4147  destroyPQExpBuffer(query);
4148 }
4149 
4150 /*
4151  * dumpSubscription
4152  * dump the definition of the given subscription
4153  */
4154 static void
4156 {
4157  PQExpBuffer delq;
4158  PQExpBuffer query;
4159  PQExpBuffer publications;
4160  char *qsubname;
4161  char **pubnames = NULL;
4162  int npubnames = 0;
4163  int i;
4164 
4165  if (!(subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
4166  return;
4167 
4168  delq = createPQExpBuffer();
4169  query = createPQExpBuffer();
4170 
4171  qsubname = pg_strdup(fmtId(subinfo->dobj.name));
4172 
4173  appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n",
4174  qsubname);
4175 
4176  appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s CONNECTION ",
4177  qsubname);
4178  appendStringLiteralAH(query, subinfo->subconninfo, fout);
4179 
4180  /* Build list of quoted publications and append them to query. */
4181  if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames))
4182  {
4183  write_msg(NULL,
4184  "WARNING: could not parse subpublications array\n");
4185  if (pubnames)
4186  free(pubnames);
4187  pubnames = NULL;
4188  npubnames = 0;
4189  }
4190 
4191  publications = createPQExpBuffer();
4192  for (i = 0; i < npubnames; i++)
4193  {
4194  if (i > 0)
4195  appendPQExpBufferStr(publications, ", ");
4196 
4197  appendPQExpBufferStr(publications, fmtId(pubnames[i]));
4198  }
4199 
4200  appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data);
4201  if (subinfo->subslotname)
4202  appendStringLiteralAH(query, subinfo->subslotname, fout);
4203  else
4204  appendPQExpBufferStr(query, "NONE");
4205 
4206  if (strcmp(subinfo->subsynccommit, "off") != 0)
4207  appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));
4208 
4209  appendPQExpBufferStr(query, ");\n");
4210 
4211  ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
4212  subinfo->dobj.name,
4213  NULL,
4214  NULL,
4215  subinfo->rolname, false,
4216  "SUBSCRIPTION", SECTION_POST_DATA,
4217  query->data, delq->data, NULL,
4218  NULL, 0,
4219  NULL, NULL);
4220 
4221  if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4222  dumpComment(fout, "SUBSCRIPTION", qsubname,
4223  NULL, subinfo->rolname,
4224  subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
4225 
4226  if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4227  dumpSecLabel(fout, "SUBSCRIPTION", qsubname,
4228  NULL, subinfo->rolname,
4229  subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
4230 
4231  destroyPQExpBuffer(publications);
4232  if (pubnames)
4233  free(pubnames);
4234 
4235  destroyPQExpBuffer(delq);
4236  destroyPQExpBuffer(query);
4237  free(qsubname);
4238 }
4239 
4240 static void
4242  PQExpBuffer upgrade_buffer,
4243  Oid pg_type_oid,
4244  bool force_array_type)
4245 {
4246  PQExpBuffer upgrade_query = createPQExpBuffer();
4247  PGresult *res;
4248  Oid pg_type_array_oid;
4249 
4250  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4251  appendPQExpBuffer(upgrade_buffer,
4252  "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4253  pg_type_oid);
4254 
4255  /* we only support old >= 8.3 for binary upgrades */
4256  appendPQExpBuffer(upgrade_query,
4257  "SELECT typarray "
4258  "FROM pg_catalog.pg_type "
4259  "WHERE oid = '%u'::pg_catalog.oid;",
4260  pg_type_oid);
4261 
4262  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4263 
4264  pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4265 
4266  PQclear(res);
4267 
4268  if (!OidIsValid(pg_type_array_oid) && force_array_type)
4269  {
4270  /*
4271  * If the old version didn't assign an array type, but the new version
4272  * does, we must select an unused type OID to assign. This currently
4273  * only happens for domains, when upgrading pre-v11 to v11 and up.
4274  *
4275  * Note: local state here is kind of ugly, but we must have some,
4276  * since we mustn't choose the same unused OID more than once.
4277  */
4278  static Oid next_possible_free_oid = FirstNormalObjectId;
4279  bool is_dup;
4280 
4281  do
4282  {
4283  ++next_possible_free_oid;
4284  printfPQExpBuffer(upgrade_query,
4285  "SELECT EXISTS(SELECT 1 "
4286  "FROM pg_catalog.pg_type "
4287  "WHERE oid = '%u'::pg_catalog.oid);",
4288  next_possible_free_oid);
4289  res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4290  is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
4291  PQclear(res);
4292  } while (is_dup);
4293 
4294  pg_type_array_oid = next_possible_free_oid;
4295  }
4296 
4297  if (OidIsValid(pg_type_array_oid))
4298  {
4299  appendPQExpBufferStr(upgrade_buffer,
4300  "\n-- For binary upgrade, must preserve pg_type array oid\n");
4301  appendPQExpBuffer(upgrade_buffer,
4302  "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4303  pg_type_array_oid);
4304  }
4305 
4306  destroyPQExpBuffer(upgrade_query);
4307 }
4308 
4309 static bool
4311  PQExpBuffer upgrade_buffer,
4312  Oid pg_rel_oid)
4313 {
4314  PQExpBuffer upgrade_query = createPQExpBuffer();
4315  PGresult *upgrade_res;
4316  Oid pg_type_oid;
4317  bool toast_set = false;
4318 
4319  /* we only support old >= 8.3 for binary upgrades */
4320  appendPQExpBuffer(upgrade_query,
4321  "SELECT c.reltype AS crel, t.reltype AS trel "
4322  "FROM pg_catalog.pg_class c "
4323  "LEFT JOIN pg_catalog.pg_class t ON "
4324  " (c.reltoastrelid = t.oid) "
4325  "WHERE c.oid = '%u'::pg_catalog.oid;",
4326  pg_rel_oid);
4327 
4328  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4329 
4330  pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4331 
4332  binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4333  pg_type_oid, false);
4334 
4335  if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
4336  {
4337  /* Toast tables do not have pg_type array rows */
4338  Oid pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
4339  PQfnumber(upgrade_res, "trel")));
4340 
4341  appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
4342  appendPQExpBuffer(upgrade_buffer,
4343  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4344  pg_type_toast_oid);
4345 
4346  toast_set = true;
4347  }
4348 
4349  PQclear(upgrade_res);
4350  destroyPQExpBuffer(upgrade_query);
4351 
4352  return toast_set;
4353 }
4354 
4355 static void
4357  PQExpBuffer upgrade_buffer, Oid pg_class_oid,
4358  bool is_index)
4359 {
4360  PQExpBuffer upgrade_query = createPQExpBuffer();
4361  PGresult *upgrade_res;
4362  Oid pg_class_reltoastrelid;
4363  Oid pg_index_indexrelid;
4364 
4365  appendPQExpBuffer(upgrade_query,
4366  "SELECT c.reltoastrelid, i.indexrelid "
4367  "FROM pg_catalog.pg_class c LEFT JOIN "
4368  "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4369  "WHERE c.oid = '%u'::pg_catalog.oid;",
4370  pg_class_oid);
4371 
4372  upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4373 
4374  pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
4375  pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
4376 
4377  appendPQExpBufferStr(upgrade_buffer,
4378  "\n-- For binary upgrade, must preserve pg_class oids\n");
4379 
4380  if (!is_index)
4381  {
4382  appendPQExpBuffer(upgrade_buffer,
4383  "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4384  pg_class_oid);
4385  /* only tables have toast tables, not indexes */
4386  if (OidIsValid(pg_class_reltoastrelid))
4387  {
4388  /*
4389  * One complexity is that the table definition might not require
4390  * the creation of a TOAST table, and the TOAST table might have
4391  * been created long after table creation, when the table was
4392  * loaded with wide data. By setting the TOAST oid we force
4393  * creation of the TOAST heap and TOAST index by the backend so we
4394  * can cleanly copy the files during binary upgrade.
4395  */
4396 
4397  appendPQExpBuffer(upgrade_buffer,
4398  "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4399  pg_class_reltoastrelid);
4400 
4401  /* every toast table has an index */
4402  appendPQExpBuffer(upgrade_buffer,
4403  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4404  pg_index_indexrelid);
4405  }
4406  }
4407  else
4408  appendPQExpBuffer(upgrade_buffer,
4409  "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4410  pg_class_oid);
4411 
4412  appendPQExpBufferChar(upgrade_buffer, '\n');
4413 
4414  PQclear(upgrade_res);
4415  destroyPQExpBuffer(upgrade_query);
4416 }
4417 
4418 /*
4419  * If the DumpableObject is a member of an extension, add a suitable
4420  * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
4421  *
4422  * For somewhat historical reasons, objname should already be quoted,
4423  * but not objnamespace (if any).
4424  */
4425 static void
4427  DumpableObject *dobj,
4428  const char *objtype,
4429  const char *objname,
4430  const char *objnamespace)
4431 {
4432  DumpableObject *extobj = NULL;
4433  int i;
4434 
4435  if (!dobj->ext_member)
4436  return;
4437 
4438  /*
4439  * Find the parent extension. We could avoid this search if we wanted to
4440  * add a link field to DumpableObject, but the space costs of that would
4441  * be considerable. We assume that member objects could only have a
4442  * direct dependency on their own extension, not any others.
4443  */
4444  for (i = 0; i < dobj->nDeps; i++)
4445  {
4446  extobj = findObjectByDumpId(dobj->dependencies[i]);
4447  if (extobj && extobj->objType == DO_EXTENSION)
4448  break;
4449  extobj = NULL;
4450  }
4451  if (extobj == NULL)
4452  exit_horribly(NULL, "could not find parent extension for %s %s\n",
4453  objtype, objname);
4454 
4455  appendPQExpBufferStr(upgrade_buffer,
4456  "\n-- For binary upgrade, handle extension membership the hard way\n");
4457  appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4458  fmtId(extobj->name),
4459  objtype);
4460  if (objnamespace && *objnamespace)
4461  appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4462  appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4463 }
4464 
4465 /*
4466  * getNamespaces:
4467  * read all namespaces in the system catalogs and return them in the
4468  * NamespaceInfo* structure
4469  *
4470  * numNamespaces is set to the number of namespaces read in
4471  */
4472 NamespaceInfo *
4474 {
4475  DumpOptions *dopt = fout->dopt;
4476  PGresult *res;
4477  int ntups;
4478  int i;
4479  PQExpBuffer query;
4480  NamespaceInfo *nsinfo;
4481  int i_tableoid;
4482  int i_oid;
4483  int i_nspname;
4484  int i_rolname;
4485  int i_nspacl;
4486  int i_rnspacl;
4487  int i_initnspacl;
4488  int i_initrnspacl;
4489 
4490  query = createPQExpBuffer();
4491 
4492  /*
4493  * we fetch all namespaces including system ones, so that every object we
4494  * read in can be linked to a containing namespace.
4495  */
4496  if (fout->remoteVersion >= 90600)
4497  {
4498  PQExpBuffer acl_subquery = createPQExpBuffer();
4499  PQExpBuffer racl_subquery = createPQExpBuffer();
4500  PQExpBuffer init_acl_subquery = createPQExpBuffer();
4501  PQExpBuffer init_racl_subquery = createPQExpBuffer();
4502 
4503  buildACLQueries(acl_subquery, racl_subquery, init_acl_subquery,
4504  init_racl_subquery, "n.nspacl", "n.nspowner", "'n'",
4505  dopt->binary_upgrade);
4506 
4507  appendPQExpBuffer(query, "SELECT n.tableoid, n.oid, n.nspname, "
4508  "(%s nspowner) AS rolname, "
4509  "%s as nspacl, "
4510  "%s as rnspacl, "
4511  "%s as initnspacl, "
4512  "%s as initrnspacl "
4513  "FROM pg_namespace n "
4514  "LEFT JOIN pg_init_privs pip "
4515  "ON (n.oid = pip.objoid "
4516  "AND pip.classoid = 'pg_namespace'::regclass "
4517  "AND pip.objsubid = 0",
4519  acl_subquery->data,
4520  racl_subquery->data,
4521  init_acl_subquery->data,
4522  init_racl_subquery->data);
4523 
4524  appendPQExpBuffer(query, ") ");
4525 
4526  destroyPQExpBuffer(acl_subquery);
4527  destroyPQExpBuffer(racl_subquery);
4528  destroyPQExpBuffer(init_acl_subquery);
4529  destroyPQExpBuffer(init_racl_subquery);
4530  }
4531  else
4532  appendPQExpBuffer(query, "SELECT tableoid, oid, nspname, "
4533  "(%s nspowner) AS rolname, "
4534  "nspacl, NULL as rnspacl, "
4535  "NULL AS initnspacl, NULL as initrnspacl "
4536  "FROM pg_namespace",
4538 
4539  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4540 
4541  ntups = PQntuples(res);
4542 
4543  nsinfo = (NamespaceInfo *) pg_malloc(ntups * sizeof(NamespaceInfo));
4544 
4545  i_tableoid = PQfnumber(res, "tableoid");
4546  i_oid = PQfnumber(res, "oid");
4547  i_nspname = PQfnumber(res, "nspname");
4548  i_rolname = PQfnumber(res, "rolname");
4549  i_nspacl = PQfnumber(res, "nspacl");
4550  i_rnspacl = PQfnumber(res, "rnspacl");
4551  i_initnspacl = PQfnumber(res, "initnspacl");
4552  i_initrnspacl = PQfnumber(res, "initrnspacl");
4553 
4554  for (i = 0; i < ntups; i++)
4555  {
4556  nsinfo[i].dobj.objType = DO_NAMESPACE;
4557  nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4558  nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4559  AssignDumpId(&nsinfo[i].dobj);
4560  nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
4561  nsinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4562  nsinfo[i].nspacl = pg_strdup(PQgetvalue(res, i, i_nspacl));
4563  nsinfo[i].rnspacl = pg_strdup(PQgetvalue(res, i, i_rnspacl));
4564  nsinfo[i].initnspacl = pg_strdup(PQgetvalue(res, i, i_initnspacl));
4565  nsinfo[i].initrnspacl = pg_strdup(PQgetvalue(res, i, i_initrnspacl));
4566 
4567  /* Decide whether to dump this namespace */
4568  selectDumpableNamespace(&nsinfo[i], fout);
4569 
4570  /*
4571  * Do not try to dump ACL if the ACL is empty or the default.
4572  *
4573  * This is useful because, for some schemas/objects, the only
4574  * component we are going to try and dump is the ACL and if we can
4575  * remove that then 'dump' goes to zero/false and we don't consider
4576  * this object for dumping at all later on.
4577  */
4578  if (PQgetisnull(res, i, i_nspacl) && PQgetisnull(res, i, i_rnspacl) &&
4579  PQgetisnull(res, i, i_initnspacl) &&
4580  PQgetisnull(res, i, i_initrnspacl))
4581  nsinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4582 
4583  if (strlen(nsinfo[i].rolname) == 0)
4584  write_msg(NULL, "WARNING: owner of schema \"%s\" appears to be invalid\n",
4585  nsinfo[i].dobj.name);
4586  }
4587 
4588  PQclear(res);
4589  destroyPQExpBuffer(query);
4590 
4591  *numNamespaces = ntups;
4592 
4593  return nsinfo;
4594 }
4595 
4596 /*
4597  * findNamespace:
4598  * given a namespace OID, look up the info read by getNamespaces
4599  */
4600 static NamespaceInfo *
4602 {
4603  NamespaceInfo *nsinfo;
4604 
4605  nsinfo = findNamespaceByOid(nsoid);
4606  if (nsinfo == NULL)
4607  exit_horribly(NULL, "schema with OID %u does not exist\n", nsoid);
4608  return nsinfo;
4609 }
4610 
4611 /*
4612  * getExtensions:
4613  * read all extensions in the system catalogs and return them in the
4614  * ExtensionInfo* structure
4615  *
4616  * numExtensions is set to the number of extensions read in
4617  */
4618 ExtensionInfo *
4620 {
4621  DumpOptions *dopt = fout->dopt;
4622  PGresult *res;
4623  int ntups;
4624  int i;
4625  PQExpBuffer query;
4626  ExtensionInfo *extinfo;
4627  int i_tableoid;
4628  int i_oid;
4629  int i_extname;
4630  int i_nspname;
4631  int i_extrelocatable;
4632  int i_extversion;
4633  int i_extconfig;
4634  int i_extcondition;
4635 
4636  /*
4637  * Before 9.1, there are no extensions.
4638  */
4639  if (fout->remoteVersion < 90100)
4640  {
4641  *numExtensions = 0;
4642  return NULL;
4643  }
4644 
4645  query = createPQExpBuffer();
4646 
4647  appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, "
4648  "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
4649  "FROM pg_extension x "
4650  "JOIN pg_namespace n ON n.oid = x.extnamespace");
4651 
4652  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4653 
4654  ntups = PQntuples(res);
4655 
4656  extinfo = (ExtensionInfo *) pg_malloc(ntups * sizeof(ExtensionInfo));
4657 
4658  i_tableoid = PQfnumber(res, "tableoid");
4659  i_oid = PQfnumber(res, "oid");
4660  i_extname = PQfnumber(res, "extname");
4661  i_nspname = PQfnumber(res, "nspname");
4662  i_extrelocatable = PQfnumber(res, "extrelocatable");
4663  i_extversion = PQfnumber(res, "extversion");
4664  i_extconfig = PQfnumber(res, "extconfig");
4665  i_extcondition = PQfnumber(res, "extcondition");
4666 
4667  for (i = 0; i < ntups; i++)
4668  {
4669  extinfo[i].dobj.objType = DO_EXTENSION;
4670  extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4671  extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4672  AssignDumpId(&extinfo[i].dobj);
4673  extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
4674  extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
4675  extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
4676  extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
4677  extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
4678  extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
4679 
4680  /* Decide whether we want to dump it */
4681  selectDumpableExtension(&(extinfo[i]), dopt);
4682  }
4683 
4684  PQclear(res);
4685  destroyPQExpBuffer(query);
4686 
4687  *numExtensions = ntups;
4688 
4689  return extinfo;
4690 }
4691 
4692 /*
4693  * getTypes:
4694  * read all types in the system catalogs and return them in the
4695  * TypeInfo* structure
4696  *
4697  * numTypes is set to the number of types read in
4698  *
4699  * NB: this must run after getFuncs() because we assume we can do
4700  * findFuncByOid().
4701  */
4702 TypeInfo *
4704 {
4705  DumpOptions *dopt = fout->dopt;
4706  PGresult *res;
4707  int ntups;
4708  int i;
4709  PQExpBuffer query = createPQExpBuffer();
4710  TypeInfo *tyinfo;
4711  ShellTypeInfo *stinfo;
4712  int i_tableoid;
4713  int i_oid;
4714  int i_typname;
4715  int i_typnamespace;
4716  int i_typacl;
4717  int i_rtypacl;
4718  int i_inittypacl;
4719  int i_initrtypacl;
4720  int i_rolname;
4721  int i_typelem;
4722  int i_typrelid;
4723  int i_typrelkind;
4724  int i_typtype;
4725  int i_typisdefined;
4726  int i_isarray;
4727 
4728  /*
4729  * we include even the built-in types because those may be used as array
4730  * elements by user-defined types
4731  *
4732  * we filter out the built-in types when we dump out the types
4733  *
4734  * same approach for undefined (shell) types and array types
4735  *
4736  * Note: as of 8.3 we can reliably detect whether a type is an
4737  * auto-generated array type by checking the element type's typarray.
4738  * (Before that the test is capable of generating false positives.) We
4739  * still check for name beginning with '_', though, so as to avoid the
4740  * cost of the subselect probe for all standard types. This would have to
4741  * be revisited if the backend ever allows renaming of array types.
4742  */
4743 
4744  if (fout->remoteVersion >= 90600)
4745  {
4746  PQExpBuffer acl_subquery = createPQExpBuffer();
4747  PQExpBuffer racl_subquery = createPQExpBuffer();
4748  PQExpBuffer initacl_subquery = createPQExpBuffer();
4749  PQExpBuffer initracl_subquery = createPQExpBuffer();
4750 
4751  buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
4752  initracl_subquery, "t.typacl", "t.typowner", "'T'",
4753  dopt->binary_upgrade);
4754 
4755  appendPQExpBuffer(query, "SELECT t.tableoid, t.oid, t.typname, "
4756  "t.typnamespace, "
4757  "%s AS typacl, "
4758  "%s AS rtypacl, "
4759  "%s AS inittypacl, "
4760  "%s AS initrtypacl, "
4761  "(%s t.typowner) AS rolname, "
4762  "t.typelem, t.typrelid, "
4763  "CASE WHEN t.typrelid = 0 THEN ' '::\"char\" "
4764  "ELSE (SELECT relkind FROM pg_class WHERE oid = t.typrelid) END AS typrelkind, "
4765  "t.typtype, t.typisdefined, "
4766  "t.typname[0] = '_' AND t.typelem != 0 AND "
4767  "(SELECT typarray FROM pg_type te WHERE oid = t.typelem) = t.oid AS isarray "
4768  "FROM pg_type t "
4769  "LEFT JOIN pg_init_privs pip ON "
4770  "(t.oid = pip.objoid "
4771  "AND pip.classoid = 'pg_type'::regclass "
4772  "AND pip.objsubid = 0) ",
4773  acl_subquery->data,
4774  racl_subquery->data,
4775  initacl_subquery->data,
4776  initracl_subquery->data,
4778 
4779  destroyPQExpBuffer(acl_subquery);
4780  destroyPQExpBuffer(racl_subquery);
4781  destroyPQExpBuffer(initacl_subquery);
4782  destroyPQExpBuffer(initracl_subquery);
4783  }
4784  else if (fout->remoteVersion >= 90200)
4785  {
4786  appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4787  "typnamespace, typacl, NULL as rtypacl, "
4788  "NULL AS inittypacl, NULL AS initrtypacl, "
4789  "(%s typowner) AS rolname, "
4790  "typelem, typrelid, "
4791  "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4792  "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4793  "typtype, typisdefined, "
4794  "typname[0] = '_' AND typelem != 0 AND "
4795  "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
4796  "FROM pg_type",
4798  }
4799  else if (fout->remoteVersion >= 80300)
4800  {
4801  appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4802  "typnamespace, NULL AS typacl, NULL as rtypacl, "
4803  "NULL AS inittypacl, NULL AS initrtypacl, "
4804  "(%s typowner) AS rolname, "
4805  "typelem, typrelid, "
4806  "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4807  "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4808  "typtype, typisdefined, "
4809  "typname[0] = '_' AND typelem != 0 AND "
4810  "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
4811  "FROM pg_type",
4813  }
4814  else
4815  {
4816  appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4817  "typnamespace, NULL AS typacl, NULL as rtypacl, "
4818  "NULL AS inittypacl, NULL AS initrtypacl, "
4819  "(%s typowner) AS rolname, "
4820  "typelem, typrelid, "
4821  "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4822  "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4823  "typtype, typisdefined, "
4824  "typname[0] = '_' AND typelem != 0 AS isarray "
4825  "FROM pg_type",
4827  }
4828 
4829  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4830 
4831  ntups = PQntuples(res);
4832 
4833  tyinfo = (TypeInfo *) pg_malloc(ntups * sizeof(TypeInfo));
4834 
4835  i_tableoid = PQfnumber(res, "tableoid");
4836  i_oid = PQfnumber(res, "oid");
4837  i_typname = PQfnumber(res, "typname");
4838  i_typnamespace = PQfnumber(res, "typnamespace");
4839  i_typacl = PQfnumber(res, "typacl");
4840  i_rtypacl = PQfnumber(res, "rtypacl");
4841  i_inittypacl = PQfnumber(res, "inittypacl");
4842  i_initrtypacl = PQfnumber(res, "initrtypacl");
4843  i_rolname = PQfnumber(res, "rolname");
4844  i_typelem = PQfnumber(res, "typelem");
4845  i_typrelid = PQfnumber(res, "typrelid");
4846  i_typrelkind = PQfnumber(res, "typrelkind");
4847  i_typtype = PQfnumber(res, "typtype");
4848  i_typisdefined = PQfnumber(res, "typisdefined");
4849  i_isarray = PQfnumber(res, "isarray");
4850 
4851  for (i = 0; i < ntups; i++)
4852  {
4853  tyinfo[i].dobj.objType = DO_TYPE;
4854  tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4855  tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4856  AssignDumpId(&tyinfo[i].dobj);
4857  tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
4858  tyinfo[i].dobj.namespace =
4859  findNamespace(fout,
4860  atooid(PQgetvalue(res, i, i_typnamespace)));
4861  tyinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4862  tyinfo[i].typacl = pg_strdup(PQgetvalue(res, i, i_typacl));
4863  tyinfo[i].rtypacl = pg_strdup(PQgetvalue(res, i, i_rtypacl));
4864  tyinfo[i].inittypacl = pg_strdup(PQgetvalue(res, i, i_inittypacl));
4865  tyinfo[i].initrtypacl = pg_strdup(PQgetvalue(res, i, i_initrtypacl));
4866  tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
4867  tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
4868  tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
4869  tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
4870  tyinfo[i].shellType = NULL;
4871 
4872  if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
4873  tyinfo[i].isDefined = true;
4874  else
4875  tyinfo[i].isDefined = false;
4876 
4877  if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
4878  tyinfo[i].isArray = true;
4879  else
4880  tyinfo[i].isArray = false;
4881 
4882  /* Decide whether we want to dump it */
4883  selectDumpableType(&tyinfo[i], fout);
4884 
4885  /* Do not try to dump ACL if no ACL exists. */
4886  if (PQgetisnull(res, i, i_typacl) && PQgetisnull(res, i, i_rtypacl) &&
4887  PQgetisnull(res, i, i_inittypacl) &&
4888  PQgetisnull(res, i, i_initrtypacl))
4889  tyinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4890 
4891  /*
4892  * If it's a domain, fetch info about its constraints, if any
4893  */
4894  tyinfo[i].nDomChecks = 0;
4895  tyinfo[i].domChecks = NULL;
4896  if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
4897  tyinfo[i].typtype == TYPTYPE_DOMAIN)
4898  getDomainConstraints(fout, &(tyinfo[i]));
4899 
4900  /*
4901  * If it's a base type, make a DumpableObject representing a shell
4902  * definition of the type. We will need to dump that ahead of the I/O
4903  * functions for the type. Similarly, range types need a shell
4904  * definition in case they have a canonicalize function.
4905  *
4906  * Note: the shell type doesn't have a catId. You might think it
4907  * should copy the base type's catId, but then it might capture the
4908  * pg_depend entries for the type, which we don't want.
4909  */
4910  if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
4911  (tyinfo[i].typtype == TYPTYPE_BASE ||
4912  tyinfo[i].typtype == TYPTYPE_RANGE))
4913  {
4914  stinfo = (ShellTypeInfo *) pg_malloc(sizeof(ShellTypeInfo));
4915  stinfo->dobj.objType = DO_SHELL_TYPE;
4916  stinfo->dobj.catId = nilCatalogId;
4917  AssignDumpId(&stinfo->dobj);
4918  stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
4919  stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
4920  stinfo->baseType = &(tyinfo[i]);
4921  tyinfo[i].shellType = stinfo;
4922 
4923  /*
4924  * Initially mark the shell type as not to be dumped. We'll only
4925  * dump it if the I/O or canonicalize functions need to be dumped;
4926  * this is taken care of while sorting dependencies.
4927  */
4928  stinfo->dobj.dump = DUMP_COMPONENT_NONE;
4929  }
4930 
4931  if (strlen(tyinfo[i].rolname) == 0)
4932  write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",
4933  tyinfo[i].dobj.name);
4934  }
4935 
4936  *numTypes = ntups;
4937 
4938  PQclear(res);
4939 
4940  destroyPQExpBuffer(query);
4941 
4942  return tyinfo;
4943 }
4944 
4945 /*
4946  * getOperators:
4947  * read all operators in the system catalogs and return them in the
4948  * OprInfo* structure
4949  *
4950  * numOprs is set to the number of operators read in
4951  */
4952 OprInfo *
4953 getOperators(Archive *fout, int *numOprs)
4954 {
4955  PGresult *res;
4956  int ntups;
4957  int i;
4958  PQExpBuffer query = createPQExpBuffer();
4959  OprInfo *oprinfo;
4960  int i_tableoid;
4961  int i_oid;
4962  int i_oprname;
4963  int i_oprnamespace;
4964  int i_rolname;
4965  int i_oprkind;
4966  int i_oprcode;
4967 
4968  /*
4969  * find all operators, including builtin operators; we filter out
4970  * system-defined operators at dump-out time.
4971  */
4972 
4973  appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
4974  "oprnamespace, "
4975  "(%s oprowner) AS rolname, "
4976  "oprkind, "
4977  "oprcode::oid AS oprcode "
4978  "FROM pg_operator",
4980 
4981  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4982 
4983  ntups = PQntuples(res);
4984  *numOprs = ntups;
4985 
4986  oprinfo = (OprInfo *) pg_malloc(ntups * sizeof(OprInfo));
4987 
4988  i_tableoid = PQfnumber(res, "tableoid");
4989  i_oid = PQfnumber(res, "oid");
4990  i_oprname = PQfnumber(res, "oprname");
4991  i_oprnamespace = PQfnumber(res, "oprnamespace");
4992  i_rolname = PQfnumber(res, "rolname");
4993  i_oprkind = PQfnumber(res, "oprkind");
4994  i_oprcode = PQfnumber(res, "oprcode");
4995 
4996  for (i = 0; i < ntups; i++)
4997  {
4998  oprinfo[i].dobj.objType = DO_OPERATOR;
4999  oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5000  oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5001  AssignDumpId(&oprinfo[i].dobj);
5002  oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
5003  oprinfo[i].dobj.namespace =
5004  findNamespace(fout,
5005  atooid(PQgetvalue(res, i, i_oprnamespace)));
5006  oprinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5007  oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
5008  oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
5009 
5010  /* Decide whether we want to dump it */
5011  selectDumpableObject(&(oprinfo[i].dobj), fout);
5012 
5013  /* Operators do not currently have ACLs. */
5014  oprinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5015 
5016  if (strlen(oprinfo[i].rolname) == 0)
5017  write_msg(NULL, "WARNING: owner of operator \"%s\" appears to be invalid\n",
5018  oprinfo[i].dobj.name);
5019  }
5020 
5021  PQclear(res);
5022 
5023  destroyPQExpBuffer(query);
5024 
5025  return oprinfo;
5026 }
5027 
5028 /*
5029  * getCollations:
5030  * read all collations in the system catalogs and return them in the
5031  * CollInfo* structure
5032  *
5033  * numCollations is set to the number of collations read in
5034  */
5035 CollInfo *
5037 {
5038  PGresult *res;
5039  int ntups;
5040  int i;
5041  PQExpBuffer query;
5042  CollInfo *collinfo;
5043  int i_tableoid;
5044  int i_oid;
5045  int i_collname;
5046  int i_collnamespace;
5047  int i_rolname;
5048 
5049  /* Collations didn't exist pre-9.1 */
5050  if (fout->remoteVersion < 90100)
5051  {
5052  *numCollations = 0;
5053  return NULL;
5054  }
5055 
5056  query = createPQExpBuffer();
5057 
5058  /*
5059  * find all collations, including builtin collations; we filter out
5060  * system-defined collations at dump-out time.
5061  */
5062 
5063  appendPQExpBuffer(query, "SELECT tableoid, oid, collname, "
5064  "collnamespace, "
5065  "(%s collowner) AS rolname "
5066  "FROM pg_collation",
5068 
5069  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5070 
5071  ntups = PQntuples(res);
5072  *numCollations = ntups;
5073 
5074  collinfo = (CollInfo *) pg_malloc(ntups * sizeof(CollInfo));
5075 
5076  i_tableoid = PQfnumber(res, "tableoid");
5077  i_oid = PQfnumber(res, "oid");
5078  i_collname = PQfnumber(res, "collname");
5079  i_collnamespace = PQfnumber(res, "collnamespace");
5080  i_rolname = PQfnumber(res, "rolname");
5081 
5082  for (i = 0; i < ntups; i++)
5083  {
5084  collinfo[i].dobj.objType = DO_COLLATION;
5085  collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5086  collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5087  AssignDumpId(&collinfo[i].dobj);
5088  collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
5089  collinfo[i].dobj.namespace =
5090  findNamespace(fout,
5091  atooid(PQgetvalue(res, i, i_collnamespace)));
5092  collinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5093 
5094  /* Decide whether we want to dump it */
5095  selectDumpableObject(&(collinfo[i].dobj), fout);
5096 
5097  /* Collations do not currently have ACLs. */
5098  collinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5099  }
5100 
5101  PQclear(res);
5102 
5103  destroyPQExpBuffer(query);
5104 
5105  return collinfo;
5106 }
5107 
5108 /*
5109  * getConversions:
5110  * read all conversions in the system catalogs and return them in the
5111  * ConvInfo* structure
5112  *
5113  * numConversions is set to the number of conversions read in
5114  */
5115 ConvInfo *
5116 getConversions(Archive *fout, int *numConversions)
5117 {
5118  PGresult *res;
5119  int ntups;
5120  int i;
5121  PQExpBuffer query;
5122  ConvInfo *convinfo;
5123  int i_tableoid;
5124  int i_oid;
5125  int i_conname;
5126  int i_connamespace;
5127  int i_rolname;
5128 
5129  query = createPQExpBuffer();
5130 
5131  /*
5132  * find all conversions, including builtin conversions; we filter out
5133  * system-defined conversions at dump-out time.
5134  */
5135 
5136  appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
5137  "connamespace, "
5138  "(%s conowner) AS rolname "
5139  "FROM pg_conversion",
5141 
5142  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5143 
5144  ntups = PQntuples(res);
5145  *numConversions = ntups;
5146 
5147  convinfo = (ConvInfo *) pg_malloc(ntups * sizeof(ConvInfo));
5148 
5149  i_tableoid = PQfnumber(res, "tableoid");
5150  i_oid = PQfnumber(res, "oid");
5151  i_conname = PQfnumber(res, "conname");
5152  i_connamespace = PQfnumber(res, "connamespace");
5153  i_rolname = PQfnumber(res, "rolname");
5154 
5155  for (i = 0; i < ntups; i++)
5156  {
5157  convinfo[i].dobj.objType = DO_CONVERSION;
5158  convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5159  convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5160  AssignDumpId(&convinfo[i].dobj);
5161  convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
5162  convinfo[i].dobj.namespace =
5163  findNamespace(fout,
5164  atooid(PQgetvalue(res, i, i_connamespace)));
5165  convinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5166 
5167  /* Decide whether we want to dump it */
5168  selectDumpableObject(&(convinfo[i].dobj), fout);
5169 
5170  /* Conversions do not currently have ACLs. */
5171  convinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5172  }
5173 
5174  PQclear(res);
5175 
5176  destroyPQExpBuffer(query);
5177 
5178  return convinfo;
5179 }
5180 
5181 /*
5182  * getAccessMethods:
5183  * read all user-defined access methods in the system catalogs and return
5184  * them in the AccessMethodInfo* structure
5185  *
5186  * numAccessMethods is set to the number of access methods read in
5187  */
5189 getAccessMethods(Archive *fout, int *numAccessMethods)
5190 {
5191  PGresult *res;
5192  int ntups;
5193  int i;
5194  PQExpBuffer query;
5195  AccessMethodInfo *aminfo;
5196  int i_tableoid;
5197  int i_oid;
5198  int i_amname;
5199  int i_amhandler;
5200  int i_amtype;
5201 
5202  /* Before 9.6, there are no user-defined access methods */
5203  if (fout->remoteVersion < 90600)
5204  {
5205  *numAccessMethods = 0;
5206  return NULL;
5207  }
5208 
5209  query = createPQExpBuffer();
5210 
5211  /* Select all access methods from pg_am table */
5212  appendPQExpBuffer(query, "SELECT tableoid, oid, amname, amtype, "
5213  "amhandler::pg_catalog.regproc AS amhandler "
5214  "FROM pg_am");
5215 
5216  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5217 
5218  ntups = PQntuples(res);
5219  *numAccessMethods = ntups;
5220 
5221  aminfo = (AccessMethodInfo *) pg_malloc(ntups * sizeof(AccessMethodInfo));
5222 
5223  i_tableoid = PQfnumber(res, "tableoid");
5224  i_oid = PQfnumber(res, "oid");
5225  i_amname = PQfnumber(res, "amname");
5226  i_amhandler = PQfnumber(res, "amhandler");
5227  i_amtype = PQfnumber(res, "amtype");
5228 
5229  for (i = 0; i < ntups; i++)
5230  {
5231  aminfo[i].dobj.objType = DO_ACCESS_METHOD;
5232  aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5233  aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5234  AssignDumpId(&aminfo[i].dobj);
5235  aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname));
5236  aminfo[i].dobj.namespace = NULL;
5237  aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler));
5238  aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype));
5239 
5240  /* Decide whether we want to dump it */
5241  selectDumpableAccessMethod(&(aminfo[i]), fout);
5242 
5243  /* Access methods do not currently have ACLs. */
5244  aminfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5245  }
5246 
5247  PQclear(res);
5248 
5249  destroyPQExpBuffer(query);
5250 
5251  return aminfo;
5252 }
5253 
5254 
5255 /*
5256  * getOpclasses:
5257  * read all opclasses in the system catalogs and return them in the
5258  * OpclassInfo* structure
5259  *
5260  * numOpclasses is set to the number of opclasses read in
5261  */
5262 OpclassInfo *
5263 getOpclasses(Archive *fout, int *numOpclasses)
5264 {
5265  PGresult *res;
5266  int ntups;
5267  int i;
5268  PQExpBuffer query = createPQExpBuffer();
5269  OpclassInfo *opcinfo;
5270  int i_tableoid;
5271  int i_oid;
5272  int i_opcname;
5273  int i_opcnamespace;
5274  int i_rolname;
5275 
5276  /*
5277  * find all opclasses, including builtin opclasses; we filter out
5278  * system-defined opclasses at dump-out time.
5279  */
5280 
5281  appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
5282  "opcnamespace, "
5283  "(%s opcowner) AS rolname "
5284  "FROM pg_opclass",
5286 
5287  res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5288 
5289  ntups = PQntuples(res);
5290  *numOpclasses = ntups;
5291 
5292  opcinfo = (OpclassInfo *) pg_malloc(ntups * sizeof(OpclassInfo));
5293 
5294  i_tableoid = PQfnumber(res, "tableoid");
5295  i_oid = PQfnumber(res, "oid");
5296  i_opcname = PQfnumber(res, "opcname");
5297  i_opcnamespace = PQfnumber(res, "opcnamespace");
5298  i_rolname = PQfnumber(res, "rolname");
5299 
5300  for (i = 0; i < ntups; i++)
5301  {
5302  opcinfo[i].dobj.objType = DO_OPCLASS;
5303  opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5304  opcinfo[i].dobj.