]> granicus.if.org Git - postgresql/blob - src/bin/pg_dump/pg_dump.c
pg_dump: Add a --load-via-partition-root option.
[postgresql] / src / bin / pg_dump / pg_dump.c
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-2017, 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_am.h"
46 #include "catalog/pg_attribute.h"
47 #include "catalog/pg_cast.h"
48 #include "catalog/pg_class.h"
49 #include "catalog/pg_default_acl.h"
50 #include "catalog/pg_largeobject.h"
51 #include "catalog/pg_largeobject_metadata.h"
52 #include "catalog/pg_proc.h"
53 #include "catalog/pg_trigger.h"
54 #include "catalog/pg_type.h"
55 #include "libpq/libpq-fs.h"
56
57 #include "dumputils.h"
58 #include "parallel.h"
59 #include "pg_backup_db.h"
60 #include "pg_backup_utils.h"
61 #include "pg_dump.h"
62 #include "fe_utils/string_utils.h"
63
64
65 typedef struct
66 {
67         const char *descr;                      /* comment for an object */
68         Oid                     classoid;               /* object class (catalog OID) */
69         Oid                     objoid;                 /* object OID */
70         int                     objsubid;               /* subobject (table column #) */
71 } CommentItem;
72
73 typedef struct
74 {
75         const char *provider;           /* label provider of this security label */
76         const char *label;                      /* security label for an object */
77         Oid                     classoid;               /* object class (catalog OID) */
78         Oid                     objoid;                 /* object OID */
79         int                     objsubid;               /* subobject (table column #) */
80 } SecLabelItem;
81
82 typedef enum OidOptions
83 {
84         zeroAsOpaque = 1,
85         zeroAsAny = 2,
86         zeroAsStar = 4,
87         zeroAsNone = 8
88 } OidOptions;
89
90 /* global decls */
91 bool            g_verbose;                      /* User wants verbose narration of our
92                                                                  * activities. */
93 static bool dosync = true;              /* Issue fsync() to make dump durable on disk. */
94
95 /* subquery used to convert user ID (eg, datdba) to user name */
96 static const char *username_subquery;
97
98 /*
99  * For 8.0 and earlier servers, pulled from pg_database, for 8.1+ we use
100  * FirstNormalObjectId - 1.
101  */
102 static Oid      g_last_builtin_oid; /* value of the last builtin oid */
103
104 /* The specified names/patterns should to match at least one entity */
105 static int      strict_names = 0;
106
107 /*
108  * Object inclusion/exclusion lists
109  *
110  * The string lists record the patterns given by command-line switches,
111  * which we then convert to lists of OIDs of matching objects.
112  */
113 static SimpleStringList schema_include_patterns = {NULL, NULL};
114 static SimpleOidList schema_include_oids = {NULL, NULL};
115 static SimpleStringList schema_exclude_patterns = {NULL, NULL};
116 static SimpleOidList schema_exclude_oids = {NULL, NULL};
117
118 static SimpleStringList table_include_patterns = {NULL, NULL};
119 static SimpleOidList table_include_oids = {NULL, NULL};
120 static SimpleStringList table_exclude_patterns = {NULL, NULL};
121 static SimpleOidList table_exclude_oids = {NULL, NULL};
122 static SimpleStringList tabledata_exclude_patterns = {NULL, NULL};
123 static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
124
125
126 char            g_opaque_type[10];      /* name for the opaque type */
127
128 /* placeholders for the delimiters for comments */
129 char            g_comment_start[10];
130 char            g_comment_end[10];
131
132 static const CatalogId nilCatalogId = {0, 0};
133
134 static void help(const char *progname);
135 static void setup_connection(Archive *AH,
136                                  const char *dumpencoding, const char *dumpsnapshot,
137                                  char *use_role);
138 static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
139 static void expand_schema_name_patterns(Archive *fout,
140                                                         SimpleStringList *patterns,
141                                                         SimpleOidList *oids,
142                                                         bool strict_names);
143 static void expand_table_name_patterns(Archive *fout,
144                                                    SimpleStringList *patterns,
145                                                    SimpleOidList *oids,
146                                                    bool strict_names);
147 static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid);
148 static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
149 static void refreshMatViewData(Archive *fout, TableDataInfo *tdinfo);
150 static void guessConstraintInheritance(TableInfo *tblinfo, int numTables);
151 static void dumpComment(Archive *fout, const char *target,
152                         const char *namespace, const char *owner,
153                         CatalogId catalogId, int subid, DumpId dumpId);
154 static int findComments(Archive *fout, Oid classoid, Oid objoid,
155                          CommentItem **items);
156 static int      collectComments(Archive *fout, CommentItem **items);
157 static void dumpSecLabel(Archive *fout, const char *target,
158                          const char *namespace, const char *owner,
159                          CatalogId catalogId, int subid, DumpId dumpId);
160 static int findSecLabels(Archive *fout, Oid classoid, Oid objoid,
161                           SecLabelItem **items);
162 static int      collectSecLabels(Archive *fout, SecLabelItem **items);
163 static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
164 static void dumpNamespace(Archive *fout, NamespaceInfo *nspinfo);
165 static void dumpExtension(Archive *fout, ExtensionInfo *extinfo);
166 static void dumpType(Archive *fout, TypeInfo *tyinfo);
167 static void dumpBaseType(Archive *fout, TypeInfo *tyinfo);
168 static void dumpEnumType(Archive *fout, TypeInfo *tyinfo);
169 static void dumpRangeType(Archive *fout, TypeInfo *tyinfo);
170 static void dumpUndefinedType(Archive *fout, TypeInfo *tyinfo);
171 static void dumpDomain(Archive *fout, TypeInfo *tyinfo);
172 static void dumpCompositeType(Archive *fout, TypeInfo *tyinfo);
173 static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo);
174 static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
175 static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
176 static void dumpFunc(Archive *fout, FuncInfo *finfo);
177 static void dumpCast(Archive *fout, CastInfo *cast);
178 static void dumpTransform(Archive *fout, TransformInfo *transform);
179 static void dumpOpr(Archive *fout, OprInfo *oprinfo);
180 static void dumpAccessMethod(Archive *fout, AccessMethodInfo *oprinfo);
181 static void dumpOpclass(Archive *fout, OpclassInfo *opcinfo);
182 static void dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo);
183 static void dumpCollation(Archive *fout, CollInfo *collinfo);
184 static void dumpConversion(Archive *fout, ConvInfo *convinfo);
185 static void dumpRule(Archive *fout, RuleInfo *rinfo);
186 static void dumpAgg(Archive *fout, AggInfo *agginfo);
187 static void dumpTrigger(Archive *fout, TriggerInfo *tginfo);
188 static void dumpEventTrigger(Archive *fout, EventTriggerInfo *evtinfo);
189 static void dumpTable(Archive *fout, TableInfo *tbinfo);
190 static void dumpTableSchema(Archive *fout, TableInfo *tbinfo);
191 static void dumpAttrDef(Archive *fout, AttrDefInfo *adinfo);
192 static void dumpSequence(Archive *fout, TableInfo *tbinfo);
193 static void dumpSequenceData(Archive *fout, TableDataInfo *tdinfo);
194 static void dumpIndex(Archive *fout, IndxInfo *indxinfo);
195 static void dumpStatisticsExt(Archive *fout, StatsExtInfo *statsextinfo);
196 static void dumpConstraint(Archive *fout, ConstraintInfo *coninfo);
197 static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo);
198 static void dumpTSParser(Archive *fout, TSParserInfo *prsinfo);
199 static void dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo);
200 static void dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo);
201 static void dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo);
202 static void dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo);
203 static void dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo);
204 static void dumpUserMappings(Archive *fout,
205                                  const char *servername, const char *namespace,
206                                  const char *owner, CatalogId catalogId, DumpId dumpId);
207 static void dumpDefaultACL(Archive *fout, DefaultACLInfo *daclinfo);
208
209 static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
210                 const char *type, const char *name, const char *subname,
211                 const char *tag, const char *nspname, const char *owner,
212                 const char *acls, const char *racls,
213                 const char *initacls, const char *initracls);
214
215 static void getDependencies(Archive *fout);
216 static void BuildArchiveDependencies(Archive *fout);
217 static void findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj,
218                                                  DumpId **dependencies, int *nDeps, int *allocDeps);
219
220 static DumpableObject *createBoundaryObjects(void);
221 static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
222                                                 DumpableObject *boundaryObjs);
223
224 static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
225 static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind);
226 static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids);
227 static void buildMatViewRefreshDependencies(Archive *fout);
228 static void getTableDataFKConstraints(void);
229 static char *format_function_arguments(FuncInfo *finfo, char *funcargs,
230                                                   bool is_agg);
231 static char *format_function_arguments_old(Archive *fout,
232                                                           FuncInfo *finfo, int nallargs,
233                                                           char **allargtypes,
234                                                           char **argmodes,
235                                                           char **argnames);
236 static char *format_function_signature(Archive *fout,
237                                                   FuncInfo *finfo, bool honor_quotes);
238 static char *convertRegProcReference(Archive *fout,
239                                                 const char *proc);
240 static char *convertOperatorReference(Archive *fout, const char *opr);
241 static char *convertTSFunction(Archive *fout, Oid funcOid);
242 static Oid      findLastBuiltinOid_V71(Archive *fout, const char *);
243 static void selectSourceSchema(Archive *fout, const char *schemaName);
244 static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
245 static void getBlobs(Archive *fout);
246 static void dumpBlob(Archive *fout, BlobInfo *binfo);
247 static int      dumpBlobs(Archive *fout, void *arg);
248 static void dumpPolicy(Archive *fout, PolicyInfo *polinfo);
249 static void dumpPublication(Archive *fout, PublicationInfo *pubinfo);
250 static void dumpPublicationTable(Archive *fout, PublicationRelInfo *pubrinfo);
251 static void dumpSubscription(Archive *fout, SubscriptionInfo *subinfo);
252 static void dumpDatabase(Archive *AH);
253 static void dumpEncoding(Archive *AH);
254 static void dumpStdStrings(Archive *AH);
255 static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
256                                                                                  PQExpBuffer upgrade_buffer, Oid pg_type_oid);
257 static bool binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
258                                                                                 PQExpBuffer upgrade_buffer, Oid pg_rel_oid);
259 static void binary_upgrade_set_pg_class_oids(Archive *fout,
260                                                                  PQExpBuffer upgrade_buffer,
261                                                                  Oid pg_class_oid, bool is_index);
262 static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
263                                                                 DumpableObject *dobj,
264                                                                 const char *objlabel);
265 static const char *getAttrName(int attrnum, TableInfo *tblInfo);
266 static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
267 static bool nonemptyReloptions(const char *reloptions);
268 static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
269                                                 const char *prefix, Archive *fout);
270 static char *get_synchronized_snapshot(Archive *fout);
271 static void setupDumpWorker(Archive *AHX);
272 static TableInfo *getRootTableInfo(TableInfo *tbinfo);
273
274
275 int
276 main(int argc, char **argv)
277 {
278         int                     c;
279         const char *filename = NULL;
280         const char *format = "p";
281         TableInfo  *tblinfo;
282         int                     numTables;
283         DumpableObject **dobjs;
284         int                     numObjs;
285         DumpableObject *boundaryObjs;
286         int                     i;
287         int                     optindex;
288         RestoreOptions *ropt;
289         Archive    *fout;                       /* the script file */
290         const char *dumpencoding = NULL;
291         const char *dumpsnapshot = NULL;
292         char       *use_role = NULL;
293         int                     numWorkers = 1;
294         trivalue        prompt_password = TRI_DEFAULT;
295         int                     compressLevel = -1;
296         int                     plainText = 0;
297         ArchiveFormat archiveFormat = archUnknown;
298         ArchiveMode archiveMode;
299
300         static DumpOptions dopt;
301
302         static struct option long_options[] = {
303                 {"data-only", no_argument, NULL, 'a'},
304                 {"blobs", no_argument, NULL, 'b'},
305                 {"no-blobs", no_argument, NULL, 'B'},
306                 {"clean", no_argument, NULL, 'c'},
307                 {"create", no_argument, NULL, 'C'},
308                 {"dbname", required_argument, NULL, 'd'},
309                 {"file", required_argument, NULL, 'f'},
310                 {"format", required_argument, NULL, 'F'},
311                 {"host", required_argument, NULL, 'h'},
312                 {"jobs", 1, NULL, 'j'},
313                 {"no-reconnect", no_argument, NULL, 'R'},
314                 {"oids", no_argument, NULL, 'o'},
315                 {"no-owner", no_argument, NULL, 'O'},
316                 {"port", required_argument, NULL, 'p'},
317                 {"schema", required_argument, NULL, 'n'},
318                 {"exclude-schema", required_argument, NULL, 'N'},
319                 {"schema-only", no_argument, NULL, 's'},
320                 {"superuser", required_argument, NULL, 'S'},
321                 {"table", required_argument, NULL, 't'},
322                 {"exclude-table", required_argument, NULL, 'T'},
323                 {"no-password", no_argument, NULL, 'w'},
324                 {"password", no_argument, NULL, 'W'},
325                 {"username", required_argument, NULL, 'U'},
326                 {"verbose", no_argument, NULL, 'v'},
327                 {"no-privileges", no_argument, NULL, 'x'},
328                 {"no-acl", no_argument, NULL, 'x'},
329                 {"compress", required_argument, NULL, 'Z'},
330                 {"encoding", required_argument, NULL, 'E'},
331                 {"help", no_argument, NULL, '?'},
332                 {"version", no_argument, NULL, 'V'},
333
334                 /*
335                  * the following options don't have an equivalent short option letter
336                  */
337                 {"attribute-inserts", no_argument, &dopt.column_inserts, 1},
338                 {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1},
339                 {"column-inserts", no_argument, &dopt.column_inserts, 1},
340                 {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1},
341                 {"disable-triggers", no_argument, &dopt.disable_triggers, 1},
342                 {"enable-row-security", no_argument, &dopt.enable_row_security, 1},
343                 {"exclude-table-data", required_argument, NULL, 4},
344                 {"if-exists", no_argument, &dopt.if_exists, 1},
345                 {"inserts", no_argument, &dopt.dump_inserts, 1},
346                 {"lock-wait-timeout", required_argument, NULL, 2},
347                 {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
348                 {"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
349                 {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
350                 {"role", required_argument, NULL, 3},
351                 {"section", required_argument, NULL, 5},
352                 {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
353                 {"snapshot", required_argument, NULL, 6},
354                 {"strict-names", no_argument, &strict_names, 1},
355                 {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1},
356                 {"no-publications", no_argument, &dopt.no_publications, 1},
357                 {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
358                 {"no-synchronized-snapshots", no_argument, &dopt.no_synchronized_snapshots, 1},
359                 {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
360                 {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
361                 {"no-sync", no_argument, NULL, 7},
362
363                 {NULL, 0, NULL, 0}
364         };
365
366         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
367
368         /*
369          * Initialize what we need for parallel execution, especially for thread
370          * support on Windows.
371          */
372         init_parallel_dump_utils();
373
374         g_verbose = false;
375
376         strcpy(g_comment_start, "-- ");
377         g_comment_end[0] = '\0';
378         strcpy(g_opaque_type, "opaque");
379
380         progname = get_progname(argv[0]);
381
382         if (argc > 1)
383         {
384                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
385                 {
386                         help(progname);
387                         exit_nicely(0);
388                 }
389                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
390                 {
391                         puts("pg_dump (PostgreSQL) " PG_VERSION);
392                         exit_nicely(0);
393                 }
394         }
395
396         InitDumpOptions(&dopt);
397
398         while ((c = getopt_long(argc, argv, "abBcCd:E:f:F:h:j:n:N:oOp:RsS:t:T:U:vwWxZ:",
399                                                         long_options, &optindex)) != -1)
400         {
401                 switch (c)
402                 {
403                         case 'a':                       /* Dump data only */
404                                 dopt.dataOnly = true;
405                                 break;
406
407                         case 'b':                       /* Dump blobs */
408                                 dopt.outputBlobs = true;
409                                 break;
410
411                         case 'B':                       /* Don't dump blobs */
412                                 dopt.dontOutputBlobs = true;
413                                 break;
414
415                         case 'c':                       /* clean (i.e., drop) schema prior to create */
416                                 dopt.outputClean = 1;
417                                 break;
418
419                         case 'C':                       /* Create DB */
420                                 dopt.outputCreateDB = 1;
421                                 break;
422
423                         case 'd':                       /* database name */
424                                 dopt.dbname = pg_strdup(optarg);
425                                 break;
426
427                         case 'E':                       /* Dump encoding */
428                                 dumpencoding = pg_strdup(optarg);
429                                 break;
430
431                         case 'f':
432                                 filename = pg_strdup(optarg);
433                                 break;
434
435                         case 'F':
436                                 format = pg_strdup(optarg);
437                                 break;
438
439                         case 'h':                       /* server host */
440                                 dopt.pghost = pg_strdup(optarg);
441                                 break;
442
443                         case 'j':                       /* number of dump jobs */
444                                 numWorkers = atoi(optarg);
445                                 break;
446
447                         case 'n':                       /* include schema(s) */
448                                 simple_string_list_append(&schema_include_patterns, optarg);
449                                 dopt.include_everything = false;
450                                 break;
451
452                         case 'N':                       /* exclude schema(s) */
453                                 simple_string_list_append(&schema_exclude_patterns, optarg);
454                                 break;
455
456                         case 'o':                       /* Dump oids */
457                                 dopt.oids = true;
458                                 break;
459
460                         case 'O':                       /* Don't reconnect to match owner */
461                                 dopt.outputNoOwner = 1;
462                                 break;
463
464                         case 'p':                       /* server port */
465                                 dopt.pgport = pg_strdup(optarg);
466                                 break;
467
468                         case 'R':
469                                 /* no-op, still accepted for backwards compatibility */
470                                 break;
471
472                         case 's':                       /* dump schema only */
473                                 dopt.schemaOnly = true;
474                                 break;
475
476                         case 'S':                       /* Username for superuser in plain text output */
477                                 dopt.outputSuperuser = pg_strdup(optarg);
478                                 break;
479
480                         case 't':                       /* include table(s) */
481                                 simple_string_list_append(&table_include_patterns, optarg);
482                                 dopt.include_everything = false;
483                                 break;
484
485                         case 'T':                       /* exclude table(s) */
486                                 simple_string_list_append(&table_exclude_patterns, optarg);
487                                 break;
488
489                         case 'U':
490                                 dopt.username = pg_strdup(optarg);
491                                 break;
492
493                         case 'v':                       /* verbose */
494                                 g_verbose = true;
495                                 break;
496
497                         case 'w':
498                                 prompt_password = TRI_NO;
499                                 break;
500
501                         case 'W':
502                                 prompt_password = TRI_YES;
503                                 break;
504
505                         case 'x':                       /* skip ACL dump */
506                                 dopt.aclsSkip = true;
507                                 break;
508
509                         case 'Z':                       /* Compression Level */
510                                 compressLevel = atoi(optarg);
511                                 if (compressLevel < 0 || compressLevel > 9)
512                                 {
513                                         write_msg(NULL, "compression level must be in range 0..9\n");
514                                         exit_nicely(1);
515                                 }
516                                 break;
517
518                         case 0:
519                                 /* This covers the long options. */
520                                 break;
521
522                         case 2:                         /* lock-wait-timeout */
523                                 dopt.lockWaitTimeout = pg_strdup(optarg);
524                                 break;
525
526                         case 3:                         /* SET ROLE */
527                                 use_role = pg_strdup(optarg);
528                                 break;
529
530                         case 4:                         /* exclude table(s) data */
531                                 simple_string_list_append(&tabledata_exclude_patterns, optarg);
532                                 break;
533
534                         case 5:                         /* section */
535                                 set_dump_section(optarg, &dopt.dumpSections);
536                                 break;
537
538                         case 6:                         /* snapshot */
539                                 dumpsnapshot = pg_strdup(optarg);
540                                 break;
541
542                         case 7:                         /* no-sync */
543                                 dosync = false;
544                                 break;
545
546                         default:
547                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
548                                 exit_nicely(1);
549                 }
550         }
551
552         /*
553          * Non-option argument specifies database name as long as it wasn't
554          * already specified with -d / --dbname
555          */
556         if (optind < argc && dopt.dbname == NULL)
557                 dopt.dbname = argv[optind++];
558
559         /* Complain if any arguments remain */
560         if (optind < argc)
561         {
562                 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
563                                 progname, argv[optind]);
564                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
565                                 progname);
566                 exit_nicely(1);
567         }
568
569         /* --column-inserts implies --inserts */
570         if (dopt.column_inserts)
571                 dopt.dump_inserts = 1;
572
573         /*
574          * Binary upgrade mode implies dumping sequence data even in schema-only
575          * mode.  This is not exposed as a separate option, but kept separate
576          * internally for clarity.
577          */
578         if (dopt.binary_upgrade)
579                 dopt.sequence_data = 1;
580
581         if (dopt.dataOnly && dopt.schemaOnly)
582         {
583                 write_msg(NULL, "options -s/--schema-only and -a/--data-only cannot be used together\n");
584                 exit_nicely(1);
585         }
586
587         if (dopt.dataOnly && dopt.outputClean)
588         {
589                 write_msg(NULL, "options -c/--clean and -a/--data-only cannot be used together\n");
590                 exit_nicely(1);
591         }
592
593         if (dopt.dump_inserts && dopt.oids)
594         {
595                 write_msg(NULL, "options --inserts/--column-inserts and -o/--oids cannot be used together\n");
596                 write_msg(NULL, "(The INSERT command cannot set OIDs.)\n");
597                 exit_nicely(1);
598         }
599
600         if (dopt.if_exists && !dopt.outputClean)
601                 exit_horribly(NULL, "option --if-exists requires option -c/--clean\n");
602
603         /* Identify archive format to emit */
604         archiveFormat = parseArchiveFormat(format, &archiveMode);
605
606         /* archiveFormat specific setup */
607         if (archiveFormat == archNull)
608                 plainText = 1;
609
610         /* Custom and directory formats are compressed by default, others not */
611         if (compressLevel == -1)
612         {
613 #ifdef HAVE_LIBZ
614                 if (archiveFormat == archCustom || archiveFormat == archDirectory)
615                         compressLevel = Z_DEFAULT_COMPRESSION;
616                 else
617 #endif
618                         compressLevel = 0;
619         }
620
621 #ifndef HAVE_LIBZ
622         if (compressLevel != 0)
623                 write_msg(NULL, "WARNING: requested compression not available in this "
624                                   "installation -- archive will be uncompressed\n");
625         compressLevel = 0;
626 #endif
627
628         /*
629          * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually)
630          * parallel jobs because that's the maximum limit for the
631          * WaitForMultipleObjects() call.
632          */
633         if (numWorkers <= 0
634 #ifdef WIN32
635                 || numWorkers > MAXIMUM_WAIT_OBJECTS
636 #endif
637                 )
638                 exit_horribly(NULL, "invalid number of parallel jobs\n");
639
640         /* Parallel backup only in the directory archive format so far */
641         if (archiveFormat != archDirectory && numWorkers > 1)
642                 exit_horribly(NULL, "parallel backup only supported by the directory format\n");
643
644         /* Open the output file */
645         fout = CreateArchive(filename, archiveFormat, compressLevel, dosync,
646                                                  archiveMode, setupDumpWorker);
647
648         /* Make dump options accessible right away */
649         SetArchiveOptions(fout, &dopt, NULL);
650
651         /* Register the cleanup hook */
652         on_exit_close_archive(fout);
653
654         /* Let the archiver know how noisy to be */
655         fout->verbose = g_verbose;
656
657         /*
658          * We allow the server to be back to 8.0, and up to any minor release of
659          * our own major version.  (See also version check in pg_dumpall.c.)
660          */
661         fout->minRemoteVersion = 80000;
662         fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
663
664         fout->numWorkers = numWorkers;
665
666         /*
667          * Open the database using the Archiver, so it knows about it. Errors mean
668          * death.
669          */
670         ConnectDatabase(fout, dopt.dbname, dopt.pghost, dopt.pgport, dopt.username, prompt_password);
671         setup_connection(fout, dumpencoding, dumpsnapshot, use_role);
672
673         /*
674          * Disable security label support if server version < v9.1.x (prevents
675          * access to nonexistent pg_seclabel catalog)
676          */
677         if (fout->remoteVersion < 90100)
678                 dopt.no_security_labels = 1;
679
680         /*
681          * On hot standbys, never try to dump unlogged table data, since it will
682          * just throw an error.
683          */
684         if (fout->isStandby)
685                 dopt.no_unlogged_table_data = true;
686
687         /* Select the appropriate subquery to convert user IDs to names */
688         if (fout->remoteVersion >= 80100)
689                 username_subquery = "SELECT rolname FROM pg_catalog.pg_roles WHERE oid =";
690         else
691                 username_subquery = "SELECT usename FROM pg_catalog.pg_user WHERE usesysid =";
692
693         /* check the version for the synchronized snapshots feature */
694         if (numWorkers > 1 && fout->remoteVersion < 90200
695                 && !dopt.no_synchronized_snapshots)
696                 exit_horribly(NULL,
697                                           "Synchronized snapshots are not supported by this server version.\n"
698                                           "Run with --no-synchronized-snapshots instead if you do not need\n"
699                                           "synchronized snapshots.\n");
700
701         /* check the version when a snapshot is explicitly specified by user */
702         if (dumpsnapshot && fout->remoteVersion < 90200)
703                 exit_horribly(NULL,
704                                           "Exported snapshots are not supported by this server version.\n");
705
706         /*
707          * Find the last built-in OID, if needed (prior to 8.1)
708          *
709          * With 8.1 and above, we can just use FirstNormalObjectId - 1.
710          */
711         if (fout->remoteVersion < 80100)
712                 g_last_builtin_oid = findLastBuiltinOid_V71(fout,
713                                                                                                         PQdb(GetConnection(fout)));
714         else
715                 g_last_builtin_oid = FirstNormalObjectId - 1;
716
717         if (g_verbose)
718                 write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
719
720         /* Expand schema selection patterns into OID lists */
721         if (schema_include_patterns.head != NULL)
722         {
723                 expand_schema_name_patterns(fout, &schema_include_patterns,
724                                                                         &schema_include_oids,
725                                                                         strict_names);
726                 if (schema_include_oids.head == NULL)
727                         exit_horribly(NULL, "no matching schemas were found\n");
728         }
729         expand_schema_name_patterns(fout, &schema_exclude_patterns,
730                                                                 &schema_exclude_oids,
731                                                                 false);
732         /* non-matching exclusion patterns aren't an error */
733
734         /* Expand table selection patterns into OID lists */
735         if (table_include_patterns.head != NULL)
736         {
737                 expand_table_name_patterns(fout, &table_include_patterns,
738                                                                    &table_include_oids,
739                                                                    strict_names);
740                 if (table_include_oids.head == NULL)
741                         exit_horribly(NULL, "no matching tables were found\n");
742         }
743         expand_table_name_patterns(fout, &table_exclude_patterns,
744                                                            &table_exclude_oids,
745                                                            false);
746
747         expand_table_name_patterns(fout, &tabledata_exclude_patterns,
748                                                            &tabledata_exclude_oids,
749                                                            false);
750
751         /* non-matching exclusion patterns aren't an error */
752
753         /*
754          * Dumping blobs is the default for dumps where an inclusion switch is not
755          * used (an "include everything" dump).  -B can be used to exclude blobs
756          * from those dumps.  -b can be used to include blobs even when an
757          * inclusion switch is used.
758          *
759          * -s means "schema only" and blobs are data, not schema, so we never
760          * include blobs when -s is used.
761          */
762         if (dopt.include_everything && !dopt.schemaOnly && !dopt.dontOutputBlobs)
763                 dopt.outputBlobs = true;
764
765         /*
766          * Now scan the database and create DumpableObject structs for all the
767          * objects we intend to dump.
768          */
769         tblinfo = getSchemaData(fout, &numTables);
770
771         if (fout->remoteVersion < 80400)
772                 guessConstraintInheritance(tblinfo, numTables);
773
774         if (!dopt.schemaOnly)
775         {
776                 getTableData(&dopt, tblinfo, numTables, dopt.oids, 0);
777                 buildMatViewRefreshDependencies(fout);
778                 if (dopt.dataOnly)
779                         getTableDataFKConstraints();
780         }
781
782         if (dopt.schemaOnly && dopt.sequence_data)
783                 getTableData(&dopt, tblinfo, numTables, dopt.oids, RELKIND_SEQUENCE);
784
785         /*
786          * In binary-upgrade mode, we do not have to worry about the actual blob
787          * data or the associated metadata that resides in the pg_largeobject and
788          * pg_largeobject_metadata tables, respectivly.
789          *
790          * However, we do need to collect blob information as there may be
791          * comments or other information on blobs that we do need to dump out.
792          */
793         if (dopt.outputBlobs || dopt.binary_upgrade)
794                 getBlobs(fout);
795
796         /*
797          * Collect dependency data to assist in ordering the objects.
798          */
799         getDependencies(fout);
800
801         /* Lastly, create dummy objects to represent the section boundaries */
802         boundaryObjs = createBoundaryObjects();
803
804         /* Get pointers to all the known DumpableObjects */
805         getDumpableObjects(&dobjs, &numObjs);
806
807         /*
808          * Add dummy dependencies to enforce the dump section ordering.
809          */
810         addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
811
812         /*
813          * Sort the objects into a safe dump order (no forward references).
814          *
815          * We rely on dependency information to help us determine a safe order, so
816          * the initial sort is mostly for cosmetic purposes: we sort by name to
817          * ensure that logically identical schemas will dump identically.
818          */
819         sortDumpableObjectsByTypeName(dobjs, numObjs);
820
821         /* If we do a parallel dump, we want the largest tables to go first */
822         if (archiveFormat == archDirectory && numWorkers > 1)
823                 sortDataAndIndexObjectsBySize(dobjs, numObjs);
824
825         sortDumpableObjects(dobjs, numObjs,
826                                                 boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
827
828         /*
829          * Create archive TOC entries for all the objects to be dumped, in a safe
830          * order.
831          */
832
833         /* First the special ENCODING and STDSTRINGS entries. */
834         dumpEncoding(fout);
835         dumpStdStrings(fout);
836
837         /* The database item is always next, unless we don't want it at all */
838         if (dopt.include_everything && !dopt.dataOnly)
839                 dumpDatabase(fout);
840
841         /* Now the rearrangeable objects. */
842         for (i = 0; i < numObjs; i++)
843                 dumpDumpableObject(fout, dobjs[i]);
844
845         /*
846          * Set up options info to ensure we dump what we want.
847          */
848         ropt = NewRestoreOptions();
849         ropt->filename = filename;
850
851         /* if you change this list, see dumpOptionsFromRestoreOptions */
852         ropt->dropSchema = dopt.outputClean;
853         ropt->dataOnly = dopt.dataOnly;
854         ropt->schemaOnly = dopt.schemaOnly;
855         ropt->if_exists = dopt.if_exists;
856         ropt->column_inserts = dopt.column_inserts;
857         ropt->dumpSections = dopt.dumpSections;
858         ropt->aclsSkip = dopt.aclsSkip;
859         ropt->superuser = dopt.outputSuperuser;
860         ropt->createDB = dopt.outputCreateDB;
861         ropt->noOwner = dopt.outputNoOwner;
862         ropt->noTablespace = dopt.outputNoTablespaces;
863         ropt->disable_triggers = dopt.disable_triggers;
864         ropt->use_setsessauth = dopt.use_setsessauth;
865         ropt->disable_dollar_quoting = dopt.disable_dollar_quoting;
866         ropt->dump_inserts = dopt.dump_inserts;
867         ropt->no_publications = dopt.no_publications;
868         ropt->no_security_labels = dopt.no_security_labels;
869         ropt->no_subscriptions = dopt.no_subscriptions;
870         ropt->lockWaitTimeout = dopt.lockWaitTimeout;
871         ropt->include_everything = dopt.include_everything;
872         ropt->enable_row_security = dopt.enable_row_security;
873         ropt->sequence_data = dopt.sequence_data;
874         ropt->binary_upgrade = dopt.binary_upgrade;
875
876         if (compressLevel == -1)
877                 ropt->compression = 0;
878         else
879                 ropt->compression = compressLevel;
880
881         ropt->suppressDumpWarnings = true;      /* We've already shown them */
882
883         SetArchiveOptions(fout, &dopt, ropt);
884
885         /* Mark which entries should be output */
886         ProcessArchiveRestoreOptions(fout);
887
888         /*
889          * The archive's TOC entries are now marked as to which ones will actually
890          * be output, so we can set up their dependency lists properly. This isn't
891          * necessary for plain-text output, though.
892          */
893         if (!plainText)
894                 BuildArchiveDependencies(fout);
895
896         /*
897          * And finally we can do the actual output.
898          *
899          * Note: for non-plain-text output formats, the output file is written
900          * inside CloseArchive().  This is, um, bizarre; but not worth changing
901          * right now.
902          */
903         if (plainText)
904                 RestoreArchive(fout);
905
906         CloseArchive(fout);
907
908         exit_nicely(0);
909 }
910
911
912 static void
913 help(const char *progname)
914 {
915         printf(_("%s dumps a database as a text file or to other formats.\n\n"), progname);
916         printf(_("Usage:\n"));
917         printf(_("  %s [OPTION]... [DBNAME]\n"), progname);
918
919         printf(_("\nGeneral options:\n"));
920         printf(_("  -f, --file=FILENAME          output file or directory name\n"));
921         printf(_("  -F, --format=c|d|t|p         output file format (custom, directory, tar,\n"
922                          "                               plain text (default))\n"));
923         printf(_("  -j, --jobs=NUM               use this many parallel jobs to dump\n"));
924         printf(_("  -v, --verbose                verbose mode\n"));
925         printf(_("  -V, --version                output version information, then exit\n"));
926         printf(_("  -Z, --compress=0-9           compression level for compressed formats\n"));
927         printf(_("  --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock\n"));
928         printf(_("  --no-sync                    do not wait for changes to be written safely to disk\n"));
929         printf(_("  -?, --help                   show this help, then exit\n"));
930
931         printf(_("\nOptions controlling the output content:\n"));
932         printf(_("  -a, --data-only              dump only the data, not the schema\n"));
933         printf(_("  -b, --blobs                  include large objects in dump\n"));
934         printf(_("  -B, --no-blobs               exclude large objects in dump\n"));
935         printf(_("  -c, --clean                  clean (drop) database objects before recreating\n"));
936         printf(_("  -C, --create                 include commands to create database in dump\n"));
937         printf(_("  -E, --encoding=ENCODING      dump the data in encoding ENCODING\n"));
938         printf(_("  -n, --schema=SCHEMA          dump the named schema(s) only\n"));
939         printf(_("  -N, --exclude-schema=SCHEMA  do NOT dump the named schema(s)\n"));
940         printf(_("  -o, --oids                   include OIDs in dump\n"));
941         printf(_("  -O, --no-owner               skip restoration of object ownership in\n"
942                          "                               plain-text format\n"));
943         printf(_("  -s, --schema-only            dump only the schema, no data\n"));
944         printf(_("  -S, --superuser=NAME         superuser user name to use in plain-text format\n"));
945         printf(_("  -t, --table=TABLE            dump the named table(s) only\n"));
946         printf(_("  -T, --exclude-table=TABLE    do NOT dump the named table(s)\n"));
947         printf(_("  -x, --no-privileges          do not dump privileges (grant/revoke)\n"));
948         printf(_("  --binary-upgrade             for use by upgrade utilities only\n"));
949         printf(_("  --column-inserts             dump data as INSERT commands with column names\n"));
950         printf(_("  --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting\n"));
951         printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
952         printf(_("  --enable-row-security        enable row security (dump only content user has\n"
953                          "                               access to)\n"));
954         printf(_("  --exclude-table-data=TABLE   do NOT dump data for the named table(s)\n"));
955         printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
956         printf(_("  --inserts                    dump data as INSERT commands, rather than COPY\n"));
957         printf(_("  --no-publications            do not dump publications\n"));
958         printf(_("  --no-security-labels         do not dump security label assignments\n"));
959         printf(_("  --no-subscriptions           do not dump subscriptions\n"));
960         printf(_("  --no-synchronized-snapshots  do not use synchronized snapshots in parallel jobs\n"));
961         printf(_("  --no-tablespaces             do not dump tablespace assignments\n"));
962         printf(_("  --no-unlogged-table-data     do not dump unlogged table data\n"));
963         printf(_("  --quote-all-identifiers      quote all identifiers, even if not key words\n"));
964         printf(_("  --load-via-partition-root    load partitions via the root table\n"));
965         printf(_("  --section=SECTION            dump named section (pre-data, data, or post-data)\n"));
966         printf(_("  --serializable-deferrable    wait until the dump can run without anomalies\n"));
967         printf(_("  --snapshot=SNAPSHOT          use given snapshot for the dump\n"));
968         printf(_("  --strict-names               require table and/or schema include patterns to\n"
969                          "                               match at least one entity each\n"));
970         printf(_("  --use-set-session-authorization\n"
971                          "                               use SET SESSION AUTHORIZATION commands instead of\n"
972                          "                               ALTER OWNER commands to set ownership\n"));
973
974         printf(_("\nConnection options:\n"));
975         printf(_("  -d, --dbname=DBNAME      database to dump\n"));
976         printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
977         printf(_("  -p, --port=PORT          database server port number\n"));
978         printf(_("  -U, --username=NAME      connect as specified database user\n"));
979         printf(_("  -w, --no-password        never prompt for password\n"));
980         printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
981         printf(_("  --role=ROLENAME          do SET ROLE before dump\n"));
982
983         printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
984                          "variable value is used.\n\n"));
985         printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
986 }
987
988 static void
989 setup_connection(Archive *AH, const char *dumpencoding,
990                                  const char *dumpsnapshot, char *use_role)
991 {
992         DumpOptions *dopt = AH->dopt;
993         PGconn     *conn = GetConnection(AH);
994         const char *std_strings;
995
996         /*
997          * Set the client encoding if requested.
998          */
999         if (dumpencoding)
1000         {
1001                 if (PQsetClientEncoding(conn, dumpencoding) < 0)
1002                         exit_horribly(NULL, "invalid client encoding \"%s\" specified\n",
1003                                                   dumpencoding);
1004         }
1005
1006         /*
1007          * Get the active encoding and the standard_conforming_strings setting, so
1008          * we know how to escape strings.
1009          */
1010         AH->encoding = PQclientEncoding(conn);
1011
1012         std_strings = PQparameterStatus(conn, "standard_conforming_strings");
1013         AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);
1014
1015         /*
1016          * Set the role if requested.  In a parallel dump worker, we'll be passed
1017          * use_role == NULL, but AH->use_role is already set (if user specified it
1018          * originally) and we should use that.
1019          */
1020         if (!use_role && AH->use_role)
1021                 use_role = AH->use_role;
1022
1023         /* Set the role if requested */
1024         if (use_role && AH->remoteVersion >= 80100)
1025         {
1026                 PQExpBuffer query = createPQExpBuffer();
1027
1028                 appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
1029                 ExecuteSqlStatement(AH, query->data);
1030                 destroyPQExpBuffer(query);
1031
1032                 /* save it for possible later use by parallel workers */
1033                 if (!AH->use_role)
1034                         AH->use_role = pg_strdup(use_role);
1035         }
1036
1037         /* Set the datestyle to ISO to ensure the dump's portability */
1038         ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
1039
1040         /* Likewise, avoid using sql_standard intervalstyle */
1041         if (AH->remoteVersion >= 80400)
1042                 ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
1043
1044         /*
1045          * Set extra_float_digits so that we can dump float data exactly (given
1046          * correctly implemented float I/O code, anyway)
1047          */
1048         if (AH->remoteVersion >= 90000)
1049                 ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
1050         else
1051                 ExecuteSqlStatement(AH, "SET extra_float_digits TO 2");
1052
1053         /*
1054          * If synchronized scanning is supported, disable it, to prevent
1055          * unpredictable changes in row ordering across a dump and reload.
1056          */
1057         if (AH->remoteVersion >= 80300)
1058                 ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
1059
1060         /*
1061          * Disable timeouts if supported.
1062          */
1063         ExecuteSqlStatement(AH, "SET statement_timeout = 0");
1064         if (AH->remoteVersion >= 90300)
1065                 ExecuteSqlStatement(AH, "SET lock_timeout = 0");
1066         if (AH->remoteVersion >= 90600)
1067                 ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
1068
1069         /*
1070          * Quote all identifiers, if requested.
1071          */
1072         if (quote_all_identifiers && AH->remoteVersion >= 90100)
1073                 ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1074
1075         /*
1076          * Adjust row-security mode, if supported.
1077          */
1078         if (AH->remoteVersion >= 90500)
1079         {
1080                 if (dopt->enable_row_security)
1081                         ExecuteSqlStatement(AH, "SET row_security = on");
1082                 else
1083                         ExecuteSqlStatement(AH, "SET row_security = off");
1084         }
1085
1086         /*
1087          * Start transaction-snapshot mode transaction to dump consistent data.
1088          */
1089         ExecuteSqlStatement(AH, "BEGIN");
1090         if (AH->remoteVersion >= 90100)
1091         {
1092                 /*
1093                  * To support the combination of serializable_deferrable with the jobs
1094                  * option we use REPEATABLE READ for the worker connections that are
1095                  * passed a snapshot.  As long as the snapshot is acquired in a
1096                  * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a
1097                  * REPEATABLE READ transaction provides the appropriate integrity
1098                  * guarantees.  This is a kluge, but safe for back-patching.
1099                  */
1100                 if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
1101                         ExecuteSqlStatement(AH,
1102                                                                 "SET TRANSACTION ISOLATION LEVEL "
1103                                                                 "SERIALIZABLE, READ ONLY, DEFERRABLE");
1104                 else
1105                         ExecuteSqlStatement(AH,
1106                                                                 "SET TRANSACTION ISOLATION LEVEL "
1107                                                                 "REPEATABLE READ, READ ONLY");
1108         }
1109         else
1110         {
1111                 ExecuteSqlStatement(AH,
1112                                                         "SET TRANSACTION ISOLATION LEVEL "
1113                                                         "SERIALIZABLE, READ ONLY");
1114         }
1115
1116         /*
1117          * If user specified a snapshot to use, select that.  In a parallel dump
1118          * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id
1119          * is already set (if the server can handle it) and we should use that.
1120          */
1121         if (dumpsnapshot)
1122                 AH->sync_snapshot_id = pg_strdup(dumpsnapshot);
1123
1124         if (AH->sync_snapshot_id)
1125         {
1126                 PQExpBuffer query = createPQExpBuffer();
1127
1128                 appendPQExpBuffer(query, "SET TRANSACTION SNAPSHOT ");
1129                 appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1130                 ExecuteSqlStatement(AH, query->data);
1131                 destroyPQExpBuffer(query);
1132         }
1133         else if (AH->numWorkers > 1 &&
1134                          AH->remoteVersion >= 90200 &&
1135                          !dopt->no_synchronized_snapshots)
1136         {
1137                 if (AH->isStandby)
1138                         exit_horribly(NULL,
1139                                                   "Synchronized snapshots are not supported on standby servers.\n"
1140                                                   "Run with --no-synchronized-snapshots instead if you do not need\n"
1141                                                   "synchronized snapshots.\n");
1142
1143
1144                 AH->sync_snapshot_id = get_synchronized_snapshot(AH);
1145         }
1146 }
1147
1148 /* Set up connection for a parallel worker process */
1149 static void
1150 setupDumpWorker(Archive *AH)
1151 {
1152         /*
1153          * We want to re-select all the same values the master connection is
1154          * using.  We'll have inherited directly-usable values in
1155          * AH->sync_snapshot_id and AH->use_role, but we need to translate the
1156          * inherited encoding value back to a string to pass to setup_connection.
1157          */
1158         setup_connection(AH,
1159                                          pg_encoding_to_char(AH->encoding),
1160                                          NULL,
1161                                          NULL);
1162 }
1163
1164 static char *
1165 get_synchronized_snapshot(Archive *fout)
1166 {
1167         char       *query = "SELECT pg_catalog.pg_export_snapshot()";
1168         char       *result;
1169         PGresult   *res;
1170
1171         res = ExecuteSqlQueryForSingleRow(fout, query);
1172         result = pg_strdup(PQgetvalue(res, 0, 0));
1173         PQclear(res);
1174
1175         return result;
1176 }
1177
1178 static ArchiveFormat
1179 parseArchiveFormat(const char *format, ArchiveMode *mode)
1180 {
1181         ArchiveFormat archiveFormat;
1182
1183         *mode = archModeWrite;
1184
1185         if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1186         {
1187                 /* This is used by pg_dumpall, and is not documented */
1188                 archiveFormat = archNull;
1189                 *mode = archModeAppend;
1190         }
1191         else if (pg_strcasecmp(format, "c") == 0)
1192                 archiveFormat = archCustom;
1193         else if (pg_strcasecmp(format, "custom") == 0)
1194                 archiveFormat = archCustom;
1195         else if (pg_strcasecmp(format, "d") == 0)
1196                 archiveFormat = archDirectory;
1197         else if (pg_strcasecmp(format, "directory") == 0)
1198                 archiveFormat = archDirectory;
1199         else if (pg_strcasecmp(format, "p") == 0)
1200                 archiveFormat = archNull;
1201         else if (pg_strcasecmp(format, "plain") == 0)
1202                 archiveFormat = archNull;
1203         else if (pg_strcasecmp(format, "t") == 0)
1204                 archiveFormat = archTar;
1205         else if (pg_strcasecmp(format, "tar") == 0)
1206                 archiveFormat = archTar;
1207         else
1208                 exit_horribly(NULL, "invalid output format \"%s\" specified\n", format);
1209         return archiveFormat;
1210 }
1211
1212 /*
1213  * Find the OIDs of all schemas matching the given list of patterns,
1214  * and append them to the given OID list.
1215  */
1216 static void
1217 expand_schema_name_patterns(Archive *fout,
1218                                                         SimpleStringList *patterns,
1219                                                         SimpleOidList *oids,
1220                                                         bool strict_names)
1221 {
1222         PQExpBuffer query;
1223         PGresult   *res;
1224         SimpleStringListCell *cell;
1225         int                     i;
1226
1227         if (patterns->head == NULL)
1228                 return;                                 /* nothing to do */
1229
1230         query = createPQExpBuffer();
1231
1232         /*
1233          * The loop below runs multiple SELECTs might sometimes result in
1234          * duplicate entries in the OID list, but we don't care.
1235          */
1236
1237         for (cell = patterns->head; cell; cell = cell->next)
1238         {
1239                 appendPQExpBuffer(query,
1240                                                   "SELECT oid FROM pg_catalog.pg_namespace n\n");
1241                 processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1242                                                           false, NULL, "n.nspname", NULL, NULL);
1243
1244                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1245                 if (strict_names && PQntuples(res) == 0)
1246                         exit_horribly(NULL, "no matching schemas were found for pattern \"%s\"\n", cell->val);
1247
1248                 for (i = 0; i < PQntuples(res); i++)
1249                 {
1250                         simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1251                 }
1252
1253                 PQclear(res);
1254                 resetPQExpBuffer(query);
1255         }
1256
1257         destroyPQExpBuffer(query);
1258 }
1259
1260 /*
1261  * Find the OIDs of all tables matching the given list of patterns,
1262  * and append them to the given OID list.
1263  */
1264 static void
1265 expand_table_name_patterns(Archive *fout,
1266                                                    SimpleStringList *patterns, SimpleOidList *oids,
1267                                                    bool strict_names)
1268 {
1269         PQExpBuffer query;
1270         PGresult   *res;
1271         SimpleStringListCell *cell;
1272         int                     i;
1273
1274         if (patterns->head == NULL)
1275                 return;                                 /* nothing to do */
1276
1277         query = createPQExpBuffer();
1278
1279         /*
1280          * this might sometimes result in duplicate entries in the OID list, but
1281          * we don't care.
1282          */
1283
1284         for (cell = patterns->head; cell; cell = cell->next)
1285         {
1286                 appendPQExpBuffer(query,
1287                                                   "SELECT c.oid"
1288                                                   "\nFROM pg_catalog.pg_class c"
1289                                                   "\n     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace"
1290                                                   "\nWHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c')\n",
1291                                                   RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1292                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
1293                                                   RELKIND_PARTITIONED_TABLE);
1294                 processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1295                                                           false, "n.nspname", "c.relname", NULL,
1296                                                           "pg_catalog.pg_table_is_visible(c.oid)");
1297
1298                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1299                 if (strict_names && PQntuples(res) == 0)
1300                         exit_horribly(NULL, "no matching tables were found for pattern \"%s\"\n", cell->val);
1301
1302                 for (i = 0; i < PQntuples(res); i++)
1303                 {
1304                         simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1305                 }
1306
1307                 PQclear(res);
1308                 resetPQExpBuffer(query);
1309         }
1310
1311         destroyPQExpBuffer(query);
1312 }
1313
1314 /*
1315  * checkExtensionMembership
1316  *              Determine whether object is an extension member, and if so,
1317  *              record an appropriate dependency and set the object's dump flag.
1318  *
1319  * It's important to call this for each object that could be an extension
1320  * member.  Generally, we integrate this with determining the object's
1321  * to-be-dumped-ness, since extension membership overrides other rules for that.
1322  *
1323  * Returns true if object is an extension member, else false.
1324  */
1325 static bool
1326 checkExtensionMembership(DumpableObject *dobj, Archive *fout)
1327 {
1328         ExtensionInfo *ext = findOwningExtension(dobj->catId);
1329
1330         if (ext == NULL)
1331                 return false;
1332
1333         dobj->ext_member = true;
1334
1335         /* Record dependency so that getDependencies needn't deal with that */
1336         addObjectDependency(dobj, ext->dobj.dumpId);
1337
1338         /*
1339          * In 9.6 and above, mark the member object to have any non-initial ACL,
1340          * policies, and security labels dumped.
1341          *
1342          * Note that any initial ACLs (see pg_init_privs) will be removed when we
1343          * extract the information about the object.  We don't provide support for
1344          * initial policies and security labels and it seems unlikely for those to
1345          * ever exist, but we may have to revisit this later.
1346          *
1347          * Prior to 9.6, we do not include any extension member components.
1348          *
1349          * In binary upgrades, we still dump all components of the members
1350          * individually, since the idea is to exactly reproduce the database
1351          * contents rather than replace the extension contents with something
1352          * different.
1353          */
1354         if (fout->dopt->binary_upgrade)
1355                 dobj->dump = ext->dobj.dump;
1356         else
1357         {
1358                 if (fout->remoteVersion < 90600)
1359                         dobj->dump = DUMP_COMPONENT_NONE;
1360                 else
1361                         dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1362                                                                                                         DUMP_COMPONENT_SECLABEL |
1363                                                                                                         DUMP_COMPONENT_POLICY);
1364         }
1365
1366         return true;
1367 }
1368
1369 /*
1370  * selectDumpableNamespace: policy-setting subroutine
1371  *              Mark a namespace as to be dumped or not
1372  */
1373 static void
1374 selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout)
1375 {
1376         /*
1377          * If specific tables are being dumped, do not dump any complete
1378          * namespaces. If specific namespaces are being dumped, dump just those
1379          * namespaces. Otherwise, dump all non-system namespaces.
1380          */
1381         if (table_include_oids.head != NULL)
1382                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1383         else if (schema_include_oids.head != NULL)
1384                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump =
1385                         simple_oid_list_member(&schema_include_oids,
1386                                                                    nsinfo->dobj.catId.oid) ?
1387                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1388         else if (fout->remoteVersion >= 90600 &&
1389                          strcmp(nsinfo->dobj.name, "pg_catalog") == 0)
1390         {
1391                 /*
1392                  * In 9.6 and above, we dump out any ACLs defined in pg_catalog, if
1393                  * they are interesting (and not the original ACLs which were set at
1394                  * initdb time, see pg_init_privs).
1395                  */
1396                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
1397         }
1398         else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
1399                          strcmp(nsinfo->dobj.name, "information_schema") == 0)
1400         {
1401                 /* Other system schemas don't get dumped */
1402                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1403         }
1404         else
1405                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
1406
1407         /*
1408          * In any case, a namespace can be excluded by an exclusion switch
1409          */
1410         if (nsinfo->dobj.dump_contains &&
1411                 simple_oid_list_member(&schema_exclude_oids,
1412                                                            nsinfo->dobj.catId.oid))
1413                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1414
1415         /*
1416          * If the schema belongs to an extension, allow extension membership to
1417          * override the dump decision for the schema itself.  However, this does
1418          * not change dump_contains, so this won't change what we do with objects
1419          * within the schema.  (If they belong to the extension, they'll get
1420          * suppressed by it, otherwise not.)
1421          */
1422         (void) checkExtensionMembership(&nsinfo->dobj, fout);
1423 }
1424
1425 /*
1426  * selectDumpableTable: policy-setting subroutine
1427  *              Mark a table as to be dumped or not
1428  */
1429 static void
1430 selectDumpableTable(TableInfo *tbinfo, Archive *fout)
1431 {
1432         if (checkExtensionMembership(&tbinfo->dobj, fout))
1433                 return;                                 /* extension membership overrides all else */
1434
1435         /*
1436          * If specific tables are being dumped, dump just those tables; else, dump
1437          * according to the parent namespace's dump flag.
1438          */
1439         if (table_include_oids.head != NULL)
1440                 tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
1441                                                                                                    tbinfo->dobj.catId.oid) ?
1442                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1443         else
1444                 tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains;
1445
1446         /*
1447          * In any case, a table can be excluded by an exclusion switch
1448          */
1449         if (tbinfo->dobj.dump &&
1450                 simple_oid_list_member(&table_exclude_oids,
1451                                                            tbinfo->dobj.catId.oid))
1452                 tbinfo->dobj.dump = DUMP_COMPONENT_NONE;
1453 }
1454
1455 /*
1456  * selectDumpableType: policy-setting subroutine
1457  *              Mark a type as to be dumped or not
1458  *
1459  * If it's a table's rowtype or an autogenerated array type, we also apply a
1460  * special type code to facilitate sorting into the desired order.  (We don't
1461  * want to consider those to be ordinary types because that would bring tables
1462  * up into the datatype part of the dump order.)  We still set the object's
1463  * dump flag; that's not going to cause the dummy type to be dumped, but we
1464  * need it so that casts involving such types will be dumped correctly -- see
1465  * dumpCast.  This means the flag should be set the same as for the underlying
1466  * object (the table or base type).
1467  */
1468 static void
1469 selectDumpableType(TypeInfo *tyinfo, Archive *fout)
1470 {
1471         /* skip complex types, except for standalone composite types */
1472         if (OidIsValid(tyinfo->typrelid) &&
1473                 tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
1474         {
1475                 TableInfo  *tytable = findTableByOid(tyinfo->typrelid);
1476
1477                 tyinfo->dobj.objType = DO_DUMMY_TYPE;
1478                 if (tytable != NULL)
1479                         tyinfo->dobj.dump = tytable->dobj.dump;
1480                 else
1481                         tyinfo->dobj.dump = DUMP_COMPONENT_NONE;
1482                 return;
1483         }
1484
1485         /* skip auto-generated array types */
1486         if (tyinfo->isArray)
1487         {
1488                 tyinfo->dobj.objType = DO_DUMMY_TYPE;
1489
1490                 /*
1491                  * Fall through to set the dump flag; we assume that the subsequent
1492                  * rules will do the same thing as they would for the array's base
1493                  * type.  (We cannot reliably look up the base type here, since
1494                  * getTypes may not have processed it yet.)
1495                  */
1496         }
1497
1498         if (checkExtensionMembership(&tyinfo->dobj, fout))
1499                 return;                                 /* extension membership overrides all else */
1500
1501         /* Dump based on if the contents of the namespace are being dumped */
1502         tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains;
1503 }
1504
1505 /*
1506  * selectDumpableDefaultACL: policy-setting subroutine
1507  *              Mark a default ACL as to be dumped or not
1508  *
1509  * For per-schema default ACLs, dump if the schema is to be dumped.
1510  * Otherwise dump if we are dumping "everything".  Note that dataOnly
1511  * and aclsSkip are checked separately.
1512  */
1513 static void
1514 selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt)
1515 {
1516         /* Default ACLs can't be extension members */
1517
1518         if (dinfo->dobj.namespace)
1519                 /* default ACLs are considered part of the namespace */
1520                 dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains;
1521         else
1522                 dinfo->dobj.dump = dopt->include_everything ?
1523                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1524 }
1525
1526 /*
1527  * selectDumpableCast: policy-setting subroutine
1528  *              Mark a cast as to be dumped or not
1529  *
1530  * Casts do not belong to any particular namespace (since they haven't got
1531  * names), nor do they have identifiable owners.  To distinguish user-defined
1532  * casts from built-in ones, we must resort to checking whether the cast's
1533  * OID is in the range reserved for initdb.
1534  */
1535 static void
1536 selectDumpableCast(CastInfo *cast, Archive *fout)
1537 {
1538         if (checkExtensionMembership(&cast->dobj, fout))
1539                 return;                                 /* extension membership overrides all else */
1540
1541         /*
1542          * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
1543          * support ACLs currently.
1544          */
1545         if (cast->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1546                 cast->dobj.dump = DUMP_COMPONENT_NONE;
1547         else
1548                 cast->dobj.dump = fout->dopt->include_everything ?
1549                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1550 }
1551
1552 /*
1553  * selectDumpableProcLang: policy-setting subroutine
1554  *              Mark a procedural language as to be dumped or not
1555  *
1556  * Procedural languages do not belong to any particular namespace.  To
1557  * identify built-in languages, we must resort to checking whether the
1558  * language's OID is in the range reserved for initdb.
1559  */
1560 static void
1561 selectDumpableProcLang(ProcLangInfo *plang, Archive *fout)
1562 {
1563         if (checkExtensionMembership(&plang->dobj, fout))
1564                 return;                                 /* extension membership overrides all else */
1565
1566         /*
1567          * Only include procedural languages when we are dumping everything.
1568          *
1569          * For from-initdb procedural languages, only include ACLs, as we do for
1570          * the pg_catalog namespace.  We need this because procedural languages do
1571          * not live in any namespace.
1572          */
1573         if (!fout->dopt->include_everything)
1574                 plang->dobj.dump = DUMP_COMPONENT_NONE;
1575         else
1576         {
1577                 if (plang->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1578                         plang->dobj.dump = fout->remoteVersion < 90600 ?
1579                                 DUMP_COMPONENT_NONE : DUMP_COMPONENT_ACL;
1580                 else
1581                         plang->dobj.dump = DUMP_COMPONENT_ALL;
1582         }
1583 }
1584
1585 /*
1586  * selectDumpableAccessMethod: policy-setting subroutine
1587  *              Mark an access method as to be dumped or not
1588  *
1589  * Access methods do not belong to any particular namespace.  To identify
1590  * built-in access methods, we must resort to checking whether the
1591  * method's OID is in the range reserved for initdb.
1592  */
1593 static void
1594 selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout)
1595 {
1596         if (checkExtensionMembership(&method->dobj, fout))
1597                 return;                                 /* extension membership overrides all else */
1598
1599         /*
1600          * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
1601          * they do not support ACLs currently.
1602          */
1603         if (method->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1604                 method->dobj.dump = DUMP_COMPONENT_NONE;
1605         else
1606                 method->dobj.dump = fout->dopt->include_everything ?
1607                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1608 }
1609
1610 /*
1611  * selectDumpableExtension: policy-setting subroutine
1612  *              Mark an extension as to be dumped or not
1613  *
1614  * Normally, we dump all extensions, or none of them if include_everything
1615  * is false (i.e., a --schema or --table switch was given).  However, in
1616  * binary-upgrade mode it's necessary to skip built-in extensions, since we
1617  * assume those will already be installed in the target database.  We identify
1618  * such extensions by their having OIDs in the range reserved for initdb.
1619  */
1620 static void
1621 selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
1622 {
1623         /*
1624          * Use DUMP_COMPONENT_ACL for from-initdb extensions, to allow users to
1625          * change permissions on those objects, if they wish to, and have those
1626          * changes preserved.
1627          */
1628         if (dopt->binary_upgrade && extinfo->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1629                 extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
1630         else
1631                 extinfo->dobj.dump = extinfo->dobj.dump_contains =
1632                         dopt->include_everything ? DUMP_COMPONENT_ALL :
1633                         DUMP_COMPONENT_NONE;
1634 }
1635
1636 /*
1637  * selectDumpablePublicationTable: policy-setting subroutine
1638  *              Mark a publication table as to be dumped or not
1639  *
1640  * Publication tables have schemas, but those are ignored in decision making,
1641  * because publications are only dumped when we are dumping everything.
1642  */
1643 static void
1644 selectDumpablePublicationTable(DumpableObject *dobj, Archive *fout)
1645 {
1646         if (checkExtensionMembership(dobj, fout))
1647                 return;                                 /* extension membership overrides all else */
1648
1649         dobj->dump = fout->dopt->include_everything ?
1650                 DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1651 }
1652
1653 /*
1654  * selectDumpableObject: policy-setting subroutine
1655  *              Mark a generic dumpable object as to be dumped or not
1656  *
1657  * Use this only for object types without a special-case routine above.
1658  */
1659 static void
1660 selectDumpableObject(DumpableObject *dobj, Archive *fout)
1661 {
1662         if (checkExtensionMembership(dobj, fout))
1663                 return;                                 /* extension membership overrides all else */
1664
1665         /*
1666          * Default policy is to dump if parent namespace is dumpable, or for
1667          * non-namespace-associated items, dump if we're dumping "everything".
1668          */
1669         if (dobj->namespace)
1670                 dobj->dump = dobj->namespace->dobj.dump_contains;
1671         else
1672                 dobj->dump = fout->dopt->include_everything ?
1673                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1674 }
1675
1676 /*
1677  *      Dump a table's contents for loading using the COPY command
1678  *      - this routine is called by the Archiver when it wants the table
1679  *        to be dumped.
1680  */
1681
1682 static int
1683 dumpTableData_copy(Archive *fout, void *dcontext)
1684 {
1685         TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1686         TableInfo  *tbinfo = tdinfo->tdtable;
1687         const char *classname = tbinfo->dobj.name;
1688         const bool      hasoids = tbinfo->hasoids;
1689         const bool      oids = tdinfo->oids;
1690         PQExpBuffer q = createPQExpBuffer();
1691
1692         /*
1693          * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
1694          * which uses it already.
1695          */
1696         PQExpBuffer clistBuf = createPQExpBuffer();
1697         PGconn     *conn = GetConnection(fout);
1698         PGresult   *res;
1699         int                     ret;
1700         char       *copybuf;
1701         const char *column_list;
1702
1703         if (g_verbose)
1704                 write_msg(NULL, "dumping contents of table \"%s.%s\"\n",
1705                                   tbinfo->dobj.namespace->dobj.name, classname);
1706
1707         /*
1708          * Make sure we are in proper schema.  We will qualify the table name
1709          * below anyway (in case its name conflicts with a pg_catalog table); but
1710          * this ensures reproducible results in case the table contains regproc,
1711          * regclass, etc columns.
1712          */
1713         selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
1714
1715         /*
1716          * Specify the column list explicitly so that we have no possibility of
1717          * retrieving data in the wrong column order.  (The default column
1718          * ordering of COPY will not be what we want in certain corner cases
1719          * involving ADD COLUMN and inheritance.)
1720          */
1721         column_list = fmtCopyColumnList(tbinfo, clistBuf);
1722
1723         if (oids && hasoids)
1724         {
1725                 appendPQExpBuffer(q, "COPY %s %s WITH OIDS TO stdout;",
1726                                                   fmtQualifiedId(fout->remoteVersion,
1727                                                                                  tbinfo->dobj.namespace->dobj.name,
1728                                                                                  classname),
1729                                                   column_list);
1730         }
1731         else if (tdinfo->filtercond)
1732         {
1733                 /* Note: this syntax is only supported in 8.2 and up */
1734                 appendPQExpBufferStr(q, "COPY (SELECT ");
1735                 /* klugery to get rid of parens in column list */
1736                 if (strlen(column_list) > 2)
1737                 {
1738                         appendPQExpBufferStr(q, column_list + 1);
1739                         q->data[q->len - 1] = ' ';
1740                 }
1741                 else
1742                         appendPQExpBufferStr(q, "* ");
1743                 appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
1744                                                   fmtQualifiedId(fout->remoteVersion,
1745                                                                                  tbinfo->dobj.namespace->dobj.name,
1746                                                                                  classname),
1747                                                   tdinfo->filtercond);
1748         }
1749         else
1750         {
1751                 appendPQExpBuffer(q, "COPY %s %s TO stdout;",
1752                                                   fmtQualifiedId(fout->remoteVersion,
1753                                                                                  tbinfo->dobj.namespace->dobj.name,
1754                                                                                  classname),
1755                                                   column_list);
1756         }
1757         res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
1758         PQclear(res);
1759         destroyPQExpBuffer(clistBuf);
1760
1761         for (;;)
1762         {
1763                 ret = PQgetCopyData(conn, &copybuf, 0);
1764
1765                 if (ret < 0)
1766                         break;                          /* done or error */
1767
1768                 if (copybuf)
1769                 {
1770                         WriteData(fout, copybuf, ret);
1771                         PQfreemem(copybuf);
1772                 }
1773
1774                 /* ----------
1775                  * THROTTLE:
1776                  *
1777                  * There was considerable discussion in late July, 2000 regarding
1778                  * slowing down pg_dump when backing up large tables. Users with both
1779                  * slow & fast (multi-processor) machines experienced performance
1780                  * degradation when doing a backup.
1781                  *
1782                  * Initial attempts based on sleeping for a number of ms for each ms
1783                  * of work were deemed too complex, then a simple 'sleep in each loop'
1784                  * implementation was suggested. The latter failed because the loop
1785                  * was too tight. Finally, the following was implemented:
1786                  *
1787                  * If throttle is non-zero, then
1788                  *              See how long since the last sleep.
1789                  *              Work out how long to sleep (based on ratio).
1790                  *              If sleep is more than 100ms, then
1791                  *                      sleep
1792                  *                      reset timer
1793                  *              EndIf
1794                  * EndIf
1795                  *
1796                  * where the throttle value was the number of ms to sleep per ms of
1797                  * work. The calculation was done in each loop.
1798                  *
1799                  * Most of the hard work is done in the backend, and this solution
1800                  * still did not work particularly well: on slow machines, the ratio
1801                  * was 50:1, and on medium paced machines, 1:1, and on fast
1802                  * multi-processor machines, it had little or no effect, for reasons
1803                  * that were unclear.
1804                  *
1805                  * Further discussion ensued, and the proposal was dropped.
1806                  *
1807                  * For those people who want this feature, it can be implemented using
1808                  * gettimeofday in each loop, calculating the time since last sleep,
1809                  * multiplying that by the sleep ratio, then if the result is more
1810                  * than a preset 'minimum sleep time' (say 100ms), call the 'select'
1811                  * function to sleep for a subsecond period ie.
1812                  *
1813                  * select(0, NULL, NULL, NULL, &tvi);
1814                  *
1815                  * This will return after the interval specified in the structure tvi.
1816                  * Finally, call gettimeofday again to save the 'last sleep time'.
1817                  * ----------
1818                  */
1819         }
1820         archprintf(fout, "\\.\n\n\n");
1821
1822         if (ret == -2)
1823         {
1824                 /* copy data transfer failed */
1825                 write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.\n", classname);
1826                 write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1827                 write_msg(NULL, "The command was: %s\n", q->data);
1828                 exit_nicely(1);
1829         }
1830
1831         /* Check command status and return to normal libpq state */
1832         res = PQgetResult(conn);
1833         if (PQresultStatus(res) != PGRES_COMMAND_OK)
1834         {
1835                 write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetResult() failed.\n", classname);
1836                 write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1837                 write_msg(NULL, "The command was: %s\n", q->data);
1838                 exit_nicely(1);
1839         }
1840         PQclear(res);
1841
1842         /* Do this to ensure we've pumped libpq back to idle state */
1843         if (PQgetResult(conn) != NULL)
1844                 write_msg(NULL, "WARNING: unexpected extra results during COPY of table \"%s\"\n",
1845                                   classname);
1846
1847         destroyPQExpBuffer(q);
1848         return 1;
1849 }
1850
1851 /*
1852  * Dump table data using INSERT commands.
1853  *
1854  * Caution: when we restore from an archive file direct to database, the
1855  * INSERT commands emitted by this function have to be parsed by
1856  * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments,
1857  * E'' strings, or dollar-quoted strings.  So don't emit anything like that.
1858  */
1859 static int
1860 dumpTableData_insert(Archive *fout, void *dcontext)
1861 {
1862         TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1863         TableInfo  *tbinfo = tdinfo->tdtable;
1864         const char *classname = tbinfo->dobj.name;
1865         DumpOptions *dopt = fout->dopt;
1866         PQExpBuffer q = createPQExpBuffer();
1867         PQExpBuffer insertStmt = NULL;
1868         PGresult   *res;
1869         int                     tuple;
1870         int                     nfields;
1871         int                     field;
1872
1873         /*
1874          * Make sure we are in proper schema.  We will qualify the table name
1875          * below anyway (in case its name conflicts with a pg_catalog table); but
1876          * this ensures reproducible results in case the table contains regproc,
1877          * regclass, etc columns.
1878          */
1879         selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
1880
1881         appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
1882                                           "SELECT * FROM ONLY %s",
1883                                           fmtQualifiedId(fout->remoteVersion,
1884                                                                          tbinfo->dobj.namespace->dobj.name,
1885                                                                          classname));
1886         if (tdinfo->filtercond)
1887                 appendPQExpBuffer(q, " %s", tdinfo->filtercond);
1888
1889         ExecuteSqlStatement(fout, q->data);
1890
1891         while (1)
1892         {
1893                 res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
1894                                                           PGRES_TUPLES_OK);
1895                 nfields = PQnfields(res);
1896                 for (tuple = 0; tuple < PQntuples(res); tuple++)
1897                 {
1898                         /*
1899                          * First time through, we build as much of the INSERT statement as
1900                          * possible in "insertStmt", which we can then just print for each
1901                          * line. If the table happens to have zero columns then this will
1902                          * be a complete statement, otherwise it will end in "VALUES(" and
1903                          * be ready to have the row's column values appended.
1904                          */
1905                         if (insertStmt == NULL)
1906                         {
1907                                 insertStmt = createPQExpBuffer();
1908
1909                                 /*
1910                                  * When load-via-partition-root is set, get the root table
1911                                  * name for the partition table, so that we can reload data
1912                                  * through the root table.
1913                                  */
1914                                 if (dopt->load_via_partition_root && tbinfo->ispartition)
1915                                 {
1916                                         TableInfo  *parentTbinfo;
1917
1918                                         parentTbinfo = getRootTableInfo(tbinfo);
1919
1920                                         /*
1921                                          * When we loading data through the root, we will qualify
1922                                          * the table name. This is needed because earlier
1923                                          * search_path will be set for the partition table.
1924                                          */
1925                                         classname = (char *) fmtQualifiedId(fout->remoteVersion,
1926                                                                                                                 parentTbinfo->dobj.namespace->dobj.name,
1927                                                                                                                 parentTbinfo->dobj.name);
1928                                 }
1929                                 else
1930                                         classname = fmtId(tbinfo->dobj.name);
1931
1932                                 appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
1933                                                                   classname);
1934
1935                                 /* corner case for zero-column table */
1936                                 if (nfields == 0)
1937                                 {
1938                                         appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n");
1939                                 }
1940                                 else
1941                                 {
1942                                         /* append the list of column names if required */
1943                                         if (dopt->column_inserts)
1944                                         {
1945                                                 appendPQExpBufferChar(insertStmt, '(');
1946                                                 for (field = 0; field < nfields; field++)
1947                                                 {
1948                                                         if (field > 0)
1949                                                                 appendPQExpBufferStr(insertStmt, ", ");
1950                                                         appendPQExpBufferStr(insertStmt,
1951                                                                                                  fmtId(PQfname(res, field)));
1952                                                 }
1953                                                 appendPQExpBufferStr(insertStmt, ") ");
1954                                         }
1955
1956                                         if (tbinfo->needs_override)
1957                                                 appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE ");
1958
1959                                         appendPQExpBufferStr(insertStmt, "VALUES (");
1960                                 }
1961                         }
1962
1963                         archputs(insertStmt->data, fout);
1964
1965                         /* if it is zero-column table then we're done */
1966                         if (nfields == 0)
1967                                 continue;
1968
1969                         for (field = 0; field < nfields; field++)
1970                         {
1971                                 if (field > 0)
1972                                         archputs(", ", fout);
1973                                 if (PQgetisnull(res, tuple, field))
1974                                 {
1975                                         archputs("NULL", fout);
1976                                         continue;
1977                                 }
1978
1979                                 /* XXX This code is partially duplicated in ruleutils.c */
1980                                 switch (PQftype(res, field))
1981                                 {
1982                                         case INT2OID:
1983                                         case INT4OID:
1984                                         case INT8OID:
1985                                         case OIDOID:
1986                                         case FLOAT4OID:
1987                                         case FLOAT8OID:
1988                                         case NUMERICOID:
1989                                                 {
1990                                                         /*
1991                                                          * These types are printed without quotes unless
1992                                                          * they contain values that aren't accepted by the
1993                                                          * scanner unquoted (e.g., 'NaN').  Note that
1994                                                          * strtod() and friends might accept NaN, so we
1995                                                          * can't use that to test.
1996                                                          *
1997                                                          * In reality we only need to defend against
1998                                                          * infinity and NaN, so we need not get too crazy
1999                                                          * about pattern matching here.
2000                                                          */
2001                                                         const char *s = PQgetvalue(res, tuple, field);
2002
2003                                                         if (strspn(s, "0123456789 +-eE.") == strlen(s))
2004                                                                 archputs(s, fout);
2005                                                         else
2006                                                                 archprintf(fout, "'%s'", s);
2007                                                 }
2008                                                 break;
2009
2010                                         case BITOID:
2011                                         case VARBITOID:
2012                                                 archprintf(fout, "B'%s'",
2013                                                                    PQgetvalue(res, tuple, field));
2014                                                 break;
2015
2016                                         case BOOLOID:
2017                                                 if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
2018                                                         archputs("true", fout);
2019                                                 else
2020                                                         archputs("false", fout);
2021                                                 break;
2022
2023                                         default:
2024                                                 /* All other types are printed as string literals. */
2025                                                 resetPQExpBuffer(q);
2026                                                 appendStringLiteralAH(q,
2027                                                                                           PQgetvalue(res, tuple, field),
2028                                                                                           fout);
2029                                                 archputs(q->data, fout);
2030                                                 break;
2031                                 }
2032                         }
2033                         archputs(");\n", fout);
2034                 }
2035
2036                 if (PQntuples(res) <= 0)
2037                 {
2038                         PQclear(res);
2039                         break;
2040                 }
2041                 PQclear(res);
2042         }
2043
2044         archputs("\n\n", fout);
2045
2046         ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
2047
2048         destroyPQExpBuffer(q);
2049         if (insertStmt != NULL)
2050                 destroyPQExpBuffer(insertStmt);
2051
2052         return 1;
2053 }
2054
2055 /*
2056  * getRootTableInfo:
2057  *     get the root TableInfo for the given partition table.
2058  */
2059 static TableInfo *
2060 getRootTableInfo(TableInfo *tbinfo)
2061 {
2062         TableInfo  *parentTbinfo;
2063
2064         Assert(tbinfo->ispartition);
2065         Assert(tbinfo->numParents == 1);
2066
2067         parentTbinfo = tbinfo->parents[0];
2068         while (parentTbinfo->ispartition)
2069         {
2070                 Assert(parentTbinfo->numParents == 1);
2071                 parentTbinfo = parentTbinfo->parents[0];
2072         }
2073
2074         return parentTbinfo;
2075 }
2076
2077 /*
2078  * dumpTableData -
2079  *        dump the contents of a single table
2080  *
2081  * Actually, this just makes an ArchiveEntry for the table contents.
2082  */
2083 static void
2084 dumpTableData(Archive *fout, TableDataInfo *tdinfo)
2085 {
2086         DumpOptions *dopt = fout->dopt;
2087         TableInfo  *tbinfo = tdinfo->tdtable;
2088         PQExpBuffer copyBuf = createPQExpBuffer();
2089         PQExpBuffer clistBuf = createPQExpBuffer();
2090         DataDumperPtr dumpFn;
2091         char       *copyStmt;
2092         const char *copyFrom;
2093
2094         if (!dopt->dump_inserts)
2095         {
2096                 /* Dump/restore using COPY */
2097                 dumpFn = dumpTableData_copy;
2098
2099                 /*
2100                  * When load-via-partition-root is set, get the root table name for
2101                  * the partition table, so that we can reload data through the root
2102                  * table.
2103                  */
2104                 if (dopt->load_via_partition_root && tbinfo->ispartition)
2105                 {
2106                         TableInfo  *parentTbinfo;
2107
2108                         parentTbinfo = getRootTableInfo(tbinfo);
2109
2110                         /*
2111                          * When we load data through the root, we will qualify the table
2112                          * name, because search_path is set for the partition.
2113                          */
2114                         copyFrom = fmtQualifiedId(fout->remoteVersion,
2115                                                                           parentTbinfo->dobj.namespace->dobj.name,
2116                                                                           parentTbinfo->dobj.name);
2117                 }
2118                 else
2119                         copyFrom = fmtId(tbinfo->dobj.name);
2120
2121                 /* must use 2 steps here 'cause fmtId is nonreentrant */
2122                 appendPQExpBuffer(copyBuf, "COPY %s ",
2123                                                   copyFrom);
2124                 appendPQExpBuffer(copyBuf, "%s %sFROM stdin;\n",
2125                                                   fmtCopyColumnList(tbinfo, clistBuf),
2126                                                   (tdinfo->oids && tbinfo->hasoids) ? "WITH OIDS " : "");
2127                 copyStmt = copyBuf->data;
2128         }
2129         else
2130         {
2131                 /* Restore using INSERT */
2132                 dumpFn = dumpTableData_insert;
2133                 copyStmt = NULL;
2134         }
2135
2136         /*
2137          * Note: although the TableDataInfo is a full DumpableObject, we treat its
2138          * dependency on its table as "special" and pass it to ArchiveEntry now.
2139          * See comments for BuildArchiveDependencies.
2140          */
2141         if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2142                 ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
2143                                          tbinfo->dobj.name, tbinfo->dobj.namespace->dobj.name,
2144                                          NULL, tbinfo->rolname,
2145                                          false, "TABLE DATA", SECTION_DATA,
2146                                          "", "", copyStmt,
2147                                          &(tbinfo->dobj.dumpId), 1,
2148                                          dumpFn, tdinfo);
2149
2150         destroyPQExpBuffer(copyBuf);
2151         destroyPQExpBuffer(clistBuf);
2152 }
2153
2154 /*
2155  * refreshMatViewData -
2156  *        load or refresh the contents of a single materialized view
2157  *
2158  * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
2159  * statement.
2160  */
2161 static void
2162 refreshMatViewData(Archive *fout, TableDataInfo *tdinfo)
2163 {
2164         TableInfo  *tbinfo = tdinfo->tdtable;
2165         PQExpBuffer q;
2166
2167         /* If the materialized view is not flagged as populated, skip this. */
2168         if (!tbinfo->relispopulated)
2169                 return;
2170
2171         q = createPQExpBuffer();
2172
2173         appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
2174                                           fmtId(tbinfo->dobj.name));
2175
2176         if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2177                 ArchiveEntry(fout,
2178                                          tdinfo->dobj.catId,    /* catalog ID */
2179                                          tdinfo->dobj.dumpId,   /* dump ID */
2180                                          tbinfo->dobj.name, /* Name */
2181                                          tbinfo->dobj.namespace->dobj.name, /* Namespace */
2182                                          NULL,          /* Tablespace */
2183                                          tbinfo->rolname,       /* Owner */
2184                                          false,         /* with oids */
2185                                          "MATERIALIZED VIEW DATA",      /* Desc */
2186                                          SECTION_POST_DATA, /* Section */
2187                                          q->data,       /* Create */
2188                                          "",            /* Del */
2189                                          NULL,          /* Copy */
2190                                          tdinfo->dobj.dependencies, /* Deps */
2191                                          tdinfo->dobj.nDeps,    /* # Deps */
2192                                          NULL,          /* Dumper */
2193                                          NULL);         /* Dumper Arg */
2194
2195         destroyPQExpBuffer(q);
2196 }
2197
2198 /*
2199  * getTableData -
2200  *        set up dumpable objects representing the contents of tables
2201  */
2202 static void
2203 getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind)
2204 {
2205         int                     i;
2206
2207         for (i = 0; i < numTables; i++)
2208         {
2209                 if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
2210                         (!relkind || tblinfo[i].relkind == relkind))
2211                         makeTableDataInfo(dopt, &(tblinfo[i]), oids);
2212         }
2213 }
2214
2215 /*
2216  * Make a dumpable object for the data of this specific table
2217  *
2218  * Note: we make a TableDataInfo if and only if we are going to dump the
2219  * table data; the "dump" flag in such objects isn't used.
2220  */
2221 static void
2222 makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids)
2223 {
2224         TableDataInfo *tdinfo;
2225
2226         /*
2227          * Nothing to do if we already decided to dump the table.  This will
2228          * happen for "config" tables.
2229          */
2230         if (tbinfo->dataObj != NULL)
2231                 return;
2232
2233         /* Skip VIEWs (no data to dump) */
2234         if (tbinfo->relkind == RELKIND_VIEW)
2235                 return;
2236         /* Skip FOREIGN TABLEs (no data to dump) */
2237         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2238                 return;
2239         /* Skip partitioned tables (data in partitions) */
2240         if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
2241                 return;
2242
2243         /* Don't dump data in unlogged tables, if so requested */
2244         if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
2245                 dopt->no_unlogged_table_data)
2246                 return;
2247
2248         /* Check that the data is not explicitly excluded */
2249         if (simple_oid_list_member(&tabledata_exclude_oids,
2250                                                            tbinfo->dobj.catId.oid))
2251                 return;
2252
2253         /* OK, let's dump it */
2254         tdinfo = (TableDataInfo *) pg_malloc(sizeof(TableDataInfo));
2255
2256         if (tbinfo->relkind == RELKIND_MATVIEW)
2257                 tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
2258         else if (tbinfo->relkind == RELKIND_SEQUENCE)
2259                 tdinfo->dobj.objType = DO_SEQUENCE_SET;
2260         else
2261                 tdinfo->dobj.objType = DO_TABLE_DATA;
2262
2263         /*
2264          * Note: use tableoid 0 so that this object won't be mistaken for
2265          * something that pg_depend entries apply to.
2266          */
2267         tdinfo->dobj.catId.tableoid = 0;
2268         tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
2269         AssignDumpId(&tdinfo->dobj);
2270         tdinfo->dobj.name = tbinfo->dobj.name;
2271         tdinfo->dobj.namespace = tbinfo->dobj.namespace;
2272         tdinfo->tdtable = tbinfo;
2273         tdinfo->oids = oids;
2274         tdinfo->filtercond = NULL;      /* might get set later */
2275         addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
2276
2277         tbinfo->dataObj = tdinfo;
2278 }
2279
2280 /*
2281  * The refresh for a materialized view must be dependent on the refresh for
2282  * any materialized view that this one is dependent on.
2283  *
2284  * This must be called after all the objects are created, but before they are
2285  * sorted.
2286  */
2287 static void
2288 buildMatViewRefreshDependencies(Archive *fout)
2289 {
2290         PQExpBuffer query;
2291         PGresult   *res;
2292         int                     ntups,
2293                                 i;
2294         int                     i_classid,
2295                                 i_objid,
2296                                 i_refobjid;
2297
2298         /* No Mat Views before 9.3. */
2299         if (fout->remoteVersion < 90300)
2300                 return;
2301
2302         /* Make sure we are in proper schema */
2303         selectSourceSchema(fout, "pg_catalog");
2304
2305         query = createPQExpBuffer();
2306
2307         appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2308                                                  "( "
2309                                                  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2310                                                  "FROM pg_depend d1 "
2311                                                  "JOIN pg_class c1 ON c1.oid = d1.objid "
2312                                                  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2313                                                  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2314                                                  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2315                                                  "AND d2.objid = r1.oid "
2316                                                  "AND d2.refobjid <> d1.objid "
2317                                                  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2318                                                  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2319                                                  CppAsString2(RELKIND_VIEW) ") "
2320                                                  "WHERE d1.classid = 'pg_class'::regclass "
2321                                                  "UNION "
2322                                                  "SELECT w.objid, d3.refobjid, c3.relkind "
2323                                                  "FROM w "
2324                                                  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2325                                                  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2326                                                  "AND d3.objid = r3.oid "
2327                                                  "AND d3.refobjid <> w.refobjid "
2328                                                  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2329                                                  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2330                                                  CppAsString2(RELKIND_VIEW) ") "
2331                                                  ") "
2332                                                  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2333                                                  "FROM w "
2334                                                  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2335
2336         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2337
2338         ntups = PQntuples(res);
2339
2340         i_classid = PQfnumber(res, "classid");
2341         i_objid = PQfnumber(res, "objid");
2342         i_refobjid = PQfnumber(res, "refobjid");
2343
2344         for (i = 0; i < ntups; i++)
2345         {
2346                 CatalogId       objId;
2347                 CatalogId       refobjId;
2348                 DumpableObject *dobj;
2349                 DumpableObject *refdobj;
2350                 TableInfo  *tbinfo;
2351                 TableInfo  *reftbinfo;
2352
2353                 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2354                 objId.oid = atooid(PQgetvalue(res, i, i_objid));
2355                 refobjId.tableoid = objId.tableoid;
2356                 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2357
2358                 dobj = findObjectByCatalogId(objId);
2359                 if (dobj == NULL)
2360                         continue;
2361
2362                 Assert(dobj->objType == DO_TABLE);
2363                 tbinfo = (TableInfo *) dobj;
2364                 Assert(tbinfo->relkind == RELKIND_MATVIEW);
2365                 dobj = (DumpableObject *) tbinfo->dataObj;
2366                 if (dobj == NULL)
2367                         continue;
2368                 Assert(dobj->objType == DO_REFRESH_MATVIEW);
2369
2370                 refdobj = findObjectByCatalogId(refobjId);
2371                 if (refdobj == NULL)
2372                         continue;
2373
2374                 Assert(refdobj->objType == DO_TABLE);
2375                 reftbinfo = (TableInfo *) refdobj;
2376                 Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2377                 refdobj = (DumpableObject *) reftbinfo->dataObj;
2378                 if (refdobj == NULL)
2379                         continue;
2380                 Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2381
2382                 addObjectDependency(dobj, refdobj->dumpId);
2383
2384                 if (!reftbinfo->relispopulated)
2385                         tbinfo->relispopulated = false;
2386         }
2387
2388         PQclear(res);
2389
2390         destroyPQExpBuffer(query);
2391 }
2392
2393 /*
2394  * getTableDataFKConstraints -
2395  *        add dump-order dependencies reflecting foreign key constraints
2396  *
2397  * This code is executed only in a data-only dump --- in schema+data dumps
2398  * we handle foreign key issues by not creating the FK constraints until
2399  * after the data is loaded.  In a data-only dump, however, we want to
2400  * order the table data objects in such a way that a table's referenced
2401  * tables are restored first.  (In the presence of circular references or
2402  * self-references this may be impossible; we'll detect and complain about
2403  * that during the dependency sorting step.)
2404  */
2405 static void
2406 getTableDataFKConstraints(void)
2407 {
2408         DumpableObject **dobjs;
2409         int                     numObjs;
2410         int                     i;
2411
2412         /* Search through all the dumpable objects for FK constraints */
2413         getDumpableObjects(&dobjs, &numObjs);
2414         for (i = 0; i < numObjs; i++)
2415         {
2416                 if (dobjs[i]->objType == DO_FK_CONSTRAINT)
2417                 {
2418                         ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
2419                         TableInfo  *ftable;
2420
2421                         /* Not interesting unless both tables are to be dumped */
2422                         if (cinfo->contable == NULL ||
2423                                 cinfo->contable->dataObj == NULL)
2424                                 continue;
2425                         ftable = findTableByOid(cinfo->confrelid);
2426                         if (ftable == NULL ||
2427                                 ftable->dataObj == NULL)
2428                                 continue;
2429
2430                         /*
2431                          * Okay, make referencing table's TABLE_DATA object depend on the
2432                          * referenced table's TABLE_DATA object.
2433                          */
2434                         addObjectDependency(&cinfo->contable->dataObj->dobj,
2435                                                                 ftable->dataObj->dobj.dumpId);
2436                 }
2437         }
2438         free(dobjs);
2439 }
2440
2441
2442 /*
2443  * guessConstraintInheritance:
2444  *      In pre-8.4 databases, we can't tell for certain which constraints
2445  *      are inherited.  We assume a CHECK constraint is inherited if its name
2446  *      matches the name of any constraint in the parent.  Originally this code
2447  *      tried to compare the expression texts, but that can fail for various
2448  *      reasons --- for example, if the parent and child tables are in different
2449  *      schemas, reverse-listing of function calls may produce different text
2450  *      (schema-qualified or not) depending on search path.
2451  *
2452  *      In 8.4 and up we can rely on the conislocal field to decide which
2453  *      constraints must be dumped; much safer.
2454  *
2455  *      This function assumes all conislocal flags were initialized to TRUE.
2456  *      It clears the flag on anything that seems to be inherited.
2457  */
2458 static void
2459 guessConstraintInheritance(TableInfo *tblinfo, int numTables)
2460 {
2461         int                     i,
2462                                 j,
2463                                 k;
2464
2465         for (i = 0; i < numTables; i++)
2466         {
2467                 TableInfo  *tbinfo = &(tblinfo[i]);
2468                 int                     numParents;
2469                 TableInfo **parents;
2470                 TableInfo  *parent;
2471
2472                 /* Sequences and views never have parents */
2473                 if (tbinfo->relkind == RELKIND_SEQUENCE ||
2474                         tbinfo->relkind == RELKIND_VIEW)
2475                         continue;
2476
2477                 /* Don't bother computing anything for non-target tables, either */
2478                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
2479                         continue;
2480
2481                 numParents = tbinfo->numParents;
2482                 parents = tbinfo->parents;
2483
2484                 if (numParents == 0)
2485                         continue;                       /* nothing to see here, move along */
2486
2487                 /* scan for inherited CHECK constraints */
2488                 for (j = 0; j < tbinfo->ncheck; j++)
2489                 {
2490                         ConstraintInfo *constr;
2491
2492                         constr = &(tbinfo->checkexprs[j]);
2493
2494                         for (k = 0; k < numParents; k++)
2495                         {
2496                                 int                     l;
2497
2498                                 parent = parents[k];
2499                                 for (l = 0; l < parent->ncheck; l++)
2500                                 {
2501                                         ConstraintInfo *pconstr = &(parent->checkexprs[l]);
2502
2503                                         if (strcmp(pconstr->dobj.name, constr->dobj.name) == 0)
2504                                         {
2505                                                 constr->conislocal = false;
2506                                                 break;
2507                                         }
2508                                 }
2509                                 if (!constr->conislocal)
2510                                         break;
2511                         }
2512                 }
2513         }
2514 }
2515
2516
2517 /*
2518  * dumpDatabase:
2519  *      dump the database definition
2520  */
2521 static void
2522 dumpDatabase(Archive *fout)
2523 {
2524         DumpOptions *dopt = fout->dopt;
2525         PQExpBuffer dbQry = createPQExpBuffer();
2526         PQExpBuffer delQry = createPQExpBuffer();
2527         PQExpBuffer creaQry = createPQExpBuffer();
2528         PGconn     *conn = GetConnection(fout);
2529         PGresult   *res;
2530         int                     i_tableoid,
2531                                 i_oid,
2532                                 i_dba,
2533                                 i_encoding,
2534                                 i_collate,
2535                                 i_ctype,
2536                                 i_frozenxid,
2537                                 i_minmxid,
2538                                 i_tablespace;
2539         CatalogId       dbCatId;
2540         DumpId          dbDumpId;
2541         const char *datname,
2542                            *dba,
2543                            *encoding,
2544                            *collate,
2545                            *ctype,
2546                            *tablespace;
2547         uint32          frozenxid,
2548                                 minmxid;
2549
2550         datname = PQdb(conn);
2551
2552         if (g_verbose)
2553                 write_msg(NULL, "saving database definition\n");
2554
2555         /* Make sure we are in proper schema */
2556         selectSourceSchema(fout, "pg_catalog");
2557
2558         /* Get the database owner and parameters from pg_database */
2559         if (fout->remoteVersion >= 90300)
2560         {
2561                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2562                                                   "(%s datdba) AS dba, "
2563                                                   "pg_encoding_to_char(encoding) AS encoding, "
2564                                                   "datcollate, datctype, datfrozenxid, datminmxid, "
2565                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2566                                                   "shobj_description(oid, 'pg_database') AS description "
2567
2568                                                   "FROM pg_database "
2569                                                   "WHERE datname = ",
2570                                                   username_subquery);
2571                 appendStringLiteralAH(dbQry, datname, fout);
2572         }
2573         else if (fout->remoteVersion >= 80400)
2574         {
2575                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2576                                                   "(%s datdba) AS dba, "
2577                                                   "pg_encoding_to_char(encoding) AS encoding, "
2578                                                   "datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
2579                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2580                                                   "shobj_description(oid, 'pg_database') AS description "
2581
2582                                                   "FROM pg_database "
2583                                                   "WHERE datname = ",
2584                                                   username_subquery);
2585                 appendStringLiteralAH(dbQry, datname, fout);
2586         }
2587         else if (fout->remoteVersion >= 80200)
2588         {
2589                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2590                                                   "(%s datdba) AS dba, "
2591                                                   "pg_encoding_to_char(encoding) AS encoding, "
2592                                                   "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2593                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2594                                                   "shobj_description(oid, 'pg_database') AS description "
2595
2596                                                   "FROM pg_database "
2597                                                   "WHERE datname = ",
2598                                                   username_subquery);
2599                 appendStringLiteralAH(dbQry, datname, fout);
2600         }
2601         else
2602         {
2603                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2604                                                   "(%s datdba) AS dba, "
2605                                                   "pg_encoding_to_char(encoding) AS encoding, "
2606                                                   "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2607                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
2608                                                   "FROM pg_database "
2609                                                   "WHERE datname = ",
2610                                                   username_subquery);
2611                 appendStringLiteralAH(dbQry, datname, fout);
2612         }
2613
2614         res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
2615
2616         i_tableoid = PQfnumber(res, "tableoid");
2617         i_oid = PQfnumber(res, "oid");
2618         i_dba = PQfnumber(res, "dba");
2619         i_encoding = PQfnumber(res, "encoding");
2620         i_collate = PQfnumber(res, "datcollate");
2621         i_ctype = PQfnumber(res, "datctype");
2622         i_frozenxid = PQfnumber(res, "datfrozenxid");
2623         i_minmxid = PQfnumber(res, "datminmxid");
2624         i_tablespace = PQfnumber(res, "tablespace");
2625
2626         dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
2627         dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2628         dba = PQgetvalue(res, 0, i_dba);
2629         encoding = PQgetvalue(res, 0, i_encoding);
2630         collate = PQgetvalue(res, 0, i_collate);
2631         ctype = PQgetvalue(res, 0, i_ctype);
2632         frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
2633         minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
2634         tablespace = PQgetvalue(res, 0, i_tablespace);
2635
2636         appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
2637                                           fmtId(datname));
2638         if (strlen(encoding) > 0)
2639         {
2640                 appendPQExpBufferStr(creaQry, " ENCODING = ");
2641                 appendStringLiteralAH(creaQry, encoding, fout);
2642         }
2643         if (strlen(collate) > 0)
2644         {
2645                 appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
2646                 appendStringLiteralAH(creaQry, collate, fout);
2647         }
2648         if (strlen(ctype) > 0)
2649         {
2650                 appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
2651                 appendStringLiteralAH(creaQry, ctype, fout);
2652         }
2653         if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
2654                 !dopt->outputNoTablespaces)
2655                 appendPQExpBuffer(creaQry, " TABLESPACE = %s",
2656                                                   fmtId(tablespace));
2657         appendPQExpBufferStr(creaQry, ";\n");
2658
2659         if (dopt->binary_upgrade)
2660         {
2661                 appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
2662                 appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
2663                                                   "SET datfrozenxid = '%u', datminmxid = '%u'\n"
2664                                                   "WHERE datname = ",
2665                                                   frozenxid, minmxid);
2666                 appendStringLiteralAH(creaQry, datname, fout);
2667                 appendPQExpBufferStr(creaQry, ";\n");
2668
2669         }
2670
2671         appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
2672                                           fmtId(datname));
2673
2674         dbDumpId = createDumpId();
2675
2676         ArchiveEntry(fout,
2677                                  dbCatId,               /* catalog ID */
2678                                  dbDumpId,              /* dump ID */
2679                                  datname,               /* Name */
2680                                  NULL,                  /* Namespace */
2681                                  NULL,                  /* Tablespace */
2682                                  dba,                   /* Owner */
2683                                  false,                 /* with oids */
2684                                  "DATABASE",    /* Desc */
2685                                  SECTION_PRE_DATA,      /* Section */
2686                                  creaQry->data, /* Create */
2687                                  delQry->data,  /* Del */
2688                                  NULL,                  /* Copy */
2689                                  NULL,                  /* Deps */
2690                                  0,                             /* # Deps */
2691                                  NULL,                  /* Dumper */
2692                                  NULL);                 /* Dumper Arg */
2693
2694         /*
2695          * pg_largeobject and pg_largeobject_metadata come from the old system
2696          * intact, so set their relfrozenxids and relminmxids.
2697          */
2698         if (dopt->binary_upgrade)
2699         {
2700                 PGresult   *lo_res;
2701                 PQExpBuffer loFrozenQry = createPQExpBuffer();
2702                 PQExpBuffer loOutQry = createPQExpBuffer();
2703                 int                     i_relfrozenxid,
2704                                         i_relminmxid;
2705
2706                 /*
2707                  * pg_largeobject
2708                  */
2709                 if (fout->remoteVersion >= 90300)
2710                         appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid\n"
2711                                                           "FROM pg_catalog.pg_class\n"
2712                                                           "WHERE oid = %u;\n",
2713                                                           LargeObjectRelationId);
2714                 else
2715                         appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid\n"
2716                                                           "FROM pg_catalog.pg_class\n"
2717                                                           "WHERE oid = %u;\n",
2718                                                           LargeObjectRelationId);
2719
2720                 lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2721
2722                 i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2723                 i_relminmxid = PQfnumber(lo_res, "relminmxid");
2724
2725                 appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
2726                 appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2727                                                   "SET relfrozenxid = '%u', relminmxid = '%u'\n"
2728                                                   "WHERE oid = %u;\n",
2729                                                   atoi(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2730                                                   atoi(PQgetvalue(lo_res, 0, i_relminmxid)),
2731                                                   LargeObjectRelationId);
2732                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
2733                                          "pg_largeobject", NULL, NULL, "",
2734                                          false, "pg_largeobject", SECTION_PRE_DATA,
2735                                          loOutQry->data, "", NULL,
2736                                          NULL, 0,
2737                                          NULL, NULL);
2738
2739                 PQclear(lo_res);
2740
2741                 /*
2742                  * pg_largeobject_metadata
2743                  */
2744                 if (fout->remoteVersion >= 90000)
2745                 {
2746                         resetPQExpBuffer(loFrozenQry);
2747                         resetPQExpBuffer(loOutQry);
2748
2749                         if (fout->remoteVersion >= 90300)
2750                                 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid\n"
2751                                                                   "FROM pg_catalog.pg_class\n"
2752                                                                   "WHERE oid = %u;\n",
2753                                                                   LargeObjectMetadataRelationId);
2754                         else
2755                                 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid\n"
2756                                                                   "FROM pg_catalog.pg_class\n"
2757                                                                   "WHERE oid = %u;\n",
2758                                                                   LargeObjectMetadataRelationId);
2759
2760                         lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2761
2762                         i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2763                         i_relminmxid = PQfnumber(lo_res, "relminmxid");
2764
2765                         appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n");
2766                         appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2767                                                           "SET relfrozenxid = '%u', relminmxid = '%u'\n"
2768                                                           "WHERE oid = %u;\n",
2769                                                           atoi(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2770                                                           atoi(PQgetvalue(lo_res, 0, i_relminmxid)),
2771                                                           LargeObjectMetadataRelationId);
2772                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
2773                                                  "pg_largeobject_metadata", NULL, NULL, "",
2774                                                  false, "pg_largeobject_metadata", SECTION_PRE_DATA,
2775                                                  loOutQry->data, "", NULL,
2776                                                  NULL, 0,
2777                                                  NULL, NULL);
2778
2779                         PQclear(lo_res);
2780                 }
2781
2782                 destroyPQExpBuffer(loFrozenQry);
2783                 destroyPQExpBuffer(loOutQry);
2784         }
2785
2786         /* Dump DB comment if any */
2787         if (fout->remoteVersion >= 80200)
2788         {
2789                 /*
2790                  * 8.2 keeps comments on shared objects in a shared table, so we
2791                  * cannot use the dumpComment used for other database objects.
2792                  */
2793                 char       *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
2794
2795                 if (comment && strlen(comment))
2796                 {
2797                         resetPQExpBuffer(dbQry);
2798
2799                         /*
2800                          * Generates warning when loaded into a differently-named
2801                          * database.
2802                          */
2803                         appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", fmtId(datname));
2804                         appendStringLiteralAH(dbQry, comment, fout);
2805                         appendPQExpBufferStr(dbQry, ";\n");
2806
2807                         ArchiveEntry(fout, dbCatId, createDumpId(), datname, NULL, NULL,
2808                                                  dba, false, "COMMENT", SECTION_NONE,
2809                                                  dbQry->data, "", NULL,
2810                                                  &dbDumpId, 1, NULL, NULL);
2811                 }
2812         }
2813         else
2814         {
2815                 resetPQExpBuffer(dbQry);
2816                 appendPQExpBuffer(dbQry, "DATABASE %s", fmtId(datname));
2817                 dumpComment(fout, dbQry->data, NULL, "",
2818                                         dbCatId, 0, dbDumpId);
2819         }
2820
2821         /* Dump shared security label. */
2822         if (!dopt->no_security_labels && fout->remoteVersion >= 90200)
2823         {
2824                 PGresult   *shres;
2825                 PQExpBuffer seclabelQry;
2826
2827                 seclabelQry = createPQExpBuffer();
2828
2829                 buildShSecLabelQuery(conn, "pg_database", dbCatId.oid, seclabelQry);
2830                 shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
2831                 resetPQExpBuffer(seclabelQry);
2832                 emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
2833                 if (strlen(seclabelQry->data))
2834                         ArchiveEntry(fout, dbCatId, createDumpId(), datname, NULL, NULL,
2835                                                  dba, false, "SECURITY LABEL", SECTION_NONE,
2836                                                  seclabelQry->data, "", NULL,
2837                                                  &dbDumpId, 1, NULL, NULL);
2838                 destroyPQExpBuffer(seclabelQry);
2839                 PQclear(shres);
2840         }
2841
2842         PQclear(res);
2843
2844         destroyPQExpBuffer(dbQry);
2845         destroyPQExpBuffer(delQry);
2846         destroyPQExpBuffer(creaQry);
2847 }
2848
2849 /*
2850  * dumpEncoding: put the correct encoding into the archive
2851  */
2852 static void
2853 dumpEncoding(Archive *AH)
2854 {
2855         const char *encname = pg_encoding_to_char(AH->encoding);
2856         PQExpBuffer qry = createPQExpBuffer();
2857
2858         if (g_verbose)
2859                 write_msg(NULL, "saving encoding = %s\n", encname);
2860
2861         appendPQExpBufferStr(qry, "SET client_encoding = ");
2862         appendStringLiteralAH(qry, encname, AH);
2863         appendPQExpBufferStr(qry, ";\n");
2864
2865         ArchiveEntry(AH, nilCatalogId, createDumpId(),
2866                                  "ENCODING", NULL, NULL, "",
2867                                  false, "ENCODING", SECTION_PRE_DATA,
2868                                  qry->data, "", NULL,
2869                                  NULL, 0,
2870                                  NULL, NULL);
2871
2872         destroyPQExpBuffer(qry);
2873 }
2874
2875
2876 /*
2877  * dumpStdStrings: put the correct escape string behavior into the archive
2878  */
2879 static void
2880 dumpStdStrings(Archive *AH)
2881 {
2882         const char *stdstrings = AH->std_strings ? "on" : "off";
2883         PQExpBuffer qry = createPQExpBuffer();
2884
2885         if (g_verbose)
2886                 write_msg(NULL, "saving standard_conforming_strings = %s\n",
2887                                   stdstrings);
2888
2889         appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
2890                                           stdstrings);
2891
2892         ArchiveEntry(AH, nilCatalogId, createDumpId(),
2893                                  "STDSTRINGS", NULL, NULL, "",
2894                                  false, "STDSTRINGS", SECTION_PRE_DATA,
2895                                  qry->data, "", NULL,
2896                                  NULL, 0,
2897                                  NULL, NULL);
2898
2899         destroyPQExpBuffer(qry);
2900 }
2901
2902
2903 /*
2904  * getBlobs:
2905  *      Collect schema-level data about large objects
2906  */
2907 static void
2908 getBlobs(Archive *fout)
2909 {
2910         DumpOptions *dopt = fout->dopt;
2911         PQExpBuffer blobQry = createPQExpBuffer();
2912         BlobInfo   *binfo;
2913         DumpableObject *bdata;
2914         PGresult   *res;
2915         int                     ntups;
2916         int                     i;
2917         int                     i_oid;
2918         int                     i_lomowner;
2919         int                     i_lomacl;
2920         int                     i_rlomacl;
2921         int                     i_initlomacl;
2922         int                     i_initrlomacl;
2923
2924         /* Verbose message */
2925         if (g_verbose)
2926                 write_msg(NULL, "reading large objects\n");
2927
2928         /* Make sure we are in proper schema */
2929         selectSourceSchema(fout, "pg_catalog");
2930
2931         /* Fetch BLOB OIDs, and owner/ACL data if >= 9.0 */
2932         if (fout->remoteVersion >= 90600)
2933         {
2934                 PQExpBuffer acl_subquery = createPQExpBuffer();
2935                 PQExpBuffer racl_subquery = createPQExpBuffer();
2936                 PQExpBuffer init_acl_subquery = createPQExpBuffer();
2937                 PQExpBuffer init_racl_subquery = createPQExpBuffer();
2938
2939                 buildACLQueries(acl_subquery, racl_subquery, init_acl_subquery,
2940                                                 init_racl_subquery, "l.lomacl", "l.lomowner", "'L'",
2941                                                 dopt->binary_upgrade);
2942
2943                 appendPQExpBuffer(blobQry,
2944                                                   "SELECT l.oid, (%s l.lomowner) AS rolname, "
2945                                                   "%s AS lomacl, "
2946                                                   "%s AS rlomacl, "
2947                                                   "%s AS initlomacl, "
2948                                                   "%s AS initrlomacl "
2949                                                   "FROM pg_largeobject_metadata l "
2950                                                   "LEFT JOIN pg_init_privs pip ON "
2951                                                   "(l.oid = pip.objoid "
2952                                                   "AND pip.classoid = 'pg_largeobject'::regclass "
2953                                                   "AND pip.objsubid = 0) ",
2954                                                   username_subquery,
2955                                                   acl_subquery->data,
2956                                                   racl_subquery->data,
2957                                                   init_acl_subquery->data,
2958                                                   init_racl_subquery->data);
2959
2960                 destroyPQExpBuffer(acl_subquery);
2961                 destroyPQExpBuffer(racl_subquery);
2962                 destroyPQExpBuffer(init_acl_subquery);
2963                 destroyPQExpBuffer(init_racl_subquery);
2964         }
2965         else if (fout->remoteVersion >= 90000)
2966                 appendPQExpBuffer(blobQry,
2967                                                   "SELECT oid, (%s lomowner) AS rolname, lomacl, "
2968                                                   "NULL AS rlomacl, NULL AS initlomacl, "
2969                                                   "NULL AS initrlomacl "
2970                                                   " FROM pg_largeobject_metadata",
2971                                                   username_subquery);
2972         else
2973                 appendPQExpBufferStr(blobQry,
2974                                                          "SELECT DISTINCT loid AS oid, "
2975                                                          "NULL::name AS rolname, NULL::oid AS lomacl, "
2976                                                          "NULL::oid AS rlomacl, NULL::oid AS initlomacl, "
2977                                                          "NULL::oid AS initrlomacl "
2978                                                          " FROM pg_largeobject");
2979
2980         res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK);
2981
2982         i_oid = PQfnumber(res, "oid");
2983         i_lomowner = PQfnumber(res, "rolname");
2984         i_lomacl = PQfnumber(res, "lomacl");
2985         i_rlomacl = PQfnumber(res, "rlomacl");
2986         i_initlomacl = PQfnumber(res, "initlomacl");
2987         i_initrlomacl = PQfnumber(res, "initrlomacl");
2988
2989         ntups = PQntuples(res);
2990
2991         /*
2992          * Each large object has its own BLOB archive entry.
2993          */
2994         binfo = (BlobInfo *) pg_malloc(ntups * sizeof(BlobInfo));
2995
2996         for (i = 0; i < ntups; i++)
2997         {
2998                 binfo[i].dobj.objType = DO_BLOB;
2999                 binfo[i].dobj.catId.tableoid = LargeObjectRelationId;
3000                 binfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3001                 AssignDumpId(&binfo[i].dobj);
3002
3003                 binfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oid));
3004                 binfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_lomowner));
3005                 binfo[i].blobacl = pg_strdup(PQgetvalue(res, i, i_lomacl));
3006                 binfo[i].rblobacl = pg_strdup(PQgetvalue(res, i, i_rlomacl));
3007                 binfo[i].initblobacl = pg_strdup(PQgetvalue(res, i, i_initlomacl));
3008                 binfo[i].initrblobacl = pg_strdup(PQgetvalue(res, i, i_initrlomacl));
3009
3010                 if (PQgetisnull(res, i, i_lomacl) &&
3011                         PQgetisnull(res, i, i_rlomacl) &&
3012                         PQgetisnull(res, i, i_initlomacl) &&
3013                         PQgetisnull(res, i, i_initrlomacl))
3014                         binfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
3015
3016                 /*
3017                  * In binary-upgrade mode for blobs, we do *not* dump out the data or
3018                  * the ACLs, should any exist.  The data and ACL (if any) will be
3019                  * copied by pg_upgrade, which simply copies the pg_largeobject and
3020                  * pg_largeobject_metadata tables.
3021                  *
3022                  * We *do* dump out the definition of the blob because we need that to
3023                  * make the restoration of the comments, and anything else, work since
3024                  * pg_upgrade copies the files behind pg_largeobject and
3025                  * pg_largeobject_metadata after the dump is restored.
3026                  */
3027                 if (dopt->binary_upgrade)
3028                         binfo[i].dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL);
3029         }
3030
3031         /*
3032          * If we have any large objects, a "BLOBS" archive entry is needed. This
3033          * is just a placeholder for sorting; it carries no data now.
3034          */
3035         if (ntups > 0)
3036         {
3037                 bdata = (DumpableObject *) pg_malloc(sizeof(DumpableObject));
3038                 bdata->objType = DO_BLOB_DATA;
3039                 bdata->catId = nilCatalogId;
3040                 AssignDumpId(bdata);
3041                 bdata->name = pg_strdup("BLOBS");
3042         }
3043
3044         PQclear(res);
3045         destroyPQExpBuffer(blobQry);
3046 }
3047
3048 /*
3049  * dumpBlob
3050  *
3051  * dump the definition (metadata) of the given large object
3052  */
3053 static void
3054 dumpBlob(Archive *fout, BlobInfo *binfo)
3055 {
3056         PQExpBuffer cquery = createPQExpBuffer();
3057         PQExpBuffer dquery = createPQExpBuffer();
3058
3059         appendPQExpBuffer(cquery,
3060                                           "SELECT pg_catalog.lo_create('%s');\n",
3061                                           binfo->dobj.name);
3062
3063         appendPQExpBuffer(dquery,
3064                                           "SELECT pg_catalog.lo_unlink('%s');\n",
3065                                           binfo->dobj.name);
3066
3067         if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3068                 ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3069                                          binfo->dobj.name,
3070                                          NULL, NULL,
3071                                          binfo->rolname, false,
3072                                          "BLOB", SECTION_PRE_DATA,
3073                                          cquery->data, dquery->data, NULL,
3074                                          NULL, 0,
3075                                          NULL, NULL);
3076
3077         /* set up tag for comment and/or ACL */
3078         resetPQExpBuffer(cquery);
3079         appendPQExpBuffer(cquery, "LARGE OBJECT %s", binfo->dobj.name);
3080
3081         /* Dump comment if any */
3082         if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3083                 dumpComment(fout, cquery->data,
3084                                         NULL, binfo->rolname,
3085                                         binfo->dobj.catId, 0, binfo->dobj.dumpId);
3086
3087         /* Dump security label if any */
3088         if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3089                 dumpSecLabel(fout, cquery->data,
3090                                          NULL, binfo->rolname,
3091                                          binfo->dobj.catId, 0, binfo->dobj.dumpId);
3092
3093         /* Dump ACL if any */
3094         if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3095                 dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
3096                                 binfo->dobj.name, NULL, cquery->data,
3097                                 NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3098                                 binfo->initblobacl, binfo->initrblobacl);
3099
3100         destroyPQExpBuffer(cquery);
3101         destroyPQExpBuffer(dquery);
3102 }
3103
3104 /*
3105  * dumpBlobs:
3106  *      dump the data contents of all large objects
3107  */
3108 static int
3109 dumpBlobs(Archive *fout, void *arg)
3110 {
3111         const char *blobQry;
3112         const char *blobFetchQry;
3113         PGconn     *conn = GetConnection(fout);
3114         PGresult   *res;
3115         char            buf[LOBBUFSIZE];
3116         int                     ntups;
3117         int                     i;
3118         int                     cnt;
3119
3120         if (g_verbose)
3121                 write_msg(NULL, "saving large objects\n");
3122
3123         /* Make sure we are in proper schema */
3124         selectSourceSchema(fout, "pg_catalog");
3125
3126         /*
3127          * Currently, we re-fetch all BLOB OIDs using a cursor.  Consider scanning
3128          * the already-in-memory dumpable objects instead...
3129          */
3130         if (fout->remoteVersion >= 90000)
3131                 blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_largeobject_metadata";
3132         else
3133                 blobQry = "DECLARE bloboid CURSOR FOR SELECT DISTINCT loid FROM pg_largeobject";
3134
3135         ExecuteSqlStatement(fout, blobQry);
3136
3137         /* Command to fetch from cursor */
3138         blobFetchQry = "FETCH 1000 IN bloboid";
3139
3140         do
3141         {
3142                 /* Do a fetch */
3143                 res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3144
3145                 /* Process the tuples, if any */
3146                 ntups = PQntuples(res);
3147                 for (i = 0; i < ntups; i++)
3148                 {
3149                         Oid                     blobOid;
3150                         int                     loFd;
3151
3152                         blobOid = atooid(PQgetvalue(res, i, 0));
3153                         /* Open the BLOB */
3154                         loFd = lo_open(conn, blobOid, INV_READ);
3155                         if (loFd == -1)
3156                                 exit_horribly(NULL, "could not open large object %u: %s",
3157                                                           blobOid, PQerrorMessage(conn));
3158
3159                         StartBlob(fout, blobOid);
3160
3161                         /* Now read it in chunks, sending data to archive */
3162                         do
3163                         {
3164                                 cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3165                                 if (cnt < 0)
3166                                         exit_horribly(NULL, "error reading large object %u: %s",
3167                                                                   blobOid, PQerrorMessage(conn));
3168
3169                                 WriteData(fout, buf, cnt);
3170                         } while (cnt > 0);
3171
3172                         lo_close(conn, loFd);
3173
3174                         EndBlob(fout, blobOid);
3175                 }
3176
3177                 PQclear(res);
3178         } while (ntups > 0);
3179
3180         return 1;
3181 }
3182
3183 /*
3184  * getPolicies
3185  *        get information about policies on a dumpable table.
3186  */
3187 void
3188 getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
3189 {
3190         PQExpBuffer query;
3191         PGresult   *res;
3192         PolicyInfo *polinfo;
3193         int                     i_oid;
3194         int                     i_tableoid;
3195         int                     i_polname;
3196         int                     i_polcmd;
3197         int                     i_polpermissive;
3198         int                     i_polroles;
3199         int                     i_polqual;
3200         int                     i_polwithcheck;
3201         int                     i,
3202                                 j,
3203                                 ntups;
3204
3205         if (fout->remoteVersion < 90500)
3206                 return;
3207
3208         query = createPQExpBuffer();
3209
3210         for (i = 0; i < numTables; i++)
3211         {
3212                 TableInfo  *tbinfo = &tblinfo[i];
3213
3214                 /* Ignore row security on tables not to be dumped */
3215                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
3216                         continue;
3217
3218                 if (g_verbose)
3219                         write_msg(NULL, "reading row security enabled for table \"%s.%s\"\n",
3220                                           tbinfo->dobj.namespace->dobj.name,
3221                                           tbinfo->dobj.name);
3222
3223                 /*
3224                  * Get row security enabled information for the table. We represent
3225                  * RLS enabled on a table by creating PolicyInfo object with an empty
3226                  * policy.
3227                  */
3228                 if (tbinfo->rowsec)
3229                 {
3230                         /*
3231                          * Note: use tableoid 0 so that this object won't be mistaken for
3232                          * something that pg_depend entries apply to.
3233                          */
3234                         polinfo = pg_malloc(sizeof(PolicyInfo));
3235                         polinfo->dobj.objType = DO_POLICY;
3236                         polinfo->dobj.catId.tableoid = 0;
3237                         polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
3238                         AssignDumpId(&polinfo->dobj);
3239                         polinfo->dobj.namespace = tbinfo->dobj.namespace;
3240                         polinfo->dobj.name = pg_strdup(tbinfo->dobj.name);
3241                         polinfo->poltable = tbinfo;
3242                         polinfo->polname = NULL;
3243                         polinfo->polcmd = '\0';
3244                         polinfo->polpermissive = 0;
3245                         polinfo->polroles = NULL;
3246                         polinfo->polqual = NULL;
3247                         polinfo->polwithcheck = NULL;
3248                 }
3249
3250                 if (g_verbose)
3251                         write_msg(NULL, "reading policies for table \"%s.%s\"\n",
3252                                           tbinfo->dobj.namespace->dobj.name,
3253                                           tbinfo->dobj.name);
3254
3255                 /*
3256                  * select table schema to ensure regproc name is qualified if needed
3257                  */
3258                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
3259
3260                 resetPQExpBuffer(query);
3261
3262                 /* Get the policies for the table. */
3263                 if (fout->remoteVersion >= 100000)
3264                         appendPQExpBuffer(query,
3265                                                           "SELECT oid, tableoid, pol.polname, pol.polcmd, pol.polpermissive, "
3266                                                           "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3267                                                           "   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, "
3268                                                           "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3269                                                           "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3270                                                           "FROM pg_catalog.pg_policy pol "
3271                                                           "WHERE polrelid = '%u'",
3272                                                           tbinfo->dobj.catId.oid);
3273                 else
3274                         appendPQExpBuffer(query,
3275                                                           "SELECT oid, tableoid, pol.polname, pol.polcmd, 't' as polpermissive, "
3276                                                           "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3277                                                           "   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, "
3278                                                           "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3279                                                           "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3280                                                           "FROM pg_catalog.pg_policy pol "
3281                                                           "WHERE polrelid = '%u'",
3282                                                           tbinfo->dobj.catId.oid);
3283                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3284
3285                 ntups = PQntuples(res);
3286
3287                 if (ntups == 0)
3288                 {
3289                         /*
3290                          * No explicit policies to handle (only the default-deny policy,
3291                          * which is handled as part of the table definition).  Clean up
3292                          * and return.
3293                          */
3294                         PQclear(res);
3295                         continue;
3296                 }
3297
3298                 i_oid = PQfnumber(res, "oid");
3299                 i_tableoid = PQfnumber(res, "tableoid");
3300                 i_polname = PQfnumber(res, "polname");
3301                 i_polcmd = PQfnumber(res, "polcmd");
3302                 i_polpermissive = PQfnumber(res, "polpermissive");
3303                 i_polroles = PQfnumber(res, "polroles");
3304                 i_polqual = PQfnumber(res, "polqual");
3305                 i_polwithcheck = PQfnumber(res, "polwithcheck");
3306
3307                 polinfo = pg_malloc(ntups * sizeof(PolicyInfo));
3308
3309                 for (j = 0; j < ntups; j++)
3310                 {
3311                         polinfo[j].dobj.objType = DO_POLICY;
3312                         polinfo[j].dobj.catId.tableoid =
3313                                 atooid(PQgetvalue(res, j, i_tableoid));
3314                         polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
3315                         AssignDumpId(&polinfo[j].dobj);
3316                         polinfo[j].dobj.namespace = tbinfo->dobj.namespace;
3317                         polinfo[j].poltable = tbinfo;
3318                         polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname));
3319                         polinfo[j].dobj.name = pg_strdup(polinfo[j].polname);
3320
3321                         polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd));
3322                         polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't';
3323
3324                         if (PQgetisnull(res, j, i_polroles))
3325                                 polinfo[j].polroles = NULL;
3326                         else
3327                                 polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles));
3328
3329                         if (PQgetisnull(res, j, i_polqual))
3330                                 polinfo[j].polqual = NULL;
3331                         else
3332                                 polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual));
3333
3334                         if (PQgetisnull(res, j, i_polwithcheck))
3335                                 polinfo[j].polwithcheck = NULL;
3336                         else
3337                                 polinfo[j].polwithcheck
3338                                         = pg_strdup(PQgetvalue(res, j, i_polwithcheck));
3339                 }
3340                 PQclear(res);
3341         }
3342         destroyPQExpBuffer(query);
3343 }
3344
3345 /*
3346  * dumpPolicy
3347  *        dump the definition of the given policy
3348  */
3349 static void
3350 dumpPolicy(Archive *fout, PolicyInfo *polinfo)
3351 {
3352         DumpOptions *dopt = fout->dopt;
3353         TableInfo  *tbinfo = polinfo->poltable;
3354         PQExpBuffer query;
3355         PQExpBuffer delqry;
3356         const char *cmd;
3357         char       *tag;
3358
3359         if (dopt->dataOnly)
3360                 return;
3361
3362         /*
3363          * If polname is NULL, then this record is just indicating that ROW LEVEL
3364          * SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE
3365          * ROW LEVEL SECURITY.
3366          */
3367         if (polinfo->polname == NULL)
3368         {
3369                 query = createPQExpBuffer();
3370
3371                 appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;",
3372                                                   fmtId(polinfo->dobj.name));
3373
3374                 if (polinfo->dobj.dump & DUMP_COMPONENT_POLICY)
3375                         ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
3376                                                  polinfo->dobj.name,
3377                                                  polinfo->dobj.namespace->dobj.name,
3378                                                  NULL,
3379                                                  tbinfo->rolname, false,
3380                                                  "ROW SECURITY", SECTION_POST_DATA,
3381                                                  query->data, "", NULL,
3382                                                  NULL, 0,
3383                                                  NULL, NULL);
3384
3385                 destroyPQExpBuffer(query);
3386                 return;
3387         }
3388
3389         if (polinfo->polcmd == '*')
3390                 cmd = "";
3391         else if (polinfo->polcmd == 'r')
3392                 cmd = " FOR SELECT";
3393         else if (polinfo->polcmd == 'a')
3394                 cmd = " FOR INSERT";
3395         else if (polinfo->polcmd == 'w')
3396                 cmd = " FOR UPDATE";
3397         else if (polinfo->polcmd == 'd')
3398                 cmd = " FOR DELETE";
3399         else
3400         {
3401                 write_msg(NULL, "unexpected policy command type: %c\n",
3402                                   polinfo->polcmd);
3403                 exit_nicely(1);
3404         }
3405
3406         query = createPQExpBuffer();
3407         delqry = createPQExpBuffer();
3408
3409         appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname));
3410
3411         appendPQExpBuffer(query, " ON %s%s%s", fmtId(tbinfo->dobj.name),
3412                                           !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd);
3413
3414         if (polinfo->polroles != NULL)
3415                 appendPQExpBuffer(query, " TO %s", polinfo->polroles);
3416
3417         if (polinfo->polqual != NULL)
3418                 appendPQExpBuffer(query, " USING (%s)", polinfo->polqual);
3419
3420         if (polinfo->polwithcheck != NULL)
3421                 appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck);
3422
3423         appendPQExpBuffer(query, ";\n");
3424
3425         appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname));
3426         appendPQExpBuffer(delqry, " ON %s;\n", fmtId(tbinfo->dobj.name));
3427
3428         tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
3429
3430         if (polinfo->dobj.dump & DUMP_COMPONENT_POLICY)
3431                 ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
3432                                          tag,
3433                                          polinfo->dobj.namespace->dobj.name,
3434                                          NULL,
3435                                          tbinfo->rolname, false,
3436                                          "POLICY", SECTION_POST_DATA,
3437                                          query->data, delqry->data, NULL,
3438                                          NULL, 0,
3439                                          NULL, NULL);
3440
3441         free(tag);
3442         destroyPQExpBuffer(query);
3443         destroyPQExpBuffer(delqry);
3444 }
3445
3446 /*
3447  * getPublications
3448  *        get information about publications
3449  */
3450 void
3451 getPublications(Archive *fout)
3452 {
3453         DumpOptions *dopt = fout->dopt;
3454         PQExpBuffer query;
3455         PGresult   *res;
3456         PublicationInfo *pubinfo;
3457         int                     i_tableoid;
3458         int                     i_oid;
3459         int                     i_pubname;
3460         int                     i_rolname;
3461         int                     i_puballtables;
3462         int                     i_pubinsert;
3463         int                     i_pubupdate;
3464         int                     i_pubdelete;
3465         int                     i,
3466                                 ntups;
3467
3468         if (dopt->no_publications || fout->remoteVersion < 100000)
3469                 return;
3470
3471         query = createPQExpBuffer();
3472
3473         resetPQExpBuffer(query);
3474
3475         /* Get the publications. */
3476         appendPQExpBuffer(query,
3477                                           "SELECT p.tableoid, p.oid, p.pubname, "
3478                                           "(%s p.pubowner) AS rolname, "
3479                                           "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete "
3480                                           "FROM pg_catalog.pg_publication p",
3481                                           username_subquery);
3482
3483         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3484
3485         ntups = PQntuples(res);
3486
3487         i_tableoid = PQfnumber(res, "tableoid");
3488         i_oid = PQfnumber(res, "oid");
3489         i_pubname = PQfnumber(res, "pubname");
3490         i_rolname = PQfnumber(res, "rolname");
3491         i_puballtables = PQfnumber(res, "puballtables");
3492         i_pubinsert = PQfnumber(res, "pubinsert");
3493         i_pubupdate = PQfnumber(res, "pubupdate");
3494         i_pubdelete = PQfnumber(res, "pubdelete");
3495
3496         pubinfo = pg_malloc(ntups * sizeof(PublicationInfo));
3497
3498         for (i = 0; i < ntups; i++)
3499         {
3500                 pubinfo[i].dobj.objType = DO_PUBLICATION;
3501                 pubinfo[i].dobj.catId.tableoid =
3502                         atooid(PQgetvalue(res, i, i_tableoid));
3503                 pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3504                 AssignDumpId(&pubinfo[i].dobj);
3505                 pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname));
3506                 pubinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3507                 pubinfo[i].puballtables =
3508                         (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0);
3509                 pubinfo[i].pubinsert =
3510                         (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0);
3511                 pubinfo[i].pubupdate =
3512                         (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0);
3513                 pubinfo[i].pubdelete =
3514                         (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
3515
3516                 if (strlen(pubinfo[i].rolname) == 0)
3517                         write_msg(NULL, "WARNING: owner of publication \"%s\" appears to be invalid\n",
3518                                           pubinfo[i].dobj.name);
3519
3520                 /* Decide whether we want to dump it */
3521                 selectDumpableObject(&(pubinfo[i].dobj), fout);
3522         }
3523         PQclear(res);
3524
3525         destroyPQExpBuffer(query);
3526 }
3527
3528 /*
3529  * dumpPublication
3530  *        dump the definition of the given publication
3531  */
3532 static void
3533 dumpPublication(Archive *fout, PublicationInfo *pubinfo)
3534 {
3535         PQExpBuffer delq;
3536         PQExpBuffer query;
3537         PQExpBuffer labelq;
3538         bool            first = true;
3539
3540         if (!(pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3541                 return;
3542
3543         delq = createPQExpBuffer();
3544         query = createPQExpBuffer();
3545         labelq = createPQExpBuffer();
3546
3547         appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n",
3548                                           fmtId(pubinfo->dobj.name));
3549
3550         appendPQExpBuffer(query, "CREATE PUBLICATION %s",
3551                                           fmtId(pubinfo->dobj.name));
3552
3553         appendPQExpBuffer(labelq, "PUBLICATION %s", fmtId(pubinfo->dobj.name));
3554
3555         if (pubinfo->puballtables)
3556                 appendPQExpBufferStr(query, " FOR ALL TABLES");
3557
3558         appendPQExpBufferStr(query, " WITH (publish = '");
3559         if (pubinfo->pubinsert)
3560         {
3561                 appendPQExpBufferStr(query, "insert");
3562                 first = false;
3563         }
3564
3565         if (pubinfo->pubupdate)
3566         {
3567                 if (!first)
3568                         appendPQExpBufferStr(query, ", ");
3569
3570                 appendPQExpBufferStr(query, "update");
3571                 first = false;
3572         }
3573
3574         if (pubinfo->pubdelete)
3575         {
3576                 if (!first)
3577                         appendPQExpBufferStr(query, ", ");
3578
3579                 appendPQExpBufferStr(query, "delete");
3580                 first = false;
3581         }
3582
3583         appendPQExpBufferStr(query, "');\n");
3584
3585         ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
3586                                  pubinfo->dobj.name,
3587                                  NULL,
3588                                  NULL,
3589                                  pubinfo->rolname, false,
3590                                  "PUBLICATION", SECTION_POST_DATA,
3591                                  query->data, delq->data, NULL,
3592                                  NULL, 0,
3593                                  NULL, NULL);
3594
3595         if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3596                 dumpComment(fout, labelq->data,
3597                                         NULL, pubinfo->rolname,
3598                                         pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
3599
3600         if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3601                 dumpSecLabel(fout, labelq->data,
3602                                          NULL, pubinfo->rolname,
3603                                          pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
3604
3605         destroyPQExpBuffer(delq);
3606         destroyPQExpBuffer(query);
3607 }
3608
3609 /*
3610  * getPublicationTables
3611  *        get information about publication membership for dumpable tables.
3612  */
3613 void
3614 getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
3615 {
3616         PQExpBuffer query;
3617         PGresult   *res;
3618         PublicationRelInfo *pubrinfo;
3619         int                     i_tableoid;
3620         int                     i_oid;
3621         int                     i_pubname;
3622         int                     i,
3623                                 j,
3624                                 ntups;
3625
3626         if (fout->remoteVersion < 100000)
3627                 return;
3628
3629         query = createPQExpBuffer();
3630
3631         for (i = 0; i < numTables; i++)
3632         {
3633                 TableInfo  *tbinfo = &tblinfo[i];
3634
3635                 /* Only plain tables can be aded to publications. */
3636                 if (tbinfo->relkind != RELKIND_RELATION)
3637                         continue;
3638
3639                 /*
3640                  * Ignore publication membership of tables whose definitions are not
3641                  * to be dumped.
3642                  */
3643                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3644                         continue;
3645
3646                 if (g_verbose)
3647                         write_msg(NULL, "reading publication membership for table \"%s.%s\"\n",
3648                                           tbinfo->dobj.namespace->dobj.name,
3649                                           tbinfo->dobj.name);
3650
3651                 resetPQExpBuffer(query);
3652
3653                 /* Get the publication membership for the table. */
3654                 appendPQExpBuffer(query,
3655                                                   "SELECT pr.tableoid, pr.oid, p.pubname "
3656                                                   "FROM pg_catalog.pg_publication_rel pr,"
3657                                                   "     pg_catalog.pg_publication p "
3658                                                   "WHERE pr.prrelid = '%u'"
3659                                                   "  AND p.oid = pr.prpubid",
3660                                                   tbinfo->dobj.catId.oid);
3661                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3662
3663                 ntups = PQntuples(res);
3664
3665                 if (ntups == 0)
3666                 {
3667                         /*
3668                          * Table is not member of any publications. Clean up and return.
3669                          */
3670                         PQclear(res);
3671                         continue;
3672                 }
3673
3674                 i_tableoid = PQfnumber(res, "tableoid");
3675                 i_oid = PQfnumber(res, "oid");
3676                 i_pubname = PQfnumber(res, "pubname");
3677
3678                 pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo));
3679
3680                 for (j = 0; j < ntups; j++)
3681                 {
3682                         pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
3683                         pubrinfo[j].dobj.catId.tableoid =
3684                                 atooid(PQgetvalue(res, j, i_tableoid));
3685                         pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
3686                         AssignDumpId(&pubrinfo[j].dobj);
3687                         pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
3688                         pubrinfo[j].dobj.name = tbinfo->dobj.name;
3689                         pubrinfo[j].pubname = pg_strdup(PQgetvalue(res, j, i_pubname));
3690                         pubrinfo[j].pubtable = tbinfo;
3691
3692                         /* Decide whether we want to dump it */
3693                         selectDumpablePublicationTable(&(pubrinfo[j].dobj), fout);
3694                 }
3695                 PQclear(res);
3696         }
3697         destroyPQExpBuffer(query);
3698 }
3699
3700 /*
3701  * dumpPublicationTable
3702  *        dump the definition of the given publication table mapping
3703  */
3704 static void
3705 dumpPublicationTable(Archive *fout, PublicationRelInfo *pubrinfo)
3706 {
3707         TableInfo  *tbinfo = pubrinfo->pubtable;
3708         PQExpBuffer query;
3709         char       *tag;
3710
3711         if (!(pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3712                 return;
3713
3714         tag = psprintf("%s %s", pubrinfo->pubname, tbinfo->dobj.name);
3715
3716         query = createPQExpBuffer();
3717
3718         appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
3719                                           fmtId(pubrinfo->pubname));
3720         appendPQExpBuffer(query, " %s;",
3721                                           fmtId(tbinfo->dobj.name));
3722
3723         /*
3724          * There is no point in creating drop query as drop query as the drop is
3725          * done by table drop.
3726          */
3727         ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
3728                                  tag,
3729                                  tbinfo->dobj.namespace->dobj.name,
3730                                  NULL,
3731                                  "", false,
3732                                  "PUBLICATION TABLE", SECTION_POST_DATA,
3733                                  query->data, "", NULL,
3734                                  NULL, 0,
3735                                  NULL, NULL);
3736
3737         free(tag);
3738         destroyPQExpBuffer(query);
3739 }
3740
3741 /*
3742  * Is the currently connected user a superuser?
3743  */
3744 static bool
3745 is_superuser(Archive *fout)
3746 {
3747         ArchiveHandle *AH = (ArchiveHandle *) fout;
3748         const char *val;
3749
3750         val = PQparameterStatus(AH->connection, "is_superuser");
3751
3752         if (val && strcmp(val, "on") == 0)
3753                 return true;
3754
3755         return false;
3756 }
3757
3758 /*
3759  * getSubscriptions
3760  *        get information about subscriptions
3761  */
3762 void
3763 getSubscriptions(Archive *fout)
3764 {
3765         DumpOptions *dopt = fout->dopt;
3766         PQExpBuffer query;
3767         PGresult   *res;
3768         SubscriptionInfo *subinfo;
3769         int                     i_tableoid;
3770         int                     i_oid;
3771         int                     i_subname;
3772         int                     i_rolname;
3773         int                     i_subconninfo;
3774         int                     i_subslotname;
3775         int                     i_subsynccommit;
3776         int                     i_subpublications;
3777         int                     i,
3778                                 ntups;
3779
3780         if (dopt->no_subscriptions || fout->remoteVersion < 100000)
3781                 return;
3782
3783         if (!is_superuser(fout))
3784         {
3785                 int                     n;
3786
3787                 res = ExecuteSqlQuery(fout,
3788                                                           "SELECT count(*) FROM pg_subscription "
3789                                                           "WHERE subdbid = (SELECT oid FROM pg_catalog.pg_database"
3790                                                           "                 WHERE datname = current_database())",
3791                                                           PGRES_TUPLES_OK);
3792                 n = atoi(PQgetvalue(res, 0, 0));
3793                 if (n > 0)
3794                         write_msg(NULL, "WARNING: subscriptions not dumped because current user is not a superuser\n");
3795                 PQclear(res);
3796                 return;
3797         }
3798
3799         query = createPQExpBuffer();
3800
3801         resetPQExpBuffer(query);
3802
3803         /* Get the subscriptions in current database. */
3804         appendPQExpBuffer(query,
3805                                           "SELECT s.tableoid, s.oid, s.subname,"
3806                                           "(%s s.subowner) AS rolname, "
3807                                           " s.subconninfo, s.subslotname, s.subsynccommit, "
3808                                           " s.subpublications "
3809                                           "FROM pg_catalog.pg_subscription s "
3810                                           "WHERE s.subdbid = (SELECT oid FROM pg_catalog.pg_database"
3811                                           "                   WHERE datname = current_database())",
3812                                           username_subquery);
3813         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3814
3815         ntups = PQntuples(res);
3816
3817         i_tableoid = PQfnumber(res, "tableoid");
3818         i_oid = PQfnumber(res, "oid");
3819         i_subname = PQfnumber(res, "subname");
3820         i_rolname = PQfnumber(res, "rolname");
3821         i_subconninfo = PQfnumber(res, "subconninfo");
3822         i_subslotname = PQfnumber(res, "subslotname");
3823         i_subsynccommit = PQfnumber(res, "subsynccommit");
3824         i_subpublications = PQfnumber(res, "subpublications");
3825
3826         subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo));
3827
3828         for (i = 0; i < ntups; i++)
3829         {
3830                 subinfo[i].dobj.objType = DO_SUBSCRIPTION;
3831                 subinfo[i].dobj.catId.tableoid =
3832                         atooid(PQgetvalue(res, i, i_tableoid));
3833                 subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3834                 AssignDumpId(&subinfo[i].dobj);
3835                 subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
3836                 subinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3837                 subinfo[i].subconninfo = pg_strdup(PQgetvalue(res, i, i_subconninfo));
3838                 if (PQgetisnull(res, i, i_subslotname))
3839                         subinfo[i].subslotname = NULL;
3840                 else
3841                         subinfo[i].subslotname = pg_strdup(PQgetvalue(res, i, i_subslotname));
3842                 subinfo[i].subsynccommit =
3843                         pg_strdup(PQgetvalue(res, i, i_subsynccommit));
3844                 subinfo[i].subpublications =
3845                         pg_strdup(PQgetvalue(res, i, i_subpublications));
3846
3847                 if (strlen(subinfo[i].rolname) == 0)
3848                         write_msg(NULL, "WARNING: owner of subscription \"%s\" appears to be invalid\n",
3849                                           subinfo[i].dobj.name);
3850
3851                 /* Decide whether we want to dump it */
3852                 selectDumpableObject(&(subinfo[i].dobj), fout);
3853         }
3854         PQclear(res);
3855
3856         destroyPQExpBuffer(query);
3857 }
3858
3859 /*
3860  * dumpSubscription
3861  *        dump the definition of the given subscription
3862  */
3863 static void
3864 dumpSubscription(Archive *fout, SubscriptionInfo *subinfo)
3865 {
3866         PQExpBuffer delq;
3867         PQExpBuffer query;
3868         PQExpBuffer labelq;
3869         PQExpBuffer publications;
3870         char      **pubnames = NULL;
3871         int                     npubnames = 0;
3872         int                     i;
3873
3874         if (!(subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3875                 return;
3876
3877         delq = createPQExpBuffer();
3878         query = createPQExpBuffer();
3879         labelq = createPQExpBuffer();
3880
3881         appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n",
3882                                           fmtId(subinfo->dobj.name));
3883
3884         appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s CONNECTION ",
3885                                           fmtId(subinfo->dobj.name));
3886         appendStringLiteralAH(query, subinfo->subconninfo, fout);
3887
3888         /* Build list of quoted publications and append them to query. */
3889         if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames))
3890         {
3891                 write_msg(NULL,
3892                                   "WARNING: could not parse subpublications array\n");
3893                 if (pubnames)
3894                         free(pubnames);
3895                 pubnames = NULL;
3896                 npubnames = 0;
3897         }
3898
3899         publications = createPQExpBuffer();
3900         for (i = 0; i < npubnames; i++)
3901         {
3902                 if (i > 0)
3903                         appendPQExpBufferStr(publications, ", ");
3904
3905                 appendPQExpBufferStr(publications, fmtId(pubnames[i]));
3906         }
3907
3908         appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data);
3909         if (subinfo->subslotname)
3910                 appendStringLiteralAH(query, subinfo->subslotname, fout);
3911         else
3912                 appendPQExpBufferStr(query, "NONE");
3913
3914         if (strcmp(subinfo->subsynccommit, "off") != 0)
3915                 appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));
3916
3917         appendPQExpBufferStr(query, ");\n");
3918
3919         appendPQExpBuffer(labelq, "SUBSCRIPTION %s", fmtId(subinfo->dobj.name));
3920
3921         ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
3922                                  subinfo->dobj.name,
3923                                  NULL,
3924                                  NULL,
3925                                  subinfo->rolname, false,
3926                                  "SUBSCRIPTION", SECTION_POST_DATA,
3927                                  query->data, delq->data, NULL,
3928                                  NULL, 0,
3929                                  NULL, NULL);
3930
3931         if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3932                 dumpComment(fout, labelq->data,
3933                                         NULL, subinfo->rolname,
3934                                         subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
3935
3936         if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3937                 dumpSecLabel(fout, labelq->data,
3938                                          NULL, subinfo->rolname,
3939                                          subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
3940
3941         destroyPQExpBuffer(publications);
3942         if (pubnames)
3943                 free(pubnames);
3944
3945         destroyPQExpBuffer(delq);
3946         destroyPQExpBuffer(query);
3947 }
3948
3949 static void
3950 binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
3951                                                                                  PQExpBuffer upgrade_buffer,
3952                                                                                  Oid pg_type_oid)
3953 {
3954         PQExpBuffer upgrade_query = createPQExpBuffer();
3955         PGresult   *upgrade_res;
3956         Oid                     pg_type_array_oid;
3957
3958         appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
3959         appendPQExpBuffer(upgrade_buffer,
3960                                           "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
3961                                           pg_type_oid);
3962
3963         /* we only support old >= 8.3 for binary upgrades */
3964         appendPQExpBuffer(upgrade_query,
3965                                           "SELECT typarray "
3966                                           "FROM pg_catalog.pg_type "
3967                                           "WHERE pg_type.oid = '%u'::pg_catalog.oid;",
3968                                           pg_type_oid);
3969
3970         upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
3971
3972         pg_type_array_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "typarray")));
3973
3974         if (OidIsValid(pg_type_array_oid))
3975         {
3976                 appendPQExpBufferStr(upgrade_buffer,
3977                                                          "\n-- For binary upgrade, must preserve pg_type array oid\n");
3978                 appendPQExpBuffer(upgrade_buffer,
3979                                                   "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
3980                                                   pg_type_array_oid);
3981         }
3982
3983         PQclear(upgrade_res);
3984         destroyPQExpBuffer(upgrade_query);
3985 }
3986
3987 static bool
3988 binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
3989                                                                                 PQExpBuffer upgrade_buffer,
3990                                                                                 Oid pg_rel_oid)
3991 {
3992         PQExpBuffer upgrade_query = createPQExpBuffer();
3993         PGresult   *upgrade_res;
3994         Oid                     pg_type_oid;
3995         bool            toast_set = false;
3996
3997         /* we only support old >= 8.3 for binary upgrades */
3998         appendPQExpBuffer(upgrade_query,
3999                                           "SELECT c.reltype AS crel, t.reltype AS trel "
4000                                           "FROM pg_catalog.pg_class c "
4001                                           "LEFT JOIN pg_catalog.pg_class t ON "
4002                                           "  (c.reltoastrelid = t.oid) "
4003                                           "WHERE c.oid = '%u'::pg_catalog.oid;",
4004                                           pg_rel_oid);
4005
4006         upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4007
4008         pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4009
4010         binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4011                                                                                          pg_type_oid);
4012
4013         if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
4014         {
4015                 /* Toast tables do not have pg_type array rows */
4016                 Oid                     pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
4017                                                                                                                   PQfnumber(upgrade_res, "trel")));
4018
4019                 appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
4020                 appendPQExpBuffer(upgrade_buffer,
4021                                                   "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4022                                                   pg_type_toast_oid);
4023
4024                 toast_set = true;
4025         }
4026
4027         PQclear(upgrade_res);
4028         destroyPQExpBuffer(upgrade_query);
4029
4030         return toast_set;
4031 }
4032
4033 static void
4034 binary_upgrade_set_pg_class_oids(Archive *fout,
4035                                                                  PQExpBuffer upgrade_buffer, Oid pg_class_oid,
4036                                                                  bool is_index)
4037 {
4038         PQExpBuffer upgrade_query = createPQExpBuffer();
4039         PGresult   *upgrade_res;
4040         Oid                     pg_class_reltoastrelid;
4041         Oid                     pg_index_indexrelid;
4042
4043         appendPQExpBuffer(upgrade_query,
4044                                           "SELECT c.reltoastrelid, i.indexrelid "
4045                                           "FROM pg_catalog.pg_class c LEFT JOIN "
4046                                           "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4047                                           "WHERE c.oid = '%u'::pg_catalog.oid;",
4048                                           pg_class_oid);
4049
4050         upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4051
4052         pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
4053         pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
4054
4055         appendPQExpBufferStr(upgrade_buffer,
4056                                                  "\n-- For binary upgrade, must preserve pg_class oids\n");
4057
4058         if (!is_index)
4059         {
4060                 appendPQExpBuffer(upgrade_buffer,
4061                                                   "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4062                                                   pg_class_oid);
4063                 /* only tables have toast tables, not indexes */
4064                 if (OidIsValid(pg_class_reltoastrelid))
4065                 {
4066                         /*
4067                          * One complexity is that the table definition might not require
4068                          * the creation of a TOAST table, and the TOAST table might have
4069                          * been created long after table creation, when the table was
4070                          * loaded with wide data.  By setting the TOAST oid we force
4071                          * creation of the TOAST heap and TOAST index by the backend so we
4072                          * can cleanly copy the files during binary upgrade.
4073                          */
4074
4075                         appendPQExpBuffer(upgrade_buffer,
4076                                                           "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4077                                                           pg_class_reltoastrelid);
4078
4079                         /* every toast table has an index */
4080                         appendPQExpBuffer(upgrade_buffer,
4081                                                           "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4082                                                           pg_index_indexrelid);
4083                 }
4084         }
4085         else
4086                 appendPQExpBuffer(upgrade_buffer,
4087                                                   "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4088                                                   pg_class_oid);
4089
4090         appendPQExpBufferChar(upgrade_buffer, '\n');
4091
4092         PQclear(upgrade_res);
4093         destroyPQExpBuffer(upgrade_query);
4094 }
4095
4096 /*
4097  * If the DumpableObject is a member of an extension, add a suitable
4098  * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
4099  */
4100 static void
4101 binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
4102                                                                 DumpableObject *dobj,
4103                                                                 const char *objlabel)
4104 {
4105         DumpableObject *extobj = NULL;
4106         int                     i;
4107
4108         if (!dobj->ext_member)
4109                 return;
4110
4111         /*
4112          * Find the parent extension.  We could avoid this search if we wanted to
4113          * add a link field to DumpableObject, but the space costs of that would
4114          * be considerable.  We assume that member objects could only have a
4115          * direct dependency on their own extension, not any others.
4116          */
4117         for (i = 0; i < dobj->nDeps; i++)
4118         {
4119                 extobj = findObjectByDumpId(dobj->dependencies[i]);
4120                 if (extobj && extobj->objType == DO_EXTENSION)
4121                         break;
4122                 extobj = NULL;
4123         }
4124         if (extobj == NULL)
4125                 exit_horribly(NULL, "could not find parent extension for %s\n", objlabel);
4126
4127         appendPQExpBufferStr(upgrade_buffer,
4128                                                  "\n-- For binary upgrade, handle extension membership the hard way\n");
4129         appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s;\n",
4130                                           fmtId(extobj->name),
4131                                           objlabel);
4132 }
4133
4134 /*
4135  * getNamespaces:
4136  *        read all namespaces in the system catalogs and return them in the
4137  * NamespaceInfo* structure
4138  *
4139  *      numNamespaces is set to the number of namespaces read in
4140  */
4141 NamespaceInfo *
4142 getNamespaces(Archive *fout, int *numNamespaces)
4143 {
4144         DumpOptions *dopt = fout->dopt;
4145         PGresult   *res;
4146         int                     ntups;
4147         int                     i;
4148         PQExpBuffer query;
4149         NamespaceInfo *nsinfo;
4150         int                     i_tableoid;
4151         int                     i_oid;
4152         int                     i_nspname;
4153         int                     i_rolname;
4154         int                     i_nspacl;
4155         int                     i_rnspacl;
4156         int                     i_initnspacl;
4157         int                     i_initrnspacl;
4158
4159         query = createPQExpBuffer();
4160
4161         /* Make sure we are in proper schema */
4162         selectSourceSchema(fout, "pg_catalog");
4163
4164         /*
4165          * we fetch all namespaces including system ones, so that every object we
4166          * read in can be linked to a containing namespace.
4167          */
4168         if (fout->remoteVersion >= 90600)
4169         {
4170                 PQExpBuffer acl_subquery = createPQExpBuffer();
4171                 PQExpBuffer racl_subquery = createPQExpBuffer();
4172                 PQExpBuffer init_acl_subquery = createPQExpBuffer();
4173                 PQExpBuffer init_racl_subquery = createPQExpBuffer();
4174
4175                 buildACLQueries(acl_subquery, racl_subquery, init_acl_subquery,
4176                                                 init_racl_subquery, "n.nspacl", "n.nspowner", "'n'",
4177                                                 dopt->binary_upgrade);
4178
4179                 appendPQExpBuffer(query, "SELECT n.tableoid, n.oid, n.nspname, "
4180                                                   "(%s nspowner) AS rolname, "
4181                                                   "%s as nspacl, "
4182                                                   "%s as rnspacl, "
4183                                                   "%s as initnspacl, "
4184                                                   "%s as initrnspacl "
4185                                                   "FROM pg_namespace n "
4186                                                   "LEFT JOIN pg_init_privs pip "
4187                                                   "ON (n.oid = pip.objoid "
4188                                                   "AND pip.classoid = 'pg_namespace'::regclass "
4189                                                   "AND pip.objsubid = 0",
4190                                                   username_subquery,
4191                                                   acl_subquery->data,
4192                                                   racl_subquery->data,
4193                                                   init_acl_subquery->data,
4194                                                   init_racl_subquery->data);
4195
4196                 /*
4197                  * When we are doing a 'clean' run, we will be dropping and recreating
4198                  * the 'public' schema (the only object which has that kind of
4199                  * treatment in the backend and which has an entry in pg_init_privs)
4200                  * and therefore we should not consider any initial privileges in
4201                  * pg_init_privs in that case.
4202                  *
4203                  * See pg_backup_archiver.c:_printTocEntry() for the details on why
4204                  * the public schema is special in this regard.
4205                  *
4206                  * Note that if the public schema is dropped and re-created, this is
4207                  * essentially a no-op because the new public schema won't have an
4208                  * entry in pg_init_privs anyway, as the entry will be removed when
4209                  * the public schema is dropped.
4210                  *
4211                  * Further, we have to handle the case where the public schema does
4212                  * not exist at all.
4213                  */
4214                 if (dopt->outputClean)
4215                         appendPQExpBuffer(query, " AND pip.objoid <> "
4216                                                           "coalesce((select oid from pg_namespace "
4217                                                           "where nspname = 'public'),0)");
4218
4219                 appendPQExpBuffer(query, ") ");
4220
4221                 destroyPQExpBuffer(acl_subquery);
4222                 destroyPQExpBuffer(racl_subquery);
4223                 destroyPQExpBuffer(init_acl_subquery);
4224                 destroyPQExpBuffer(init_racl_subquery);
4225         }
4226         else
4227                 appendPQExpBuffer(query, "SELECT tableoid, oid, nspname, "
4228                                                   "(%s nspowner) AS rolname, "
4229                                                   "nspacl, NULL as rnspacl, "
4230                                                   "NULL AS initnspacl, NULL as initrnspacl "
4231                                                   "FROM pg_namespace",
4232                                                   username_subquery);
4233
4234         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4235
4236         ntups = PQntuples(res);
4237
4238         nsinfo = (NamespaceInfo *) pg_malloc(ntups * sizeof(NamespaceInfo));
4239
4240         i_tableoid = PQfnumber(res, "tableoid");
4241         i_oid = PQfnumber(res, "oid");
4242         i_nspname = PQfnumber(res, "nspname");
4243         i_rolname = PQfnumber(res, "rolname");
4244         i_nspacl = PQfnumber(res, "nspacl");
4245         i_rnspacl = PQfnumber(res, "rnspacl");
4246         i_initnspacl = PQfnumber(res, "initnspacl");
4247         i_initrnspacl = PQfnumber(res, "initrnspacl");
4248
4249         for (i = 0; i < ntups; i++)
4250         {
4251                 nsinfo[i].dobj.objType = DO_NAMESPACE;
4252                 nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4253                 nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4254                 AssignDumpId(&nsinfo[i].dobj);
4255                 nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
4256                 nsinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4257                 nsinfo[i].nspacl = pg_strdup(PQgetvalue(res, i, i_nspacl));
4258                 nsinfo[i].rnspacl = pg_strdup(PQgetvalue(res, i, i_rnspacl));
4259                 nsinfo[i].initnspacl = pg_strdup(PQgetvalue(res, i, i_initnspacl));
4260                 nsinfo[i].initrnspacl = pg_strdup(PQgetvalue(res, i, i_initrnspacl));
4261
4262                 /* Decide whether to dump this namespace */
4263                 selectDumpableNamespace(&nsinfo[i], fout);
4264
4265                 /*
4266                  * Do not try to dump ACL if the ACL is empty or the default.
4267                  *
4268                  * This is useful because, for some schemas/objects, the only
4269                  * component we are going to try and dump is the ACL and if we can
4270                  * remove that then 'dump' goes to zero/false and we don't consider
4271                  * this object for dumping at all later on.
4272                  */
4273                 if (PQgetisnull(res, i, i_nspacl) && PQgetisnull(res, i, i_rnspacl) &&
4274                         PQgetisnull(res, i, i_initnspacl) &&
4275                         PQgetisnull(res, i, i_initrnspacl))
4276                         nsinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4277
4278                 if (strlen(nsinfo[i].rolname) == 0)
4279                         write_msg(NULL, "WARNING: owner of schema \"%s\" appears to be invalid\n",
4280                                           nsinfo[i].dobj.name);
4281         }
4282
4283         PQclear(res);
4284         destroyPQExpBuffer(query);
4285
4286         *numNamespaces = ntups;
4287
4288         return nsinfo;
4289 }
4290
4291 /*
4292  * findNamespace:
4293  *              given a namespace OID, look up the info read by getNamespaces
4294  */
4295 static NamespaceInfo *
4296 findNamespace(Archive *fout, Oid nsoid)
4297 {
4298         NamespaceInfo *nsinfo;
4299
4300         nsinfo = findNamespaceByOid(nsoid);
4301         if (nsinfo == NULL)
4302                 exit_horribly(NULL, "schema with OID %u does not exist\n", nsoid);
4303         return nsinfo;
4304 }
4305
4306 /*
4307  * getExtensions:
4308  *        read all extensions in the system catalogs and return them in the
4309  * ExtensionInfo* structure
4310  *
4311  *      numExtensions is set to the number of extensions read in
4312  */
4313 ExtensionInfo *
4314 getExtensions(Archive *fout, int *numExtensions)
4315 {
4316         DumpOptions *dopt = fout->dopt;
4317         PGresult   *res;
4318         int                     ntups;
4319         int                     i;
4320         PQExpBuffer query;
4321         ExtensionInfo *extinfo;
4322         int                     i_tableoid;
4323         int                     i_oid;
4324         int                     i_extname;
4325         int                     i_nspname;
4326         int                     i_extrelocatable;
4327         int                     i_extversion;
4328         int                     i_extconfig;
4329         int                     i_extcondition;
4330
4331         /*
4332          * Before 9.1, there are no extensions.
4333          */
4334         if (fout->remoteVersion < 90100)
4335         {
4336                 *numExtensions = 0;
4337                 return NULL;
4338         }
4339
4340         query = createPQExpBuffer();
4341
4342         /* Make sure we are in proper schema */
4343         selectSourceSchema(fout, "pg_catalog");
4344
4345         appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, "
4346                                                  "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
4347                                                  "FROM pg_extension x "
4348                                                  "JOIN pg_namespace n ON n.oid = x.extnamespace");
4349
4350         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4351
4352         ntups = PQntuples(res);
4353
4354         extinfo = (ExtensionInfo *) pg_malloc(ntups * sizeof(ExtensionInfo));
4355
4356         i_tableoid = PQfnumber(res, "tableoid");
4357         i_oid = PQfnumber(res, "oid");
4358         i_extname = PQfnumber(res, "extname");
4359         i_nspname = PQfnumber(res, "nspname");
4360         i_extrelocatable = PQfnumber(res, "extrelocatable");
4361         i_extversion = PQfnumber(res, "extversion");
4362         i_extconfig = PQfnumber(res, "extconfig");
4363         i_extcondition = PQfnumber(res, "extcondition");
4364
4365         for (i = 0; i < ntups; i++)
4366         {
4367                 extinfo[i].dobj.objType = DO_EXTENSION;
4368                 extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4369                 extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4370                 AssignDumpId(&extinfo[i].dobj);
4371                 extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
4372                 extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
4373                 extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
4374                 extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
4375                 extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
4376                 extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
4377
4378                 /* Decide whether we want to dump it */
4379                 selectDumpableExtension(&(extinfo[i]), dopt);
4380         }
4381
4382         PQclear(res);
4383         destroyPQExpBuffer(query);
4384
4385         *numExtensions = ntups;
4386
4387         return extinfo;
4388 }
4389
4390 /*
4391  * getTypes:
4392  *        read all types in the system catalogs and return them in the
4393  * TypeInfo* structure
4394  *
4395  *      numTypes is set to the number of types read in
4396  *
4397  * NB: this must run after getFuncs() because we assume we can do
4398  * findFuncByOid().
4399  */
4400 TypeInfo *
4401 getTypes(Archive *fout, int *numTypes)
4402 {
4403         DumpOptions *dopt = fout->dopt;
4404         PGresult   *res;
4405         int                     ntups;
4406         int                     i;
4407         PQExpBuffer query = createPQExpBuffer();
4408         TypeInfo   *tyinfo;
4409         ShellTypeInfo *stinfo;
4410         int                     i_tableoid;
4411         int                     i_oid;
4412         int                     i_typname;
4413         int                     i_typnamespace;
4414         int                     i_typacl;
4415         int                     i_rtypacl;
4416         int                     i_inittypacl;
4417         int                     i_initrtypacl;
4418         int                     i_rolname;
4419         int                     i_typelem;
4420         int                     i_typrelid;
4421         int                     i_typrelkind;
4422         int                     i_typtype;
4423         int                     i_typisdefined;
4424         int                     i_isarray;
4425
4426         /*
4427          * we include even the built-in types because those may be used as array
4428          * elements by user-defined types
4429          *
4430          * we filter out the built-in types when we dump out the types
4431          *
4432          * same approach for undefined (shell) types and array types
4433          *
4434          * Note: as of 8.3 we can reliably detect whether a type is an
4435          * auto-generated array type by checking the element type's typarray.
4436          * (Before that the test is capable of generating false positives.) We
4437          * still check for name beginning with '_', though, so as to avoid the
4438          * cost of the subselect probe for all standard types.  This would have to
4439          * be revisited if the backend ever allows renaming of array types.
4440          */
4441
4442         /* Make sure we are in proper schema */
4443         selectSourceSchema(fout, "pg_catalog");
4444
4445         if (fout->remoteVersion >= 90600)
4446         {
4447                 PQExpBuffer acl_subquery = createPQExpBuffer();
4448                 PQExpBuffer racl_subquery = createPQExpBuffer();
4449                 PQExpBuffer initacl_subquery = createPQExpBuffer();
4450                 PQExpBuffer initracl_subquery = createPQExpBuffer();
4451
4452                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
4453                                                 initracl_subquery, "t.typacl", "t.typowner", "'T'",
4454                                                 dopt->binary_upgrade);
4455
4456                 appendPQExpBuffer(query, "SELECT t.tableoid, t.oid, t.typname, "
4457                                                   "t.typnamespace, "
4458                                                   "%s AS typacl, "
4459                                                   "%s AS rtypacl, "
4460                                                   "%s AS inittypacl, "
4461                                                   "%s AS initrtypacl, "
4462                                                   "(%s t.typowner) AS rolname, "
4463                                                   "t.typelem, t.typrelid, "
4464                                                   "CASE WHEN t.typrelid = 0 THEN ' '::\"char\" "
4465                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = t.typrelid) END AS typrelkind, "
4466                                                   "t.typtype, t.typisdefined, "
4467                                                   "t.typname[0] = '_' AND t.typelem != 0 AND "
4468                                                   "(SELECT typarray FROM pg_type te WHERE oid = t.typelem) = t.oid AS isarray "
4469                                                   "FROM pg_type t "
4470                                                   "LEFT JOIN pg_init_privs pip ON "
4471                                                   "(t.oid = pip.objoid "
4472                                                   "AND pip.classoid = 'pg_type'::regclass "
4473                                                   "AND pip.objsubid = 0) ",
4474                                                   acl_subquery->data,
4475                                                   racl_subquery->data,
4476                                                   initacl_subquery->data,
4477                                                   initracl_subquery->data,
4478                                                   username_subquery);
4479
4480                 destroyPQExpBuffer(acl_subquery);
4481                 destroyPQExpBuffer(racl_subquery);
4482                 destroyPQExpBuffer(initacl_subquery);
4483                 destroyPQExpBuffer(initracl_subquery);
4484         }
4485         else if (fout->remoteVersion >= 90200)
4486         {
4487                 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4488                                                   "typnamespace, typacl, NULL as rtypacl, "
4489                                                   "NULL AS inittypacl, NULL AS initrtypacl, "
4490                                                   "(%s typowner) AS rolname, "
4491                                                   "typelem, typrelid, "
4492                                                   "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4493                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4494                                                   "typtype, typisdefined, "
4495                                                   "typname[0] = '_' AND typelem != 0 AND "
4496                                                   "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
4497                                                   "FROM pg_type",
4498                                                   username_subquery);
4499         }
4500         else if (fout->remoteVersion >= 80300)
4501         {
4502                 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4503                                                   "typnamespace, NULL AS typacl, NULL as rtypacl, "
4504                                                   "NULL AS inittypacl, NULL AS initrtypacl, "
4505                                                   "(%s typowner) AS rolname, "
4506                                                   "typelem, typrelid, "
4507                                                   "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4508                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4509                                                   "typtype, typisdefined, "
4510                                                   "typname[0] = '_' AND typelem != 0 AND "
4511                                                   "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
4512                                                   "FROM pg_type",
4513                                                   username_subquery);
4514         }
4515         else
4516         {
4517                 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4518                                                   "typnamespace, NULL AS typacl, NULL as rtypacl, "
4519                                                   "NULL AS inittypacl, NULL AS initrtypacl, "
4520                                                   "(%s typowner) AS rolname, "
4521                                                   "typelem, typrelid, "
4522                                                   "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4523                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4524                                                   "typtype, typisdefined, "
4525                                                   "typname[0] = '_' AND typelem != 0 AS isarray "
4526                                                   "FROM pg_type",
4527                                                   username_subquery);
4528         }
4529
4530         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4531
4532         ntups = PQntuples(res);
4533
4534         tyinfo = (TypeInfo *) pg_malloc(ntups * sizeof(TypeInfo));
4535
4536         i_tableoid = PQfnumber(res, "tableoid");
4537         i_oid = PQfnumber(res, "oid");
4538         i_typname = PQfnumber(res, "typname");
4539         i_typnamespace = PQfnumber(res, "typnamespace");
4540         i_typacl = PQfnumber(res, "typacl");
4541         i_rtypacl = PQfnumber(res, "rtypacl");
4542         i_inittypacl = PQfnumber(res, "inittypacl");
4543         i_initrtypacl = PQfnumber(res, "initrtypacl");
4544         i_rolname = PQfnumber(res, "rolname");
4545         i_typelem = PQfnumber(res, "typelem");
4546         i_typrelid = PQfnumber(res, "typrelid");
4547         i_typrelkind = PQfnumber(res, "typrelkind");
4548         i_typtype = PQfnumber(res, "typtype");
4549         i_typisdefined = PQfnumber(res, "typisdefined");
4550         i_isarray = PQfnumber(res, "isarray");
4551
4552         for (i = 0; i < ntups; i++)
4553         {
4554                 tyinfo[i].dobj.objType = DO_TYPE;
4555                 tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4556                 tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4557                 AssignDumpId(&tyinfo[i].dobj);
4558                 tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
4559                 tyinfo[i].dobj.namespace =
4560                         findNamespace(fout,
4561                                                   atooid(PQgetvalue(res, i, i_typnamespace)));
4562                 tyinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4563                 tyinfo[i].typacl = pg_strdup(PQgetvalue(res, i, i_typacl));
4564                 tyinfo[i].rtypacl = pg_strdup(PQgetvalue(res, i, i_rtypacl));
4565                 tyinfo[i].inittypacl = pg_strdup(PQgetvalue(res, i, i_inittypacl));
4566                 tyinfo[i].initrtypacl = pg_strdup(PQgetvalue(res, i, i_initrtypacl));
4567                 tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
4568                 tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
4569                 tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
4570                 tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
4571                 tyinfo[i].shellType = NULL;
4572
4573                 if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
4574                         tyinfo[i].isDefined = true;
4575                 else
4576                         tyinfo[i].isDefined = false;
4577
4578                 if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
4579                         tyinfo[i].isArray = true;
4580                 else
4581                         tyinfo[i].isArray = false;
4582
4583                 /* Decide whether we want to dump it */
4584                 selectDumpableType(&tyinfo[i], fout);
4585
4586                 /* Do not try to dump ACL if no ACL exists. */
4587                 if (PQgetisnull(res, i, i_typacl) && PQgetisnull(res, i, i_rtypacl) &&
4588                         PQgetisnull(res, i, i_inittypacl) &&
4589                         PQgetisnull(res, i, i_initrtypacl))
4590                         tyinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4591
4592                 /*
4593                  * If it's a domain, fetch info about its constraints, if any
4594                  */
4595                 tyinfo[i].nDomChecks = 0;
4596                 tyinfo[i].domChecks = NULL;
4597                 if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
4598                         tyinfo[i].typtype == TYPTYPE_DOMAIN)
4599                         getDomainConstraints(fout, &(tyinfo[i]));
4600
4601                 /*
4602                  * If it's a base type, make a DumpableObject representing a shell
4603                  * definition of the type.  We will need to dump that ahead of the I/O
4604                  * functions for the type.  Similarly, range types need a shell
4605                  * definition in case they have a canonicalize function.
4606                  *
4607                  * Note: the shell type doesn't have a catId.  You might think it
4608                  * should copy the base type's catId, but then it might capture the
4609                  * pg_depend entries for the type, which we don't want.
4610                  */
4611                 if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
4612                         (tyinfo[i].typtype == TYPTYPE_BASE ||
4613                          tyinfo[i].typtype == TYPTYPE_RANGE))
4614                 {
4615                         stinfo = (ShellTypeInfo *) pg_malloc(sizeof(ShellTypeInfo));
4616                         stinfo->dobj.objType = DO_SHELL_TYPE;
4617                         stinfo->dobj.catId = nilCatalogId;
4618                         AssignDumpId(&stinfo->dobj);
4619                         stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
4620                         stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
4621                         stinfo->baseType = &(tyinfo[i]);
4622                         tyinfo[i].shellType = stinfo;
4623
4624                         /*
4625                          * Initially mark the shell type as not to be dumped.  We'll only
4626                          * dump it if the I/O or canonicalize functions need to be dumped;
4627                          * this is taken care of while sorting dependencies.
4628                          */
4629                         stinfo->dobj.dump = DUMP_COMPONENT_NONE;
4630                 }
4631
4632                 if (strlen(tyinfo[i].rolname) == 0)
4633                         write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",
4634                                           tyinfo[i].dobj.name);
4635         }
4636
4637         *numTypes = ntups;
4638
4639         PQclear(res);
4640
4641         destroyPQExpBuffer(query);
4642
4643         return tyinfo;
4644 }
4645
4646 /*
4647  * getOperators:
4648  *        read all operators in the system catalogs and return them in the
4649  * OprInfo* structure
4650  *
4651  *      numOprs is set to the number of operators read in
4652  */
4653 OprInfo *
4654 getOperators(Archive *fout, int *numOprs)
4655 {
4656         PGresult   *res;
4657         int                     ntups;
4658         int                     i;
4659         PQExpBuffer query = createPQExpBuffer();
4660         OprInfo    *oprinfo;
4661         int                     i_tableoid;
4662         int                     i_oid;
4663         int                     i_oprname;
4664         int                     i_oprnamespace;
4665         int                     i_rolname;
4666         int                     i_oprkind;
4667         int                     i_oprcode;
4668
4669         /*
4670          * find all operators, including builtin operators; we filter out
4671          * system-defined operators at dump-out time.
4672          */
4673
4674         /* Make sure we are in proper schema */
4675         selectSourceSchema(fout, "pg_catalog");
4676
4677         appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
4678                                           "oprnamespace, "
4679                                           "(%s oprowner) AS rolname, "
4680                                           "oprkind, "
4681                                           "oprcode::oid AS oprcode "
4682                                           "FROM pg_operator",
4683                                           username_subquery);
4684
4685         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4686
4687         ntups = PQntuples(res);
4688         *numOprs = ntups;
4689
4690         oprinfo = (OprInfo *) pg_malloc(ntups * sizeof(OprInfo));
4691
4692         i_tableoid = PQfnumber(res, "tableoid");
4693         i_oid = PQfnumber(res, "oid");
4694         i_oprname = PQfnumber(res, "oprname");
4695         i_oprnamespace = PQfnumber(res, "oprnamespace");
4696         i_rolname = PQfnumber(res, "rolname");
4697         i_oprkind = PQfnumber(res, "oprkind");
4698         i_oprcode = PQfnumber(res, "oprcode");
4699
4700         for (i = 0; i < ntups; i++)
4701         {
4702                 oprinfo[i].dobj.objType = DO_OPERATOR;
4703                 oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4704                 oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4705                 AssignDumpId(&oprinfo[i].dobj);
4706                 oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
4707                 oprinfo[i].dobj.namespace =
4708                         findNamespace(fout,
4709                                                   atooid(PQgetvalue(res, i, i_oprnamespace)));
4710                 oprinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4711                 oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
4712                 oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
4713
4714                 /* Decide whether we want to dump it */
4715                 selectDumpableObject(&(oprinfo[i].dobj), fout);
4716
4717                 /* Operators do not currently have ACLs. */
4718                 oprinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4719
4720                 if (strlen(oprinfo[i].rolname) == 0)
4721                         write_msg(NULL, "WARNING: owner of operator \"%s\" appears to be invalid\n",
4722                                           oprinfo[i].dobj.name);
4723         }
4724
4725         PQclear(res);
4726
4727         destroyPQExpBuffer(query);
4728
4729         return oprinfo;
4730 }
4731
4732 /*
4733  * getCollations:
4734  *        read all collations in the system catalogs and return them in the
4735  * CollInfo* structure
4736  *
4737  *      numCollations is set to the number of collations read in
4738  */
4739 CollInfo *
4740 getCollations(Archive *fout, int *numCollations)
4741 {
4742         PGresult   *res;
4743         int                     ntups;
4744         int                     i;
4745         PQExpBuffer query;
4746         CollInfo   *collinfo;
4747         int                     i_tableoid;
4748         int                     i_oid;
4749         int                     i_collname;
4750         int                     i_collnamespace;
4751         int                     i_rolname;
4752
4753         /* Collations didn't exist pre-9.1 */
4754         if (fout->remoteVersion < 90100)
4755         {
4756                 *numCollations = 0;
4757                 return NULL;
4758         }
4759
4760         query = createPQExpBuffer();
4761
4762         /*
4763          * find all collations, including builtin collations; we filter out
4764          * system-defined collations at dump-out time.
4765          */
4766
4767         /* Make sure we are in proper schema */
4768         selectSourceSchema(fout, "pg_catalog");
4769
4770         appendPQExpBuffer(query, "SELECT tableoid, oid, collname, "
4771                                           "collnamespace, "
4772                                           "(%s collowner) AS rolname "
4773                                           "FROM pg_collation",
4774                                           username_subquery);
4775
4776         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4777
4778         ntups = PQntuples(res);
4779         *numCollations = ntups;
4780
4781         collinfo = (CollInfo *) pg_malloc(ntups * sizeof(CollInfo));
4782
4783         i_tableoid = PQfnumber(res, "tableoid");
4784         i_oid = PQfnumber(res, "oid");
4785         i_collname = PQfnumber(res, "collname");
4786         i_collnamespace = PQfnumber(res, "collnamespace");
4787         i_rolname = PQfnumber(res, "rolname");
4788
4789         for (i = 0; i < ntups; i++)
4790         {
4791                 collinfo[i].dobj.objType = DO_COLLATION;
4792                 collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4793                 collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4794                 AssignDumpId(&collinfo[i].dobj);
4795                 collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
4796                 collinfo[i].dobj.namespace =
4797                         findNamespace(fout,
4798                                                   atooid(PQgetvalue(res, i, i_collnamespace)));
4799                 collinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4800
4801                 /* Decide whether we want to dump it */
4802                 selectDumpableObject(&(collinfo[i].dobj), fout);
4803
4804                 /* Collations do not currently have ACLs. */
4805                 collinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4806         }
4807
4808         PQclear(res);
4809
4810         destroyPQExpBuffer(query);
4811
4812         return collinfo;
4813 }
4814
4815 /*
4816  * getConversions:
4817  *        read all conversions in the system catalogs and return them in the
4818  * ConvInfo* structure
4819  *
4820  *      numConversions is set to the number of conversions read in
4821  */
4822 ConvInfo *
4823 getConversions(Archive *fout, int *numConversions)
4824 {
4825         PGresult   *res;
4826         int                     ntups;
4827         int                     i;
4828         PQExpBuffer query;
4829         ConvInfo   *convinfo;
4830         int                     i_tableoid;
4831         int                     i_oid;
4832         int                     i_conname;
4833         int                     i_connamespace;
4834         int                     i_rolname;
4835
4836         query = createPQExpBuffer();
4837
4838         /*
4839          * find all conversions, including builtin conversions; we filter out
4840          * system-defined conversions at dump-out time.
4841          */
4842
4843         /* Make sure we are in proper schema */
4844         selectSourceSchema(fout, "pg_catalog");
4845
4846         appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
4847                                           "connamespace, "
4848                                           "(%s conowner) AS rolname "
4849                                           "FROM pg_conversion",
4850                                           username_subquery);
4851
4852         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4853
4854         ntups = PQntuples(res);
4855         *numConversions = ntups;
4856
4857         convinfo = (ConvInfo *) pg_malloc(ntups * sizeof(ConvInfo));
4858
4859         i_tableoid = PQfnumber(res, "tableoid");
4860         i_oid = PQfnumber(res, "oid");
4861         i_conname = PQfnumber(res, "conname");
4862         i_connamespace = PQfnumber(res, "connamespace");
4863         i_rolname = PQfnumber(res, "rolname");
4864
4865         for (i = 0; i < ntups; i++)
4866         {
4867                 convinfo[i].dobj.objType = DO_CONVERSION;
4868                 convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4869                 convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4870                 AssignDumpId(&convinfo[i].dobj);
4871                 convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
4872                 convinfo[i].dobj.namespace =
4873                         findNamespace(fout,
4874                                                   atooid(PQgetvalue(res, i, i_connamespace)));
4875                 convinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4876
4877                 /* Decide whether we want to dump it */
4878                 selectDumpableObject(&(convinfo[i].dobj), fout);
4879
4880                 /* Conversions do not currently have ACLs. */
4881                 convinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4882         }
4883
4884         PQclear(res);
4885
4886         destroyPQExpBuffer(query);
4887
4888         return convinfo;
4889 }
4890
4891 /*
4892  * getAccessMethods:
4893  *        read all user-defined access methods in the system catalogs and return
4894  *        them in the AccessMethodInfo* structure
4895  *
4896  *      numAccessMethods is set to the number of access methods read in
4897  */
4898 AccessMethodInfo *
4899 getAccessMethods(Archive *fout, int *numAccessMethods)
4900 {
4901         PGresult   *res;
4902         int                     ntups;
4903         int                     i;
4904         PQExpBuffer query;
4905         AccessMethodInfo *aminfo;
4906         int                     i_tableoid;
4907         int                     i_oid;
4908         int                     i_amname;
4909         int                     i_amhandler;
4910         int                     i_amtype;
4911
4912         /* Before 9.6, there are no user-defined access methods */
4913         if (fout->remoteVersion < 90600)
4914         {
4915                 *numAccessMethods = 0;
4916                 return NULL;
4917         }
4918
4919         query = createPQExpBuffer();
4920
4921         /* Make sure we are in proper schema */
4922         selectSourceSchema(fout, "pg_catalog");
4923
4924         /* Select all access methods from pg_am table */
4925         appendPQExpBuffer(query, "SELECT tableoid, oid, amname, amtype, "
4926                                           "amhandler::pg_catalog.regproc AS amhandler "
4927                                           "FROM pg_am");
4928
4929         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4930
4931         ntups = PQntuples(res);
4932         *numAccessMethods = ntups;
4933
4934         aminfo = (AccessMethodInfo *) pg_malloc(ntups * sizeof(AccessMethodInfo));
4935
4936         i_tableoid = PQfnumber(res, "tableoid");
4937         i_oid = PQfnumber(res, "oid");
4938         i_amname = PQfnumber(res, "amname");
4939         i_amhandler = PQfnumber(res, "amhandler");
4940         i_amtype = PQfnumber(res, "amtype");
4941
4942         for (i = 0; i < ntups; i++)
4943         {
4944                 aminfo[i].dobj.objType = DO_ACCESS_METHOD;
4945                 aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4946                 aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4947                 AssignDumpId(&aminfo[i].dobj);
4948                 aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname));
4949                 aminfo[i].dobj.namespace = NULL;
4950                 aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler));
4951                 aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype));
4952
4953                 /* Decide whether we want to dump it */
4954                 selectDumpableAccessMethod(&(aminfo[i]), fout);
4955
4956                 /* Access methods do not currently have ACLs. */
4957                 aminfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4958         }
4959
4960         PQclear(res);
4961
4962         destroyPQExpBuffer(query);
4963
4964         return aminfo;
4965 }
4966
4967
4968 /*
4969  * getOpclasses:
4970  *        read all opclasses in the system catalogs and return them in the
4971  * OpclassInfo* structure
4972  *
4973  *      numOpclasses is set to the number of opclasses read in
4974  */
4975 OpclassInfo *
4976 getOpclasses(Archive *fout, int *numOpclasses)
4977 {
4978         PGresult   *res;
4979         int                     ntups;
4980         int                     i;
4981         PQExpBuffer query = createPQExpBuffer();
4982         OpclassInfo *opcinfo;
4983         int                     i_tableoid;
4984         int                     i_oid;
4985         int                     i_opcname;
4986         int                     i_opcnamespace;
4987         int                     i_rolname;
4988
4989         /*
4990          * find all opclasses, including builtin opclasses; we filter out
4991          * system-defined opclasses at dump-out time.
4992          */
4993
4994         /* Make sure we are in proper schema */
4995         selectSourceSchema(fout, "pg_catalog");
4996
4997         appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
4998                                           "opcnamespace, "
4999                                           "(%s opcowner) AS rolname "
5000                                           "FROM pg_opclass",
5001                                           username_subquery);
5002
5003         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5004
5005         ntups = PQntuples(res);
5006         *numOpclasses = ntups;
5007
5008         opcinfo = (OpclassInfo *) pg_malloc(ntups * sizeof(OpclassInfo));
5009
5010         i_tableoid = PQfnumber(res, "tableoid");
5011         i_oid = PQfnumber(res, "oid");
5012         i_opcname = PQfnumber(res, "opcname");
5013         i_opcnamespace = PQfnumber(res, "opcnamespace");
5014         i_rolname = PQfnumber(res, "rolname");
5015
5016         for (i = 0; i < ntups; i++)
5017         {
5018                 opcinfo[i].dobj.objType = DO_OPCLASS;
5019                 opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5020                 opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5021                 AssignDumpId(&opcinfo[i].dobj);
5022                 opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
5023                 opcinfo[i].dobj.namespace =
5024                         findNamespace(fout,
5025                                                   atooid(PQgetvalue(res, i, i_opcnamespace)));
5026                 opcinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5027
5028                 /* Decide whether we want to dump it */
5029                 selectDumpableObject(&(opcinfo[i].dobj), fout);
5030
5031                 /* Op Classes do not currently have ACLs. */
5032                 opcinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5033
5034                 if (strlen(opcinfo[i].rolname) == 0)
5035                         write_msg(NULL, "WARNING: owner of operator class \"%s\" appears to be invalid\n",
5036                                           opcinfo[i].dobj.name);
5037         }
5038
5039         PQclear(res);
5040
5041         destroyPQExpBuffer(query);
5042
5043         return opcinfo;
5044 }
5045
5046 /*
5047  * getOpfamilies:
5048  *        read all opfamilies in the system catalogs and return them in the
5049  * OpfamilyInfo* structure
5050  *
5051  *      numOpfamilies is set to the number of opfamilies read in
5052  */
5053 OpfamilyInfo *
5054 getOpfamilies(Archive *fout, int *numOpfamilies)
5055 {
5056         PGresult   *res;
5057         int                     ntups;
5058         int                     i;
5059         PQExpBuffer query;
5060         OpfamilyInfo *opfinfo;
5061         int                     i_tableoid;
5062         int                     i_oid;
5063         int                     i_opfname;
5064         int                     i_opfnamespace;
5065         int                     i_rolname;
5066
5067         /* Before 8.3, there is no separate concept of opfamilies */
5068         if (fout->remoteVersion < 80300)
5069         {
5070                 *numOpfamilies = 0;
5071                 return NULL;
5072         }
5073
5074         query = createPQExpBuffer();
5075
5076         /*
5077          * find all opfamilies, including builtin opfamilies; we filter out
5078          * system-defined opfamilies at dump-out time.
5079          */
5080
5081         /* Make sure we are in proper schema */
5082         selectSourceSchema(fout, "pg_catalog");
5083
5084         appendPQExpBuffer(query, "SELECT tableoid, oid, opfname, "
5085                                           "opfnamespace, "
5086                                           "(%s opfowner) AS rolname "
5087                                           "FROM pg_opfamily",
5088                                           username_subquery);
5089
5090         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5091
5092         ntups = PQntuples(res);
5093         *numOpfamilies = ntups;
5094
5095         opfinfo = (OpfamilyInfo *) pg_malloc(ntups * sizeof(OpfamilyInfo));
5096
5097         i_tableoid = PQfnumber(res, "tableoid");
5098         i_oid = PQfnumber(res, "oid");
5099         i_opfname = PQfnumber(res, "opfname");
5100         i_opfnamespace = PQfnumber(res, "opfnamespace");
5101         i_rolname = PQfnumber(res, "rolname");
5102
5103         for (i = 0; i < ntups; i++)
5104         {
5105                 opfinfo[i].dobj.objType = DO_OPFAMILY;
5106                 opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5107                 opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5108                 AssignDumpId(&opfinfo[i].dobj);
5109                 opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
5110                 opfinfo[i].dobj.namespace =
5111                         findNamespace(fout,
5112                                                   atooid(PQgetvalue(res, i, i_opfnamespace)));
5113                 opfinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5114
5115                 /* Decide whether we want to dump it */
5116                 selectDumpableObject(&(opfinfo[i].dobj), fout);
5117
5118                 /* Extensions do not currently have ACLs. */
5119                 opfinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5120
5121                 if (strlen(opfinfo[i].rolname) == 0)
5122                         write_msg(NULL, "WARNING: owner of operator family \"%s\" appears to be invalid\n",
5123                                           opfinfo[i].dobj.name);
5124         }
5125
5126         PQclear(res);
5127
5128         destroyPQExpBuffer(query);
5129
5130         return opfinfo;
5131 }
5132
5133 /*
5134  * getAggregates:
5135  *        read all the user-defined aggregates in the system catalogs and
5136  * return them in the AggInfo* structure
5137  *
5138  * numAggs is set to the number of aggregates read in
5139  */
5140 AggInfo *
5141 getAggregates(Archive *fout, int *numAggs)
5142 {
5143         DumpOptions *dopt = fout->dopt;
5144         PGresult   *res;
5145         int                     ntups;
5146         int                     i;
5147         PQExpBuffer query = createPQExpBuffer();
5148         AggInfo    *agginfo;
5149         int                     i_tableoid;
5150         int                     i_oid;
5151         int                     i_aggname;
5152         int                     i_aggnamespace;
5153         int                     i_pronargs;
5154         int                     i_proargtypes;
5155         int                     i_rolname;
5156         int                     i_aggacl;
5157         int                     i_raggacl;
5158         int                     i_initaggacl;
5159         int                     i_initraggacl;
5160
5161         /* Make sure we are in proper schema */
5162         selectSourceSchema(fout, "pg_catalog");
5163
5164         /*
5165          * Find all interesting aggregates.  See comment in getFuncs() for the
5166          * rationale behind the filtering logic.
5167          */
5168         if (fout->remoteVersion >= 90600)
5169         {
5170                 PQExpBuffer acl_subquery = createPQExpBuffer();
5171                 PQExpBuffer racl_subquery = createPQExpBuffer();
5172                 PQExpBuffer initacl_subquery = createPQExpBuffer();
5173                 PQExpBuffer initracl_subquery = createPQExpBuffer();
5174
5175                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
5176                                                 initracl_subquery, "p.proacl", "p.proowner", "'f'",
5177                                                 dopt->binary_upgrade);
5178
5179                 appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, "
5180                                                   "p.proname AS aggname, "
5181                                                   "p.pronamespace AS aggnamespace, "
5182                                                   "p.pronargs, p.proargtypes, "
5183                                                   "(%s p.proowner) AS rolname, "
5184                                                   "%s AS aggacl, "
5185                                                   "%s AS raggacl, "
5186                                                   "%s AS initaggacl, "
5187                                                   "%s AS initraggacl "
5188                                                   "FROM pg_proc p "
5189                                                   "LEFT JOIN pg_init_privs pip ON "
5190                                                   "(p.oid = pip.objoid "
5191                                                   "AND pip.classoid = 'pg_proc'::regclass "
5192                                                   "AND pip.objsubid = 0) "
5193                                                   "WHERE p.proisagg AND ("
5194                                                   "p.pronamespace != "
5195                                                   "(SELECT oid FROM pg_namespace "
5196                                                   "WHERE nspname = 'pg_catalog') OR "
5197                                                   "p.proacl IS DISTINCT FROM pip.initprivs",
5198                                                   username_subquery,
5199                                                   acl_subquery->data,
5200                                                   racl_subquery->data,
5201                                                   initacl_subquery->data,
5202                                                   initracl_subquery->data);
5203                 if (dopt->binary_upgrade)
5204                         appendPQExpBufferStr(query,
5205                                                                  " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5206                                                                  "classid = 'pg_proc'::regclass AND "
5207                                                                  "objid = p.oid AND "
5208                                                                  "refclassid = 'pg_extension'::regclass AND "
5209                                                                  "deptype = 'e')");
5210                 appendPQExpBufferChar(query, ')');
5211
5212                 destroyPQExpBuffer(acl_subquery);
5213                 destroyPQExpBuffer(racl_subquery);
5214                 destroyPQExpBuffer(initacl_subquery);
5215                 destroyPQExpBuffer(initracl_subquery);
5216         }
5217         else if (fout->remoteVersion >= 80200)
5218         {
5219                 appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
5220                                                   "pronamespace AS aggnamespace, "
5221                                                   "pronargs, proargtypes, "
5222                                                   "(%s proowner) AS rolname, "
5223                                                   "proacl AS aggacl, "
5224                                                   "NULL AS raggacl, "
5225                                                   "NULL AS initaggacl, NULL AS initraggacl "
5226                                                   "FROM pg_proc p "
5227                                                   "WHERE proisagg AND ("
5228                                                   "pronamespace != "
5229                                                   "(SELECT oid FROM pg_namespace "
5230                                                   "WHERE nspname = 'pg_catalog')",
5231                                                   username_subquery);
5232                 if (dopt->binary_upgrade && fout->remoteVersion >= 90100)
5233                         appendPQExpBufferStr(query,
5234                                                                  " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5235                                                                  "classid = 'pg_proc'::regclass AND "
5236                                                                  "objid = p.oid AND "
5237                                                                  "refclassid = 'pg_extension'::regclass AND "
5238                                                                  "deptype = 'e')");
5239                 appendPQExpBufferChar(query, ')');
5240         }
5241         else
5242         {
5243                 appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
5244                                                   "pronamespace AS aggnamespace, "
5245                                                   "CASE WHEN proargtypes[0] = 'pg_catalog.\"any\"'::pg_catalog.regtype THEN 0 ELSE 1 END AS pronargs, "
5246                                                   "proargtypes, "
5247                                                   "(%s proowner) AS rolname, "
5248                                                   "proacl AS aggacl, "
5249                                                   "NULL AS raggacl, "
5250                                                   "NULL AS initaggacl, NULL AS initraggacl "
5251                                                   "FROM pg_proc "
5252                                                   "WHERE proisagg "
5253                                                   "AND pronamespace != "
5254                                                   "(SELECT oid FROM pg_namespace WHERE nspname = 'pg_catalog')",
5255                                                   username_subquery);
5256         }
5257
5258         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5259
5260         ntups = PQntuples(res);
5261         *numAggs = ntups;
5262
5263         agginfo = (AggInfo *) pg_malloc(ntups * sizeof(AggInfo));
5264
5265         i_tableoid = PQfnumber(res, "tableoid");
5266         i_oid = PQfnumber(res, "oid");
5267         i_aggname = PQfnumber(res, "aggname");
5268         i_aggnamespace = PQfnumber(res, "aggnamespace");
5269         i_pronargs = PQfnumber(res, "pronargs");
5270         i_proargtypes = PQfnumber(res, "proargtypes");
5271         i_rolname = PQfnumber(res, "rolname");
5272         i_aggacl = PQfnumber(res, "aggacl");
5273         i_raggacl = PQfnumber(res, "raggacl");
5274         i_initaggacl = PQfnumber(res, "initaggacl");
5275         i_initraggacl = PQfnumber(res, "initraggacl");
5276
5277         for (i = 0; i < ntups; i++)
5278         {
5279                 agginfo[i].aggfn.dobj.objType = DO_AGG;
5280                 agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5281                 agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5282                 AssignDumpId(&agginfo[i].aggfn.dobj);
5283                 agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
5284                 agginfo[i].aggfn.dobj.namespace =
5285                         findNamespace(fout,
5286                                                   atooid(PQgetvalue(res, i, i_aggnamespace)));
5287                 agginfo[i].aggfn.rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5288                 if (strlen(agginfo[i].aggfn.rolname) == 0)
5289                         write_msg(NULL, "WARNING: owner of aggregate function \"%s\" appears to be invalid\n",
5290                                           agginfo[i].aggfn.dobj.name);
5291                 agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
5292                 agginfo[i].aggfn.prorettype = InvalidOid;       /* not saved */
5293                 agginfo[i].aggfn.proacl = pg_strdup(PQgetvalue(res, i, i_aggacl));
5294                 agginfo[i].aggfn.rproacl = pg_strdup(PQgetvalue(res, i, i_raggacl));
5295                 agginfo[i].aggfn.initproacl = pg_strdup(PQgetvalue(res, i, i_initaggacl));
5296                 agginfo[i].aggfn.initrproacl = pg_strdup(PQgetvalue(res, i, i_initraggacl));
5297                 agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs));
5298                 if (agginfo[i].aggfn.nargs == 0)
5299                         agginfo[i].aggfn.argtypes = NULL;
5300                 else
5301                 {
5302                         agginfo[i].aggfn.argtypes = (Oid *) pg_malloc(agginfo[i].aggfn.nargs * sizeof(Oid));
5303                         parseOidArray(PQgetvalue(res, i, i_proargtypes),
5304                                                   agginfo[i].aggfn.argtypes,
5305                                                   agginfo[i].aggfn.nargs);
5306                 }
5307
5308                 /* Decide whether we want to dump it */
5309                 selectDumpableObject(&(agginfo[i].aggfn.dobj), fout);
5310
5311                 /* Do not try to dump ACL if no ACL exists. */
5312                 if (PQgetisnull(res, i, i_aggacl) && PQgetisnull(res, i, i_raggacl) &&
5313                         PQgetisnull(res, i, i_initaggacl) &&
5314                         PQgetisnull(res, i, i_initraggacl))
5315                         agginfo[i].aggfn.dobj.dump &= ~DUMP_COMPONENT_ACL;
5316         }
5317
5318         PQclear(res);
5319
5320         destroyPQExpBuffer(query);
5321
5322         return agginfo;
5323 }
5324
5325 /*
5326  * getFuncs:
5327  *        read all the user-defined functions in the system catalogs and
5328  * return them in the FuncInfo* structure
5329  *
5330  * numFuncs is set to the number of functions read in
5331  */
5332 FuncInfo *
5333 getFuncs(Archive *fout, int *numFuncs)
5334 {
5335         DumpOptions *dopt = fout->dopt;
5336         PGresult   *res;
5337         int                     ntups;
5338         int                     i;
5339         PQExpBuffer query = createPQExpBuffer();
5340         FuncInfo   *finfo;
5341         int                     i_tableoid;
5342         int                     i_oid;
5343         int                     i_proname;
5344         int                     i_pronamespace;
5345         int                     i_rolname;
5346         int                     i_prolang;
5347         int                     i_pronargs;
5348         int                     i_proargtypes;
5349         int                     i_prorettype;
5350         int                     i_proacl;
5351         int                     i_rproacl;
5352         int                     i_initproacl;
5353         int                     i_initrproacl;
5354
5355         /* Make sure we are in proper schema */
5356         selectSourceSchema(fout, "pg_catalog");
5357
5358         /*
5359          * Find all interesting functions.  This is a bit complicated:
5360          *
5361          * 1. Always exclude aggregates; those are handled elsewhere.
5362          *
5363          * 2. Always exclude functions that are internally dependent on something
5364          * else, since presumably those will be created as a result of creating
5365          * the something else.  This currently acts only to suppress constructor
5366          * functions for range types (so we only need it in 9.2 and up).  Note
5367          * this is OK only because the constructors don't have any dependencies
5368          * the range type doesn't have; otherwise we might not get creation
5369          * ordering correct.
5370          *
5371          * 3. Otherwise, we normally exclude functions in pg_catalog.  However, if
5372          * they're members of extensions and we are in binary-upgrade mode then
5373          * include them, since we want to dump extension members individually in
5374          * that mode.  Also, if they are used by casts or transforms then we need
5375          * to gather the information about them, though they won't be dumped if
5376          * they are built-in.  Also, in 9.6 and up, include functions in
5377          * pg_catalog if they have an ACL different from what's shown in
5378          * pg_init_privs.
5379          */
5380         if (fout->remoteVersion >= 90600)
5381         {
5382                 PQExpBuffer acl_subquery = createPQExpBuffer();
5383                 PQExpBuffer racl_subquery = createPQExpBuffer();
5384                 PQExpBuffer initacl_subquery = createPQExpBuffer();
5385                 PQExpBuffer initracl_subquery = createPQExpBuffer();
5386
5387                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
5388                                                 initracl_subquery, "p.proacl", "p.proowner", "'f'",
5389                                                 dopt->binary_upgrade);
5390
5391                 appendPQExpBuffer(query,
5392                                                   "SELECT p.tableoid, p.oid, p.proname, p.prolang, "
5393                                                   "p.pronargs, p.proargtypes, p.prorettype, "
5394                                                   "%s AS proacl, "
5395                                                   "%s AS rproacl, "
5396                                                   "%s AS initproacl, "
5397                                                   "%s AS initrproacl, "
5398                                                   "p.pronamespace, "
5399                                                   "(%s p.proowner) AS rolname "
5400                                                   "FROM pg_proc p "
5401                                                   "LEFT JOIN pg_init_privs pip ON "
5402                                                   "(p.oid = pip.objoid "
5403                                                   "AND pip.classoid = 'pg_proc'::regclass "
5404                                                   "AND pip.objsubid = 0) "
5405                                                   "WHERE NOT proisagg"
5406                                                   "\n  AND NOT EXISTS (SELECT 1 FROM pg_depend "
5407                                                   "WHERE classid = 'pg_proc'::regclass AND "
5408                                                   "objid = p.oid AND deptype = 'i')"
5409                                                   "\n  AND ("
5410                                                   "\n  pronamespace != "
5411                                                   "(SELECT oid FROM pg_namespace "
5412                                                   "WHERE nspname = 'pg_catalog')"
5413                                                   "\n  OR EXISTS (SELECT 1 FROM pg_cast"
5414                                                   "\n  WHERE pg_cast.oid > %u "
5415                                                   "\n  AND p.oid = pg_cast.castfunc)"
5416                                                   "\n  OR EXISTS (SELECT 1 FROM pg_transform"
5417                                                   "\n  WHERE pg_transform.oid > %u AND "
5418                                                   "\n  (p.oid = pg_transform.trffromsql"
5419                                                   "\n  OR p.oid = pg_transform.trftosql))",
5420                                                   acl_subquery->data,
5421                                                   racl_subquery->data,
5422                                                   initacl_subquery->data,
5423                                                   initracl_subquery->data,
5424                                                   username_subquery,
5425                                                   g_last_builtin_oid,
5426                                                   g_last_builtin_oid);
5427                 if (dopt->binary_upgrade)
5428                         appendPQExpBufferStr(query,
5429                                                                  "\n  OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5430                                                                  "classid = 'pg_proc'::regclass AND "
5431                                                                  "objid = p.oid AND "
5432                                                                  "refclassid = 'pg_extension'::regclass AND "
5433                                                                  "deptype = 'e')");
5434                 appendPQExpBufferStr(query,
5435                                                          "\n  OR p.proacl IS DISTINCT FROM pip.initprivs");
5436                 appendPQExpBufferChar(query, ')');
5437
5438                 destroyPQExpBuffer(acl_subquery);
5439                 destroyPQExpBuffer(racl_subquery);
5440                 destroyPQExpBuffer(initacl_subquery);
5441                 destroyPQExpBuffer(initracl_subquery);
5442         }
5443         else
5444         {
5445                 appendPQExpBuffer(query,
5446                                                   "SELECT tableoid, oid, proname, prolang, "
5447                                                   "pronargs, proargtypes, prorettype, proacl, "
5448                                                   "NULL as rproacl, "
5449                                                   "NULL as initproacl, NULL AS initrproacl, "
5450                                                   "pronamespace, "
5451                                                   "(%s proowner) AS rolname "
5452                                                   "FROM pg_proc p "
5453                                                   "WHERE NOT proisagg",
5454                                                   username_subquery);
5455                 if (fout->remoteVersion >= 90200)
5456                         appendPQExpBufferStr(query,
5457                                                                  "\n  AND NOT EXISTS (SELECT 1 FROM pg_depend "
5458                                                                  "WHERE classid = 'pg_proc'::regclass AND "
5459                                                                  "objid = p.oid AND deptype = 'i')");
5460                 appendPQExpBuffer(query,
5461                                                   "\n  AND ("
5462                                                   "\n  pronamespace != "
5463                                                   "(SELECT oid FROM pg_namespace "
5464                                                   "WHERE nspname = 'pg_catalog')"
5465                                                   "\n  OR EXISTS (SELECT 1 FROM pg_cast"
5466                                                   "\n  WHERE pg_cast.oid > '%u'::oid"
5467                                                   "\n  AND p.oid = pg_cast.castfunc)",
5468                                                   g_last_builtin_oid);
5469
5470                 if (fout->remoteVersion >= 90500)
5471                         appendPQExpBuffer(query,
5472                                                           "\n  OR EXISTS (SELECT 1 FROM pg_transform"
5473                                                           "\n  WHERE pg_transform.oid > '%u'::oid"
5474                                                           "\n  AND (p.oid = pg_transform.trffromsql"
5475                                                           "\n  OR p.oid = pg_transform.trftosql))",
5476                                                           g_last_builtin_oid);
5477
5478                 if (dopt->binary_upgrade && fout->remoteVersion >= 90100)
5479                         appendPQExpBufferStr(query,
5480                                                                  "\n  OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5481                                                                  "classid = 'pg_proc'::regclass AND "
5482                                                                  "objid = p.oid AND "
5483                                                                  "refclassid = 'pg_extension'::regclass AND "
5484                                                                  "deptype = 'e')");
5485                 appendPQExpBufferChar(query, ')');
5486         }
5487
5488         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5489
5490         ntups = PQntuples(res);
5491
5492         *numFuncs = ntups;
5493
5494         finfo = (FuncInfo *) pg_malloc0(ntups * sizeof(FuncInfo));
5495
5496         i_tableoid = PQfnumber(res, "tableoid");
5497         i_oid = PQfnumber(res, "oid");
5498         i_proname = PQfnumber(res, "proname");
5499         i_pronamespace = PQfnumber(res, "pronamespace");
5500         i_rolname = PQfnumber(res, "rolname");
5501         i_prolang = PQfnumber(res, "prolang");
5502         i_pronargs = PQfnumber(res, "pronargs");
5503         i_proargtypes = PQfnumber(res, "proargtypes");
5504         i_prorettype = PQfnumber(res, "prorettype");
5505         i_proacl = PQfnumber(res, "proacl");
5506         i_rproacl = PQfnumber(res, "rproacl");
5507         i_initproacl = PQfnumber(res, "initproacl");
5508         i_initrproacl = PQfnumber(res, "initrproacl");
5509
5510         for (i = 0; i < ntups; i++)
5511         {
5512                 finfo[i].dobj.objType = DO_FUNC;
5513                 finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5514                 finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5515                 AssignDumpId(&finfo[i].dobj);
5516                 finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
5517                 finfo[i].dobj.namespace =
5518                         findNamespace(fout,
5519                                                   atooid(PQgetvalue(res, i, i_pronamespace)));
5520                 finfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5521                 finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
5522                 finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
5523                 finfo[i].proacl = pg_strdup(PQgetvalue(res, i, i_proacl));
5524                 finfo[i].rproacl = pg_strdup(PQgetvalue(res, i, i_rproacl));
5525                 finfo[i].initproacl = pg_strdup(PQgetvalue(res, i, i_initproacl));
5526                 finfo[i].initrproacl = pg_strdup(PQgetvalue(res, i, i_initrproacl));
5527                 finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
5528                 if (finfo[i].nargs == 0)
5529                         finfo[i].argtypes = NULL;
5530                 else
5531                 {
5532                         finfo[i].argtypes = (Oid *) pg_malloc(finfo[i].nargs * sizeof(Oid));
5533                         parseOidArray(PQgetvalue(res, i, i_proargtypes),
5534                                                   finfo[i].argtypes, finfo[i].nargs);
5535                 }
5536
5537                 /* Decide whether we want to dump it */
5538                 selectDumpableObject(&(finfo[i].dobj), fout);
5539
5540                 /* Do not try to dump ACL if no ACL exists. */
5541                 if (PQgetisnull(res, i, i_proacl) && PQgetisnull(res, i, i_rproacl) &&
5542                         PQgetisnull(res, i, i_initproacl) &&
5543                         PQgetisnull(res, i, i_initrproacl))
5544                         finfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5545
5546                 if (strlen(finfo[i].rolname) == 0)
5547                         write_msg(NULL,
5548                                           "WARNING: owner of function \"%s\" appears to be invalid\n",
5549                                           finfo[i].dobj.name);
5550         }
5551
5552         PQclear(res);
5553
5554         destroyPQExpBuffer(query);
5555
5556         return finfo;
5557 }
5558
5559 /*
5560  * getTables
5561  *        read all the tables (no indexes)
5562  * in the system catalogs return them in the TableInfo* structure
5563  *
5564  * numTables is set to the number of tables read in
5565  */
5566 TableInfo *
5567 getTables(Archive *fout, int *numTables)
5568 {
5569         DumpOptions *dopt = fout->dopt;
5570         PGresult   *res;
5571         int                     ntups;
5572         int                     i;
5573         PQExpBuffer query = createPQExpBuffer();
5574         TableInfo  *tblinfo;
5575         int                     i_reltableoid;
5576         int                     i_reloid;
5577         int                     i_relname;
5578         int                     i_relnamespace;
5579         int                     i_relkind;
5580         int                     i_relacl;
5581         int                     i_rrelacl;
5582         int                     i_initrelacl;
5583         int                     i_initrrelacl;
5584         int                     i_rolname;
5585         int                     i_relchecks;
5586         int                     i_relhastriggers;
5587         int                     i_relhasindex;
5588         int                     i_relhasrules;
5589         int                     i_relrowsec;
5590         int                     i_relforcerowsec;
5591         int                     i_relhasoids;
5592         int                     i_relfrozenxid;
5593         int                     i_relminmxid;
5594         int                     i_toastoid;
5595         int                     i_toastfrozenxid;
5596         int                     i_toastminmxid;
5597         int                     i_relpersistence;
5598         int                     i_relispopulated;
5599         int                     i_relreplident;
5600         int                     i_owning_tab;
5601         int                     i_owning_col;
5602         int                     i_reltablespace;
5603         int                     i_reloptions;
5604         int                     i_checkoption;
5605         int                     i_toastreloptions;
5606         int                     i_reloftype;
5607         int                     i_relpages;
5608         int                     i_is_identity_sequence;
5609         int                     i_changed_acl;
5610         int                     i_partkeydef;
5611         int                     i_ispartition;
5612         int                     i_partbound;
5613
5614         /* Make sure we are in proper schema */
5615         selectSourceSchema(fout, "pg_catalog");
5616
5617         /*
5618          * Find all the tables and table-like objects.
5619          *
5620          * We include system catalogs, so that we can work if a user table is
5621          * defined to inherit from a system catalog (pretty weird, but...)
5622          *
5623          * We ignore relations that are not ordinary tables, sequences, views,
5624          * materialized views, composite types, or foreign tables.
5625          *
5626          * Composite-type table entries won't be dumped as such, but we have to
5627          * make a DumpableObject for them so that we can track dependencies of the
5628          * composite type (pg_depend entries for columns of the composite type
5629          * link to the pg_class entry not the pg_type entry).
5630          *
5631          * Note: in this phase we should collect only a minimal amount of
5632          * information about each table, basically just enough to decide if it is
5633          * interesting. We must fetch all tables in this phase because otherwise
5634          * we cannot correctly identify inherited columns, owned sequences, etc.
5635          */
5636
5637         if (fout->remoteVersion >= 90600)
5638         {
5639                 char       *partkeydef = "NULL";
5640                 char       *ispartition = "false";
5641                 char       *partbound = "NULL";
5642
5643                 PQExpBuffer acl_subquery = createPQExpBuffer();
5644                 PQExpBuffer racl_subquery = createPQExpBuffer();
5645                 PQExpBuffer initacl_subquery = createPQExpBuffer();
5646                 PQExpBuffer initracl_subquery = createPQExpBuffer();
5647
5648                 PQExpBuffer attacl_subquery = createPQExpBuffer();
5649                 PQExpBuffer attracl_subquery = createPQExpBuffer();
5650                 PQExpBuffer attinitacl_subquery = createPQExpBuffer();
5651                 PQExpBuffer attinitracl_subquery = createPQExpBuffer();
5652
5653                 /*
5654                  * Collect the information about any partitioned tables, which were
5655                  * added in PG10.
5656                  */
5657
5658                 if (fout->remoteVersion >= 100000)
5659                 {
5660                         partkeydef = "pg_get_partkeydef(c.oid)";
5661                         ispartition = "c.relispartition";
5662                         partbound = "pg_get_expr(c.relpartbound, c.oid)";
5663                 }
5664
5665                 /*
5666                  * Left join to pick up dependency info linking sequences to their
5667                  * owning column, if any (note this dependency is AUTO as of 8.2)
5668                  *
5669                  * Left join to detect if any privileges are still as-set-at-init, in
5670                  * which case we won't dump out ACL commands for those.
5671                  */
5672
5673                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
5674                                                 initracl_subquery, "c.relacl", "c.relowner",
5675                                                 "CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
5676                                                 " THEN 's' ELSE 'r' END::\"char\"",
5677                                                 dopt->binary_upgrade);
5678
5679                 buildACLQueries(attacl_subquery, attracl_subquery, attinitacl_subquery,
5680                                                 attinitracl_subquery, "at.attacl", "c.relowner", "'c'",
5681                                                 dopt->binary_upgrade);
5682
5683                 appendPQExpBuffer(query,
5684                                                   "SELECT c.tableoid, c.oid, c.relname, "
5685                                                   "%s AS relacl, %s as rrelacl, "
5686                                                   "%s AS initrelacl, %s as initrrelacl, "
5687                                                   "c.relkind, c.relnamespace, "
5688                                                   "(%s c.relowner) AS rolname, "
5689                                                   "c.relchecks, c.relhastriggers, "
5690                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
5691                                                   "c.relrowsecurity, c.relforcerowsecurity, "
5692                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
5693                                                   "tc.relfrozenxid AS tfrozenxid, "
5694                                                   "tc.relminmxid AS tminmxid, "
5695                                                   "c.relpersistence, c.relispopulated, "
5696                                                   "c.relreplident, c.relpages, "
5697                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
5698                                                   "d.refobjid AS owning_tab, "
5699                                                   "d.refobjsubid AS owning_col, "
5700                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
5701                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
5702                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
5703                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
5704                                                   "tc.reloptions AS toast_reloptions, "
5705                                                   "c.relkind = '%c' AND EXISTS (SELECT 1 FROM pg_depend WHERE classid = 'pg_class'::regclass AND objid = c.oid AND objsubid = 0 AND refclassid = 'pg_class'::regclass AND deptype = 'i') AS is_identity_sequence, "
5706                                                   "EXISTS (SELECT 1 FROM pg_attribute at LEFT JOIN pg_init_privs pip ON "
5707                                                   "(c.oid = pip.objoid "
5708                                                   "AND pip.classoid = 'pg_class'::regclass "
5709                                                   "AND pip.objsubid = at.attnum)"
5710                                                   "WHERE at.attrelid = c.oid AND ("
5711                                                   "%s IS NOT NULL "
5712                                                   "OR %s IS NOT NULL "
5713                                                   "OR %s IS NOT NULL "
5714                                                   "OR %s IS NOT NULL"
5715                                                   "))"
5716                                                   "AS changed_acl, "
5717                                                   "%s AS partkeydef, "
5718                                                   "%s AS ispartition, "
5719                                                   "%s AS partbound "
5720                                                   "FROM pg_class c "
5721                                                   "LEFT JOIN pg_depend d ON "
5722                                                   "(c.relkind = '%c' AND "
5723                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
5724                                                   "d.objsubid = 0 AND "
5725                                                   "d.refclassid = c.tableoid AND d.deptype IN ('a', 'i')) "
5726                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
5727                                                   "LEFT JOIN pg_init_privs pip ON "
5728                                                   "(c.oid = pip.objoid "
5729                                                   "AND pip.classoid = 'pg_class'::regclass "
5730                                                   "AND pip.objsubid = 0) "
5731                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c', '%c') "
5732                                                   "ORDER BY c.oid",
5733                                                   acl_subquery->data,
5734                                                   racl_subquery->data,
5735                                                   initacl_subquery->data,
5736                                                   initracl_subquery->data,
5737                                                   username_subquery,
5738                                                   RELKIND_SEQUENCE,
5739                                                   attacl_subquery->data,
5740                                                   attracl_subquery->data,
5741                                                   attinitacl_subquery->data,
5742                                                   attinitracl_subquery->data,
5743                                                   partkeydef,
5744                                                   ispartition,
5745                                                   partbound,
5746                                                   RELKIND_SEQUENCE,
5747                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
5748                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
5749                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
5750                                                   RELKIND_PARTITIONED_TABLE);
5751
5752                 destroyPQExpBuffer(acl_subquery);
5753                 destroyPQExpBuffer(racl_subquery);
5754                 destroyPQExpBuffer(initacl_subquery);
5755                 destroyPQExpBuffer(initracl_subquery);
5756
5757                 destroyPQExpBuffer(attacl_subquery);
5758                 destroyPQExpBuffer(attracl_subquery);
5759                 destroyPQExpBuffer(attinitacl_subquery);
5760                 destroyPQExpBuffer(attinitracl_subquery);
5761         }
5762         else if (fout->remoteVersion >= 90500)
5763         {
5764                 /*
5765                  * Left join to pick up dependency info linking sequences to their
5766                  * owning column, if any (note this dependency is AUTO as of 8.2)
5767                  */
5768                 appendPQExpBuffer(query,
5769                                                   "SELECT c.tableoid, c.oid, c.relname, "
5770                                                   "c.relacl, NULL as rrelacl, "
5771                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
5772                                                   "c.relkind, "
5773                                                   "c.relnamespace, "
5774                                                   "(%s c.relowner) AS rolname, "
5775                                                   "c.relchecks, c.relhastriggers, "
5776                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
5777                                                   "c.relrowsecurity, c.relforcerowsecurity, "
5778                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
5779                                                   "tc.relfrozenxid AS tfrozenxid, "
5780                                                   "tc.relminmxid AS tminmxid, "
5781                                                   "c.relpersistence, c.relispopulated, "
5782                                                   "c.relreplident, c.relpages, "
5783                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
5784                                                   "d.refobjid AS owning_tab, "
5785                                                   "d.refobjsubid AS owning_col, "
5786                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
5787                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
5788                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
5789                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
5790                                                   "tc.reloptions AS toast_reloptions, "
5791                                                   "NULL AS changed_acl, "
5792                                                   "NULL AS partkeydef, "
5793                                                   "false AS ispartition, "
5794                                                   "NULL AS partbound "
5795                                                   "FROM pg_class c "
5796                                                   "LEFT JOIN pg_depend d ON "
5797                                                   "(c.relkind = '%c' AND "
5798                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
5799                                                   "d.objsubid = 0 AND "
5800                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
5801                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
5802                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
5803                                                   "ORDER BY c.oid",
5804                                                   username_subquery,
5805                                                   RELKIND_SEQUENCE,
5806                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
5807                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
5808                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
5809         }
5810         else if (fout->remoteVersion >= 90400)
5811         {
5812                 /*
5813                  * Left join to pick up dependency info linking sequences to their
5814                  * owning column, if any (note this dependency is AUTO as of 8.2)
5815                  */
5816                 appendPQExpBuffer(query,
5817                                                   "SELECT c.tableoid, c.oid, c.relname, "
5818                                                   "c.relacl, NULL as rrelacl, "
5819                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
5820                                                   "c.relkind, "
5821                                                   "c.relnamespace, "
5822                                                   "(%s c.relowner) AS rolname, "
5823                                                   "c.relchecks, c.relhastriggers, "
5824                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
5825                                                   "'f'::bool AS relrowsecurity, "
5826                                                   "'f'::bool AS relforcerowsecurity, "
5827                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
5828                                                   "tc.relfrozenxid AS tfrozenxid, "
5829                                                   "tc.relminmxid AS tminmxid, "
5830                                                   "c.relpersistence, c.relispopulated, "
5831                                                   "c.relreplident, c.relpages, "
5832                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
5833                                                   "d.refobjid AS owning_tab, "
5834                                                   "d.refobjsubid AS owning_col, "
5835                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
5836                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
5837                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
5838                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
5839                                                   "tc.reloptions AS toast_reloptions, "
5840                                                   "NULL AS changed_acl, "
5841                                                   "NULL AS partkeydef, "
5842                                                   "false AS ispartition, "
5843                                                   "NULL AS partbound "
5844                                                   "FROM pg_class c "
5845                                                   "LEFT JOIN pg_depend d ON "
5846                                                   "(c.relkind = '%c' AND "
5847                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
5848                                                   "d.objsubid = 0 AND "
5849                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
5850                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
5851                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
5852                                                   "ORDER BY c.oid",
5853                                                   username_subquery,
5854                                                   RELKIND_SEQUENCE,
5855                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
5856                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
5857                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
5858         }
5859         else if (fout->remoteVersion >= 90300)
5860         {
5861                 /*
5862                  * Left join to pick up dependency info linking sequences to their
5863                  * owning column, if any (note this dependency is AUTO as of 8.2)
5864                  */
5865                 appendPQExpBuffer(query,
5866                                                   "SELECT c.tableoid, c.oid, c.relname, "
5867                                                   "c.relacl, NULL as rrelacl, "
5868                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
5869                                                   "c.relkind, "
5870                                                   "c.relnamespace, "
5871                                                   "(%s c.relowner) AS rolname, "
5872                                                   "c.relchecks, c.relhastriggers, "
5873                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
5874                                                   "'f'::bool AS relrowsecurity, "
5875                                                   "'f'::bool AS relforcerowsecurity, "
5876                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
5877                                                   "tc.relfrozenxid AS tfrozenxid, "
5878                                                   "tc.relminmxid AS tminmxid, "
5879                                                   "c.relpersistence, c.relispopulated, "
5880                                                   "'d' AS relreplident, c.relpages, "
5881                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
5882                                                   "d.refobjid AS owning_tab, "
5883                                                   "d.refobjsubid AS owning_col, "
5884                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
5885                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
5886                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
5887                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
5888                                                   "tc.reloptions AS toast_reloptions, "
5889                                                   "NULL AS changed_acl, "
5890                                                   "NULL AS partkeydef, "
5891                                                   "false AS ispartition, "
5892                                                   "NULL AS partbound "
5893                                                   "FROM pg_class c "
5894                                                   "LEFT JOIN pg_depend d ON "
5895                                                   "(c.relkind = '%c' AND "
5896                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
5897                                                   "d.objsubid = 0 AND "
5898                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
5899                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
5900                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
5901                                                   "ORDER BY c.oid",
5902                                                   username_subquery,
5903                                                   RELKIND_SEQUENCE,
5904                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
5905                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
5906                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
5907         }
5908         else if (fout->remoteVersion >= 90100)
5909         {
5910                 /*
5911                  * Left join to pick up dependency info linking sequences to their
5912                  * owning column, if any (note this dependency is AUTO as of 8.2)
5913                  */
5914                 appendPQExpBuffer(query,
5915                                                   "SELECT c.tableoid, c.oid, c.relname, "
5916                                                   "c.relacl, NULL as rrelacl, "
5917                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
5918                                                   "c.relkind, "
5919                                                   "c.relnamespace, "
5920                                                   "(%s c.relowner) AS rolname, "
5921                                                   "c.relchecks, c.relhastriggers, "
5922                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
5923                                                   "'f'::bool AS relrowsecurity, "
5924                                                   "'f'::bool AS relforcerowsecurity, "
5925                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
5926                                                   "tc.relfrozenxid AS tfrozenxid, "
5927                                                   "0 AS tminmxid, "
5928                                                   "c.relpersistence, 't' as relispopulated, "
5929                                                   "'d' AS relreplident, c.relpages, "
5930                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
5931                                                   "d.refobjid AS owning_tab, "
5932                                                   "d.refobjsubid AS owning_col, "
5933                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
5934                                                   "c.reloptions AS reloptions, "
5935                                                   "tc.reloptions AS toast_reloptions, "
5936                                                   "NULL AS changed_acl, "
5937                                                   "NULL AS partkeydef, "
5938                                                   "false AS ispartition, "
5939                                                   "NULL AS partbound "
5940                                                   "FROM pg_class c "
5941                                                   "LEFT JOIN pg_depend d ON "
5942                                                   "(c.relkind = '%c' AND "
5943                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
5944                                                   "d.objsubid = 0 AND "
5945                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
5946                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
5947                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
5948                                                   "ORDER BY c.oid",
5949                                                   username_subquery,
5950                                                   RELKIND_SEQUENCE,
5951                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
5952                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
5953                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
5954         }
5955         else if (fout->remoteVersion >= 90000)
5956         {
5957                 /*
5958                  * Left join to pick up dependency info linking sequences to their
5959                  * owning column, if any (note this dependency is AUTO as of 8.2)
5960                  */
5961                 appendPQExpBuffer(query,
5962                                                   "SELECT c.tableoid, c.oid, c.relname, "
5963                                                   "c.relacl, NULL as rrelacl, "
5964                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
5965                                                   "c.relkind, "
5966                                                   "c.relnamespace, "
5967                                                   "(%s c.relowner) AS rolname, "
5968                                                   "c.relchecks, c.relhastriggers, "
5969                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
5970                                                   "'f'::bool AS relrowsecurity, "
5971                                                   "'f'::bool AS relforcerowsecurity, "
5972                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
5973                                                   "tc.relfrozenxid AS tfrozenxid, "
5974                                                   "0 AS tminmxid, "
5975                                                   "'p' AS relpersistence, 't' as relispopulated, "
5976                                                   "'d' AS relreplident, c.relpages, "
5977                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
5978                                                   "d.refobjid AS owning_tab, "
5979                                                   "d.refobjsubid AS owning_col, "
5980                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
5981                                                   "c.reloptions AS reloptions, "
5982                                                   "tc.reloptions AS toast_reloptions, "
5983                                                   "NULL AS changed_acl, "
5984                                                   "NULL AS partkeydef, "
5985                                                   "false AS ispartition, "
5986                                                   "NULL AS partbound "
5987                                                   "FROM pg_class c "
5988                                                   "LEFT JOIN pg_depend d ON "
5989                                                   "(c.relkind = '%c' AND "
5990                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
5991                                                   "d.objsubid = 0 AND "
5992                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
5993                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
5994                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
5995                                                   "ORDER BY c.oid",
5996                                                   username_subquery,
5997                                                   RELKIND_SEQUENCE,
5998                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
5999                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6000         }
6001         else if (fout->remoteVersion >= 80400)
6002         {
6003                 /*
6004                  * Left join to pick up dependency info linking sequences to their
6005                  * owning column, if any (note this dependency is AUTO as of 8.2)
6006                  */
6007                 appendPQExpBuffer(query,
6008                                                   "SELECT c.tableoid, c.oid, c.relname, "
6009                                                   "c.relacl, NULL as rrelacl, "
6010                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6011                                                   "c.relkind, "
6012                                                   "c.relnamespace, "
6013                                                   "(%s c.relowner) AS rolname, "
6014                                                   "c.relchecks, c.relhastriggers, "
6015                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6016                                                   "'f'::bool AS relrowsecurity, "
6017                                                   "'f'::bool AS relforcerowsecurity, "
6018                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
6019                                                   "tc.relfrozenxid AS tfrozenxid, "
6020                                                   "0 AS tminmxid, "
6021                                                   "'p' AS relpersistence, 't' as relispopulated, "
6022                                                   "'d' AS relreplident, c.relpages, "
6023                                                   "NULL AS reloftype, "
6024                                                   "d.refobjid AS owning_tab, "
6025                                                   "d.refobjsubid AS owning_col, "
6026                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6027                                                   "c.reloptions AS reloptions, "
6028                                                   "tc.reloptions AS toast_reloptions, "
6029                                                   "NULL AS changed_acl, "
6030                                                   "NULL AS partkeydef, "
6031                                                   "false AS ispartition, "
6032                                                   "NULL AS partbound "
6033                                                   "FROM pg_class c "
6034                                                   "LEFT JOIN pg_depend d ON "
6035                                                   "(c.relkind = '%c' AND "
6036                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6037                                                   "d.objsubid = 0 AND "
6038                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6039                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6040                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
6041                                                   "ORDER BY c.oid",
6042                                                   username_subquery,
6043                                                   RELKIND_SEQUENCE,
6044                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6045                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6046         }
6047         else if (fout->remoteVersion >= 80200)
6048         {
6049                 /*
6050                  * Left join to pick up dependency info linking sequences to their
6051                  * owning column, if any (note this dependency is AUTO as of 8.2)
6052                  */
6053                 appendPQExpBuffer(query,
6054                                                   "SELECT c.tableoid, c.oid, c.relname, "
6055                                                   "c.relacl, NULL as rrelacl, "
6056                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6057                                                   "c.relkind, "
6058                                                   "c.relnamespace, "
6059                                                   "(%s c.relowner) AS rolname, "
6060                                                   "c.relchecks, (c.reltriggers <> 0) AS relhastriggers, "
6061                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6062                                                   "'f'::bool AS relrowsecurity, "
6063                                                   "'f'::bool AS relforcerowsecurity, "
6064                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
6065                                                   "tc.relfrozenxid AS tfrozenxid, "
6066                                                   "0 AS tminmxid, "
6067                                                   "'p' AS relpersistence, 't' as relispopulated, "
6068                                                   "'d' AS relreplident, c.relpages, "
6069                                                   "NULL AS reloftype, "
6070                                                   "d.refobjid AS owning_tab, "
6071                                                   "d.refobjsubid AS owning_col, "
6072                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6073                                                   "c.reloptions AS reloptions, "
6074                                                   "NULL AS toast_reloptions, "
6075                                                   "NULL AS changed_acl, "
6076                                                   "NULL AS partkeydef, "
6077                                                   "false AS ispartition, "
6078                                                   "NULL AS partbound "
6079                                                   "FROM pg_class c "
6080                                                   "LEFT JOIN pg_depend d ON "
6081                                                   "(c.relkind = '%c' AND "
6082                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6083                                                   "d.objsubid = 0 AND "
6084                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6085                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6086                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
6087                                                   "ORDER BY c.oid",
6088                                                   username_subquery,
6089                                                   RELKIND_SEQUENCE,
6090                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6091                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6092         }
6093         else
6094         {
6095                 /*
6096                  * Left join to pick up dependency info linking sequences to their
6097                  * owning column, if any
6098                  */
6099                 appendPQExpBuffer(query,
6100                                                   "SELECT c.tableoid, c.oid, relname, "
6101                                                   "relacl, NULL as rrelacl, "
6102                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6103                                                   "relkind, relnamespace, "
6104                                                   "(%s relowner) AS rolname, "
6105                                                   "relchecks, (reltriggers <> 0) AS relhastriggers, "
6106                                                   "relhasindex, relhasrules, relhasoids, "
6107                                                   "'f'::bool AS relrowsecurity, "
6108                                                   "'f'::bool AS relforcerowsecurity, "
6109                                                   "0 AS relfrozenxid, 0 AS relminmxid,"
6110                                                   "0 AS toid, "
6111                                                   "0 AS tfrozenxid, 0 AS tminmxid,"
6112                                                   "'p' AS relpersistence, 't' as relispopulated, "
6113                                                   "'d' AS relreplident, relpages, "
6114                                                   "NULL AS reloftype, "
6115                                                   "d.refobjid AS owning_tab, "
6116                                                   "d.refobjsubid AS owning_col, "
6117                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6118                                                   "NULL AS reloptions, "
6119                                                   "NULL AS toast_reloptions, "
6120                                                   "NULL AS changed_acl, "
6121                                                   "NULL AS partkeydef, "
6122                                                   "false AS ispartition, "
6123                                                   "NULL AS partbound "
6124                                                   "FROM pg_class c "
6125                                                   "LEFT JOIN pg_depend d ON "
6126                                                   "(c.relkind = '%c' AND "
6127                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6128                                                   "d.objsubid = 0 AND "
6129                                                   "d.refclassid = c.tableoid AND d.deptype = 'i') "
6130                                                   "WHERE relkind in ('%c', '%c', '%c', '%c') "
6131                                                   "ORDER BY c.oid",
6132                                                   username_subquery,
6133                                                   RELKIND_SEQUENCE,
6134                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6135                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6136         }
6137
6138         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6139
6140         ntups = PQntuples(res);
6141
6142         *numTables = ntups;
6143
6144         /*
6145          * Extract data from result and lock dumpable tables.  We do the locking
6146          * before anything else, to minimize the window wherein a table could
6147          * disappear under us.
6148          *
6149          * Note that we have to save info about all tables here, even when dumping
6150          * only one, because we don't yet know which tables might be inheritance
6151          * ancestors of the target table.
6152          */
6153         tblinfo = (TableInfo *) pg_malloc0(ntups * sizeof(TableInfo));
6154
6155         i_reltableoid = PQfnumber(res, "tableoid");
6156         i_reloid = PQfnumber(res, "oid");
6157         i_relname = PQfnumber(res, "relname");
6158         i_relnamespace = PQfnumber(res, "relnamespace");
6159         i_relacl = PQfnumber(res, "relacl");
6160         i_rrelacl = PQfnumber(res, "rrelacl");
6161         i_initrelacl = PQfnumber(res, "initrelacl");
6162         i_initrrelacl = PQfnumber(res, "initrrelacl");
6163         i_relkind = PQfnumber(res, "relkind");
6164         i_rolname = PQfnumber(res, "rolname");
6165         i_relchecks = PQfnumber(res, "relchecks");
6166         i_relhastriggers = PQfnumber(res, "relhastriggers");
6167         i_relhasindex = PQfnumber(res, "relhasindex");
6168         i_relhasrules = PQfnumber(res, "relhasrules");
6169         i_relrowsec = PQfnumber(res, "relrowsecurity");
6170         i_relforcerowsec = PQfnumber(res, "relforcerowsecurity");
6171         i_relhasoids = PQfnumber(res, "relhasoids");
6172         i_relfrozenxid = PQfnumber(res, "relfrozenxid");
6173         i_relminmxid = PQfnumber(res, "relminmxid");
6174         i_toastoid = PQfnumber(res, "toid");
6175         i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
6176         i_toastminmxid = PQfnumber(res, "tminmxid");
6177         i_relpersistence = PQfnumber(res, "relpersistence");
6178         i_relispopulated = PQfnumber(res, "relispopulated");
6179         i_relreplident = PQfnumber(res, "relreplident");
6180         i_relpages = PQfnumber(res, "relpages");
6181         i_owning_tab = PQfnumber(res, "owning_tab");
6182         i_owning_col = PQfnumber(res, "owning_col");
6183         i_reltablespace = PQfnumber(res, "reltablespace");
6184         i_reloptions = PQfnumber(res, "reloptions");
6185         i_checkoption = PQfnumber(res, "checkoption");
6186         i_toastreloptions = PQfnumber(res, "toast_reloptions");
6187         i_reloftype = PQfnumber(res, "reloftype");
6188         i_is_identity_sequence = PQfnumber(res, "is_identity_sequence");
6189         i_changed_acl = PQfnumber(res, "changed_acl");
6190         i_partkeydef = PQfnumber(res, "partkeydef");
6191         i_ispartition = PQfnumber(res, "ispartition");
6192         i_partbound = PQfnumber(res, "partbound");
6193
6194         if (dopt->lockWaitTimeout)
6195         {
6196                 /*
6197                  * Arrange to fail instead of waiting forever for a table lock.
6198                  *
6199                  * NB: this coding assumes that the only queries issued within the
6200                  * following loop are LOCK TABLEs; else the timeout may be undesirably
6201                  * applied to other things too.
6202                  */
6203                 resetPQExpBuffer(query);
6204                 appendPQExpBufferStr(query, "SET statement_timeout = ");
6205                 appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout));
6206                 ExecuteSqlStatement(fout, query->data);
6207         }
6208
6209         for (i = 0; i < ntups; i++)
6210         {
6211                 tblinfo[i].dobj.objType = DO_TABLE;
6212                 tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
6213                 tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
6214                 AssignDumpId(&tblinfo[i].dobj);
6215                 tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
6216                 tblinfo[i].dobj.namespace =
6217                         findNamespace(fout,
6218                                                   atooid(PQgetvalue(res, i, i_relnamespace)));
6219                 tblinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
6220                 tblinfo[i].relacl = pg_strdup(PQgetvalue(res, i, i_relacl));
6221                 tblinfo[i].rrelacl = pg_strdup(PQgetvalue(res, i, i_rrelacl));
6222                 tblinfo[i].initrelacl = pg_strdup(PQgetvalue(res, i, i_initrelacl));
6223                 tblinfo[i].initrrelacl = pg_strdup(PQgetvalue(res, i, i_initrrelacl));
6224                 tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
6225                 tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence));
6226                 tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
6227                 tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
6228                 tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
6229                 tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0);
6230                 tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0);
6231                 tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
6232                 tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0);
6233                 tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident));
6234                 tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages));
6235                 tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
6236                 tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid));
6237                 tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
6238                 tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
6239                 tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid));
6240                 if (PQgetisnull(res, i, i_reloftype))
6241                         tblinfo[i].reloftype = NULL;
6242                 else
6243                         tblinfo[i].reloftype = pg_strdup(PQgetvalue(res, i, i_reloftype));
6244                 tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
6245                 if (PQgetisnull(res, i, i_owning_tab))
6246                 {
6247                         tblinfo[i].owning_tab = InvalidOid;
6248                         tblinfo[i].owning_col = 0;
6249                 }
6250                 else
6251                 {
6252                         tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
6253                         tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
6254                 }
6255                 tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace));
6256                 tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions));
6257                 if (i_checkoption == -1 || PQgetisnull(res, i, i_checkoption))
6258                         tblinfo[i].checkoption = NULL;
6259                 else
6260                         tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption));
6261                 tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions));
6262
6263                 /* other fields were zeroed above */
6264
6265                 /*
6266                  * Decide whether we want to dump this table.
6267                  */
6268                 if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
6269                         tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE;
6270                 else
6271                         selectDumpableTable(&tblinfo[i], fout);
6272
6273                 /*
6274                  * If the table-level and all column-level ACLs for this table are
6275                  * unchanged, then we don't need to worry about including the ACLs for
6276                  * this table.  If any column-level ACLs have been changed, the
6277                  * 'changed_acl' column from the query will indicate that.
6278                  *
6279                  * This can result in a significant performance improvement in cases
6280                  * where we are only looking to dump out the ACL (eg: pg_catalog).
6281                  */
6282                 if (PQgetisnull(res, i, i_relacl) && PQgetisnull(res, i, i_rrelacl) &&
6283                         PQgetisnull(res, i, i_initrelacl) &&
6284                         PQgetisnull(res, i, i_initrrelacl) &&
6285                         strcmp(PQgetvalue(res, i, i_changed_acl), "f") == 0)
6286                         tblinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
6287
6288                 tblinfo[i].interesting = tblinfo[i].dobj.dump ? true : false;
6289                 tblinfo[i].dummy_view = false;  /* might get set during sort */
6290                 tblinfo[i].postponed_def = false;       /* might get set during sort */
6291
6292                 tblinfo[i].is_identity_sequence = (i_is_identity_sequence >= 0 &&
6293                                                                                    strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0);
6294
6295                 /* Partition key string or NULL */
6296                 tblinfo[i].partkeydef = pg_strdup(PQgetvalue(res, i, i_partkeydef));
6297                 tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0);
6298                 tblinfo[i].partbound = pg_strdup(PQgetvalue(res, i, i_partbound));
6299
6300                 /*
6301                  * Read-lock target tables to make sure they aren't DROPPED or altered
6302                  * in schema before we get around to dumping them.
6303                  *
6304                  * Note that we don't explicitly lock parents of the target tables; we
6305                  * assume our lock on the child is enough to prevent schema
6306                  * alterations to parent tables.
6307                  *
6308                  * NOTE: it'd be kinda nice to lock other relations too, not only
6309                  * plain tables, but the backend doesn't presently allow that.
6310                  *
6311                  * We only need to lock the table for certain components; see
6312                  * pg_dump.h
6313                  */
6314                 if (tblinfo[i].dobj.dump &&
6315                         (tblinfo[i].relkind == RELKIND_RELATION ||
6316                          tblinfo->relkind == RELKIND_PARTITIONED_TABLE) &&
6317                         (tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK))
6318                 {
6319                         resetPQExpBuffer(query);
6320                         appendPQExpBuffer(query,
6321                                                           "LOCK TABLE %s IN ACCESS SHARE MODE",
6322                                                           fmtQualifiedId(fout->remoteVersion,
6323                                                                                          tblinfo[i].dobj.namespace->dobj.name,
6324                                                                                          tblinfo[i].dobj.name));
6325                         ExecuteSqlStatement(fout, query->data);
6326                 }
6327
6328                 /* Emit notice if join for owner failed */
6329                 if (strlen(tblinfo[i].rolname) == 0)
6330                         write_msg(NULL, "WARNING: owner of table \"%s\" appears to be invalid\n",
6331                                           tblinfo[i].dobj.name);
6332         }
6333
6334         if (dopt->lockWaitTimeout)
6335         {
6336                 ExecuteSqlStatement(fout, "SET statement_timeout = 0");
6337         }
6338
6339         PQclear(res);
6340
6341         destroyPQExpBuffer(query);
6342
6343         return tblinfo;
6344 }
6345
6346 /*
6347  * getOwnedSeqs
6348  *        identify owned sequences and mark them as dumpable if owning table is
6349  *
6350  * We used to do this in getTables(), but it's better to do it after the
6351  * index used by findTableByOid() has been set up.
6352  */
6353 void
6354 getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables)
6355 {
6356         int                     i;
6357
6358         /*
6359          * Force sequences that are "owned" by table columns to be dumped whenever
6360          * their owning table is being dumped.
6361          */
6362         for (i = 0; i < numTables; i++)
6363         {
6364                 TableInfo  *seqinfo = &tblinfo[i];
6365                 TableInfo  *owning_tab;
6366
6367                 if (!OidIsValid(seqinfo->owning_tab))
6368                         continue;                       /* not an owned sequence */
6369
6370                 owning_tab = findTableByOid(seqinfo->owning_tab);
6371                 if (owning_tab == NULL)
6372                         exit_horribly(NULL, "failed sanity check, parent table with OID %u of sequence with OID %u not found\n",
6373                                                   seqinfo->owning_tab, seqinfo->dobj.catId.oid);
6374
6375                 /*
6376                  * We need to dump the components that are being dumped for the table
6377                  * and any components which the sequence is explicitly marked with.
6378                  *
6379                  * We can't simply use the set of components which are being dumped
6380                  * for the table as the table might be in an extension (and only the
6381                  * non-extension components, eg: ACLs if changed, security labels, and
6382                  * policies, are being dumped) while the sequence is not (and
6383                  * therefore the definition and other components should also be
6384                  * dumped).
6385                  *
6386                  * If the sequence is part of the extension then it should be properly
6387                  * marked by checkExtensionMembership() and this will be a no-op as
6388                  * the table will be equivalently marked.
6389                  */
6390                 seqinfo->dobj.dump = seqinfo->dobj.dump | owning_tab->dobj.dump;
6391
6392                 if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE)
6393                         seqinfo->interesting = true;
6394         }
6395 }
6396
6397 /*
6398  * getInherits
6399  *        read all the inheritance information
6400  * from the system catalogs return them in the InhInfo* structure
6401  *
6402  * numInherits is set to the number of pairs read in
6403  */
6404 InhInfo *
6405 getInherits(Archive *fout, int *numInherits)
6406 {
6407         PGresult   *res;
6408         int                     ntups;
6409         int                     i;
6410         PQExpBuffer query = createPQExpBuffer();
6411         InhInfo    *inhinfo;
6412
6413         int                     i_inhrelid;
6414         int                     i_inhparent;
6415
6416         /* Make sure we are in proper schema */
6417         selectSourceSchema(fout, "pg_catalog");
6418
6419         /*
6420          * Find all the inheritance information, excluding implicit inheritance
6421          * via partitioning.  We handle that case using getPartitions(), because
6422          * we want more information about partitions than just the parent-child
6423          * relationship.
6424          */
6425         appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits");
6426
6427         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6428
6429         ntups = PQntuples(res);
6430
6431         *numInherits = ntups;
6432
6433         inhinfo = (InhInfo *) pg_malloc(ntups * sizeof(InhInfo));
6434
6435         i_inhrelid = PQfnumber(res, "inhrelid");
6436         i_inhparent = PQfnumber(res, "inhparent");
6437
6438         for (i = 0; i < ntups; i++)
6439         {
6440                 inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
6441                 inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
6442         }
6443
6444         PQclear(res);
6445
6446         destroyPQExpBuffer(query);
6447
6448         return inhinfo;
6449 }
6450
6451 /*
6452  * getIndexes
6453  *        get information about every index on a dumpable table
6454  *
6455  * Note: index data is not returned directly to the caller, but it
6456  * does get entered into the DumpableObject tables.
6457  */
6458 void
6459 getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6460 {
6461         int                     i,
6462                                 j;
6463         PQExpBuffer query = createPQExpBuffer();
6464         PGresult   *res;
6465         IndxInfo   *indxinfo;
6466         ConstraintInfo *constrinfo;
6467         int                     i_tableoid,
6468                                 i_oid,
6469                                 i_indexname,
6470                                 i_indexdef,
6471                                 i_indnkeys,
6472                                 i_indkey,
6473                                 i_indisclustered,
6474                                 i_indisreplident,
6475                                 i_contype,
6476                                 i_conname,
6477                                 i_condeferrable,
6478                                 i_condeferred,
6479                                 i_contableoid,
6480                                 i_conoid,
6481                                 i_condef,
6482                                 i_tablespace,
6483                                 i_indreloptions,
6484                                 i_relpages;
6485         int                     ntups;
6486
6487         for (i = 0; i < numTables; i++)
6488         {
6489                 TableInfo  *tbinfo = &tblinfo[i];
6490
6491                 /* Only plain tables and materialized views have indexes. */
6492                 if (tbinfo->relkind != RELKIND_RELATION &&
6493                         tbinfo->relkind != RELKIND_MATVIEW)
6494                         continue;
6495                 if (!tbinfo->hasindex)
6496                         continue;
6497
6498                 /* Ignore indexes of tables whose definitions are not to be dumped */
6499                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
6500                         continue;
6501
6502                 if (g_verbose)
6503                         write_msg(NULL, "reading indexes for table \"%s.%s\"\n",
6504                                           tbinfo->dobj.namespace->dobj.name,
6505                                           tbinfo->dobj.name);
6506
6507                 /* Make sure we are in proper schema so indexdef is right */
6508                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
6509
6510                 /*
6511                  * The point of the messy-looking outer join is to find a constraint
6512                  * that is related by an internal dependency link to the index. If we
6513                  * find one, create a CONSTRAINT entry linked to the INDEX entry.  We
6514                  * assume an index won't have more than one internal dependency.
6515                  *
6516                  * As of 9.0 we don't need to look at pg_depend but can check for a
6517                  * match to pg_constraint.conindid.  The check on conrelid is
6518                  * redundant but useful because that column is indexed while conindid
6519                  * is not.
6520                  */
6521                 resetPQExpBuffer(query);
6522                 if (fout->remoteVersion >= 90400)
6523                 {
6524                         /*
6525                          * the test on indisready is necessary in 9.2, and harmless in
6526                          * earlier/later versions
6527                          */
6528                         appendPQExpBuffer(query,
6529                                                           "SELECT t.tableoid, t.oid, "
6530                                                           "t.relname AS indexname, "
6531                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6532                                                           "t.relnatts AS indnkeys, "
6533                                                           "i.indkey, i.indisclustered, "
6534                                                           "i.indisreplident, t.relpages, "
6535                                                           "c.contype, c.conname, "
6536                                                           "c.condeferrable, c.condeferred, "
6537                                                           "c.tableoid AS contableoid, "
6538                                                           "c.oid AS conoid, "
6539                                                           "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6540                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6541                                                           "t.reloptions AS indreloptions "
6542                                                           "FROM pg_catalog.pg_index i "
6543                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6544                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6545                                                           "ON (i.indrelid = c.conrelid AND "
6546                                                           "i.indexrelid = c.conindid AND "
6547                                                           "c.contype IN ('p','u','x')) "
6548                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6549                                                           "AND i.indisvalid AND i.indisready "
6550                                                           "ORDER BY indexname",
6551                                                           tbinfo->dobj.catId.oid);
6552                 }
6553                 else if (fout->remoteVersion >= 90000)
6554                 {
6555                         /*
6556                          * the test on indisready is necessary in 9.2, and harmless in
6557                          * earlier/later versions
6558                          */
6559                         appendPQExpBuffer(query,
6560                                                           "SELECT t.tableoid, t.oid, "
6561                                                           "t.relname AS indexname, "
6562                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6563                                                           "t.relnatts AS indnkeys, "
6564                                                           "i.indkey, i.indisclustered, "
6565                                                           "false AS indisreplident, t.relpages, "
6566                                                           "c.contype, c.conname, "
6567                                                           "c.condeferrable, c.condeferred, "
6568                                                           "c.tableoid AS contableoid, "
6569                                                           "c.oid AS conoid, "
6570                                                           "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6571                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6572                                                           "t.reloptions AS indreloptions "
6573                                                           "FROM pg_catalog.pg_index i "
6574                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6575                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6576                                                           "ON (i.indrelid = c.conrelid AND "
6577                                                           "i.indexrelid = c.conindid AND "
6578                                                           "c.contype IN ('p','u','x')) "
6579                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6580                                                           "AND i.indisvalid AND i.indisready "
6581                                                           "ORDER BY indexname",
6582                                                           tbinfo->dobj.catId.oid);
6583                 }
6584                 else if (fout->remoteVersion >= 80200)
6585                 {
6586                         appendPQExpBuffer(query,
6587                                                           "SELECT t.tableoid, t.oid, "
6588                                                           "t.relname AS indexname, "
6589                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6590                                                           "t.relnatts AS indnkeys, "
6591                                                           "i.indkey, i.indisclustered, "
6592                                                           "false AS indisreplident, t.relpages, "
6593                                                           "c.contype, c.conname, "
6594                                                           "c.condeferrable, c.condeferred, "
6595                                                           "c.tableoid AS contableoid, "
6596                                                           "c.oid AS conoid, "
6597                                                           "null AS condef, "
6598                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6599                                                           "t.reloptions AS indreloptions "
6600                                                           "FROM pg_catalog.pg_index i "
6601                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6602                                                           "LEFT JOIN pg_catalog.pg_depend d "
6603                                                           "ON (d.classid = t.tableoid "
6604                                                           "AND d.objid = t.oid "
6605                                                           "AND d.deptype = 'i') "
6606                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6607                                                           "ON (d.refclassid = c.tableoid "
6608                                                           "AND d.refobjid = c.oid) "
6609                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6610                                                           "AND i.indisvalid "
6611                                                           "ORDER BY indexname",
6612                                                           tbinfo->dobj.catId.oid);
6613                 }
6614                 else
6615                 {
6616                         appendPQExpBuffer(query,
6617                                                           "SELECT t.tableoid, t.oid, "
6618                                                           "t.relname AS indexname, "
6619                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6620                                                           "t.relnatts AS indnkeys, "
6621                                                           "i.indkey, i.indisclustered, "
6622                                                           "false AS indisreplident, t.relpages, "
6623                                                           "c.contype, c.conname, "
6624                                                           "c.condeferrable, c.condeferred, "
6625                                                           "c.tableoid AS contableoid, "
6626                                                           "c.oid AS conoid, "
6627                                                           "null AS condef, "
6628                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6629                                                           "null AS indreloptions "
6630                                                           "FROM pg_catalog.pg_index i "
6631                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6632                                                           "LEFT JOIN pg_catalog.pg_depend d "
6633                                                           "ON (d.classid = t.tableoid "
6634                                                           "AND d.objid = t.oid "
6635                                                           "AND d.deptype = 'i') "
6636                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6637                                                           "ON (d.refclassid = c.tableoid "
6638                                                           "AND d.refobjid = c.oid) "
6639                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6640                                                           "ORDER BY indexname",
6641                                                           tbinfo->dobj.catId.oid);
6642                 }
6643
6644                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6645
6646                 ntups = PQntuples(res);
6647
6648                 i_tableoid = PQfnumber(res, "tableoid");
6649                 i_oid = PQfnumber(res, "oid");
6650                 i_indexname = PQfnumber(res, "indexname");
6651                 i_indexdef = PQfnumber(res, "indexdef");
6652                 i_indnkeys = PQfnumber(res, "indnkeys");
6653                 i_indkey = PQfnumber(res, "indkey");
6654                 i_indisclustered = PQfnumber(res, "indisclustered");
6655                 i_indisreplident = PQfnumber(res, "indisreplident");
6656                 i_relpages = PQfnumber(res, "relpages");
6657                 i_contype = PQfnumber(res, "contype");
6658                 i_conname = PQfnumber(res, "conname");
6659                 i_condeferrable = PQfnumber(res, "condeferrable");
6660                 i_condeferred = PQfnumber(res, "condeferred");
6661                 i_contableoid = PQfnumber(res, "contableoid");
6662                 i_conoid = PQfnumber(res, "conoid");
6663                 i_condef = PQfnumber(res, "condef");
6664                 i_tablespace = PQfnumber(res, "tablespace");
6665                 i_indreloptions = PQfnumber(res, "indreloptions");
6666
6667                 indxinfo = (IndxInfo *) pg_malloc(ntups * sizeof(IndxInfo));
6668                 constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
6669
6670                 for (j = 0; j < ntups; j++)
6671                 {
6672                         char            contype;
6673
6674                         indxinfo[j].dobj.objType = DO_INDEX;
6675                         indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
6676                         indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
6677                         AssignDumpId(&indxinfo[j].dobj);
6678                         indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname));
6679                         indxinfo[j].dobj.namespace = tbinfo->dobj.namespace;
6680                         indxinfo[j].indextable = tbinfo;
6681                         indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef));
6682                         indxinfo[j].indnkeys = atoi(PQgetvalue(res, j, i_indnkeys));
6683                         indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
6684                         indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions));
6685                         indxinfo[j].indkeys = (Oid *) pg_malloc(indxinfo[j].indnkeys * sizeof(Oid));
6686                         parseOidArray(PQgetvalue(res, j, i_indkey),
6687                                                   indxinfo[j].indkeys, indxinfo[j].indnkeys);
6688                         indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
6689                         indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
6690                         indxinfo[j].relpages = atoi(PQgetvalue(res, j, i_relpages));
6691                         contype = *(PQgetvalue(res, j, i_contype));
6692
6693                         if (contype == 'p' || contype == 'u' || contype == 'x')
6694                         {
6695                                 /*
6696                                  * If we found a constraint matching the index, create an
6697                                  * entry for it.
6698                                  */
6699                                 constrinfo[j].dobj.objType = DO_CONSTRAINT;
6700                                 constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
6701                                 constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
6702                                 AssignDumpId(&constrinfo[j].dobj);
6703                                 constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
6704                                 constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
6705                                 constrinfo[j].contable = tbinfo;
6706                                 constrinfo[j].condomain = NULL;
6707                                 constrinfo[j].contype = contype;
6708                                 if (contype == 'x')
6709                                         constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
6710                                 else
6711                                         constrinfo[j].condef = NULL;
6712                                 constrinfo[j].confrelid = InvalidOid;
6713                                 constrinfo[j].conindex = indxinfo[j].dobj.dumpId;
6714                                 constrinfo[j].condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't';
6715                                 constrinfo[j].condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't';
6716                                 constrinfo[j].conislocal = true;
6717                                 constrinfo[j].separate = true;
6718
6719                                 indxinfo[j].indexconstraint = constrinfo[j].dobj.dumpId;
6720                         }
6721                         else
6722                         {
6723                                 /* Plain secondary index */
6724                                 indxinfo[j].indexconstraint = 0;
6725                         }
6726                 }
6727
6728                 PQclear(res);
6729         }
6730
6731         destroyPQExpBuffer(query);
6732 }
6733
6734 /*
6735  * getExtendedStatistics
6736  *        get information about extended statistics on a dumpable table
6737  *        or materialized view.
6738  *
6739  * Note: extended statistics data is not returned directly to the caller, but
6740  * it does get entered into the DumpableObject tables.
6741  */
6742 void
6743 getExtendedStatistics(Archive *fout, TableInfo tblinfo[], int numTables)
6744 {
6745         int                     i,
6746                                 j;
6747         PQExpBuffer query;
6748         PGresult   *res;
6749         StatsExtInfo *statsextinfo;
6750         int                     ntups;
6751         int                     i_tableoid;
6752         int                     i_oid;
6753         int                     i_stxname;
6754         int                     i_stxdef;
6755
6756         /* Extended statistics were new in v10 */
6757         if (fout->remoteVersion < 100000)
6758                 return;
6759
6760         query = createPQExpBuffer();
6761
6762         for (i = 0; i < numTables; i++)
6763         {
6764                 TableInfo  *tbinfo = &tblinfo[i];
6765
6766                 /*
6767                  * Only plain tables, materialized views, foreign tables and
6768                  * partitioned tables can have extended statistics.
6769                  */
6770                 if (tbinfo->relkind != RELKIND_RELATION &&
6771                         tbinfo->relkind != RELKIND_MATVIEW &&
6772                         tbinfo->relkind != RELKIND_FOREIGN_TABLE &&
6773                         tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
6774                         continue;
6775
6776                 /*
6777                  * Ignore extended statistics of tables whose definitions are not to
6778                  * be dumped.
6779                  */
6780                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
6781                         continue;
6782
6783                 if (g_verbose)
6784                         write_msg(NULL, "reading extended statistics for table \"%s.%s\"\n",
6785                                           tbinfo->dobj.namespace->dobj.name,
6786                                           tbinfo->dobj.name);
6787
6788                 /* Make sure we are in proper schema so stadef is right */
6789                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
6790
6791                 resetPQExpBuffer(query);
6792
6793                 appendPQExpBuffer(query,
6794                                                   "SELECT "
6795                                                   "tableoid, "
6796                                                   "oid, "
6797                                                   "stxname, "
6798                                                   "pg_catalog.pg_get_statisticsobjdef(oid) AS stxdef "
6799                                                   "FROM pg_statistic_ext "
6800                                                   "WHERE stxrelid = '%u' "
6801                                                   "ORDER BY stxname", tbinfo->dobj.catId.oid);
6802
6803                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6804
6805                 ntups = PQntuples(res);
6806
6807                 i_tableoid = PQfnumber(res, "tableoid");
6808                 i_oid = PQfnumber(res, "oid");
6809                 i_stxname = PQfnumber(res, "stxname");
6810                 i_stxdef = PQfnumber(res, "stxdef");
6811
6812                 statsextinfo = (StatsExtInfo *) pg_malloc(ntups * sizeof(StatsExtInfo));
6813
6814                 for (j = 0; j < ntups; j++)
6815                 {
6816                         statsextinfo[j].dobj.objType = DO_STATSEXT;
6817                         statsextinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
6818                         statsextinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
6819                         AssignDumpId(&statsextinfo[j].dobj);
6820                         statsextinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_stxname));
6821                         statsextinfo[j].dobj.namespace = tbinfo->dobj.namespace;
6822                         statsextinfo[j].statsexttable = tbinfo;
6823                         statsextinfo[j].statsextdef = pg_strdup(PQgetvalue(res, j, i_stxdef));
6824                 }
6825
6826                 PQclear(res);
6827         }
6828
6829         destroyPQExpBuffer(query);
6830 }
6831
6832 /*
6833  * getConstraints
6834  *
6835  * Get info about constraints on dumpable tables.
6836  *
6837  * Currently handles foreign keys only.
6838  * Unique and primary key constraints are handled with indexes,
6839  * while check constraints are processed in getTableAttrs().
6840  */
6841 void
6842 getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
6843 {
6844         int                     i,
6845                                 j;
6846         ConstraintInfo *constrinfo;
6847         PQExpBuffer query;
6848         PGresult   *res;
6849         int                     i_contableoid,
6850                                 i_conoid,
6851                                 i_conname,
6852                                 i_confrelid,
6853                                 i_condef;
6854         int                     ntups;
6855
6856         query = createPQExpBuffer();
6857
6858         for (i = 0; i < numTables; i++)
6859         {
6860                 TableInfo  *tbinfo = &tblinfo[i];
6861
6862                 if (!tbinfo->hastriggers ||
6863                         !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
6864                         continue;
6865
6866                 if (g_verbose)
6867                         write_msg(NULL, "reading foreign key constraints for table \"%s.%s\"\n",
6868                                           tbinfo->dobj.namespace->dobj.name,
6869                                           tbinfo->dobj.name);
6870
6871                 /*
6872                  * select table schema to ensure constraint expr is qualified if
6873                  * needed
6874                  */
6875                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
6876
6877                 resetPQExpBuffer(query);
6878                 appendPQExpBuffer(query,
6879                                                   "SELECT tableoid, oid, conname, confrelid, "
6880                                                   "pg_catalog.pg_get_constraintdef(oid) AS condef "
6881                                                   "FROM pg_catalog.pg_constraint "
6882                                                   "WHERE conrelid = '%u'::pg_catalog.oid "
6883                                                   "AND contype = 'f'",
6884                                                   tbinfo->dobj.catId.oid);
6885                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6886
6887                 ntups = PQntuples(res);
6888
6889                 i_contableoid = PQfnumber(res, "tableoid");
6890                 i_conoid = PQfnumber(res, "oid");
6891                 i_conname = PQfnumber(res, "conname");
6892                 i_confrelid = PQfnumber(res, "confrelid");
6893                 i_condef = PQfnumber(res, "condef");
6894
6895                 constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
6896
6897                 for (j = 0; j < ntups; j++)
6898                 {
6899                         constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
6900                         constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
6901                         constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
6902                         AssignDumpId(&constrinfo[j].dobj);
6903                         constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
6904                         constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
6905                         constrinfo[j].contable = tbinfo;
6906                         constrinfo[j].condomain = NULL;
6907                         constrinfo[j].contype = 'f';
6908                         constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
6909                         constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid));
6910                         constrinfo[j].conindex = 0;
6911                         constrinfo[j].condeferrable = false;
6912                         constrinfo[j].condeferred = false;
6913                         constrinfo[j].conislocal = true;
6914                         constrinfo[j].separate = true;
6915                 }
6916
6917                 PQclear(res);
6918         }
6919
6920         destroyPQExpBuffer(query);
6921 }
6922
6923 /*
6924  * getDomainConstraints
6925  *
6926  * Get info about constraints on a domain.
6927  */
6928 static void
6929 getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
6930 {
6931         int                     i;
6932         ConstraintInfo *constrinfo;
6933         PQExpBuffer query;
6934         PGresult   *res;
6935         int                     i_tableoid,
6936                                 i_oid,
6937                                 i_conname,
6938                                 i_consrc;
6939         int                     ntups;
6940
6941         /*
6942          * select appropriate schema to ensure names in constraint are properly
6943          * qualified
6944          */
6945         selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
6946
6947         query = createPQExpBuffer();
6948
6949         if (fout->remoteVersion >= 90100)
6950                 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
6951                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
6952                                                   "convalidated "
6953                                                   "FROM pg_catalog.pg_constraint "
6954                                                   "WHERE contypid = '%u'::pg_catalog.oid "
6955                                                   "ORDER BY conname",
6956                                                   tyinfo->dobj.catId.oid);
6957
6958         else
6959                 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
6960                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
6961                                                   "true as convalidated "
6962                                                   "FROM pg_catalog.pg_constraint "
6963                                                   "WHERE contypid = '%u'::pg_catalog.oid "
6964                                                   "ORDER BY conname",
6965                                                   tyinfo->dobj.catId.oid);
6966
6967         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6968
6969         ntups = PQntuples(res);
6970
6971         i_tableoid = PQfnumber(res, "tableoid");
6972         i_oid = PQfnumber(res, "oid");
6973         i_conname = PQfnumber(res, "conname");
6974         i_consrc = PQfnumber(res, "consrc");
6975
6976         constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
6977
6978         tyinfo->nDomChecks = ntups;
6979         tyinfo->domChecks = constrinfo;
6980
6981         for (i = 0; i < ntups; i++)
6982         {
6983                 bool            validated = PQgetvalue(res, i, 4)[0] == 't';
6984
6985                 constrinfo[i].dobj.objType = DO_CONSTRAINT;
6986                 constrinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6987                 constrinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6988                 AssignDumpId(&constrinfo[i].dobj);
6989                 constrinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
6990                 constrinfo[i].dobj.namespace = tyinfo->dobj.namespace;
6991                 constrinfo[i].contable = NULL;
6992                 constrinfo[i].condomain = tyinfo;
6993                 constrinfo[i].contype = 'c';
6994                 constrinfo[i].condef = pg_strdup(PQgetvalue(res, i, i_consrc));
6995                 constrinfo[i].confrelid = InvalidOid;
6996                 constrinfo[i].conindex = 0;
6997                 constrinfo[i].condeferrable = false;
6998                 constrinfo[i].condeferred = false;
6999                 constrinfo[i].conislocal = true;
7000
7001                 constrinfo[i].separate = !validated;
7002
7003                 /*
7004                  * Make the domain depend on the constraint, ensuring it won't be
7005                  * output till any constraint dependencies are OK.  If the constraint
7006                  * has not been validated, it's going to be dumped after the domain
7007                  * anyway, so this doesn't matter.
7008                  */
7009                 if (validated)
7010                         addObjectDependency(&tyinfo->dobj,
7011                                                                 constrinfo[i].dobj.dumpId);
7012         }
7013
7014         PQclear(res);
7015
7016         destroyPQExpBuffer(query);
7017 }
7018
7019 /*
7020  * getRules
7021  *        get basic information about every rule in the system
7022  *
7023  * numRules is set to the number of rules read in
7024  */
7025 RuleInfo *
7026 getRules(Archive *fout, int *numRules)
7027 {
7028         PGresult   *res;
7029         int                     ntups;
7030         int                     i;
7031         PQExpBuffer query = createPQExpBuffer();
7032         RuleInfo   *ruleinfo;
7033         int                     i_tableoid;
7034         int                     i_oid;
7035         int                     i_rulename;
7036         int                     i_ruletable;
7037         int                     i_ev_type;
7038         int                     i_is_instead;
7039         int                     i_ev_enabled;
7040
7041         /* Make sure we are in proper schema */
7042         selectSourceSchema(fout, "pg_catalog");
7043
7044         if (fout->remoteVersion >= 80300)
7045         {
7046                 appendPQExpBufferStr(query, "SELECT "
7047                                                          "tableoid, oid, rulename, "
7048                                                          "ev_class AS ruletable, ev_type, is_instead, "
7049                                                          "ev_enabled "
7050                                                          "FROM pg_rewrite "
7051                                                          "ORDER BY oid");
7052         }
7053         else
7054         {
7055                 appendPQExpBufferStr(query, "SELECT "
7056                                                          "tableoid, oid, rulename, "
7057                                                          "ev_class AS ruletable, ev_type, is_instead, "
7058                                                          "'O'::char AS ev_enabled "
7059                                                          "FROM pg_rewrite "
7060                                                          "ORDER BY oid");
7061         }
7062
7063         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7064
7065         ntups = PQntuples(res);
7066
7067         *numRules = ntups;
7068
7069         ruleinfo = (RuleInfo *) pg_malloc(ntups * sizeof(RuleInfo));
7070
7071         i_tableoid = PQfnumber(res, "tableoid");
7072         i_oid = PQfnumber(res, "oid");
7073         i_rulename = PQfnumber(res, "rulename");
7074         i_ruletable = PQfnumber(res, "ruletable");
7075         i_ev_type = PQfnumber(res, "ev_type");
7076         i_is_instead = PQfnumber(res, "is_instead");
7077         i_ev_enabled = PQfnumber(res, "ev_enabled");
7078
7079         for (i = 0; i < ntups; i++)
7080         {
7081                 Oid                     ruletableoid;
7082
7083                 ruleinfo[i].dobj.objType = DO_RULE;
7084                 ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7085                 ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7086                 AssignDumpId(&ruleinfo[i].dobj);
7087                 ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename));
7088                 ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
7089                 ruleinfo[i].ruletable = findTableByOid(ruletableoid);
7090                 if (ruleinfo[i].ruletable == NULL)
7091                         exit_horribly(NULL, "failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found\n",
7092                                                   ruletableoid, ruleinfo[i].dobj.catId.oid);
7093                 ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace;
7094                 ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump;
7095                 ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
7096                 ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
7097                 ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled));
7098                 if (ruleinfo[i].ruletable)
7099                 {
7100                         /*
7101                          * If the table is a view or materialized view, force its ON
7102                          * SELECT rule to be sorted before the view itself --- this
7103                          * ensures that any dependencies for the rule affect the table's
7104                          * positioning. Other rules are forced to appear after their
7105                          * table.
7106                          */
7107                         if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW ||
7108                                  ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) &&
7109                                 ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
7110                         {
7111                                 addObjectDependency(&ruleinfo[i].ruletable->dobj,
7112                                                                         ruleinfo[i].dobj.dumpId);
7113                                 /* We'll merge the rule into CREATE VIEW, if possible */
7114                                 ruleinfo[i].separate = false;
7115                         }
7116                         else
7117                         {
7118                                 addObjectDependency(&ruleinfo[i].dobj,
7119                                                                         ruleinfo[i].ruletable->dobj.dumpId);
7120                                 ruleinfo[i].separate = true;
7121                         }
7122                 }
7123                 else
7124                         ruleinfo[i].separate = true;
7125         }
7126
7127         PQclear(res);
7128
7129         destroyPQExpBuffer(query);
7130
7131         return ruleinfo;
7132 }
7133
7134 /*
7135  * getTriggers
7136  *        get information about every trigger on a dumpable table
7137  *
7138  * Note: trigger data is not returned directly to the caller, but it
7139  * does get entered into the DumpableObject tables.
7140  */
7141 void
7142 getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
7143 {
7144         int                     i,
7145                                 j;
7146         PQExpBuffer query = createPQExpBuffer();
7147         PGresult   *res;
7148         TriggerInfo *tginfo;
7149         int                     i_tableoid,
7150                                 i_oid,
7151                                 i_tgname,
7152                                 i_tgfname,
7153                                 i_tgtype,
7154                                 i_tgnargs,
7155                                 i_tgargs,
7156                                 i_tgisconstraint,
7157                                 i_tgconstrname,
7158                                 i_tgconstrrelid,
7159                                 i_tgconstrrelname,
7160                                 i_tgenabled,
7161                                 i_tgdeferrable,
7162                                 i_tginitdeferred,
7163                                 i_tgdef;
7164         int                     ntups;
7165
7166         for (i = 0; i < numTables; i++)
7167         {
7168                 TableInfo  *tbinfo = &tblinfo[i];
7169
7170                 if (!tbinfo->hastriggers ||
7171                         !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
7172                         continue;
7173
7174                 if (g_verbose)
7175                         write_msg(NULL, "reading triggers for table \"%s.%s\"\n",
7176                                           tbinfo->dobj.namespace->dobj.name,
7177                                           tbinfo->dobj.name);
7178
7179                 /*
7180                  * select table schema to ensure regproc name is qualified if needed
7181                  */
7182                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
7183
7184                 resetPQExpBuffer(query);
7185                 if (fout->remoteVersion >= 90000)
7186                 {
7187                         /*
7188                          * NB: think not to use pretty=true in pg_get_triggerdef.  It
7189                          * could result in non-forward-compatible dumps of WHEN clauses
7190                          * due to under-parenthesization.
7191                          */
7192                         appendPQExpBuffer(query,
7193                                                           "SELECT tgname, "
7194                                                           "tgfoid::pg_catalog.regproc AS tgfname, "
7195                                                           "pg_catalog.pg_get_triggerdef(oid, false) AS tgdef, "
7196                                                           "tgenabled, tableoid, oid "
7197                                                           "FROM pg_catalog.pg_trigger t "
7198                                                           "WHERE tgrelid = '%u'::pg_catalog.oid "
7199                                                           "AND NOT tgisinternal",
7200                                                           tbinfo->dobj.catId.oid);
7201                 }
7202                 else if (fout->remoteVersion >= 80300)
7203                 {
7204                         /*
7205                          * We ignore triggers that are tied to a foreign-key constraint
7206                          */
7207                         appendPQExpBuffer(query,
7208                                                           "SELECT tgname, "
7209                                                           "tgfoid::pg_catalog.regproc AS tgfname, "
7210                                                           "tgtype, tgnargs, tgargs, tgenabled, "
7211                                                           "tgisconstraint, tgconstrname, tgdeferrable, "
7212                                                           "tgconstrrelid, tginitdeferred, tableoid, oid, "
7213                                                           "tgconstrrelid::pg_catalog.regclass AS tgconstrrelname "
7214                                                           "FROM pg_catalog.pg_trigger t "
7215                                                           "WHERE tgrelid = '%u'::pg_catalog.oid "
7216                                                           "AND tgconstraint = 0",
7217                                                           tbinfo->dobj.catId.oid);
7218                 }
7219                 else
7220                 {
7221                         /*
7222                          * We ignore triggers that are tied to a foreign-key constraint,
7223                          * but in these versions we have to grovel through pg_constraint
7224                          * to find out
7225                          */
7226                         appendPQExpBuffer(query,
7227                                                           "SELECT tgname, "
7228                                                           "tgfoid::pg_catalog.regproc AS tgfname, "
7229                                                           "tgtype, tgnargs, tgargs, tgenabled, "
7230                                                           "tgisconstraint, tgconstrname, tgdeferrable, "
7231                                                           "tgconstrrelid, tginitdeferred, tableoid, oid, "
7232                                                           "tgconstrrelid::pg_catalog.regclass AS tgconstrrelname "
7233                                                           "FROM pg_catalog.pg_trigger t "
7234                                                           "WHERE tgrelid = '%u'::pg_catalog.oid "
7235                                                           "AND (NOT tgisconstraint "
7236                                                           " OR NOT EXISTS"
7237                                                           "  (SELECT 1 FROM pg_catalog.pg_depend d "
7238                                                           "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
7239                                                           "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))",
7240                                                           tbinfo->dobj.catId.oid);
7241                 }
7242
7243                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7244
7245                 ntups = PQntuples(res);
7246
7247                 i_tableoid = PQfnumber(res, "tableoid");
7248                 i_oid = PQfnumber(res, "oid");
7249                 i_tgname = PQfnumber(res, "tgname");
7250                 i_tgfname = PQfnumber(res, "tgfname");
7251                 i_tgtype = PQfnumber(res, "tgtype");
7252                 i_tgnargs = PQfnumber(res, "tgnargs");
7253                 i_tgargs = PQfnumber(res, "tgargs");
7254                 i_tgisconstraint = PQfnumber(res, "tgisconstraint");
7255                 i_tgconstrname = PQfnumber(res, "tgconstrname");
7256                 i_tgconstrrelid = PQfnumber(res, "tgconstrrelid");
7257                 i_tgconstrrelname = PQfnumber(res, "tgconstrrelname");
7258                 i_tgenabled = PQfnumber(res, "tgenabled");
7259                 i_tgdeferrable = PQfnumber(res, "tgdeferrable");
7260                 i_tginitdeferred = PQfnumber(res, "tginitdeferred");
7261                 i_tgdef = PQfnumber(res, "tgdef");
7262
7263                 tginfo = (TriggerInfo *) pg_malloc(ntups * sizeof(TriggerInfo));
7264
7265                 tbinfo->numTriggers = ntups;
7266                 tbinfo->triggers = tginfo;
7267
7268                 for (j = 0; j < ntups; j++)
7269                 {
7270                         tginfo[j].dobj.objType = DO_TRIGGER;
7271                         tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
7272                         tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
7273                         AssignDumpId(&tginfo[j].dobj);
7274                         tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname));
7275                         tginfo[j].dobj.namespace = tbinfo->dobj.namespace;
7276                         tginfo[j].tgtable = tbinfo;
7277                         tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled));
7278                         if (i_tgdef >= 0)
7279                         {
7280                                 tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef));
7281
7282                                 /* remaining fields are not valid if we have tgdef */
7283                                 tginfo[j].tgfname = NULL;
7284                                 tginfo[j].tgtype = 0;
7285                                 tginfo[j].tgnargs = 0;
7286                                 tginfo[j].tgargs = NULL;
7287                                 tginfo[j].tgisconstraint = false;
7288                                 tginfo[j].tgdeferrable = false;
7289                                 tginfo[j].tginitdeferred = false;
7290                                 tginfo[j].tgconstrname = NULL;
7291                                 tginfo[j].tgconstrrelid = InvalidOid;
7292                                 tginfo[j].tgconstrrelname = NULL;
7293                         }
7294                         else
7295                         {
7296                                 tginfo[j].tgdef = NULL;
7297
7298                                 tginfo[j].tgfname = pg_strdup(PQgetvalue(res, j, i_tgfname));
7299                                 tginfo[j].tgtype = atoi(PQgetvalue(res, j, i_tgtype));
7300                                 tginfo[j].tgnargs = atoi(PQgetvalue(res, j, i_tgnargs));
7301                                 tginfo[j].tgargs = pg_strdup(PQgetvalue(res, j, i_tgargs));
7302                                 tginfo[j].tgisconstraint = *(PQgetvalue(res, j, i_tgisconstraint)) == 't';
7303                                 tginfo[j].tgdeferrable = *(PQgetvalue(res, j, i_tgdeferrable)) == 't';
7304                                 tginfo[j].tginitdeferred = *(PQgetvalue(res, j, i_tginitdeferred)) == 't';
7305
7306                                 if (tginfo[j].tgisconstraint)
7307                                 {
7308                                         tginfo[j].tgconstrname = pg_strdup(PQgetvalue(res, j, i_tgconstrname));
7309                                         tginfo[j].tgconstrrelid = atooid(PQgetvalue(res, j, i_tgconstrrelid));
7310                                         if (OidIsValid(tginfo[j].tgconstrrelid))
7311                                         {
7312                                                 if (PQgetisnull(res, j, i_tgconstrrelname))
7313                                                         exit_horribly(NULL, "query produced null referenced table name for foreign key trigger \"%s\" on table \"%s\" (OID of table: %u)\n",
7314                                                                                   tginfo[j].dobj.name,
7315                                                                                   tbinfo->dobj.name,
7316                                                                                   tginfo[j].tgconstrrelid);
7317                                                 tginfo[j].tgconstrrelname = pg_strdup(PQgetvalue(res, j, i_tgconstrrelname));
7318                                         }
7319                                         else
7320                                                 tginfo[j].tgconstrrelname = NULL;
7321                                 }
7322                                 else
7323                                 {
7324                                         tginfo[j].tgconstrname = NULL;
7325                                         tginfo[j].tgconstrrelid = InvalidOid;
7326                                         tginfo[j].tgconstrrelname = NULL;
7327                                 }
7328                         }
7329                 }
7330
7331                 PQclear(res);
7332         }
7333
7334         destroyPQExpBuffer(query);
7335 }
7336
7337 /*
7338  * getEventTriggers
7339  *        get information about event triggers
7340  */
7341 EventTriggerInfo *
7342 getEventTriggers(Archive *fout, int *numEventTriggers)
7343 {
7344         int                     i;
7345         PQExpBuffer query;
7346         PGresult   *res;
7347         EventTriggerInfo *evtinfo;
7348         int                     i_tableoid,
7349                                 i_oid,
7350                                 i_evtname,
7351                                 i_evtevent,
7352                                 i_evtowner,
7353                                 i_evttags,
7354                                 i_evtfname,
7355                                 i_evtenabled;
7356         int                     ntups;
7357
7358         /* Before 9.3, there are no event triggers */
7359         if (fout->remoteVersion < 90300)
7360         {
7361                 *numEventTriggers = 0;
7362                 return NULL;
7363         }
7364
7365         query = createPQExpBuffer();
7366
7367         /* Make sure we are in proper schema */
7368         selectSourceSchema(fout, "pg_catalog");
7369
7370         appendPQExpBuffer(query,
7371                                           "SELECT e.tableoid, e.oid, evtname, evtenabled, "
7372                                           "evtevent, (%s evtowner) AS evtowner, "
7373                                           "array_to_string(array("
7374                                           "select quote_literal(x) "
7375                                           " from unnest(evttags) as t(x)), ', ') as evttags, "
7376                                           "e.evtfoid::regproc as evtfname "
7377                                           "FROM pg_event_trigger e "
7378                                           "ORDER BY e.oid",
7379                                           username_subquery);
7380
7381         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7382
7383         ntups = PQntuples(res);
7384
7385         *numEventTriggers = ntups;
7386
7387         evtinfo = (EventTriggerInfo *) pg_malloc(ntups * sizeof(EventTriggerInfo));
7388
7389         i_tableoid = PQfnumber(res, "tableoid");
7390         i_oid = PQfnumber(res, "oid");
7391         i_evtname = PQfnumber(res, "evtname");
7392         i_evtevent = PQfnumber(res, "evtevent");
7393         i_evtowner = PQfnumber(res, "evtowner");
7394         i_evttags = PQfnumber(res, "evttags");
7395         i_evtfname = PQfnumber(res, "evtfname");
7396         i_evtenabled = PQfnumber(res, "evtenabled");
7397
7398         for (i = 0; i < ntups; i++)
7399         {
7400                 evtinfo[i].dobj.objType = DO_EVENT_TRIGGER;
7401                 evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7402                 evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7403                 AssignDumpId(&evtinfo[i].dobj);
7404                 evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname));
7405                 evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname));
7406                 evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent));
7407                 evtinfo[i].evtowner = pg_strdup(PQgetvalue(res, i, i_evtowner));
7408                 evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags));
7409                 evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname));
7410                 evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled));
7411
7412                 /* Decide whether we want to dump it */
7413                 selectDumpableObject(&(evtinfo[i].dobj), fout);
7414
7415                 /* Event Triggers do not currently have ACLs. */
7416                 evtinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
7417         }
7418
7419         PQclear(res);
7420
7421         destroyPQExpBuffer(query);
7422
7423         return evtinfo;
7424 }
7425
7426 /*
7427  * getProcLangs
7428  *        get basic information about every procedural language in the system
7429  *
7430  * numProcLangs is set to the number of langs read in
7431  *
7432  * NB: this must run after getFuncs() because we assume we can do
7433  * findFuncByOid().
7434  */
7435 ProcLangInfo *
7436 getProcLangs(Archive *fout, int *numProcLangs)
7437 {
7438         DumpOptions *dopt = fout->dopt;
7439         PGresult   *res;
7440         int                     ntups;
7441         int                     i;
7442         PQExpBuffer query = createPQExpBuffer();
7443         ProcLangInfo *planginfo;
7444         int                     i_tableoid;
7445         int                     i_oid;
7446         int                     i_lanname;
7447         int                     i_lanpltrusted;
7448         int                     i_lanplcallfoid;
7449         int                     i_laninline;
7450         int                     i_lanvalidator;
7451         int                     i_lanacl;
7452         int                     i_rlanacl;
7453         int                     i_initlanacl;
7454         int                     i_initrlanacl;
7455         int                     i_lanowner;
7456
7457         /* Make sure we are in proper schema */
7458         selectSourceSchema(fout, "pg_catalog");
7459
7460         if (fout->remoteVersion >= 90600)
7461         {
7462                 PQExpBuffer acl_subquery = createPQExpBuffer();
7463                 PQExpBuffer racl_subquery = createPQExpBuffer();
7464                 PQExpBuffer initacl_subquery = createPQExpBuffer();
7465                 PQExpBuffer initracl_subquery = createPQExpBuffer();
7466
7467                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
7468                                                 initracl_subquery, "l.lanacl", "l.lanowner", "'l'",
7469                                                 dopt->binary_upgrade);
7470
7471                 /* pg_language has a laninline column */
7472                 appendPQExpBuffer(query, "SELECT l.tableoid, l.oid, "
7473                                                   "l.lanname, l.lanpltrusted, l.lanplcallfoid, "
7474                                                   "l.laninline, l.lanvalidator, "
7475                                                   "%s AS lanacl, "
7476                                                   "%s AS rlanacl, "
7477                                                   "%s AS initlanacl, "
7478                                                   "%s AS initrlanacl, "
7479                                                   "(%s l.lanowner) AS lanowner "
7480                                                   "FROM pg_language l "
7481                                                   "LEFT JOIN pg_init_privs pip ON "
7482                                                   "(l.oid = pip.objoid "
7483                                                   "AND pip.classoid = 'pg_language'::regclass "
7484                                                   "AND pip.objsubid = 0) "
7485                                                   "WHERE l.lanispl "
7486                                                   "ORDER BY l.oid",
7487                                                   acl_subquery->data,
7488                                                   racl_subquery->data,
7489                                                   initacl_subquery->data,
7490                                                   initracl_subquery->data,
7491                                                   username_subquery);
7492
7493                 destroyPQExpBuffer(acl_subquery);
7494                 destroyPQExpBuffer(racl_subquery);
7495                 destroyPQExpBuffer(initacl_subquery);
7496                 destroyPQExpBuffer(initracl_subquery);
7497         }
7498         else if (fout->remoteVersion >= 90000)
7499         {
7500                 /* pg_language has a laninline column */
7501                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7502                                                   "lanname, lanpltrusted, lanplcallfoid, "
7503                                                   "laninline, lanvalidator, lanacl, NULL AS rlanacl, "
7504                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7505                                                   "(%s lanowner) AS lanowner "
7506                                                   "FROM pg_language "
7507                                                   "WHERE lanispl "
7508                                                   "ORDER BY oid",
7509                                                   username_subquery);
7510         }
7511         else if (fout->remoteVersion >= 80300)
7512         {
7513                 /* pg_language has a lanowner column */
7514                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7515                                                   "lanname, lanpltrusted, lanplcallfoid, "
7516                                                   "0 AS laninline, lanvalidator, lanacl, "
7517                                                   "NULL AS rlanacl, "
7518                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7519                                                   "(%s lanowner) AS lanowner "
7520                                                   "FROM pg_language "
7521                                                   "WHERE lanispl "
7522                                                   "ORDER BY oid",
7523                                                   username_subquery);
7524         }
7525         else if (fout->remoteVersion >= 80100)
7526         {
7527                 /* Languages are owned by the bootstrap superuser, OID 10 */
7528                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7529                                                   "lanname, lanpltrusted, lanplcallfoid, "
7530                                                   "0 AS laninline, lanvalidator, lanacl, "
7531                                                   "NULL AS rlanacl, "
7532                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7533                                                   "(%s '10') AS lanowner "
7534                                                   "FROM pg_language "
7535                                                   "WHERE lanispl "
7536                                                   "ORDER BY oid",
7537                                                   username_subquery);
7538         }
7539         else
7540         {
7541                 /* Languages are owned by the bootstrap superuser, sysid 1 */
7542                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7543                                                   "lanname, lanpltrusted, lanplcallfoid, "
7544                                                   "0 AS laninline, lanvalidator, lanacl, "
7545                                                   "NULL AS rlanacl, "
7546                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7547                                                   "(%s '1') AS lanowner "
7548                                                   "FROM pg_language "
7549                                                   "WHERE lanispl "
7550                                                   "ORDER BY oid",
7551                                                   username_subquery);
7552         }
7553
7554         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7555
7556         ntups = PQntuples(res);
7557
7558         *numProcLangs = ntups;
7559
7560         planginfo = (ProcLangInfo *) pg_malloc(ntups * sizeof(ProcLangInfo));
7561
7562         i_tableoid = PQfnumber(res, "tableoid");
7563         i_oid = PQfnumber(res, "oid");
7564         i_lanname = PQfnumber(res, "lanname");
7565         i_lanpltrusted = PQfnumber(res, "lanpltrusted");
7566         i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
7567         i_laninline = PQfnumber(res, "laninline");
7568         i_lanvalidator = PQfnumber(res, "lanvalidator");
7569         i_lanacl = PQfnumber(res, "lanacl");
7570         i_rlanacl = PQfnumber(res, "rlanacl");
7571         i_initlanacl = PQfnumber(res, "initlanacl");
7572         i_initrlanacl = PQfnumber(res, "initrlanacl");
7573         i_lanowner = PQfnumber(res, "lanowner");
7574
7575         for (i = 0; i < ntups; i++)
7576         {
7577                 planginfo[i].dobj.objType = DO_PROCLANG;
7578                 planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7579                 planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7580                 AssignDumpId(&planginfo[i].dobj);
7581
7582                 planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
7583                 planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
7584                 planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
7585                 planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
7586                 planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
7587                 planginfo[i].lanacl = pg_strdup(PQgetvalue(res, i, i_lanacl));
7588                 planginfo[i].rlanacl = pg_strdup(PQgetvalue(res, i, i_rlanacl));
7589                 planginfo[i].initlanacl = pg_strdup(PQgetvalue(res, i, i_initlanacl));
7590                 planginfo[i].initrlanacl = pg_strdup(PQgetvalue(res, i, i_initrlanacl));
7591                 planginfo[i].lanowner = pg_strdup(PQgetvalue(res, i, i_lanowner));
7592
7593                 /* Decide whether we want to dump it */
7594                 selectDumpableProcLang(&(planginfo[i]), fout);
7595
7596                 /* Do not try to dump ACL if no ACL exists. */
7597                 if (PQgetisnull(res, i, i_lanacl) && PQgetisnull(res, i, i_rlanacl) &&
7598                         PQgetisnull(res, i, i_initlanacl) &&
7599                         PQgetisnull(res, i, i_initrlanacl))
7600                         planginfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
7601         }
7602
7603         PQclear(res);
7604
7605         destroyPQExpBuffer(query);
7606
7607         return planginfo;
7608 }
7609
7610 /*
7611  * getCasts
7612  *        get basic information about every cast in the system
7613  *
7614  * numCasts is set to the number of casts read in
7615  */
7616 CastInfo *
7617 getCasts(Archive *fout, int *numCasts)
7618 {
7619         PGresult   *res;
7620         int                     ntups;
7621         int                     i;
7622         PQExpBuffer query = createPQExpBuffer();
7623         CastInfo   *castinfo;
7624         int                     i_tableoid;
7625         int                     i_oid;
7626         int                     i_castsource;
7627         int                     i_casttarget;
7628         int                     i_castfunc;
7629         int                     i_castcontext;
7630         int                     i_castmethod;
7631
7632         /* Make sure we are in proper schema */
7633         selectSourceSchema(fout, "pg_catalog");
7634
7635         if (fout->remoteVersion >= 80400)
7636         {
7637                 appendPQExpBufferStr(query, "SELECT tableoid, oid, "
7638                                                          "castsource, casttarget, castfunc, castcontext, "
7639                                                          "castmethod "
7640                                                          "FROM pg_cast ORDER BY 3,4");
7641         }
7642         else
7643         {
7644                 appendPQExpBufferStr(query, "SELECT tableoid, oid, "
7645                                                          "castsource, casttarget, castfunc, castcontext, "
7646                                                          "CASE WHEN castfunc = 0 THEN 'b' ELSE 'f' END AS castmethod "
7647                                                          "FROM pg_cast ORDER BY 3,4");
7648         }
7649
7650         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7651
7652         ntups = PQntuples(res);
7653
7654         *numCasts = ntups;
7655
7656         castinfo = (CastInfo *) pg_malloc(ntups * sizeof(CastInfo));
7657
7658         i_tableoid = PQfnumber(res, "tableoid");
7659         i_oid = PQfnumber(res, "oid");
7660         i_castsource = PQfnumber(res, "castsource");
7661         i_casttarget = PQfnumber(res, "casttarget");
7662         i_castfunc = PQfnumber(res, "castfunc");
7663         i_castcontext = PQfnumber(res, "castcontext");
7664         i_castmethod = PQfnumber(res, "castmethod");
7665
7666         for (i = 0; i < ntups; i++)
7667         {
7668                 PQExpBufferData namebuf;
7669                 TypeInfo   *sTypeInfo;
7670                 TypeInfo   *tTypeInfo;
7671
7672                 castinfo[i].dobj.objType = DO_CAST;
7673                 castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7674                 castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7675                 AssignDumpId(&castinfo[i].dobj);
7676                 castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
7677                 castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
7678                 castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
7679                 castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
7680                 castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod));
7681
7682                 /*
7683                  * Try to name cast as concatenation of typnames.  This is only used
7684                  * for purposes of sorting.  If we fail to find either type, the name
7685                  * will be an empty string.
7686                  */
7687                 initPQExpBuffer(&namebuf);
7688                 sTypeInfo = findTypeByOid(castinfo[i].castsource);
7689                 tTypeInfo = findTypeByOid(castinfo[i].casttarget);
7690                 if (sTypeInfo && tTypeInfo)
7691                         appendPQExpBuffer(&namebuf, "%s %s",
7692                                                           sTypeInfo->dobj.name, tTypeInfo->dobj.name);
7693                 castinfo[i].dobj.name = namebuf.data;
7694
7695                 /* Decide whether we want to dump it */
7696                 selectDumpableCast(&(castinfo[i]), fout);
7697
7698                 /* Casts do not currently have ACLs. */
7699                 castinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
7700         }
7701
7702         PQclear(res);
7703
7704         destroyPQExpBuffer(query);
7705
7706         return castinfo;
7707 }
7708
7709 static char *
7710 get_language_name(Archive *fout, Oid langid)
7711 {
7712         PQExpBuffer query;
7713         PGresult   *res;
7714         char       *lanname;
7715
7716         query = createPQExpBuffer();
7717         appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid);
7718         res = ExecuteSqlQueryForSingleRow(fout, query->data);
7719         lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
7720         destroyPQExpBuffer(query);
7721         PQclear(res);
7722
7723         return lanname;
7724 }
7725
7726 /*
7727  * getTransforms
7728  *        get basic information about every transform in the system
7729  *
7730  * numTransforms is set to the number of transforms read in
7731  */
7732 TransformInfo *
7733 getTransforms(Archive *fout, int *numTransforms)
7734 {
7735         PGresult   *res;
7736         int                     ntups;
7737         int                     i;
7738         PQExpBuffer query;
7739         TransformInfo *transforminfo;
7740         int                     i_tableoid;
7741         int                     i_oid;
7742         int                     i_trftype;
7743         int                     i_trflang;
7744         int                     i_trffromsql;
7745         int                     i_trftosql;
7746
7747         /* Transforms didn't exist pre-9.5 */
7748         if (fout->remoteVersion < 90500)
7749         {
7750                 *numTransforms = 0;
7751                 return NULL;
7752         }
7753
7754         query = createPQExpBuffer();
7755
7756         /* Make sure we are in proper schema */
7757         selectSourceSchema(fout, "pg_catalog");
7758
7759         appendPQExpBuffer(query, "SELECT tableoid, oid, "
7760                                           "trftype, trflang, trffromsql::oid, trftosql::oid "
7761                                           "FROM pg_transform "
7762                                           "ORDER BY 3,4");
7763
7764         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7765
7766         ntups = PQntuples(res);
7767
7768         *numTransforms = ntups;
7769
7770         transforminfo = (TransformInfo *) pg_malloc(ntups * sizeof(TransformInfo));
7771
7772         i_tableoid = PQfnumber(res, "tableoid");
7773         i_oid = PQfnumber(res, "oid");
7774         i_trftype = PQfnumber(res, "trftype");
7775         i_trflang = PQfnumber(res, "trflang");
7776         i_trffromsql = PQfnumber(res, "trffromsql");
7777         i_trftosql = PQfnumber(res, "trftosql");
7778
7779         for (i = 0; i < ntups; i++)
7780         {
7781                 PQExpBufferData namebuf;
7782                 TypeInfo   *typeInfo;
7783                 char       *lanname;
7784
7785                 transforminfo[i].dobj.objType = DO_TRANSFORM;
7786                 transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7787                 transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7788                 AssignDumpId(&transforminfo[i].dobj);
7789                 transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype));
7790                 transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang));
7791                 transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql));
7792                 transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql));
7793
7794                 /*
7795                  * Try to name transform as concatenation of type and language name.
7796                  * This is only used for purposes of sorting.  If we fail to find
7797                  * either, the name will be an empty string.
7798                  */
7799                 initPQExpBuffer(&namebuf);
7800                 typeInfo = findTypeByOid(transforminfo[i].trftype);
7801                 lanname = get_language_name(fout, transforminfo[i].trflang);
7802                 if (typeInfo && lanname)
7803                         appendPQExpBuffer(&namebuf, "%s %s",
7804                                                           typeInfo->dobj.name, lanname);
7805                 transforminfo[i].dobj.name = namebuf.data;
7806                 free(lanname);
7807
7808                 /* Decide whether we want to dump it */
7809                 selectDumpableObject(&(transforminfo[i].dobj), fout);
7810         }
7811
7812         PQclear(res);
7813
7814         destroyPQExpBuffer(query);
7815
7816         return transforminfo;
7817 }
7818
7819 /*
7820  * getTableAttrs -
7821  *        for each interesting table, read info about its attributes
7822  *        (names, types, default values, CHECK constraints, etc)
7823  *
7824  * This is implemented in a very inefficient way right now, looping
7825  * through the tblinfo and doing a join per table to find the attrs and their
7826  * types.  However, because we want type names and so forth to be named
7827  * relative to the schema of each table, we couldn't do it in just one
7828  * query.  (Maybe one query per schema?)
7829  *
7830  *      modifies tblinfo
7831  */
7832 void
7833 getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
7834 {
7835         DumpOptions *dopt = fout->dopt;
7836         int                     i,
7837                                 j;
7838         PQExpBuffer q = createPQExpBuffer();
7839         int                     i_attnum;
7840         int                     i_attname;
7841         int                     i_atttypname;
7842         int                     i_atttypmod;
7843         int                     i_attstattarget;
7844         int                     i_attstorage;
7845         int                     i_typstorage;
7846         int                     i_attnotnull;
7847         int                     i_atthasdef;
7848         int                     i_attidentity;
7849         int                     i_attisdropped;
7850         int                     i_attlen;
7851         int                     i_attalign;
7852         int                     i_attislocal;
7853         int                     i_attoptions;
7854         int                     i_attcollation;
7855         int                     i_attfdwoptions;
7856         PGresult   *res;
7857         int                     ntups;
7858         bool            hasdefaults;
7859
7860         for (i = 0; i < numTables; i++)
7861         {
7862                 TableInfo  *tbinfo = &tblinfo[i];
7863
7864                 /* Don't bother to collect info for sequences */
7865                 if (tbinfo->relkind == RELKIND_SEQUENCE)
7866                         continue;
7867
7868                 /* Don't bother with uninteresting tables, either */
7869                 if (!tbinfo->interesting)
7870                         continue;
7871
7872                 /*
7873                  * Make sure we are in proper schema for this table; this allows
7874                  * correct retrieval of formatted type names and default exprs
7875                  */
7876                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
7877
7878                 /* find all the user attributes and their types */
7879
7880                 /*
7881                  * we must read the attribute names in attribute number order! because
7882                  * we will use the attnum to index into the attnames array later.
7883                  */
7884                 if (g_verbose)
7885                         write_msg(NULL, "finding the columns and types of table \"%s.%s\"\n",
7886                                           tbinfo->dobj.namespace->dobj.name,
7887                                           tbinfo->dobj.name);
7888
7889                 resetPQExpBuffer(q);
7890
7891                 if (fout->remoteVersion >= 100000)
7892                 {
7893                         /*
7894                          * attidentity is new in version 10.
7895                          */
7896                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
7897                                                           "a.attstattarget, a.attstorage, t.typstorage, "
7898                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
7899                                                           "a.attlen, a.attalign, a.attislocal, "
7900                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
7901                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
7902                                                           "CASE WHEN a.attcollation <> t.typcollation "
7903                                                           "THEN a.attcollation ELSE 0 END AS attcollation, "
7904                                                           "a.attidentity, "
7905                                                           "pg_catalog.array_to_string(ARRAY("
7906                                                           "SELECT pg_catalog.quote_ident(option_name) || "
7907                                                           "' ' || pg_catalog.quote_literal(option_value) "
7908                                                           "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
7909                                                           "ORDER BY option_name"
7910                                                           "), E',\n    ') AS attfdwoptions "
7911                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
7912                                                           "ON a.atttypid = t.oid "
7913                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
7914                                                           "AND a.attnum > 0::pg_catalog.int2 "
7915                                                           "ORDER BY a.attnum",
7916                                                           tbinfo->dobj.catId.oid);
7917                 }
7918                 else if (fout->remoteVersion >= 90200)
7919                 {
7920                         /*
7921                          * attfdwoptions is new in 9.2.
7922                          */
7923                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
7924                                                           "a.attstattarget, a.attstorage, t.typstorage, "
7925                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
7926                                                           "a.attlen, a.attalign, a.attislocal, "
7927                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
7928                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
7929                                                           "CASE WHEN a.attcollation <> t.typcollation "
7930                                                           "THEN a.attcollation ELSE 0 END AS attcollation, "
7931                                                           "pg_catalog.array_to_string(ARRAY("
7932                                                           "SELECT pg_catalog.quote_ident(option_name) || "
7933                                                           "' ' || pg_catalog.quote_literal(option_value) "
7934                                                           "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
7935                                                           "ORDER BY option_name"
7936                                                           "), E',\n    ') AS attfdwoptions "
7937                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
7938                                                           "ON a.atttypid = t.oid "
7939                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
7940                                                           "AND a.attnum > 0::pg_catalog.int2 "
7941                                                           "ORDER BY a.attnum",
7942                                                           tbinfo->dobj.catId.oid);
7943                 }
7944                 else if (fout->remoteVersion >= 90100)
7945                 {
7946                         /*
7947                          * attcollation is new in 9.1.  Since we only want to dump COLLATE
7948                          * clauses for attributes whose collation is different from their
7949                          * type's default, we use a CASE here to suppress uninteresting
7950                          * attcollations cheaply.
7951                          */
7952                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
7953                                                           "a.attstattarget, a.attstorage, t.typstorage, "
7954                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
7955                                                           "a.attlen, a.attalign, a.attislocal, "
7956                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
7957                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
7958                                                           "CASE WHEN a.attcollation <> t.typcollation "
7959                                                           "THEN a.attcollation ELSE 0 END AS attcollation, "
7960                                                           "NULL AS attfdwoptions "
7961                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
7962                                                           "ON a.atttypid = t.oid "
7963                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
7964                                                           "AND a.attnum > 0::pg_catalog.int2 "
7965                                                           "ORDER BY a.attnum",
7966                                                           tbinfo->dobj.catId.oid);
7967                 }
7968                 else if (fout->remoteVersion >= 90000)
7969                 {
7970                         /* attoptions is new in 9.0 */
7971                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
7972                                                           "a.attstattarget, a.attstorage, t.typstorage, "
7973                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
7974                                                           "a.attlen, a.attalign, a.attislocal, "
7975                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
7976                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
7977                                                           "0 AS attcollation, "
7978                                                           "NULL AS attfdwoptions "
7979                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
7980                                                           "ON a.atttypid = t.oid "
7981                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
7982                                                           "AND a.attnum > 0::pg_catalog.int2 "
7983                                                           "ORDER BY a.attnum",
7984                                                           tbinfo->dobj.catId.oid);
7985                 }
7986                 else
7987                 {
7988                         /* need left join here to not fail on dropped columns ... */
7989                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
7990                                                           "a.attstattarget, a.attstorage, t.typstorage, "
7991                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
7992                                                           "a.attlen, a.attalign, a.attislocal, "
7993                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
7994                                                           "'' AS attoptions, 0 AS attcollation, "
7995                                                           "NULL AS attfdwoptions "
7996                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
7997                                                           "ON a.atttypid = t.oid "
7998                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
7999                                                           "AND a.attnum > 0::pg_catalog.int2 "
8000                                                           "ORDER BY a.attnum",
8001                                                           tbinfo->dobj.catId.oid);
8002                 }
8003
8004                 res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
8005
8006                 ntups = PQntuples(res);
8007
8008                 i_attnum = PQfnumber(res, "attnum");
8009                 i_attname = PQfnumber(res, "attname");
8010                 i_atttypname = PQfnumber(res, "atttypname");
8011                 i_atttypmod = PQfnumber(res, "atttypmod");
8012                 i_attstattarget = PQfnumber(res, "attstattarget");
8013                 i_attstorage = PQfnumber(res, "attstorage");
8014                 i_typstorage = PQfnumber(res, "typstorage");
8015                 i_attnotnull = PQfnumber(res, "attnotnull");
8016                 i_atthasdef = PQfnumber(res, "atthasdef");
8017                 i_attidentity = PQfnumber(res, "attidentity");
8018                 i_attisdropped = PQfnumber(res, "attisdropped");
8019                 i_attlen = PQfnumber(res, "attlen");
8020                 i_attalign = PQfnumber(res, "attalign");
8021                 i_attislocal = PQfnumber(res, "attislocal");
8022                 i_attoptions = PQfnumber(res, "attoptions");
8023                 i_attcollation = PQfnumber(res, "attcollation");
8024                 i_attfdwoptions = PQfnumber(res, "attfdwoptions");
8025
8026                 tbinfo->numatts = ntups;
8027                 tbinfo->attnames = (char **) pg_malloc(ntups * sizeof(char *));
8028                 tbinfo->atttypnames = (char **) pg_malloc(ntups * sizeof(char *));
8029                 tbinfo->atttypmod = (int *) pg_malloc(ntups * sizeof(int));
8030                 tbinfo->attstattarget = (int *) pg_malloc(ntups * sizeof(int));
8031                 tbinfo->attstorage = (char *) pg_malloc(ntups * sizeof(char));
8032                 tbinfo->typstorage = (char *) pg_malloc(ntups * sizeof(char));
8033                 tbinfo->attidentity = (char *) pg_malloc(ntups * sizeof(char));
8034                 tbinfo->attisdropped = (bool *) pg_malloc(ntups * sizeof(bool));
8035                 tbinfo->attlen = (int *) pg_malloc(ntups * sizeof(int));
8036                 tbinfo->attalign = (char *) pg_malloc(ntups * sizeof(char));
8037                 tbinfo->attislocal = (bool *) pg_malloc(ntups * sizeof(bool));
8038                 tbinfo->attoptions = (char **) pg_malloc(ntups * sizeof(char *));
8039                 tbinfo->attcollation = (Oid *) pg_malloc(ntups * sizeof(Oid));
8040                 tbinfo->attfdwoptions = (char **) pg_malloc(ntups * sizeof(char *));
8041                 tbinfo->notnull = (bool *) pg_malloc(ntups * sizeof(bool));
8042                 tbinfo->inhNotNull = (bool *) pg_malloc(ntups * sizeof(bool));
8043                 tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
8044                 hasdefaults = false;
8045
8046                 for (j = 0; j < ntups; j++)
8047                 {
8048                         if (j + 1 != atoi(PQgetvalue(res, j, i_attnum)))
8049                                 exit_horribly(NULL,
8050                                                           "invalid column numbering in table \"%s\"\n",
8051                                                           tbinfo->dobj.name);
8052                         tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, i_attname));
8053                         tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, i_atttypname));
8054                         tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
8055                         tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
8056                         tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
8057                         tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
8058                         tbinfo->attidentity[j] = (i_attidentity >= 0 ? *(PQgetvalue(res, j, i_attidentity)) : '\0');
8059                         tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
8060                         tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
8061                         tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
8062                         tbinfo->attalign[j] = *(PQgetvalue(res, j, i_attalign));
8063                         tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
8064                         tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
8065                         tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, i_attoptions));
8066                         tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
8067                         tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, i_attfdwoptions));
8068                         tbinfo->attrdefs[j] = NULL; /* fix below */
8069                         if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
8070                                 hasdefaults = true;
8071                         /* these flags will be set in flagInhAttrs() */
8072                         tbinfo->inhNotNull[j] = false;
8073                 }
8074
8075                 PQclear(res);
8076
8077                 /*
8078                  * Get info about column defaults
8079                  */
8080                 if (hasdefaults)
8081                 {
8082                         AttrDefInfo *attrdefs;
8083                         int                     numDefaults;
8084
8085                         if (g_verbose)
8086                                 write_msg(NULL, "finding default expressions of table \"%s.%s\"\n",
8087                                                   tbinfo->dobj.namespace->dobj.name,
8088                                                   tbinfo->dobj.name);
8089
8090                         printfPQExpBuffer(q, "SELECT tableoid, oid, adnum, "
8091                                                           "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc "
8092                                                           "FROM pg_catalog.pg_attrdef "
8093                                                           "WHERE adrelid = '%u'::pg_catalog.oid",
8094                                                           tbinfo->dobj.catId.oid);
8095
8096                         res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
8097
8098                         numDefaults = PQntuples(res);
8099                         attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
8100
8101                         for (j = 0; j < numDefaults; j++)
8102                         {
8103                                 int                     adnum;
8104
8105                                 adnum = atoi(PQgetvalue(res, j, 2));
8106
8107                                 if (adnum <= 0 || adnum > ntups)
8108                                         exit_horribly(NULL,
8109                                                                   "invalid adnum value %d for table \"%s\"\n",
8110                                                                   adnum, tbinfo->dobj.name);
8111
8112                                 /*
8113                                  * dropped columns shouldn't have defaults, but just in case,
8114                                  * ignore 'em
8115                                  */
8116                                 if (tbinfo->attisdropped[adnum - 1])
8117                                         continue;
8118
8119                                 attrdefs[j].dobj.objType = DO_ATTRDEF;
8120                                 attrdefs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
8121                                 attrdefs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
8122                                 AssignDumpId(&attrdefs[j].dobj);
8123                                 attrdefs[j].adtable = tbinfo;
8124                                 attrdefs[j].adnum = adnum;
8125                                 attrdefs[j].adef_expr = pg_strdup(PQgetvalue(res, j, 3));
8126
8127                                 attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name);
8128                                 attrdefs[j].dobj.namespace = tbinfo->dobj.namespace;
8129
8130                                 attrdefs[j].dobj.dump = tbinfo->dobj.dump;
8131
8132                                 /*
8133                                  * Defaults on a VIEW must always be dumped as separate ALTER
8134                                  * TABLE commands.  Defaults on regular tables are dumped as
8135                                  * part of the CREATE TABLE if possible, which it won't be if
8136                                  * the column is not going to be emitted explicitly.
8137                                  */
8138                                 if (tbinfo->relkind == RELKIND_VIEW)
8139                                 {
8140                                         attrdefs[j].separate = true;
8141                                 }
8142                                 else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1))
8143                                 {
8144                                         /* column will be suppressed, print default separately */
8145                                         attrdefs[j].separate = true;
8146                                 }
8147                                 else
8148                                 {
8149                                         attrdefs[j].separate = false;
8150
8151                                         /*
8152                                          * Mark the default as needing to appear before the table,
8153                                          * so that any dependencies it has must be emitted before
8154                                          * the CREATE TABLE.  If this is not possible, we'll
8155                                          * change to "separate" mode while sorting dependencies.
8156                                          */
8157                                         addObjectDependency(&tbinfo->dobj,
8158                                                                                 attrdefs[j].dobj.dumpId);
8159                                 }
8160
8161                                 tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
8162                         }
8163                         PQclear(res);
8164                 }
8165
8166                 /*
8167                  * Get info about table CHECK constraints
8168                  */
8169                 if (tbinfo->ncheck > 0)
8170                 {
8171                         ConstraintInfo *constrs;
8172                         int                     numConstrs;
8173
8174                         if (g_verbose)
8175                                 write_msg(NULL, "finding check constraints for table \"%s.%s\"\n",
8176                                                   tbinfo->dobj.namespace->dobj.name,
8177                                                   tbinfo->dobj.name);
8178
8179                         resetPQExpBuffer(q);
8180                         if (fout->remoteVersion >= 90200)
8181                         {
8182                                 /*
8183                                  * convalidated is new in 9.2 (actually, it is there in 9.1,
8184                                  * but it wasn't ever false for check constraints until 9.2).
8185                                  */
8186                                 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
8187                                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8188                                                                   "conislocal, convalidated "
8189                                                                   "FROM pg_catalog.pg_constraint "
8190                                                                   "WHERE conrelid = '%u'::pg_catalog.oid "
8191                                                                   "   AND contype = 'c' "
8192                                                                   "ORDER BY conname",
8193                                                                   tbinfo->dobj.catId.oid);
8194                         }
8195                         else if (fout->remoteVersion >= 80400)
8196                         {
8197                                 /* conislocal is new in 8.4 */
8198                                 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
8199                                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8200                                                                   "conislocal, true AS convalidated "
8201                                                                   "FROM pg_catalog.pg_constraint "
8202                                                                   "WHERE conrelid = '%u'::pg_catalog.oid "
8203                                                                   "   AND contype = 'c' "
8204                                                                   "ORDER BY conname",
8205                                                                   tbinfo->dobj.catId.oid);
8206                         }
8207                         else
8208                         {
8209                                 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
8210                                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8211                                                                   "true AS conislocal, true AS convalidated "
8212                                                                   "FROM pg_catalog.pg_constraint "
8213                                                                   "WHERE conrelid = '%u'::pg_catalog.oid "
8214                                                                   "   AND contype = 'c' "
8215                                                                   "ORDER BY conname",
8216                                                                   tbinfo->dobj.catId.oid);
8217                         }
8218
8219                         res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
8220
8221                         numConstrs = PQntuples(res);
8222                         if (numConstrs != tbinfo->ncheck)
8223                         {
8224                                 write_msg(NULL, ngettext("expected %d check constraint on table \"%s\" but found %d\n",
8225                                                                                  "expected %d check constraints on table \"%s\" but found %d\n",
8226                                                                                  tbinfo->ncheck),
8227                                                   tbinfo->ncheck, tbinfo->dobj.name, numConstrs);
8228                                 write_msg(NULL, "(The system catalogs might be corrupted.)\n");
8229                                 exit_nicely(1);
8230                         }
8231
8232                         constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo));
8233                         tbinfo->checkexprs = constrs;
8234
8235                         for (j = 0; j < numConstrs; j++)
8236                         {
8237                                 bool            validated = PQgetvalue(res, j, 5)[0] == 't';
8238
8239                                 constrs[j].dobj.objType = DO_CONSTRAINT;
8240                                 constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
8241                                 constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
8242                                 AssignDumpId(&constrs[j].dobj);
8243                                 constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, 2));
8244                                 constrs[j].dobj.namespace = tbinfo->dobj.namespace;
8245                                 constrs[j].contable = tbinfo;
8246                                 constrs[j].condomain = NULL;
8247                                 constrs[j].contype = 'c';
8248                                 constrs[j].condef = pg_strdup(PQgetvalue(res, j, 3));
8249                                 constrs[j].confrelid = InvalidOid;
8250                                 constrs[j].conindex = 0;
8251                                 constrs[j].condeferrable = false;
8252                                 constrs[j].condeferred = false;
8253                                 constrs[j].conislocal = (PQgetvalue(res, j, 4)[0] == 't');
8254
8255                                 /*
8256                                  * An unvalidated constraint needs to be dumped separately, so
8257                                  * that potentially-violating existing data is loaded before
8258                                  * the constraint.
8259                                  */
8260                                 constrs[j].separate = !validated;
8261
8262                                 constrs[j].dobj.dump = tbinfo->dobj.dump;
8263
8264                                 /*
8265                                  * Mark the constraint as needing to appear before the table
8266                                  * --- this is so that any other dependencies of the
8267                                  * constraint will be emitted before we try to create the
8268                                  * table.  If the constraint is to be dumped separately, it
8269                                  * will be dumped after data is loaded anyway, so don't do it.
8270                                  * (There's an automatic dependency in the opposite direction
8271                                  * anyway, so don't need to add one manually here.)
8272                                  */
8273                                 if (!constrs[j].separate)
8274                                         addObjectDependency(&tbinfo->dobj,
8275                                                                                 constrs[j].dobj.dumpId);
8276
8277                                 /*
8278                                  * If the constraint is inherited, this will be detected later
8279                                  * (in pre-8.4 databases).  We also detect later if the
8280                                  * constraint must be split out from the table definition.
8281                                  */
8282                         }
8283                         PQclear(res);
8284                 }
8285         }
8286
8287         destroyPQExpBuffer(q);
8288 }
8289
8290 /*
8291  * Test whether a column should be printed as part of table's CREATE TABLE.
8292  * Column number is zero-based.
8293  *
8294  * Normally this is always true, but it's false for dropped columns, as well
8295  * as those that were inherited without any local definition.  (If we print
8296  * such a column it will mistakenly get pg_attribute.attislocal set to true.)
8297  * However, in binary_upgrade mode, we must print all such columns anyway and
8298  * fix the attislocal/attisdropped state later, so as to keep control of the
8299  * physical column order.
8300  *
8301  * This function exists because there are scattered nonobvious places that
8302  * must be kept in sync with this decision.
8303  */
8304 bool
8305 shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
8306 {
8307         if (dopt->binary_upgrade)
8308                 return true;
8309         return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
8310 }
8311
8312
8313 /*
8314  * getTSParsers:
8315  *        read all text search parsers in the system catalogs and return them
8316  *        in the TSParserInfo* structure
8317  *
8318  *      numTSParsers is set to the number of parsers read in
8319  */
8320 TSParserInfo *
8321 getTSParsers(Archive *fout, int *numTSParsers)
8322 {
8323         PGresult   *res;
8324         int                     ntups;
8325         int                     i;
8326         PQExpBuffer query;
8327         TSParserInfo *prsinfo;
8328         int                     i_tableoid;
8329         int                     i_oid;
8330         int                     i_prsname;
8331         int                     i_prsnamespace;
8332         int                     i_prsstart;
8333         int                     i_prstoken;
8334         int                     i_prsend;
8335         int                     i_prsheadline;
8336         int                     i_prslextype;
8337
8338         /* Before 8.3, there is no built-in text search support */
8339         if (fout->remoteVersion < 80300)
8340         {
8341                 *numTSParsers = 0;
8342                 return NULL;
8343         }
8344
8345         query = createPQExpBuffer();
8346
8347         /*
8348          * find all text search objects, including builtin ones; we filter out
8349          * system-defined objects at dump-out time.
8350          */
8351
8352         /* Make sure we are in proper schema */
8353         selectSourceSchema(fout, "pg_catalog");
8354
8355         appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, "
8356                                                  "prsstart::oid, prstoken::oid, "
8357                                                  "prsend::oid, prsheadline::oid, prslextype::oid "
8358                                                  "FROM pg_ts_parser");
8359
8360         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8361
8362         ntups = PQntuples(res);
8363         *numTSParsers = ntups;
8364
8365         prsinfo = (TSParserInfo *) pg_malloc(ntups * sizeof(TSParserInfo));
8366
8367         i_tableoid = PQfnumber(res, "tableoid");
8368         i_oid = PQfnumber(res, "oid");
8369         i_prsname = PQfnumber(res, "prsname");
8370         i_prsnamespace = PQfnumber(res, "prsnamespace");
8371         i_prsstart = PQfnumber(res, "prsstart");
8372         i_prstoken = PQfnumber(res, "prstoken");
8373         i_prsend = PQfnumber(res, "prsend");
8374         i_prsheadline = PQfnumber(res, "prsheadline");
8375         i_prslextype = PQfnumber(res, "prslextype");
8376
8377         for (i = 0; i < ntups; i++)
8378         {
8379                 prsinfo[i].dobj.objType = DO_TSPARSER;
8380                 prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8381                 prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8382                 AssignDumpId(&prsinfo[i].dobj);
8383                 prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
8384                 prsinfo[i].dobj.namespace =
8385                         findNamespace(fout,
8386                                                   atooid(PQgetvalue(res, i, i_prsnamespace)));
8387                 prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
8388                 prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
8389                 prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
8390                 prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline));
8391                 prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype));
8392
8393                 /* Decide whether we want to dump it */
8394                 selectDumpableObject(&(prsinfo[i].dobj), fout);
8395
8396                 /* Text Search Parsers do not currently have ACLs. */
8397                 prsinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8398         }
8399
8400         PQclear(res);
8401
8402         destroyPQExpBuffer(query);
8403
8404         return prsinfo;
8405 }
8406
8407 /*
8408  * getTSDictionaries:
8409  *        read all text search dictionaries in the system catalogs and return them
8410  *        in the TSDictInfo* structure
8411  *
8412  *      numTSDicts is set to the number of dictionaries read in
8413  */
8414 TSDictInfo *
8415 getTSDictionaries(Archive *fout, int *numTSDicts)
8416 {
8417         PGresult   *res;
8418         int                     ntups;
8419         int                     i;
8420         PQExpBuffer query;
8421         TSDictInfo *dictinfo;
8422         int                     i_tableoid;
8423         int                     i_oid;
8424         int                     i_dictname;
8425         int                     i_dictnamespace;
8426         int                     i_rolname;
8427         int                     i_dicttemplate;
8428         int                     i_dictinitoption;
8429
8430         /* Before 8.3, there is no built-in text search support */
8431         if (fout->remoteVersion < 80300)
8432         {
8433                 *numTSDicts = 0;
8434                 return NULL;
8435         }
8436
8437         query = createPQExpBuffer();
8438
8439         /* Make sure we are in proper schema */
8440         selectSourceSchema(fout, "pg_catalog");
8441
8442         appendPQExpBuffer(query, "SELECT tableoid, oid, dictname, "
8443                                           "dictnamespace, (%s dictowner) AS rolname, "
8444                                           "dicttemplate, dictinitoption "
8445                                           "FROM pg_ts_dict",
8446                                           username_subquery);
8447
8448         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8449
8450         ntups = PQntuples(res);
8451         *numTSDicts = ntups;
8452
8453         dictinfo = (TSDictInfo *) pg_malloc(ntups * sizeof(TSDictInfo));
8454
8455         i_tableoid = PQfnumber(res, "tableoid");
8456         i_oid = PQfnumber(res, "oid");
8457         i_dictname = PQfnumber(res, "dictname");
8458         i_dictnamespace = PQfnumber(res, "dictnamespace");
8459         i_rolname = PQfnumber(res, "rolname");
8460         i_dictinitoption = PQfnumber(res, "dictinitoption");
8461         i_dicttemplate = PQfnumber(res, "dicttemplate");
8462
8463         for (i = 0; i < ntups; i++)
8464         {
8465                 dictinfo[i].dobj.objType = DO_TSDICT;
8466                 dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8467                 dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8468                 AssignDumpId(&dictinfo[i].dobj);
8469                 dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
8470                 dictinfo[i].dobj.namespace =
8471                         findNamespace(fout,
8472                                                   atooid(PQgetvalue(res, i, i_dictnamespace)));
8473                 dictinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
8474                 dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
8475                 if (PQgetisnull(res, i, i_dictinitoption))
8476                         dictinfo[i].dictinitoption = NULL;
8477                 else
8478                         dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption));
8479
8480                 /* Decide whether we want to dump it */
8481                 selectDumpableObject(&(dictinfo[i].dobj), fout);
8482
8483                 /* Text Search Dictionaries do not currently have ACLs. */
8484                 dictinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8485         }
8486
8487         PQclear(res);
8488
8489         destroyPQExpBuffer(query);
8490
8491         return dictinfo;
8492 }
8493
8494 /*
8495  * getTSTemplates:
8496  *        read all text search templates in the system catalogs and return them
8497  *        in the TSTemplateInfo* structure
8498  *
8499  *      numTSTemplates is set to the number of templates read in
8500  */
8501 TSTemplateInfo *
8502 getTSTemplates(Archive *fout, int *numTSTemplates)
8503 {
8504         PGresult   *res;
8505         int                     ntups;
8506         int                     i;
8507         PQExpBuffer query;
8508         TSTemplateInfo *tmplinfo;
8509         int                     i_tableoid;
8510         int                     i_oid;
8511         int                     i_tmplname;
8512         int                     i_tmplnamespace;
8513         int                     i_tmplinit;
8514         int                     i_tmpllexize;
8515
8516         /* Before 8.3, there is no built-in text search support */
8517         if (fout->remoteVersion < 80300)
8518         {
8519                 *numTSTemplates = 0;
8520                 return NULL;
8521         }
8522
8523         query = createPQExpBuffer();
8524
8525         /* Make sure we are in proper schema */
8526         selectSourceSchema(fout, "pg_catalog");
8527
8528         appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, "
8529                                                  "tmplnamespace, tmplinit::oid, tmpllexize::oid "
8530                                                  "FROM pg_ts_template");
8531
8532         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8533
8534         ntups = PQntuples(res);
8535         *numTSTemplates = ntups;
8536
8537         tmplinfo = (TSTemplateInfo *) pg_malloc(ntups * sizeof(TSTemplateInfo));
8538
8539         i_tableoid = PQfnumber(res, "tableoid");
8540         i_oid = PQfnumber(res, "oid");
8541         i_tmplname = PQfnumber(res, "tmplname");
8542         i_tmplnamespace = PQfnumber(res, "tmplnamespace");
8543         i_tmplinit = PQfnumber(res, "tmplinit");
8544         i_tmpllexize = PQfnumber(res, "tmpllexize");
8545
8546         for (i = 0; i < ntups; i++)
8547         {
8548                 tmplinfo[i].dobj.objType = DO_TSTEMPLATE;
8549                 tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8550                 tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8551                 AssignDumpId(&tmplinfo[i].dobj);
8552                 tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
8553                 tmplinfo[i].dobj.namespace =
8554                         findNamespace(fout,
8555                                                   atooid(PQgetvalue(res, i, i_tmplnamespace)));
8556                 tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
8557                 tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
8558
8559                 /* Decide whether we want to dump it */
8560                 selectDumpableObject(&(tmplinfo[i].dobj), fout);
8561
8562                 /* Text Search Templates do not currently have ACLs. */
8563                 tmplinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8564         }
8565
8566         PQclear(res);
8567
8568         destroyPQExpBuffer(query);
8569
8570         return tmplinfo;
8571 }
8572
8573 /*
8574  * getTSConfigurations:
8575  *        read all text search configurations in the system catalogs and return
8576  *        them in the TSConfigInfo* structure
8577  *
8578  *      numTSConfigs is set to the number of configurations read in
8579  */
8580 TSConfigInfo *
8581 getTSConfigurations(Archive *fout, int *numTSConfigs)
8582 {
8583         PGresult   *res;
8584         int                     ntups;
8585         int                     i;
8586         PQExpBuffer query;
8587         TSConfigInfo *cfginfo;
8588         int                     i_tableoid;
8589         int                     i_oid;
8590         int                     i_cfgname;
8591         int                     i_cfgnamespace;
8592         int                     i_rolname;
8593         int                     i_cfgparser;
8594
8595         /* Before 8.3, there is no built-in text search support */
8596         if (fout->remoteVersion < 80300)
8597         {
8598                 *numTSConfigs = 0;
8599                 return NULL;
8600         }
8601
8602         query = createPQExpBuffer();
8603
8604         /* Make sure we are in proper schema */
8605         selectSourceSchema(fout, "pg_catalog");
8606
8607         appendPQExpBuffer(query, "SELECT tableoid, oid, cfgname, "
8608                                           "cfgnamespace, (%s cfgowner) AS rolname, cfgparser "
8609                                           "FROM pg_ts_config",
8610                                           username_subquery);
8611
8612         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8613
8614         ntups = PQntuples(res);
8615         *numTSConfigs = ntups;
8616
8617         cfginfo = (TSConfigInfo *) pg_malloc(ntups * sizeof(TSConfigInfo));
8618
8619         i_tableoid = PQfnumber(res, "tableoid");
8620         i_oid = PQfnumber(res, "oid");
8621         i_cfgname = PQfnumber(res, "cfgname");
8622         i_cfgnamespace = PQfnumber(res, "cfgnamespace");
8623         i_rolname = PQfnumber(res, "rolname");
8624         i_cfgparser = PQfnumber(res, "cfgparser");
8625
8626         for (i = 0; i < ntups; i++)
8627         {
8628                 cfginfo[i].dobj.objType = DO_TSCONFIG;
8629                 cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8630                 cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8631                 AssignDumpId(&cfginfo[i].dobj);
8632                 cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
8633                 cfginfo[i].dobj.namespace =
8634                         findNamespace(fout,
8635                                                   atooid(PQgetvalue(res, i, i_cfgnamespace)));
8636                 cfginfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
8637                 cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
8638
8639                 /* Decide whether we want to dump it */
8640                 selectDumpableObject(&(cfginfo[i].dobj), fout);
8641
8642                 /* Text Search Configurations do not currently have ACLs. */
8643                 cfginfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8644         }
8645
8646         PQclear(res);
8647
8648         destroyPQExpBuffer(query);
8649
8650         return cfginfo;
8651 }
8652
8653 /*
8654  * getForeignDataWrappers:
8655  *        read all foreign-data wrappers in the system catalogs and return
8656  *        them in the FdwInfo* structure
8657  *
8658  *      numForeignDataWrappers is set to the number of fdws read in
8659  */
8660 FdwInfo *
8661 getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
8662 {
8663         DumpOptions *dopt = fout->dopt;
8664         PGresult   *res;
8665         int                     ntups;
8666         int                     i;
8667         PQExpBuffer query;
8668         FdwInfo    *fdwinfo;
8669         int                     i_tableoid;
8670         int                     i_oid;
8671         int                     i_fdwname;
8672         int                     i_rolname;
8673         int                     i_fdwhandler;
8674         int                     i_fdwvalidator;
8675         int                     i_fdwacl;
8676         int                     i_rfdwacl;
8677         int                     i_initfdwacl;
8678         int                     i_initrfdwacl;
8679         int                     i_fdwoptions;
8680
8681         /* Before 8.4, there are no foreign-data wrappers */
8682         if (fout->remoteVersion < 80400)
8683         {
8684                 *numForeignDataWrappers = 0;
8685                 return NULL;
8686         }
8687
8688         query = createPQExpBuffer();
8689
8690         /* Make sure we are in proper schema */
8691         selectSourceSchema(fout, "pg_catalog");
8692
8693         if (fout->remoteVersion >= 90600)
8694         {
8695                 PQExpBuffer acl_subquery = createPQExpBuffer();
8696                 PQExpBuffer racl_subquery = createPQExpBuffer();
8697                 PQExpBuffer initacl_subquery = createPQExpBuffer();
8698                 PQExpBuffer initracl_subquery = createPQExpBuffer();
8699
8700                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
8701                                                 initracl_subquery, "f.fdwacl", "f.fdwowner", "'F'",
8702                                                 dopt->binary_upgrade);
8703
8704                 appendPQExpBuffer(query, "SELECT f.tableoid, f.oid, f.fdwname, "
8705                                                   "(%s f.fdwowner) AS rolname, "
8706                                                   "f.fdwhandler::pg_catalog.regproc, "
8707                                                   "f.fdwvalidator::pg_catalog.regproc, "
8708                                                   "%s AS fdwacl, "
8709                                                   "%s AS rfdwacl, "
8710                                                   "%s AS initfdwacl, "
8711                                                   "%s AS initrfdwacl, "
8712                                                   "array_to_string(ARRAY("
8713                                                   "SELECT quote_ident(option_name) || ' ' || "
8714                                                   "quote_literal(option_value) "
8715                                                   "FROM pg_options_to_table(f.fdwoptions) "
8716                                                   "ORDER BY option_name"
8717                                                   "), E',\n    ') AS fdwoptions "
8718                                                   "FROM pg_foreign_data_wrapper f "
8719                                                   "LEFT JOIN pg_init_privs pip ON "
8720                                                   "(f.oid = pip.objoid "
8721                                                   "AND pip.classoid = 'pg_foreign_data_wrapper'::regclass "
8722                                                   "AND pip.objsubid = 0) ",
8723                                                   username_subquery,
8724                                                   acl_subquery->data,
8725                                                   racl_subquery->data,
8726                                                   initacl_subquery->data,
8727                                                   initracl_subquery->data);
8728
8729                 destroyPQExpBuffer(acl_subquery);
8730                 destroyPQExpBuffer(racl_subquery);
8731                 destroyPQExpBuffer(initacl_subquery);
8732                 destroyPQExpBuffer(initracl_subquery);
8733         }
8734         else if (fout->remoteVersion >= 90100)
8735         {
8736                 appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
8737                                                   "(%s fdwowner) AS rolname, "
8738                                                   "fdwhandler::pg_catalog.regproc, "
8739                                                   "fdwvalidator::pg_catalog.regproc, fdwacl, "
8740                                                   "NULL as rfdwacl, "
8741                                                   "NULL as initfdwacl, NULL AS initrfdwacl, "
8742                                                   "array_to_string(ARRAY("
8743                                                   "SELECT quote_ident(option_name) || ' ' || "
8744                                                   "quote_literal(option_value) "
8745                                                   "FROM pg_options_to_table(fdwoptions) "
8746                                                   "ORDER BY option_name"
8747                                                   "), E',\n    ') AS fdwoptions "
8748                                                   "FROM pg_foreign_data_wrapper",
8749                                                   username_subquery);
8750         }
8751         else
8752         {
8753                 appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
8754                                                   "(%s fdwowner) AS rolname, "
8755                                                   "'-' AS fdwhandler, "
8756                                                   "fdwvalidator::pg_catalog.regproc, fdwacl, "
8757                                                   "NULL as rfdwacl, "
8758                                                   "NULL as initfdwacl, NULL AS initrfdwacl, "
8759                                                   "array_to_string(ARRAY("
8760                                                   "SELECT quote_ident(option_name) || ' ' || "
8761                                                   "quote_literal(option_value) "
8762                                                   "FROM pg_options_to_table(fdwoptions) "
8763                                                   "ORDER BY option_name"
8764                                                   "), E',\n    ') AS fdwoptions "
8765                                                   "FROM pg_foreign_data_wrapper",
8766                                                   username_subquery);
8767         }
8768
8769         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8770
8771         ntups = PQntuples(res);
8772         *numForeignDataWrappers = ntups;
8773
8774         fdwinfo = (FdwInfo *) pg_malloc(ntups * sizeof(FdwInfo));
8775
8776         i_tableoid = PQfnumber(res, "tableoid");
8777         i_oid = PQfnumber(res, "oid");
8778         i_fdwname = PQfnumber(res, "fdwname");
8779         i_rolname = PQfnumber(res, "rolname");
8780         i_fdwhandler = PQfnumber(res, "fdwhandler");
8781         i_fdwvalidator = PQfnumber(res, "fdwvalidator");
8782         i_fdwacl = PQfnumber(res, "fdwacl");
8783         i_rfdwacl = PQfnumber(res, "rfdwacl");
8784         i_initfdwacl = PQfnumber(res, "initfdwacl");
8785         i_initrfdwacl = PQfnumber(res, "initrfdwacl");
8786         i_fdwoptions = PQfnumber(res, "fdwoptions");
8787
8788         for (i = 0; i < ntups; i++)
8789         {
8790                 fdwinfo[i].dobj.objType = DO_FDW;
8791                 fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8792                 fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8793                 AssignDumpId(&fdwinfo[i].dobj);
8794                 fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname));
8795                 fdwinfo[i].dobj.namespace = NULL;
8796                 fdwinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
8797                 fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler));
8798                 fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator));
8799                 fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions));
8800                 fdwinfo[i].fdwacl = pg_strdup(PQgetvalue(res, i, i_fdwacl));
8801                 fdwinfo[i].rfdwacl = pg_strdup(PQgetvalue(res, i, i_rfdwacl));
8802                 fdwinfo[i].initfdwacl = pg_strdup(PQgetvalue(res, i, i_initfdwacl));
8803                 fdwinfo[i].initrfdwacl = pg_strdup(PQgetvalue(res, i, i_initrfdwacl));
8804
8805                 /* Decide whether we want to dump it */
8806                 selectDumpableObject(&(fdwinfo[i].dobj), fout);
8807
8808                 /* Do not try to dump ACL if no ACL exists. */
8809                 if (PQgetisnull(res, i, i_fdwacl) && PQgetisnull(res, i, i_rfdwacl) &&
8810                         PQgetisnull(res, i, i_initfdwacl) &&
8811                         PQgetisnull(res, i, i_initrfdwacl))
8812                         fdwinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8813         }
8814
8815         PQclear(res);
8816
8817         destroyPQExpBuffer(query);
8818
8819         return fdwinfo;
8820 }
8821
8822 /*
8823  * getForeignServers:
8824  *        read all foreign servers in the system catalogs and return
8825  *        them in the ForeignServerInfo * structure
8826  *
8827  *      numForeignServers is set to the number of servers read in
8828  */
8829 ForeignServerInfo *
8830 getForeignServers(Archive *fout, int *numForeignServers)
8831 {
8832         DumpOptions *dopt = fout->dopt;
8833         PGresult   *res;
8834         int                     ntups;
8835         int                     i;
8836         PQExpBuffer query;
8837         ForeignServerInfo *srvinfo;
8838         int                     i_tableoid;
8839         int                     i_oid;
8840         int                     i_srvname;
8841         int                     i_rolname;
8842         int                     i_srvfdw;
8843         int                     i_srvtype;
8844         int                     i_srvversion;
8845         int                     i_srvacl;
8846         int                     i_rsrvacl;
8847         int                     i_initsrvacl;
8848         int                     i_initrsrvacl;
8849         int                     i_srvoptions;
8850
8851         /* Before 8.4, there are no foreign servers */
8852         if (fout->remoteVersion < 80400)
8853         {
8854                 *numForeignServers = 0;
8855                 return NULL;
8856         }
8857
8858         query = createPQExpBuffer();
8859
8860         /* Make sure we are in proper schema */
8861         selectSourceSchema(fout, "pg_catalog");
8862
8863         if (fout->remoteVersion >= 90600)
8864         {
8865                 PQExpBuffer acl_subquery = createPQExpBuffer();
8866                 PQExpBuffer racl_subquery = createPQExpBuffer();
8867                 PQExpBuffer initacl_subquery = createPQExpBuffer();
8868                 PQExpBuffer initracl_subquery = createPQExpBuffer();
8869
8870                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
8871                                                 initracl_subquery, "f.srvacl", "f.srvowner", "'S'",
8872                                                 dopt->binary_upgrade);
8873
8874                 appendPQExpBuffer(query, "SELECT f.tableoid, f.oid, f.srvname, "
8875                                                   "(%s f.srvowner) AS rolname, "
8876                                                   "f.srvfdw, f.srvtype, f.srvversion, "
8877                                                   "%s AS srvacl, "
8878                                                   "%s AS rsrvacl, "
8879                                                   "%s AS initsrvacl, "
8880                                                   "%s AS initrsrvacl, "
8881                                                   "array_to_string(ARRAY("
8882                                                   "SELECT quote_ident(option_name) || ' ' || "
8883                                                   "quote_literal(option_value) "
8884                                                   "FROM pg_options_to_table(f.srvoptions) "
8885                                                   "ORDER BY option_name"
8886                                                   "), E',\n    ') AS srvoptions "
8887                                                   "FROM pg_foreign_server f "
8888                                                   "LEFT JOIN pg_init_privs pip "
8889                                                   "ON (f.oid = pip.objoid "
8890                                                   "AND pip.classoid = 'pg_foreign_server'::regclass "
8891                                                   "AND pip.objsubid = 0) ",
8892                                                   username_subquery,
8893                                                   acl_subquery->data,
8894                                                   racl_subquery->data,
8895                                                   initacl_subquery->data,
8896                                                   initracl_subquery->data);
8897
8898                 destroyPQExpBuffer(acl_subquery);
8899                 destroyPQExpBuffer(racl_subquery);
8900                 destroyPQExpBuffer(initacl_subquery);
8901                 destroyPQExpBuffer(initracl_subquery);
8902         }
8903         else
8904         {
8905                 appendPQExpBuffer(query, "SELECT tableoid, oid, srvname, "
8906                                                   "(%s srvowner) AS rolname, "
8907                                                   "srvfdw, srvtype, srvversion, srvacl, "
8908                                                   "NULL AS rsrvacl, "
8909                                                   "NULL AS initsrvacl, NULL AS initrsrvacl, "
8910                                                   "array_to_string(ARRAY("
8911                                                   "SELECT quote_ident(option_name) || ' ' || "
8912                                                   "quote_literal(option_value) "
8913                                                   "FROM pg_options_to_table(srvoptions) "
8914                                                   "ORDER BY option_name"
8915                                                   "), E',\n    ') AS srvoptions "
8916                                                   "FROM pg_foreign_server",
8917                                                   username_subquery);
8918         }
8919
8920         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8921
8922         ntups = PQntuples(res);
8923         *numForeignServers = ntups;
8924
8925         srvinfo = (ForeignServerInfo *) pg_malloc(ntups * sizeof(ForeignServerInfo));
8926
8927         i_tableoid = PQfnumber(res, "tableoid");
8928         i_oid = PQfnumber(res, "oid");
8929         i_srvname = PQfnumber(res, "srvname");
8930         i_rolname = PQfnumber(res, "rolname");
8931         i_srvfdw = PQfnumber(res, "srvfdw");
8932         i_srvtype = PQfnumber(res, "srvtype");
8933         i_srvversion = PQfnumber(res, "srvversion");
8934         i_srvacl = PQfnumber(res, "srvacl");
8935         i_rsrvacl = PQfnumber(res, "rsrvacl");
8936         i_initsrvacl = PQfnumber(res, "initsrvacl");
8937         i_initrsrvacl = PQfnumber(res, "initrsrvacl");
8938         i_srvoptions = PQfnumber(res, "srvoptions");
8939
8940         for (i = 0; i < ntups; i++)
8941         {
8942                 srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
8943                 srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8944                 srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8945                 AssignDumpId(&srvinfo[i].dobj);
8946                 srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname));
8947                 srvinfo[i].dobj.namespace = NULL;
8948                 srvinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
8949                 srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw));
8950                 srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype));
8951                 srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion));
8952                 srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions));
8953                 srvinfo[i].srvacl = pg_strdup(PQgetvalue(res, i, i_srvacl));
8954                 srvinfo[i].rsrvacl = pg_strdup(PQgetvalue(res, i, i_rsrvacl));
8955                 srvinfo[i].initsrvacl = pg_strdup(PQgetvalue(res, i, i_initsrvacl));
8956                 srvinfo[i].initrsrvacl = pg_strdup(PQgetvalue(res, i, i_initrsrvacl));
8957
8958                 /* Decide whether we want to dump it */
8959                 selectDumpableObject(&(srvinfo[i].dobj), fout);
8960
8961                 /* Do not try to dump ACL if no ACL exists. */
8962                 if (PQgetisnull(res, i, i_srvacl) && PQgetisnull(res, i, i_rsrvacl) &&
8963                         PQgetisnull(res, i, i_initsrvacl) &&
8964                         PQgetisnull(res, i, i_initrsrvacl))
8965                         srvinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8966         }
8967
8968         PQclear(res);
8969
8970         destroyPQExpBuffer(query);
8971
8972         return srvinfo;
8973 }
8974
8975 /*
8976  * getDefaultACLs:
8977  *        read all default ACL information in the system catalogs and return
8978  *        them in the DefaultACLInfo structure
8979  *
8980  *      numDefaultACLs is set to the number of ACLs read in
8981  */
8982 DefaultACLInfo *
8983 getDefaultACLs(Archive *fout, int *numDefaultACLs)
8984 {
8985         DumpOptions *dopt = fout->dopt;
8986         DefaultACLInfo *daclinfo;
8987         PQExpBuffer query;
8988         PGresult   *res;
8989         int                     i_oid;
8990         int                     i_tableoid;
8991         int                     i_defaclrole;
8992         int                     i_defaclnamespace;
8993         int                     i_defaclobjtype;
8994         int                     i_defaclacl;
8995         int                     i_rdefaclacl;
8996         int                     i_initdefaclacl;
8997         int                     i_initrdefaclacl;
8998         int                     i,
8999                                 ntups;
9000
9001         if (fout->remoteVersion < 90000)
9002         {
9003                 *numDefaultACLs = 0;
9004                 return NULL;
9005         }
9006
9007         query = createPQExpBuffer();
9008
9009         /* Make sure we are in proper schema */
9010         selectSourceSchema(fout, "pg_catalog");
9011
9012         if (fout->remoteVersion >= 90600)
9013         {
9014                 PQExpBuffer acl_subquery = createPQExpBuffer();
9015                 PQExpBuffer racl_subquery = createPQExpBuffer();
9016                 PQExpBuffer initacl_subquery = createPQExpBuffer();
9017                 PQExpBuffer initracl_subquery = createPQExpBuffer();
9018
9019                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
9020                                                 initracl_subquery, "defaclacl", "defaclrole",
9021                                                 "CASE WHEN defaclobjtype = 'S' THEN 's' ELSE defaclobjtype END::\"char\"",
9022                                                 dopt->binary_upgrade);
9023
9024                 appendPQExpBuffer(query, "SELECT d.oid, d.tableoid, "
9025                                                   "(%s d.defaclrole) AS defaclrole, "
9026                                                   "d.defaclnamespace, "
9027                                                   "d.defaclobjtype, "
9028                                                   "%s AS defaclacl, "
9029                                                   "%s AS rdefaclacl, "
9030                                                   "%s AS initdefaclacl, "
9031                                                   "%s AS initrdefaclacl "
9032                                                   "FROM pg_default_acl d "
9033                                                   "LEFT JOIN pg_init_privs pip ON "
9034                                                   "(d.oid = pip.objoid "
9035                                                   "AND pip.classoid = 'pg_default_acl'::regclass "
9036                                                   "AND pip.objsubid = 0) ",
9037                                                   username_subquery,
9038                                                   acl_subquery->data,
9039                                                   racl_subquery->data,
9040                                                   initacl_subquery->data,
9041                                                   initracl_subquery->data);
9042         }
9043         else
9044         {
9045                 appendPQExpBuffer(query, "SELECT oid, tableoid, "
9046                                                   "(%s defaclrole) AS defaclrole, "
9047                                                   "defaclnamespace, "
9048                                                   "defaclobjtype, "
9049                                                   "defaclacl, "
9050                                                   "NULL AS rdefaclacl, "
9051                                                   "NULL AS initdefaclacl, "
9052                                                   "NULL AS initrdefaclacl "
9053                                                   "FROM pg_default_acl",
9054                                                   username_subquery);
9055         }
9056
9057         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9058
9059         ntups = PQntuples(res);
9060         *numDefaultACLs = ntups;
9061
9062         daclinfo = (DefaultACLInfo *) pg_malloc(ntups * sizeof(DefaultACLInfo));
9063
9064         i_oid = PQfnumber(res, "oid");
9065         i_tableoid = PQfnumber(res, "tableoid");
9066         i_defaclrole = PQfnumber(res, "defaclrole");
9067         i_defaclnamespace = PQfnumber(res, "defaclnamespace");
9068         i_defaclobjtype = PQfnumber(res, "defaclobjtype");
9069         i_defaclacl = PQfnumber(res, "defaclacl");
9070         i_rdefaclacl = PQfnumber(res, "rdefaclacl");
9071         i_initdefaclacl = PQfnumber(res, "initdefaclacl");
9072         i_initrdefaclacl = PQfnumber(res, "initrdefaclacl");
9073
9074         for (i = 0; i < ntups; i++)
9075         {
9076                 Oid                     nspid = atooid(PQgetvalue(res, i, i_defaclnamespace));
9077
9078                 daclinfo[i].dobj.objType = DO_DEFAULT_ACL;
9079                 daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9080                 daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9081                 AssignDumpId(&daclinfo[i].dobj);
9082                 /* cheesy ... is it worth coming up with a better object name? */
9083                 daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
9084
9085                 if (nspid != InvalidOid)
9086                         daclinfo[i].dobj.namespace = findNamespace(fout, nspid);
9087                 else
9088                         daclinfo[i].dobj.namespace = NULL;
9089
9090                 daclinfo[i].defaclrole = pg_strdup(PQgetvalue(res, i, i_defaclrole));
9091                 daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype));
9092                 daclinfo[i].defaclacl = pg_strdup(PQgetvalue(res, i, i_defaclacl));
9093                 daclinfo[i].rdefaclacl = pg_strdup(PQgetvalue(res, i, i_rdefaclacl));
9094                 daclinfo[i].initdefaclacl = pg_strdup(PQgetvalue(res, i, i_initdefaclacl));
9095                 daclinfo[i].initrdefaclacl = pg_strdup(PQgetvalue(res, i, i_initrdefaclacl));
9096
9097                 /* Decide whether we want to dump it */
9098                 selectDumpableDefaultACL(&(daclinfo[i]), dopt);
9099         }
9100
9101         PQclear(res);
9102
9103         destroyPQExpBuffer(query);
9104
9105         return daclinfo;
9106 }
9107
9108 /*
9109  * dumpComment --
9110  *
9111  * This routine is used to dump any comments associated with the
9112  * object handed to this routine. The routine takes a constant character
9113  * string for the target part of the comment-creation command, plus
9114  * the namespace and owner of the object (for labeling the ArchiveEntry),
9115  * plus catalog ID and subid which are the lookup key for pg_description,
9116  * plus the dump ID for the object (for setting a dependency).
9117  * If a matching pg_description entry is found, it is dumped.
9118  *
9119  * Note: although this routine takes a dumpId for dependency purposes,
9120  * that purpose is just to mark the dependency in the emitted dump file
9121  * for possible future use by pg_restore.  We do NOT use it for determining
9122  * ordering of the comment in the dump file, because this routine is called
9123  * after dependency sorting occurs.  This routine should be called just after
9124  * calling ArchiveEntry() for the specified object.
9125  */
9126 static void
9127 dumpComment(Archive *fout, const char *target,
9128                         const char *namespace, const char *owner,
9129                         CatalogId catalogId, int subid, DumpId dumpId)
9130 {
9131         DumpOptions *dopt = fout->dopt;
9132         CommentItem *comments;
9133         int                     ncomments;
9134
9135         /* Comments are schema not data ... except blob comments are data */
9136         if (strncmp(target, "LARGE OBJECT ", 13) != 0)
9137         {
9138                 if (dopt->dataOnly)
9139                         return;
9140         }
9141         else
9142         {
9143                 /* We do dump blob comments in binary-upgrade mode */
9144                 if (dopt->schemaOnly && !dopt->binary_upgrade)
9145                         return;
9146         }
9147
9148         /* Search for comments associated with catalogId, using table */
9149         ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9150                                                          &comments);
9151
9152         /* Is there one matching the subid? */
9153         while (ncomments > 0)
9154         {
9155                 if (comments->objsubid == subid)
9156                         break;
9157                 comments++;
9158                 ncomments--;
9159         }
9160
9161         /* If a comment exists, build COMMENT ON statement */
9162         if (ncomments > 0)
9163         {
9164                 PQExpBuffer query = createPQExpBuffer();
9165
9166                 appendPQExpBuffer(query, "COMMENT ON %s IS ", target);
9167                 appendStringLiteralAH(query, comments->descr, fout);
9168                 appendPQExpBufferStr(query, ";\n");
9169
9170                 /*
9171                  * We mark comments as SECTION_NONE because they really belong in the
9172                  * same section as their parent, whether that is pre-data or
9173                  * post-data.
9174                  */
9175                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
9176                                          target, namespace, NULL, owner,
9177                                          false, "COMMENT", SECTION_NONE,
9178                                          query->data, "", NULL,
9179                                          &(dumpId), 1,
9180                                          NULL, NULL);
9181
9182                 destroyPQExpBuffer(query);
9183         }
9184 }
9185
9186 /*
9187  * dumpTableComment --
9188  *
9189  * As above, but dump comments for both the specified table (or view)
9190  * and its columns.
9191  */
9192 static void
9193 dumpTableComment(Archive *fout, TableInfo *tbinfo,
9194                                  const char *reltypename)
9195 {
9196         DumpOptions *dopt = fout->dopt;
9197         CommentItem *comments;
9198         int                     ncomments;
9199         PQExpBuffer query;
9200         PQExpBuffer target;
9201
9202         /* Comments are SCHEMA not data */
9203         if (dopt->dataOnly)
9204                 return;
9205
9206         /* Search for comments associated with relation, using table */
9207         ncomments = findComments(fout,
9208                                                          tbinfo->dobj.catId.tableoid,
9209                                                          tbinfo->dobj.catId.oid,
9210                                                          &comments);
9211
9212         /* If comments exist, build COMMENT ON statements */
9213         if (ncomments <= 0)
9214                 return;
9215
9216         query = createPQExpBuffer();
9217         target = createPQExpBuffer();
9218
9219         while (ncomments > 0)
9220         {
9221                 const char *descr = comments->descr;
9222                 int                     objsubid = comments->objsubid;
9223
9224                 if (objsubid == 0)
9225                 {
9226                         resetPQExpBuffer(target);
9227                         appendPQExpBuffer(target, "%s %s", reltypename,
9228                                                           fmtId(tbinfo->dobj.name));
9229
9230                         resetPQExpBuffer(query);
9231                         appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
9232                         appendStringLiteralAH(query, descr, fout);
9233                         appendPQExpBufferStr(query, ";\n");
9234
9235                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
9236                                                  target->data,
9237                                                  tbinfo->dobj.namespace->dobj.name,
9238                                                  NULL, tbinfo->rolname,
9239                                                  false, "COMMENT", SECTION_NONE,
9240                                                  query->data, "", NULL,
9241                                                  &(tbinfo->dobj.dumpId), 1,
9242                                                  NULL, NULL);
9243                 }
9244                 else if (objsubid > 0 && objsubid <= tbinfo->numatts)
9245                 {
9246                         resetPQExpBuffer(target);
9247                         appendPQExpBuffer(target, "COLUMN %s.",
9248                                                           fmtId(tbinfo->dobj.name));
9249                         appendPQExpBufferStr(target, fmtId(tbinfo->attnames[objsubid - 1]));
9250
9251                         resetPQExpBuffer(query);
9252                         appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
9253                         appendStringLiteralAH(query, descr, fout);
9254                         appendPQExpBufferStr(query, ";\n");
9255
9256                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
9257                                                  target->data,
9258                                                  tbinfo->dobj.namespace->dobj.name,
9259                                                  NULL, tbinfo->rolname,
9260                                                  false, "COMMENT", SECTION_NONE,
9261                                                  query->data, "", NULL,
9262                                                  &(tbinfo->dobj.dumpId), 1,
9263                                                  NULL, NULL);
9264                 }
9265
9266                 comments++;
9267                 ncomments--;
9268         }
9269
9270         destroyPQExpBuffer(query);
9271         destroyPQExpBuffer(target);
9272 }
9273
9274 /*
9275  * findComments --
9276  *
9277  * Find the comment(s), if any, associated with the given object.  All the
9278  * objsubid values associated with the given classoid/objoid are found with
9279  * one search.
9280  */
9281 static int
9282 findComments(Archive *fout, Oid classoid, Oid objoid,
9283                          CommentItem **items)
9284 {
9285         /* static storage for table of comments */
9286         static CommentItem *comments = NULL;
9287         static int      ncomments = -1;
9288
9289         CommentItem *middle = NULL;
9290         CommentItem *low;
9291         CommentItem *high;
9292         int                     nmatch;
9293
9294         /* Get comments if we didn't already */
9295         if (ncomments < 0)
9296                 ncomments = collectComments(fout, &comments);
9297
9298         /*
9299          * Do binary search to find some item matching the object.
9300          */
9301         low = &comments[0];
9302         high = &comments[ncomments - 1];
9303         while (low <= high)
9304         {
9305                 middle = low + (high - low) / 2;
9306
9307                 if (classoid < middle->classoid)
9308                         high = middle - 1;
9309                 else if (classoid > middle->classoid)
9310                         low = middle + 1;
9311                 else if (objoid < middle->objoid)
9312                         high = middle - 1;
9313                 else if (objoid > middle->objoid)
9314                         low = middle + 1;
9315                 else
9316                         break;                          /* found a match */
9317         }
9318
9319         if (low > high)                         /* no matches */
9320         {
9321                 *items = NULL;
9322                 return 0;
9323         }
9324
9325         /*
9326          * Now determine how many items match the object.  The search loop
9327          * invariant still holds: only items between low and high inclusive could
9328          * match.
9329          */
9330         nmatch = 1;
9331         while (middle > low)
9332         {
9333                 if (classoid != middle[-1].classoid ||
9334                         objoid != middle[-1].objoid)
9335                         break;
9336                 middle--;
9337                 nmatch++;
9338         }
9339
9340         *items = middle;
9341
9342         middle += nmatch;
9343         while (middle <= high)
9344         {
9345                 if (classoid != middle->classoid ||
9346                         objoid != middle->objoid)
9347                         break;
9348                 middle++;
9349                 nmatch++;
9350         }
9351
9352         return nmatch;
9353 }
9354
9355 /*
9356  * collectComments --
9357  *
9358  * Construct a table of all comments available for database objects.
9359  * We used to do per-object queries for the comments, but it's much faster
9360  * to pull them all over at once, and on most databases the memory cost
9361  * isn't high.
9362  *
9363  * The table is sorted by classoid/objid/objsubid for speed in lookup.
9364  */
9365 static int
9366 collectComments(Archive *fout, CommentItem **items)
9367 {
9368         PGresult   *res;
9369         PQExpBuffer query;
9370         int                     i_description;
9371         int                     i_classoid;
9372         int                     i_objoid;
9373         int                     i_objsubid;
9374         int                     ntups;
9375         int                     i;
9376         CommentItem *comments;
9377
9378         /*
9379          * Note we do NOT change source schema here; preserve the caller's
9380          * setting, instead.
9381          */
9382
9383         query = createPQExpBuffer();
9384
9385         appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9386                                                  "FROM pg_catalog.pg_description "
9387                                                  "ORDER BY classoid, objoid, objsubid");
9388
9389         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9390
9391         /* Construct lookup table containing OIDs in numeric form */
9392
9393         i_description = PQfnumber(res, "description");
9394         i_classoid = PQfnumber(res, "classoid");
9395         i_objoid = PQfnumber(res, "objoid");
9396         i_objsubid = PQfnumber(res, "objsubid");
9397
9398         ntups = PQntuples(res);
9399
9400         comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9401
9402         for (i = 0; i < ntups; i++)
9403         {
9404                 comments[i].descr = PQgetvalue(res, i, i_description);
9405                 comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9406                 comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9407                 comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9408         }
9409
9410         /* Do NOT free the PGresult since we are keeping pointers into it */
9411         destroyPQExpBuffer(query);
9412
9413         *items = comments;
9414         return ntups;
9415 }
9416
9417 /*
9418  * dumpDumpableObject
9419  *
9420  * This routine and its subsidiaries are responsible for creating
9421  * ArchiveEntries (TOC objects) for each object to be dumped.
9422  */
9423 static void
9424 dumpDumpableObject(Archive *fout, DumpableObject *dobj)
9425 {
9426         switch (dobj->objType)
9427         {
9428                 case DO_NAMESPACE:
9429                         dumpNamespace(fout, (NamespaceInfo *) dobj);
9430                         break;
9431                 case DO_EXTENSION:
9432                         dumpExtension(fout, (ExtensionInfo *) dobj);
9433                         break;
9434                 case DO_TYPE:
9435                         dumpType(fout, (TypeInfo *) dobj);
9436                         break;
9437                 case DO_SHELL_TYPE:
9438                         dumpShellType(fout, (ShellTypeInfo *) dobj);
9439                         break;
9440                 case DO_FUNC:
9441                         dumpFunc(fout, (FuncInfo *) dobj);
9442                         break;
9443                 case DO_AGG:
9444                         dumpAgg(fout, (AggInfo *) dobj);
9445                         break;
9446                 case DO_OPERATOR:
9447                         dumpOpr(fout, (OprInfo *) dobj);
9448                         break;
9449                 case DO_ACCESS_METHOD:
9450                         dumpAccessMethod(fout, (AccessMethodInfo *) dobj);
9451                         break;
9452                 case DO_OPCLASS:
9453                         dumpOpclass(fout, (OpclassInfo *) dobj);
9454                         break;
9455                 case DO_OPFAMILY:
9456                         dumpOpfamily(fout, (OpfamilyInfo *) dobj);
9457                         break;
9458                 case DO_COLLATION:
9459                         dumpCollation(fout, (CollInfo *) dobj);
9460                         break;
9461                 case DO_CONVERSION:
9462                         dumpConversion(fout, (ConvInfo *) dobj);
9463                         break;
9464                 case DO_TABLE:
9465                         dumpTable(fout, (TableInfo *) dobj);
9466                         break;
9467                 case DO_ATTRDEF:
9468                         dumpAttrDef(fout, (AttrDefInfo *) dobj);
9469                         break;
9470                 case DO_INDEX:
9471                         dumpIndex(fout, (IndxInfo *) dobj);
9472                         break;
9473                 case DO_STATSEXT:
9474                         dumpStatisticsExt(fout, (StatsExtInfo *) dobj);
9475                         break;
9476                 case DO_REFRESH_MATVIEW:
9477                         refreshMatViewData(fout, (TableDataInfo *) dobj);
9478                         break;
9479                 case DO_RULE:
9480                         dumpRule(fout, (RuleInfo *) dobj);
9481                         break;
9482                 case DO_TRIGGER:
9483                         dumpTrigger(fout, (TriggerInfo *) dobj);
9484                         break;
9485                 case DO_EVENT_TRIGGER:
9486                         dumpEventTrigger(fout, (EventTriggerInfo *) dobj);
9487                         break;
9488                 case DO_CONSTRAINT:
9489                         dumpConstraint(fout, (ConstraintInfo *) dobj);
9490                         break;
9491                 case DO_FK_CONSTRAINT:
9492                         dumpConstraint(fout, (ConstraintInfo *) dobj);
9493                         break;
9494                 case DO_PROCLANG:
9495                         dumpProcLang(fout, (ProcLangInfo *) dobj);
9496                         break;
9497                 case DO_CAST:
9498                         dumpCast(fout, (CastInfo *) dobj);
9499                         break;
9500                 case DO_TRANSFORM:
9501                         dumpTransform(fout, (TransformInfo *) dobj);
9502                         break;
9503                 case DO_SEQUENCE_SET:
9504                         dumpSequenceData(fout, (TableDataInfo *) dobj);
9505                         break;
9506                 case DO_TABLE_DATA:
9507                         dumpTableData(fout, (TableDataInfo *) dobj);
9508                         break;
9509                 case DO_DUMMY_TYPE:
9510                         /* table rowtypes and array types are never dumped separately */
9511                         break;
9512                 case DO_TSPARSER:
9513                         dumpTSParser(fout, (TSParserInfo *) dobj);
9514                         break;
9515                 case DO_TSDICT:
9516                         dumpTSDictionary(fout, (TSDictInfo *) dobj);
9517                         break;
9518                 case DO_TSTEMPLATE:
9519                         dumpTSTemplate(fout, (TSTemplateInfo *) dobj);
9520                         break;
9521                 case DO_TSCONFIG:
9522                         dumpTSConfig(fout, (TSConfigInfo *) dobj);
9523                         break;
9524                 case DO_FDW:
9525                         dumpForeignDataWrapper(fout, (FdwInfo *) dobj);
9526                         break;
9527                 case DO_FOREIGN_SERVER:
9528                         dumpForeignServer(fout, (ForeignServerInfo *) dobj);
9529                         break;
9530                 case DO_DEFAULT_ACL:
9531                         dumpDefaultACL(fout, (DefaultACLInfo *) dobj);
9532                         break;
9533                 case DO_BLOB:
9534                         dumpBlob(fout, (BlobInfo *) dobj);
9535                         break;
9536                 case DO_BLOB_DATA:
9537                         if (dobj->dump & DUMP_COMPONENT_DATA)
9538                                 ArchiveEntry(fout, dobj->catId, dobj->dumpId,
9539                                                          dobj->name, NULL, NULL, "",
9540                                                          false, "BLOBS", SECTION_DATA,
9541                                                          "", "", NULL,
9542                                                          NULL, 0,
9543                                                          dumpBlobs, NULL);
9544                         break;
9545                 case DO_POLICY:
9546                         dumpPolicy(fout, (PolicyInfo *) dobj);
9547                         break;
9548                 case DO_PUBLICATION:
9549                         dumpPublication(fout, (PublicationInfo *) dobj);
9550                         break;
9551                 case DO_PUBLICATION_REL:
9552                         dumpPublicationTable(fout, (PublicationRelInfo *) dobj);
9553                         break;
9554                 case DO_SUBSCRIPTION:
9555                         dumpSubscription(fout, (SubscriptionInfo *) dobj);
9556                         break;
9557                 case DO_PRE_DATA_BOUNDARY:
9558                 case DO_POST_DATA_BOUNDARY:
9559                         /* never dumped, nothing to do */
9560                         break;
9561         }
9562 }
9563
9564 /*
9565  * dumpNamespace
9566  *        writes out to fout the queries to recreate a user-defined namespace
9567  */
9568 static void
9569 dumpNamespace(Archive *fout, NamespaceInfo *nspinfo)
9570 {
9571         DumpOptions *dopt = fout->dopt;
9572         PQExpBuffer q;
9573         PQExpBuffer delq;
9574         PQExpBuffer labelq;
9575         char       *qnspname;
9576
9577         /* Skip if not to be dumped */
9578         if (!nspinfo->dobj.dump || dopt->dataOnly)
9579                 return;
9580
9581         q = createPQExpBuffer();
9582         delq = createPQExpBuffer();
9583         labelq = createPQExpBuffer();
9584
9585         qnspname = pg_strdup(fmtId(nspinfo->dobj.name));
9586
9587         appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
9588
9589         appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname);
9590
9591         appendPQExpBuffer(labelq, "SCHEMA %s", qnspname);
9592
9593         if (dopt->binary_upgrade)
9594                 binary_upgrade_extension_member(q, &nspinfo->dobj, labelq->data);
9595
9596         if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
9597                 ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
9598                                          nspinfo->dobj.name,
9599                                          NULL, NULL,
9600                                          nspinfo->rolname,
9601                                          false, "SCHEMA", SECTION_PRE_DATA,
9602                                          q->data, delq->data, NULL,
9603                                          NULL, 0,
9604                                          NULL, NULL);
9605
9606         /* Dump Schema Comments and Security Labels */
9607         if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
9608                 dumpComment(fout, labelq->data,
9609                                         NULL, nspinfo->rolname,
9610                                         nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
9611
9612         if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
9613                 dumpSecLabel(fout, labelq->data,
9614                                          NULL, nspinfo->rolname,
9615                                          nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
9616
9617         if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL)
9618                 dumpACL(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId, "SCHEMA",
9619                                 qnspname, NULL, nspinfo->dobj.name, NULL,
9620                                 nspinfo->rolname, nspinfo->nspacl, nspinfo->rnspacl,
9621                                 nspinfo->initnspacl, nspinfo->initrnspacl);
9622
9623         free(qnspname);
9624
9625         destroyPQExpBuffer(q);
9626         destroyPQExpBuffer(delq);
9627         destroyPQExpBuffer(labelq);
9628 }
9629
9630 /*
9631  * dumpExtension
9632  *        writes out to fout the queries to recreate an extension
9633  */
9634 static void
9635 dumpExtension(Archive *fout, ExtensionInfo *extinfo)
9636 {
9637         DumpOptions *dopt = fout->dopt;
9638         PQExpBuffer q;
9639         PQExpBuffer delq;
9640         PQExpBuffer labelq;
9641         char       *qextname;
9642
9643         /* Skip if not to be dumped */
9644         if (!extinfo->dobj.dump || dopt->dataOnly)
9645                 return;
9646
9647         q = createPQExpBuffer();
9648         delq = createPQExpBuffer();
9649         labelq = createPQExpBuffer();
9650
9651         qextname = pg_strdup(fmtId(extinfo->dobj.name));
9652
9653         appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname);
9654
9655         if (!dopt->binary_upgrade)
9656         {
9657                 /*
9658                  * In a regular dump, we use IF NOT EXISTS so that there isn't a
9659                  * problem if the extension already exists in the target database;
9660                  * this is essential for installed-by-default extensions such as
9661                  * plpgsql.
9662                  *
9663                  * In binary-upgrade mode, that doesn't work well, so instead we skip
9664                  * built-in extensions based on their OIDs; see
9665                  * selectDumpableExtension.
9666                  */
9667                 appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n",
9668                                                   qextname, fmtId(extinfo->namespace));
9669         }
9670         else
9671         {
9672                 int                     i;
9673                 int                     n;
9674
9675                 appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
9676
9677                 /*
9678                  * We unconditionally create the extension, so we must drop it if it
9679                  * exists.  This could happen if the user deleted 'plpgsql' and then
9680                  * readded it, causing its oid to be greater than g_last_builtin_oid.
9681                  * The g_last_builtin_oid test was kept to avoid repeatedly dropping
9682                  * and recreating extensions like 'plpgsql'.
9683                  */
9684                 appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
9685
9686                 appendPQExpBufferStr(q,
9687                                                          "SELECT pg_catalog.binary_upgrade_create_empty_extension(");
9688                 appendStringLiteralAH(q, extinfo->dobj.name, fout);
9689                 appendPQExpBufferStr(q, ", ");
9690                 appendStringLiteralAH(q, extinfo->namespace, fout);
9691                 appendPQExpBufferStr(q, ", ");
9692                 appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false");
9693                 appendStringLiteralAH(q, extinfo->extversion, fout);
9694                 appendPQExpBufferStr(q, ", ");
9695
9696                 /*
9697                  * Note that we're pushing extconfig (an OID array) back into
9698                  * pg_extension exactly as-is.  This is OK because pg_class OIDs are
9699                  * preserved in binary upgrade.
9700                  */
9701                 if (strlen(extinfo->extconfig) > 2)
9702                         appendStringLiteralAH(q, extinfo->extconfig, fout);
9703                 else
9704                         appendPQExpBufferStr(q, "NULL");
9705                 appendPQExpBufferStr(q, ", ");
9706                 if (strlen(extinfo->extcondition) > 2)
9707                         appendStringLiteralAH(q, extinfo->extcondition, fout);
9708                 else
9709                         appendPQExpBufferStr(q, "NULL");
9710                 appendPQExpBufferStr(q, ", ");
9711                 appendPQExpBufferStr(q, "ARRAY[");
9712                 n = 0;
9713                 for (i = 0; i < extinfo->dobj.nDeps; i++)
9714                 {
9715                         DumpableObject *extobj;
9716
9717                         extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]);
9718                         if (extobj && extobj->objType == DO_EXTENSION)
9719                         {
9720                                 if (n++ > 0)
9721                                         appendPQExpBufferChar(q, ',');
9722                                 appendStringLiteralAH(q, extobj->name, fout);
9723                         }
9724                 }
9725                 appendPQExpBufferStr(q, "]::pg_catalog.text[]");
9726                 appendPQExpBufferStr(q, ");\n");
9727         }
9728
9729         appendPQExpBuffer(labelq, "EXTENSION %s", qextname);
9730
9731         if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
9732                 ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId,
9733                                          extinfo->dobj.name,
9734                                          NULL, NULL,
9735                                          "",
9736                                          false, "EXTENSION", SECTION_PRE_DATA,
9737                                          q->data, delq->data, NULL,
9738                                          NULL, 0,
9739                                          NULL, NULL);
9740
9741         /* Dump Extension Comments and Security Labels */
9742         if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
9743                 dumpComment(fout, labelq->data,
9744                                         NULL, "",
9745                                         extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
9746
9747         if (extinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
9748                 dumpSecLabel(fout, labelq->data,
9749                                          NULL, "",
9750                                          extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
9751
9752         free(qextname);
9753
9754         destroyPQExpBuffer(q);
9755         destroyPQExpBuffer(delq);
9756         destroyPQExpBuffer(labelq);
9757 }
9758
9759 /*
9760  * dumpType
9761  *        writes out to fout the queries to recreate a user-defined type
9762  */
9763 static void
9764 dumpType(Archive *fout, TypeInfo *tyinfo)
9765 {
9766         DumpOptions *dopt = fout->dopt;
9767
9768         /* Skip if not to be dumped */
9769         if (!tyinfo->dobj.dump || dopt->dataOnly)
9770                 return;
9771
9772         /* Dump out in proper style */
9773         if (tyinfo->typtype == TYPTYPE_BASE)
9774                 dumpBaseType(fout, tyinfo);
9775         else if (tyinfo->typtype == TYPTYPE_DOMAIN)
9776                 dumpDomain(fout, tyinfo);
9777         else if (tyinfo->typtype == TYPTYPE_COMPOSITE)
9778                 dumpCompositeType(fout, tyinfo);
9779         else if (tyinfo->typtype == TYPTYPE_ENUM)
9780                 dumpEnumType(fout, tyinfo);
9781         else if (tyinfo->typtype == TYPTYPE_RANGE)
9782                 dumpRangeType(fout, tyinfo);
9783         else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
9784                 dumpUndefinedType(fout, tyinfo);
9785         else
9786                 write_msg(NULL, "WARNING: typtype of data type \"%s\" appears to be invalid\n",
9787                                   tyinfo->dobj.name);
9788 }
9789
9790 /*
9791  * dumpEnumType
9792  *        writes out to fout the queries to recreate a user-defined enum type
9793  */
9794 static void
9795 dumpEnumType(Archive *fout, TypeInfo *tyinfo)
9796 {
9797         DumpOptions *dopt = fout->dopt;
9798         PQExpBuffer q = createPQExpBuffer();
9799         PQExpBuffer delq = createPQExpBuffer();
9800         PQExpBuffer labelq = createPQExpBuffer();
9801         PQExpBuffer query = createPQExpBuffer();
9802         PGresult   *res;
9803         int                     num,
9804                                 i;
9805         Oid                     enum_oid;
9806         char       *qtypname;
9807         char       *label;
9808
9809         /* Set proper schema search path */
9810         selectSourceSchema(fout, "pg_catalog");
9811
9812         if (fout->remoteVersion >= 90100)
9813                 appendPQExpBuffer(query, "SELECT oid, enumlabel "
9814                                                   "FROM pg_catalog.pg_enum "
9815                                                   "WHERE enumtypid = '%u'"
9816                                                   "ORDER BY enumsortorder",
9817                                                   tyinfo->dobj.catId.oid);
9818         else
9819                 appendPQExpBuffer(query, "SELECT oid, enumlabel "
9820                                                   "FROM pg_catalog.pg_enum "
9821                                                   "WHERE enumtypid = '%u'"
9822                                                   "ORDER BY oid",
9823                                                   tyinfo->dobj.catId.oid);
9824
9825         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9826
9827         num = PQntuples(res);
9828
9829         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
9830
9831         /*
9832          * DROP must be fully qualified in case same name appears in pg_catalog.
9833          * CASCADE shouldn't be required here as for normal types since the I/O
9834          * functions are generic and do not get dropped.
9835          */
9836         appendPQExpBuffer(delq, "DROP TYPE %s.",
9837                                           fmtId(tyinfo->dobj.namespace->dobj.name));
9838         appendPQExpBuffer(delq, "%s;\n",
9839                                           qtypname);
9840
9841         if (dopt->binary_upgrade)
9842                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
9843                                                                                                  tyinfo->dobj.catId.oid);
9844
9845         appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
9846                                           qtypname);
9847
9848         if (!dopt->binary_upgrade)
9849         {
9850                 /* Labels with server-assigned oids */
9851                 for (i = 0; i < num; i++)
9852                 {
9853                         label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
9854                         if (i > 0)
9855                                 appendPQExpBufferChar(q, ',');
9856                         appendPQExpBufferStr(q, "\n    ");
9857                         appendStringLiteralAH(q, label, fout);
9858                 }
9859         }
9860
9861         appendPQExpBufferStr(q, "\n);\n");
9862
9863         if (dopt->binary_upgrade)
9864         {
9865                 /* Labels with dump-assigned (preserved) oids */
9866                 for (i = 0; i < num; i++)
9867                 {
9868                         enum_oid = atooid(PQgetvalue(res, i, PQfnumber(res, "oid")));
9869                         label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
9870
9871                         if (i == 0)
9872                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
9873                         appendPQExpBuffer(q,
9874                                                           "SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n",
9875                                                           enum_oid);
9876                         appendPQExpBuffer(q, "ALTER TYPE %s.",
9877                                                           fmtId(tyinfo->dobj.namespace->dobj.name));
9878                         appendPQExpBuffer(q, "%s ADD VALUE ",
9879                                                           qtypname);
9880                         appendStringLiteralAH(q, label, fout);
9881                         appendPQExpBufferStr(q, ";\n\n");
9882                 }
9883         }
9884
9885         appendPQExpBuffer(labelq, "TYPE %s", qtypname);
9886
9887         if (dopt->binary_upgrade)
9888                 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
9889
9890         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
9891                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
9892                                          tyinfo->dobj.name,
9893                                          tyinfo->dobj.namespace->dobj.name,
9894                                          NULL,
9895                                          tyinfo->rolname, false,
9896                                          "TYPE", SECTION_PRE_DATA,
9897                                          q->data, delq->data, NULL,
9898                                          NULL, 0,
9899                                          NULL, NULL);
9900
9901         /* Dump Type Comments and Security Labels */
9902         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
9903                 dumpComment(fout, labelq->data,
9904                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
9905                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
9906
9907         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
9908                 dumpSecLabel(fout, labelq->data,
9909                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
9910                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
9911
9912         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
9913                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
9914                                 qtypname, NULL, tyinfo->dobj.name,
9915                                 tyinfo->dobj.namespace->dobj.name,
9916                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
9917                                 tyinfo->inittypacl, tyinfo->initrtypacl);
9918
9919         PQclear(res);
9920         destroyPQExpBuffer(q);
9921         destroyPQExpBuffer(delq);
9922         destroyPQExpBuffer(labelq);
9923         destroyPQExpBuffer(query);
9924 }
9925
9926 /*
9927  * dumpRangeType
9928  *        writes out to fout the queries to recreate a user-defined range type
9929  */
9930 static void
9931 dumpRangeType(Archive *fout, TypeInfo *tyinfo)
9932 {
9933         DumpOptions *dopt = fout->dopt;
9934         PQExpBuffer q = createPQExpBuffer();
9935         PQExpBuffer delq = createPQExpBuffer();
9936         PQExpBuffer labelq = createPQExpBuffer();
9937         PQExpBuffer query = createPQExpBuffer();
9938         PGresult   *res;
9939         Oid                     collationOid;
9940         char       *qtypname;
9941         char       *procname;
9942
9943         /*
9944          * select appropriate schema to ensure names in CREATE are properly
9945          * qualified
9946          */
9947         selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
9948
9949         appendPQExpBuffer(query,
9950                                           "SELECT pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
9951                                           "opc.opcname AS opcname, "
9952                                           "(SELECT nspname FROM pg_catalog.pg_namespace nsp "
9953                                           "  WHERE nsp.oid = opc.opcnamespace) AS opcnsp, "
9954                                           "opc.opcdefault, "
9955                                           "CASE WHEN rngcollation = st.typcollation THEN 0 "
9956                                           "     ELSE rngcollation END AS collation, "
9957                                           "rngcanonical, rngsubdiff "
9958                                           "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, "
9959                                           "     pg_catalog.pg_opclass opc "
9960                                           "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND "
9961                                           "rngtypid = '%u'",
9962                                           tyinfo->dobj.catId.oid);
9963
9964         res = ExecuteSqlQueryForSingleRow(fout, query->data);
9965
9966         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
9967
9968         /*
9969          * DROP must be fully qualified in case same name appears in pg_catalog.
9970          * CASCADE shouldn't be required here as for normal types since the I/O
9971          * functions are generic and do not get dropped.
9972          */
9973         appendPQExpBuffer(delq, "DROP TYPE %s.",
9974                                           fmtId(tyinfo->dobj.namespace->dobj.name));
9975         appendPQExpBuffer(delq, "%s;\n",
9976                                           qtypname);
9977
9978         if (dopt->binary_upgrade)
9979                 binary_upgrade_set_type_oids_by_type_oid(fout,
9980                                                                                                  q, tyinfo->dobj.catId.oid);
9981
9982         appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
9983                                           qtypname);
9984
9985         appendPQExpBuffer(q, "\n    subtype = %s",
9986                                           PQgetvalue(res, 0, PQfnumber(res, "rngsubtype")));
9987
9988         /* print subtype_opclass only if not default for subtype */
9989         if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't')
9990         {
9991                 char       *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname"));
9992                 char       *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp"));
9993
9994                 /* always schema-qualify, don't try to be smart */
9995                 appendPQExpBuffer(q, ",\n    subtype_opclass = %s.",
9996                                                   fmtId(nspname));
9997                 appendPQExpBufferStr(q, fmtId(opcname));
9998         }
9999
10000         collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation")));
10001         if (OidIsValid(collationOid))
10002         {
10003                 CollInfo   *coll = findCollationByOid(collationOid);
10004
10005                 if (coll)
10006                 {
10007                         /* always schema-qualify, don't try to be smart */
10008                         appendPQExpBuffer(q, ",\n    collation = %s.",
10009                                                           fmtId(coll->dobj.namespace->dobj.name));
10010                         appendPQExpBufferStr(q, fmtId(coll->dobj.name));
10011                 }
10012         }
10013
10014         procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical"));
10015         if (strcmp(procname, "-") != 0)
10016                 appendPQExpBuffer(q, ",\n    canonical = %s", procname);
10017
10018         procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff"));
10019         if (strcmp(procname, "-") != 0)
10020                 appendPQExpBuffer(q, ",\n    subtype_diff = %s", procname);
10021
10022         appendPQExpBufferStr(q, "\n);\n");
10023
10024         appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10025
10026         if (dopt->binary_upgrade)
10027                 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10028
10029         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10030                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10031                                          tyinfo->dobj.name,
10032                                          tyinfo->dobj.namespace->dobj.name,
10033                                          NULL,
10034                                          tyinfo->rolname, false,
10035                                          "TYPE", SECTION_PRE_DATA,
10036                                          q->data, delq->data, NULL,
10037                                          NULL, 0,
10038                                          NULL, NULL);
10039
10040         /* Dump Type Comments and Security Labels */
10041         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10042                 dumpComment(fout, labelq->data,
10043                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10044                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10045
10046         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10047                 dumpSecLabel(fout, labelq->data,
10048                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10049                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10050
10051         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10052                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10053                                 qtypname, NULL, tyinfo->dobj.name,
10054                                 tyinfo->dobj.namespace->dobj.name,
10055                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10056                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10057
10058         PQclear(res);
10059         destroyPQExpBuffer(q);
10060         destroyPQExpBuffer(delq);
10061         destroyPQExpBuffer(labelq);
10062         destroyPQExpBuffer(query);
10063 }
10064
10065 /*
10066  * dumpUndefinedType
10067  *        writes out to fout the queries to recreate a !typisdefined type
10068  *
10069  * This is a shell type, but we use different terminology to distinguish
10070  * this case from where we have to emit a shell type definition to break
10071  * circular dependencies.  An undefined type shouldn't ever have anything
10072  * depending on it.
10073  */
10074 static void
10075 dumpUndefinedType(Archive *fout, TypeInfo *tyinfo)
10076 {
10077         DumpOptions *dopt = fout->dopt;
10078         PQExpBuffer q = createPQExpBuffer();
10079         PQExpBuffer delq = createPQExpBuffer();
10080         PQExpBuffer labelq = createPQExpBuffer();
10081         char       *qtypname;
10082
10083         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10084
10085         /*
10086          * DROP must be fully qualified in case same name appears in pg_catalog.
10087          */
10088         appendPQExpBuffer(delq, "DROP TYPE %s.",
10089                                           fmtId(tyinfo->dobj.namespace->dobj.name));
10090         appendPQExpBuffer(delq, "%s;\n",
10091                                           qtypname);
10092
10093         if (dopt->binary_upgrade)
10094                 binary_upgrade_set_type_oids_by_type_oid(fout,
10095                                                                                                  q, tyinfo->dobj.catId.oid);
10096
10097         appendPQExpBuffer(q, "CREATE TYPE %s;\n",
10098                                           qtypname);
10099
10100         appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10101
10102         if (dopt->binary_upgrade)
10103                 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10104
10105         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10106                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10107                                          tyinfo->dobj.name,
10108                                          tyinfo->dobj.namespace->dobj.name,
10109                                          NULL,
10110                                          tyinfo->rolname, false,
10111                                          "TYPE", SECTION_PRE_DATA,
10112                                          q->data, delq->data, NULL,
10113                                          NULL, 0,
10114                                          NULL, NULL);
10115
10116         /* Dump Type Comments and Security Labels */
10117         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10118                 dumpComment(fout, labelq->data,
10119                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10120                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10121
10122         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10123                 dumpSecLabel(fout, labelq->data,
10124                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10125                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10126
10127         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10128                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10129                                 qtypname, NULL, tyinfo->dobj.name,
10130                                 tyinfo->dobj.namespace->dobj.name,
10131                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10132                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10133
10134         destroyPQExpBuffer(q);
10135         destroyPQExpBuffer(delq);
10136         destroyPQExpBuffer(labelq);
10137 }
10138
10139 /*
10140  * dumpBaseType
10141  *        writes out to fout the queries to recreate a user-defined base type
10142  */
10143 static void
10144 dumpBaseType(Archive *fout, TypeInfo *tyinfo)
10145 {
10146         DumpOptions *dopt = fout->dopt;
10147         PQExpBuffer q = createPQExpBuffer();
10148         PQExpBuffer delq = createPQExpBuffer();
10149         PQExpBuffer labelq = createPQExpBuffer();
10150         PQExpBuffer query = createPQExpBuffer();
10151         PGresult   *res;
10152         char       *qtypname;
10153         char       *typlen;
10154         char       *typinput;
10155         char       *typoutput;
10156         char       *typreceive;
10157         char       *typsend;
10158         char       *typmodin;
10159         char       *typmodout;
10160         char       *typanalyze;
10161         Oid                     typreceiveoid;
10162         Oid                     typsendoid;
10163         Oid                     typmodinoid;
10164         Oid                     typmodoutoid;
10165         Oid                     typanalyzeoid;
10166         char       *typcategory;
10167         char       *typispreferred;
10168         char       *typdelim;
10169         char       *typbyval;
10170         char       *typalign;
10171         char       *typstorage;
10172         char       *typcollatable;
10173         char       *typdefault;
10174         bool            typdefault_is_literal = false;
10175
10176         /* Set proper schema search path so regproc references list correctly */
10177         selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10178
10179         /* Fetch type-specific details */
10180         if (fout->remoteVersion >= 90100)
10181         {
10182                 appendPQExpBuffer(query, "SELECT typlen, "
10183                                                   "typinput, typoutput, typreceive, typsend, "
10184                                                   "typmodin, typmodout, typanalyze, "
10185                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10186                                                   "typsend::pg_catalog.oid AS typsendoid, "
10187                                                   "typmodin::pg_catalog.oid AS typmodinoid, "
10188                                                   "typmodout::pg_catalog.oid AS typmodoutoid, "
10189                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10190                                                   "typcategory, typispreferred, "
10191                                                   "typdelim, typbyval, typalign, typstorage, "
10192                                                   "(typcollation <> 0) AS typcollatable, "
10193                                                   "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10194                                                   "FROM pg_catalog.pg_type "
10195                                                   "WHERE oid = '%u'::pg_catalog.oid",
10196                                                   tyinfo->dobj.catId.oid);
10197         }
10198         else if (fout->remoteVersion >= 80400)
10199         {
10200                 appendPQExpBuffer(query, "SELECT typlen, "
10201                                                   "typinput, typoutput, typreceive, typsend, "
10202                                                   "typmodin, typmodout, typanalyze, "
10203                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10204                                                   "typsend::pg_catalog.oid AS typsendoid, "
10205                                                   "typmodin::pg_catalog.oid AS typmodinoid, "
10206                                                   "typmodout::pg_catalog.oid AS typmodoutoid, "
10207                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10208                                                   "typcategory, typispreferred, "
10209                                                   "typdelim, typbyval, typalign, typstorage, "
10210                                                   "false AS typcollatable, "
10211                                                   "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10212                                                   "FROM pg_catalog.pg_type "
10213                                                   "WHERE oid = '%u'::pg_catalog.oid",
10214                                                   tyinfo->dobj.catId.oid);
10215         }
10216         else if (fout->remoteVersion >= 80300)
10217         {
10218                 /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
10219                 appendPQExpBuffer(query, "SELECT typlen, "
10220                                                   "typinput, typoutput, typreceive, typsend, "
10221                                                   "typmodin, typmodout, typanalyze, "
10222                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10223                                                   "typsend::pg_catalog.oid AS typsendoid, "
10224                                                   "typmodin::pg_catalog.oid AS typmodinoid, "
10225                                                   "typmodout::pg_catalog.oid AS typmodoutoid, "
10226                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10227                                                   "'U' AS typcategory, false AS typispreferred, "
10228                                                   "typdelim, typbyval, typalign, typstorage, "
10229                                                   "false AS typcollatable, "
10230                                                   "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10231                                                   "FROM pg_catalog.pg_type "
10232                                                   "WHERE oid = '%u'::pg_catalog.oid",
10233                                                   tyinfo->dobj.catId.oid);
10234         }
10235         else
10236         {
10237                 appendPQExpBuffer(query, "SELECT typlen, "
10238                                                   "typinput, typoutput, typreceive, typsend, "
10239                                                   "'-' AS typmodin, '-' AS typmodout, "
10240                                                   "typanalyze, "
10241                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10242                                                   "typsend::pg_catalog.oid AS typsendoid, "
10243                                                   "0 AS typmodinoid, 0 AS typmodoutoid, "
10244                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10245                                                   "'U' AS typcategory, false AS typispreferred, "
10246                                                   "typdelim, typbyval, typalign, typstorage, "
10247                                                   "false AS typcollatable, "
10248                                                   "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10249                                                   "FROM pg_catalog.pg_type "
10250                                                   "WHERE oid = '%u'::pg_catalog.oid",
10251                                                   tyinfo->dobj.catId.oid);
10252         }
10253
10254         res = ExecuteSqlQueryForSingleRow(fout, query->data);
10255
10256         typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10257         typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10258         typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10259         typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10260         typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10261         typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10262         typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10263         typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10264         typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10265         typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10266         typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10267         typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10268         typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10269         typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10270         typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10271         typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10272         typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10273         typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10274         typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10275         typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10276         if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10277                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10278         else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10279         {
10280                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10281                 typdefault_is_literal = true;   /* it needs quotes */
10282         }
10283         else
10284                 typdefault = NULL;
10285
10286         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10287
10288         /*
10289          * DROP must be fully qualified in case same name appears in pg_catalog.
10290          * The reason we include CASCADE is that the circular dependency between
10291          * the type and its I/O functions makes it impossible to drop the type any
10292          * other way.
10293          */
10294         appendPQExpBuffer(delq, "DROP TYPE %s.",
10295                                           fmtId(tyinfo->dobj.namespace->dobj.name));
10296         appendPQExpBuffer(delq, "%s CASCADE;\n",
10297                                           qtypname);
10298
10299         /* We might already have a shell type, but setting pg_type_oid is harmless */
10300         if (dopt->binary_upgrade)
10301                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10302                                                                                                  tyinfo->dobj.catId.oid);
10303
10304         appendPQExpBuffer(q,
10305                                           "CREATE TYPE %s (\n"
10306                                           "    INTERNALLENGTH = %s",
10307                                           qtypname,
10308                                           (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10309
10310         /* regproc result is sufficiently quoted already */
10311         appendPQExpBuffer(q, ",\n    INPUT = %s", typinput);
10312         appendPQExpBuffer(q, ",\n    OUTPUT = %s", typoutput);
10313         if (OidIsValid(typreceiveoid))
10314                 appendPQExpBuffer(q, ",\n    RECEIVE = %s", typreceive);
10315         if (OidIsValid(typsendoid))
10316                 appendPQExpBuffer(q, ",\n    SEND = %s", typsend);
10317         if (OidIsValid(typmodinoid))
10318                 appendPQExpBuffer(q, ",\n    TYPMOD_IN = %s", typmodin);
10319         if (OidIsValid(typmodoutoid))
10320                 appendPQExpBuffer(q, ",\n    TYPMOD_OUT = %s", typmodout);
10321         if (OidIsValid(typanalyzeoid))
10322                 appendPQExpBuffer(q, ",\n    ANALYZE = %s", typanalyze);
10323
10324         if (strcmp(typcollatable, "t") == 0)
10325                 appendPQExpBufferStr(q, ",\n    COLLATABLE = true");
10326
10327         if (typdefault != NULL)
10328         {
10329                 appendPQExpBufferStr(q, ",\n    DEFAULT = ");
10330                 if (typdefault_is_literal)
10331                         appendStringLiteralAH(q, typdefault, fout);
10332                 else
10333                         appendPQExpBufferStr(q, typdefault);
10334         }
10335
10336         if (OidIsValid(tyinfo->typelem))
10337         {
10338                 char       *elemType;
10339
10340                 /* reselect schema in case changed by function dump */
10341                 selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10342                 elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroAsOpaque);
10343                 appendPQExpBuffer(q, ",\n    ELEMENT = %s", elemType);
10344                 free(elemType);
10345         }
10346
10347         if (strcmp(typcategory, "U") != 0)
10348         {
10349                 appendPQExpBufferStr(q, ",\n    CATEGORY = ");
10350                 appendStringLiteralAH(q, typcategory, fout);
10351         }
10352
10353         if (strcmp(typispreferred, "t") == 0)
10354                 appendPQExpBufferStr(q, ",\n    PREFERRED = true");
10355
10356         if (typdelim && strcmp(typdelim, ",") != 0)
10357         {
10358                 appendPQExpBufferStr(q, ",\n    DELIMITER = ");
10359                 appendStringLiteralAH(q, typdelim, fout);
10360         }
10361
10362         if (strcmp(typalign, "c") == 0)
10363                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = char");
10364         else if (strcmp(typalign, "s") == 0)
10365                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = int2");
10366         else if (strcmp(typalign, "i") == 0)
10367                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = int4");
10368         else if (strcmp(typalign, "d") == 0)
10369                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = double");
10370
10371         if (strcmp(typstorage, "p") == 0)
10372                 appendPQExpBufferStr(q, ",\n    STORAGE = plain");
10373         else if (strcmp(typstorage, "e") == 0)
10374                 appendPQExpBufferStr(q, ",\n    STORAGE = external");
10375         else if (strcmp(typstorage, "x") == 0)
10376                 appendPQExpBufferStr(q, ",\n    STORAGE = extended");
10377         else if (strcmp(typstorage, "m") == 0)
10378                 appendPQExpBufferStr(q, ",\n    STORAGE = main");
10379
10380         if (strcmp(typbyval, "t") == 0)
10381                 appendPQExpBufferStr(q, ",\n    PASSEDBYVALUE");
10382
10383         appendPQExpBufferStr(q, "\n);\n");
10384
10385         appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10386
10387         if (dopt->binary_upgrade)
10388                 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10389
10390         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10391                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10392                                          tyinfo->dobj.name,
10393                                          tyinfo->dobj.namespace->dobj.name,
10394                                          NULL,
10395                                          tyinfo->rolname, false,
10396                                          "TYPE", SECTION_PRE_DATA,
10397                                          q->data, delq->data, NULL,
10398                                          NULL, 0,
10399                                          NULL, NULL);
10400
10401         /* Dump Type Comments and Security Labels */
10402         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10403                 dumpComment(fout, labelq->data,
10404                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10405                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10406
10407         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10408                 dumpSecLabel(fout, labelq->data,
10409                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10410                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10411
10412         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10413                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10414                                 qtypname, NULL, tyinfo->dobj.name,
10415                                 tyinfo->dobj.namespace->dobj.name,
10416                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10417                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10418
10419         PQclear(res);
10420         destroyPQExpBuffer(q);
10421         destroyPQExpBuffer(delq);
10422         destroyPQExpBuffer(labelq);
10423         destroyPQExpBuffer(query);
10424 }
10425
10426 /*
10427  * dumpDomain
10428  *        writes out to fout the queries to recreate a user-defined domain
10429  */
10430 static void
10431 dumpDomain(Archive *fout, TypeInfo *tyinfo)
10432 {
10433         DumpOptions *dopt = fout->dopt;
10434         PQExpBuffer q = createPQExpBuffer();
10435         PQExpBuffer delq = createPQExpBuffer();
10436         PQExpBuffer labelq = createPQExpBuffer();
10437         PQExpBuffer query = createPQExpBuffer();
10438         PGresult   *res;
10439         int                     i;
10440         char       *qtypname;
10441         char       *typnotnull;
10442         char       *typdefn;
10443         char       *typdefault;
10444         Oid                     typcollation;
10445         bool            typdefault_is_literal = false;
10446
10447         /* Set proper schema search path so type references list correctly */
10448         selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10449
10450         /* Fetch domain specific details */
10451         if (fout->remoteVersion >= 90100)
10452         {
10453                 /* typcollation is new in 9.1 */
10454                 appendPQExpBuffer(query, "SELECT t.typnotnull, "
10455                                                   "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
10456                                                   "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
10457                                                   "t.typdefault, "
10458                                                   "CASE WHEN t.typcollation <> u.typcollation "
10459                                                   "THEN t.typcollation ELSE 0 END AS typcollation "
10460                                                   "FROM pg_catalog.pg_type t "
10461                                                   "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
10462                                                   "WHERE t.oid = '%u'::pg_catalog.oid",
10463                                                   tyinfo->dobj.catId.oid);
10464         }
10465         else
10466         {
10467                 appendPQExpBuffer(query, "SELECT typnotnull, "
10468                                                   "pg_catalog.format_type(typbasetype, typtypmod) AS typdefn, "
10469                                                   "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
10470                                                   "typdefault, 0 AS typcollation "
10471                                                   "FROM pg_catalog.pg_type "
10472                                                   "WHERE oid = '%u'::pg_catalog.oid",
10473                                                   tyinfo->dobj.catId.oid);
10474         }
10475
10476         res = ExecuteSqlQueryForSingleRow(fout, query->data);
10477
10478         typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
10479         typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
10480         if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10481                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10482         else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10483         {
10484                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10485                 typdefault_is_literal = true;   /* it needs quotes */
10486         }
10487         else
10488                 typdefault = NULL;
10489         typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
10490
10491         if (dopt->binary_upgrade)
10492                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10493                                                                                                  tyinfo->dobj.catId.oid);
10494
10495         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10496
10497         appendPQExpBuffer(q,
10498                                           "CREATE DOMAIN %s AS %s",
10499                                           qtypname,
10500                                           typdefn);
10501
10502         /* Print collation only if different from base type's collation */
10503         if (OidIsValid(typcollation))
10504         {
10505                 CollInfo   *coll;
10506
10507                 coll = findCollationByOid(typcollation);
10508                 if (coll)
10509                 {
10510                         /* always schema-qualify, don't try to be smart */
10511                         appendPQExpBuffer(q, " COLLATE %s.",
10512                                                           fmtId(coll->dobj.namespace->dobj.name));
10513                         appendPQExpBufferStr(q, fmtId(coll->dobj.name));
10514                 }
10515         }
10516
10517         if (typnotnull[0] == 't')
10518                 appendPQExpBufferStr(q, " NOT NULL");
10519
10520         if (typdefault != NULL)
10521         {
10522                 appendPQExpBufferStr(q, " DEFAULT ");
10523                 if (typdefault_is_literal)
10524                         appendStringLiteralAH(q, typdefault, fout);
10525                 else
10526                         appendPQExpBufferStr(q, typdefault);
10527         }
10528
10529         PQclear(res);
10530
10531         /*
10532          * Add any CHECK constraints for the domain
10533          */
10534         for (i = 0; i < tyinfo->nDomChecks; i++)
10535         {
10536                 ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
10537
10538                 if (!domcheck->separate)
10539                         appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
10540                                                           fmtId(domcheck->dobj.name), domcheck->condef);
10541         }
10542
10543         appendPQExpBufferStr(q, ";\n");
10544
10545         /*
10546          * DROP must be fully qualified in case same name appears in pg_catalog
10547          */
10548         appendPQExpBuffer(delq, "DROP DOMAIN %s.",
10549                                           fmtId(tyinfo->dobj.namespace->dobj.name));
10550         appendPQExpBuffer(delq, "%s;\n",
10551                                           qtypname);
10552
10553         appendPQExpBuffer(labelq, "DOMAIN %s", qtypname);
10554
10555         if (dopt->binary_upgrade)
10556                 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10557
10558         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10559                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10560                                          tyinfo->dobj.name,
10561                                          tyinfo->dobj.namespace->dobj.name,
10562                                          NULL,
10563                                          tyinfo->rolname, false,
10564                                          "DOMAIN", SECTION_PRE_DATA,
10565                                          q->data, delq->data, NULL,
10566                                          NULL, 0,
10567                                          NULL, NULL);
10568
10569         /* Dump Domain Comments and Security Labels */
10570         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10571                 dumpComment(fout, labelq->data,
10572                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10573                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10574
10575         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10576                 dumpSecLabel(fout, labelq->data,
10577                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10578                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10579
10580         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10581                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10582                                 qtypname, NULL, tyinfo->dobj.name,
10583                                 tyinfo->dobj.namespace->dobj.name,
10584                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10585                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10586
10587         /* Dump any per-constraint comments */
10588         for (i = 0; i < tyinfo->nDomChecks; i++)
10589         {
10590                 ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
10591                 PQExpBuffer labelq = createPQExpBuffer();
10592
10593                 appendPQExpBuffer(labelq, "CONSTRAINT %s ",
10594                                                   fmtId(domcheck->dobj.name));
10595                 appendPQExpBuffer(labelq, "ON DOMAIN %s",
10596                                                   qtypname);
10597
10598                 if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10599                         dumpComment(fout, labelq->data,
10600                                                 tyinfo->dobj.namespace->dobj.name,
10601                                                 tyinfo->rolname,
10602                                                 domcheck->dobj.catId, 0, tyinfo->dobj.dumpId);
10603
10604                 destroyPQExpBuffer(labelq);
10605         }
10606
10607         destroyPQExpBuffer(q);
10608         destroyPQExpBuffer(delq);
10609         destroyPQExpBuffer(labelq);
10610         destroyPQExpBuffer(query);
10611 }
10612
10613 /*
10614  * dumpCompositeType
10615  *        writes out to fout the queries to recreate a user-defined stand-alone
10616  *        composite type
10617  */
10618 static void
10619 dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
10620 {
10621         DumpOptions *dopt = fout->dopt;
10622         PQExpBuffer q = createPQExpBuffer();
10623         PQExpBuffer dropped = createPQExpBuffer();
10624         PQExpBuffer delq = createPQExpBuffer();
10625         PQExpBuffer labelq = createPQExpBuffer();
10626         PQExpBuffer query = createPQExpBuffer();
10627         PGresult   *res;
10628         char       *qtypname;
10629         int                     ntups;
10630         int                     i_attname;
10631         int                     i_atttypdefn;
10632         int                     i_attlen;
10633         int                     i_attalign;
10634         int                     i_attisdropped;
10635         int                     i_attcollation;
10636         int                     i;
10637         int                     actual_atts;
10638
10639         /* Set proper schema search path so type references list correctly */
10640         selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
10641
10642         /* Fetch type specific details */
10643         if (fout->remoteVersion >= 90100)
10644         {
10645                 /*
10646                  * attcollation is new in 9.1.  Since we only want to dump COLLATE
10647                  * clauses for attributes whose collation is different from their
10648                  * type's default, we use a CASE here to suppress uninteresting
10649                  * attcollations cheaply.  atttypid will be 0 for dropped columns;
10650                  * collation does not matter for those.
10651                  */
10652                 appendPQExpBuffer(query, "SELECT a.attname, "
10653                                                   "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10654                                                   "a.attlen, a.attalign, a.attisdropped, "
10655                                                   "CASE WHEN a.attcollation <> at.typcollation "
10656                                                   "THEN a.attcollation ELSE 0 END AS attcollation "
10657                                                   "FROM pg_catalog.pg_type ct "
10658                                                   "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
10659                                                   "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
10660                                                   "WHERE ct.oid = '%u'::pg_catalog.oid "
10661                                                   "ORDER BY a.attnum ",
10662                                                   tyinfo->dobj.catId.oid);
10663         }
10664         else
10665         {
10666                 /*
10667                  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
10668                  * should always be false.
10669                  */
10670                 appendPQExpBuffer(query, "SELECT a.attname, "
10671                                                   "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10672                                                   "a.attlen, a.attalign, a.attisdropped, "
10673                                                   "0 AS attcollation "
10674                                                   "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
10675                                                   "WHERE ct.oid = '%u'::pg_catalog.oid "
10676                                                   "AND a.attrelid = ct.typrelid "
10677                                                   "ORDER BY a.attnum ",
10678                                                   tyinfo->dobj.catId.oid);
10679         }
10680
10681         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10682
10683         ntups = PQntuples(res);
10684
10685         i_attname = PQfnumber(res, "attname");
10686         i_atttypdefn = PQfnumber(res, "atttypdefn");
10687         i_attlen = PQfnumber(res, "attlen");
10688         i_attalign = PQfnumber(res, "attalign");
10689         i_attisdropped = PQfnumber(res, "attisdropped");
10690         i_attcollation = PQfnumber(res, "attcollation");
10691
10692         if (dopt->binary_upgrade)
10693         {
10694                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10695                                                                                                  tyinfo->dobj.catId.oid);
10696                 binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
10697         }
10698
10699         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10700
10701         appendPQExpBuffer(q, "CREATE TYPE %s AS (",
10702                                           qtypname);
10703
10704         actual_atts = 0;
10705         for (i = 0; i < ntups; i++)
10706         {
10707                 char       *attname;
10708                 char       *atttypdefn;
10709                 char       *attlen;
10710                 char       *attalign;
10711                 bool            attisdropped;
10712                 Oid                     attcollation;
10713
10714                 attname = PQgetvalue(res, i, i_attname);
10715                 atttypdefn = PQgetvalue(res, i, i_atttypdefn);
10716                 attlen = PQgetvalue(res, i, i_attlen);
10717                 attalign = PQgetvalue(res, i, i_attalign);
10718                 attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
10719                 attcollation = atooid(PQgetvalue(res, i, i_attcollation));
10720
10721                 if (attisdropped && !dopt->binary_upgrade)
10722                         continue;
10723
10724                 /* Format properly if not first attr */
10725                 if (actual_atts++ > 0)
10726                         appendPQExpBufferChar(q, ',');
10727                 appendPQExpBufferStr(q, "\n\t");
10728
10729                 if (!attisdropped)
10730                 {
10731                         appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
10732
10733                         /* Add collation if not default for the column type */
10734                         if (OidIsValid(attcollation))
10735                         {
10736                                 CollInfo   *coll;
10737
10738                                 coll = findCollationByOid(attcollation);
10739                                 if (coll)
10740                                 {
10741                                         /* always schema-qualify, don't try to be smart */
10742                                         appendPQExpBuffer(q, " COLLATE %s.",
10743                                                                           fmtId(coll->dobj.namespace->dobj.name));
10744                                         appendPQExpBufferStr(q, fmtId(coll->dobj.name));
10745                                 }
10746                         }
10747                 }
10748                 else
10749                 {
10750                         /*
10751                          * This is a dropped attribute and we're in binary_upgrade mode.
10752                          * Insert a placeholder for it in the CREATE TYPE command, and set
10753                          * length and alignment with direct UPDATE to the catalogs
10754                          * afterwards. See similar code in dumpTableSchema().
10755                          */
10756                         appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
10757
10758                         /* stash separately for insertion after the CREATE TYPE */
10759                         appendPQExpBufferStr(dropped,
10760                                                                  "\n-- For binary upgrade, recreate dropped column.\n");
10761                         appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
10762                                                           "SET attlen = %s, "
10763                                                           "attalign = '%s', attbyval = false\n"
10764                                                           "WHERE attname = ", attlen, attalign);
10765                         appendStringLiteralAH(dropped, attname, fout);
10766                         appendPQExpBufferStr(dropped, "\n  AND attrelid = ");
10767                         appendStringLiteralAH(dropped, qtypname, fout);
10768                         appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
10769
10770                         appendPQExpBuffer(dropped, "ALTER TYPE %s ",
10771                                                           qtypname);
10772                         appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
10773                                                           fmtId(attname));
10774                 }
10775         }
10776         appendPQExpBufferStr(q, "\n);\n");
10777         appendPQExpBufferStr(q, dropped->data);
10778
10779         /*
10780          * DROP must be fully qualified in case same name appears in pg_catalog
10781          */
10782         appendPQExpBuffer(delq, "DROP TYPE %s.",
10783                                           fmtId(tyinfo->dobj.namespace->dobj.name));
10784         appendPQExpBuffer(delq, "%s;\n",
10785                                           qtypname);
10786
10787         appendPQExpBuffer(labelq, "TYPE %s", qtypname);
10788
10789         if (dopt->binary_upgrade)
10790                 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
10791
10792         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10793                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10794                                          tyinfo->dobj.name,
10795                                          tyinfo->dobj.namespace->dobj.name,
10796                                          NULL,
10797                                          tyinfo->rolname, false,
10798                                          "TYPE", SECTION_PRE_DATA,
10799                                          q->data, delq->data, NULL,
10800                                          NULL, 0,
10801                                          NULL, NULL);
10802
10803
10804         /* Dump Type Comments and Security Labels */
10805         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10806                 dumpComment(fout, labelq->data,
10807                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10808                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10809
10810         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10811                 dumpSecLabel(fout, labelq->data,
10812                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10813                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10814
10815         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10816                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10817                                 qtypname, NULL, tyinfo->dobj.name,
10818                                 tyinfo->dobj.namespace->dobj.name,
10819                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10820                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10821
10822         PQclear(res);
10823         destroyPQExpBuffer(q);
10824         destroyPQExpBuffer(dropped);
10825         destroyPQExpBuffer(delq);
10826         destroyPQExpBuffer(labelq);
10827         destroyPQExpBuffer(query);
10828
10829         /* Dump any per-column comments */
10830         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10831                 dumpCompositeTypeColComments(fout, tyinfo);
10832 }
10833
10834 /*
10835  * dumpCompositeTypeColComments
10836  *        writes out to fout the queries to recreate comments on the columns of
10837  *        a user-defined stand-alone composite type
10838  */
10839 static void
10840 dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
10841 {
10842         CommentItem *comments;
10843         int                     ncomments;
10844         PGresult   *res;
10845         PQExpBuffer query;
10846         PQExpBuffer target;
10847         Oid                     pgClassOid;
10848         int                     i;
10849         int                     ntups;
10850         int                     i_attname;
10851         int                     i_attnum;
10852
10853         query = createPQExpBuffer();
10854
10855         appendPQExpBuffer(query,
10856                                           "SELECT c.tableoid, a.attname, a.attnum "
10857                                           "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
10858                                           "WHERE c.oid = '%u' AND c.oid = a.attrelid "
10859                                           "  AND NOT a.attisdropped "
10860                                           "ORDER BY a.attnum ",
10861                                           tyinfo->typrelid);
10862
10863         /* Fetch column attnames */
10864         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10865
10866         ntups = PQntuples(res);
10867         if (ntups < 1)
10868         {
10869                 PQclear(res);
10870                 destroyPQExpBuffer(query);
10871                 return;
10872         }
10873
10874         pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
10875
10876         /* Search for comments associated with type's pg_class OID */
10877         ncomments = findComments(fout,
10878                                                          pgClassOid,
10879                                                          tyinfo->typrelid,
10880                                                          &comments);
10881
10882         /* If no comments exist, we're done */
10883         if (ncomments <= 0)
10884         {
10885                 PQclear(res);
10886                 destroyPQExpBuffer(query);
10887                 return;
10888         }
10889
10890         /* Build COMMENT ON statements */
10891         target = createPQExpBuffer();
10892
10893         i_attnum = PQfnumber(res, "attnum");
10894         i_attname = PQfnumber(res, "attname");
10895         while (ncomments > 0)
10896         {
10897                 const char *attname;
10898
10899                 attname = NULL;
10900                 for (i = 0; i < ntups; i++)
10901                 {
10902                         if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
10903                         {
10904                                 attname = PQgetvalue(res, i, i_attname);
10905                                 break;
10906                         }
10907                 }
10908                 if (attname)                    /* just in case we don't find it */
10909                 {
10910                         const char *descr = comments->descr;
10911
10912                         resetPQExpBuffer(target);
10913                         appendPQExpBuffer(target, "COLUMN %s.",
10914                                                           fmtId(tyinfo->dobj.name));
10915                         appendPQExpBufferStr(target, fmtId(attname));
10916
10917                         resetPQExpBuffer(query);
10918                         appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
10919                         appendStringLiteralAH(query, descr, fout);
10920                         appendPQExpBufferStr(query, ";\n");
10921
10922                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
10923                                                  target->data,
10924                                                  tyinfo->dobj.namespace->dobj.name,
10925                                                  NULL, tyinfo->rolname,
10926                                                  false, "COMMENT", SECTION_NONE,
10927                                                  query->data, "", NULL,
10928                                                  &(tyinfo->dobj.dumpId), 1,
10929                                                  NULL, NULL);
10930                 }
10931
10932                 comments++;
10933                 ncomments--;
10934         }
10935
10936         PQclear(res);
10937         destroyPQExpBuffer(query);
10938         destroyPQExpBuffer(target);
10939 }
10940
10941 /*
10942  * dumpShellType
10943  *        writes out to fout the queries to create a shell type
10944  *
10945  * We dump a shell definition in advance of the I/O functions for the type.
10946  */
10947 static void
10948 dumpShellType(Archive *fout, ShellTypeInfo *stinfo)
10949 {
10950         DumpOptions *dopt = fout->dopt;
10951         PQExpBuffer q;
10952
10953         /* Skip if not to be dumped */
10954         if (!stinfo->dobj.dump || dopt->dataOnly)
10955                 return;
10956
10957         q = createPQExpBuffer();
10958
10959         /*
10960          * Note the lack of a DROP command for the shell type; any required DROP
10961          * is driven off the base type entry, instead.  This interacts with
10962          * _printTocEntry()'s use of the presence of a DROP command to decide
10963          * whether an entry needs an ALTER OWNER command.  We don't want to alter
10964          * the shell type's owner immediately on creation; that should happen only
10965          * after it's filled in, otherwise the backend complains.
10966          */
10967
10968         if (dopt->binary_upgrade)
10969                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10970                                                                                                  stinfo->baseType->dobj.catId.oid);
10971
10972         appendPQExpBuffer(q, "CREATE TYPE %s;\n",
10973                                           fmtId(stinfo->dobj.name));
10974
10975         if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10976                 ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId,
10977                                          stinfo->dobj.name,
10978                                          stinfo->dobj.namespace->dobj.name,
10979                                          NULL,
10980                                          stinfo->baseType->rolname, false,
10981                                          "SHELL TYPE", SECTION_PRE_DATA,
10982                                          q->data, "", NULL,
10983                                          NULL, 0,
10984                                          NULL, NULL);
10985
10986         destroyPQExpBuffer(q);
10987 }
10988
10989 /*
10990  * dumpProcLang
10991  *                writes out to fout the queries to recreate a user-defined
10992  *                procedural language
10993  */
10994 static void
10995 dumpProcLang(Archive *fout, ProcLangInfo *plang)
10996 {
10997         DumpOptions *dopt = fout->dopt;
10998         PQExpBuffer defqry;
10999         PQExpBuffer delqry;
11000         PQExpBuffer labelq;
11001         bool            useParams;
11002         char       *qlanname;
11003         char       *lanschema;
11004         FuncInfo   *funcInfo;
11005         FuncInfo   *inlineInfo = NULL;
11006         FuncInfo   *validatorInfo = NULL;
11007
11008         /* Skip if not to be dumped */
11009         if (!plang->dobj.dump || dopt->dataOnly)
11010                 return;
11011
11012         /*
11013          * Try to find the support function(s).  It is not an error if we don't
11014          * find them --- if the functions are in the pg_catalog schema, as is
11015          * standard in 8.1 and up, then we won't have loaded them. (In this case
11016          * we will emit a parameterless CREATE LANGUAGE command, which will
11017          * require PL template knowledge in the backend to reload.)
11018          */
11019
11020         funcInfo = findFuncByOid(plang->lanplcallfoid);
11021         if (funcInfo != NULL && !funcInfo->dobj.dump)
11022                 funcInfo = NULL;                /* treat not-dumped same as not-found */
11023
11024         if (OidIsValid(plang->laninline))
11025         {
11026                 inlineInfo = findFuncByOid(plang->laninline);
11027                 if (inlineInfo != NULL && !inlineInfo->dobj.dump)
11028                         inlineInfo = NULL;
11029         }
11030
11031         if (OidIsValid(plang->lanvalidator))
11032         {
11033                 validatorInfo = findFuncByOid(plang->lanvalidator);
11034                 if (validatorInfo != NULL && !validatorInfo->dobj.dump)
11035                         validatorInfo = NULL;
11036         }
11037
11038         /*
11039          * If the functions are dumpable then emit a traditional CREATE LANGUAGE
11040          * with parameters.  Otherwise, we'll write a parameterless command, which
11041          * will rely on data from pg_pltemplate.
11042          */
11043         useParams = (funcInfo != NULL &&
11044                                  (inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
11045                                  (validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
11046
11047         defqry = createPQExpBuffer();
11048         delqry = createPQExpBuffer();
11049         labelq = createPQExpBuffer();
11050
11051         qlanname = pg_strdup(fmtId(plang->dobj.name));
11052
11053         /*
11054          * If dumping a HANDLER clause, treat the language as being in the handler
11055          * function's schema; this avoids cluttering the HANDLER clause. Otherwise
11056          * it doesn't really have a schema.
11057          */
11058         if (useParams)
11059                 lanschema = funcInfo->dobj.namespace->dobj.name;
11060         else
11061                 lanschema = NULL;
11062
11063         appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
11064                                           qlanname);
11065
11066         if (useParams)
11067         {
11068                 appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
11069                                                   plang->lanpltrusted ? "TRUSTED " : "",
11070                                                   qlanname);
11071                 appendPQExpBuffer(defqry, " HANDLER %s",
11072                                                   fmtId(funcInfo->dobj.name));
11073                 if (OidIsValid(plang->laninline))
11074                 {
11075                         appendPQExpBufferStr(defqry, " INLINE ");
11076                         /* Cope with possibility that inline is in different schema */
11077                         if (inlineInfo->dobj.namespace != funcInfo->dobj.namespace)
11078                                 appendPQExpBuffer(defqry, "%s.",
11079                                                                   fmtId(inlineInfo->dobj.namespace->dobj.name));
11080                         appendPQExpBufferStr(defqry, fmtId(inlineInfo->dobj.name));
11081                 }
11082                 if (OidIsValid(plang->lanvalidator))
11083                 {
11084                         appendPQExpBufferStr(defqry, " VALIDATOR ");
11085                         /* Cope with possibility that validator is in different schema */
11086                         if (validatorInfo->dobj.namespace != funcInfo->dobj.namespace)
11087                                 appendPQExpBuffer(defqry, "%s.",
11088                                                                   fmtId(validatorInfo->dobj.namespace->dobj.name));
11089                         appendPQExpBufferStr(defqry, fmtId(validatorInfo->dobj.name));
11090                 }
11091         }
11092         else
11093         {
11094                 /*
11095                  * If not dumping parameters, then use CREATE OR REPLACE so that the
11096                  * command will not fail if the language is preinstalled in the target
11097                  * database.  We restrict the use of REPLACE to this case so as to
11098                  * eliminate the risk of replacing a language with incompatible
11099                  * parameter settings: this command will only succeed at all if there
11100                  * is a pg_pltemplate entry, and if there is one, the existing entry
11101                  * must match it too.
11102                  */
11103                 appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s",
11104                                                   qlanname);
11105         }
11106         appendPQExpBufferStr(defqry, ";\n");
11107
11108         appendPQExpBuffer(labelq, "LANGUAGE %s", qlanname);
11109
11110         if (dopt->binary_upgrade)
11111                 binary_upgrade_extension_member(defqry, &plang->dobj, labelq->data);
11112
11113         if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION)
11114                 ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
11115                                          plang->dobj.name,
11116                                          lanschema, NULL, plang->lanowner,
11117                                          false, "PROCEDURAL LANGUAGE", SECTION_PRE_DATA,
11118                                          defqry->data, delqry->data, NULL,
11119                                          NULL, 0,
11120                                          NULL, NULL);
11121
11122         /* Dump Proc Lang Comments and Security Labels */
11123         if (plang->dobj.dump & DUMP_COMPONENT_COMMENT)
11124                 dumpComment(fout, labelq->data,
11125                                         lanschema, plang->lanowner,
11126                                         plang->dobj.catId, 0, plang->dobj.dumpId);
11127
11128         if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL)
11129                 dumpSecLabel(fout, labelq->data,
11130                                          lanschema, plang->lanowner,
11131                                          plang->dobj.catId, 0, plang->dobj.dumpId);
11132
11133         if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL)
11134                 dumpACL(fout, plang->dobj.catId, plang->dobj.dumpId, "LANGUAGE",
11135                                 qlanname, NULL, plang->dobj.name,
11136                                 lanschema,
11137                                 plang->lanowner, plang->lanacl, plang->rlanacl,
11138                                 plang->initlanacl, plang->initrlanacl);
11139
11140         free(qlanname);
11141
11142         destroyPQExpBuffer(defqry);
11143         destroyPQExpBuffer(delqry);
11144         destroyPQExpBuffer(labelq);
11145 }
11146
11147 /*
11148  * format_function_arguments: generate function name and argument list
11149  *
11150  * This is used when we can rely on pg_get_function_arguments to format
11151  * the argument list.  Note, however, that pg_get_function_arguments
11152  * does not special-case zero-argument aggregates.
11153  */
11154 static char *
11155 format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
11156 {
11157         PQExpBufferData fn;
11158
11159         initPQExpBuffer(&fn);
11160         appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name));
11161         if (is_agg && finfo->nargs == 0)
11162                 appendPQExpBufferStr(&fn, "(*)");
11163         else
11164                 appendPQExpBuffer(&fn, "(%s)", funcargs);
11165         return fn.data;
11166 }
11167
11168 /*
11169  * format_function_arguments_old: generate function name and argument list
11170  *
11171  * The argument type names are qualified if needed.  The function name
11172  * is never qualified.
11173  *
11174  * This is used only with pre-8.4 servers, so we aren't expecting to see
11175  * VARIADIC or TABLE arguments, nor are there any defaults for arguments.
11176  *
11177  * Any or all of allargtypes, argmodes, argnames may be NULL.
11178  */
11179 static char *
11180 format_function_arguments_old(Archive *fout,
11181                                                           FuncInfo *finfo, int nallargs,
11182                                                           char **allargtypes,
11183                                                           char **argmodes,
11184                                                           char **argnames)
11185 {
11186         PQExpBufferData fn;
11187         int                     j;
11188
11189         initPQExpBuffer(&fn);
11190         appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
11191         for (j = 0; j < nallargs; j++)
11192         {
11193                 Oid                     typid;
11194                 char       *typname;
11195                 const char *argmode;
11196                 const char *argname;
11197
11198                 typid = allargtypes ? atooid(allargtypes[j]) : finfo->argtypes[j];
11199                 typname = getFormattedTypeName(fout, typid, zeroAsOpaque);
11200
11201                 if (argmodes)
11202                 {
11203                         switch (argmodes[j][0])
11204                         {
11205                                 case PROARGMODE_IN:
11206                                         argmode = "";
11207                                         break;
11208                                 case PROARGMODE_OUT:
11209                                         argmode = "OUT ";
11210                                         break;
11211                                 case PROARGMODE_INOUT:
11212                                         argmode = "INOUT ";
11213                                         break;
11214                                 default:
11215                                         write_msg(NULL, "WARNING: bogus value in proargmodes array\n");
11216                                         argmode = "";
11217                                         break;
11218                         }
11219                 }
11220                 else
11221                         argmode = "";
11222
11223                 argname = argnames ? argnames[j] : (char *) NULL;
11224                 if (argname && argname[0] == '\0')
11225                         argname = NULL;
11226
11227                 appendPQExpBuffer(&fn, "%s%s%s%s%s",
11228                                                   (j > 0) ? ", " : "",
11229                                                   argmode,
11230                                                   argname ? fmtId(argname) : "",
11231                                                   argname ? " " : "",
11232                                                   typname);
11233                 free(typname);
11234         }
11235         appendPQExpBufferChar(&fn, ')');
11236         return fn.data;
11237 }
11238
11239 /*
11240  * format_function_signature: generate function name and argument list
11241  *
11242  * This is like format_function_arguments_old except that only a minimal
11243  * list of input argument types is generated; this is sufficient to
11244  * reference the function, but not to define it.
11245  *
11246  * If honor_quotes is false then the function name is never quoted.
11247  * This is appropriate for use in TOC tags, but not in SQL commands.
11248  */
11249 static char *
11250 format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
11251 {
11252         PQExpBufferData fn;
11253         int                     j;
11254
11255         initPQExpBuffer(&fn);
11256         if (honor_quotes)
11257                 appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
11258         else
11259                 appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
11260         for (j = 0; j < finfo->nargs; j++)
11261         {
11262                 char       *typname;
11263
11264                 if (j > 0)
11265                         appendPQExpBufferStr(&fn, ", ");
11266
11267                 typname = getFormattedTypeName(fout, finfo->argtypes[j],
11268                                                                            zeroAsOpaque);
11269                 appendPQExpBufferStr(&fn, typname);
11270                 free(typname);
11271         }
11272         appendPQExpBufferChar(&fn, ')');
11273         return fn.data;
11274 }
11275
11276
11277 /*
11278  * dumpFunc:
11279  *        dump out one function
11280  */
11281 static void
11282 dumpFunc(Archive *fout, FuncInfo *finfo)
11283 {
11284         DumpOptions *dopt = fout->dopt;
11285         PQExpBuffer query;
11286         PQExpBuffer q;
11287         PQExpBuffer delqry;
11288         PQExpBuffer labelq;
11289         PQExpBuffer asPart;
11290         PGresult   *res;
11291         char       *funcsig;            /* identity signature */
11292         char       *funcfullsig = NULL; /* full signature */
11293         char       *funcsig_tag;
11294         char       *proretset;
11295         char       *prosrc;
11296         char       *probin;
11297         char       *funcargs;
11298         char       *funciargs;
11299         char       *funcresult;
11300         char       *proallargtypes;
11301         char       *proargmodes;
11302         char       *proargnames;
11303         char       *protrftypes;
11304         char       *proiswindow;
11305         char       *provolatile;
11306         char       *proisstrict;
11307         char       *prosecdef;
11308         char       *proleakproof;
11309         char       *proconfig;
11310         char       *procost;
11311         char       *prorows;
11312         char       *proparallel;
11313         char       *lanname;
11314         char       *rettypename;
11315         int                     nallargs;
11316         char      **allargtypes = NULL;
11317         char      **argmodes = NULL;
11318         char      **argnames = NULL;
11319         char      **configitems = NULL;
11320         int                     nconfigitems = 0;
11321         int                     i;
11322
11323         /* Skip if not to be dumped */
11324         if (!finfo->dobj.dump || dopt->dataOnly)
11325                 return;
11326
11327         query = createPQExpBuffer();
11328         q = createPQExpBuffer();
11329         delqry = createPQExpBuffer();
11330         labelq = createPQExpBuffer();
11331         asPart = createPQExpBuffer();
11332
11333         /* Set proper schema search path so type references list correctly */
11334         selectSourceSchema(fout, finfo->dobj.namespace->dobj.name);
11335
11336         /* Fetch function-specific details */
11337         if (fout->remoteVersion >= 90600)
11338         {
11339                 /*
11340                  * proparallel was added in 9.6
11341                  */
11342                 appendPQExpBuffer(query,
11343                                                   "SELECT proretset, prosrc, probin, "
11344                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11345                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11346                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11347                                                   "array_to_string(protrftypes, ' ') AS protrftypes, "
11348                                                   "proiswindow, provolatile, proisstrict, prosecdef, "
11349                                                   "proleakproof, proconfig, procost, prorows, "
11350                                                   "proparallel, "
11351                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11352                                                   "FROM pg_catalog.pg_proc "
11353                                                   "WHERE oid = '%u'::pg_catalog.oid",
11354                                                   finfo->dobj.catId.oid);
11355         }
11356         else if (fout->remoteVersion >= 90500)
11357         {
11358                 /*
11359                  * protrftypes was added in 9.5
11360                  */
11361                 appendPQExpBuffer(query,
11362                                                   "SELECT proretset, prosrc, probin, "
11363                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11364                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11365                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11366                                                   "array_to_string(protrftypes, ' ') AS protrftypes, "
11367                                                   "proiswindow, provolatile, proisstrict, prosecdef, "
11368                                                   "proleakproof, proconfig, procost, prorows, "
11369                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11370                                                   "FROM pg_catalog.pg_proc "
11371                                                   "WHERE oid = '%u'::pg_catalog.oid",
11372                                                   finfo->dobj.catId.oid);
11373         }
11374         else if (fout->remoteVersion >= 90200)
11375         {
11376                 /*
11377                  * proleakproof was added in 9.2
11378                  */
11379                 appendPQExpBuffer(query,
11380                                                   "SELECT proretset, prosrc, probin, "
11381                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11382                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11383                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11384                                                   "proiswindow, provolatile, proisstrict, prosecdef, "
11385                                                   "proleakproof, proconfig, procost, prorows, "
11386                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11387                                                   "FROM pg_catalog.pg_proc "
11388                                                   "WHERE oid = '%u'::pg_catalog.oid",
11389                                                   finfo->dobj.catId.oid);
11390         }
11391         else if (fout->remoteVersion >= 80400)
11392         {
11393                 /*
11394                  * In 8.4 and up we rely on pg_get_function_arguments and
11395                  * pg_get_function_result instead of examining proallargtypes etc.
11396                  */
11397                 appendPQExpBuffer(query,
11398                                                   "SELECT proretset, prosrc, probin, "
11399                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11400                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11401                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11402                                                   "proiswindow, provolatile, proisstrict, prosecdef, "
11403                                                   "false AS proleakproof, "
11404                                                   " proconfig, procost, prorows, "
11405                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11406                                                   "FROM pg_catalog.pg_proc "
11407                                                   "WHERE oid = '%u'::pg_catalog.oid",
11408                                                   finfo->dobj.catId.oid);
11409         }
11410         else if (fout->remoteVersion >= 80300)
11411         {
11412                 appendPQExpBuffer(query,
11413                                                   "SELECT proretset, prosrc, probin, "
11414                                                   "proallargtypes, proargmodes, proargnames, "
11415                                                   "false AS proiswindow, "
11416                                                   "provolatile, proisstrict, prosecdef, "
11417                                                   "false AS proleakproof, "
11418                                                   "proconfig, procost, prorows, "
11419                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11420                                                   "FROM pg_catalog.pg_proc "
11421                                                   "WHERE oid = '%u'::pg_catalog.oid",
11422                                                   finfo->dobj.catId.oid);
11423         }
11424         else if (fout->remoteVersion >= 80100)
11425         {
11426                 appendPQExpBuffer(query,
11427                                                   "SELECT proretset, prosrc, probin, "
11428                                                   "proallargtypes, proargmodes, proargnames, "
11429                                                   "false AS proiswindow, "
11430                                                   "provolatile, proisstrict, prosecdef, "
11431                                                   "false AS proleakproof, "
11432                                                   "null AS proconfig, 0 AS procost, 0 AS prorows, "
11433                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11434                                                   "FROM pg_catalog.pg_proc "
11435                                                   "WHERE oid = '%u'::pg_catalog.oid",
11436                                                   finfo->dobj.catId.oid);
11437         }
11438         else
11439         {
11440                 appendPQExpBuffer(query,
11441                                                   "SELECT proretset, prosrc, probin, "
11442                                                   "null AS proallargtypes, "
11443                                                   "null AS proargmodes, "
11444                                                   "proargnames, "
11445                                                   "false AS proiswindow, "
11446                                                   "provolatile, proisstrict, prosecdef, "
11447                                                   "false AS proleakproof, "
11448                                                   "null AS proconfig, 0 AS procost, 0 AS prorows, "
11449                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11450                                                   "FROM pg_catalog.pg_proc "
11451                                                   "WHERE oid = '%u'::pg_catalog.oid",
11452                                                   finfo->dobj.catId.oid);
11453         }
11454
11455         res = ExecuteSqlQueryForSingleRow(fout, query->data);
11456
11457         proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
11458         prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
11459         probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
11460         if (fout->remoteVersion >= 80400)
11461         {
11462                 funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
11463                 funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
11464                 funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult"));
11465                 proallargtypes = proargmodes = proargnames = NULL;
11466         }
11467         else
11468         {
11469                 proallargtypes = PQgetvalue(res, 0, PQfnumber(res, "proallargtypes"));
11470                 proargmodes = PQgetvalue(res, 0, PQfnumber(res, "proargmodes"));
11471                 proargnames = PQgetvalue(res, 0, PQfnumber(res, "proargnames"));
11472                 funcargs = funciargs = funcresult = NULL;
11473         }
11474         if (PQfnumber(res, "protrftypes") != -1)
11475                 protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes"));
11476         else
11477                 protrftypes = NULL;
11478         proiswindow = PQgetvalue(res, 0, PQfnumber(res, "proiswindow"));
11479         provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
11480         proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
11481         prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
11482         proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof"));
11483         proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig"));
11484         procost = PQgetvalue(res, 0, PQfnumber(res, "procost"));
11485         prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows"));
11486
11487         if (PQfnumber(res, "proparallel") != -1)
11488                 proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
11489         else
11490                 proparallel = NULL;
11491
11492         lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
11493
11494         /*
11495          * See backend/commands/functioncmds.c for details of how the 'AS' clause
11496          * is used.  In 8.4 and up, an unused probin is NULL (here ""); previous
11497          * versions would set it to "-".  There are no known cases in which prosrc
11498          * is unused, so the tests below for "-" are probably useless.
11499          */
11500         if (probin[0] != '\0' && strcmp(probin, "-") != 0)
11501         {
11502                 appendPQExpBufferStr(asPart, "AS ");
11503                 appendStringLiteralAH(asPart, probin, fout);
11504                 if (strcmp(prosrc, "-") != 0)
11505                 {
11506                         appendPQExpBufferStr(asPart, ", ");
11507
11508                         /*
11509                          * where we have bin, use dollar quoting if allowed and src
11510                          * contains quote or backslash; else use regular quoting.
11511                          */
11512                         if (dopt->disable_dollar_quoting ||
11513                                 (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL))
11514                                 appendStringLiteralAH(asPart, prosrc, fout);
11515                         else
11516                                 appendStringLiteralDQ(asPart, prosrc, NULL);
11517                 }
11518         }
11519         else
11520         {
11521                 if (strcmp(prosrc, "-") != 0)
11522                 {
11523                         appendPQExpBufferStr(asPart, "AS ");
11524                         /* with no bin, dollar quote src unconditionally if allowed */
11525                         if (dopt->disable_dollar_quoting)
11526                                 appendStringLiteralAH(asPart, prosrc, fout);
11527                         else
11528                                 appendStringLiteralDQ(asPart, prosrc, NULL);
11529                 }
11530         }
11531
11532         nallargs = finfo->nargs;        /* unless we learn different from allargs */
11533
11534         if (proallargtypes && *proallargtypes)
11535         {
11536                 int                     nitems = 0;
11537
11538                 if (!parsePGArray(proallargtypes, &allargtypes, &nitems) ||
11539                         nitems < finfo->nargs)
11540                 {
11541                         write_msg(NULL, "WARNING: could not parse proallargtypes array\n");
11542                         if (allargtypes)
11543                                 free(allargtypes);
11544                         allargtypes = NULL;
11545                 }
11546                 else
11547                         nallargs = nitems;
11548         }
11549
11550         if (proargmodes && *proargmodes)
11551         {
11552                 int                     nitems = 0;
11553
11554                 if (!parsePGArray(proargmodes, &argmodes, &nitems) ||
11555                         nitems != nallargs)
11556                 {
11557                         write_msg(NULL, "WARNING: could not parse proargmodes array\n");
11558                         if (argmodes)
11559                                 free(argmodes);
11560                         argmodes = NULL;
11561                 }
11562         }
11563
11564         if (proargnames && *proargnames)
11565         {
11566                 int                     nitems = 0;
11567
11568                 if (!parsePGArray(proargnames, &argnames, &nitems) ||
11569                         nitems != nallargs)
11570                 {
11571                         write_msg(NULL, "WARNING: could not parse proargnames array\n");
11572                         if (argnames)
11573                                 free(argnames);
11574                         argnames = NULL;
11575                 }
11576         }
11577
11578         if (proconfig && *proconfig)
11579         {
11580                 if (!parsePGArray(proconfig, &configitems, &nconfigitems))
11581                 {
11582                         write_msg(NULL, "WARNING: could not parse proconfig array\n");
11583                         if (configitems)
11584                                 free(configitems);
11585                         configitems = NULL;
11586                         nconfigitems = 0;
11587                 }
11588         }
11589
11590         if (funcargs)
11591         {
11592                 /* 8.4 or later; we rely on server-side code for most of the work */
11593                 funcfullsig = format_function_arguments(finfo, funcargs, false);
11594                 funcsig = format_function_arguments(finfo, funciargs, false);
11595         }
11596         else
11597                 /* pre-8.4, do it ourselves */
11598                 funcsig = format_function_arguments_old(fout,
11599                                                                                                 finfo, nallargs, allargtypes,
11600                                                                                                 argmodes, argnames);
11601
11602         funcsig_tag = format_function_signature(fout, finfo, false);
11603
11604         /*
11605          * DROP must be fully qualified in case same name appears in pg_catalog
11606          */
11607         appendPQExpBuffer(delqry, "DROP FUNCTION %s.%s;\n",
11608                                           fmtId(finfo->dobj.namespace->dobj.name),
11609                                           funcsig);
11610
11611         appendPQExpBuffer(q, "CREATE FUNCTION %s ", funcfullsig ? funcfullsig :
11612                                           funcsig);
11613         if (funcresult)
11614                 appendPQExpBuffer(q, "RETURNS %s", funcresult);
11615         else
11616         {
11617                 rettypename = getFormattedTypeName(fout, finfo->prorettype,
11618                                                                                    zeroAsOpaque);
11619                 appendPQExpBuffer(q, "RETURNS %s%s",
11620                                                   (proretset[0] == 't') ? "SETOF " : "",
11621                                                   rettypename);
11622                 free(rettypename);
11623         }
11624
11625         appendPQExpBuffer(q, "\n    LANGUAGE %s", fmtId(lanname));
11626
11627         if (protrftypes != NULL && strcmp(protrftypes, "") != 0)
11628         {
11629                 Oid                *typeids = palloc(FUNC_MAX_ARGS * sizeof(Oid));
11630                 int                     i;
11631
11632                 appendPQExpBufferStr(q, " TRANSFORM ");
11633                 parseOidArray(protrftypes, typeids, FUNC_MAX_ARGS);
11634                 for (i = 0; typeids[i]; i++)
11635                 {
11636                         if (i != 0)
11637                                 appendPQExpBufferStr(q, ", ");
11638                         appendPQExpBuffer(q, "FOR TYPE %s",
11639                                                           getFormattedTypeName(fout, typeids[i], zeroAsNone));
11640                 }
11641         }
11642
11643         if (proiswindow[0] == 't')
11644                 appendPQExpBufferStr(q, " WINDOW");
11645
11646         if (provolatile[0] != PROVOLATILE_VOLATILE)
11647         {
11648                 if (provolatile[0] == PROVOLATILE_IMMUTABLE)
11649                         appendPQExpBufferStr(q, " IMMUTABLE");
11650                 else if (provolatile[0] == PROVOLATILE_STABLE)
11651                         appendPQExpBufferStr(q, " STABLE");
11652                 else if (provolatile[0] != PROVOLATILE_VOLATILE)
11653                         exit_horribly(NULL, "unrecognized provolatile value for function \"%s\"\n",
11654                                                   finfo->dobj.name);
11655         }
11656
11657         if (proisstrict[0] == 't')
11658                 appendPQExpBufferStr(q, " STRICT");
11659
11660         if (prosecdef[0] == 't')
11661                 appendPQExpBufferStr(q, " SECURITY DEFINER");
11662
11663         if (proleakproof[0] == 't')
11664                 appendPQExpBufferStr(q, " LEAKPROOF");
11665
11666         /*
11667          * COST and ROWS are emitted only if present and not default, so as not to
11668          * break backwards-compatibility of the dump without need.  Keep this code
11669          * in sync with the defaults in functioncmds.c.
11670          */
11671         if (strcmp(procost, "0") != 0)
11672         {
11673                 if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0)
11674                 {
11675                         /* default cost is 1 */
11676                         if (strcmp(procost, "1") != 0)
11677                                 appendPQExpBuffer(q, " COST %s", procost);
11678                 }
11679                 else
11680                 {
11681                         /* default cost is 100 */
11682                         if (strcmp(procost, "100") != 0)
11683                                 appendPQExpBuffer(q, " COST %s", procost);
11684                 }
11685         }
11686         if (proretset[0] == 't' &&
11687                 strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0)
11688                 appendPQExpBuffer(q, " ROWS %s", prorows);
11689
11690         if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
11691         {
11692                 if (proparallel[0] == PROPARALLEL_SAFE)
11693                         appendPQExpBufferStr(q, " PARALLEL SAFE");
11694                 else if (proparallel[0] == PROPARALLEL_RESTRICTED)
11695                         appendPQExpBufferStr(q, " PARALLEL RESTRICTED");
11696                 else if (proparallel[0] != PROPARALLEL_UNSAFE)
11697                         exit_horribly(NULL, "unrecognized proparallel value for function \"%s\"\n",
11698                                                   finfo->dobj.name);
11699         }
11700
11701         for (i = 0; i < nconfigitems; i++)
11702         {
11703                 /* we feel free to scribble on configitems[] here */
11704                 char       *configitem = configitems[i];
11705                 char       *pos;
11706
11707                 pos = strchr(configitem, '=');
11708                 if (pos == NULL)
11709                         continue;
11710                 *pos++ = '\0';
11711                 appendPQExpBuffer(q, "\n    SET %s TO ", fmtId(configitem));
11712
11713                 /*
11714                  * Some GUC variable names are 'LIST' type and hence must not be
11715                  * quoted.
11716                  */
11717                 if (pg_strcasecmp(configitem, "DateStyle") == 0
11718                         || pg_strcasecmp(configitem, "search_path") == 0)
11719                         appendPQExpBufferStr(q, pos);
11720                 else
11721                         appendStringLiteralAH(q, pos, fout);
11722         }
11723
11724         appendPQExpBuffer(q, "\n    %s;\n", asPart->data);
11725
11726         appendPQExpBuffer(labelq, "FUNCTION %s", funcsig);
11727
11728         if (dopt->binary_upgrade)
11729                 binary_upgrade_extension_member(q, &finfo->dobj, labelq->data);
11730
11731         if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11732                 ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
11733                                          funcsig_tag,
11734                                          finfo->dobj.namespace->dobj.name,
11735                                          NULL,
11736                                          finfo->rolname, false,
11737                                          "FUNCTION", SECTION_PRE_DATA,
11738                                          q->data, delqry->data, NULL,
11739                                          NULL, 0,
11740                                          NULL, NULL);
11741
11742         /* Dump Function Comments and Security Labels */
11743         if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11744                 dumpComment(fout, labelq->data,
11745                                         finfo->dobj.namespace->dobj.name, finfo->rolname,
11746                                         finfo->dobj.catId, 0, finfo->dobj.dumpId);
11747
11748         if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11749                 dumpSecLabel(fout, labelq->data,
11750                                          finfo->dobj.namespace->dobj.name, finfo->rolname,
11751                                          finfo->dobj.catId, 0, finfo->dobj.dumpId);
11752
11753         if (finfo->dobj.dump & DUMP_COMPONENT_ACL)
11754                 dumpACL(fout, finfo->dobj.catId, finfo->dobj.dumpId, "FUNCTION",
11755                                 funcsig, NULL, funcsig_tag,
11756                                 finfo->dobj.namespace->dobj.name,
11757                                 finfo->rolname, finfo->proacl, finfo->rproacl,
11758                                 finfo->initproacl, finfo->initrproacl);
11759
11760         PQclear(res);
11761
11762         destroyPQExpBuffer(query);
11763         destroyPQExpBuffer(q);
11764         destroyPQExpBuffer(delqry);
11765         destroyPQExpBuffer(labelq);
11766         destroyPQExpBuffer(asPart);
11767         free(funcsig);
11768         if (funcfullsig)
11769                 free(funcfullsig);
11770         free(funcsig_tag);
11771         if (allargtypes)
11772                 free(allargtypes);
11773         if (argmodes)
11774                 free(argmodes);
11775         if (argnames)
11776                 free(argnames);
11777         if (configitems)
11778                 free(configitems);
11779 }
11780
11781
11782 /*
11783  * Dump a user-defined cast
11784  */
11785 static void
11786 dumpCast(Archive *fout, CastInfo *cast)
11787 {
11788         DumpOptions *dopt = fout->dopt;
11789         PQExpBuffer defqry;
11790         PQExpBuffer delqry;
11791         PQExpBuffer labelq;
11792         FuncInfo   *funcInfo = NULL;
11793         char       *sourceType;
11794         char       *targetType;
11795
11796         /* Skip if not to be dumped */
11797         if (!cast->dobj.dump || dopt->dataOnly)
11798                 return;
11799
11800         /* Cannot dump if we don't have the cast function's info */
11801         if (OidIsValid(cast->castfunc))
11802         {
11803                 funcInfo = findFuncByOid(cast->castfunc);
11804                 if (funcInfo == NULL)
11805                         exit_horribly(NULL, "could not find function definition for function with OID %u\n",
11806                                                   cast->castfunc);
11807         }
11808
11809         /*
11810          * Make sure we are in proper schema (needed for getFormattedTypeName).
11811          * Casts don't have a schema of their own, so use pg_catalog.
11812          */
11813         selectSourceSchema(fout, "pg_catalog");
11814
11815         defqry = createPQExpBuffer();
11816         delqry = createPQExpBuffer();
11817         labelq = createPQExpBuffer();
11818
11819         sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
11820         targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
11821         appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
11822                                           sourceType, targetType);
11823
11824         appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
11825                                           sourceType, targetType);
11826
11827         switch (cast->castmethod)
11828         {
11829                 case COERCION_METHOD_BINARY:
11830                         appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
11831                         break;
11832                 case COERCION_METHOD_INOUT:
11833                         appendPQExpBufferStr(defqry, "WITH INOUT");
11834                         break;
11835                 case COERCION_METHOD_FUNCTION:
11836                         if (funcInfo)
11837                         {
11838                                 char       *fsig = format_function_signature(fout, funcInfo, true);
11839
11840                                 /*
11841                                  * Always qualify the function name, in case it is not in
11842                                  * pg_catalog schema (format_function_signature won't qualify
11843                                  * it).
11844                                  */
11845                                 appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
11846                                                                   fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
11847                                 free(fsig);
11848                         }
11849                         else
11850                                 write_msg(NULL, "WARNING: bogus value in pg_cast.castfunc or pg_cast.castmethod field\n");
11851                         break;
11852                 default:
11853                         write_msg(NULL, "WARNING: bogus value in pg_cast.castmethod field\n");
11854         }
11855
11856         if (cast->castcontext == 'a')
11857                 appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
11858         else if (cast->castcontext == 'i')
11859                 appendPQExpBufferStr(defqry, " AS IMPLICIT");
11860         appendPQExpBufferStr(defqry, ";\n");
11861
11862         appendPQExpBuffer(labelq, "CAST (%s AS %s)",
11863                                           sourceType, targetType);
11864
11865         if (dopt->binary_upgrade)
11866                 binary_upgrade_extension_member(defqry, &cast->dobj, labelq->data);
11867
11868         if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
11869                 ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
11870                                          labelq->data,
11871                                          "pg_catalog", NULL, "",
11872                                          false, "CAST", SECTION_PRE_DATA,
11873                                          defqry->data, delqry->data, NULL,
11874                                          NULL, 0,
11875                                          NULL, NULL);
11876
11877         /* Dump Cast Comments */
11878         if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
11879                 dumpComment(fout, labelq->data,
11880                                         "pg_catalog", "",
11881                                         cast->dobj.catId, 0, cast->dobj.dumpId);
11882
11883         free(sourceType);
11884         free(targetType);
11885
11886         destroyPQExpBuffer(defqry);
11887         destroyPQExpBuffer(delqry);
11888         destroyPQExpBuffer(labelq);
11889 }
11890
11891 /*
11892  * Dump a transform
11893  */
11894 static void
11895 dumpTransform(Archive *fout, TransformInfo *transform)
11896 {
11897         DumpOptions *dopt = fout->dopt;
11898         PQExpBuffer defqry;
11899         PQExpBuffer delqry;
11900         PQExpBuffer labelq;
11901         FuncInfo   *fromsqlFuncInfo = NULL;
11902         FuncInfo   *tosqlFuncInfo = NULL;
11903         char       *lanname;
11904         char       *transformType;
11905
11906         /* Skip if not to be dumped */
11907         if (!transform->dobj.dump || dopt->dataOnly)
11908                 return;
11909
11910         /* Cannot dump if we don't have the transform functions' info */
11911         if (OidIsValid(transform->trffromsql))
11912         {
11913                 fromsqlFuncInfo = findFuncByOid(transform->trffromsql);
11914                 if (fromsqlFuncInfo == NULL)
11915                         exit_horribly(NULL, "could not find function definition for function with OID %u\n",
11916                                                   transform->trffromsql);
11917         }
11918         if (OidIsValid(transform->trftosql))
11919         {
11920                 tosqlFuncInfo = findFuncByOid(transform->trftosql);
11921                 if (tosqlFuncInfo == NULL)
11922                         exit_horribly(NULL, "could not find function definition for function with OID %u\n",
11923                                                   transform->trftosql);
11924         }
11925
11926         /* Make sure we are in proper schema (needed for getFormattedTypeName) */
11927         selectSourceSchema(fout, "pg_catalog");
11928
11929         defqry = createPQExpBuffer();
11930         delqry = createPQExpBuffer();
11931         labelq = createPQExpBuffer();
11932
11933         lanname = get_language_name(fout, transform->trflang);
11934         transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone);
11935
11936         appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n",
11937                                           transformType, lanname);
11938
11939         appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (",
11940                                           transformType, lanname);
11941
11942         if (!transform->trffromsql && !transform->trftosql)
11943                 write_msg(NULL, "WARNING: bogus transform definition, at least one of trffromsql and trftosql should be nonzero\n");
11944
11945         if (transform->trffromsql)
11946         {
11947                 if (fromsqlFuncInfo)
11948                 {
11949                         char       *fsig = format_function_signature(fout, fromsqlFuncInfo, true);
11950
11951                         /*
11952                          * Always qualify the function name, in case it is not in
11953                          * pg_catalog schema (format_function_signature won't qualify it).
11954                          */
11955                         appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s",
11956                                                           fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig);
11957                         free(fsig);
11958                 }
11959                 else
11960                         write_msg(NULL, "WARNING: bogus value in pg_transform.trffromsql field\n");
11961         }
11962
11963         if (transform->trftosql)
11964         {
11965                 if (transform->trffromsql)
11966                         appendPQExpBuffer(defqry, ", ");
11967
11968                 if (tosqlFuncInfo)
11969                 {
11970                         char       *fsig = format_function_signature(fout, tosqlFuncInfo, true);
11971
11972                         /*
11973                          * Always qualify the function name, in case it is not in
11974                          * pg_catalog schema (format_function_signature won't qualify it).
11975                          */
11976                         appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s",
11977                                                           fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig);
11978                         free(fsig);
11979                 }
11980                 else
11981                         write_msg(NULL, "WARNING: bogus value in pg_transform.trftosql field\n");
11982         }
11983
11984         appendPQExpBuffer(defqry, ");\n");
11985
11986         appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s",
11987                                           transformType, lanname);
11988
11989         if (dopt->binary_upgrade)
11990                 binary_upgrade_extension_member(defqry, &transform->dobj, labelq->data);
11991
11992         if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION)
11993                 ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId,
11994                                          labelq->data,
11995                                          "pg_catalog", NULL, "",
11996                                          false, "TRANSFORM", SECTION_PRE_DATA,
11997                                          defqry->data, delqry->data, NULL,
11998                                          transform->dobj.dependencies, transform->dobj.nDeps,
11999                                          NULL, NULL);
12000
12001         /* Dump Transform Comments */
12002         if (transform->dobj.dump & DUMP_COMPONENT_COMMENT)
12003                 dumpComment(fout, labelq->data,
12004                                         "pg_catalog", "",
12005                                         transform->dobj.catId, 0, transform->dobj.dumpId);
12006
12007         free(lanname);
12008         free(transformType);
12009         destroyPQExpBuffer(defqry);
12010         destroyPQExpBuffer(delqry);
12011         destroyPQExpBuffer(labelq);
12012 }
12013
12014
12015 /*
12016  * dumpOpr
12017  *        write out a single operator definition
12018  */
12019 static void
12020 dumpOpr(Archive *fout, OprInfo *oprinfo)
12021 {
12022         DumpOptions *dopt = fout->dopt;
12023         PQExpBuffer query;
12024         PQExpBuffer q;
12025         PQExpBuffer delq;
12026         PQExpBuffer labelq;
12027         PQExpBuffer oprid;
12028         PQExpBuffer details;
12029         PGresult   *res;
12030         int                     i_oprkind;
12031         int                     i_oprcode;
12032         int                     i_oprleft;
12033         int                     i_oprright;
12034         int                     i_oprcom;
12035         int                     i_oprnegate;
12036         int                     i_oprrest;
12037         int                     i_oprjoin;
12038         int                     i_oprcanmerge;
12039         int                     i_oprcanhash;
12040         char       *oprkind;
12041         char       *oprcode;
12042         char       *oprleft;
12043         char       *oprright;
12044         char       *oprcom;
12045         char       *oprnegate;
12046         char       *oprrest;
12047         char       *oprjoin;
12048         char       *oprcanmerge;
12049         char       *oprcanhash;
12050         char       *oprregproc;
12051         char       *oprref;
12052
12053         /* Skip if not to be dumped */
12054         if (!oprinfo->dobj.dump || dopt->dataOnly)
12055                 return;
12056
12057         /*
12058          * some operators are invalid because they were the result of user
12059          * defining operators before commutators exist
12060          */
12061         if (!OidIsValid(oprinfo->oprcode))
12062                 return;
12063
12064         query = createPQExpBuffer();
12065         q = createPQExpBuffer();
12066         delq = createPQExpBuffer();
12067         labelq = createPQExpBuffer();
12068         oprid = createPQExpBuffer();
12069         details = createPQExpBuffer();
12070
12071         /* Make sure we are in proper schema so regoperator works correctly */
12072         selectSourceSchema(fout, oprinfo->dobj.namespace->dobj.name);
12073
12074         if (fout->remoteVersion >= 80300)
12075         {
12076                 appendPQExpBuffer(query, "SELECT oprkind, "
12077                                                   "oprcode::pg_catalog.regprocedure, "
12078                                                   "oprleft::pg_catalog.regtype, "
12079                                                   "oprright::pg_catalog.regtype, "
12080                                                   "oprcom::pg_catalog.regoperator, "
12081                                                   "oprnegate::pg_catalog.regoperator, "
12082                                                   "oprrest::pg_catalog.regprocedure, "
12083                                                   "oprjoin::pg_catalog.regprocedure, "
12084                                                   "oprcanmerge, oprcanhash "
12085                                                   "FROM pg_catalog.pg_operator "
12086                                                   "WHERE oid = '%u'::pg_catalog.oid",
12087                                                   oprinfo->dobj.catId.oid);
12088         }
12089         else
12090         {
12091                 appendPQExpBuffer(query, "SELECT oprkind, "
12092                                                   "oprcode::pg_catalog.regprocedure, "
12093                                                   "oprleft::pg_catalog.regtype, "
12094                                                   "oprright::pg_catalog.regtype, "
12095                                                   "oprcom::pg_catalog.regoperator, "
12096                                                   "oprnegate::pg_catalog.regoperator, "
12097                                                   "oprrest::pg_catalog.regprocedure, "
12098                                                   "oprjoin::pg_catalog.regprocedure, "
12099                                                   "(oprlsortop != 0) AS oprcanmerge, "
12100                                                   "oprcanhash "
12101                                                   "FROM pg_catalog.pg_operator "
12102                                                   "WHERE oid = '%u'::pg_catalog.oid",
12103                                                   oprinfo->dobj.catId.oid);
12104         }
12105
12106         res = ExecuteSqlQueryForSingleRow(fout, query->data);
12107
12108         i_oprkind = PQfnumber(res, "oprkind");
12109         i_oprcode = PQfnumber(res, "oprcode");
12110         i_oprleft = PQfnumber(res, "oprleft");
12111         i_oprright = PQfnumber(res, "oprright");
12112         i_oprcom = PQfnumber(res, "oprcom");
12113         i_oprnegate = PQfnumber(res, "oprnegate");
12114         i_oprrest = PQfnumber(res, "oprrest");
12115         i_oprjoin = PQfnumber(res, "oprjoin");
12116         i_oprcanmerge = PQfnumber(res, "oprcanmerge");
12117         i_oprcanhash = PQfnumber(res, "oprcanhash");
12118
12119         oprkind = PQgetvalue(res, 0, i_oprkind);
12120         oprcode = PQgetvalue(res, 0, i_oprcode);
12121         oprleft = PQgetvalue(res, 0, i_oprleft);
12122         oprright = PQgetvalue(res, 0, i_oprright);
12123         oprcom = PQgetvalue(res, 0, i_oprcom);
12124         oprnegate = PQgetvalue(res, 0, i_oprnegate);
12125         oprrest = PQgetvalue(res, 0, i_oprrest);
12126         oprjoin = PQgetvalue(res, 0, i_oprjoin);
12127         oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
12128         oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
12129
12130         oprregproc = convertRegProcReference(fout, oprcode);
12131         if (oprregproc)
12132         {
12133                 appendPQExpBuffer(details, "    PROCEDURE = %s", oprregproc);
12134                 free(oprregproc);
12135         }
12136
12137         appendPQExpBuffer(oprid, "%s (",
12138                                           oprinfo->dobj.name);
12139
12140         /*
12141          * right unary means there's a left arg and left unary means there's a
12142          * right arg
12143          */
12144         if (strcmp(oprkind, "r") == 0 ||
12145                 strcmp(oprkind, "b") == 0)
12146         {
12147                 appendPQExpBuffer(details, ",\n    LEFTARG = %s", oprleft);
12148                 appendPQExpBufferStr(oprid, oprleft);
12149         }
12150         else
12151                 appendPQExpBufferStr(oprid, "NONE");
12152
12153         if (strcmp(oprkind, "l") == 0 ||
12154                 strcmp(oprkind, "b") == 0)
12155         {
12156                 appendPQExpBuffer(details, ",\n    RIGHTARG = %s", oprright);
12157                 appendPQExpBuffer(oprid, ", %s)", oprright);
12158         }
12159         else
12160                 appendPQExpBufferStr(oprid, ", NONE)");
12161
12162         oprref = convertOperatorReference(fout, oprcom);
12163         if (oprref)
12164         {
12165                 appendPQExpBuffer(details, ",\n    COMMUTATOR = %s", oprref);
12166                 free(oprref);
12167         }
12168
12169         oprref = convertOperatorReference(fout, oprnegate);
12170         if (oprref)
12171         {
12172                 appendPQExpBuffer(details, ",\n    NEGATOR = %s", oprref);
12173                 free(oprref);
12174         }
12175
12176         if (strcmp(oprcanmerge, "t") == 0)
12177                 appendPQExpBufferStr(details, ",\n    MERGES");
12178
12179         if (strcmp(oprcanhash, "t") == 0)
12180                 appendPQExpBufferStr(details, ",\n    HASHES");
12181
12182         oprregproc = convertRegProcReference(fout, oprrest);
12183         if (oprregproc)
12184         {
12185                 appendPQExpBuffer(details, ",\n    RESTRICT = %s", oprregproc);
12186                 free(oprregproc);
12187         }
12188
12189         oprregproc = convertRegProcReference(fout, oprjoin);
12190         if (oprregproc)
12191         {
12192                 appendPQExpBuffer(details, ",\n    JOIN = %s", oprregproc);
12193                 free(oprregproc);
12194         }
12195
12196         /*
12197          * DROP must be fully qualified in case same name appears in pg_catalog
12198          */
12199         appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
12200                                           fmtId(oprinfo->dobj.namespace->dobj.name),
12201                                           oprid->data);
12202
12203         appendPQExpBuffer(q, "CREATE OPERATOR %s (\n%s\n);\n",
12204                                           oprinfo->dobj.name, details->data);
12205
12206         appendPQExpBuffer(labelq, "OPERATOR %s", oprid->data);
12207
12208         if (dopt->binary_upgrade)
12209                 binary_upgrade_extension_member(q, &oprinfo->dobj, labelq->data);
12210
12211         if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12212                 ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
12213                                          oprinfo->dobj.name,
12214                                          oprinfo->dobj.namespace->dobj.name,
12215                                          NULL,
12216                                          oprinfo->rolname,
12217                                          false, "OPERATOR", SECTION_PRE_DATA,
12218                                          q->data, delq->data, NULL,
12219                                          NULL, 0,
12220                                          NULL, NULL);
12221
12222         /* Dump Operator Comments */
12223         if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12224                 dumpComment(fout, labelq->data,
12225                                         oprinfo->dobj.namespace->dobj.name, oprinfo->rolname,
12226                                         oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
12227
12228         PQclear(res);
12229
12230         destroyPQExpBuffer(query);
12231         destroyPQExpBuffer(q);
12232         destroyPQExpBuffer(delq);
12233         destroyPQExpBuffer(labelq);
12234         destroyPQExpBuffer(oprid);
12235         destroyPQExpBuffer(details);
12236 }
12237
12238 /*
12239  * Convert a function reference obtained from pg_operator
12240  *
12241  * Returns allocated string of what to print, or NULL if function references
12242  * is InvalidOid. Returned string is expected to be free'd by the caller.
12243  *
12244  * The input is a REGPROCEDURE display; we have to strip the argument-types
12245  * part.
12246  */
12247 static char *
12248 convertRegProcReference(Archive *fout, const char *proc)
12249 {
12250         char       *name;
12251         char       *paren;
12252         bool            inquote;
12253
12254         /* In all cases "-" means a null reference */
12255         if (strcmp(proc, "-") == 0)
12256                 return NULL;
12257
12258         name = pg_strdup(proc);
12259         /* find non-double-quoted left paren */
12260         inquote = false;
12261         for (paren = name; *paren; paren++)
12262         {
12263                 if (*paren == '(' && !inquote)
12264                 {
12265                         *paren = '\0';
12266                         break;
12267                 }
12268                 if (*paren == '"')
12269                         inquote = !inquote;
12270         }
12271         return name;
12272 }
12273
12274 /*
12275  * Convert an operator cross-reference obtained from pg_operator
12276  *
12277  * Returns an allocated string of what to print, or NULL to print nothing.
12278  * Caller is responsible for free'ing result string.
12279  *
12280  * The input is a REGOPERATOR display; we have to strip the argument-types
12281  * part, and add OPERATOR() decoration if the name is schema-qualified.
12282  */
12283 static char *
12284 convertOperatorReference(Archive *fout, const char *opr)
12285 {
12286         char       *name;
12287         char       *oname;
12288         char       *ptr;
12289         bool            inquote;
12290         bool            sawdot;
12291
12292         /* In all cases "0" means a null reference */
12293         if (strcmp(opr, "0") == 0)
12294                 return NULL;
12295
12296         name = pg_strdup(opr);
12297         /* find non-double-quoted left paren, and check for non-quoted dot */
12298         inquote = false;
12299         sawdot = false;
12300         for (ptr = name; *ptr; ptr++)
12301         {
12302                 if (*ptr == '"')
12303                         inquote = !inquote;
12304                 else if (*ptr == '.' && !inquote)
12305                         sawdot = true;
12306                 else if (*ptr == '(' && !inquote)
12307                 {
12308                         *ptr = '\0';
12309                         break;
12310                 }
12311         }
12312         /* If not schema-qualified, don't need to add OPERATOR() */
12313         if (!sawdot)
12314                 return name;
12315         oname = psprintf("OPERATOR(%s)", name);
12316         free(name);
12317         return oname;
12318 }
12319
12320 /*
12321  * Convert a function OID obtained from pg_ts_parser or pg_ts_template
12322  *
12323  * It is sufficient to use REGPROC rather than REGPROCEDURE, since the
12324  * argument lists of these functions are predetermined.  Note that the
12325  * caller should ensure we are in the proper schema, because the results
12326  * are search path dependent!
12327  */
12328 static char *
12329 convertTSFunction(Archive *fout, Oid funcOid)
12330 {
12331         char       *result;
12332         char            query[128];
12333         PGresult   *res;
12334
12335         snprintf(query, sizeof(query),
12336                          "SELECT '%u'::pg_catalog.regproc", funcOid);
12337         res = ExecuteSqlQueryForSingleRow(fout, query);
12338
12339         result = pg_strdup(PQgetvalue(res, 0, 0));
12340
12341         PQclear(res);
12342
12343         return result;
12344 }
12345
12346 /*
12347  * dumpAccessMethod
12348  *        write out a single access method definition
12349  */
12350 static void
12351 dumpAccessMethod(Archive *fout, AccessMethodInfo *aminfo)
12352 {
12353         DumpOptions *dopt = fout->dopt;
12354         PQExpBuffer q;
12355         PQExpBuffer delq;
12356         PQExpBuffer labelq;
12357         char       *qamname;
12358
12359         /* Skip if not to be dumped */
12360         if (!aminfo->dobj.dump || dopt->dataOnly)
12361                 return;
12362
12363         q = createPQExpBuffer();
12364         delq = createPQExpBuffer();
12365         labelq = createPQExpBuffer();
12366
12367         qamname = pg_strdup(fmtId(aminfo->dobj.name));
12368
12369         appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12370
12371         switch (aminfo->amtype)
12372         {
12373                 case AMTYPE_INDEX:
12374                         appendPQExpBuffer(q, "TYPE INDEX ");
12375                         break;
12376                 default:
12377                         write_msg(NULL, "WARNING: invalid type \"%c\" of access method \"%s\"\n",
12378                                           aminfo->amtype, qamname);
12379                         pg_free(qamname);
12380                         destroyPQExpBuffer(q);
12381                         destroyPQExpBuffer(delq);
12382                         destroyPQExpBuffer(labelq);
12383                         return;
12384         }
12385
12386         appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12387
12388         appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12389                                           qamname);
12390
12391         appendPQExpBuffer(labelq, "ACCESS METHOD %s",
12392                                           qamname);
12393
12394         if (dopt->binary_upgrade)
12395                 binary_upgrade_extension_member(q, &aminfo->dobj, labelq->data);
12396
12397         if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12398                 ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12399                                          aminfo->dobj.name,
12400                                          NULL,
12401                                          NULL,
12402                                          "",
12403                                          false, "ACCESS METHOD", SECTION_PRE_DATA,
12404                                          q->data, delq->data, NULL,
12405                                          NULL, 0,
12406                                          NULL, NULL);
12407
12408         /* Dump Access Method Comments */
12409         if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12410                 dumpComment(fout, labelq->data,
12411                                         NULL, "",
12412                                         aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12413
12414         pg_free(qamname);
12415
12416         destroyPQExpBuffer(q);
12417         destroyPQExpBuffer(delq);
12418         destroyPQExpBuffer(labelq);
12419 }
12420
12421 /*
12422  * dumpOpclass
12423  *        write out a single operator class definition
12424  */
12425 static void
12426 dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
12427 {
12428         DumpOptions *dopt = fout->dopt;
12429         PQExpBuffer query;
12430         PQExpBuffer q;
12431         PQExpBuffer delq;
12432         PQExpBuffer labelq;
12433         PGresult   *res;
12434         int                     ntups;
12435         int                     i_opcintype;
12436         int                     i_opckeytype;
12437         int                     i_opcdefault;
12438         int                     i_opcfamily;
12439         int                     i_opcfamilyname;
12440         int                     i_opcfamilynsp;
12441         int                     i_amname;
12442         int                     i_amopstrategy;
12443         int                     i_amopreqcheck;
12444         int                     i_amopopr;
12445         int                     i_sortfamily;
12446         int                     i_sortfamilynsp;
12447         int                     i_amprocnum;
12448         int                     i_amproc;
12449         int                     i_amproclefttype;
12450         int                     i_amprocrighttype;
12451         char       *opcintype;
12452         char       *opckeytype;
12453         char       *opcdefault;
12454         char       *opcfamily;
12455         char       *opcfamilyname;
12456         char       *opcfamilynsp;
12457         char       *amname;
12458         char       *amopstrategy;
12459         char       *amopreqcheck;
12460         char       *amopopr;
12461         char       *sortfamily;
12462         char       *sortfamilynsp;
12463         char       *amprocnum;
12464         char       *amproc;
12465         char       *amproclefttype;
12466         char       *amprocrighttype;
12467         bool            needComma;
12468         int                     i;
12469
12470         /* Skip if not to be dumped */
12471         if (!opcinfo->dobj.dump || dopt->dataOnly)
12472                 return;
12473
12474         query = createPQExpBuffer();
12475         q = createPQExpBuffer();
12476         delq = createPQExpBuffer();
12477         labelq = createPQExpBuffer();
12478
12479         /* Make sure we are in proper schema so regoperator works correctly */
12480         selectSourceSchema(fout, opcinfo->dobj.namespace->dobj.name);
12481
12482         /* Get additional fields from the pg_opclass row */
12483         if (fout->remoteVersion >= 80300)
12484         {
12485                 appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
12486                                                   "opckeytype::pg_catalog.regtype, "
12487                                                   "opcdefault, opcfamily, "
12488                                                   "opfname AS opcfamilyname, "
12489                                                   "nspname AS opcfamilynsp, "
12490                                                   "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
12491                                                   "FROM pg_catalog.pg_opclass c "
12492                                                   "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
12493                                                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
12494                                                   "WHERE c.oid = '%u'::pg_catalog.oid",
12495                                                   opcinfo->dobj.catId.oid);
12496         }
12497         else
12498         {
12499                 appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
12500                                                   "opckeytype::pg_catalog.regtype, "
12501                                                   "opcdefault, NULL AS opcfamily, "
12502                                                   "NULL AS opcfamilyname, "
12503                                                   "NULL AS opcfamilynsp, "
12504                                                   "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcamid) AS amname "
12505                                                   "FROM pg_catalog.pg_opclass "
12506                                                   "WHERE oid = '%u'::pg_catalog.oid",
12507                                                   opcinfo->dobj.catId.oid);
12508         }
12509
12510         res = ExecuteSqlQueryForSingleRow(fout, query->data);
12511
12512         i_opcintype = PQfnumber(res, "opcintype");
12513         i_opckeytype = PQfnumber(res, "opckeytype");
12514         i_opcdefault = PQfnumber(res, "opcdefault");
12515         i_opcfamily = PQfnumber(res, "opcfamily");
12516         i_opcfamilyname = PQfnumber(res, "opcfamilyname");
12517         i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
12518         i_amname = PQfnumber(res, "amname");
12519
12520         /* opcintype may still be needed after we PQclear res */
12521         opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
12522         opckeytype = PQgetvalue(res, 0, i_opckeytype);
12523         opcdefault = PQgetvalue(res, 0, i_opcdefault);
12524         /* opcfamily will still be needed after we PQclear res */
12525         opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily));
12526         opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname);
12527         opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
12528         /* amname will still be needed after we PQclear res */
12529         amname = pg_strdup(PQgetvalue(res, 0, i_amname));
12530
12531         /*
12532          * DROP must be fully qualified in case same name appears in pg_catalog
12533          */
12534         appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
12535                                           fmtId(opcinfo->dobj.namespace->dobj.name));
12536         appendPQExpBuffer(delq, ".%s",
12537                                           fmtId(opcinfo->dobj.name));
12538         appendPQExpBuffer(delq, " USING %s;\n",
12539                                           fmtId(amname));
12540
12541         /* Build the fixed portion of the CREATE command */
12542         appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n    ",
12543                                           fmtId(opcinfo->dobj.name));
12544         if (strcmp(opcdefault, "t") == 0)
12545                 appendPQExpBufferStr(q, "DEFAULT ");
12546         appendPQExpBuffer(q, "FOR TYPE %s USING %s",
12547                                           opcintype,
12548                                           fmtId(amname));
12549         if (strlen(opcfamilyname) > 0)
12550         {
12551                 appendPQExpBufferStr(q, " FAMILY ");
12552                 if (strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0)
12553                         appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
12554                 appendPQExpBufferStr(q, fmtId(opcfamilyname));
12555         }
12556         appendPQExpBufferStr(q, " AS\n    ");
12557
12558         needComma = false;
12559
12560         if (strcmp(opckeytype, "-") != 0)
12561         {
12562                 appendPQExpBuffer(q, "STORAGE %s",
12563                                                   opckeytype);
12564                 needComma = true;
12565         }
12566
12567         PQclear(res);
12568
12569         /*
12570          * Now fetch and print the OPERATOR entries (pg_amop rows).
12571          *
12572          * Print only those opfamily members that are tied to the opclass by
12573          * pg_depend entries.
12574          *
12575          * XXX RECHECK is gone as of 8.4, but we'll still print it if dumping an
12576          * older server's opclass in which it is used.  This is to avoid
12577          * hard-to-detect breakage if a newer pg_dump is used to dump from an
12578          * older server and then reload into that old version.  This can go away
12579          * once 8.3 is so old as to not be of interest to anyone.
12580          */
12581         resetPQExpBuffer(query);
12582
12583         if (fout->remoteVersion >= 90100)
12584         {
12585                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
12586                                                   "amopopr::pg_catalog.regoperator, "
12587                                                   "opfname AS sortfamily, "
12588                                                   "nspname AS sortfamilynsp "
12589                                                   "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
12590                                                   "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
12591                                                   "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
12592                                                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
12593                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12594                                                   "AND refobjid = '%u'::pg_catalog.oid "
12595                                                   "AND amopfamily = '%s'::pg_catalog.oid "
12596                                                   "ORDER BY amopstrategy",
12597                                                   opcinfo->dobj.catId.oid,
12598                                                   opcfamily);
12599         }
12600         else if (fout->remoteVersion >= 80400)
12601         {
12602                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
12603                                                   "amopopr::pg_catalog.regoperator, "
12604                                                   "NULL AS sortfamily, "
12605                                                   "NULL AS sortfamilynsp "
12606                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
12607                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12608                                                   "AND refobjid = '%u'::pg_catalog.oid "
12609                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
12610                                                   "AND objid = ao.oid "
12611                                                   "ORDER BY amopstrategy",
12612                                                   opcinfo->dobj.catId.oid);
12613         }
12614         else if (fout->remoteVersion >= 80300)
12615         {
12616                 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
12617                                                   "amopopr::pg_catalog.regoperator, "
12618                                                   "NULL AS sortfamily, "
12619                                                   "NULL AS sortfamilynsp "
12620                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
12621                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12622                                                   "AND refobjid = '%u'::pg_catalog.oid "
12623                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
12624                                                   "AND objid = ao.oid "
12625                                                   "ORDER BY amopstrategy",
12626                                                   opcinfo->dobj.catId.oid);
12627         }
12628         else
12629         {
12630                 /*
12631                  * Here, we print all entries since there are no opfamilies and hence
12632                  * no loose operators to worry about.
12633                  */
12634                 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
12635                                                   "amopopr::pg_catalog.regoperator, "
12636                                                   "NULL AS sortfamily, "
12637                                                   "NULL AS sortfamilynsp "
12638                                                   "FROM pg_catalog.pg_amop "
12639                                                   "WHERE amopclaid = '%u'::pg_catalog.oid "
12640                                                   "ORDER BY amopstrategy",
12641                                                   opcinfo->dobj.catId.oid);
12642         }
12643
12644         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12645
12646         ntups = PQntuples(res);
12647
12648         i_amopstrategy = PQfnumber(res, "amopstrategy");
12649         i_amopreqcheck = PQfnumber(res, "amopreqcheck");
12650         i_amopopr = PQfnumber(res, "amopopr");
12651         i_sortfamily = PQfnumber(res, "sortfamily");
12652         i_sortfamilynsp = PQfnumber(res, "sortfamilynsp");
12653
12654         for (i = 0; i < ntups; i++)
12655         {
12656                 amopstrategy = PQgetvalue(res, i, i_amopstrategy);
12657                 amopreqcheck = PQgetvalue(res, i, i_amopreqcheck);
12658                 amopopr = PQgetvalue(res, i, i_amopopr);
12659                 sortfamily = PQgetvalue(res, i, i_sortfamily);
12660                 sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp);
12661
12662                 if (needComma)
12663                         appendPQExpBufferStr(q, " ,\n    ");
12664
12665                 appendPQExpBuffer(q, "OPERATOR %s %s",
12666                                                   amopstrategy, amopopr);
12667
12668                 if (strlen(sortfamily) > 0)
12669                 {
12670                         appendPQExpBufferStr(q, " FOR ORDER BY ");
12671                         if (strcmp(sortfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0)
12672                                 appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
12673                         appendPQExpBufferStr(q, fmtId(sortfamily));
12674                 }
12675
12676                 if (strcmp(amopreqcheck, "t") == 0)
12677                         appendPQExpBufferStr(q, " RECHECK");
12678
12679                 needComma = true;
12680         }
12681
12682         PQclear(res);
12683
12684         /*
12685          * Now fetch and print the FUNCTION entries (pg_amproc rows).
12686          *
12687          * Print only those opfamily members that are tied to the opclass by
12688          * pg_depend entries.
12689          *
12690          * We print the amproclefttype/amprocrighttype even though in most cases
12691          * the backend could deduce the right values, because of the corner case
12692          * of a btree sort support function for a cross-type comparison.  That's
12693          * only allowed in 9.2 and later, but for simplicity print them in all
12694          * versions that have the columns.
12695          */
12696         resetPQExpBuffer(query);
12697
12698         if (fout->remoteVersion >= 80300)
12699         {
12700                 appendPQExpBuffer(query, "SELECT amprocnum, "
12701                                                   "amproc::pg_catalog.regprocedure, "
12702                                                   "amproclefttype::pg_catalog.regtype, "
12703                                                   "amprocrighttype::pg_catalog.regtype "
12704                                                   "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
12705                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12706                                                   "AND refobjid = '%u'::pg_catalog.oid "
12707                                                   "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
12708                                                   "AND objid = ap.oid "
12709                                                   "ORDER BY amprocnum",
12710                                                   opcinfo->dobj.catId.oid);
12711         }
12712         else
12713         {
12714                 appendPQExpBuffer(query, "SELECT amprocnum, "
12715                                                   "amproc::pg_catalog.regprocedure, "
12716                                                   "'' AS amproclefttype, "
12717                                                   "'' AS amprocrighttype "
12718                                                   "FROM pg_catalog.pg_amproc "
12719                                                   "WHERE amopclaid = '%u'::pg_catalog.oid "
12720                                                   "ORDER BY amprocnum",
12721                                                   opcinfo->dobj.catId.oid);
12722         }
12723
12724         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12725
12726         ntups = PQntuples(res);
12727
12728         i_amprocnum = PQfnumber(res, "amprocnum");
12729         i_amproc = PQfnumber(res, "amproc");
12730         i_amproclefttype = PQfnumber(res, "amproclefttype");
12731         i_amprocrighttype = PQfnumber(res, "amprocrighttype");
12732
12733         for (i = 0; i < ntups; i++)
12734         {
12735                 amprocnum = PQgetvalue(res, i, i_amprocnum);
12736                 amproc = PQgetvalue(res, i, i_amproc);
12737                 amproclefttype = PQgetvalue(res, i, i_amproclefttype);
12738                 amprocrighttype = PQgetvalue(res, i, i_amprocrighttype);
12739
12740                 if (needComma)
12741                         appendPQExpBufferStr(q, " ,\n    ");
12742
12743                 appendPQExpBuffer(q, "FUNCTION %s", amprocnum);
12744
12745                 if (*amproclefttype && *amprocrighttype)
12746                         appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype);
12747
12748                 appendPQExpBuffer(q, " %s", amproc);
12749
12750                 needComma = true;
12751         }
12752
12753         PQclear(res);
12754
12755         /*
12756          * If needComma is still false it means we haven't added anything after
12757          * the AS keyword.  To avoid printing broken SQL, append a dummy STORAGE
12758          * clause with the same datatype.  This isn't sanctioned by the
12759          * documentation, but actually DefineOpClass will treat it as a no-op.
12760          */
12761         if (!needComma)
12762                 appendPQExpBuffer(q, "STORAGE %s", opcintype);
12763
12764         appendPQExpBufferStr(q, ";\n");
12765
12766         appendPQExpBuffer(labelq, "OPERATOR CLASS %s",
12767                                           fmtId(opcinfo->dobj.name));
12768         appendPQExpBuffer(labelq, " USING %s",
12769                                           fmtId(amname));
12770
12771         if (dopt->binary_upgrade)
12772                 binary_upgrade_extension_member(q, &opcinfo->dobj, labelq->data);
12773
12774         if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12775                 ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
12776                                          opcinfo->dobj.name,
12777                                          opcinfo->dobj.namespace->dobj.name,
12778                                          NULL,
12779                                          opcinfo->rolname,
12780                                          false, "OPERATOR CLASS", SECTION_PRE_DATA,
12781                                          q->data, delq->data, NULL,
12782                                          NULL, 0,
12783                                          NULL, NULL);
12784
12785         /* Dump Operator Class Comments */
12786         if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12787                 dumpComment(fout, labelq->data,
12788                                         opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
12789                                         opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
12790
12791         free(opcintype);
12792         free(opcfamily);
12793         free(amname);
12794         destroyPQExpBuffer(query);
12795         destroyPQExpBuffer(q);
12796         destroyPQExpBuffer(delq);
12797         destroyPQExpBuffer(labelq);
12798 }
12799
12800 /*
12801  * dumpOpfamily
12802  *        write out a single operator family definition
12803  *
12804  * Note: this also dumps any "loose" operator members that aren't bound to a
12805  * specific opclass within the opfamily.
12806  */
12807 static void
12808 dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
12809 {
12810         DumpOptions *dopt = fout->dopt;
12811         PQExpBuffer query;
12812         PQExpBuffer q;
12813         PQExpBuffer delq;
12814         PQExpBuffer labelq;
12815         PGresult   *res;
12816         PGresult   *res_ops;
12817         PGresult   *res_procs;
12818         int                     ntups;
12819         int                     i_amname;
12820         int                     i_amopstrategy;
12821         int                     i_amopreqcheck;
12822         int                     i_amopopr;
12823         int                     i_sortfamily;
12824         int                     i_sortfamilynsp;
12825         int                     i_amprocnum;
12826         int                     i_amproc;
12827         int                     i_amproclefttype;
12828         int                     i_amprocrighttype;
12829         char       *amname;
12830         char       *amopstrategy;
12831         char       *amopreqcheck;
12832         char       *amopopr;
12833         char       *sortfamily;
12834         char       *sortfamilynsp;
12835         char       *amprocnum;
12836         char       *amproc;
12837         char       *amproclefttype;
12838         char       *amprocrighttype;
12839         bool            needComma;
12840         int                     i;
12841
12842         /* Skip if not to be dumped */
12843         if (!opfinfo->dobj.dump || dopt->dataOnly)
12844                 return;
12845
12846         query = createPQExpBuffer();
12847         q = createPQExpBuffer();
12848         delq = createPQExpBuffer();
12849         labelq = createPQExpBuffer();
12850
12851         /* Make sure we are in proper schema so regoperator works correctly */
12852         selectSourceSchema(fout, opfinfo->dobj.namespace->dobj.name);
12853
12854         /*
12855          * Fetch only those opfamily members that are tied directly to the
12856          * opfamily by pg_depend entries.
12857          *
12858          * XXX RECHECK is gone as of 8.4, but we'll still print it if dumping an
12859          * older server's opclass in which it is used.  This is to avoid
12860          * hard-to-detect breakage if a newer pg_dump is used to dump from an
12861          * older server and then reload into that old version.  This can go away
12862          * once 8.3 is so old as to not be of interest to anyone.
12863          */
12864         if (fout->remoteVersion >= 90100)
12865         {
12866                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
12867                                                   "amopopr::pg_catalog.regoperator, "
12868                                                   "opfname AS sortfamily, "
12869                                                   "nspname AS sortfamilynsp "
12870                                                   "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
12871                                                   "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
12872                                                   "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
12873                                                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
12874                                                   "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
12875                                                   "AND refobjid = '%u'::pg_catalog.oid "
12876                                                   "AND amopfamily = '%u'::pg_catalog.oid "
12877                                                   "ORDER BY amopstrategy",
12878                                                   opfinfo->dobj.catId.oid,
12879                                                   opfinfo->dobj.catId.oid);
12880         }
12881         else if (fout->remoteVersion >= 80400)
12882         {
12883                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
12884                                                   "amopopr::pg_catalog.regoperator, "
12885                                                   "NULL AS sortfamily, "
12886                                                   "NULL AS sortfamilynsp "
12887                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
12888                                                   "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
12889                                                   "AND refobjid = '%u'::pg_catalog.oid "
12890                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
12891                                                   "AND objid = ao.oid "
12892                                                   "ORDER BY amopstrategy",
12893                                                   opfinfo->dobj.catId.oid);
12894         }
12895         else
12896         {
12897                 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
12898                                                   "amopopr::pg_catalog.regoperator, "
12899                                                   "NULL AS sortfamily, "
12900                                                   "NULL AS sortfamilynsp "
12901                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
12902                                                   "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
12903                                                   "AND refobjid = '%u'::pg_catalog.oid "
12904                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
12905                                                   "AND objid = ao.oid "
12906                                                   "ORDER BY amopstrategy",
12907                                                   opfinfo->dobj.catId.oid);
12908         }
12909
12910         res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12911
12912         resetPQExpBuffer(query);
12913
12914         appendPQExpBuffer(query, "SELECT amprocnum, "
12915                                           "amproc::pg_catalog.regprocedure, "
12916                                           "amproclefttype::pg_catalog.regtype, "
12917                                           "amprocrighttype::pg_catalog.regtype "
12918                                           "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
12919                                           "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
12920                                           "AND refobjid = '%u'::pg_catalog.oid "
12921                                           "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
12922                                           "AND objid = ap.oid "
12923                                           "ORDER BY amprocnum",
12924                                           opfinfo->dobj.catId.oid);
12925
12926         res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12927
12928         /* Get additional fields from the pg_opfamily row */
12929         resetPQExpBuffer(query);
12930
12931         appendPQExpBuffer(query, "SELECT "
12932                                           "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
12933                                           "FROM pg_catalog.pg_opfamily "
12934                                           "WHERE oid = '%u'::pg_catalog.oid",
12935                                           opfinfo->dobj.catId.oid);
12936
12937         res = ExecuteSqlQueryForSingleRow(fout, query->data);
12938
12939         i_amname = PQfnumber(res, "amname");
12940
12941         /* amname will still be needed after we PQclear res */
12942         amname = pg_strdup(PQgetvalue(res, 0, i_amname));
12943
12944         /*
12945          * DROP must be fully qualified in case same name appears in pg_catalog
12946          */
12947         appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
12948                                           fmtId(opfinfo->dobj.namespace->dobj.name));
12949         appendPQExpBuffer(delq, ".%s",
12950                                           fmtId(opfinfo->dobj.name));
12951         appendPQExpBuffer(delq, " USING %s;\n",
12952                                           fmtId(amname));
12953
12954         /* Build the fixed portion of the CREATE command */
12955         appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
12956                                           fmtId(opfinfo->dobj.name));
12957         appendPQExpBuffer(q, " USING %s;\n",
12958                                           fmtId(amname));
12959
12960         PQclear(res);
12961
12962         /* Do we need an ALTER to add loose members? */
12963         if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
12964         {
12965                 appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
12966                                                   fmtId(opfinfo->dobj.name));
12967                 appendPQExpBuffer(q, " USING %s ADD\n    ",
12968                                                   fmtId(amname));
12969
12970                 needComma = false;
12971
12972                 /*
12973                  * Now fetch and print the OPERATOR entries (pg_amop rows).
12974                  */
12975                 ntups = PQntuples(res_ops);
12976
12977                 i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
12978                 i_amopreqcheck = PQfnumber(res_ops, "amopreqcheck");
12979                 i_amopopr = PQfnumber(res_ops, "amopopr");
12980                 i_sortfamily = PQfnumber(res_ops, "sortfamily");
12981                 i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp");
12982
12983                 for (i = 0; i < ntups; i++)
12984                 {
12985                         amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
12986                         amopreqcheck = PQgetvalue(res_ops, i, i_amopreqcheck);
12987                         amopopr = PQgetvalue(res_ops, i, i_amopopr);
12988                         sortfamily = PQgetvalue(res_ops, i, i_sortfamily);
12989                         sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp);
12990
12991                         if (needComma)
12992                                 appendPQExpBufferStr(q, " ,\n    ");
12993
12994                         appendPQExpBuffer(q, "OPERATOR %s %s",
12995                                                           amopstrategy, amopopr);
12996
12997                         if (strlen(sortfamily) > 0)
12998                         {
12999                                 appendPQExpBufferStr(q, " FOR ORDER BY ");
13000                                 if (strcmp(sortfamilynsp, opfinfo->dobj.namespace->dobj.name) != 0)
13001                                         appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
13002                                 appendPQExpBufferStr(q, fmtId(sortfamily));
13003                         }
13004
13005                         if (strcmp(amopreqcheck, "t") == 0)
13006                                 appendPQExpBufferStr(q, " RECHECK");
13007
13008                         needComma = true;
13009                 }
13010
13011                 /*
13012                  * Now fetch and print the FUNCTION entries (pg_amproc rows).
13013                  */
13014                 ntups = PQntuples(res_procs);
13015
13016                 i_amprocnum = PQfnumber(res_procs, "amprocnum");
13017                 i_amproc = PQfnumber(res_procs, "amproc");
13018                 i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
13019                 i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
13020
13021                 for (i = 0; i < ntups; i++)
13022                 {
13023                         amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
13024                         amproc = PQgetvalue(res_procs, i, i_amproc);
13025                         amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
13026                         amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
13027
13028                         if (needComma)
13029                                 appendPQExpBufferStr(q, " ,\n    ");
13030
13031                         appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
13032                                                           amprocnum, amproclefttype, amprocrighttype,
13033                                                           amproc);
13034
13035                         needComma = true;
13036                 }
13037
13038                 appendPQExpBufferStr(q, ";\n");
13039         }
13040
13041         appendPQExpBuffer(labelq, "OPERATOR FAMILY %s",
13042                                           fmtId(opfinfo->dobj.name));
13043         appendPQExpBuffer(labelq, " USING %s",
13044                                           fmtId(amname));
13045
13046         if (dopt->binary_upgrade)
13047                 binary_upgrade_extension_member(q, &opfinfo->dobj, labelq->data);
13048
13049         if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13050                 ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
13051                                          opfinfo->dobj.name,
13052                                          opfinfo->dobj.namespace->dobj.name,
13053                                          NULL,
13054                                          opfinfo->rolname,
13055                                          false, "OPERATOR FAMILY", SECTION_PRE_DATA,
13056                                          q->data, delq->data, NULL,
13057                                          NULL, 0,
13058                                          NULL, NULL);
13059
13060         /* Dump Operator Family Comments */
13061         if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13062                 dumpComment(fout, labelq->data,
13063                                         opfinfo->dobj.namespace->dobj.name, opfinfo->rolname,
13064                                         opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
13065
13066         free(amname);
13067         PQclear(res_ops);
13068         PQclear(res_procs);
13069         destroyPQExpBuffer(query);
13070         destroyPQExpBuffer(q);
13071         destroyPQExpBuffer(delq);
13072         destroyPQExpBuffer(labelq);
13073 }
13074
13075 /*
13076  * dumpCollation
13077  *        write out a single collation definition
13078  */
13079 static void
13080 dumpCollation(Archive *fout, CollInfo *collinfo)
13081 {
13082         DumpOptions *dopt = fout->dopt;
13083         PQExpBuffer query;
13084         PQExpBuffer q;
13085         PQExpBuffer delq;
13086         PQExpBuffer labelq;
13087         PGresult   *res;
13088         int                     i_collprovider;
13089         int                     i_collcollate;
13090         int                     i_collctype;
13091         const char *collprovider;
13092         const char *collcollate;
13093         const char *collctype;
13094
13095         /* Skip if not to be dumped */
13096         if (!collinfo->dobj.dump || dopt->dataOnly)
13097                 return;
13098
13099         query = createPQExpBuffer();
13100         q = createPQExpBuffer();
13101         delq = createPQExpBuffer();
13102         labelq = createPQExpBuffer();
13103
13104         /* Make sure we are in proper schema */
13105         selectSourceSchema(fout, collinfo->dobj.namespace->dobj.name);
13106
13107         /* Get collation-specific details */
13108         if (fout->remoteVersion >= 100000)
13109                 appendPQExpBuffer(query, "SELECT "
13110                                                   "collprovider, "
13111                                                   "collcollate, "
13112                                                   "collctype, "
13113                                                   "collversion "
13114                                                   "FROM pg_catalog.pg_collation c "
13115                                                   "WHERE c.oid = '%u'::pg_catalog.oid",
13116                                                   collinfo->dobj.catId.oid);
13117         else
13118                 appendPQExpBuffer(query, "SELECT "
13119                                                   "'c'::char AS collprovider, "
13120                                                   "collcollate, "
13121                                                   "collctype, "
13122                                                   "NULL AS collversion "
13123                                                   "FROM pg_catalog.pg_collation c "
13124                                                   "WHERE c.oid = '%u'::pg_catalog.oid",
13125                                                   collinfo->dobj.catId.oid);
13126
13127         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13128
13129         i_collprovider = PQfnumber(res, "collprovider");
13130         i_collcollate = PQfnumber(res, "collcollate");
13131         i_collctype = PQfnumber(res, "collctype");
13132
13133         collprovider = PQgetvalue(res, 0, i_collprovider);
13134         collcollate = PQgetvalue(res, 0, i_collcollate);
13135         collctype = PQgetvalue(res, 0, i_collctype);
13136
13137         /*
13138          * DROP must be fully qualified in case same name appears in pg_catalog
13139          */
13140         appendPQExpBuffer(delq, "DROP COLLATION %s",
13141                                           fmtId(collinfo->dobj.namespace->dobj.name));
13142         appendPQExpBuffer(delq, ".%s;\n",
13143                                           fmtId(collinfo->dobj.name));
13144
13145         appendPQExpBuffer(q, "CREATE COLLATION %s (",
13146                                           fmtId(collinfo->dobj.name));
13147
13148         appendPQExpBufferStr(q, "provider = ");
13149         if (collprovider[0] == 'c')
13150                 appendPQExpBufferStr(q, "libc");
13151         else if (collprovider[0] == 'i')
13152                 appendPQExpBufferStr(q, "icu");
13153         else if (collprovider[0] == 'd')
13154                 /* to allow dumping pg_catalog; not accepted on input */
13155                 appendPQExpBufferStr(q, "default");
13156         else
13157                 exit_horribly(NULL,
13158                                           "unrecognized collation provider: %s\n",
13159                                           collprovider);
13160
13161         if (strcmp(collcollate, collctype) == 0)
13162         {
13163                 appendPQExpBufferStr(q, ", locale = ");
13164                 appendStringLiteralAH(q, collcollate, fout);
13165         }
13166         else
13167         {
13168                 appendPQExpBufferStr(q, ", lc_collate = ");
13169                 appendStringLiteralAH(q, collcollate, fout);
13170                 appendPQExpBufferStr(q, ", lc_ctype = ");
13171                 appendStringLiteralAH(q, collctype, fout);
13172         }
13173
13174         /*
13175          * For binary upgrade, carry over the collation version.  For normal
13176          * dump/restore, omit the version, so that it is computed upon restore.
13177          */
13178         if (dopt->binary_upgrade)
13179         {
13180                 int                     i_collversion;
13181
13182                 i_collversion = PQfnumber(res, "collversion");
13183                 if (!PQgetisnull(res, 0, i_collversion))
13184                 {
13185                         appendPQExpBufferStr(q, ", version = ");
13186                         appendStringLiteralAH(q,
13187                                                                   PQgetvalue(res, 0, i_collversion),
13188                                                                   fout);
13189                 }
13190         }
13191
13192         appendPQExpBufferStr(q, ");\n");
13193
13194         appendPQExpBuffer(labelq, "COLLATION %s", fmtId(collinfo->dobj.name));
13195
13196         if (dopt->binary_upgrade)
13197                 binary_upgrade_extension_member(q, &collinfo->dobj, labelq->data);
13198
13199         if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13200                 ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13201                                          collinfo->dobj.name,
13202                                          collinfo->dobj.namespace->dobj.name,
13203                                          NULL,
13204                                          collinfo->rolname,
13205                                          false, "COLLATION", SECTION_PRE_DATA,
13206                                          q->data, delq->data, NULL,
13207                                          NULL, 0,
13208                                          NULL, NULL);
13209
13210         /* Dump Collation Comments */
13211         if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13212                 dumpComment(fout, labelq->data,
13213                                         collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13214                                         collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13215
13216         PQclear(res);
13217
13218         destroyPQExpBuffer(query);
13219         destroyPQExpBuffer(q);
13220         destroyPQExpBuffer(delq);
13221         destroyPQExpBuffer(labelq);
13222 }
13223
13224 /*
13225  * dumpConversion
13226  *        write out a single conversion definition
13227  */
13228 static void
13229 dumpConversion(Archive *fout, ConvInfo *convinfo)
13230 {
13231         DumpOptions *dopt = fout->dopt;
13232         PQExpBuffer query;
13233         PQExpBuffer q;
13234         PQExpBuffer delq;
13235         PQExpBuffer labelq;
13236         PGresult   *res;
13237         int                     i_conforencoding;
13238         int                     i_contoencoding;
13239         int                     i_conproc;
13240         int                     i_condefault;
13241         const char *conforencoding;
13242         const char *contoencoding;
13243         const char *conproc;
13244         bool            condefault;
13245
13246         /* Skip if not to be dumped */
13247         if (!convinfo->dobj.dump || dopt->dataOnly)
13248                 return;
13249
13250         query = createPQExpBuffer();
13251         q = createPQExpBuffer();
13252         delq = createPQExpBuffer();
13253         labelq = createPQExpBuffer();
13254
13255         /* Make sure we are in proper schema */
13256         selectSourceSchema(fout, convinfo->dobj.namespace->dobj.name);
13257
13258         /* Get conversion-specific details */
13259         appendPQExpBuffer(query, "SELECT "
13260                                           "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13261                                           "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13262                                           "conproc, condefault "
13263                                           "FROM pg_catalog.pg_conversion c "
13264                                           "WHERE c.oid = '%u'::pg_catalog.oid",
13265                                           convinfo->dobj.catId.oid);
13266
13267         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13268
13269         i_conforencoding = PQfnumber(res, "conforencoding");
13270         i_contoencoding = PQfnumber(res, "contoencoding");
13271         i_conproc = PQfnumber(res, "conproc");
13272         i_condefault = PQfnumber(res, "condefault");
13273
13274         conforencoding = PQgetvalue(res, 0, i_conforencoding);
13275         contoencoding = PQgetvalue(res, 0, i_contoencoding);
13276         conproc = PQgetvalue(res, 0, i_conproc);
13277         condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13278
13279         /*
13280          * DROP must be fully qualified in case same name appears in pg_catalog
13281          */
13282         appendPQExpBuffer(delq, "DROP CONVERSION %s",
13283                                           fmtId(convinfo->dobj.namespace->dobj.name));
13284         appendPQExpBuffer(delq, ".%s;\n",
13285                                           fmtId(convinfo->dobj.name));
13286
13287         appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13288                                           (condefault) ? "DEFAULT " : "",
13289                                           fmtId(convinfo->dobj.name));
13290         appendStringLiteralAH(q, conforencoding, fout);
13291         appendPQExpBufferStr(q, " TO ");
13292         appendStringLiteralAH(q, contoencoding, fout);
13293         /* regproc output is already sufficiently quoted */
13294         appendPQExpBuffer(q, " FROM %s;\n", conproc);
13295
13296         appendPQExpBuffer(labelq, "CONVERSION %s", fmtId(convinfo->dobj.name));
13297
13298         if (dopt->binary_upgrade)
13299                 binary_upgrade_extension_member(q, &convinfo->dobj, labelq->data);
13300
13301         if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13302                 ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13303                                          convinfo->dobj.name,
13304                                          convinfo->dobj.namespace->dobj.name,
13305                                          NULL,
13306                                          convinfo->rolname,
13307                                          false, "CONVERSION", SECTION_PRE_DATA,
13308                                          q->data, delq->data, NULL,
13309                                          NULL, 0,
13310                                          NULL, NULL);
13311
13312         /* Dump Conversion Comments */
13313         if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13314                 dumpComment(fout, labelq->data,
13315                                         convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13316                                         convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13317
13318         PQclear(res);
13319
13320         destroyPQExpBuffer(query);
13321         destroyPQExpBuffer(q);
13322         destroyPQExpBuffer(delq);
13323         destroyPQExpBuffer(labelq);
13324 }
13325
13326 /*
13327  * format_aggregate_signature: generate aggregate name and argument list
13328  *
13329  * The argument type names are qualified if needed.  The aggregate name
13330  * is never qualified.
13331  */
13332 static char *
13333 format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
13334 {
13335         PQExpBufferData buf;
13336         int                     j;
13337
13338         initPQExpBuffer(&buf);
13339         if (honor_quotes)
13340                 appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name));
13341         else
13342                 appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name);
13343
13344         if (agginfo->aggfn.nargs == 0)
13345                 appendPQExpBuffer(&buf, "(*)");
13346         else
13347         {
13348                 appendPQExpBufferChar(&buf, '(');
13349                 for (j = 0; j < agginfo->aggfn.nargs; j++)
13350                 {
13351                         char       *typname;
13352
13353                         typname = getFormattedTypeName(fout, agginfo->aggfn.argtypes[j],
13354                                                                                    zeroAsOpaque);
13355
13356                         appendPQExpBuffer(&buf, "%s%s",
13357                                                           (j > 0) ? ", " : "",
13358                                                           typname);
13359                         free(typname);
13360                 }
13361                 appendPQExpBufferChar(&buf, ')');
13362         }
13363         return buf.data;
13364 }
13365
13366 /*
13367  * dumpAgg
13368  *        write out a single aggregate definition
13369  */
13370 static void
13371 dumpAgg(Archive *fout, AggInfo *agginfo)
13372 {
13373         DumpOptions *dopt = fout->dopt;
13374         PQExpBuffer query;
13375         PQExpBuffer q;
13376         PQExpBuffer delq;
13377         PQExpBuffer labelq;
13378         PQExpBuffer details;
13379         char       *aggsig;                     /* identity signature */
13380         char       *aggfullsig = NULL;  /* full signature */
13381         char       *aggsig_tag;
13382         PGresult   *res;
13383         int                     i_aggtransfn;
13384         int                     i_aggfinalfn;
13385         int                     i_aggcombinefn;
13386         int                     i_aggserialfn;
13387         int                     i_aggdeserialfn;
13388         int                     i_aggmtransfn;
13389         int                     i_aggminvtransfn;
13390         int                     i_aggmfinalfn;
13391         int                     i_aggfinalextra;
13392         int                     i_aggmfinalextra;
13393         int                     i_aggsortop;
13394         int                     i_hypothetical;
13395         int                     i_aggtranstype;
13396         int                     i_aggtransspace;
13397         int                     i_aggmtranstype;
13398         int                     i_aggmtransspace;
13399         int                     i_agginitval;
13400         int                     i_aggminitval;
13401         int                     i_convertok;
13402         int                     i_proparallel;
13403         const char *aggtransfn;
13404         const char *aggfinalfn;
13405         const char *aggcombinefn;
13406         const char *aggserialfn;
13407         const char *aggdeserialfn;
13408         const char *aggmtransfn;
13409         const char *aggminvtransfn;
13410         const char *aggmfinalfn;
13411         bool            aggfinalextra;
13412         bool            aggmfinalextra;
13413         const char *aggsortop;
13414         char       *aggsortconvop;
13415         bool            hypothetical;
13416         const char *aggtranstype;
13417         const char *aggtransspace;
13418         const char *aggmtranstype;
13419         const char *aggmtransspace;
13420         const char *agginitval;
13421         const char *aggminitval;
13422         bool            convertok;
13423         const char *proparallel;
13424
13425         /* Skip if not to be dumped */
13426         if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13427                 return;
13428
13429         query = createPQExpBuffer();
13430         q = createPQExpBuffer();
13431         delq = createPQExpBuffer();
13432         labelq = createPQExpBuffer();
13433         details = createPQExpBuffer();
13434
13435         /* Make sure we are in proper schema */
13436         selectSourceSchema(fout, agginfo->aggfn.dobj.namespace->dobj.name);
13437
13438         /* Get aggregate-specific details */
13439         if (fout->remoteVersion >= 90600)
13440         {
13441                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13442                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13443                                                   "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13444                                                   "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13445                                                   "aggfinalextra, aggmfinalextra, "
13446                                                   "aggsortop::pg_catalog.regoperator, "
13447                                                   "(aggkind = 'h') AS hypothetical, "
13448                                                   "aggtransspace, agginitval, "
13449                                                   "aggmtransspace, aggminitval, "
13450                                                   "true AS convertok, "
13451                                                   "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13452                                                   "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13453                                                   "p.proparallel "
13454                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13455                                                   "WHERE a.aggfnoid = p.oid "
13456                                                   "AND p.oid = '%u'::pg_catalog.oid",
13457                                                   agginfo->aggfn.dobj.catId.oid);
13458         }
13459         else if (fout->remoteVersion >= 90400)
13460         {
13461                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13462                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13463                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13464                                                   "'-' AS aggdeserialfn, aggmtransfn, aggminvtransfn, "
13465                                                   "aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13466                                                   "aggfinalextra, aggmfinalextra, "
13467                                                   "aggsortop::pg_catalog.regoperator, "
13468                                                   "(aggkind = 'h') AS hypothetical, "
13469                                                   "aggtransspace, agginitval, "
13470                                                   "aggmtransspace, aggminitval, "
13471                                                   "true AS convertok, "
13472                                                   "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13473                                                   "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13474                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13475                                                   "WHERE a.aggfnoid = p.oid "
13476                                                   "AND p.oid = '%u'::pg_catalog.oid",
13477                                                   agginfo->aggfn.dobj.catId.oid);
13478         }
13479         else if (fout->remoteVersion >= 80400)
13480         {
13481                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13482                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13483                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13484                                                   "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13485                                                   "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13486                                                   "0 AS aggmtranstype, false AS aggfinalextra, "
13487                                                   "false AS aggmfinalextra, "
13488                                                   "aggsortop::pg_catalog.regoperator, "
13489                                                   "false AS hypothetical, "
13490                                                   "0 AS aggtransspace, agginitval, "
13491                                                   "0 AS aggmtransspace, NULL AS aggminitval, "
13492                                                   "true AS convertok, "
13493                                                   "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13494                                                   "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13495                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13496                                                   "WHERE a.aggfnoid = p.oid "
13497                                                   "AND p.oid = '%u'::pg_catalog.oid",
13498                                                   agginfo->aggfn.dobj.catId.oid);
13499         }
13500         else if (fout->remoteVersion >= 80100)
13501         {
13502                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13503                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13504                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13505                                                   "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13506                                                   "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13507                                                   "0 AS aggmtranstype, false AS aggfinalextra, "
13508                                                   "false AS aggmfinalextra, "
13509                                                   "aggsortop::pg_catalog.regoperator, "
13510                                                   "false AS hypothetical, "
13511                                                   "0 AS aggtransspace, agginitval, "
13512                                                   "0 AS aggmtransspace, NULL AS aggminitval, "
13513                                                   "true AS convertok "
13514                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13515                                                   "WHERE a.aggfnoid = p.oid "
13516                                                   "AND p.oid = '%u'::pg_catalog.oid",
13517                                                   agginfo->aggfn.dobj.catId.oid);
13518         }
13519         else
13520         {
13521                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13522                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13523                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13524                                                   "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13525                                                   "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13526                                                   "0 AS aggmtranstype, false AS aggfinalextra, "
13527                                                   "false AS aggmfinalextra, 0 AS aggsortop, "
13528                                                   "false AS hypothetical, "
13529                                                   "0 AS aggtransspace, agginitval, "
13530                                                   "0 AS aggmtransspace, NULL AS aggminitval, "
13531                                                   "true AS convertok "
13532                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13533                                                   "WHERE a.aggfnoid = p.oid "
13534                                                   "AND p.oid = '%u'::pg_catalog.oid",
13535                                                   agginfo->aggfn.dobj.catId.oid);
13536         }
13537
13538         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13539
13540         i_aggtransfn = PQfnumber(res, "aggtransfn");
13541         i_aggfinalfn = PQfnumber(res, "aggfinalfn");
13542         i_aggcombinefn = PQfnumber(res, "aggcombinefn");
13543         i_aggserialfn = PQfnumber(res, "aggserialfn");
13544         i_aggdeserialfn = PQfnumber(res, "aggdeserialfn");
13545         i_aggmtransfn = PQfnumber(res, "aggmtransfn");
13546         i_aggminvtransfn = PQfnumber(res, "aggminvtransfn");
13547         i_aggmfinalfn = PQfnumber(res, "aggmfinalfn");
13548         i_aggfinalextra = PQfnumber(res, "aggfinalextra");
13549         i_aggmfinalextra = PQfnumber(res, "aggmfinalextra");
13550         i_aggsortop = PQfnumber(res, "aggsortop");
13551         i_hypothetical = PQfnumber(res, "hypothetical");
13552         i_aggtranstype = PQfnumber(res, "aggtranstype");
13553         i_aggtransspace = PQfnumber(res, "aggtransspace");
13554         i_aggmtranstype = PQfnumber(res, "aggmtranstype");
13555         i_aggmtransspace = PQfnumber(res, "aggmtransspace");
13556         i_agginitval = PQfnumber(res, "agginitval");
13557         i_aggminitval = PQfnumber(res, "aggminitval");
13558         i_convertok = PQfnumber(res, "convertok");
13559         i_proparallel = PQfnumber(res, "proparallel");
13560
13561         aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
13562         aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
13563         aggcombinefn = PQgetvalue(res, 0, i_aggcombinefn);
13564         aggserialfn = PQgetvalue(res, 0, i_aggserialfn);
13565         aggdeserialfn = PQgetvalue(res, 0, i_aggdeserialfn);
13566         aggmtransfn = PQgetvalue(res, 0, i_aggmtransfn);
13567         aggminvtransfn = PQgetvalue(res, 0, i_aggminvtransfn);
13568         aggmfinalfn = PQgetvalue(res, 0, i_aggmfinalfn);
13569         aggfinalextra = (PQgetvalue(res, 0, i_aggfinalextra)[0] == 't');
13570         aggmfinalextra = (PQgetvalue(res, 0, i_aggmfinalextra)[0] == 't');
13571         aggsortop = PQgetvalue(res, 0, i_aggsortop);
13572         hypothetical = (PQgetvalue(res, 0, i_hypothetical)[0] == 't');
13573         aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
13574         aggtransspace = PQgetvalue(res, 0, i_aggtransspace);
13575         aggmtranstype = PQgetvalue(res, 0, i_aggmtranstype);
13576         aggmtransspace = PQgetvalue(res, 0, i_aggmtransspace);
13577         agginitval = PQgetvalue(res, 0, i_agginitval);
13578         aggminitval = PQgetvalue(res, 0, i_aggminitval);
13579         convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
13580
13581         if (fout->remoteVersion >= 80400)
13582         {
13583                 /* 8.4 or later; we rely on server-side code for most of the work */
13584                 char       *funcargs;
13585                 char       *funciargs;
13586
13587                 funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13588                 funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13589                 aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13590                 aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13591         }
13592         else
13593                 /* pre-8.4, do it ourselves */
13594                 aggsig = format_aggregate_signature(agginfo, fout, true);
13595
13596         aggsig_tag = format_aggregate_signature(agginfo, fout, false);
13597
13598         if (i_proparallel != -1)
13599                 proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13600         else
13601                 proparallel = NULL;
13602
13603         if (!convertok)
13604         {
13605                 write_msg(NULL, "WARNING: aggregate function %s could not be dumped correctly for this database version; ignored\n",
13606                                   aggsig);
13607
13608                 if (aggfullsig)
13609                         free(aggfullsig);
13610
13611                 free(aggsig);
13612
13613                 return;
13614         }
13615
13616         /* regproc and regtype output is already sufficiently quoted */
13617         appendPQExpBuffer(details, "    SFUNC = %s,\n    STYPE = %s",
13618                                           aggtransfn, aggtranstype);
13619
13620         if (strcmp(aggtransspace, "0") != 0)
13621         {
13622                 appendPQExpBuffer(details, ",\n    SSPACE = %s",
13623                                                   aggtransspace);
13624         }
13625
13626         if (!PQgetisnull(res, 0, i_agginitval))
13627         {
13628                 appendPQExpBufferStr(details, ",\n    INITCOND = ");
13629                 appendStringLiteralAH(details, agginitval, fout);
13630         }
13631
13632         if (strcmp(aggfinalfn, "-") != 0)
13633         {
13634                 appendPQExpBuffer(details, ",\n    FINALFUNC = %s",
13635                                                   aggfinalfn);
13636                 if (aggfinalextra)
13637                         appendPQExpBufferStr(details, ",\n    FINALFUNC_EXTRA");
13638         }
13639
13640         if (strcmp(aggcombinefn, "-") != 0)
13641                 appendPQExpBuffer(details, ",\n    COMBINEFUNC = %s", aggcombinefn);
13642
13643         if (strcmp(aggserialfn, "-") != 0)
13644                 appendPQExpBuffer(details, ",\n    SERIALFUNC = %s", aggserialfn);
13645
13646         if (strcmp(aggdeserialfn, "-") != 0)
13647                 appendPQExpBuffer(details, ",\n    DESERIALFUNC = %s", aggdeserialfn);
13648
13649         if (strcmp(aggmtransfn, "-") != 0)
13650         {
13651                 appendPQExpBuffer(details, ",\n    MSFUNC = %s,\n    MINVFUNC = %s,\n    MSTYPE = %s",
13652                                                   aggmtransfn,
13653                                                   aggminvtransfn,
13654                                                   aggmtranstype);
13655         }
13656
13657         if (strcmp(aggmtransspace, "0") != 0)
13658         {
13659                 appendPQExpBuffer(details, ",\n    MSSPACE = %s",
13660                                                   aggmtransspace);
13661         }
13662
13663         if (!PQgetisnull(res, 0, i_aggminitval))
13664         {
13665                 appendPQExpBufferStr(details, ",\n    MINITCOND = ");
13666                 appendStringLiteralAH(details, aggminitval, fout);
13667         }
13668
13669         if (strcmp(aggmfinalfn, "-") != 0)
13670         {
13671                 appendPQExpBuffer(details, ",\n    MFINALFUNC = %s",
13672                                                   aggmfinalfn);
13673                 if (aggmfinalextra)
13674                         appendPQExpBufferStr(details, ",\n    MFINALFUNC_EXTRA");
13675         }
13676
13677         aggsortconvop = convertOperatorReference(fout, aggsortop);
13678         if (aggsortconvop)
13679         {
13680                 appendPQExpBuffer(details, ",\n    SORTOP = %s",
13681                                                   aggsortconvop);
13682                 free(aggsortconvop);
13683         }
13684
13685         if (hypothetical)
13686                 appendPQExpBufferStr(details, ",\n    HYPOTHETICAL");
13687
13688         if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
13689         {
13690                 if (proparallel[0] == PROPARALLEL_SAFE)
13691                         appendPQExpBufferStr(details, ",\n    PARALLEL = safe");
13692                 else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13693                         appendPQExpBufferStr(details, ",\n    PARALLEL = restricted");
13694                 else if (proparallel[0] != PROPARALLEL_UNSAFE)
13695                         exit_horribly(NULL, "unrecognized proparallel value for function \"%s\"\n",
13696                                                   agginfo->aggfn.dobj.name);
13697         }
13698
13699         /*
13700          * DROP must be fully qualified in case same name appears in pg_catalog
13701          */
13702         appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
13703                                           fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13704                                           aggsig);
13705
13706         appendPQExpBuffer(q, "CREATE AGGREGATE %s (\n%s\n);\n",
13707                                           aggfullsig ? aggfullsig : aggsig, details->data);
13708
13709         appendPQExpBuffer(labelq, "AGGREGATE %s", aggsig);
13710
13711         if (dopt->binary_upgrade)
13712                 binary_upgrade_extension_member(q, &agginfo->aggfn.dobj, labelq->data);
13713
13714         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
13715                 ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
13716                                          agginfo->aggfn.dobj.dumpId,
13717                                          aggsig_tag,
13718                                          agginfo->aggfn.dobj.namespace->dobj.name,
13719                                          NULL,
13720                                          agginfo->aggfn.rolname,
13721                                          false, "AGGREGATE", SECTION_PRE_DATA,
13722                                          q->data, delq->data, NULL,
13723                                          NULL, 0,
13724                                          NULL, NULL);
13725
13726         /* Dump Aggregate Comments */
13727         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
13728                 dumpComment(fout, labelq->data,
13729                                         agginfo->aggfn.dobj.namespace->dobj.name,
13730                                         agginfo->aggfn.rolname,
13731                                         agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13732
13733         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
13734                 dumpSecLabel(fout, labelq->data,
13735                                          agginfo->aggfn.dobj.namespace->dobj.name,
13736                                          agginfo->aggfn.rolname,
13737                                          agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13738
13739         /*
13740          * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
13741          * command look like a function's GRANT; in particular this affects the
13742          * syntax for zero-argument aggregates and ordered-set aggregates.
13743          */
13744         free(aggsig);
13745         free(aggsig_tag);
13746
13747         aggsig = format_function_signature(fout, &agginfo->aggfn, true);
13748         aggsig_tag = format_function_signature(fout, &agginfo->aggfn, false);
13749
13750         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
13751                 dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
13752                                 "FUNCTION",
13753                                 aggsig, NULL, aggsig_tag,
13754                                 agginfo->aggfn.dobj.namespace->dobj.name,
13755                                 agginfo->aggfn.rolname, agginfo->aggfn.proacl,
13756                                 agginfo->aggfn.rproacl,
13757                                 agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
13758
13759         free(aggsig);
13760         if (aggfullsig)
13761                 free(aggfullsig);
13762         free(aggsig_tag);
13763
13764         PQclear(res);
13765
13766         destroyPQExpBuffer(query);
13767         destroyPQExpBuffer(q);
13768         destroyPQExpBuffer(delq);
13769         destroyPQExpBuffer(labelq);
13770         destroyPQExpBuffer(details);
13771 }
13772
13773 /*
13774  * dumpTSParser
13775  *        write out a single text search parser
13776  */
13777 static void
13778 dumpTSParser(Archive *fout, TSParserInfo *prsinfo)
13779 {
13780         DumpOptions *dopt = fout->dopt;
13781         PQExpBuffer q;
13782         PQExpBuffer delq;
13783         PQExpBuffer labelq;
13784
13785         /* Skip if not to be dumped */
13786         if (!prsinfo->dobj.dump || dopt->dataOnly)
13787                 return;
13788
13789         q = createPQExpBuffer();
13790         delq = createPQExpBuffer();
13791         labelq = createPQExpBuffer();
13792
13793         /* Make sure we are in proper schema */
13794         selectSourceSchema(fout, prsinfo->dobj.namespace->dobj.name);
13795
13796         appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n",
13797                                           fmtId(prsinfo->dobj.name));
13798
13799         appendPQExpBuffer(q, "    START = %s,\n",
13800                                           convertTSFunction(fout, prsinfo->prsstart));
13801         appendPQExpBuffer(q, "    GETTOKEN = %s,\n",
13802                                           convertTSFunction(fout, prsinfo->prstoken));
13803         appendPQExpBuffer(q, "    END = %s,\n",
13804                                           convertTSFunction(fout, prsinfo->prsend));
13805         if (prsinfo->prsheadline != InvalidOid)
13806                 appendPQExpBuffer(q, "    HEADLINE = %s,\n",
13807                                                   convertTSFunction(fout, prsinfo->prsheadline));
13808         appendPQExpBuffer(q, "    LEXTYPES = %s );\n",
13809                                           convertTSFunction(fout, prsinfo->prslextype));
13810
13811         /*
13812          * DROP must be fully qualified in case same name appears in pg_catalog
13813          */
13814         appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s",
13815                                           fmtId(prsinfo->dobj.namespace->dobj.name));
13816         appendPQExpBuffer(delq, ".%s;\n",
13817                                           fmtId(prsinfo->dobj.name));
13818
13819         appendPQExpBuffer(labelq, "TEXT SEARCH PARSER %s",
13820                                           fmtId(prsinfo->dobj.name));
13821
13822         if (dopt->binary_upgrade)
13823                 binary_upgrade_extension_member(q, &prsinfo->dobj, labelq->data);
13824
13825         if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13826                 ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId,
13827                                          prsinfo->dobj.name,
13828                                          prsinfo->dobj.namespace->dobj.name,
13829                                          NULL,
13830                                          "",
13831                                          false, "TEXT SEARCH PARSER", SECTION_PRE_DATA,
13832                                          q->data, delq->data, NULL,
13833                                          NULL, 0,
13834                                          NULL, NULL);
13835
13836         /* Dump Parser Comments */
13837         if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13838                 dumpComment(fout, labelq->data,
13839                                         prsinfo->dobj.namespace->dobj.name, "",
13840                                         prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId);
13841
13842         destroyPQExpBuffer(q);
13843         destroyPQExpBuffer(delq);
13844         destroyPQExpBuffer(labelq);
13845 }
13846
13847 /*
13848  * dumpTSDictionary
13849  *        write out a single text search dictionary
13850  */
13851 static void
13852 dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo)
13853 {
13854         DumpOptions *dopt = fout->dopt;
13855         PQExpBuffer q;
13856         PQExpBuffer delq;
13857         PQExpBuffer labelq;
13858         PQExpBuffer query;
13859         PGresult   *res;
13860         char       *nspname;
13861         char       *tmplname;
13862
13863         /* Skip if not to be dumped */
13864         if (!dictinfo->dobj.dump || dopt->dataOnly)
13865                 return;
13866
13867         q = createPQExpBuffer();
13868         delq = createPQExpBuffer();
13869         labelq = createPQExpBuffer();
13870         query = createPQExpBuffer();
13871
13872         /* Fetch name and namespace of the dictionary's template */
13873         selectSourceSchema(fout, "pg_catalog");
13874         appendPQExpBuffer(query, "SELECT nspname, tmplname "
13875                                           "FROM pg_ts_template p, pg_namespace n "
13876                                           "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
13877                                           dictinfo->dicttemplate);
13878         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13879         nspname = PQgetvalue(res, 0, 0);
13880         tmplname = PQgetvalue(res, 0, 1);
13881
13882         /* Make sure we are in proper schema */
13883         selectSourceSchema(fout, dictinfo->dobj.namespace->dobj.name);
13884
13885         appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n",
13886                                           fmtId(dictinfo->dobj.name));
13887
13888         appendPQExpBufferStr(q, "    TEMPLATE = ");
13889         if (strcmp(nspname, dictinfo->dobj.namespace->dobj.name) != 0)
13890                 appendPQExpBuffer(q, "%s.", fmtId(nspname));
13891         appendPQExpBufferStr(q, fmtId(tmplname));
13892
13893         PQclear(res);
13894
13895         /* the dictinitoption can be dumped straight into the command */
13896         if (dictinfo->dictinitoption)
13897                 appendPQExpBuffer(q, ",\n    %s", dictinfo->dictinitoption);
13898
13899         appendPQExpBufferStr(q, " );\n");
13900
13901         /*
13902          * DROP must be fully qualified in case same name appears in pg_catalog
13903          */
13904         appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s",
13905                                           fmtId(dictinfo->dobj.namespace->dobj.name));
13906         appendPQExpBuffer(delq, ".%s;\n",
13907                                           fmtId(dictinfo->dobj.name));
13908
13909         appendPQExpBuffer(labelq, "TEXT SEARCH DICTIONARY %s",
13910                                           fmtId(dictinfo->dobj.name));
13911
13912         if (dopt->binary_upgrade)
13913                 binary_upgrade_extension_member(q, &dictinfo->dobj, labelq->data);
13914
13915         if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13916                 ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId,
13917                                          dictinfo->dobj.name,
13918                                          dictinfo->dobj.namespace->dobj.name,
13919                                          NULL,
13920                                          dictinfo->rolname,
13921                                          false, "TEXT SEARCH DICTIONARY", SECTION_PRE_DATA,
13922                                          q->data, delq->data, NULL,
13923                                          NULL, 0,
13924                                          NULL, NULL);
13925
13926         /* Dump Dictionary Comments */
13927         if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13928                 dumpComment(fout, labelq->data,
13929                                         dictinfo->dobj.namespace->dobj.name, dictinfo->rolname,
13930                                         dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId);
13931
13932         destroyPQExpBuffer(q);
13933         destroyPQExpBuffer(delq);
13934         destroyPQExpBuffer(labelq);
13935         destroyPQExpBuffer(query);
13936 }
13937
13938 /*
13939  * dumpTSTemplate
13940  *        write out a single text search template
13941  */
13942 static void
13943 dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo)
13944 {
13945         DumpOptions *dopt = fout->dopt;
13946         PQExpBuffer q;
13947         PQExpBuffer delq;
13948         PQExpBuffer labelq;
13949
13950         /* Skip if not to be dumped */
13951         if (!tmplinfo->dobj.dump || dopt->dataOnly)
13952                 return;
13953
13954         q = createPQExpBuffer();
13955         delq = createPQExpBuffer();
13956         labelq = createPQExpBuffer();
13957
13958         /* Make sure we are in proper schema */
13959         selectSourceSchema(fout, tmplinfo->dobj.namespace->dobj.name);
13960
13961         appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n",
13962                                           fmtId(tmplinfo->dobj.name));
13963
13964         if (tmplinfo->tmplinit != InvalidOid)
13965                 appendPQExpBuffer(q, "    INIT = %s,\n",
13966                                                   convertTSFunction(fout, tmplinfo->tmplinit));
13967         appendPQExpBuffer(q, "    LEXIZE = %s );\n",
13968                                           convertTSFunction(fout, tmplinfo->tmpllexize));
13969
13970         /*
13971          * DROP must be fully qualified in case same name appears in pg_catalog
13972          */
13973         appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s",
13974                                           fmtId(tmplinfo->dobj.namespace->dobj.name));
13975         appendPQExpBuffer(delq, ".%s;\n",
13976                                           fmtId(tmplinfo->dobj.name));
13977
13978         appendPQExpBuffer(labelq, "TEXT SEARCH TEMPLATE %s",
13979                                           fmtId(tmplinfo->dobj.name));
13980
13981         if (dopt->binary_upgrade)
13982                 binary_upgrade_extension_member(q, &tmplinfo->dobj, labelq->data);
13983
13984         if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13985                 ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId,
13986                                          tmplinfo->dobj.name,
13987                                          tmplinfo->dobj.namespace->dobj.name,
13988                                          NULL,
13989                                          "",
13990                                          false, "TEXT SEARCH TEMPLATE", SECTION_PRE_DATA,
13991                                          q->data, delq->data, NULL,
13992                                          NULL, 0,
13993                                          NULL, NULL);
13994
13995         /* Dump Template Comments */
13996         if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13997                 dumpComment(fout, labelq->data,
13998                                         tmplinfo->dobj.namespace->dobj.name, "",
13999                                         tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId);
14000
14001         destroyPQExpBuffer(q);
14002         destroyPQExpBuffer(delq);
14003         destroyPQExpBuffer(labelq);
14004 }
14005
14006 /*
14007  * dumpTSConfig
14008  *        write out a single text search configuration
14009  */
14010 static void
14011 dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo)
14012 {
14013         DumpOptions *dopt = fout->dopt;
14014         PQExpBuffer q;
14015         PQExpBuffer delq;
14016         PQExpBuffer labelq;
14017         PQExpBuffer query;
14018         PGresult   *res;
14019         char       *nspname;
14020         char       *prsname;
14021         int                     ntups,
14022                                 i;
14023         int                     i_tokenname;
14024         int                     i_dictname;
14025
14026         /* Skip if not to be dumped */
14027         if (!cfginfo->dobj.dump || dopt->dataOnly)
14028                 return;
14029
14030         q = createPQExpBuffer();
14031         delq = createPQExpBuffer();
14032         labelq = createPQExpBuffer();
14033         query = createPQExpBuffer();
14034
14035         /* Fetch name and namespace of the config's parser */
14036         selectSourceSchema(fout, "pg_catalog");
14037         appendPQExpBuffer(query, "SELECT nspname, prsname "
14038                                           "FROM pg_ts_parser p, pg_namespace n "
14039                                           "WHERE p.oid = '%u' AND n.oid = prsnamespace",
14040                                           cfginfo->cfgparser);
14041         res = ExecuteSqlQueryForSingleRow(fout, query->data);
14042         nspname = PQgetvalue(res, 0, 0);
14043         prsname = PQgetvalue(res, 0, 1);
14044
14045         /* Make sure we are in proper schema */
14046         selectSourceSchema(fout, cfginfo->dobj.namespace->dobj.name);
14047
14048         appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n",
14049                                           fmtId(cfginfo->dobj.name));
14050
14051         appendPQExpBufferStr(q, "    PARSER = ");
14052         if (strcmp(nspname, cfginfo->dobj.namespace->dobj.name) != 0)
14053                 appendPQExpBuffer(q, "%s.", fmtId(nspname));
14054         appendPQExpBuffer(q, "%s );\n", fmtId(prsname));
14055
14056         PQclear(res);
14057
14058         resetPQExpBuffer(query);
14059         appendPQExpBuffer(query,
14060                                           "SELECT\n"
14061                                           "  ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n"
14062                                           "    WHERE t.tokid = m.maptokentype ) AS tokenname,\n"
14063                                           "  m.mapdict::pg_catalog.regdictionary AS dictname\n"
14064                                           "FROM pg_catalog.pg_ts_config_map AS m\n"
14065                                           "WHERE m.mapcfg = '%u'\n"
14066                                           "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
14067                                           cfginfo->cfgparser, cfginfo->dobj.catId.oid);
14068
14069         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14070         ntups = PQntuples(res);
14071
14072         i_tokenname = PQfnumber(res, "tokenname");
14073         i_dictname = PQfnumber(res, "dictname");
14074
14075         for (i = 0; i < ntups; i++)
14076         {
14077                 char       *tokenname = PQgetvalue(res, i, i_tokenname);
14078                 char       *dictname = PQgetvalue(res, i, i_dictname);
14079
14080                 if (i == 0 ||
14081                         strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0)
14082                 {
14083                         /* starting a new token type, so start a new command */
14084                         if (i > 0)
14085                                 appendPQExpBufferStr(q, ";\n");
14086                         appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n",
14087                                                           fmtId(cfginfo->dobj.name));
14088                         /* tokenname needs quoting, dictname does NOT */
14089                         appendPQExpBuffer(q, "    ADD MAPPING FOR %s WITH %s",
14090                                                           fmtId(tokenname), dictname);
14091                 }
14092                 else
14093                         appendPQExpBuffer(q, ", %s", dictname);
14094         }
14095
14096         if (ntups > 0)
14097                 appendPQExpBufferStr(q, ";\n");
14098
14099         PQclear(res);
14100
14101         /*
14102          * DROP must be fully qualified in case same name appears in pg_catalog
14103          */
14104         appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s",
14105                                           fmtId(cfginfo->dobj.namespace->dobj.name));
14106         appendPQExpBuffer(delq, ".%s;\n",
14107                                           fmtId(cfginfo->dobj.name));
14108
14109         appendPQExpBuffer(labelq, "TEXT SEARCH CONFIGURATION %s",
14110                                           fmtId(cfginfo->dobj.name));
14111
14112         if (dopt->binary_upgrade)
14113                 binary_upgrade_extension_member(q, &cfginfo->dobj, labelq->data);
14114
14115         if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14116                 ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId,
14117                                          cfginfo->dobj.name,
14118                                          cfginfo->dobj.namespace->dobj.name,
14119                                          NULL,
14120                                          cfginfo->rolname,
14121                                          false, "TEXT SEARCH CONFIGURATION", SECTION_PRE_DATA,
14122                                          q->data, delq->data, NULL,
14123                                          NULL, 0,
14124                                          NULL, NULL);
14125
14126         /* Dump Configuration Comments */
14127         if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14128                 dumpComment(fout, labelq->data,
14129                                         cfginfo->dobj.namespace->dobj.name, cfginfo->rolname,
14130                                         cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId);
14131
14132         destroyPQExpBuffer(q);
14133         destroyPQExpBuffer(delq);
14134         destroyPQExpBuffer(labelq);
14135         destroyPQExpBuffer(query);
14136 }
14137
14138 /*
14139  * dumpForeignDataWrapper
14140  *        write out a single foreign-data wrapper definition
14141  */
14142 static void
14143 dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo)
14144 {
14145         DumpOptions *dopt = fout->dopt;
14146         PQExpBuffer q;
14147         PQExpBuffer delq;
14148         PQExpBuffer labelq;
14149         char       *qfdwname;
14150
14151         /* Skip if not to be dumped */
14152         if (!fdwinfo->dobj.dump || dopt->dataOnly)
14153                 return;
14154
14155         q = createPQExpBuffer();
14156         delq = createPQExpBuffer();
14157         labelq = createPQExpBuffer();
14158
14159         qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name));
14160
14161         appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s",
14162                                           qfdwname);
14163
14164         if (strcmp(fdwinfo->fdwhandler, "-") != 0)
14165                 appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler);
14166
14167         if (strcmp(fdwinfo->fdwvalidator, "-") != 0)
14168                 appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator);
14169
14170         if (strlen(fdwinfo->fdwoptions) > 0)
14171                 appendPQExpBuffer(q, " OPTIONS (\n    %s\n)", fdwinfo->fdwoptions);
14172
14173         appendPQExpBufferStr(q, ";\n");
14174
14175         appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n",
14176                                           qfdwname);
14177
14178         appendPQExpBuffer(labelq, "FOREIGN DATA WRAPPER %s",
14179                                           qfdwname);
14180
14181         if (dopt->binary_upgrade)
14182                 binary_upgrade_extension_member(q, &fdwinfo->dobj, labelq->data);
14183
14184         if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14185                 ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
14186                                          fdwinfo->dobj.name,
14187                                          NULL,
14188                                          NULL,
14189                                          fdwinfo->rolname,
14190                                          false, "FOREIGN DATA WRAPPER", SECTION_PRE_DATA,
14191                                          q->data, delq->data, NULL,
14192                                          NULL, 0,
14193                                          NULL, NULL);
14194
14195         /* Handle the ACL */
14196         if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL)
14197                 dumpACL(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
14198                                 "FOREIGN DATA WRAPPER",
14199                                 qfdwname, NULL, fdwinfo->dobj.name,
14200                                 NULL, fdwinfo->rolname,
14201                                 fdwinfo->fdwacl, fdwinfo->rfdwacl,
14202                                 fdwinfo->initfdwacl, fdwinfo->initrfdwacl);
14203
14204         /* Dump Foreign Data Wrapper Comments */
14205         if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14206                 dumpComment(fout, labelq->data,
14207                                         NULL, fdwinfo->rolname,
14208                                         fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
14209
14210         free(qfdwname);
14211
14212         destroyPQExpBuffer(q);
14213         destroyPQExpBuffer(delq);
14214         destroyPQExpBuffer(labelq);
14215 }
14216
14217 /*
14218  * dumpForeignServer
14219  *        write out a foreign server definition
14220  */
14221 static void
14222 dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo)
14223 {
14224         DumpOptions *dopt = fout->dopt;
14225         PQExpBuffer q;
14226         PQExpBuffer delq;
14227         PQExpBuffer labelq;
14228         PQExpBuffer query;
14229         PGresult   *res;
14230         char       *qsrvname;
14231         char       *fdwname;
14232
14233         /* Skip if not to be dumped */
14234         if (!srvinfo->dobj.dump || dopt->dataOnly)
14235                 return;
14236
14237         q = createPQExpBuffer();
14238         delq = createPQExpBuffer();
14239         labelq = createPQExpBuffer();
14240         query = createPQExpBuffer();
14241
14242         qsrvname = pg_strdup(fmtId(srvinfo->dobj.name));
14243
14244         /* look up the foreign-data wrapper */
14245         selectSourceSchema(fout, "pg_catalog");
14246         appendPQExpBuffer(query, "SELECT fdwname "
14247                                           "FROM pg_foreign_data_wrapper w "
14248                                           "WHERE w.oid = '%u'",
14249                                           srvinfo->srvfdw);
14250         res = ExecuteSqlQueryForSingleRow(fout, query->data);
14251         fdwname = PQgetvalue(res, 0, 0);
14252
14253         appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
14254         if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0)
14255         {
14256                 appendPQExpBufferStr(q, " TYPE ");
14257                 appendStringLiteralAH(q, srvinfo->srvtype, fout);
14258         }
14259         if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0)
14260         {
14261                 appendPQExpBufferStr(q, " VERSION ");
14262                 appendStringLiteralAH(q, srvinfo->srvversion, fout);
14263         }
14264
14265         appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER ");
14266         appendPQExpBufferStr(q, fmtId(fdwname));
14267
14268         if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0)
14269                 appendPQExpBuffer(q, " OPTIONS (\n    %s\n)", srvinfo->srvoptions);
14270
14271         appendPQExpBufferStr(q, ";\n");
14272
14273         appendPQExpBuffer(delq, "DROP SERVER %s;\n",
14274                                           qsrvname);
14275
14276         appendPQExpBuffer(labelq, "SERVER %s", qsrvname);
14277
14278         if (dopt->binary_upgrade)
14279                 binary_upgrade_extension_member(q, &srvinfo->dobj, labelq->data);
14280
14281         if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14282                 ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
14283                                          srvinfo->dobj.name,
14284                                          NULL,
14285                                          NULL,
14286                                          srvinfo->rolname,
14287                                          false, "SERVER", SECTION_PRE_DATA,
14288                                          q->data, delq->data, NULL,
14289                                          NULL, 0,
14290                                          NULL, NULL);
14291
14292         /* Handle the ACL */
14293         if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL)
14294                 dumpACL(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
14295                                 "FOREIGN SERVER",
14296                                 qsrvname, NULL, srvinfo->dobj.name,
14297                                 NULL, srvinfo->rolname,
14298                                 srvinfo->srvacl, srvinfo->rsrvacl,
14299                                 srvinfo->initsrvacl, srvinfo->initrsrvacl);
14300
14301         /* Dump user mappings */
14302         if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP)
14303                 dumpUserMappings(fout,
14304                                                  srvinfo->dobj.name, NULL,
14305                                                  srvinfo->rolname,
14306                                                  srvinfo->dobj.catId, srvinfo->dobj.dumpId);
14307
14308         /* Dump Foreign Server Comments */
14309         if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14310                 dumpComment(fout, labelq->data,
14311                                         NULL, srvinfo->rolname,
14312                                         srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
14313
14314         free(qsrvname);
14315
14316         destroyPQExpBuffer(q);
14317         destroyPQExpBuffer(delq);
14318         destroyPQExpBuffer(labelq);
14319         destroyPQExpBuffer(query);
14320 }
14321
14322 /*
14323  * dumpUserMappings
14324  *
14325  * This routine is used to dump any user mappings associated with the
14326  * server handed to this routine. Should be called after ArchiveEntry()
14327  * for the server.
14328  */
14329 static void
14330 dumpUserMappings(Archive *fout,
14331                                  const char *servername, const char *namespace,
14332                                  const char *owner,
14333                                  CatalogId catalogId, DumpId dumpId)
14334 {
14335         PQExpBuffer q;
14336         PQExpBuffer delq;
14337         PQExpBuffer query;
14338         PQExpBuffer tag;
14339         PGresult   *res;
14340         int                     ntups;
14341         int                     i_usename;
14342         int                     i_umoptions;
14343         int                     i;
14344
14345         q = createPQExpBuffer();
14346         tag = createPQExpBuffer();
14347         delq = createPQExpBuffer();
14348         query = createPQExpBuffer();
14349
14350         /*
14351          * We read from the publicly accessible view pg_user_mappings, so as not
14352          * to fail if run by a non-superuser.  Note that the view will show
14353          * umoptions as null if the user hasn't got privileges for the associated
14354          * server; this means that pg_dump will dump such a mapping, but with no
14355          * OPTIONS clause.  A possible alternative is to skip such mappings
14356          * altogether, but it's not clear that that's an improvement.
14357          */
14358         selectSourceSchema(fout, "pg_catalog");
14359
14360         appendPQExpBuffer(query,
14361                                           "SELECT usename, "
14362                                           "array_to_string(ARRAY("
14363                                           "SELECT quote_ident(option_name) || ' ' || "
14364                                           "quote_literal(option_value) "
14365                                           "FROM pg_options_to_table(umoptions) "
14366                                           "ORDER BY option_name"
14367                                           "), E',\n    ') AS umoptions "
14368                                           "FROM pg_user_mappings "
14369                                           "WHERE srvid = '%u' "
14370                                           "ORDER BY usename",
14371                                           catalogId.oid);
14372
14373         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14374
14375         ntups = PQntuples(res);
14376         i_usename = PQfnumber(res, "usename");
14377         i_umoptions = PQfnumber(res, "umoptions");
14378
14379         for (i = 0; i < ntups; i++)
14380         {
14381                 char       *usename;
14382                 char       *umoptions;
14383
14384                 usename = PQgetvalue(res, i, i_usename);
14385                 umoptions = PQgetvalue(res, i, i_umoptions);
14386
14387                 resetPQExpBuffer(q);
14388                 appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
14389                 appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
14390
14391                 if (umoptions && strlen(umoptions) > 0)
14392                         appendPQExpBuffer(q, " OPTIONS (\n    %s\n)", umoptions);
14393
14394                 appendPQExpBufferStr(q, ";\n");
14395
14396                 resetPQExpBuffer(delq);
14397                 appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
14398                 appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
14399
14400                 resetPQExpBuffer(tag);
14401                 appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
14402                                                   usename, servername);
14403
14404                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14405                                          tag->data,
14406                                          namespace,
14407                                          NULL,
14408                                          owner, false,
14409                                          "USER MAPPING", SECTION_PRE_DATA,
14410                                          q->data, delq->data, NULL,
14411                                          &dumpId, 1,
14412                                          NULL, NULL);
14413         }
14414
14415         PQclear(res);
14416
14417         destroyPQExpBuffer(query);
14418         destroyPQExpBuffer(delq);
14419         destroyPQExpBuffer(tag);
14420         destroyPQExpBuffer(q);
14421 }
14422
14423 /*
14424  * Write out default privileges information
14425  */
14426 static void
14427 dumpDefaultACL(Archive *fout, DefaultACLInfo *daclinfo)
14428 {
14429         DumpOptions *dopt = fout->dopt;
14430         PQExpBuffer q;
14431         PQExpBuffer tag;
14432         const char *type;
14433
14434         /* Skip if not to be dumped */
14435         if (!daclinfo->dobj.dump || dopt->dataOnly || dopt->aclsSkip)
14436                 return;
14437
14438         q = createPQExpBuffer();
14439         tag = createPQExpBuffer();
14440
14441         switch (daclinfo->defaclobjtype)
14442         {
14443                 case DEFACLOBJ_RELATION:
14444                         type = "TABLES";
14445                         break;
14446                 case DEFACLOBJ_SEQUENCE:
14447                         type = "SEQUENCES";
14448                         break;
14449                 case DEFACLOBJ_FUNCTION:
14450                         type = "FUNCTIONS";
14451                         break;
14452                 case DEFACLOBJ_TYPE:
14453                         type = "TYPES";
14454                         break;
14455                 case DEFACLOBJ_NAMESPACE:
14456                         type = "SCHEMAS";
14457                         break;
14458                 default:
14459                         /* shouldn't get here */
14460                         exit_horribly(NULL,
14461                                                   "unrecognized object type in default privileges: %d\n",
14462                                                   (int) daclinfo->defaclobjtype);
14463                         type = "";                      /* keep compiler quiet */
14464         }
14465
14466         appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
14467
14468         /* build the actual command(s) for this tuple */
14469         if (!buildDefaultACLCommands(type,
14470                                                                  daclinfo->dobj.namespace != NULL ?
14471                                                                  daclinfo->dobj.namespace->dobj.name : NULL,
14472                                                                  daclinfo->defaclacl,
14473                                                                  daclinfo->rdefaclacl,
14474                                                                  daclinfo->initdefaclacl,
14475                                                                  daclinfo->initrdefaclacl,
14476                                                                  daclinfo->defaclrole,
14477                                                                  fout->remoteVersion,
14478                                                                  q))
14479                 exit_horribly(NULL, "could not parse default ACL list (%s)\n",
14480                                           daclinfo->defaclacl);
14481
14482         if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
14483                 ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
14484                                          tag->data,
14485                                          daclinfo->dobj.namespace ? daclinfo->dobj.namespace->dobj.name : NULL,
14486                                          NULL,
14487                                          daclinfo->defaclrole,
14488                                          false, "DEFAULT ACL", SECTION_POST_DATA,
14489                                          q->data, "", NULL,
14490                                          NULL, 0,
14491                                          NULL, NULL);
14492
14493         destroyPQExpBuffer(tag);
14494         destroyPQExpBuffer(q);
14495 }
14496
14497 /*----------
14498  * Write out grant/revoke information
14499  *
14500  * 'objCatId' is the catalog ID of the underlying object.
14501  * 'objDumpId' is the dump ID of the underlying object.
14502  * 'type' must be one of
14503  *              TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
14504  *              FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT.
14505  * 'name' is the formatted name of the object.  Must be quoted etc. already.
14506  * 'subname' is the formatted name of the sub-object, if any.  Must be quoted.
14507  * 'tag' is the tag for the archive entry (typ. unquoted name of object).
14508  * 'nspname' is the namespace the object is in (NULL if none).
14509  * 'owner' is the owner, NULL if there is no owner (for languages).
14510  * 'acls' contains the ACL string of the object from the appropriate system
14511  *              catalog field; it will be passed to buildACLCommands for building the
14512  *              appropriate GRANT commands.
14513  * 'racls' contains the ACL string of any initial-but-now-revoked ACLs of the
14514  *              object; it will be passed to buildACLCommands for building the
14515  *              appropriate REVOKE commands.
14516  * 'initacls' In binary-upgrade mode, ACL string of the object's initial
14517  *              privileges, to be recorded into pg_init_privs
14518  * 'initracls' In binary-upgrade mode, ACL string of the object's
14519  *              revoked-from-default privileges, to be recorded into pg_init_privs
14520  *
14521  * NB: initacls/initracls are needed because extensions can set privileges on
14522  * an object during the extension's script file and we record those into
14523  * pg_init_privs as that object's initial privileges.
14524  *----------
14525  */
14526 static void
14527 dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
14528                 const char *type, const char *name, const char *subname,
14529                 const char *tag, const char *nspname, const char *owner,
14530                 const char *acls, const char *racls,
14531                 const char *initacls, const char *initracls)
14532 {
14533         DumpOptions *dopt = fout->dopt;
14534         PQExpBuffer sql;
14535
14536         /* Do nothing if ACL dump is not enabled */
14537         if (dopt->aclsSkip)
14538                 return;
14539
14540         /* --data-only skips ACLs *except* BLOB ACLs */
14541         if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14542                 return;
14543
14544         sql = createPQExpBuffer();
14545
14546         /*
14547          * Check to see if this object has had any initial ACLs included for it.
14548          * If so, we are in binary upgrade mode and these are the ACLs to turn
14549          * into GRANT and REVOKE statements to set and record the initial
14550          * privileges for an extension object.  Let the backend know that these
14551          * are to be recorded by calling binary_upgrade_set_record_init_privs()
14552          * before and after.
14553          */
14554         if (strlen(initacls) != 0 || strlen(initracls) != 0)
14555         {
14556                 appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14557                 if (!buildACLCommands(name, subname, type, initacls, initracls, owner,
14558                                                           "", fout->remoteVersion, sql))
14559                         exit_horribly(NULL,
14560                                                   "could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14561                                                   initacls, initracls, name, type);
14562                 appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14563         }
14564
14565         if (!buildACLCommands(name, subname, type, acls, racls, owner,
14566                                                   "", fout->remoteVersion, sql))
14567                 exit_horribly(NULL,
14568                                           "could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14569                                           acls, racls, name, type);
14570
14571         if (sql->len > 0)
14572                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14573                                          tag, nspname,
14574                                          NULL,
14575                                          owner ? owner : "",
14576                                          false, "ACL", SECTION_NONE,
14577                                          sql->data, "", NULL,
14578                                          &(objDumpId), 1,
14579                                          NULL, NULL);
14580
14581         destroyPQExpBuffer(sql);
14582 }
14583
14584 /*
14585  * dumpSecLabel
14586  *
14587  * This routine is used to dump any security labels associated with the
14588  * object handed to this routine. The routine takes a constant character
14589  * string for the target part of the security-label command, plus
14590  * the namespace and owner of the object (for labeling the ArchiveEntry),
14591  * plus catalog ID and subid which are the lookup key for pg_seclabel,
14592  * plus the dump ID for the object (for setting a dependency).
14593  * If a matching pg_seclabel entry is found, it is dumped.
14594  *
14595  * Note: although this routine takes a dumpId for dependency purposes,
14596  * that purpose is just to mark the dependency in the emitted dump file
14597  * for possible future use by pg_restore.  We do NOT use it for determining
14598  * ordering of the label in the dump file, because this routine is called
14599  * after dependency sorting occurs.  This routine should be called just after
14600  * calling ArchiveEntry() for the specified object.
14601  */
14602 static void
14603 dumpSecLabel(Archive *fout, const char *target,
14604                          const char *namespace, const char *owner,
14605                          CatalogId catalogId, int subid, DumpId dumpId)
14606 {
14607         DumpOptions *dopt = fout->dopt;
14608         SecLabelItem *labels;
14609         int                     nlabels;
14610         int                     i;
14611         PQExpBuffer query;
14612
14613         /* do nothing, if --no-security-labels is supplied */
14614         if (dopt->no_security_labels)
14615                 return;
14616
14617         /* Comments are schema not data ... except blob comments are data */
14618         if (strncmp(target, "LARGE OBJECT ", 13) != 0)
14619         {
14620                 if (dopt->dataOnly)
14621                         return;
14622         }
14623         else
14624         {
14625                 /* We do dump blob security labels in binary-upgrade mode */
14626                 if (dopt->schemaOnly && !dopt->binary_upgrade)
14627                         return;
14628         }
14629
14630         /* Search for security labels associated with catalogId, using table */
14631         nlabels = findSecLabels(fout, catalogId.tableoid, catalogId.oid, &labels);
14632
14633         query = createPQExpBuffer();
14634
14635         for (i = 0; i < nlabels; i++)
14636         {
14637                 /*
14638                  * Ignore label entries for which the subid doesn't match.
14639                  */
14640                 if (labels[i].objsubid != subid)
14641                         continue;
14642
14643                 appendPQExpBuffer(query,
14644                                                   "SECURITY LABEL FOR %s ON %s IS ",
14645                                                   fmtId(labels[i].provider), target);
14646                 appendStringLiteralAH(query, labels[i].label, fout);
14647                 appendPQExpBufferStr(query, ";\n");
14648         }
14649
14650         if (query->len > 0)
14651         {
14652                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14653                                          target, namespace, NULL, owner,
14654                                          false, "SECURITY LABEL", SECTION_NONE,
14655                                          query->data, "", NULL,
14656                                          &(dumpId), 1,
14657                                          NULL, NULL);
14658         }
14659         destroyPQExpBuffer(query);
14660 }
14661
14662 /*
14663  * dumpTableSecLabel
14664  *
14665  * As above, but dump security label for both the specified table (or view)
14666  * and its columns.
14667  */
14668 static void
14669 dumpTableSecLabel(Archive *fout, TableInfo *tbinfo, const char *reltypename)
14670 {
14671         DumpOptions *dopt = fout->dopt;
14672         SecLabelItem *labels;
14673         int                     nlabels;
14674         int                     i;
14675         PQExpBuffer query;
14676         PQExpBuffer target;
14677
14678         /* do nothing, if --no-security-labels is supplied */
14679         if (dopt->no_security_labels)
14680                 return;
14681
14682         /* SecLabel are SCHEMA not data */
14683         if (dopt->dataOnly)
14684                 return;
14685
14686         /* Search for comments associated with relation, using table */
14687         nlabels = findSecLabels(fout,
14688                                                         tbinfo->dobj.catId.tableoid,
14689                                                         tbinfo->dobj.catId.oid,
14690                                                         &labels);
14691
14692         /* If security labels exist, build SECURITY LABEL statements */
14693         if (nlabels <= 0)
14694                 return;
14695
14696         query = createPQExpBuffer();
14697         target = createPQExpBuffer();
14698
14699         for (i = 0; i < nlabels; i++)
14700         {
14701                 const char *colname;
14702                 const char *provider = labels[i].provider;
14703                 const char *label = labels[i].label;
14704                 int                     objsubid = labels[i].objsubid;
14705
14706                 resetPQExpBuffer(target);
14707                 if (objsubid == 0)
14708                 {
14709                         appendPQExpBuffer(target, "%s %s", reltypename,
14710                                                           fmtId(tbinfo->dobj.name));
14711                 }
14712                 else
14713                 {
14714                         colname = getAttrName(objsubid, tbinfo);
14715                         /* first fmtId result must be consumed before calling it again */
14716                         appendPQExpBuffer(target, "COLUMN %s", fmtId(tbinfo->dobj.name));
14717                         appendPQExpBuffer(target, ".%s", fmtId(colname));
14718                 }
14719                 appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ",
14720                                                   fmtId(provider), target->data);
14721                 appendStringLiteralAH(query, label, fout);
14722                 appendPQExpBufferStr(query, ";\n");
14723         }
14724         if (query->len > 0)
14725         {
14726                 resetPQExpBuffer(target);
14727                 appendPQExpBuffer(target, "%s %s", reltypename,
14728                                                   fmtId(tbinfo->dobj.name));
14729                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14730                                          target->data,
14731                                          tbinfo->dobj.namespace->dobj.name,
14732                                          NULL, tbinfo->rolname,
14733                                          false, "SECURITY LABEL", SECTION_NONE,
14734                                          query->data, "", NULL,
14735                                          &(tbinfo->dobj.dumpId), 1,
14736                                          NULL, NULL);
14737         }
14738         destroyPQExpBuffer(query);
14739         destroyPQExpBuffer(target);
14740 }
14741
14742 /*
14743  * findSecLabels
14744  *
14745  * Find the security label(s), if any, associated with the given object.
14746  * All the objsubid values associated with the given classoid/objoid are
14747  * found with one search.
14748  */
14749 static int
14750 findSecLabels(Archive *fout, Oid classoid, Oid objoid, SecLabelItem **items)
14751 {
14752         /* static storage for table of security labels */
14753         static SecLabelItem *labels = NULL;
14754         static int      nlabels = -1;
14755
14756         SecLabelItem *middle = NULL;
14757         SecLabelItem *low;
14758         SecLabelItem *high;
14759         int                     nmatch;
14760
14761         /* Get security labels if we didn't already */
14762         if (nlabels < 0)
14763                 nlabels = collectSecLabels(fout, &labels);
14764
14765         if (nlabels <= 0)                       /* no labels, so no match is possible */
14766         {
14767                 *items = NULL;
14768                 return 0;
14769         }
14770
14771         /*
14772          * Do binary search to find some item matching the object.
14773          */
14774         low = &labels[0];
14775         high = &labels[nlabels - 1];
14776         while (low <= high)
14777         {
14778                 middle = low + (high - low) / 2;
14779
14780                 if (classoid < middle->classoid)
14781                         high = middle - 1;
14782                 else if (classoid > middle->classoid)
14783                         low = middle + 1;
14784                 else if (objoid < middle->objoid)
14785                         high = middle - 1;
14786                 else if (objoid > middle->objoid)
14787                         low = middle + 1;
14788                 else
14789                         break;                          /* found a match */
14790         }
14791
14792         if (low > high)                         /* no matches */
14793         {
14794                 *items = NULL;
14795                 return 0;
14796         }
14797
14798         /*
14799          * Now determine how many items match the object.  The search loop
14800          * invariant still holds: only items between low and high inclusive could
14801          * match.
14802          */
14803         nmatch = 1;
14804         while (middle > low)
14805         {
14806                 if (classoid != middle[-1].classoid ||
14807                         objoid != middle[-1].objoid)
14808                         break;
14809                 middle--;
14810                 nmatch++;
14811         }
14812
14813         *items = middle;
14814
14815         middle += nmatch;
14816         while (middle <= high)
14817         {
14818                 if (classoid != middle->classoid ||
14819                         objoid != middle->objoid)
14820                         break;
14821                 middle++;
14822                 nmatch++;
14823         }
14824
14825         return nmatch;
14826 }
14827
14828 /*
14829  * collectSecLabels
14830  *
14831  * Construct a table of all security labels available for database objects.
14832  * It's much faster to pull them all at once.
14833  *
14834  * The table is sorted by classoid/objid/objsubid for speed in lookup.
14835  */
14836 static int
14837 collectSecLabels(Archive *fout, SecLabelItem **items)
14838 {
14839         PGresult   *res;
14840         PQExpBuffer query;
14841         int                     i_label;
14842         int                     i_provider;
14843         int                     i_classoid;
14844         int                     i_objoid;
14845         int                     i_objsubid;
14846         int                     ntups;
14847         int                     i;
14848         SecLabelItem *labels;
14849
14850         query = createPQExpBuffer();
14851
14852         appendPQExpBufferStr(query,
14853                                                  "SELECT label, provider, classoid, objoid, objsubid "
14854                                                  "FROM pg_catalog.pg_seclabel "
14855                                                  "ORDER BY classoid, objoid, objsubid");
14856
14857         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14858
14859         /* Construct lookup table containing OIDs in numeric form */
14860         i_label = PQfnumber(res, "label");
14861         i_provider = PQfnumber(res, "provider");
14862         i_classoid = PQfnumber(res, "classoid");
14863         i_objoid = PQfnumber(res, "objoid");
14864         i_objsubid = PQfnumber(res, "objsubid");
14865
14866         ntups = PQntuples(res);
14867
14868         labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
14869
14870         for (i = 0; i < ntups; i++)
14871         {
14872                 labels[i].label = PQgetvalue(res, i, i_label);
14873                 labels[i].provider = PQgetvalue(res, i, i_provider);
14874                 labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
14875                 labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
14876                 labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
14877         }
14878
14879         /* Do NOT free the PGresult since we are keeping pointers into it */
14880         destroyPQExpBuffer(query);
14881
14882         *items = labels;
14883         return ntups;
14884 }
14885
14886 /*
14887  * dumpTable
14888  *        write out to fout the declarations (not data) of a user-defined table
14889  */
14890 static void
14891 dumpTable(Archive *fout, TableInfo *tbinfo)
14892 {
14893         DumpOptions *dopt = fout->dopt;
14894         char       *namecopy;
14895
14896         /*
14897          * noop if we are not dumping anything about this table, or if we are
14898          * doing a data-only dump
14899          */
14900         if (!tbinfo->dobj.dump || dopt->dataOnly)
14901                 return;
14902
14903         if (tbinfo->relkind == RELKIND_SEQUENCE)
14904                 dumpSequence(fout, tbinfo);
14905         else
14906                 dumpTableSchema(fout, tbinfo);
14907
14908         /* Handle the ACL here */
14909         namecopy = pg_strdup(fmtId(tbinfo->dobj.name));
14910         if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
14911                 dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
14912                                 (tbinfo->relkind == RELKIND_SEQUENCE) ? "SEQUENCE" :
14913                                 "TABLE",
14914                                 namecopy, NULL, tbinfo->dobj.name,
14915                                 tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
14916                                 tbinfo->relacl, tbinfo->rrelacl,
14917                                 tbinfo->initrelacl, tbinfo->initrrelacl);
14918
14919         /*
14920          * Handle column ACLs, if any.  Note: we pull these with a separate query
14921          * rather than trying to fetch them during getTableAttrs, so that we won't
14922          * miss ACLs on system columns.
14923          */
14924         if (fout->remoteVersion >= 80400 && tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
14925         {
14926                 PQExpBuffer query = createPQExpBuffer();
14927                 PGresult   *res;
14928                 int                     i;
14929
14930                 if (fout->remoteVersion >= 90600)
14931                 {
14932                         PQExpBuffer acl_subquery = createPQExpBuffer();
14933                         PQExpBuffer racl_subquery = createPQExpBuffer();
14934                         PQExpBuffer initacl_subquery = createPQExpBuffer();
14935                         PQExpBuffer initracl_subquery = createPQExpBuffer();
14936
14937                         buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
14938                                                         initracl_subquery, "at.attacl", "c.relowner", "'c'",
14939                                                         dopt->binary_upgrade);
14940
14941                         appendPQExpBuffer(query,
14942                                                           "SELECT at.attname, "
14943                                                           "%s AS attacl, "
14944                                                           "%s AS rattacl, "
14945                                                           "%s AS initattacl, "
14946                                                           "%s AS initrattacl "
14947                                                           "FROM pg_catalog.pg_attribute at "
14948                                                           "JOIN pg_catalog.pg_class c ON (at.attrelid = c.oid) "
14949                                                           "LEFT JOIN pg_catalog.pg_init_privs pip ON "
14950                                                           "(at.attrelid = pip.objoid "
14951                                                           "AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass "
14952                                                           "AND at.attnum = pip.objsubid) "
14953                                                           "WHERE at.attrelid = '%u'::pg_catalog.oid AND "
14954                                                           "NOT at.attisdropped "
14955                                                           "AND ("
14956                                                           "%s IS NOT NULL OR "
14957                                                           "%s IS NOT NULL OR "
14958                                                           "%s IS NOT NULL OR "
14959                                                           "%s IS NOT NULL)"
14960                                                           "ORDER BY at.attnum",
14961                                                           acl_subquery->data,
14962                                                           racl_subquery->data,
14963                                                           initacl_subquery->data,
14964                                                           initracl_subquery->data,
14965                                                           tbinfo->dobj.catId.oid,
14966                                                           acl_subquery->data,
14967                                                           racl_subquery->data,
14968                                                           initacl_subquery->data,
14969                                                           initracl_subquery->data);
14970
14971                         destroyPQExpBuffer(acl_subquery);
14972                         destroyPQExpBuffer(racl_subquery);
14973                         destroyPQExpBuffer(initacl_subquery);
14974                         destroyPQExpBuffer(initracl_subquery);
14975                 }
14976                 else
14977                 {
14978                         appendPQExpBuffer(query,
14979                                                           "SELECT attname, attacl, NULL as rattacl, "
14980                                                           "NULL AS initattacl, NULL AS initrattacl "
14981                                                           "FROM pg_catalog.pg_attribute "
14982                                                           "WHERE attrelid = '%u'::pg_catalog.oid AND NOT attisdropped "
14983                                                           "AND attacl IS NOT NULL "
14984                                                           "ORDER BY attnum",
14985                                                           tbinfo->dobj.catId.oid);
14986                 }
14987
14988                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14989
14990                 for (i = 0; i < PQntuples(res); i++)
14991                 {
14992                         char       *attname = PQgetvalue(res, i, 0);
14993                         char       *attacl = PQgetvalue(res, i, 1);
14994                         char       *rattacl = PQgetvalue(res, i, 2);
14995                         char       *initattacl = PQgetvalue(res, i, 3);
14996                         char       *initrattacl = PQgetvalue(res, i, 4);
14997                         char       *attnamecopy;
14998                         char       *acltag;
14999
15000                         attnamecopy = pg_strdup(fmtId(attname));
15001                         acltag = psprintf("%s.%s", tbinfo->dobj.name, attname);
15002                         /* Column's GRANT type is always TABLE */
15003                         dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, "TABLE",
15004                                         namecopy, attnamecopy, acltag,
15005                                         tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
15006                                         attacl, rattacl, initattacl, initrattacl);
15007                         free(attnamecopy);
15008                         free(acltag);
15009                 }
15010                 PQclear(res);
15011                 destroyPQExpBuffer(query);
15012         }
15013
15014         free(namecopy);
15015
15016         return;
15017 }
15018
15019 /*
15020  * Create the AS clause for a view or materialized view. The semicolon is
15021  * stripped because a materialized view must add a WITH NO DATA clause.
15022  *
15023  * This returns a new buffer which must be freed by the caller.
15024  */
15025 static PQExpBuffer
15026 createViewAsClause(Archive *fout, TableInfo *tbinfo)
15027 {
15028         PQExpBuffer query = createPQExpBuffer();
15029         PQExpBuffer result = createPQExpBuffer();
15030         PGresult   *res;
15031         int                     len;
15032
15033         /* Fetch the view definition */
15034         appendPQExpBuffer(query,
15035                                           "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15036                                           tbinfo->dobj.catId.oid);
15037
15038         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15039
15040         if (PQntuples(res) != 1)
15041         {
15042                 if (PQntuples(res) < 1)
15043                         exit_horribly(NULL, "query to obtain definition of view \"%s\" returned no data\n",
15044                                                   tbinfo->dobj.name);
15045                 else
15046                         exit_horribly(NULL, "query to obtain definition of view \"%s\" returned more than one definition\n",
15047                                                   tbinfo->dobj.name);
15048         }
15049
15050         len = PQgetlength(res, 0, 0);
15051
15052         if (len == 0)
15053                 exit_horribly(NULL, "definition of view \"%s\" appears to be empty (length zero)\n",
15054                                           tbinfo->dobj.name);
15055
15056         /* Strip off the trailing semicolon so that other things may follow. */
15057         Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15058         appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15059
15060         PQclear(res);
15061         destroyPQExpBuffer(query);
15062
15063         return result;
15064 }
15065
15066 /*
15067  * Create a dummy AS clause for a view.  This is used when the real view
15068  * definition has to be postponed because of circular dependencies.
15069  * We must duplicate the view's external properties -- column names and types
15070  * (including collation) -- so that it works for subsequent references.
15071  *
15072  * This returns a new buffer which must be freed by the caller.
15073  */
15074 static PQExpBuffer
15075 createDummyViewAsClause(Archive *fout, TableInfo *tbinfo)
15076 {
15077         PQExpBuffer result = createPQExpBuffer();
15078         int                     j;
15079
15080         appendPQExpBufferStr(result, "SELECT");
15081
15082         for (j = 0; j < tbinfo->numatts; j++)
15083         {
15084                 if (j > 0)
15085                         appendPQExpBufferChar(result, ',');
15086                 appendPQExpBufferStr(result, "\n    ");
15087
15088                 appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15089
15090                 /*
15091                  * Must add collation if not default for the type, because CREATE OR
15092                  * REPLACE VIEW won't change it
15093                  */
15094                 if (OidIsValid(tbinfo->attcollation[j]))
15095                 {
15096                         CollInfo   *coll;
15097
15098                         coll = findCollationByOid(tbinfo->attcollation[j]);
15099                         if (coll)
15100                         {
15101                                 /* always schema-qualify, don't try to be smart */
15102                                 appendPQExpBuffer(result, " COLLATE %s.",
15103                                                                   fmtId(coll->dobj.namespace->dobj.name));
15104                                 appendPQExpBufferStr(result, fmtId(coll->dobj.name));
15105                         }
15106                 }
15107
15108                 appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15109         }
15110
15111         return result;
15112 }
15113
15114 /*
15115  * dumpTableSchema
15116  *        write the declaration (not data) of one user-defined table or view
15117  */
15118 static void
15119 dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15120 {
15121         DumpOptions *dopt = fout->dopt;
15122         PQExpBuffer q = createPQExpBuffer();
15123         PQExpBuffer delq = createPQExpBuffer();
15124         PQExpBuffer labelq = createPQExpBuffer();
15125         int                     numParents;
15126         TableInfo **parents;
15127         int                     actual_atts;    /* number of attrs in this CREATE statement */
15128         const char *reltypename;
15129         char       *storage;
15130         char       *srvname;
15131         char       *ftoptions;
15132         int                     j,
15133                                 k;
15134
15135         /* Make sure we are in proper schema */
15136         selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
15137
15138         if (dopt->binary_upgrade)
15139                 binary_upgrade_set_type_oids_by_rel_oid(fout, q,
15140                                                                                                 tbinfo->dobj.catId.oid);
15141
15142         /* Is it a table or a view? */
15143         if (tbinfo->relkind == RELKIND_VIEW)
15144         {
15145                 PQExpBuffer result;
15146
15147                 /*
15148                  * Note: keep this code in sync with the is_view case in dumpRule()
15149                  */
15150
15151                 reltypename = "VIEW";
15152
15153                 /*
15154                  * DROP must be fully qualified in case same name appears in
15155                  * pg_catalog
15156                  */
15157                 appendPQExpBuffer(delq, "DROP VIEW %s.",
15158                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
15159                 appendPQExpBuffer(delq, "%s;\n",
15160                                                   fmtId(tbinfo->dobj.name));
15161
15162                 if (dopt->binary_upgrade)
15163                         binary_upgrade_set_pg_class_oids(fout, q,
15164                                                                                          tbinfo->dobj.catId.oid, false);
15165
15166                 appendPQExpBuffer(q, "CREATE VIEW %s", fmtId(tbinfo->dobj.name));
15167                 if (tbinfo->dummy_view)
15168                         result = createDummyViewAsClause(fout, tbinfo);
15169                 else
15170                 {
15171                         if (nonemptyReloptions(tbinfo->reloptions))
15172                         {
15173                                 appendPQExpBufferStr(q, " WITH (");
15174                                 appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
15175                                 appendPQExpBufferChar(q, ')');
15176                         }
15177                         result = createViewAsClause(fout, tbinfo);
15178                 }
15179                 appendPQExpBuffer(q, " AS\n%s", result->data);
15180                 destroyPQExpBuffer(result);
15181
15182                 if (tbinfo->checkoption != NULL && !tbinfo->dummy_view)
15183                         appendPQExpBuffer(q, "\n  WITH %s CHECK OPTION", tbinfo->checkoption);
15184                 appendPQExpBufferStr(q, ";\n");
15185
15186                 appendPQExpBuffer(labelq, "VIEW %s",
15187                                                   fmtId(tbinfo->dobj.name));
15188         }
15189         else
15190         {
15191                 switch (tbinfo->relkind)
15192                 {
15193                         case RELKIND_FOREIGN_TABLE:
15194                                 {
15195                                         PQExpBuffer query = createPQExpBuffer();
15196                                         PGresult   *res;
15197                                         int                     i_srvname;
15198                                         int                     i_ftoptions;
15199
15200                                         reltypename = "FOREIGN TABLE";
15201
15202                                         /* retrieve name of foreign server and generic options */
15203                                         appendPQExpBuffer(query,
15204                                                                           "SELECT fs.srvname, "
15205                                                                           "pg_catalog.array_to_string(ARRAY("
15206                                                                           "SELECT pg_catalog.quote_ident(option_name) || "
15207                                                                           "' ' || pg_catalog.quote_literal(option_value) "
15208                                                                           "FROM pg_catalog.pg_options_to_table(ftoptions) "
15209                                                                           "ORDER BY option_name"
15210                                                                           "), E',\n    ') AS ftoptions "
15211                                                                           "FROM pg_catalog.pg_foreign_table ft "
15212                                                                           "JOIN pg_catalog.pg_foreign_server fs "
15213                                                                           "ON (fs.oid = ft.ftserver) "
15214                                                                           "WHERE ft.ftrelid = '%u'",
15215                                                                           tbinfo->dobj.catId.oid);
15216                                         res = ExecuteSqlQueryForSingleRow(fout, query->data);
15217                                         i_srvname = PQfnumber(res, "srvname");
15218                                         i_ftoptions = PQfnumber(res, "ftoptions");
15219                                         srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
15220                                         ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
15221                                         PQclear(res);
15222                                         destroyPQExpBuffer(query);
15223                                         break;
15224                                 }
15225                         case RELKIND_MATVIEW:
15226                                 reltypename = "MATERIALIZED VIEW";
15227                                 srvname = NULL;
15228                                 ftoptions = NULL;
15229                                 break;
15230                         default:
15231                                 reltypename = "TABLE";
15232                                 srvname = NULL;
15233                                 ftoptions = NULL;
15234                 }
15235
15236                 numParents = tbinfo->numParents;
15237                 parents = tbinfo->parents;
15238
15239                 /*
15240                  * DROP must be fully qualified in case same name appears in
15241                  * pg_catalog
15242                  */
15243                 appendPQExpBuffer(delq, "DROP %s %s.", reltypename,
15244                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
15245                 appendPQExpBuffer(delq, "%s;\n",
15246                                                   fmtId(tbinfo->dobj.name));
15247
15248                 appendPQExpBuffer(labelq, "%s %s", reltypename,
15249                                                   fmtId(tbinfo->dobj.name));
15250
15251                 if (dopt->binary_upgrade)
15252                         binary_upgrade_set_pg_class_oids(fout, q,
15253                                                                                          tbinfo->dobj.catId.oid, false);
15254
15255                 appendPQExpBuffer(q, "CREATE %s%s %s",
15256                                                   tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
15257                                                   "UNLOGGED " : "",
15258                                                   reltypename,
15259                                                   fmtId(tbinfo->dobj.name));
15260
15261                 /*
15262                  * Attach to type, if reloftype; except in case of a binary upgrade,
15263                  * we dump the table normally and attach it to the type afterward.
15264                  */
15265                 if (tbinfo->reloftype && !dopt->binary_upgrade)
15266                         appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
15267
15268                 /*
15269                  * If the table is a partition, dump it as such; except in the case of
15270                  * a binary upgrade, we dump the table normally and attach it to the
15271                  * parent afterward.
15272                  */
15273                 if (tbinfo->ispartition && !dopt->binary_upgrade)
15274                 {
15275                         TableInfo  *parentRel = tbinfo->parents[0];
15276
15277                         /*
15278                          * With partitions, unlike inheritance, there can only be one
15279                          * parent.
15280                          */
15281                         if (tbinfo->numParents != 1)
15282                                 exit_horribly(NULL, "invalid number of parents %d for table \"%s\"\n",
15283                                                           tbinfo->numParents, tbinfo->dobj.name);
15284
15285                         appendPQExpBuffer(q, " PARTITION OF ");
15286                         if (parentRel->dobj.namespace != tbinfo->dobj.namespace)
15287                                 appendPQExpBuffer(q, "%s.",
15288                                                                   fmtId(parentRel->dobj.namespace->dobj.name));
15289                         appendPQExpBufferStr(q, fmtId(parentRel->dobj.name));
15290                 }
15291
15292                 if (tbinfo->relkind != RELKIND_MATVIEW)
15293                 {
15294                         /* Dump the attributes */
15295                         actual_atts = 0;
15296                         for (j = 0; j < tbinfo->numatts; j++)
15297                         {
15298                                 /*
15299                                  * Normally, dump if it's locally defined in this table, and
15300                                  * not dropped.  But for binary upgrade, we'll dump all the
15301                                  * columns, and then fix up the dropped and nonlocal cases
15302                                  * below.
15303                                  */
15304                                 if (shouldPrintColumn(dopt, tbinfo, j))
15305                                 {
15306                                         /*
15307                                          * Default value --- suppress if to be printed separately.
15308                                          */
15309                                         bool            has_default = (tbinfo->attrdefs[j] != NULL &&
15310                                                                                            !tbinfo->attrdefs[j]->separate);
15311
15312                                         /*
15313                                          * Not Null constraint --- suppress if inherited, except
15314                                          * in binary-upgrade case where that won't work.
15315                                          */
15316                                         bool            has_notnull = (tbinfo->notnull[j] &&
15317                                                                                            (!tbinfo->inhNotNull[j] ||
15318                                                                                                 dopt->binary_upgrade));
15319
15320                                         /*
15321                                          * Skip column if fully defined by reloftype or the
15322                                          * partition parent.
15323                                          */
15324                                         if ((tbinfo->reloftype || tbinfo->ispartition) &&
15325                                                 !has_default && !has_notnull && !dopt->binary_upgrade)
15326                                                 continue;
15327
15328                                         /* Format properly if not first attr */
15329                                         if (actual_atts == 0)
15330                                                 appendPQExpBufferStr(q, " (");
15331                                         else
15332                                                 appendPQExpBufferChar(q, ',');
15333                                         appendPQExpBufferStr(q, "\n    ");
15334                                         actual_atts++;
15335
15336                                         /* Attribute name */
15337                                         appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j]));
15338
15339                                         if (tbinfo->attisdropped[j])
15340                                         {
15341                                                 /*
15342                                                  * ALTER TABLE DROP COLUMN clears
15343                                                  * pg_attribute.atttypid, so we will not have gotten a
15344                                                  * valid type name; insert INTEGER as a stopgap. We'll
15345                                                  * clean things up later.
15346                                                  */
15347                                                 appendPQExpBufferStr(q, " INTEGER /* dummy */");
15348                                                 /* Skip all the rest, too */
15349                                                 continue;
15350                                         }
15351
15352                                         /*
15353                                          * Attribute type
15354                                          *
15355                                          * In binary-upgrade mode, we always include the type. If
15356                                          * we aren't in binary-upgrade mode, then we skip the type
15357                                          * when creating a typed table ('OF type_name') or a
15358                                          * partition ('PARTITION OF'), since the type comes from
15359                                          * the parent/partitioned table.
15360                                          */
15361                                         if (dopt->binary_upgrade || (!tbinfo->reloftype && !tbinfo->ispartition))
15362                                         {
15363                                                 appendPQExpBuffer(q, " %s",
15364                                                                                   tbinfo->atttypnames[j]);
15365                                         }
15366
15367                                         /* Add collation if not default for the type */
15368                                         if (OidIsValid(tbinfo->attcollation[j]))
15369                                         {
15370                                                 CollInfo   *coll;
15371
15372                                                 coll = findCollationByOid(tbinfo->attcollation[j]);
15373                                                 if (coll)
15374                                                 {
15375                                                         /* always schema-qualify, don't try to be smart */
15376                                                         appendPQExpBuffer(q, " COLLATE %s.",
15377                                                                                           fmtId(coll->dobj.namespace->dobj.name));
15378                                                         appendPQExpBufferStr(q, fmtId(coll->dobj.name));
15379                                                 }
15380                                         }
15381
15382                                         if (has_default)
15383                                                 appendPQExpBuffer(q, " DEFAULT %s",
15384                                                                                   tbinfo->attrdefs[j]->adef_expr);
15385
15386                                         if (has_notnull)
15387                                                 appendPQExpBufferStr(q, " NOT NULL");
15388                                 }
15389                         }
15390
15391                         /*
15392                          * Add non-inherited CHECK constraints, if any.
15393                          */
15394                         for (j = 0; j < tbinfo->ncheck; j++)
15395                         {
15396                                 ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
15397
15398                                 if (constr->separate || !constr->conislocal)
15399                                         continue;
15400
15401                                 if (actual_atts == 0)
15402                                         appendPQExpBufferStr(q, " (\n    ");
15403                                 else
15404                                         appendPQExpBufferStr(q, ",\n    ");
15405
15406                                 appendPQExpBuffer(q, "CONSTRAINT %s ",
15407                                                                   fmtId(constr->dobj.name));
15408                                 appendPQExpBufferStr(q, constr->condef);
15409
15410                                 actual_atts++;
15411                         }
15412
15413                         if (actual_atts)
15414                                 appendPQExpBufferStr(q, "\n)");
15415                         else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15416                                            !dopt->binary_upgrade))
15417                         {
15418                                 /*
15419                                  * We must have a parenthesized attribute list, even though
15420                                  * empty, when not using the OF TYPE or PARTITION OF syntax.
15421                                  */
15422                                 appendPQExpBufferStr(q, " (\n)");
15423                         }
15424
15425                         if (tbinfo->ispartition && !dopt->binary_upgrade)
15426                         {
15427                                 appendPQExpBufferStr(q, "\n");
15428                                 appendPQExpBufferStr(q, tbinfo->partbound);
15429                         }
15430
15431                         /* Emit the INHERITS clause, except if this is a partition. */
15432                         if (numParents > 0 &&
15433                                 !tbinfo->ispartition &&
15434                                 !dopt->binary_upgrade)
15435                         {
15436                                 appendPQExpBufferStr(q, "\nINHERITS (");
15437                                 for (k = 0; k < numParents; k++)
15438                                 {
15439                                         TableInfo  *parentRel = parents[k];
15440
15441                                         if (k > 0)
15442                                                 appendPQExpBufferStr(q, ", ");
15443                                         if (parentRel->dobj.namespace != tbinfo->dobj.namespace)
15444                                                 appendPQExpBuffer(q, "%s.",
15445                                                                                   fmtId(parentRel->dobj.namespace->dobj.name));
15446                                         appendPQExpBufferStr(q, fmtId(parentRel->dobj.name));
15447                                 }
15448                                 appendPQExpBufferChar(q, ')');
15449                         }
15450
15451                         if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
15452                                 appendPQExpBuffer(q, "\nPARTITION BY %s", tbinfo->partkeydef);
15453
15454                         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
15455                                 appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
15456                 }
15457
15458                 if (nonemptyReloptions(tbinfo->reloptions) ||
15459                         nonemptyReloptions(tbinfo->toast_reloptions))
15460                 {
15461                         bool            addcomma = false;
15462
15463                         appendPQExpBufferStr(q, "\nWITH (");
15464                         if (nonemptyReloptions(tbinfo->reloptions))
15465                         {
15466                                 addcomma = true;
15467                                 appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
15468                         }
15469                         if (nonemptyReloptions(tbinfo->toast_reloptions))
15470                         {
15471                                 if (addcomma)
15472                                         appendPQExpBufferStr(q, ", ");
15473                                 appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.",
15474                                                                                 fout);
15475                         }
15476                         appendPQExpBufferChar(q, ')');
15477                 }
15478
15479                 /* Dump generic options if any */
15480                 if (ftoptions && ftoptions[0])
15481                         appendPQExpBuffer(q, "\nOPTIONS (\n    %s\n)", ftoptions);
15482
15483                 /*
15484                  * For materialized views, create the AS clause just like a view. At
15485                  * this point, we always mark the view as not populated.
15486                  */
15487                 if (tbinfo->relkind == RELKIND_MATVIEW)
15488                 {
15489                         PQExpBuffer result;
15490
15491                         result = createViewAsClause(fout, tbinfo);
15492                         appendPQExpBuffer(q, " AS\n%s\n  WITH NO DATA;\n",
15493                                                           result->data);
15494                         destroyPQExpBuffer(result);
15495                 }
15496                 else
15497                         appendPQExpBufferStr(q, ";\n");
15498
15499                 /*
15500                  * To create binary-compatible heap files, we have to ensure the same
15501                  * physical column order, including dropped columns, as in the
15502                  * original.  Therefore, we create dropped columns above and drop them
15503                  * here, also updating their attlen/attalign values so that the
15504                  * dropped column can be skipped properly.  (We do not bother with
15505                  * restoring the original attbyval setting.)  Also, inheritance
15506                  * relationships are set up by doing ALTER TABLE INHERIT rather than
15507                  * using an INHERITS clause --- the latter would possibly mess up the
15508                  * column order.  That also means we have to take care about setting
15509                  * attislocal correctly, plus fix up any inherited CHECK constraints.
15510                  * Analogously, we set up typed tables using ALTER TABLE / OF here.
15511                  */
15512                 if (dopt->binary_upgrade &&
15513                         (tbinfo->relkind == RELKIND_RELATION ||
15514                          tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
15515                          tbinfo->relkind == RELKIND_PARTITIONED_TABLE))
15516                 {
15517                         for (j = 0; j < tbinfo->numatts; j++)
15518                         {
15519                                 if (tbinfo->attisdropped[j])
15520                                 {
15521                                         appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped column.\n");
15522                                         appendPQExpBuffer(q, "UPDATE pg_catalog.pg_attribute\n"
15523                                                                           "SET attlen = %d, "
15524                                                                           "attalign = '%c', attbyval = false\n"
15525                                                                           "WHERE attname = ",
15526                                                                           tbinfo->attlen[j],
15527                                                                           tbinfo->attalign[j]);
15528                                         appendStringLiteralAH(q, tbinfo->attnames[j], fout);
15529                                         appendPQExpBufferStr(q, "\n  AND attrelid = ");
15530                                         appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
15531                                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15532
15533                                         if (tbinfo->relkind == RELKIND_RELATION ||
15534                                                 tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
15535                                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15536                                                                                   fmtId(tbinfo->dobj.name));
15537                                         else
15538                                                 appendPQExpBuffer(q, "ALTER FOREIGN TABLE ONLY %s ",
15539                                                                                   fmtId(tbinfo->dobj.name));
15540                                         appendPQExpBuffer(q, "DROP COLUMN %s;\n",
15541                                                                           fmtId(tbinfo->attnames[j]));
15542                                 }
15543                                 else if (!tbinfo->attislocal[j])
15544                                 {
15545                                         appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited column.\n");
15546                                         appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n"
15547                                                                                  "SET attislocal = false\n"
15548                                                                                  "WHERE attname = ");
15549                                         appendStringLiteralAH(q, tbinfo->attnames[j], fout);
15550                                         appendPQExpBufferStr(q, "\n  AND attrelid = ");
15551                                         appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
15552                                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15553                                 }
15554                         }
15555
15556                         for (k = 0; k < tbinfo->ncheck; k++)
15557                         {
15558                                 ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
15559
15560                                 if (constr->separate || constr->conislocal)
15561                                         continue;
15562
15563                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraint.\n");
15564                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15565                                                                   fmtId(tbinfo->dobj.name));
15566                                 appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
15567                                                                   fmtId(constr->dobj.name));
15568                                 appendPQExpBuffer(q, "%s;\n", constr->condef);
15569                                 appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
15570                                                                          "SET conislocal = false\n"
15571                                                                          "WHERE contype = 'c' AND conname = ");
15572                                 appendStringLiteralAH(q, constr->dobj.name, fout);
15573                                 appendPQExpBufferStr(q, "\n  AND conrelid = ");
15574                                 appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
15575                                 appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15576                         }
15577
15578                         if (numParents > 0)
15579                         {
15580                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance and partitioning this way.\n");
15581                                 for (k = 0; k < numParents; k++)
15582                                 {
15583                                         TableInfo  *parentRel = parents[k];
15584                                         PQExpBuffer parentname = createPQExpBuffer();
15585
15586                                         /* Schema-qualify the parent table, if necessary */
15587                                         if (parentRel->dobj.namespace != tbinfo->dobj.namespace)
15588                                                 appendPQExpBuffer(parentname, "%s.",
15589                                                                                   fmtId(parentRel->dobj.namespace->dobj.name));
15590
15591                                         appendPQExpBuffer(parentname, "%s",
15592                                                                           fmtId(parentRel->dobj.name));
15593
15594                                         /* In the partitioning case, we alter the parent */
15595                                         if (tbinfo->ispartition)
15596                                                 appendPQExpBuffer(q,
15597                                                                                   "ALTER TABLE ONLY %s ATTACH PARTITION ",
15598                                                                                   parentname->data);
15599                                         else
15600                                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
15601                                                                                   fmtId(tbinfo->dobj.name));
15602
15603                                         /* Partition needs specifying the bounds */
15604                                         if (tbinfo->ispartition)
15605                                                 appendPQExpBuffer(q, "%s %s;\n",
15606                                                                                   fmtId(tbinfo->dobj.name),
15607                                                                                   tbinfo->partbound);
15608                                         else
15609                                                 appendPQExpBuffer(q, "%s;\n", parentname->data);
15610
15611                                         destroyPQExpBuffer(parentname);
15612                                 }
15613                         }
15614
15615                         if (tbinfo->reloftype)
15616                         {
15617                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n");
15618                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
15619                                                                   fmtId(tbinfo->dobj.name),
15620                                                                   tbinfo->reloftype);
15621                         }
15622
15623                         appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
15624                         appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
15625                                                           "SET relfrozenxid = '%u', relminmxid = '%u'\n"
15626                                                           "WHERE oid = ",
15627                                                           tbinfo->frozenxid, tbinfo->minmxid);
15628                         appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
15629                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15630
15631                         if (tbinfo->toast_oid)
15632                         {
15633                                 /* We preserve the toast oids, so we can use it during restore */
15634                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
15635                                 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
15636                                                                   "SET relfrozenxid = '%u', relminmxid = '%u'\n"
15637                                                                   "WHERE oid = '%u';\n",
15638                                                                   tbinfo->toast_frozenxid,
15639                                                                   tbinfo->toast_minmxid, tbinfo->toast_oid);
15640                         }
15641                 }
15642
15643                 /*
15644                  * In binary_upgrade mode, restore matviews' populated status by
15645                  * poking pg_class directly.  This is pretty ugly, but we can't use
15646                  * REFRESH MATERIALIZED VIEW since it's possible that some underlying
15647                  * matview is not populated even though this matview is.
15648                  */
15649                 if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
15650                         tbinfo->relispopulated)
15651                 {
15652                         appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n");
15653                         appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n"
15654                                                                  "SET relispopulated = 't'\n"
15655                                                                  "WHERE oid = ");
15656                         appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
15657                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15658                 }
15659
15660                 /*
15661                  * Dump additional per-column properties that we can't handle in the
15662                  * main CREATE TABLE command.
15663                  */
15664                 for (j = 0; j < tbinfo->numatts; j++)
15665                 {
15666                         /* None of this applies to dropped columns */
15667                         if (tbinfo->attisdropped[j])
15668                                 continue;
15669
15670                         /*
15671                          * If we didn't dump the column definition explicitly above, and
15672                          * it is NOT NULL and did not inherit that property from a parent,
15673                          * we have to mark it separately.
15674                          */
15675                         if (!shouldPrintColumn(dopt, tbinfo, j) &&
15676                                 tbinfo->notnull[j] && !tbinfo->inhNotNull[j])
15677                         {
15678                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15679                                                                   fmtId(tbinfo->dobj.name));
15680                                 appendPQExpBuffer(q, "ALTER COLUMN %s SET NOT NULL;\n",
15681                                                                   fmtId(tbinfo->attnames[j]));
15682                         }
15683
15684                         /*
15685                          * Dump per-column statistics information. We only issue an ALTER
15686                          * TABLE statement if the attstattarget entry for this column is
15687                          * non-negative (i.e. it's not the default value)
15688                          */
15689                         if (tbinfo->attstattarget[j] >= 0)
15690                         {
15691                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15692                                                                   fmtId(tbinfo->dobj.name));
15693                                 appendPQExpBuffer(q, "ALTER COLUMN %s ",
15694                                                                   fmtId(tbinfo->attnames[j]));
15695                                 appendPQExpBuffer(q, "SET STATISTICS %d;\n",
15696                                                                   tbinfo->attstattarget[j]);
15697                         }
15698
15699                         /*
15700                          * Dump per-column storage information.  The statement is only
15701                          * dumped if the storage has been changed from the type's default.
15702                          */
15703                         if (tbinfo->attstorage[j] != tbinfo->typstorage[j])
15704                         {
15705                                 switch (tbinfo->attstorage[j])
15706                                 {
15707                                         case 'p':
15708                                                 storage = "PLAIN";
15709                                                 break;
15710                                         case 'e':
15711                                                 storage = "EXTERNAL";
15712                                                 break;
15713                                         case 'm':
15714                                                 storage = "MAIN";
15715                                                 break;
15716                                         case 'x':
15717                                                 storage = "EXTENDED";
15718                                                 break;
15719                                         default:
15720                                                 storage = NULL;
15721                                 }
15722
15723                                 /*
15724                                  * Only dump the statement if it's a storage type we recognize
15725                                  */
15726                                 if (storage != NULL)
15727                                 {
15728                                         appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15729                                                                           fmtId(tbinfo->dobj.name));
15730                                         appendPQExpBuffer(q, "ALTER COLUMN %s ",
15731                                                                           fmtId(tbinfo->attnames[j]));
15732                                         appendPQExpBuffer(q, "SET STORAGE %s;\n",
15733                                                                           storage);
15734                                 }
15735                         }
15736
15737                         /*
15738                          * Dump per-column attributes.
15739                          */
15740                         if (tbinfo->attoptions[j] && tbinfo->attoptions[j][0] != '\0')
15741                         {
15742                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15743                                                                   fmtId(tbinfo->dobj.name));
15744                                 appendPQExpBuffer(q, "ALTER COLUMN %s ",
15745                                                                   fmtId(tbinfo->attnames[j]));
15746                                 appendPQExpBuffer(q, "SET (%s);\n",
15747                                                                   tbinfo->attoptions[j]);
15748                         }
15749
15750                         /*
15751                          * Dump per-column fdw options.
15752                          */
15753                         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
15754                                 tbinfo->attfdwoptions[j] &&
15755                                 tbinfo->attfdwoptions[j][0] != '\0')
15756                         {
15757                                 appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
15758                                                                   fmtId(tbinfo->dobj.name));
15759                                 appendPQExpBuffer(q, "ALTER COLUMN %s ",
15760                                                                   fmtId(tbinfo->attnames[j]));
15761                                 appendPQExpBuffer(q, "OPTIONS (\n    %s\n);\n",
15762                                                                   tbinfo->attfdwoptions[j]);
15763                         }
15764                 }
15765         }
15766
15767         /*
15768          * dump properties we only have ALTER TABLE syntax for
15769          */
15770         if ((tbinfo->relkind == RELKIND_RELATION ||
15771                  tbinfo->relkind == RELKIND_PARTITIONED_TABLE ||
15772                  tbinfo->relkind == RELKIND_MATVIEW) &&
15773                 tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT)
15774         {
15775                 if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX)
15776                 {
15777                         /* nothing to do, will be set when the index is dumped */
15778                 }
15779                 else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING)
15780                 {
15781                         appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n",
15782                                                           fmtId(tbinfo->dobj.name));
15783                 }
15784                 else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL)
15785                 {
15786                         appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n",
15787                                                           fmtId(tbinfo->dobj.name));
15788                 }
15789         }
15790
15791         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE && tbinfo->hasoids)
15792                 appendPQExpBuffer(q, "\nALTER TABLE ONLY %s SET WITH OIDS;\n",
15793                                                   fmtId(tbinfo->dobj.name));
15794
15795         if (tbinfo->forcerowsec)
15796                 appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n",
15797                                                   fmtId(tbinfo->dobj.name));
15798
15799         if (dopt->binary_upgrade)
15800                 binary_upgrade_extension_member(q, &tbinfo->dobj, labelq->data);
15801
15802         if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15803                 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
15804                                          tbinfo->dobj.name,
15805                                          tbinfo->dobj.namespace->dobj.name,
15806                                          (tbinfo->relkind == RELKIND_VIEW) ? NULL : tbinfo->reltablespace,
15807                                          tbinfo->rolname,
15808                                          (strcmp(reltypename, "TABLE") == 0) ? tbinfo->hasoids : false,
15809                                          reltypename,
15810                                          tbinfo->postponed_def ?
15811                                          SECTION_POST_DATA : SECTION_PRE_DATA,
15812                                          q->data, delq->data, NULL,
15813                                          NULL, 0,
15814                                          NULL, NULL);
15815
15816
15817         /* Dump Table Comments */
15818         if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15819                 dumpTableComment(fout, tbinfo, reltypename);
15820
15821         /* Dump Table Security Labels */
15822         if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
15823                 dumpTableSecLabel(fout, tbinfo, reltypename);
15824
15825         /* Dump comments on inlined table constraints */
15826         for (j = 0; j < tbinfo->ncheck; j++)
15827         {
15828                 ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
15829
15830                 if (constr->separate || !constr->conislocal)
15831                         continue;
15832
15833                 if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15834                         dumpTableConstraintComment(fout, constr);
15835         }
15836
15837         destroyPQExpBuffer(q);
15838         destroyPQExpBuffer(delq);
15839         destroyPQExpBuffer(labelq);
15840 }
15841
15842 /*
15843  * dumpAttrDef --- dump an attribute's default-value declaration
15844  */
15845 static void
15846 dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
15847 {
15848         DumpOptions *dopt = fout->dopt;
15849         TableInfo  *tbinfo = adinfo->adtable;
15850         int                     adnum = adinfo->adnum;
15851         PQExpBuffer q;
15852         PQExpBuffer delq;
15853         char       *tag;
15854
15855         /* Skip if table definition not to be dumped */
15856         if (!tbinfo->dobj.dump || dopt->dataOnly)
15857                 return;
15858
15859         /* Skip if not "separate"; it was dumped in the table's definition */
15860         if (!adinfo->separate)
15861                 return;
15862
15863         q = createPQExpBuffer();
15864         delq = createPQExpBuffer();
15865
15866         appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15867                                           fmtId(tbinfo->dobj.name));
15868         appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
15869                                           fmtId(tbinfo->attnames[adnum - 1]),
15870                                           adinfo->adef_expr);
15871
15872         /*
15873          * DROP must be fully qualified in case same name appears in pg_catalog
15874          */
15875         appendPQExpBuffer(delq, "ALTER TABLE %s.",
15876                                           fmtId(tbinfo->dobj.namespace->dobj.name));
15877         appendPQExpBuffer(delq, "%s ",
15878                                           fmtId(tbinfo->dobj.name));
15879         appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
15880                                           fmtId(tbinfo->attnames[adnum - 1]));
15881
15882         tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
15883
15884         if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15885                 ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
15886                                          tag,
15887                                          tbinfo->dobj.namespace->dobj.name,
15888                                          NULL,
15889                                          tbinfo->rolname,
15890                                          false, "DEFAULT", SECTION_PRE_DATA,
15891                                          q->data, delq->data, NULL,
15892                                          NULL, 0,
15893                                          NULL, NULL);
15894
15895         free(tag);
15896         destroyPQExpBuffer(q);
15897         destroyPQExpBuffer(delq);
15898 }
15899
15900 /*
15901  * getAttrName: extract the correct name for an attribute
15902  *
15903  * The array tblInfo->attnames[] only provides names of user attributes;
15904  * if a system attribute number is supplied, we have to fake it.
15905  * We also do a little bit of bounds checking for safety's sake.
15906  */
15907 static const char *
15908 getAttrName(int attrnum, TableInfo *tblInfo)
15909 {
15910         if (attrnum > 0 && attrnum <= tblInfo->numatts)
15911                 return tblInfo->attnames[attrnum - 1];
15912         switch (attrnum)
15913         {
15914                 case SelfItemPointerAttributeNumber:
15915                         return "ctid";
15916                 case ObjectIdAttributeNumber:
15917                         return "oid";
15918                 case MinTransactionIdAttributeNumber:
15919                         return "xmin";
15920                 case MinCommandIdAttributeNumber:
15921                         return "cmin";
15922                 case MaxTransactionIdAttributeNumber:
15923                         return "xmax";
15924                 case MaxCommandIdAttributeNumber:
15925                         return "cmax";
15926                 case TableOidAttributeNumber:
15927                         return "tableoid";
15928         }
15929         exit_horribly(NULL, "invalid column number %d for table \"%s\"\n",
15930                                   attrnum, tblInfo->dobj.name);
15931         return NULL;                            /* keep compiler quiet */
15932 }
15933
15934 /*
15935  * dumpIndex
15936  *        write out to fout a user-defined index
15937  */
15938 static void
15939 dumpIndex(Archive *fout, IndxInfo *indxinfo)
15940 {
15941         DumpOptions *dopt = fout->dopt;
15942         TableInfo  *tbinfo = indxinfo->indextable;
15943         bool            is_constraint = (indxinfo->indexconstraint != 0);
15944         PQExpBuffer q;
15945         PQExpBuffer delq;
15946         PQExpBuffer labelq;
15947
15948         if (dopt->dataOnly)
15949                 return;
15950
15951         q = createPQExpBuffer();
15952         delq = createPQExpBuffer();
15953         labelq = createPQExpBuffer();
15954
15955         appendPQExpBuffer(labelq, "INDEX %s",
15956                                           fmtId(indxinfo->dobj.name));
15957
15958         /*
15959          * If there's an associated constraint, don't dump the index per se, but
15960          * do dump any comment for it.  (This is safe because dependency ordering
15961          * will have ensured the constraint is emitted first.)  Note that the
15962          * emitted comment has to be shown as depending on the constraint, not the
15963          * index, in such cases.
15964          */
15965         if (!is_constraint)
15966         {
15967                 if (dopt->binary_upgrade)
15968                         binary_upgrade_set_pg_class_oids(fout, q,
15969                                                                                          indxinfo->dobj.catId.oid, true);
15970
15971                 /* Plain secondary index */
15972                 appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
15973
15974                 /* If the index is clustered, we need to record that. */
15975                 if (indxinfo->indisclustered)
15976                 {
15977                         appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
15978                                                           fmtId(tbinfo->dobj.name));
15979                         appendPQExpBuffer(q, " ON %s;\n",
15980                                                           fmtId(indxinfo->dobj.name));
15981                 }
15982
15983                 /* If the index defines identity, we need to record that. */
15984                 if (indxinfo->indisreplident)
15985                 {
15986                         appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
15987                                                           fmtId(tbinfo->dobj.name));
15988                         appendPQExpBuffer(q, " INDEX %s;\n",
15989                                                           fmtId(indxinfo->dobj.name));
15990                 }
15991
15992                 /*
15993                  * DROP must be fully qualified in case same name appears in
15994                  * pg_catalog
15995                  */
15996                 appendPQExpBuffer(delq, "DROP INDEX %s.",
15997                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
15998                 appendPQExpBuffer(delq, "%s;\n",
15999                                                   fmtId(indxinfo->dobj.name));
16000
16001                 if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16002                         ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
16003                                                  indxinfo->dobj.name,
16004                                                  tbinfo->dobj.namespace->dobj.name,
16005                                                  indxinfo->tablespace,
16006                                                  tbinfo->rolname, false,
16007                                                  "INDEX", SECTION_POST_DATA,
16008                                                  q->data, delq->data, NULL,
16009                                                  NULL, 0,
16010                                                  NULL, NULL);
16011         }
16012
16013         /* Dump Index Comments */
16014         if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16015                 dumpComment(fout, labelq->data,
16016                                         tbinfo->dobj.namespace->dobj.name,
16017                                         tbinfo->rolname,
16018                                         indxinfo->dobj.catId, 0,
16019                                         is_constraint ? indxinfo->indexconstraint :
16020                                         indxinfo->dobj.dumpId);
16021
16022         destroyPQExpBuffer(q);
16023         destroyPQExpBuffer(delq);
16024         destroyPQExpBuffer(labelq);
16025 }
16026
16027 /*
16028  * dumpStatisticsExt
16029  *        write out to fout an extended statistics object
16030  */
16031 static void
16032 dumpStatisticsExt(Archive *fout, StatsExtInfo *statsextinfo)
16033 {
16034         DumpOptions *dopt = fout->dopt;
16035         TableInfo  *tbinfo = statsextinfo->statsexttable;
16036         PQExpBuffer q;
16037         PQExpBuffer delq;
16038         PQExpBuffer labelq;
16039
16040         if (dopt->dataOnly)
16041                 return;
16042
16043         q = createPQExpBuffer();
16044         delq = createPQExpBuffer();
16045         labelq = createPQExpBuffer();
16046
16047         appendPQExpBuffer(labelq, "STATISTICS %s",
16048                                           fmtId(statsextinfo->dobj.name));
16049
16050         appendPQExpBuffer(q, "%s;\n", statsextinfo->statsextdef);
16051
16052         appendPQExpBuffer(delq, "DROP STATISTICS %s.",
16053                                           fmtId(tbinfo->dobj.namespace->dobj.name));
16054         appendPQExpBuffer(delq, "%s;\n",
16055                                           fmtId(statsextinfo->dobj.name));
16056
16057         if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16058                 ArchiveEntry(fout, statsextinfo->dobj.catId,
16059                                          statsextinfo->dobj.dumpId,
16060                                          statsextinfo->dobj.name,
16061                                          tbinfo->dobj.namespace->dobj.name,
16062                                          NULL,
16063                                          tbinfo->rolname, false,
16064                                          "STATISTICS", SECTION_POST_DATA,
16065                                          q->data, delq->data, NULL,
16066                                          NULL, 0,
16067                                          NULL, NULL);
16068
16069         /* Dump Statistics Comments */
16070         if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16071                 dumpComment(fout, labelq->data,
16072                                         tbinfo->dobj.namespace->dobj.name,
16073                                         tbinfo->rolname,
16074                                         statsextinfo->dobj.catId, 0,
16075                                         statsextinfo->dobj.dumpId);
16076
16077         destroyPQExpBuffer(q);
16078         destroyPQExpBuffer(delq);
16079         destroyPQExpBuffer(labelq);
16080 }
16081
16082 /*
16083  * dumpConstraint
16084  *        write out to fout a user-defined constraint
16085  */
16086 static void
16087 dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
16088 {
16089         DumpOptions *dopt = fout->dopt;
16090         TableInfo  *tbinfo = coninfo->contable;
16091         PQExpBuffer q;
16092         PQExpBuffer delq;
16093         char       *tag = NULL;
16094
16095         /* Skip if not to be dumped */
16096         if (!coninfo->dobj.dump || dopt->dataOnly)
16097                 return;
16098
16099         q = createPQExpBuffer();
16100         delq = createPQExpBuffer();
16101
16102         if (coninfo->contype == 'p' ||
16103                 coninfo->contype == 'u' ||
16104                 coninfo->contype == 'x')
16105         {
16106                 /* Index-related constraint */
16107                 IndxInfo   *indxinfo;
16108                 int                     k;
16109
16110                 indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16111
16112                 if (indxinfo == NULL)
16113                         exit_horribly(NULL, "missing index for constraint \"%s\"\n",
16114                                                   coninfo->dobj.name);
16115
16116                 if (dopt->binary_upgrade)
16117                         binary_upgrade_set_pg_class_oids(fout, q,
16118                                                                                          indxinfo->dobj.catId.oid, true);
16119
16120                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
16121                                                   fmtId(tbinfo->dobj.name));
16122                 appendPQExpBuffer(q, "    ADD CONSTRAINT %s ",
16123                                                   fmtId(coninfo->dobj.name));
16124
16125                 if (coninfo->condef)
16126                 {
16127                         /* pg_get_constraintdef should have provided everything */
16128                         appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16129                 }
16130                 else
16131                 {
16132                         appendPQExpBuffer(q, "%s (",
16133                                                           coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16134                         for (k = 0; k < indxinfo->indnkeys; k++)
16135                         {
16136                                 int                     indkey = (int) indxinfo->indkeys[k];
16137                                 const char *attname;
16138
16139                                 if (indkey == InvalidAttrNumber)
16140                                         break;
16141                                 attname = getAttrName(indkey, tbinfo);
16142
16143                                 appendPQExpBuffer(q, "%s%s",
16144                                                                   (k == 0) ? "" : ", ",
16145                                                                   fmtId(attname));
16146                         }
16147
16148                         appendPQExpBufferChar(q, ')');
16149
16150                         if (nonemptyReloptions(indxinfo->indreloptions))
16151                         {
16152                                 appendPQExpBufferStr(q, " WITH (");
16153                                 appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16154                                 appendPQExpBufferChar(q, ')');
16155                         }
16156
16157                         if (coninfo->condeferrable)
16158                         {
16159                                 appendPQExpBufferStr(q, " DEFERRABLE");
16160                                 if (coninfo->condeferred)
16161                                         appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16162                         }
16163
16164                         appendPQExpBufferStr(q, ";\n");
16165                 }
16166
16167                 /* If the index is clustered, we need to record that. */
16168                 if (indxinfo->indisclustered)
16169                 {
16170                         appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16171                                                           fmtId(tbinfo->dobj.name));
16172                         appendPQExpBuffer(q, " ON %s;\n",
16173                                                           fmtId(indxinfo->dobj.name));
16174                 }
16175
16176                 /*
16177                  * DROP must be fully qualified in case same name appears in
16178                  * pg_catalog
16179                  */
16180                 appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
16181                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
16182                 appendPQExpBuffer(delq, "%s ",
16183                                                   fmtId(tbinfo->dobj.name));
16184                 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16185                                                   fmtId(coninfo->dobj.name));
16186
16187                 tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16188
16189                 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16190                         ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16191                                                  tag,
16192                                                  tbinfo->dobj.namespace->dobj.name,
16193                                                  indxinfo->tablespace,
16194                                                  tbinfo->rolname, false,
16195                                                  "CONSTRAINT", SECTION_POST_DATA,
16196                                                  q->data, delq->data, NULL,
16197                                                  NULL, 0,
16198                                                  NULL, NULL);
16199         }
16200         else if (coninfo->contype == 'f')
16201         {
16202                 /*
16203                  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
16204                  * current table data is not processed
16205                  */
16206                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
16207                                                   fmtId(tbinfo->dobj.name));
16208                 appendPQExpBuffer(q, "    ADD CONSTRAINT %s %s;\n",
16209                                                   fmtId(coninfo->dobj.name),
16210                                                   coninfo->condef);
16211
16212                 /*
16213                  * DROP must be fully qualified in case same name appears in
16214                  * pg_catalog
16215                  */
16216                 appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
16217                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
16218                 appendPQExpBuffer(delq, "%s ",
16219                                                   fmtId(tbinfo->dobj.name));
16220                 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16221                                                   fmtId(coninfo->dobj.name));
16222
16223                 tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16224
16225                 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16226                         ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16227                                                  tag,
16228                                                  tbinfo->dobj.namespace->dobj.name,
16229                                                  NULL,
16230                                                  tbinfo->rolname, false,
16231                                                  "FK CONSTRAINT", SECTION_POST_DATA,
16232                                                  q->data, delq->data, NULL,
16233                                                  NULL, 0,
16234                                                  NULL, NULL);
16235         }
16236         else if (coninfo->contype == 'c' && tbinfo)
16237         {
16238                 /* CHECK constraint on a table */
16239
16240                 /* Ignore if not to be dumped separately, or if it was inherited */
16241                 if (coninfo->separate && coninfo->conislocal)
16242                 {
16243                         /* not ONLY since we want it to propagate to children */
16244                         appendPQExpBuffer(q, "ALTER TABLE %s\n",
16245                                                           fmtId(tbinfo->dobj.name));
16246                         appendPQExpBuffer(q, "    ADD CONSTRAINT %s %s;\n",
16247                                                           fmtId(coninfo->dobj.name),
16248                                                           coninfo->condef);
16249
16250                         /*
16251                          * DROP must be fully qualified in case same name appears in
16252                          * pg_catalog
16253                          */
16254                         appendPQExpBuffer(delq, "ALTER TABLE %s.",
16255                                                           fmtId(tbinfo->dobj.namespace->dobj.name));
16256                         appendPQExpBuffer(delq, "%s ",
16257                                                           fmtId(tbinfo->dobj.name));
16258                         appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16259                                                           fmtId(coninfo->dobj.name));
16260
16261                         tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16262
16263                         if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16264                                 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16265                                                          tag,
16266                                                          tbinfo->dobj.namespace->dobj.name,
16267                                                          NULL,
16268                                                          tbinfo->rolname, false,
16269                                                          "CHECK CONSTRAINT", SECTION_POST_DATA,
16270                                                          q->data, delq->data, NULL,
16271                                                          NULL, 0,
16272                                                          NULL, NULL);
16273                 }
16274         }
16275         else if (coninfo->contype == 'c' && tbinfo == NULL)
16276         {
16277                 /* CHECK constraint on a domain */
16278                 TypeInfo   *tyinfo = coninfo->condomain;
16279
16280                 /* Ignore if not to be dumped separately */
16281                 if (coninfo->separate)
16282                 {
16283                         appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
16284                                                           fmtId(tyinfo->dobj.name));
16285                         appendPQExpBuffer(q, "    ADD CONSTRAINT %s %s;\n",
16286                                                           fmtId(coninfo->dobj.name),
16287                                                           coninfo->condef);
16288
16289                         /*
16290                          * DROP must be fully qualified in case same name appears in
16291                          * pg_catalog
16292                          */
16293                         appendPQExpBuffer(delq, "ALTER DOMAIN %s.",
16294                                                           fmtId(tyinfo->dobj.namespace->dobj.name));
16295                         appendPQExpBuffer(delq, "%s ",
16296                                                           fmtId(tyinfo->dobj.name));
16297                         appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16298                                                           fmtId(coninfo->dobj.name));
16299
16300                         tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
16301
16302                         if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16303                                 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16304                                                          tag,
16305                                                          tyinfo->dobj.namespace->dobj.name,
16306                                                          NULL,
16307                                                          tyinfo->rolname, false,
16308                                                          "CHECK CONSTRAINT", SECTION_POST_DATA,
16309                                                          q->data, delq->data, NULL,
16310                                                          NULL, 0,
16311                                                          NULL, NULL);
16312                 }
16313         }
16314         else
16315         {
16316                 exit_horribly(NULL, "unrecognized constraint type: %c\n",
16317                                           coninfo->contype);
16318         }
16319
16320         /* Dump Constraint Comments --- only works for table constraints */
16321         if (tbinfo && coninfo->separate &&
16322                 coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16323                 dumpTableConstraintComment(fout, coninfo);
16324
16325         free(tag);
16326         destroyPQExpBuffer(q);
16327         destroyPQExpBuffer(delq);
16328 }
16329
16330 /*
16331  * dumpTableConstraintComment --- dump a constraint's comment if any
16332  *
16333  * This is split out because we need the function in two different places
16334  * depending on whether the constraint is dumped as part of CREATE TABLE
16335  * or as a separate ALTER command.
16336  */
16337 static void
16338 dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
16339 {
16340         TableInfo  *tbinfo = coninfo->contable;
16341         PQExpBuffer labelq = createPQExpBuffer();
16342
16343         appendPQExpBuffer(labelq, "CONSTRAINT %s ",
16344                                           fmtId(coninfo->dobj.name));
16345         appendPQExpBuffer(labelq, "ON %s",
16346                                           fmtId(tbinfo->dobj.name));
16347
16348         if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16349                 dumpComment(fout, labelq->data,
16350                                         tbinfo->dobj.namespace->dobj.name,
16351                                         tbinfo->rolname,
16352                                         coninfo->dobj.catId, 0,
16353                                         coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
16354
16355         destroyPQExpBuffer(labelq);
16356 }
16357
16358 /*
16359  * findLastBuiltinOid_V71 -
16360  *
16361  * find the last built in oid
16362  *
16363  * For 7.1 through 8.0, we do this by retrieving datlastsysoid from the
16364  * pg_database entry for the current database.
16365  */
16366 static Oid
16367 findLastBuiltinOid_V71(Archive *fout, const char *dbname)
16368 {
16369         PGresult   *res;
16370         Oid                     last_oid;
16371         PQExpBuffer query = createPQExpBuffer();
16372
16373         resetPQExpBuffer(query);
16374         appendPQExpBufferStr(query, "SELECT datlastsysoid from pg_database where datname = ");
16375         appendStringLiteralAH(query, dbname, fout);
16376
16377         res = ExecuteSqlQueryForSingleRow(fout, query->data);
16378         last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
16379         PQclear(res);
16380         destroyPQExpBuffer(query);
16381
16382         return last_oid;
16383 }
16384
16385 /*
16386  * dumpSequence
16387  *        write the declaration (not data) of one user-defined sequence
16388  */
16389 static void
16390 dumpSequence(Archive *fout, TableInfo *tbinfo)
16391 {
16392         DumpOptions *dopt = fout->dopt;
16393         PGresult   *res;
16394         char       *startv,
16395                            *incby,
16396                            *maxv,
16397                            *minv,
16398                            *cache,
16399                            *seqtype;
16400         bool            cycled;
16401         bool            is_ascending;
16402         PQExpBuffer query = createPQExpBuffer();
16403         PQExpBuffer delqry = createPQExpBuffer();
16404         PQExpBuffer labelq = createPQExpBuffer();
16405
16406         if (fout->remoteVersion >= 100000)
16407         {
16408                 /* Make sure we are in proper schema */
16409                 selectSourceSchema(fout, "pg_catalog");
16410
16411                 appendPQExpBuffer(query,
16412                                                   "SELECT format_type(seqtypid, NULL), "
16413                                                   "seqstart, seqincrement, "
16414                                                   "seqmax, seqmin, "
16415                                                   "seqcache, seqcycle "
16416                                                   "FROM pg_class c "
16417                                                   "JOIN pg_sequence s ON (s.seqrelid = c.oid) "
16418                                                   "WHERE c.oid = '%u'::oid",
16419                                                   tbinfo->dobj.catId.oid);
16420         }
16421         else if (fout->remoteVersion >= 80400)
16422         {
16423                 /*
16424                  * Before PostgreSQL 10, sequence metadata is in the sequence itself,
16425                  * so switch to the sequence's schema instead of pg_catalog.
16426                  */
16427
16428                 /* Make sure we are in proper schema */
16429                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
16430
16431                 appendPQExpBuffer(query,
16432                                                   "SELECT 'bigint'::name AS sequence_type, "
16433                                                   "start_value, increment_by, max_value, min_value, "
16434                                                   "cache_value, is_cycled FROM %s",
16435                                                   fmtId(tbinfo->dobj.name));
16436         }
16437         else
16438         {
16439                 /* Make sure we are in proper schema */
16440                 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
16441
16442                 appendPQExpBuffer(query,
16443                                                   "SELECT 'bigint'::name AS sequence_type, "
16444                                                   "0 AS start_value, increment_by, max_value, min_value, "
16445                                                   "cache_value, is_cycled FROM %s",
16446                                                   fmtId(tbinfo->dobj.name));
16447         }
16448
16449         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16450
16451         if (PQntuples(res) != 1)
16452         {
16453                 write_msg(NULL, ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)\n",
16454                                                                  "query to get data of sequence \"%s\" returned %d rows (expected 1)\n",
16455                                                                  PQntuples(res)),
16456                                   tbinfo->dobj.name, PQntuples(res));
16457                 exit_nicely(1);
16458         }
16459
16460         seqtype = PQgetvalue(res, 0, 0);
16461         startv = PQgetvalue(res, 0, 1);
16462         incby = PQgetvalue(res, 0, 2);
16463         maxv = PQgetvalue(res, 0, 3);
16464         minv = PQgetvalue(res, 0, 4);
16465         cache = PQgetvalue(res, 0, 5);
16466         cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
16467
16468         is_ascending = incby[0] != '-';
16469
16470         if (is_ascending && atoi(minv) == 1)
16471                 minv = NULL;
16472         if (!is_ascending && atoi(maxv) == -1)
16473                 maxv = NULL;
16474
16475         if (strcmp(seqtype, "smallint") == 0)
16476         {
16477                 if (!is_ascending && atoi(minv) == PG_INT16_MIN)
16478                         minv = NULL;
16479                 if (is_ascending && atoi(maxv) == PG_INT16_MAX)
16480                         maxv = NULL;
16481         }
16482         else if (strcmp(seqtype, "integer") == 0)
16483         {
16484                 if (!is_ascending && atoi(minv) == PG_INT32_MIN)
16485                         minv = NULL;
16486                 if (is_ascending && atoi(maxv) == PG_INT32_MAX)
16487                         maxv = NULL;
16488         }
16489         else if (strcmp(seqtype, "bigint") == 0)
16490         {
16491                 char            bufm[100],
16492                                         bufx[100];
16493
16494                 snprintf(bufm, sizeof(bufm), INT64_FORMAT, PG_INT64_MIN);
16495                 snprintf(bufx, sizeof(bufx), INT64_FORMAT, PG_INT64_MAX);
16496
16497                 if (!is_ascending && strcmp(minv, bufm) == 0)
16498                         minv = NULL;
16499                 if (is_ascending && strcmp(maxv, bufx) == 0)
16500                         maxv = NULL;
16501         }
16502
16503         /*
16504          * DROP must be fully qualified in case same name appears in pg_catalog
16505          */
16506         if (!tbinfo->is_identity_sequence)
16507         {
16508                 appendPQExpBuffer(delqry, "DROP SEQUENCE %s.",
16509                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
16510                 appendPQExpBuffer(delqry, "%s;\n",
16511                                                   fmtId(tbinfo->dobj.name));
16512         }
16513
16514         resetPQExpBuffer(query);
16515
16516         if (dopt->binary_upgrade)
16517         {
16518                 binary_upgrade_set_pg_class_oids(fout, query,
16519                                                                                  tbinfo->dobj.catId.oid, false);
16520                 binary_upgrade_set_type_oids_by_rel_oid(fout, query,
16521                                                                                                 tbinfo->dobj.catId.oid);
16522         }
16523
16524         if (tbinfo->is_identity_sequence)
16525         {
16526                 TableInfo  *owning_tab = findTableByOid(tbinfo->owning_tab);
16527
16528                 appendPQExpBuffer(query,
16529                                                   "ALTER TABLE %s ",
16530                                                   fmtId(owning_tab->dobj.name));
16531                 appendPQExpBuffer(query,
16532                                                   "ALTER COLUMN %s ADD GENERATED ",
16533                                                   fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
16534                 if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS)
16535                         appendPQExpBuffer(query, "ALWAYS");
16536                 else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
16537                         appendPQExpBuffer(query, "BY DEFAULT");
16538                 appendPQExpBuffer(query, " AS IDENTITY (\n    SEQUENCE NAME %s\n",
16539                                                   fmtId(tbinfo->dobj.name));
16540         }
16541         else
16542         {
16543                 appendPQExpBuffer(query,
16544                                                   "CREATE SEQUENCE %s\n",
16545                                                   fmtId(tbinfo->dobj.name));
16546
16547                 if (strcmp(seqtype, "bigint") != 0)
16548                         appendPQExpBuffer(query, "    AS %s\n", seqtype);
16549         }
16550
16551         if (fout->remoteVersion >= 80400)
16552                 appendPQExpBuffer(query, "    START WITH %s\n", startv);
16553
16554         appendPQExpBuffer(query, "    INCREMENT BY %s\n", incby);
16555
16556         if (minv)
16557                 appendPQExpBuffer(query, "    MINVALUE %s\n", minv);
16558         else
16559                 appendPQExpBufferStr(query, "    NO MINVALUE\n");
16560
16561         if (maxv)
16562                 appendPQExpBuffer(query, "    MAXVALUE %s\n", maxv);
16563         else
16564                 appendPQExpBufferStr(query, "    NO MAXVALUE\n");
16565
16566         appendPQExpBuffer(query,
16567                                           "    CACHE %s%s",
16568                                           cache, (cycled ? "\n    CYCLE" : ""));
16569
16570         if (tbinfo->is_identity_sequence)
16571                 appendPQExpBufferStr(query, "\n);\n");
16572         else
16573                 appendPQExpBufferStr(query, ";\n");
16574
16575         appendPQExpBuffer(labelq, "SEQUENCE %s", fmtId(tbinfo->dobj.name));
16576
16577         /* binary_upgrade:      no need to clear TOAST table oid */
16578
16579         if (dopt->binary_upgrade)
16580                 binary_upgrade_extension_member(query, &tbinfo->dobj,
16581                                                                                 labelq->data);
16582
16583         if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16584                 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
16585                                          tbinfo->dobj.name,
16586                                          tbinfo->dobj.namespace->dobj.name,
16587                                          NULL,
16588                                          tbinfo->rolname,
16589                                          false, "SEQUENCE", SECTION_PRE_DATA,
16590                                          query->data, delqry->data, NULL,
16591                                          NULL, 0,
16592                                          NULL, NULL);
16593
16594         /*
16595          * If the sequence is owned by a table column, emit the ALTER for it as a
16596          * separate TOC entry immediately following the sequence's own entry. It's
16597          * OK to do this rather than using full sorting logic, because the
16598          * dependency that tells us it's owned will have forced the table to be
16599          * created first.  We can't just include the ALTER in the TOC entry
16600          * because it will fail if we haven't reassigned the sequence owner to
16601          * match the table's owner.
16602          *
16603          * We need not schema-qualify the table reference because both sequence
16604          * and table must be in the same schema.
16605          */
16606         if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence)
16607         {
16608                 TableInfo  *owning_tab = findTableByOid(tbinfo->owning_tab);
16609
16610                 if (owning_tab == NULL)
16611                         exit_horribly(NULL, "failed sanity check, parent table with OID %u of sequence with OID %u not found\n",
16612                                                   tbinfo->owning_tab, tbinfo->dobj.catId.oid);
16613
16614                 if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION)
16615                 {
16616                         resetPQExpBuffer(query);
16617                         appendPQExpBuffer(query, "ALTER SEQUENCE %s",
16618                                                           fmtId(tbinfo->dobj.name));
16619                         appendPQExpBuffer(query, " OWNED BY %s",
16620                                                           fmtId(owning_tab->dobj.name));
16621                         appendPQExpBuffer(query, ".%s;\n",
16622                                                           fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
16623
16624                         if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16625                                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
16626                                                          tbinfo->dobj.name,
16627                                                          tbinfo->dobj.namespace->dobj.name,
16628                                                          NULL,
16629                                                          tbinfo->rolname,
16630                                                          false, "SEQUENCE OWNED BY", SECTION_PRE_DATA,
16631                                                          query->data, "", NULL,
16632                                                          &(tbinfo->dobj.dumpId), 1,
16633                                                          NULL, NULL);
16634                 }
16635         }
16636
16637         /* Dump Sequence Comments and Security Labels */
16638         if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16639                 dumpComment(fout, labelq->data,
16640                                         tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
16641                                         tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
16642
16643         if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
16644                 dumpSecLabel(fout, labelq->data,
16645                                          tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
16646                                          tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
16647
16648         PQclear(res);
16649
16650         destroyPQExpBuffer(query);
16651         destroyPQExpBuffer(delqry);
16652         destroyPQExpBuffer(labelq);
16653 }
16654
16655 /*
16656  * dumpSequenceData
16657  *        write the data of one user-defined sequence
16658  */
16659 static void
16660 dumpSequenceData(Archive *fout, TableDataInfo *tdinfo)
16661 {
16662         TableInfo  *tbinfo = tdinfo->tdtable;
16663         PGresult   *res;
16664         char       *last;
16665         bool            called;
16666         PQExpBuffer query = createPQExpBuffer();
16667
16668         /* Make sure we are in proper schema */
16669         selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
16670
16671         appendPQExpBuffer(query,
16672                                           "SELECT last_value, is_called FROM %s",
16673                                           fmtId(tbinfo->dobj.name));
16674
16675         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16676
16677         if (PQntuples(res) != 1)
16678         {
16679                 write_msg(NULL, ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)\n",
16680                                                                  "query to get data of sequence \"%s\" returned %d rows (expected 1)\n",
16681                                                                  PQntuples(res)),
16682                                   tbinfo->dobj.name, PQntuples(res));
16683                 exit_nicely(1);
16684         }
16685
16686         last = PQgetvalue(res, 0, 0);
16687         called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
16688
16689         resetPQExpBuffer(query);
16690         appendPQExpBufferStr(query, "SELECT pg_catalog.setval(");
16691         appendStringLiteralAH(query, fmtId(tbinfo->dobj.name), fout);
16692         appendPQExpBuffer(query, ", %s, %s);\n",
16693                                           last, (called ? "true" : "false"));
16694
16695         if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
16696                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
16697                                          tbinfo->dobj.name,
16698                                          tbinfo->dobj.namespace->dobj.name,
16699                                          NULL,
16700                                          tbinfo->rolname,
16701                                          false, "SEQUENCE SET", SECTION_DATA,
16702                                          query->data, "", NULL,
16703                                          &(tbinfo->dobj.dumpId), 1,
16704                                          NULL, NULL);
16705
16706         PQclear(res);
16707
16708         destroyPQExpBuffer(query);
16709 }
16710
16711 /*
16712  * dumpTrigger
16713  *        write the declaration of one user-defined table trigger
16714  */
16715 static void
16716 dumpTrigger(Archive *fout, TriggerInfo *tginfo)
16717 {
16718         DumpOptions *dopt = fout->dopt;
16719         TableInfo  *tbinfo = tginfo->tgtable;
16720         PQExpBuffer query;
16721         PQExpBuffer delqry;
16722         PQExpBuffer labelq;
16723         char       *tgargs;
16724         size_t          lentgargs;
16725         const char *p;
16726         int                     findx;
16727         char       *tag;
16728
16729         /*
16730          * we needn't check dobj.dump because TriggerInfo wouldn't have been
16731          * created in the first place for non-dumpable triggers
16732          */
16733         if (dopt->dataOnly)
16734                 return;
16735
16736         query = createPQExpBuffer();
16737         delqry = createPQExpBuffer();
16738         labelq = createPQExpBuffer();
16739
16740         /*
16741          * DROP must be fully qualified in case same name appears in pg_catalog
16742          */
16743         appendPQExpBuffer(delqry, "DROP TRIGGER %s ",
16744                                           fmtId(tginfo->dobj.name));
16745         appendPQExpBuffer(delqry, "ON %s.",
16746                                           fmtId(tbinfo->dobj.namespace->dobj.name));
16747         appendPQExpBuffer(delqry, "%s;\n",
16748                                           fmtId(tbinfo->dobj.name));
16749
16750         if (tginfo->tgdef)
16751         {
16752                 appendPQExpBuffer(query, "%s;\n", tginfo->tgdef);
16753         }
16754         else
16755         {
16756                 if (tginfo->tgisconstraint)
16757                 {
16758                         appendPQExpBufferStr(query, "CREATE CONSTRAINT TRIGGER ");
16759                         appendPQExpBufferStr(query, fmtId(tginfo->tgconstrname));
16760                 }
16761                 else
16762                 {
16763                         appendPQExpBufferStr(query, "CREATE TRIGGER ");
16764                         appendPQExpBufferStr(query, fmtId(tginfo->dobj.name));
16765                 }
16766                 appendPQExpBufferStr(query, "\n    ");
16767
16768                 /* Trigger type */
16769                 if (TRIGGER_FOR_BEFORE(tginfo->tgtype))
16770                         appendPQExpBufferStr(query, "BEFORE");
16771                 else if (TRIGGER_FOR_AFTER(tginfo->tgtype))
16772                         appendPQExpBufferStr(query, "AFTER");
16773                 else if (TRIGGER_FOR_INSTEAD(tginfo->tgtype))
16774                         appendPQExpBufferStr(query, "INSTEAD OF");
16775                 else
16776                 {
16777                         write_msg(NULL, "unexpected tgtype value: %d\n", tginfo->tgtype);
16778                         exit_nicely(1);
16779                 }
16780
16781                 findx = 0;
16782                 if (TRIGGER_FOR_INSERT(tginfo->tgtype))
16783                 {
16784                         appendPQExpBufferStr(query, " INSERT");
16785                         findx++;
16786                 }
16787                 if (TRIGGER_FOR_DELETE(tginfo->tgtype))
16788                 {
16789                         if (findx > 0)
16790                                 appendPQExpBufferStr(query, " OR DELETE");
16791                         else
16792                                 appendPQExpBufferStr(query, " DELETE");
16793                         findx++;
16794                 }
16795                 if (TRIGGER_FOR_UPDATE(tginfo->tgtype))
16796                 {
16797                         if (findx > 0)
16798                                 appendPQExpBufferStr(query, " OR UPDATE");
16799                         else
16800                                 appendPQExpBufferStr(query, " UPDATE");
16801                         findx++;
16802                 }
16803                 if (TRIGGER_FOR_TRUNCATE(tginfo->tgtype))
16804                 {
16805                         if (findx > 0)
16806                                 appendPQExpBufferStr(query, " OR TRUNCATE");
16807                         else
16808                                 appendPQExpBufferStr(query, " TRUNCATE");
16809                         findx++;
16810                 }
16811                 appendPQExpBuffer(query, " ON %s\n",
16812                                                   fmtId(tbinfo->dobj.name));
16813
16814                 if (tginfo->tgisconstraint)
16815                 {
16816                         if (OidIsValid(tginfo->tgconstrrelid))
16817                         {
16818                                 /* regclass output is already quoted */
16819                                 appendPQExpBuffer(query, "    FROM %s\n    ",
16820                                                                   tginfo->tgconstrrelname);
16821                         }
16822                         if (!tginfo->tgdeferrable)
16823                                 appendPQExpBufferStr(query, "NOT ");
16824                         appendPQExpBufferStr(query, "DEFERRABLE INITIALLY ");
16825                         if (tginfo->tginitdeferred)
16826                                 appendPQExpBufferStr(query, "DEFERRED\n");
16827                         else
16828                                 appendPQExpBufferStr(query, "IMMEDIATE\n");
16829                 }
16830
16831                 if (TRIGGER_FOR_ROW(tginfo->tgtype))
16832                         appendPQExpBufferStr(query, "    FOR EACH ROW\n    ");
16833                 else
16834                         appendPQExpBufferStr(query, "    FOR EACH STATEMENT\n    ");
16835
16836                 /* regproc output is already sufficiently quoted */
16837                 appendPQExpBuffer(query, "EXECUTE PROCEDURE %s(",
16838                                                   tginfo->tgfname);
16839
16840                 tgargs = (char *) PQunescapeBytea((unsigned char *) tginfo->tgargs,
16841                                                                                   &lentgargs);
16842                 p = tgargs;
16843                 for (findx = 0; findx < tginfo->tgnargs; findx++)
16844                 {
16845                         /* find the embedded null that terminates this trigger argument */
16846                         size_t          tlen = strlen(p);
16847
16848                         if (p + tlen >= tgargs + lentgargs)
16849                         {
16850                                 /* hm, not found before end of bytea value... */
16851                                 write_msg(NULL, "invalid argument string (%s) for trigger \"%s\" on table \"%s\"\n",
16852                                                   tginfo->tgargs,
16853                                                   tginfo->dobj.name,
16854                                                   tbinfo->dobj.name);
16855                                 exit_nicely(1);
16856                         }
16857
16858                         if (findx > 0)
16859                                 appendPQExpBufferStr(query, ", ");
16860                         appendStringLiteralAH(query, p, fout);
16861                         p += tlen + 1;
16862                 }
16863                 free(tgargs);
16864                 appendPQExpBufferStr(query, ");\n");
16865         }
16866
16867         if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
16868         {
16869                 appendPQExpBuffer(query, "\nALTER TABLE %s ",
16870                                                   fmtId(tbinfo->dobj.name));
16871                 switch (tginfo->tgenabled)
16872                 {
16873                         case 'D':
16874                         case 'f':
16875                                 appendPQExpBufferStr(query, "DISABLE");
16876                                 break;
16877                         case 'A':
16878                                 appendPQExpBufferStr(query, "ENABLE ALWAYS");
16879                                 break;
16880                         case 'R':
16881                                 appendPQExpBufferStr(query, "ENABLE REPLICA");
16882                                 break;
16883                         default:
16884                                 appendPQExpBufferStr(query, "ENABLE");
16885                                 break;
16886                 }
16887                 appendPQExpBuffer(query, " TRIGGER %s;\n",
16888                                                   fmtId(tginfo->dobj.name));
16889         }
16890
16891         appendPQExpBuffer(labelq, "TRIGGER %s ",
16892                                           fmtId(tginfo->dobj.name));
16893         appendPQExpBuffer(labelq, "ON %s",
16894                                           fmtId(tbinfo->dobj.name));
16895
16896         tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name);
16897
16898         if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16899                 ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
16900                                          tag,
16901                                          tbinfo->dobj.namespace->dobj.name,
16902                                          NULL,
16903                                          tbinfo->rolname, false,
16904                                          "TRIGGER", SECTION_POST_DATA,
16905                                          query->data, delqry->data, NULL,
16906                                          NULL, 0,
16907                                          NULL, NULL);
16908
16909         if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16910                 dumpComment(fout, labelq->data,
16911                                         tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
16912                                         tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
16913
16914         free(tag);
16915         destroyPQExpBuffer(query);
16916         destroyPQExpBuffer(delqry);
16917         destroyPQExpBuffer(labelq);
16918 }
16919
16920 /*
16921  * dumpEventTrigger
16922  *        write the declaration of one user-defined event trigger
16923  */
16924 static void
16925 dumpEventTrigger(Archive *fout, EventTriggerInfo *evtinfo)
16926 {
16927         DumpOptions *dopt = fout->dopt;
16928         PQExpBuffer query;
16929         PQExpBuffer delqry;
16930         PQExpBuffer labelq;
16931
16932         /* Skip if not to be dumped */
16933         if (!evtinfo->dobj.dump || dopt->dataOnly)
16934                 return;
16935
16936         query = createPQExpBuffer();
16937         delqry = createPQExpBuffer();
16938         labelq = createPQExpBuffer();
16939
16940         appendPQExpBufferStr(query, "CREATE EVENT TRIGGER ");
16941         appendPQExpBufferStr(query, fmtId(evtinfo->dobj.name));
16942         appendPQExpBufferStr(query, " ON ");
16943         appendPQExpBufferStr(query, fmtId(evtinfo->evtevent));
16944
16945         if (strcmp("", evtinfo->evttags) != 0)
16946         {
16947                 appendPQExpBufferStr(query, "\n         WHEN TAG IN (");
16948                 appendPQExpBufferStr(query, evtinfo->evttags);
16949                 appendPQExpBufferChar(query, ')');
16950         }
16951
16952         appendPQExpBufferStr(query, "\n   EXECUTE PROCEDURE ");
16953         appendPQExpBufferStr(query, evtinfo->evtfname);
16954         appendPQExpBufferStr(query, "();\n");
16955
16956         if (evtinfo->evtenabled != 'O')
16957         {
16958                 appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ",
16959                                                   fmtId(evtinfo->dobj.name));
16960                 switch (evtinfo->evtenabled)
16961                 {
16962                         case 'D':
16963                                 appendPQExpBufferStr(query, "DISABLE");
16964                                 break;
16965                         case 'A':
16966                                 appendPQExpBufferStr(query, "ENABLE ALWAYS");
16967                                 break;
16968                         case 'R':
16969                                 appendPQExpBufferStr(query, "ENABLE REPLICA");
16970                                 break;
16971                         default:
16972                                 appendPQExpBufferStr(query, "ENABLE");
16973                                 break;
16974                 }
16975                 appendPQExpBufferStr(query, ";\n");
16976         }
16977
16978         appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n",
16979                                           fmtId(evtinfo->dobj.name));
16980
16981         appendPQExpBuffer(labelq, "EVENT TRIGGER %s",
16982                                           fmtId(evtinfo->dobj.name));
16983
16984         if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16985                 ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId,
16986                                          evtinfo->dobj.name, NULL, NULL,
16987                                          evtinfo->evtowner, false,
16988                                          "EVENT TRIGGER", SECTION_POST_DATA,
16989                                          query->data, delqry->data, NULL,
16990                                          NULL, 0,
16991                                          NULL, NULL);
16992
16993         if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16994                 dumpComment(fout, labelq->data,
16995                                         NULL, evtinfo->evtowner,
16996                                         evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
16997
16998         destroyPQExpBuffer(query);
16999         destroyPQExpBuffer(delqry);
17000         destroyPQExpBuffer(labelq);
17001 }
17002
17003 /*
17004  * dumpRule
17005  *              Dump a rule
17006  */
17007 static void
17008 dumpRule(Archive *fout, RuleInfo *rinfo)
17009 {
17010         DumpOptions *dopt = fout->dopt;
17011         TableInfo  *tbinfo = rinfo->ruletable;
17012         bool            is_view;
17013         PQExpBuffer query;
17014         PQExpBuffer cmd;
17015         PQExpBuffer delcmd;
17016         PQExpBuffer labelq;
17017         PGresult   *res;
17018         char       *tag;
17019
17020         /* Skip if not to be dumped */
17021         if (!rinfo->dobj.dump || dopt->dataOnly)
17022                 return;
17023
17024         /*
17025          * If it is an ON SELECT rule that is created implicitly by CREATE VIEW,
17026          * we do not want to dump it as a separate object.
17027          */
17028         if (!rinfo->separate)
17029                 return;
17030
17031         /*
17032          * If it's an ON SELECT rule, we want to print it as a view definition,
17033          * instead of a rule.
17034          */
17035         is_view = (rinfo->ev_type == '1' && rinfo->is_instead);
17036
17037         /*
17038          * Make sure we are in proper schema.
17039          */
17040         selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
17041
17042         query = createPQExpBuffer();
17043         cmd = createPQExpBuffer();
17044         delcmd = createPQExpBuffer();
17045         labelq = createPQExpBuffer();
17046
17047         if (is_view)
17048         {
17049                 PQExpBuffer result;
17050
17051                 /*
17052                  * We need OR REPLACE here because we'll be replacing a dummy view.
17053                  * Otherwise this should look largely like the regular view dump code.
17054                  */
17055                 appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s",
17056                                                   fmtId(tbinfo->dobj.name));
17057                 if (nonemptyReloptions(tbinfo->reloptions))
17058                 {
17059                         appendPQExpBufferStr(cmd, " WITH (");
17060                         appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout);
17061                         appendPQExpBufferChar(cmd, ')');
17062                 }
17063                 result = createViewAsClause(fout, tbinfo);
17064                 appendPQExpBuffer(cmd, " AS\n%s", result->data);
17065                 destroyPQExpBuffer(result);
17066                 if (tbinfo->checkoption != NULL)
17067                         appendPQExpBuffer(cmd, "\n  WITH %s CHECK OPTION",
17068                                                           tbinfo->checkoption);
17069                 appendPQExpBufferStr(cmd, ";\n");
17070         }
17071         else
17072         {
17073                 /* In the rule case, just print pg_get_ruledef's result verbatim */
17074                 appendPQExpBuffer(query,
17075                                                   "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)",
17076                                                   rinfo->dobj.catId.oid);
17077
17078                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17079
17080                 if (PQntuples(res) != 1)
17081                 {
17082                         write_msg(NULL, "query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned\n",
17083                                           rinfo->dobj.name, tbinfo->dobj.name);
17084                         exit_nicely(1);
17085                 }
17086
17087                 printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
17088
17089                 PQclear(res);
17090         }
17091
17092         /*
17093          * Add the command to alter the rules replication firing semantics if it
17094          * differs from the default.
17095          */
17096         if (rinfo->ev_enabled != 'O')
17097         {
17098                 appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtId(tbinfo->dobj.name));
17099                 switch (rinfo->ev_enabled)
17100                 {
17101                         case 'A':
17102                                 appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n",
17103                                                                   fmtId(rinfo->dobj.name));
17104                                 break;
17105                         case 'R':
17106                                 appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n",
17107                                                                   fmtId(rinfo->dobj.name));
17108                                 break;
17109                         case 'D':
17110                                 appendPQExpBuffer(cmd, "DISABLE RULE %s;\n",
17111                                                                   fmtId(rinfo->dobj.name));
17112                                 break;
17113                 }
17114         }
17115
17116         /*
17117          * DROP must be fully qualified in case same name appears in pg_catalog
17118          */
17119         if (is_view)
17120         {
17121                 /*
17122                  * We can't DROP a view's ON SELECT rule.  Instead, use CREATE OR
17123                  * REPLACE VIEW to replace the rule with something with minimal
17124                  * dependencies.
17125                  */
17126                 PQExpBuffer result;
17127
17128                 appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s.",
17129                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
17130                 appendPQExpBuffer(delcmd, "%s",
17131                                                   fmtId(tbinfo->dobj.name));
17132                 result = createDummyViewAsClause(fout, tbinfo);
17133                 appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data);
17134                 destroyPQExpBuffer(result);
17135         }
17136         else
17137         {
17138                 appendPQExpBuffer(delcmd, "DROP RULE %s ",
17139                                                   fmtId(rinfo->dobj.name));
17140                 appendPQExpBuffer(delcmd, "ON %s.",
17141                                                   fmtId(tbinfo->dobj.namespace->dobj.name));
17142                 appendPQExpBuffer(delcmd, "%s;\n",
17143                                                   fmtId(tbinfo->dobj.name));
17144         }
17145
17146         appendPQExpBuffer(labelq, "RULE %s",
17147                                           fmtId(rinfo->dobj.name));
17148         appendPQExpBuffer(labelq, " ON %s",
17149                                           fmtId(tbinfo->dobj.name));
17150
17151         tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name);
17152
17153         if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17154                 ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
17155                                          tag,
17156                                          tbinfo->dobj.namespace->dobj.name,
17157                                          NULL,
17158                                          tbinfo->rolname, false,
17159                                          "RULE", SECTION_POST_DATA,
17160                                          cmd->data, delcmd->data, NULL,
17161                                          NULL, 0,
17162                                          NULL, NULL);
17163
17164         /* Dump rule comments */
17165         if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17166                 dumpComment(fout, labelq->data,
17167                                         tbinfo->dobj.namespace->dobj.name,
17168                                         tbinfo->rolname,
17169                                         rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
17170
17171         free(tag);
17172         destroyPQExpBuffer(query);
17173         destroyPQExpBuffer(cmd);
17174         destroyPQExpBuffer(delcmd);
17175         destroyPQExpBuffer(labelq);
17176 }
17177
17178 /*
17179  * getExtensionMembership --- obtain extension membership data
17180  *
17181  * We need to identify objects that are extension members as soon as they're
17182  * loaded, so that we can correctly determine whether they need to be dumped.
17183  * Generally speaking, extension member objects will get marked as *not* to
17184  * be dumped, as they will be recreated by the single CREATE EXTENSION
17185  * command.  However, in binary upgrade mode we still need to dump the members
17186  * individually.
17187  */
17188 void
17189 getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
17190                                            int numExtensions)
17191 {
17192         PQExpBuffer query;
17193         PGresult   *res;
17194         int                     ntups,
17195                                 nextmembers,
17196                                 i;
17197         int                     i_classid,
17198                                 i_objid,
17199                                 i_refobjid;
17200         ExtensionMemberId *extmembers;
17201         ExtensionInfo *ext;
17202
17203         /* Nothing to do if no extensions */
17204         if (numExtensions == 0)
17205                 return;
17206
17207         /* Make sure we are in proper schema */
17208         selectSourceSchema(fout, "pg_catalog");
17209
17210         query = createPQExpBuffer();
17211
17212         /* refclassid constraint is redundant but may speed the search */
17213         appendPQExpBufferStr(query, "SELECT "
17214                                                  "classid, objid, refobjid "
17215                                                  "FROM pg_depend "
17216                                                  "WHERE refclassid = 'pg_extension'::regclass "
17217                                                  "AND deptype = 'e' "
17218                                                  "ORDER BY 3");
17219
17220         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17221
17222         ntups = PQntuples(res);
17223
17224         i_classid = PQfnumber(res, "classid");
17225         i_objid = PQfnumber(res, "objid");
17226         i_refobjid = PQfnumber(res, "refobjid");
17227
17228         extmembers = (ExtensionMemberId *) pg_malloc(ntups * sizeof(ExtensionMemberId));
17229         nextmembers = 0;
17230
17231         /*
17232          * Accumulate data into extmembers[].
17233          *
17234          * Since we ordered the SELECT by referenced ID, we can expect that
17235          * multiple entries for the same extension will appear together; this
17236          * saves on searches.
17237          */
17238         ext = NULL;
17239
17240         for (i = 0; i < ntups; i++)
17241         {
17242                 CatalogId       objId;
17243                 Oid                     extId;
17244
17245                 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
17246                 objId.oid = atooid(PQgetvalue(res, i, i_objid));
17247                 extId = atooid(PQgetvalue(res, i, i_refobjid));
17248
17249                 if (ext == NULL ||
17250                         ext->dobj.catId.oid != extId)
17251                         ext = findExtensionByOid(extId);
17252
17253                 if (ext == NULL)
17254                 {
17255                         /* shouldn't happen */
17256                         fprintf(stderr, "could not find referenced extension %u\n", extId);
17257                         continue;
17258                 }
17259
17260                 extmembers[nextmembers].catId = objId;
17261                 extmembers[nextmembers].ext = ext;
17262                 nextmembers++;
17263         }
17264
17265         PQclear(res);
17266
17267         /* Remember the data for use later */
17268         setExtensionMembership(extmembers, nextmembers);
17269
17270         destroyPQExpBuffer(query);
17271 }
17272
17273 /*
17274  * processExtensionTables --- deal with extension configuration tables
17275  *
17276  * There are two parts to this process:
17277  *
17278  * 1. Identify and create dump records for extension configuration tables.
17279  *
17280  *        Extensions can mark tables as "configuration", which means that the user
17281  *        is able and expected to modify those tables after the extension has been
17282  *        loaded.  For these tables, we dump out only the data- the structure is
17283  *        expected to be handled at CREATE EXTENSION time, including any indexes or
17284  *        foreign keys, which brings us to-
17285  *
17286  * 2. Record FK dependencies between configuration tables.
17287  *
17288  *        Due to the FKs being created at CREATE EXTENSION time and therefore before
17289  *        the data is loaded, we have to work out what the best order for reloading
17290  *        the data is, to avoid FK violations when the tables are restored.  This is
17291  *        not perfect- we can't handle circular dependencies and if any exist they
17292  *        will cause an invalid dump to be produced (though at least all of the data
17293  *        is included for a user to manually restore).  This is currently documented
17294  *        but perhaps we can provide a better solution in the future.
17295  */
17296 void
17297 processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
17298                                            int numExtensions)
17299 {
17300         DumpOptions *dopt = fout->dopt;
17301         PQExpBuffer query;
17302         PGresult   *res;
17303         int                     ntups,
17304                                 i;
17305         int                     i_conrelid,
17306                                 i_confrelid;
17307
17308         /* Nothing to do if no extensions */
17309         if (numExtensions == 0)
17310                 return;
17311
17312         /*
17313          * Identify extension configuration tables and create TableDataInfo
17314          * objects for them, ensuring their data will be dumped even though the
17315          * tables themselves won't be.
17316          *
17317          * Note that we create TableDataInfo objects even in schemaOnly mode, ie,
17318          * user data in a configuration table is treated like schema data. This
17319          * seems appropriate since system data in a config table would get
17320          * reloaded by CREATE EXTENSION.
17321          */
17322         for (i = 0; i < numExtensions; i++)
17323         {
17324                 ExtensionInfo *curext = &(extinfo[i]);
17325                 char       *extconfig = curext->extconfig;
17326                 char       *extcondition = curext->extcondition;
17327                 char      **extconfigarray = NULL;
17328                 char      **extconditionarray = NULL;
17329                 int                     nconfigitems;
17330                 int                     nconditionitems;
17331
17332                 if (parsePGArray(extconfig, &extconfigarray, &nconfigitems) &&
17333                         parsePGArray(extcondition, &extconditionarray, &nconditionitems) &&
17334                         nconfigitems == nconditionitems)
17335                 {
17336                         int                     j;
17337
17338                         for (j = 0; j < nconfigitems; j++)
17339                         {
17340                                 TableInfo  *configtbl;
17341                                 Oid                     configtbloid = atooid(extconfigarray[j]);
17342                                 bool            dumpobj =
17343                                 curext->dobj.dump & DUMP_COMPONENT_DEFINITION;
17344
17345                                 configtbl = findTableByOid(configtbloid);
17346                                 if (configtbl == NULL)
17347                                         continue;
17348
17349                                 /*
17350                                  * Tables of not-to-be-dumped extensions shouldn't be dumped
17351                                  * unless the table or its schema is explicitly included
17352                                  */
17353                                 if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION))
17354                                 {
17355                                         /* check table explicitly requested */
17356                                         if (table_include_oids.head != NULL &&
17357                                                 simple_oid_list_member(&table_include_oids,
17358                                                                                            configtbloid))
17359                                                 dumpobj = true;
17360
17361                                         /* check table's schema explicitly requested */
17362                                         if (configtbl->dobj.namespace->dobj.dump &
17363                                                 DUMP_COMPONENT_DATA)
17364                                                 dumpobj = true;
17365                                 }
17366
17367                                 /* check table excluded by an exclusion switch */
17368                                 if (table_exclude_oids.head != NULL &&
17369                                         simple_oid_list_member(&table_exclude_oids,
17370                                                                                    configtbloid))
17371                                         dumpobj = false;
17372
17373                                 /* check schema excluded by an exclusion switch */
17374                                 if (simple_oid_list_member(&schema_exclude_oids,
17375                                                                                    configtbl->dobj.namespace->dobj.catId.oid))
17376                                         dumpobj = false;
17377
17378                                 if (dumpobj)
17379                                 {
17380                                         /*
17381                                          * Note: config tables are dumped without OIDs regardless
17382                                          * of the --oids setting.  This is because row filtering
17383                                          * conditions aren't compatible with dumping OIDs.
17384                                          */
17385                                         makeTableDataInfo(dopt, configtbl, false);
17386                                         if (configtbl->dataObj != NULL)
17387                                         {
17388                                                 if (strlen(extconditionarray[j]) > 0)
17389                                                         configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
17390                                         }
17391                                 }
17392                         }
17393                 }
17394                 if (extconfigarray)
17395                         free(extconfigarray);
17396                 if (extconditionarray)
17397                         free(extconditionarray);
17398         }
17399
17400         /*
17401          * Now that all the TableInfoData objects have been created for all the
17402          * extensions, check their FK dependencies and register them to try and
17403          * dump the data out in an order that they can be restored in.
17404          *
17405          * Note that this is not a problem for user tables as their FKs are
17406          * recreated after the data has been loaded.
17407          */
17408
17409         /* Make sure we are in proper schema */
17410         selectSourceSchema(fout, "pg_catalog");
17411
17412         query = createPQExpBuffer();
17413
17414         printfPQExpBuffer(query,
17415                                           "SELECT conrelid, confrelid "
17416                                           "FROM pg_constraint "
17417                                           "JOIN pg_depend ON (objid = confrelid) "
17418                                           "WHERE contype = 'f' "
17419                                           "AND refclassid = 'pg_extension'::regclass "
17420                                           "AND classid = 'pg_class'::regclass;");
17421
17422         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17423         ntups = PQntuples(res);
17424
17425         i_conrelid = PQfnumber(res, "conrelid");
17426         i_confrelid = PQfnumber(res, "confrelid");
17427
17428         /* Now get the dependencies and register them */
17429         for (i = 0; i < ntups; i++)
17430         {
17431                 Oid                     conrelid,
17432                                         confrelid;
17433                 TableInfo  *reftable,
17434                                    *contable;
17435
17436                 conrelid = atooid(PQgetvalue(res, i, i_conrelid));
17437                 confrelid = atooid(PQgetvalue(res, i, i_confrelid));
17438                 contable = findTableByOid(conrelid);
17439                 reftable = findTableByOid(confrelid);
17440
17441                 if (reftable == NULL ||
17442                         reftable->dataObj == NULL ||
17443                         contable == NULL ||
17444                         contable->dataObj == NULL)
17445                         continue;
17446
17447                 /*
17448                  * Make referencing TABLE_DATA object depend on the referenced table's
17449                  * TABLE_DATA object.
17450                  */
17451                 addObjectDependency(&contable->dataObj->dobj,
17452                                                         reftable->dataObj->dobj.dumpId);
17453         }
17454         PQclear(res);
17455         destroyPQExpBuffer(query);
17456 }
17457
17458 /*
17459  * getDependencies --- obtain available dependency data
17460  */
17461 static void
17462 getDependencies(Archive *fout)
17463 {
17464         PQExpBuffer query;
17465         PGresult   *res;
17466         int                     ntups,
17467                                 i;
17468         int                     i_classid,
17469                                 i_objid,
17470                                 i_refclassid,
17471                                 i_refobjid,
17472                                 i_deptype;
17473         DumpableObject *dobj,
17474                            *refdobj;
17475
17476         if (g_verbose)
17477                 write_msg(NULL, "reading dependency data\n");
17478
17479         /* Make sure we are in proper schema */
17480         selectSourceSchema(fout, "pg_catalog");
17481
17482         query = createPQExpBuffer();
17483
17484         /*
17485          * PIN dependencies aren't interesting, and EXTENSION dependencies were
17486          * already processed by getExtensionMembership.
17487          */
17488         appendPQExpBufferStr(query, "SELECT "
17489                                                  "classid, objid, refclassid, refobjid, deptype "
17490                                                  "FROM pg_depend "
17491                                                  "WHERE deptype != 'p' AND deptype != 'e' "
17492                                                  "ORDER BY 1,2");
17493
17494         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17495
17496         ntups = PQntuples(res);
17497
17498         i_classid = PQfnumber(res, "classid");
17499         i_objid = PQfnumber(res, "objid");
17500         i_refclassid = PQfnumber(res, "refclassid");
17501         i_refobjid = PQfnumber(res, "refobjid");
17502         i_deptype = PQfnumber(res, "deptype");
17503
17504         /*
17505          * Since we ordered the SELECT by referencing ID, we can expect that
17506          * multiple entries for the same object will appear together; this saves
17507          * on searches.
17508          */
17509         dobj = NULL;
17510
17511         for (i = 0; i < ntups; i++)
17512         {
17513                 CatalogId       objId;
17514                 CatalogId       refobjId;
17515                 char            deptype;
17516
17517                 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
17518                 objId.oid = atooid(PQgetvalue(res, i, i_objid));
17519                 refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
17520                 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
17521                 deptype = *(PQgetvalue(res, i, i_deptype));
17522
17523                 if (dobj == NULL ||
17524                         dobj->catId.tableoid != objId.tableoid ||
17525                         dobj->catId.oid != objId.oid)
17526                         dobj = findObjectByCatalogId(objId);
17527
17528                 /*
17529                  * Failure to find objects mentioned in pg_depend is not unexpected,
17530                  * since for example we don't collect info about TOAST tables.
17531                  */
17532                 if (dobj == NULL)
17533                 {
17534 #ifdef NOT_USED
17535                         fprintf(stderr, "no referencing object %u %u\n",
17536                                         objId.tableoid, objId.oid);
17537 #endif
17538                         continue;
17539                 }
17540
17541                 refdobj = findObjectByCatalogId(refobjId);
17542
17543                 if (refdobj == NULL)
17544                 {
17545 #ifdef NOT_USED
17546                         fprintf(stderr, "no referenced object %u %u\n",
17547                                         refobjId.tableoid, refobjId.oid);
17548 #endif
17549                         continue;
17550                 }
17551
17552                 /*
17553                  * Ordinarily, table rowtypes have implicit dependencies on their
17554                  * tables.  However, for a composite type the implicit dependency goes
17555                  * the other way in pg_depend; which is the right thing for DROP but
17556                  * it doesn't produce the dependency ordering we need. So in that one
17557                  * case, we reverse the direction of the dependency.
17558                  */
17559                 if (deptype == 'i' &&
17560                         dobj->objType == DO_TABLE &&
17561                         refdobj->objType == DO_TYPE)
17562                         addObjectDependency(refdobj, dobj->dumpId);
17563                 else
17564                         /* normal case */
17565                         addObjectDependency(dobj, refdobj->dumpId);
17566         }
17567
17568         PQclear(res);
17569
17570         destroyPQExpBuffer(query);
17571 }
17572
17573
17574 /*
17575  * createBoundaryObjects - create dummy DumpableObjects to represent
17576  * dump section boundaries.
17577  */
17578 static DumpableObject *
17579 createBoundaryObjects(void)
17580 {
17581         DumpableObject *dobjs;
17582
17583         dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
17584
17585         dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
17586         dobjs[0].catId = nilCatalogId;
17587         AssignDumpId(dobjs + 0);
17588         dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
17589
17590         dobjs[1].objType = DO_POST_DATA_BOUNDARY;
17591         dobjs[1].catId = nilCatalogId;
17592         AssignDumpId(dobjs + 1);
17593         dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
17594
17595         return dobjs;
17596 }
17597
17598 /*
17599  * addBoundaryDependencies - add dependencies as needed to enforce the dump
17600  * section boundaries.
17601  */
17602 static void
17603 addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
17604                                                 DumpableObject *boundaryObjs)
17605 {
17606         DumpableObject *preDataBound = boundaryObjs + 0;
17607         DumpableObject *postDataBound = boundaryObjs + 1;
17608         int                     i;
17609
17610         for (i = 0; i < numObjs; i++)
17611         {
17612                 DumpableObject *dobj = dobjs[i];
17613
17614                 /*
17615                  * The classification of object types here must match the SECTION_xxx
17616                  * values assigned during subsequent ArchiveEntry calls!
17617                  */
17618                 switch (dobj->objType)
17619                 {
17620                         case DO_NAMESPACE:
17621                         case DO_EXTENSION:
17622                         case DO_TYPE:
17623                         case DO_SHELL_TYPE:
17624                         case DO_FUNC:
17625                         case DO_AGG:
17626                         case DO_OPERATOR:
17627                         case DO_ACCESS_METHOD:
17628                         case DO_OPCLASS:
17629                         case DO_OPFAMILY:
17630                         case DO_COLLATION:
17631                         case DO_CONVERSION:
17632                         case DO_TABLE:
17633                         case DO_ATTRDEF:
17634                         case DO_PROCLANG:
17635                         case DO_CAST:
17636                         case DO_DUMMY_TYPE:
17637                         case DO_TSPARSER:
17638                         case DO_TSDICT:
17639                         case DO_TSTEMPLATE:
17640                         case DO_TSCONFIG:
17641                         case DO_FDW:
17642                         case DO_FOREIGN_SERVER:
17643                         case DO_TRANSFORM:
17644                         case DO_BLOB:
17645                                 /* Pre-data objects: must come before the pre-data boundary */
17646                                 addObjectDependency(preDataBound, dobj->dumpId);
17647                                 break;
17648                         case DO_TABLE_DATA:
17649                         case DO_SEQUENCE_SET:
17650                         case DO_BLOB_DATA:
17651                                 /* Data objects: must come between the boundaries */
17652                                 addObjectDependency(dobj, preDataBound->dumpId);
17653                                 addObjectDependency(postDataBound, dobj->dumpId);
17654                                 break;
17655                         case DO_INDEX:
17656                         case DO_STATSEXT:
17657                         case DO_REFRESH_MATVIEW:
17658                         case DO_TRIGGER:
17659                         case DO_EVENT_TRIGGER:
17660                         case DO_DEFAULT_ACL:
17661                         case DO_POLICY:
17662                         case DO_PUBLICATION:
17663                         case DO_PUBLICATION_REL:
17664                         case DO_SUBSCRIPTION:
17665                                 /* Post-data objects: must come after the post-data boundary */
17666                                 addObjectDependency(dobj, postDataBound->dumpId);
17667                                 break;
17668                         case DO_RULE:
17669                                 /* Rules are post-data, but only if dumped separately */
17670                                 if (((RuleInfo *) dobj)->separate)
17671                                         addObjectDependency(dobj, postDataBound->dumpId);
17672                                 break;
17673                         case DO_CONSTRAINT:
17674                         case DO_FK_CONSTRAINT:
17675                                 /* Constraints are post-data, but only if dumped separately */
17676                                 if (((ConstraintInfo *) dobj)->separate)
17677                                         addObjectDependency(dobj, postDataBound->dumpId);
17678                                 break;
17679                         case DO_PRE_DATA_BOUNDARY:
17680                                 /* nothing to do */
17681                                 break;
17682                         case DO_POST_DATA_BOUNDARY:
17683                                 /* must come after the pre-data boundary */
17684                                 addObjectDependency(dobj, preDataBound->dumpId);
17685                                 break;
17686                 }
17687         }
17688 }
17689
17690
17691 /*
17692  * BuildArchiveDependencies - create dependency data for archive TOC entries
17693  *
17694  * The raw dependency data obtained by getDependencies() is not terribly
17695  * useful in an archive dump, because in many cases there are dependency
17696  * chains linking through objects that don't appear explicitly in the dump.
17697  * For example, a view will depend on its _RETURN rule while the _RETURN rule
17698  * will depend on other objects --- but the rule will not appear as a separate
17699  * object in the dump.  We need to adjust the view's dependencies to include
17700  * whatever the rule depends on that is included in the dump.
17701  *
17702  * Just to make things more complicated, there are also "special" dependencies
17703  * such as the dependency of a TABLE DATA item on its TABLE, which we must
17704  * not rearrange because pg_restore knows that TABLE DATA only depends on
17705  * its table.  In these cases we must leave the dependencies strictly as-is
17706  * even if they refer to not-to-be-dumped objects.
17707  *
17708  * To handle this, the convention is that "special" dependencies are created
17709  * during ArchiveEntry calls, and an archive TOC item that has any such
17710  * entries will not be touched here.  Otherwise, we recursively search the
17711  * DumpableObject data structures to build the correct dependencies for each
17712  * archive TOC item.
17713  */
17714 static void
17715 BuildArchiveDependencies(Archive *fout)
17716 {
17717         ArchiveHandle *AH = (ArchiveHandle *) fout;
17718         TocEntry   *te;
17719
17720         /* Scan all TOC entries in the archive */
17721         for (te = AH->toc->next; te != AH->toc; te = te->next)
17722         {
17723                 DumpableObject *dobj;
17724                 DumpId     *dependencies;
17725                 int                     nDeps;
17726                 int                     allocDeps;
17727
17728                 /* No need to process entries that will not be dumped */
17729                 if (te->reqs == 0)
17730                         continue;
17731                 /* Ignore entries that already have "special" dependencies */
17732                 if (te->nDeps > 0)
17733                         continue;
17734                 /* Otherwise, look up the item's original DumpableObject, if any */
17735                 dobj = findObjectByDumpId(te->dumpId);
17736                 if (dobj == NULL)
17737                         continue;
17738                 /* No work if it has no dependencies */
17739                 if (dobj->nDeps <= 0)
17740                         continue;
17741                 /* Set up work array */
17742                 allocDeps = 64;
17743                 dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
17744                 nDeps = 0;
17745                 /* Recursively find all dumpable dependencies */
17746                 findDumpableDependencies(AH, dobj,
17747                                                                  &dependencies, &nDeps, &allocDeps);
17748                 /* And save 'em ... */
17749                 if (nDeps > 0)
17750                 {
17751                         dependencies = (DumpId *) pg_realloc(dependencies,
17752                                                                                                  nDeps * sizeof(DumpId));
17753                         te->dependencies = dependencies;
17754                         te->nDeps = nDeps;
17755                 }
17756                 else
17757                         free(dependencies);
17758         }
17759 }
17760
17761 /* Recursive search subroutine for BuildArchiveDependencies */
17762 static void
17763 findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj,
17764                                                  DumpId **dependencies, int *nDeps, int *allocDeps)
17765 {
17766         int                     i;
17767
17768         /*
17769          * Ignore section boundary objects: if we search through them, we'll
17770          * report lots of bogus dependencies.
17771          */
17772         if (dobj->objType == DO_PRE_DATA_BOUNDARY ||
17773                 dobj->objType == DO_POST_DATA_BOUNDARY)
17774                 return;
17775
17776         for (i = 0; i < dobj->nDeps; i++)
17777         {
17778                 DumpId          depid = dobj->dependencies[i];
17779
17780                 if (TocIDRequired(AH, depid) != 0)
17781                 {
17782                         /* Object will be dumped, so just reference it as a dependency */
17783                         if (*nDeps >= *allocDeps)
17784                         {
17785                                 *allocDeps *= 2;
17786                                 *dependencies = (DumpId *) pg_realloc(*dependencies,
17787                                                                                                           *allocDeps * sizeof(DumpId));
17788                         }
17789                         (*dependencies)[*nDeps] = depid;
17790                         (*nDeps)++;
17791                 }
17792                 else
17793                 {
17794                         /*
17795                          * Object will not be dumped, so recursively consider its deps. We
17796                          * rely on the assumption that sortDumpableObjects already broke
17797                          * any dependency loops, else we might recurse infinitely.
17798                          */
17799                         DumpableObject *otherdobj = findObjectByDumpId(depid);
17800
17801                         if (otherdobj)
17802                                 findDumpableDependencies(AH, otherdobj,
17803                                                                                  dependencies, nDeps, allocDeps);
17804                 }
17805         }
17806 }
17807
17808
17809 /*
17810  * selectSourceSchema - make the specified schema the active search path
17811  * in the source database.
17812  *
17813  * NB: pg_catalog is explicitly searched after the specified schema;
17814  * so user names are only qualified if they are cross-schema references,
17815  * and system names are only qualified if they conflict with a user name
17816  * in the current schema.
17817  *
17818  * Whenever the selected schema is not pg_catalog, be careful to qualify
17819  * references to system catalogs and types in our emitted commands!
17820  *
17821  * This function is called only from selectSourceSchemaOnAH and
17822  * selectSourceSchema.
17823  */
17824 static void
17825 selectSourceSchema(Archive *fout, const char *schemaName)
17826 {
17827         PQExpBuffer query;
17828
17829         /* This is checked by the callers already */
17830         Assert(schemaName != NULL && *schemaName != '\0');
17831
17832         query = createPQExpBuffer();
17833         appendPQExpBuffer(query, "SET search_path = %s",
17834                                           fmtId(schemaName));
17835         if (strcmp(schemaName, "pg_catalog") != 0)
17836                 appendPQExpBufferStr(query, ", pg_catalog");
17837
17838         ExecuteSqlStatement(fout, query->data);
17839
17840         destroyPQExpBuffer(query);
17841 }
17842
17843 /*
17844  * getFormattedTypeName - retrieve a nicely-formatted type name for the
17845  * given type name.
17846  *
17847  * NB: the result may depend on the currently-selected search_path; this is
17848  * why we don't try to cache the names.
17849  */
17850 static char *
17851 getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
17852 {
17853         char       *result;
17854         PQExpBuffer query;
17855         PGresult   *res;
17856
17857         if (oid == 0)
17858         {
17859                 if ((opts & zeroAsOpaque) != 0)
17860                         return pg_strdup(g_opaque_type);
17861                 else if ((opts & zeroAsAny) != 0)
17862                         return pg_strdup("'any'");
17863                 else if ((opts & zeroAsStar) != 0)
17864                         return pg_strdup("*");
17865                 else if ((opts & zeroAsNone) != 0)
17866                         return pg_strdup("NONE");
17867         }
17868
17869         query = createPQExpBuffer();
17870         appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
17871                                           oid);
17872
17873         res = ExecuteSqlQueryForSingleRow(fout, query->data);
17874
17875         /* result of format_type is already quoted */
17876         result = pg_strdup(PQgetvalue(res, 0, 0));
17877
17878         PQclear(res);
17879         destroyPQExpBuffer(query);
17880
17881         return result;
17882 }
17883
17884 /*
17885  * Return a column list clause for the given relation.
17886  *
17887  * Special case: if there are no undropped columns in the relation, return
17888  * "", not an invalid "()" column list.
17889  */
17890 static const char *
17891 fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer)
17892 {
17893         int                     numatts = ti->numatts;
17894         char      **attnames = ti->attnames;
17895         bool       *attisdropped = ti->attisdropped;
17896         bool            needComma;
17897         int                     i;
17898
17899         appendPQExpBufferChar(buffer, '(');
17900         needComma = false;
17901         for (i = 0; i < numatts; i++)
17902         {
17903                 if (attisdropped[i])
17904                         continue;
17905                 if (needComma)
17906                         appendPQExpBufferStr(buffer, ", ");
17907                 appendPQExpBufferStr(buffer, fmtId(attnames[i]));
17908                 needComma = true;
17909         }
17910
17911         if (!needComma)
17912                 return "";                              /* no undropped columns */
17913
17914         appendPQExpBufferChar(buffer, ')');
17915         return buffer->data;
17916 }
17917
17918 /*
17919  * Check if a reloptions array is nonempty.
17920  */
17921 static bool
17922 nonemptyReloptions(const char *reloptions)
17923 {
17924         /* Don't want to print it if it's just "{}" */
17925         return (reloptions != NULL && strlen(reloptions) > 2);
17926 }
17927
17928 /*
17929  * Format a reloptions array and append it to the given buffer.
17930  *
17931  * "prefix" is prepended to the option names; typically it's "" or "toast.".
17932  */
17933 static void
17934 appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
17935                                                 const char *prefix, Archive *fout)
17936 {
17937         bool            res;
17938
17939         res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
17940                                                                 fout->std_strings);
17941         if (!res)
17942                 write_msg(NULL, "WARNING: could not parse reloptions array\n");
17943 }