]> granicus.if.org Git - postgresql/blob - src/bin/pg_dump/pg_dump.c
Improve spelling of new FINALFUNC_MODIFY aggregate attribute.
[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-2018, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *      pg_dump will read the system catalogs in a database and dump out a
11  *      script that reproduces the schema in terms of SQL that is understood
12  *      by PostgreSQL
13  *
14  *      Note that pg_dump runs in a transaction-snapshot mode transaction,
15  *      so it sees a consistent snapshot of the database including system
16  *      catalogs. However, it relies in part on various specialized backend
17  *      functions like pg_get_indexdef(), and those things tend to look at
18  *      the currently committed state.  So it is possible to get 'cache
19  *      lookup failed' error if someone performs DDL changes while a dump is
20  *      happening. The window for this sort of thing is from the acquisition
21  *      of the transaction snapshot to getSchemaData() (when pg_dump acquires
22  *      AccessShareLock on every table it intends to dump). It isn't very large,
23  *      but it can happen.
24  *
25  *      http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
26  *
27  * IDENTIFICATION
28  *        src/bin/pg_dump/pg_dump.c
29  *
30  *-------------------------------------------------------------------------
31  */
32 #include "postgres_fe.h"
33
34 #include <unistd.h>
35 #include <ctype.h>
36 #ifdef HAVE_TERMIOS_H
37 #include <termios.h>
38 #endif
39
40 #include "getopt_long.h"
41
42 #include "access/attnum.h"
43 #include "access/sysattr.h"
44 #include "access/transam.h"
45 #include "catalog/pg_aggregate_d.h"
46 #include "catalog/pg_am_d.h"
47 #include "catalog/pg_attribute_d.h"
48 #include "catalog/pg_cast_d.h"
49 #include "catalog/pg_class_d.h"
50 #include "catalog/pg_default_acl_d.h"
51 #include "catalog/pg_largeobject_d.h"
52 #include "catalog/pg_largeobject_metadata_d.h"
53 #include "catalog/pg_proc_d.h"
54 #include "catalog/pg_trigger_d.h"
55 #include "catalog/pg_type_d.h"
56 #include "libpq/libpq-fs.h"
57
58 #include "dumputils.h"
59 #include "parallel.h"
60 #include "pg_backup_db.h"
61 #include "pg_backup_utils.h"
62 #include "pg_dump.h"
63 #include "fe_utils/connect.h"
64 #include "fe_utils/string_utils.h"
65
66
67 typedef struct
68 {
69         const char *descr;                      /* comment for an object */
70         Oid                     classoid;               /* object class (catalog OID) */
71         Oid                     objoid;                 /* object OID */
72         int                     objsubid;               /* subobject (table column #) */
73 } CommentItem;
74
75 typedef struct
76 {
77         const char *provider;           /* label provider of this security label */
78         const char *label;                      /* security label for an object */
79         Oid                     classoid;               /* object class (catalog OID) */
80         Oid                     objoid;                 /* object OID */
81         int                     objsubid;               /* subobject (table column #) */
82 } SecLabelItem;
83
84 typedef enum OidOptions
85 {
86         zeroAsOpaque = 1,
87         zeroAsAny = 2,
88         zeroAsStar = 4,
89         zeroAsNone = 8
90 } OidOptions;
91
92 /* global decls */
93 bool            g_verbose;                      /* User wants verbose narration of our
94                                                                  * activities. */
95 static bool dosync = true;              /* Issue fsync() to make dump durable on disk. */
96
97 /* subquery used to convert user ID (eg, datdba) to user name */
98 static const char *username_subquery;
99
100 /*
101  * For 8.0 and earlier servers, pulled from pg_database, for 8.1+ we use
102  * FirstNormalObjectId - 1.
103  */
104 static Oid      g_last_builtin_oid; /* value of the last builtin oid */
105
106 /* The specified names/patterns should to match at least one entity */
107 static int      strict_names = 0;
108
109 /*
110  * Object inclusion/exclusion lists
111  *
112  * The string lists record the patterns given by command-line switches,
113  * which we then convert to lists of OIDs of matching objects.
114  */
115 static SimpleStringList schema_include_patterns = {NULL, NULL};
116 static SimpleOidList schema_include_oids = {NULL, NULL};
117 static SimpleStringList schema_exclude_patterns = {NULL, NULL};
118 static SimpleOidList schema_exclude_oids = {NULL, NULL};
119
120 static SimpleStringList table_include_patterns = {NULL, NULL};
121 static SimpleOidList table_include_oids = {NULL, NULL};
122 static SimpleStringList table_exclude_patterns = {NULL, NULL};
123 static SimpleOidList table_exclude_oids = {NULL, NULL};
124 static SimpleStringList tabledata_exclude_patterns = {NULL, NULL};
125 static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
126
127
128 char            g_opaque_type[10];      /* name for the opaque type */
129
130 /* placeholders for the delimiters for comments */
131 char            g_comment_start[10];
132 char            g_comment_end[10];
133
134 static const CatalogId nilCatalogId = {0, 0};
135
136 /*
137  * Macro for producing quoted, schema-qualified name of a dumpable object.
138  * Note implicit dependence on "fout"; we should get rid of that argument.
139  */
140 #define fmtQualifiedDumpable(obj) \
141         fmtQualifiedId(fout->remoteVersion, \
142                                    (obj)->dobj.namespace->dobj.name, \
143                                    (obj)->dobj.name)
144
145 static void help(const char *progname);
146 static void setup_connection(Archive *AH,
147                                  const char *dumpencoding, const char *dumpsnapshot,
148                                  char *use_role);
149 static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
150 static void expand_schema_name_patterns(Archive *fout,
151                                                         SimpleStringList *patterns,
152                                                         SimpleOidList *oids,
153                                                         bool strict_names);
154 static void expand_table_name_patterns(Archive *fout,
155                                                    SimpleStringList *patterns,
156                                                    SimpleOidList *oids,
157                                                    bool strict_names);
158 static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid);
159 static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
160 static void refreshMatViewData(Archive *fout, TableDataInfo *tdinfo);
161 static void guessConstraintInheritance(TableInfo *tblinfo, int numTables);
162 static void dumpComment(Archive *fout, const char *type, const char *name,
163                         const char *namespace, const char *owner,
164                         CatalogId catalogId, int subid, DumpId dumpId);
165 static int findComments(Archive *fout, Oid classoid, Oid objoid,
166                          CommentItem **items);
167 static int      collectComments(Archive *fout, CommentItem **items);
168 static void dumpSecLabel(Archive *fout, const char *type, const char *name,
169                          const char *namespace, const char *owner,
170                          CatalogId catalogId, int subid, DumpId dumpId);
171 static int findSecLabels(Archive *fout, Oid classoid, Oid objoid,
172                           SecLabelItem **items);
173 static int      collectSecLabels(Archive *fout, SecLabelItem **items);
174 static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
175 static void dumpNamespace(Archive *fout, NamespaceInfo *nspinfo);
176 static void dumpExtension(Archive *fout, ExtensionInfo *extinfo);
177 static void dumpType(Archive *fout, TypeInfo *tyinfo);
178 static void dumpBaseType(Archive *fout, TypeInfo *tyinfo);
179 static void dumpEnumType(Archive *fout, TypeInfo *tyinfo);
180 static void dumpRangeType(Archive *fout, TypeInfo *tyinfo);
181 static void dumpUndefinedType(Archive *fout, TypeInfo *tyinfo);
182 static void dumpDomain(Archive *fout, TypeInfo *tyinfo);
183 static void dumpCompositeType(Archive *fout, TypeInfo *tyinfo);
184 static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo);
185 static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
186 static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
187 static void dumpFunc(Archive *fout, FuncInfo *finfo);
188 static void dumpCast(Archive *fout, CastInfo *cast);
189 static void dumpTransform(Archive *fout, TransformInfo *transform);
190 static void dumpOpr(Archive *fout, OprInfo *oprinfo);
191 static void dumpAccessMethod(Archive *fout, AccessMethodInfo *oprinfo);
192 static void dumpOpclass(Archive *fout, OpclassInfo *opcinfo);
193 static void dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo);
194 static void dumpCollation(Archive *fout, CollInfo *collinfo);
195 static void dumpConversion(Archive *fout, ConvInfo *convinfo);
196 static void dumpRule(Archive *fout, RuleInfo *rinfo);
197 static void dumpAgg(Archive *fout, AggInfo *agginfo);
198 static void dumpTrigger(Archive *fout, TriggerInfo *tginfo);
199 static void dumpEventTrigger(Archive *fout, EventTriggerInfo *evtinfo);
200 static void dumpTable(Archive *fout, TableInfo *tbinfo);
201 static void dumpTableSchema(Archive *fout, TableInfo *tbinfo);
202 static void dumpAttrDef(Archive *fout, AttrDefInfo *adinfo);
203 static void dumpSequence(Archive *fout, TableInfo *tbinfo);
204 static void dumpSequenceData(Archive *fout, TableDataInfo *tdinfo);
205 static void dumpIndex(Archive *fout, IndxInfo *indxinfo);
206 static void dumpIndexAttach(Archive *fout, IndexAttachInfo *attachinfo);
207 static void dumpStatisticsExt(Archive *fout, StatsExtInfo *statsextinfo);
208 static void dumpConstraint(Archive *fout, ConstraintInfo *coninfo);
209 static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo);
210 static void dumpTSParser(Archive *fout, TSParserInfo *prsinfo);
211 static void dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo);
212 static void dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo);
213 static void dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo);
214 static void dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo);
215 static void dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo);
216 static void dumpUserMappings(Archive *fout,
217                                  const char *servername, const char *namespace,
218                                  const char *owner, CatalogId catalogId, DumpId dumpId);
219 static void dumpDefaultACL(Archive *fout, DefaultACLInfo *daclinfo);
220
221 static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
222                 const char *type, const char *name, const char *subname,
223                 const char *nspname, const char *owner,
224                 const char *acls, const char *racls,
225                 const char *initacls, const char *initracls);
226
227 static void getDependencies(Archive *fout);
228 static void BuildArchiveDependencies(Archive *fout);
229 static void findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj,
230                                                  DumpId **dependencies, int *nDeps, int *allocDeps);
231
232 static DumpableObject *createBoundaryObjects(void);
233 static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
234                                                 DumpableObject *boundaryObjs);
235
236 static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
237 static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind);
238 static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids);
239 static void buildMatViewRefreshDependencies(Archive *fout);
240 static void getTableDataFKConstraints(void);
241 static char *format_function_arguments(FuncInfo *finfo, char *funcargs,
242                                                   bool is_agg);
243 static char *format_function_arguments_old(Archive *fout,
244                                                           FuncInfo *finfo, int nallargs,
245                                                           char **allargtypes,
246                                                           char **argmodes,
247                                                           char **argnames);
248 static char *format_function_signature(Archive *fout,
249                                                   FuncInfo *finfo, bool honor_quotes);
250 static char *convertRegProcReference(Archive *fout,
251                                                 const char *proc);
252 static char *getFormattedOperatorName(Archive *fout, const char *oproid);
253 static char *convertTSFunction(Archive *fout, Oid funcOid);
254 static Oid      findLastBuiltinOid_V71(Archive *fout);
255 static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
256 static void getBlobs(Archive *fout);
257 static void dumpBlob(Archive *fout, BlobInfo *binfo);
258 static int      dumpBlobs(Archive *fout, void *arg);
259 static void dumpPolicy(Archive *fout, PolicyInfo *polinfo);
260 static void dumpPublication(Archive *fout, PublicationInfo *pubinfo);
261 static void dumpPublicationTable(Archive *fout, PublicationRelInfo *pubrinfo);
262 static void dumpSubscription(Archive *fout, SubscriptionInfo *subinfo);
263 static void dumpDatabase(Archive *AH);
264 static void dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
265                                    const char *dbname, Oid dboid);
266 static void dumpEncoding(Archive *AH);
267 static void dumpStdStrings(Archive *AH);
268 static void dumpSearchPath(Archive *AH);
269 static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
270                                                                                  PQExpBuffer upgrade_buffer,
271                                                                                  Oid pg_type_oid,
272                                                                                  bool force_array_type);
273 static bool binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
274                                                                                 PQExpBuffer upgrade_buffer, Oid pg_rel_oid);
275 static void binary_upgrade_set_pg_class_oids(Archive *fout,
276                                                                  PQExpBuffer upgrade_buffer,
277                                                                  Oid pg_class_oid, bool is_index);
278 static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
279                                                                 DumpableObject *dobj,
280                                                                 const char *objtype,
281                                                                 const char *objname,
282                                                                 const char *objnamespace);
283 static const char *getAttrName(int attrnum, TableInfo *tblInfo);
284 static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
285 static bool nonemptyReloptions(const char *reloptions);
286 static void appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
287                                                 const char *prefix, Archive *fout);
288 static char *get_synchronized_snapshot(Archive *fout);
289 static void setupDumpWorker(Archive *AHX);
290 static TableInfo *getRootTableInfo(TableInfo *tbinfo);
291
292
293 int
294 main(int argc, char **argv)
295 {
296         int                     c;
297         const char *filename = NULL;
298         const char *format = "p";
299         TableInfo  *tblinfo;
300         int                     numTables;
301         DumpableObject **dobjs;
302         int                     numObjs;
303         DumpableObject *boundaryObjs;
304         int                     i;
305         int                     optindex;
306         RestoreOptions *ropt;
307         Archive    *fout;                       /* the script file */
308         const char *dumpencoding = NULL;
309         const char *dumpsnapshot = NULL;
310         char       *use_role = NULL;
311         int                     numWorkers = 1;
312         trivalue        prompt_password = TRI_DEFAULT;
313         int                     compressLevel = -1;
314         int                     plainText = 0;
315         ArchiveFormat archiveFormat = archUnknown;
316         ArchiveMode archiveMode;
317
318         static DumpOptions dopt;
319
320         static struct option long_options[] = {
321                 {"data-only", no_argument, NULL, 'a'},
322                 {"blobs", no_argument, NULL, 'b'},
323                 {"no-blobs", no_argument, NULL, 'B'},
324                 {"clean", no_argument, NULL, 'c'},
325                 {"create", no_argument, NULL, 'C'},
326                 {"dbname", required_argument, NULL, 'd'},
327                 {"file", required_argument, NULL, 'f'},
328                 {"format", required_argument, NULL, 'F'},
329                 {"host", required_argument, NULL, 'h'},
330                 {"jobs", 1, NULL, 'j'},
331                 {"no-reconnect", no_argument, NULL, 'R'},
332                 {"oids", no_argument, NULL, 'o'},
333                 {"no-owner", no_argument, NULL, 'O'},
334                 {"port", required_argument, NULL, 'p'},
335                 {"schema", required_argument, NULL, 'n'},
336                 {"exclude-schema", required_argument, NULL, 'N'},
337                 {"schema-only", no_argument, NULL, 's'},
338                 {"superuser", required_argument, NULL, 'S'},
339                 {"table", required_argument, NULL, 't'},
340                 {"exclude-table", required_argument, NULL, 'T'},
341                 {"no-password", no_argument, NULL, 'w'},
342                 {"password", no_argument, NULL, 'W'},
343                 {"username", required_argument, NULL, 'U'},
344                 {"verbose", no_argument, NULL, 'v'},
345                 {"no-privileges", no_argument, NULL, 'x'},
346                 {"no-acl", no_argument, NULL, 'x'},
347                 {"compress", required_argument, NULL, 'Z'},
348                 {"encoding", required_argument, NULL, 'E'},
349                 {"help", no_argument, NULL, '?'},
350                 {"version", no_argument, NULL, 'V'},
351
352                 /*
353                  * the following options don't have an equivalent short option letter
354                  */
355                 {"attribute-inserts", no_argument, &dopt.column_inserts, 1},
356                 {"binary-upgrade", no_argument, &dopt.binary_upgrade, 1},
357                 {"column-inserts", no_argument, &dopt.column_inserts, 1},
358                 {"disable-dollar-quoting", no_argument, &dopt.disable_dollar_quoting, 1},
359                 {"disable-triggers", no_argument, &dopt.disable_triggers, 1},
360                 {"enable-row-security", no_argument, &dopt.enable_row_security, 1},
361                 {"exclude-table-data", required_argument, NULL, 4},
362                 {"if-exists", no_argument, &dopt.if_exists, 1},
363                 {"inserts", no_argument, &dopt.dump_inserts, 1},
364                 {"lock-wait-timeout", required_argument, NULL, 2},
365                 {"no-tablespaces", no_argument, &dopt.outputNoTablespaces, 1},
366                 {"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
367                 {"load-via-partition-root", no_argument, &dopt.load_via_partition_root, 1},
368                 {"role", required_argument, NULL, 3},
369                 {"section", required_argument, NULL, 5},
370                 {"serializable-deferrable", no_argument, &dopt.serializable_deferrable, 1},
371                 {"snapshot", required_argument, NULL, 6},
372                 {"strict-names", no_argument, &strict_names, 1},
373                 {"use-set-session-authorization", no_argument, &dopt.use_setsessauth, 1},
374                 {"no-comments", no_argument, &dopt.no_comments, 1},
375                 {"no-publications", no_argument, &dopt.no_publications, 1},
376                 {"no-security-labels", no_argument, &dopt.no_security_labels, 1},
377                 {"no-synchronized-snapshots", no_argument, &dopt.no_synchronized_snapshots, 1},
378                 {"no-unlogged-table-data", no_argument, &dopt.no_unlogged_table_data, 1},
379                 {"no-subscriptions", no_argument, &dopt.no_subscriptions, 1},
380                 {"no-sync", no_argument, NULL, 7},
381
382                 {NULL, 0, NULL, 0}
383         };
384
385         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
386
387         /*
388          * Initialize what we need for parallel execution, especially for thread
389          * support on Windows.
390          */
391         init_parallel_dump_utils();
392
393         g_verbose = false;
394
395         strcpy(g_comment_start, "-- ");
396         g_comment_end[0] = '\0';
397         strcpy(g_opaque_type, "opaque");
398
399         progname = get_progname(argv[0]);
400
401         if (argc > 1)
402         {
403                 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
404                 {
405                         help(progname);
406                         exit_nicely(0);
407                 }
408                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
409                 {
410                         puts("pg_dump (PostgreSQL) " PG_VERSION);
411                         exit_nicely(0);
412                 }
413         }
414
415         InitDumpOptions(&dopt);
416
417         while ((c = getopt_long(argc, argv, "abBcCd:E:f:F:h:j:n:N:oOp:RsS:t:T:U:vwWxZ:",
418                                                         long_options, &optindex)) != -1)
419         {
420                 switch (c)
421                 {
422                         case 'a':                       /* Dump data only */
423                                 dopt.dataOnly = true;
424                                 break;
425
426                         case 'b':                       /* Dump blobs */
427                                 dopt.outputBlobs = true;
428                                 break;
429
430                         case 'B':                       /* Don't dump blobs */
431                                 dopt.dontOutputBlobs = true;
432                                 break;
433
434                         case 'c':                       /* clean (i.e., drop) schema prior to create */
435                                 dopt.outputClean = 1;
436                                 break;
437
438                         case 'C':                       /* Create DB */
439                                 dopt.outputCreateDB = 1;
440                                 break;
441
442                         case 'd':                       /* database name */
443                                 dopt.dbname = pg_strdup(optarg);
444                                 break;
445
446                         case 'E':                       /* Dump encoding */
447                                 dumpencoding = pg_strdup(optarg);
448                                 break;
449
450                         case 'f':
451                                 filename = pg_strdup(optarg);
452                                 break;
453
454                         case 'F':
455                                 format = pg_strdup(optarg);
456                                 break;
457
458                         case 'h':                       /* server host */
459                                 dopt.pghost = pg_strdup(optarg);
460                                 break;
461
462                         case 'j':                       /* number of dump jobs */
463                                 numWorkers = atoi(optarg);
464                                 break;
465
466                         case 'n':                       /* include schema(s) */
467                                 simple_string_list_append(&schema_include_patterns, optarg);
468                                 dopt.include_everything = false;
469                                 break;
470
471                         case 'N':                       /* exclude schema(s) */
472                                 simple_string_list_append(&schema_exclude_patterns, optarg);
473                                 break;
474
475                         case 'o':                       /* Dump oids */
476                                 dopt.oids = true;
477                                 break;
478
479                         case 'O':                       /* Don't reconnect to match owner */
480                                 dopt.outputNoOwner = 1;
481                                 break;
482
483                         case 'p':                       /* server port */
484                                 dopt.pgport = pg_strdup(optarg);
485                                 break;
486
487                         case 'R':
488                                 /* no-op, still accepted for backwards compatibility */
489                                 break;
490
491                         case 's':                       /* dump schema only */
492                                 dopt.schemaOnly = true;
493                                 break;
494
495                         case 'S':                       /* Username for superuser in plain text output */
496                                 dopt.outputSuperuser = pg_strdup(optarg);
497                                 break;
498
499                         case 't':                       /* include table(s) */
500                                 simple_string_list_append(&table_include_patterns, optarg);
501                                 dopt.include_everything = false;
502                                 break;
503
504                         case 'T':                       /* exclude table(s) */
505                                 simple_string_list_append(&table_exclude_patterns, optarg);
506                                 break;
507
508                         case 'U':
509                                 dopt.username = pg_strdup(optarg);
510                                 break;
511
512                         case 'v':                       /* verbose */
513                                 g_verbose = true;
514                                 break;
515
516                         case 'w':
517                                 prompt_password = TRI_NO;
518                                 break;
519
520                         case 'W':
521                                 prompt_password = TRI_YES;
522                                 break;
523
524                         case 'x':                       /* skip ACL dump */
525                                 dopt.aclsSkip = true;
526                                 break;
527
528                         case 'Z':                       /* Compression Level */
529                                 compressLevel = atoi(optarg);
530                                 if (compressLevel < 0 || compressLevel > 9)
531                                 {
532                                         write_msg(NULL, "compression level must be in range 0..9\n");
533                                         exit_nicely(1);
534                                 }
535                                 break;
536
537                         case 0:
538                                 /* This covers the long options. */
539                                 break;
540
541                         case 2:                         /* lock-wait-timeout */
542                                 dopt.lockWaitTimeout = pg_strdup(optarg);
543                                 break;
544
545                         case 3:                         /* SET ROLE */
546                                 use_role = pg_strdup(optarg);
547                                 break;
548
549                         case 4:                         /* exclude table(s) data */
550                                 simple_string_list_append(&tabledata_exclude_patterns, optarg);
551                                 break;
552
553                         case 5:                         /* section */
554                                 set_dump_section(optarg, &dopt.dumpSections);
555                                 break;
556
557                         case 6:                         /* snapshot */
558                                 dumpsnapshot = pg_strdup(optarg);
559                                 break;
560
561                         case 7:                         /* no-sync */
562                                 dosync = false;
563                                 break;
564
565                         default:
566                                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
567                                 exit_nicely(1);
568                 }
569         }
570
571         /*
572          * Non-option argument specifies database name as long as it wasn't
573          * already specified with -d / --dbname
574          */
575         if (optind < argc && dopt.dbname == NULL)
576                 dopt.dbname = argv[optind++];
577
578         /* Complain if any arguments remain */
579         if (optind < argc)
580         {
581                 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
582                                 progname, argv[optind]);
583                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
584                                 progname);
585                 exit_nicely(1);
586         }
587
588         /* --column-inserts implies --inserts */
589         if (dopt.column_inserts)
590                 dopt.dump_inserts = 1;
591
592         /*
593          * Binary upgrade mode implies dumping sequence data even in schema-only
594          * mode.  This is not exposed as a separate option, but kept separate
595          * internally for clarity.
596          */
597         if (dopt.binary_upgrade)
598                 dopt.sequence_data = 1;
599
600         if (dopt.dataOnly && dopt.schemaOnly)
601         {
602                 write_msg(NULL, "options -s/--schema-only and -a/--data-only cannot be used together\n");
603                 exit_nicely(1);
604         }
605
606         if (dopt.dataOnly && dopt.outputClean)
607         {
608                 write_msg(NULL, "options -c/--clean and -a/--data-only cannot be used together\n");
609                 exit_nicely(1);
610         }
611
612         if (dopt.dump_inserts && dopt.oids)
613         {
614                 write_msg(NULL, "options --inserts/--column-inserts and -o/--oids cannot be used together\n");
615                 write_msg(NULL, "(The INSERT command cannot set OIDs.)\n");
616                 exit_nicely(1);
617         }
618
619         if (dopt.if_exists && !dopt.outputClean)
620                 exit_horribly(NULL, "option --if-exists requires option -c/--clean\n");
621
622         /* Identify archive format to emit */
623         archiveFormat = parseArchiveFormat(format, &archiveMode);
624
625         /* archiveFormat specific setup */
626         if (archiveFormat == archNull)
627                 plainText = 1;
628
629         /* Custom and directory formats are compressed by default, others not */
630         if (compressLevel == -1)
631         {
632 #ifdef HAVE_LIBZ
633                 if (archiveFormat == archCustom || archiveFormat == archDirectory)
634                         compressLevel = Z_DEFAULT_COMPRESSION;
635                 else
636 #endif
637                         compressLevel = 0;
638         }
639
640 #ifndef HAVE_LIBZ
641         if (compressLevel != 0)
642                 write_msg(NULL, "WARNING: requested compression not available in this "
643                                   "installation -- archive will be uncompressed\n");
644         compressLevel = 0;
645 #endif
646
647         /*
648          * If emitting an archive format, we always want to emit a DATABASE item,
649          * in case --create is specified at pg_restore time.
650          */
651         if (!plainText)
652                 dopt.outputCreateDB = 1;
653
654         /*
655          * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually)
656          * parallel jobs because that's the maximum limit for the
657          * WaitForMultipleObjects() call.
658          */
659         if (numWorkers <= 0
660 #ifdef WIN32
661                 || numWorkers > MAXIMUM_WAIT_OBJECTS
662 #endif
663                 )
664                 exit_horribly(NULL, "invalid number of parallel jobs\n");
665
666         /* Parallel backup only in the directory archive format so far */
667         if (archiveFormat != archDirectory && numWorkers > 1)
668                 exit_horribly(NULL, "parallel backup only supported by the directory format\n");
669
670         /* Open the output file */
671         fout = CreateArchive(filename, archiveFormat, compressLevel, dosync,
672                                                  archiveMode, setupDumpWorker);
673
674         /* Make dump options accessible right away */
675         SetArchiveOptions(fout, &dopt, NULL);
676
677         /* Register the cleanup hook */
678         on_exit_close_archive(fout);
679
680         /* Let the archiver know how noisy to be */
681         fout->verbose = g_verbose;
682
683         /*
684          * We allow the server to be back to 8.0, and up to any minor release of
685          * our own major version.  (See also version check in pg_dumpall.c.)
686          */
687         fout->minRemoteVersion = 80000;
688         fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
689
690         fout->numWorkers = numWorkers;
691
692         /*
693          * Open the database using the Archiver, so it knows about it. Errors mean
694          * death.
695          */
696         ConnectDatabase(fout, dopt.dbname, dopt.pghost, dopt.pgport, dopt.username, prompt_password);
697         setup_connection(fout, dumpencoding, dumpsnapshot, use_role);
698
699         /*
700          * Disable security label support if server version < v9.1.x (prevents
701          * access to nonexistent pg_seclabel catalog)
702          */
703         if (fout->remoteVersion < 90100)
704                 dopt.no_security_labels = 1;
705
706         /*
707          * On hot standbys, never try to dump unlogged table data, since it will
708          * just throw an error.
709          */
710         if (fout->isStandby)
711                 dopt.no_unlogged_table_data = true;
712
713         /* Select the appropriate subquery to convert user IDs to names */
714         if (fout->remoteVersion >= 80100)
715                 username_subquery = "SELECT rolname FROM pg_catalog.pg_roles WHERE oid =";
716         else
717                 username_subquery = "SELECT usename FROM pg_catalog.pg_user WHERE usesysid =";
718
719         /* check the version for the synchronized snapshots feature */
720         if (numWorkers > 1 && fout->remoteVersion < 90200
721                 && !dopt.no_synchronized_snapshots)
722                 exit_horribly(NULL,
723                                           "Synchronized snapshots are not supported by this server version.\n"
724                                           "Run with --no-synchronized-snapshots instead if you do not need\n"
725                                           "synchronized snapshots.\n");
726
727         /* check the version when a snapshot is explicitly specified by user */
728         if (dumpsnapshot && fout->remoteVersion < 90200)
729                 exit_horribly(NULL,
730                                           "Exported snapshots are not supported by this server version.\n");
731
732         /*
733          * Find the last built-in OID, if needed (prior to 8.1)
734          *
735          * With 8.1 and above, we can just use FirstNormalObjectId - 1.
736          */
737         if (fout->remoteVersion < 80100)
738                 g_last_builtin_oid = findLastBuiltinOid_V71(fout);
739         else
740                 g_last_builtin_oid = FirstNormalObjectId - 1;
741
742         if (g_verbose)
743                 write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
744
745         /* Expand schema selection patterns into OID lists */
746         if (schema_include_patterns.head != NULL)
747         {
748                 expand_schema_name_patterns(fout, &schema_include_patterns,
749                                                                         &schema_include_oids,
750                                                                         strict_names);
751                 if (schema_include_oids.head == NULL)
752                         exit_horribly(NULL, "no matching schemas were found\n");
753         }
754         expand_schema_name_patterns(fout, &schema_exclude_patterns,
755                                                                 &schema_exclude_oids,
756                                                                 false);
757         /* non-matching exclusion patterns aren't an error */
758
759         /* Expand table selection patterns into OID lists */
760         if (table_include_patterns.head != NULL)
761         {
762                 expand_table_name_patterns(fout, &table_include_patterns,
763                                                                    &table_include_oids,
764                                                                    strict_names);
765                 if (table_include_oids.head == NULL)
766                         exit_horribly(NULL, "no matching tables were found\n");
767         }
768         expand_table_name_patterns(fout, &table_exclude_patterns,
769                                                            &table_exclude_oids,
770                                                            false);
771
772         expand_table_name_patterns(fout, &tabledata_exclude_patterns,
773                                                            &tabledata_exclude_oids,
774                                                            false);
775
776         /* non-matching exclusion patterns aren't an error */
777
778         /*
779          * Dumping blobs is the default for dumps where an inclusion switch is not
780          * used (an "include everything" dump).  -B can be used to exclude blobs
781          * from those dumps.  -b can be used to include blobs even when an
782          * inclusion switch is used.
783          *
784          * -s means "schema only" and blobs are data, not schema, so we never
785          * include blobs when -s is used.
786          */
787         if (dopt.include_everything && !dopt.schemaOnly && !dopt.dontOutputBlobs)
788                 dopt.outputBlobs = true;
789
790         /*
791          * Now scan the database and create DumpableObject structs for all the
792          * objects we intend to dump.
793          */
794         tblinfo = getSchemaData(fout, &numTables);
795
796         if (fout->remoteVersion < 80400)
797                 guessConstraintInheritance(tblinfo, numTables);
798
799         if (!dopt.schemaOnly)
800         {
801                 getTableData(&dopt, tblinfo, numTables, dopt.oids, 0);
802                 buildMatViewRefreshDependencies(fout);
803                 if (dopt.dataOnly)
804                         getTableDataFKConstraints();
805         }
806
807         if (dopt.schemaOnly && dopt.sequence_data)
808                 getTableData(&dopt, tblinfo, numTables, dopt.oids, RELKIND_SEQUENCE);
809
810         /*
811          * In binary-upgrade mode, we do not have to worry about the actual blob
812          * data or the associated metadata that resides in the pg_largeobject and
813          * pg_largeobject_metadata tables, respectively.
814          *
815          * However, we do need to collect blob information as there may be
816          * comments or other information on blobs that we do need to dump out.
817          */
818         if (dopt.outputBlobs || dopt.binary_upgrade)
819                 getBlobs(fout);
820
821         /*
822          * Collect dependency data to assist in ordering the objects.
823          */
824         getDependencies(fout);
825
826         /* Lastly, create dummy objects to represent the section boundaries */
827         boundaryObjs = createBoundaryObjects();
828
829         /* Get pointers to all the known DumpableObjects */
830         getDumpableObjects(&dobjs, &numObjs);
831
832         /*
833          * Add dummy dependencies to enforce the dump section ordering.
834          */
835         addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
836
837         /*
838          * Sort the objects into a safe dump order (no forward references).
839          *
840          * We rely on dependency information to help us determine a safe order, so
841          * the initial sort is mostly for cosmetic purposes: we sort by name to
842          * ensure that logically identical schemas will dump identically.
843          */
844         sortDumpableObjectsByTypeName(dobjs, numObjs);
845
846         /* If we do a parallel dump, we want the largest tables to go first */
847         if (archiveFormat == archDirectory && numWorkers > 1)
848                 sortDataAndIndexObjectsBySize(dobjs, numObjs);
849
850         sortDumpableObjects(dobjs, numObjs,
851                                                 boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
852
853         /*
854          * Create archive TOC entries for all the objects to be dumped, in a safe
855          * order.
856          */
857
858         /* First the special ENCODING, STDSTRINGS, and SEARCHPATH entries. */
859         dumpEncoding(fout);
860         dumpStdStrings(fout);
861         dumpSearchPath(fout);
862
863         /* The database items are always next, unless we don't want them at all */
864         if (dopt.outputCreateDB)
865                 dumpDatabase(fout);
866
867         /* Now the rearrangeable objects. */
868         for (i = 0; i < numObjs; i++)
869                 dumpDumpableObject(fout, dobjs[i]);
870
871         /*
872          * Set up options info to ensure we dump what we want.
873          */
874         ropt = NewRestoreOptions();
875         ropt->filename = filename;
876
877         /* if you change this list, see dumpOptionsFromRestoreOptions */
878         ropt->dropSchema = dopt.outputClean;
879         ropt->dataOnly = dopt.dataOnly;
880         ropt->schemaOnly = dopt.schemaOnly;
881         ropt->if_exists = dopt.if_exists;
882         ropt->column_inserts = dopt.column_inserts;
883         ropt->dumpSections = dopt.dumpSections;
884         ropt->aclsSkip = dopt.aclsSkip;
885         ropt->superuser = dopt.outputSuperuser;
886         ropt->createDB = dopt.outputCreateDB;
887         ropt->noOwner = dopt.outputNoOwner;
888         ropt->noTablespace = dopt.outputNoTablespaces;
889         ropt->disable_triggers = dopt.disable_triggers;
890         ropt->use_setsessauth = dopt.use_setsessauth;
891         ropt->disable_dollar_quoting = dopt.disable_dollar_quoting;
892         ropt->dump_inserts = dopt.dump_inserts;
893         ropt->no_comments = dopt.no_comments;
894         ropt->no_publications = dopt.no_publications;
895         ropt->no_security_labels = dopt.no_security_labels;
896         ropt->no_subscriptions = dopt.no_subscriptions;
897         ropt->lockWaitTimeout = dopt.lockWaitTimeout;
898         ropt->include_everything = dopt.include_everything;
899         ropt->enable_row_security = dopt.enable_row_security;
900         ropt->sequence_data = dopt.sequence_data;
901         ropt->binary_upgrade = dopt.binary_upgrade;
902
903         if (compressLevel == -1)
904                 ropt->compression = 0;
905         else
906                 ropt->compression = compressLevel;
907
908         ropt->suppressDumpWarnings = true;      /* We've already shown them */
909
910         SetArchiveOptions(fout, &dopt, ropt);
911
912         /* Mark which entries should be output */
913         ProcessArchiveRestoreOptions(fout);
914
915         /*
916          * The archive's TOC entries are now marked as to which ones will actually
917          * be output, so we can set up their dependency lists properly. This isn't
918          * necessary for plain-text output, though.
919          */
920         if (!plainText)
921                 BuildArchiveDependencies(fout);
922
923         /*
924          * And finally we can do the actual output.
925          *
926          * Note: for non-plain-text output formats, the output file is written
927          * inside CloseArchive().  This is, um, bizarre; but not worth changing
928          * right now.
929          */
930         if (plainText)
931                 RestoreArchive(fout);
932
933         CloseArchive(fout);
934
935         exit_nicely(0);
936 }
937
938
939 static void
940 help(const char *progname)
941 {
942         printf(_("%s dumps a database as a text file or to other formats.\n\n"), progname);
943         printf(_("Usage:\n"));
944         printf(_("  %s [OPTION]... [DBNAME]\n"), progname);
945
946         printf(_("\nGeneral options:\n"));
947         printf(_("  -f, --file=FILENAME          output file or directory name\n"));
948         printf(_("  -F, --format=c|d|t|p         output file format (custom, directory, tar,\n"
949                          "                               plain text (default))\n"));
950         printf(_("  -j, --jobs=NUM               use this many parallel jobs to dump\n"));
951         printf(_("  -v, --verbose                verbose mode\n"));
952         printf(_("  -V, --version                output version information, then exit\n"));
953         printf(_("  -Z, --compress=0-9           compression level for compressed formats\n"));
954         printf(_("  --lock-wait-timeout=TIMEOUT  fail after waiting TIMEOUT for a table lock\n"));
955         printf(_("  --no-sync                    do not wait for changes to be written safely to disk\n"));
956         printf(_("  -?, --help                   show this help, then exit\n"));
957
958         printf(_("\nOptions controlling the output content:\n"));
959         printf(_("  -a, --data-only              dump only the data, not the schema\n"));
960         printf(_("  -b, --blobs                  include large objects in dump\n"));
961         printf(_("  -B, --no-blobs               exclude large objects in dump\n"));
962         printf(_("  -c, --clean                  clean (drop) database objects before recreating\n"));
963         printf(_("  -C, --create                 include commands to create database in dump\n"));
964         printf(_("  -E, --encoding=ENCODING      dump the data in encoding ENCODING\n"));
965         printf(_("  -n, --schema=SCHEMA          dump the named schema(s) only\n"));
966         printf(_("  -N, --exclude-schema=SCHEMA  do NOT dump the named schema(s)\n"));
967         printf(_("  -o, --oids                   include OIDs in dump\n"));
968         printf(_("  -O, --no-owner               skip restoration of object ownership in\n"
969                          "                               plain-text format\n"));
970         printf(_("  -s, --schema-only            dump only the schema, no data\n"));
971         printf(_("  -S, --superuser=NAME         superuser user name to use in plain-text format\n"));
972         printf(_("  -t, --table=TABLE            dump the named table(s) only\n"));
973         printf(_("  -T, --exclude-table=TABLE    do NOT dump the named table(s)\n"));
974         printf(_("  -x, --no-privileges          do not dump privileges (grant/revoke)\n"));
975         printf(_("  --binary-upgrade             for use by upgrade utilities only\n"));
976         printf(_("  --column-inserts             dump data as INSERT commands with column names\n"));
977         printf(_("  --disable-dollar-quoting     disable dollar quoting, use SQL standard quoting\n"));
978         printf(_("  --disable-triggers           disable triggers during data-only restore\n"));
979         printf(_("  --enable-row-security        enable row security (dump only content user has\n"
980                          "                               access to)\n"));
981         printf(_("  --exclude-table-data=TABLE   do NOT dump data for the named table(s)\n"));
982         printf(_("  --if-exists                  use IF EXISTS when dropping objects\n"));
983         printf(_("  --inserts                    dump data as INSERT commands, rather than COPY\n"));
984         printf(_("  --no-comments                do not dump comments\n"));
985         printf(_("  --no-publications            do not dump publications\n"));
986         printf(_("  --no-security-labels         do not dump security label assignments\n"));
987         printf(_("  --no-subscriptions           do not dump subscriptions\n"));
988         printf(_("  --no-synchronized-snapshots  do not use synchronized snapshots in parallel jobs\n"));
989         printf(_("  --no-tablespaces             do not dump tablespace assignments\n"));
990         printf(_("  --no-unlogged-table-data     do not dump unlogged table data\n"));
991         printf(_("  --quote-all-identifiers      quote all identifiers, even if not key words\n"));
992         printf(_("  --load-via-partition-root    load partitions via the root table\n"));
993         printf(_("  --section=SECTION            dump named section (pre-data, data, or post-data)\n"));
994         printf(_("  --serializable-deferrable    wait until the dump can run without anomalies\n"));
995         printf(_("  --snapshot=SNAPSHOT          use given snapshot for the dump\n"));
996         printf(_("  --strict-names               require table and/or schema include patterns to\n"
997                          "                               match at least one entity each\n"));
998         printf(_("  --use-set-session-authorization\n"
999                          "                               use SET SESSION AUTHORIZATION commands instead of\n"
1000                          "                               ALTER OWNER commands to set ownership\n"));
1001
1002         printf(_("\nConnection options:\n"));
1003         printf(_("  -d, --dbname=DBNAME      database to dump\n"));
1004         printf(_("  -h, --host=HOSTNAME      database server host or socket directory\n"));
1005         printf(_("  -p, --port=PORT          database server port number\n"));
1006         printf(_("  -U, --username=NAME      connect as specified database user\n"));
1007         printf(_("  -w, --no-password        never prompt for password\n"));
1008         printf(_("  -W, --password           force password prompt (should happen automatically)\n"));
1009         printf(_("  --role=ROLENAME          do SET ROLE before dump\n"));
1010
1011         printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
1012                          "variable value is used.\n\n"));
1013         printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
1014 }
1015
1016 static void
1017 setup_connection(Archive *AH, const char *dumpencoding,
1018                                  const char *dumpsnapshot, char *use_role)
1019 {
1020         DumpOptions *dopt = AH->dopt;
1021         PGconn     *conn = GetConnection(AH);
1022         const char *std_strings;
1023
1024         PQclear(ExecuteSqlQueryForSingleRow(AH, ALWAYS_SECURE_SEARCH_PATH_SQL));
1025
1026         /*
1027          * Set the client encoding if requested.
1028          */
1029         if (dumpencoding)
1030         {
1031                 if (PQsetClientEncoding(conn, dumpencoding) < 0)
1032                         exit_horribly(NULL, "invalid client encoding \"%s\" specified\n",
1033                                                   dumpencoding);
1034         }
1035
1036         /*
1037          * Get the active encoding and the standard_conforming_strings setting, so
1038          * we know how to escape strings.
1039          */
1040         AH->encoding = PQclientEncoding(conn);
1041
1042         std_strings = PQparameterStatus(conn, "standard_conforming_strings");
1043         AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);
1044
1045         /*
1046          * Set the role if requested.  In a parallel dump worker, we'll be passed
1047          * use_role == NULL, but AH->use_role is already set (if user specified it
1048          * originally) and we should use that.
1049          */
1050         if (!use_role && AH->use_role)
1051                 use_role = AH->use_role;
1052
1053         /* Set the role if requested */
1054         if (use_role && AH->remoteVersion >= 80100)
1055         {
1056                 PQExpBuffer query = createPQExpBuffer();
1057
1058                 appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
1059                 ExecuteSqlStatement(AH, query->data);
1060                 destroyPQExpBuffer(query);
1061
1062                 /* save it for possible later use by parallel workers */
1063                 if (!AH->use_role)
1064                         AH->use_role = pg_strdup(use_role);
1065         }
1066
1067         /* Set the datestyle to ISO to ensure the dump's portability */
1068         ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
1069
1070         /* Likewise, avoid using sql_standard intervalstyle */
1071         if (AH->remoteVersion >= 80400)
1072                 ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
1073
1074         /*
1075          * Set extra_float_digits so that we can dump float data exactly (given
1076          * correctly implemented float I/O code, anyway)
1077          */
1078         if (AH->remoteVersion >= 90000)
1079                 ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
1080         else
1081                 ExecuteSqlStatement(AH, "SET extra_float_digits TO 2");
1082
1083         /*
1084          * If synchronized scanning is supported, disable it, to prevent
1085          * unpredictable changes in row ordering across a dump and reload.
1086          */
1087         if (AH->remoteVersion >= 80300)
1088                 ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
1089
1090         /*
1091          * Disable timeouts if supported.
1092          */
1093         ExecuteSqlStatement(AH, "SET statement_timeout = 0");
1094         if (AH->remoteVersion >= 90300)
1095                 ExecuteSqlStatement(AH, "SET lock_timeout = 0");
1096         if (AH->remoteVersion >= 90600)
1097                 ExecuteSqlStatement(AH, "SET idle_in_transaction_session_timeout = 0");
1098
1099         /*
1100          * Quote all identifiers, if requested.
1101          */
1102         if (quote_all_identifiers && AH->remoteVersion >= 90100)
1103                 ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1104
1105         /*
1106          * Adjust row-security mode, if supported.
1107          */
1108         if (AH->remoteVersion >= 90500)
1109         {
1110                 if (dopt->enable_row_security)
1111                         ExecuteSqlStatement(AH, "SET row_security = on");
1112                 else
1113                         ExecuteSqlStatement(AH, "SET row_security = off");
1114         }
1115
1116         /*
1117          * Start transaction-snapshot mode transaction to dump consistent data.
1118          */
1119         ExecuteSqlStatement(AH, "BEGIN");
1120         if (AH->remoteVersion >= 90100)
1121         {
1122                 /*
1123                  * To support the combination of serializable_deferrable with the jobs
1124                  * option we use REPEATABLE READ for the worker connections that are
1125                  * passed a snapshot.  As long as the snapshot is acquired in a
1126                  * SERIALIZABLE, READ ONLY, DEFERRABLE transaction, its use within a
1127                  * REPEATABLE READ transaction provides the appropriate integrity
1128                  * guarantees.  This is a kluge, but safe for back-patching.
1129                  */
1130                 if (dopt->serializable_deferrable && AH->sync_snapshot_id == NULL)
1131                         ExecuteSqlStatement(AH,
1132                                                                 "SET TRANSACTION ISOLATION LEVEL "
1133                                                                 "SERIALIZABLE, READ ONLY, DEFERRABLE");
1134                 else
1135                         ExecuteSqlStatement(AH,
1136                                                                 "SET TRANSACTION ISOLATION LEVEL "
1137                                                                 "REPEATABLE READ, READ ONLY");
1138         }
1139         else
1140         {
1141                 ExecuteSqlStatement(AH,
1142                                                         "SET TRANSACTION ISOLATION LEVEL "
1143                                                         "SERIALIZABLE, READ ONLY");
1144         }
1145
1146         /*
1147          * If user specified a snapshot to use, select that.  In a parallel dump
1148          * worker, we'll be passed dumpsnapshot == NULL, but AH->sync_snapshot_id
1149          * is already set (if the server can handle it) and we should use that.
1150          */
1151         if (dumpsnapshot)
1152                 AH->sync_snapshot_id = pg_strdup(dumpsnapshot);
1153
1154         if (AH->sync_snapshot_id)
1155         {
1156                 PQExpBuffer query = createPQExpBuffer();
1157
1158                 appendPQExpBuffer(query, "SET TRANSACTION SNAPSHOT ");
1159                 appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1160                 ExecuteSqlStatement(AH, query->data);
1161                 destroyPQExpBuffer(query);
1162         }
1163         else if (AH->numWorkers > 1 &&
1164                          AH->remoteVersion >= 90200 &&
1165                          !dopt->no_synchronized_snapshots)
1166         {
1167                 if (AH->isStandby && AH->remoteVersion < 100000)
1168                         exit_horribly(NULL,
1169                                                   "Synchronized snapshots on standby servers are not supported by this server version.\n"
1170                                                   "Run with --no-synchronized-snapshots instead if you do not need\n"
1171                                                   "synchronized snapshots.\n");
1172
1173
1174                 AH->sync_snapshot_id = get_synchronized_snapshot(AH);
1175         }
1176 }
1177
1178 /* Set up connection for a parallel worker process */
1179 static void
1180 setupDumpWorker(Archive *AH)
1181 {
1182         /*
1183          * We want to re-select all the same values the master connection is
1184          * using.  We'll have inherited directly-usable values in
1185          * AH->sync_snapshot_id and AH->use_role, but we need to translate the
1186          * inherited encoding value back to a string to pass to setup_connection.
1187          */
1188         setup_connection(AH,
1189                                          pg_encoding_to_char(AH->encoding),
1190                                          NULL,
1191                                          NULL);
1192 }
1193
1194 static char *
1195 get_synchronized_snapshot(Archive *fout)
1196 {
1197         char       *query = "SELECT pg_catalog.pg_export_snapshot()";
1198         char       *result;
1199         PGresult   *res;
1200
1201         res = ExecuteSqlQueryForSingleRow(fout, query);
1202         result = pg_strdup(PQgetvalue(res, 0, 0));
1203         PQclear(res);
1204
1205         return result;
1206 }
1207
1208 static ArchiveFormat
1209 parseArchiveFormat(const char *format, ArchiveMode *mode)
1210 {
1211         ArchiveFormat archiveFormat;
1212
1213         *mode = archModeWrite;
1214
1215         if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1216         {
1217                 /* This is used by pg_dumpall, and is not documented */
1218                 archiveFormat = archNull;
1219                 *mode = archModeAppend;
1220         }
1221         else if (pg_strcasecmp(format, "c") == 0)
1222                 archiveFormat = archCustom;
1223         else if (pg_strcasecmp(format, "custom") == 0)
1224                 archiveFormat = archCustom;
1225         else if (pg_strcasecmp(format, "d") == 0)
1226                 archiveFormat = archDirectory;
1227         else if (pg_strcasecmp(format, "directory") == 0)
1228                 archiveFormat = archDirectory;
1229         else if (pg_strcasecmp(format, "p") == 0)
1230                 archiveFormat = archNull;
1231         else if (pg_strcasecmp(format, "plain") == 0)
1232                 archiveFormat = archNull;
1233         else if (pg_strcasecmp(format, "t") == 0)
1234                 archiveFormat = archTar;
1235         else if (pg_strcasecmp(format, "tar") == 0)
1236                 archiveFormat = archTar;
1237         else
1238                 exit_horribly(NULL, "invalid output format \"%s\" specified\n", format);
1239         return archiveFormat;
1240 }
1241
1242 /*
1243  * Find the OIDs of all schemas matching the given list of patterns,
1244  * and append them to the given OID list.
1245  */
1246 static void
1247 expand_schema_name_patterns(Archive *fout,
1248                                                         SimpleStringList *patterns,
1249                                                         SimpleOidList *oids,
1250                                                         bool strict_names)
1251 {
1252         PQExpBuffer query;
1253         PGresult   *res;
1254         SimpleStringListCell *cell;
1255         int                     i;
1256
1257         if (patterns->head == NULL)
1258                 return;                                 /* nothing to do */
1259
1260         query = createPQExpBuffer();
1261
1262         /*
1263          * The loop below runs multiple SELECTs might sometimes result in
1264          * duplicate entries in the OID list, but we don't care.
1265          */
1266
1267         for (cell = patterns->head; cell; cell = cell->next)
1268         {
1269                 appendPQExpBuffer(query,
1270                                                   "SELECT oid FROM pg_catalog.pg_namespace n\n");
1271                 processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1272                                                           false, NULL, "n.nspname", NULL, NULL);
1273
1274                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1275                 if (strict_names && PQntuples(res) == 0)
1276                         exit_horribly(NULL, "no matching schemas were found for pattern \"%s\"\n", cell->val);
1277
1278                 for (i = 0; i < PQntuples(res); i++)
1279                 {
1280                         simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1281                 }
1282
1283                 PQclear(res);
1284                 resetPQExpBuffer(query);
1285         }
1286
1287         destroyPQExpBuffer(query);
1288 }
1289
1290 /*
1291  * Find the OIDs of all tables matching the given list of patterns,
1292  * and append them to the given OID list.
1293  */
1294 static void
1295 expand_table_name_patterns(Archive *fout,
1296                                                    SimpleStringList *patterns, SimpleOidList *oids,
1297                                                    bool strict_names)
1298 {
1299         PQExpBuffer query;
1300         PGresult   *res;
1301         SimpleStringListCell *cell;
1302         int                     i;
1303
1304         if (patterns->head == NULL)
1305                 return;                                 /* nothing to do */
1306
1307         query = createPQExpBuffer();
1308
1309         /*
1310          * this might sometimes result in duplicate entries in the OID list, but
1311          * we don't care.
1312          */
1313
1314         for (cell = patterns->head; cell; cell = cell->next)
1315         {
1316                 /*
1317                  * Query must remain ABSOLUTELY devoid of unqualified names.  This
1318                  * would be unnecessary given a pg_table_is_visible() variant taking a
1319                  * search_path argument.
1320                  */
1321                 appendPQExpBuffer(query,
1322                                                   "SELECT c.oid"
1323                                                   "\nFROM pg_catalog.pg_class c"
1324                                                   "\n     LEFT JOIN pg_catalog.pg_namespace n"
1325                                                   "\n     ON n.oid OPERATOR(pg_catalog.=) c.relnamespace"
1326                                                   "\nWHERE c.relkind OPERATOR(pg_catalog.=) ANY"
1327                                                   "\n    (array['%c', '%c', '%c', '%c', '%c', '%c'])\n",
1328                                                   RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1329                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
1330                                                   RELKIND_PARTITIONED_TABLE);
1331                 processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1332                                                           false, "n.nspname", "c.relname", NULL,
1333                                                           "pg_catalog.pg_table_is_visible(c.oid)");
1334
1335                 ExecuteSqlStatement(fout, "RESET search_path");
1336                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1337                 PQclear(ExecuteSqlQueryForSingleRow(fout,
1338                                                                                         ALWAYS_SECURE_SEARCH_PATH_SQL));
1339                 if (strict_names && PQntuples(res) == 0)
1340                         exit_horribly(NULL, "no matching tables were found for pattern \"%s\"\n", cell->val);
1341
1342                 for (i = 0; i < PQntuples(res); i++)
1343                 {
1344                         simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1345                 }
1346
1347                 PQclear(res);
1348                 resetPQExpBuffer(query);
1349         }
1350
1351         destroyPQExpBuffer(query);
1352 }
1353
1354 /*
1355  * checkExtensionMembership
1356  *              Determine whether object is an extension member, and if so,
1357  *              record an appropriate dependency and set the object's dump flag.
1358  *
1359  * It's important to call this for each object that could be an extension
1360  * member.  Generally, we integrate this with determining the object's
1361  * to-be-dumped-ness, since extension membership overrides other rules for that.
1362  *
1363  * Returns true if object is an extension member, else false.
1364  */
1365 static bool
1366 checkExtensionMembership(DumpableObject *dobj, Archive *fout)
1367 {
1368         ExtensionInfo *ext = findOwningExtension(dobj->catId);
1369
1370         if (ext == NULL)
1371                 return false;
1372
1373         dobj->ext_member = true;
1374
1375         /* Record dependency so that getDependencies needn't deal with that */
1376         addObjectDependency(dobj, ext->dobj.dumpId);
1377
1378         /*
1379          * In 9.6 and above, mark the member object to have any non-initial ACL,
1380          * policies, and security labels dumped.
1381          *
1382          * Note that any initial ACLs (see pg_init_privs) will be removed when we
1383          * extract the information about the object.  We don't provide support for
1384          * initial policies and security labels and it seems unlikely for those to
1385          * ever exist, but we may have to revisit this later.
1386          *
1387          * Prior to 9.6, we do not include any extension member components.
1388          *
1389          * In binary upgrades, we still dump all components of the members
1390          * individually, since the idea is to exactly reproduce the database
1391          * contents rather than replace the extension contents with something
1392          * different.
1393          */
1394         if (fout->dopt->binary_upgrade)
1395                 dobj->dump = ext->dobj.dump;
1396         else
1397         {
1398                 if (fout->remoteVersion < 90600)
1399                         dobj->dump = DUMP_COMPONENT_NONE;
1400                 else
1401                         dobj->dump = ext->dobj.dump_contains & (DUMP_COMPONENT_ACL |
1402                                                                                                         DUMP_COMPONENT_SECLABEL |
1403                                                                                                         DUMP_COMPONENT_POLICY);
1404         }
1405
1406         return true;
1407 }
1408
1409 /*
1410  * selectDumpableNamespace: policy-setting subroutine
1411  *              Mark a namespace as to be dumped or not
1412  */
1413 static void
1414 selectDumpableNamespace(NamespaceInfo *nsinfo, Archive *fout)
1415 {
1416         /*
1417          * If specific tables are being dumped, do not dump any complete
1418          * namespaces. If specific namespaces are being dumped, dump just those
1419          * namespaces. Otherwise, dump all non-system namespaces.
1420          */
1421         if (table_include_oids.head != NULL)
1422                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1423         else if (schema_include_oids.head != NULL)
1424                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump =
1425                         simple_oid_list_member(&schema_include_oids,
1426                                                                    nsinfo->dobj.catId.oid) ?
1427                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1428         else if (fout->remoteVersion >= 90600 &&
1429                          strcmp(nsinfo->dobj.name, "pg_catalog") == 0)
1430         {
1431                 /*
1432                  * In 9.6 and above, we dump out any ACLs defined in pg_catalog, if
1433                  * they are interesting (and not the original ACLs which were set at
1434                  * initdb time, see pg_init_privs).
1435                  */
1436                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
1437         }
1438         else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
1439                          strcmp(nsinfo->dobj.name, "information_schema") == 0)
1440         {
1441                 /* Other system schemas don't get dumped */
1442                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1443         }
1444         else if (strcmp(nsinfo->dobj.name, "public") == 0)
1445         {
1446                 /*
1447                  * The public schema is a strange beast that sits in a sort of
1448                  * no-mans-land between being a system object and a user object.  We
1449                  * don't want to dump creation or comment commands for it, because
1450                  * that complicates matters for non-superuser use of pg_dump.  But we
1451                  * should dump any ACL changes that have occurred for it, and of
1452                  * course we should dump contained objects.
1453                  */
1454                 nsinfo->dobj.dump = DUMP_COMPONENT_ACL;
1455                 nsinfo->dobj.dump_contains = DUMP_COMPONENT_ALL;
1456         }
1457         else
1458                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_ALL;
1459
1460         /*
1461          * In any case, a namespace can be excluded by an exclusion switch
1462          */
1463         if (nsinfo->dobj.dump_contains &&
1464                 simple_oid_list_member(&schema_exclude_oids,
1465                                                            nsinfo->dobj.catId.oid))
1466                 nsinfo->dobj.dump_contains = nsinfo->dobj.dump = DUMP_COMPONENT_NONE;
1467
1468         /*
1469          * If the schema belongs to an extension, allow extension membership to
1470          * override the dump decision for the schema itself.  However, this does
1471          * not change dump_contains, so this won't change what we do with objects
1472          * within the schema.  (If they belong to the extension, they'll get
1473          * suppressed by it, otherwise not.)
1474          */
1475         (void) checkExtensionMembership(&nsinfo->dobj, fout);
1476 }
1477
1478 /*
1479  * selectDumpableTable: policy-setting subroutine
1480  *              Mark a table as to be dumped or not
1481  */
1482 static void
1483 selectDumpableTable(TableInfo *tbinfo, Archive *fout)
1484 {
1485         if (checkExtensionMembership(&tbinfo->dobj, fout))
1486                 return;                                 /* extension membership overrides all else */
1487
1488         /*
1489          * If specific tables are being dumped, dump just those tables; else, dump
1490          * according to the parent namespace's dump flag.
1491          */
1492         if (table_include_oids.head != NULL)
1493                 tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
1494                                                                                                    tbinfo->dobj.catId.oid) ?
1495                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1496         else
1497                 tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump_contains;
1498
1499         /*
1500          * In any case, a table can be excluded by an exclusion switch
1501          */
1502         if (tbinfo->dobj.dump &&
1503                 simple_oid_list_member(&table_exclude_oids,
1504                                                            tbinfo->dobj.catId.oid))
1505                 tbinfo->dobj.dump = DUMP_COMPONENT_NONE;
1506 }
1507
1508 /*
1509  * selectDumpableType: policy-setting subroutine
1510  *              Mark a type as to be dumped or not
1511  *
1512  * If it's a table's rowtype or an autogenerated array type, we also apply a
1513  * special type code to facilitate sorting into the desired order.  (We don't
1514  * want to consider those to be ordinary types because that would bring tables
1515  * up into the datatype part of the dump order.)  We still set the object's
1516  * dump flag; that's not going to cause the dummy type to be dumped, but we
1517  * need it so that casts involving such types will be dumped correctly -- see
1518  * dumpCast.  This means the flag should be set the same as for the underlying
1519  * object (the table or base type).
1520  */
1521 static void
1522 selectDumpableType(TypeInfo *tyinfo, Archive *fout)
1523 {
1524         /* skip complex types, except for standalone composite types */
1525         if (OidIsValid(tyinfo->typrelid) &&
1526                 tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
1527         {
1528                 TableInfo  *tytable = findTableByOid(tyinfo->typrelid);
1529
1530                 tyinfo->dobj.objType = DO_DUMMY_TYPE;
1531                 if (tytable != NULL)
1532                         tyinfo->dobj.dump = tytable->dobj.dump;
1533                 else
1534                         tyinfo->dobj.dump = DUMP_COMPONENT_NONE;
1535                 return;
1536         }
1537
1538         /* skip auto-generated array types */
1539         if (tyinfo->isArray)
1540         {
1541                 tyinfo->dobj.objType = DO_DUMMY_TYPE;
1542
1543                 /*
1544                  * Fall through to set the dump flag; we assume that the subsequent
1545                  * rules will do the same thing as they would for the array's base
1546                  * type.  (We cannot reliably look up the base type here, since
1547                  * getTypes may not have processed it yet.)
1548                  */
1549         }
1550
1551         if (checkExtensionMembership(&tyinfo->dobj, fout))
1552                 return;                                 /* extension membership overrides all else */
1553
1554         /* Dump based on if the contents of the namespace are being dumped */
1555         tyinfo->dobj.dump = tyinfo->dobj.namespace->dobj.dump_contains;
1556 }
1557
1558 /*
1559  * selectDumpableDefaultACL: policy-setting subroutine
1560  *              Mark a default ACL as to be dumped or not
1561  *
1562  * For per-schema default ACLs, dump if the schema is to be dumped.
1563  * Otherwise dump if we are dumping "everything".  Note that dataOnly
1564  * and aclsSkip are checked separately.
1565  */
1566 static void
1567 selectDumpableDefaultACL(DefaultACLInfo *dinfo, DumpOptions *dopt)
1568 {
1569         /* Default ACLs can't be extension members */
1570
1571         if (dinfo->dobj.namespace)
1572                 /* default ACLs are considered part of the namespace */
1573                 dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump_contains;
1574         else
1575                 dinfo->dobj.dump = dopt->include_everything ?
1576                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1577 }
1578
1579 /*
1580  * selectDumpableCast: policy-setting subroutine
1581  *              Mark a cast as to be dumped or not
1582  *
1583  * Casts do not belong to any particular namespace (since they haven't got
1584  * names), nor do they have identifiable owners.  To distinguish user-defined
1585  * casts from built-in ones, we must resort to checking whether the cast's
1586  * OID is in the range reserved for initdb.
1587  */
1588 static void
1589 selectDumpableCast(CastInfo *cast, Archive *fout)
1590 {
1591         if (checkExtensionMembership(&cast->dobj, fout))
1592                 return;                                 /* extension membership overrides all else */
1593
1594         /*
1595          * This would be DUMP_COMPONENT_ACL for from-initdb casts, but they do not
1596          * support ACLs currently.
1597          */
1598         if (cast->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1599                 cast->dobj.dump = DUMP_COMPONENT_NONE;
1600         else
1601                 cast->dobj.dump = fout->dopt->include_everything ?
1602                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1603 }
1604
1605 /*
1606  * selectDumpableProcLang: policy-setting subroutine
1607  *              Mark a procedural language as to be dumped or not
1608  *
1609  * Procedural languages do not belong to any particular namespace.  To
1610  * identify built-in languages, we must resort to checking whether the
1611  * language's OID is in the range reserved for initdb.
1612  */
1613 static void
1614 selectDumpableProcLang(ProcLangInfo *plang, Archive *fout)
1615 {
1616         if (checkExtensionMembership(&plang->dobj, fout))
1617                 return;                                 /* extension membership overrides all else */
1618
1619         /*
1620          * Only include procedural languages when we are dumping everything.
1621          *
1622          * For from-initdb procedural languages, only include ACLs, as we do for
1623          * the pg_catalog namespace.  We need this because procedural languages do
1624          * not live in any namespace.
1625          */
1626         if (!fout->dopt->include_everything)
1627                 plang->dobj.dump = DUMP_COMPONENT_NONE;
1628         else
1629         {
1630                 if (plang->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1631                         plang->dobj.dump = fout->remoteVersion < 90600 ?
1632                                 DUMP_COMPONENT_NONE : DUMP_COMPONENT_ACL;
1633                 else
1634                         plang->dobj.dump = DUMP_COMPONENT_ALL;
1635         }
1636 }
1637
1638 /*
1639  * selectDumpableAccessMethod: policy-setting subroutine
1640  *              Mark an access method as to be dumped or not
1641  *
1642  * Access methods do not belong to any particular namespace.  To identify
1643  * built-in access methods, we must resort to checking whether the
1644  * method's OID is in the range reserved for initdb.
1645  */
1646 static void
1647 selectDumpableAccessMethod(AccessMethodInfo *method, Archive *fout)
1648 {
1649         if (checkExtensionMembership(&method->dobj, fout))
1650                 return;                                 /* extension membership overrides all else */
1651
1652         /*
1653          * This would be DUMP_COMPONENT_ACL for from-initdb access methods, but
1654          * they do not support ACLs currently.
1655          */
1656         if (method->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1657                 method->dobj.dump = DUMP_COMPONENT_NONE;
1658         else
1659                 method->dobj.dump = fout->dopt->include_everything ?
1660                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1661 }
1662
1663 /*
1664  * selectDumpableExtension: policy-setting subroutine
1665  *              Mark an extension as to be dumped or not
1666  *
1667  * Built-in extensions should be skipped except for checking ACLs, since we
1668  * assume those will already be installed in the target database.  We identify
1669  * such extensions by their having OIDs in the range reserved for initdb.
1670  * We dump all user-added extensions by default, or none of them if
1671  * include_everything is false (i.e., a --schema or --table switch was given).
1672  */
1673 static void
1674 selectDumpableExtension(ExtensionInfo *extinfo, DumpOptions *dopt)
1675 {
1676         /*
1677          * Use DUMP_COMPONENT_ACL for built-in extensions, to allow users to
1678          * change permissions on their member objects, if they wish to, and have
1679          * those changes preserved.
1680          */
1681         if (extinfo->dobj.catId.oid <= (Oid) g_last_builtin_oid)
1682                 extinfo->dobj.dump = extinfo->dobj.dump_contains = DUMP_COMPONENT_ACL;
1683         else
1684                 extinfo->dobj.dump = extinfo->dobj.dump_contains =
1685                         dopt->include_everything ? DUMP_COMPONENT_ALL :
1686                         DUMP_COMPONENT_NONE;
1687 }
1688
1689 /*
1690  * selectDumpablePublicationTable: policy-setting subroutine
1691  *              Mark a publication table as to be dumped or not
1692  *
1693  * Publication tables have schemas, but those are ignored in decision making,
1694  * because publications are only dumped when we are dumping everything.
1695  */
1696 static void
1697 selectDumpablePublicationTable(DumpableObject *dobj, Archive *fout)
1698 {
1699         if (checkExtensionMembership(dobj, fout))
1700                 return;                                 /* extension membership overrides all else */
1701
1702         dobj->dump = fout->dopt->include_everything ?
1703                 DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1704 }
1705
1706 /*
1707  * selectDumpableObject: policy-setting subroutine
1708  *              Mark a generic dumpable object as to be dumped or not
1709  *
1710  * Use this only for object types without a special-case routine above.
1711  */
1712 static void
1713 selectDumpableObject(DumpableObject *dobj, Archive *fout)
1714 {
1715         if (checkExtensionMembership(dobj, fout))
1716                 return;                                 /* extension membership overrides all else */
1717
1718         /*
1719          * Default policy is to dump if parent namespace is dumpable, or for
1720          * non-namespace-associated items, dump if we're dumping "everything".
1721          */
1722         if (dobj->namespace)
1723                 dobj->dump = dobj->namespace->dobj.dump_contains;
1724         else
1725                 dobj->dump = fout->dopt->include_everything ?
1726                         DUMP_COMPONENT_ALL : DUMP_COMPONENT_NONE;
1727 }
1728
1729 /*
1730  *      Dump a table's contents for loading using the COPY command
1731  *      - this routine is called by the Archiver when it wants the table
1732  *        to be dumped.
1733  */
1734
1735 static int
1736 dumpTableData_copy(Archive *fout, void *dcontext)
1737 {
1738         TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1739         TableInfo  *tbinfo = tdinfo->tdtable;
1740         const char *classname = tbinfo->dobj.name;
1741         const bool      hasoids = tbinfo->hasoids;
1742         const bool      oids = tdinfo->oids;
1743         PQExpBuffer q = createPQExpBuffer();
1744
1745         /*
1746          * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
1747          * which uses it already.
1748          */
1749         PQExpBuffer clistBuf = createPQExpBuffer();
1750         PGconn     *conn = GetConnection(fout);
1751         PGresult   *res;
1752         int                     ret;
1753         char       *copybuf;
1754         const char *column_list;
1755
1756         if (g_verbose)
1757                 write_msg(NULL, "dumping contents of table \"%s.%s\"\n",
1758                                   tbinfo->dobj.namespace->dobj.name, classname);
1759
1760         /*
1761          * Specify the column list explicitly so that we have no possibility of
1762          * retrieving data in the wrong column order.  (The default column
1763          * ordering of COPY will not be what we want in certain corner cases
1764          * involving ADD COLUMN and inheritance.)
1765          */
1766         column_list = fmtCopyColumnList(tbinfo, clistBuf);
1767
1768         if (oids && hasoids)
1769         {
1770                 appendPQExpBuffer(q, "COPY %s %s WITH OIDS TO stdout;",
1771                                                   fmtQualifiedDumpable(tbinfo),
1772                                                   column_list);
1773         }
1774         else if (tdinfo->filtercond)
1775         {
1776                 /* Note: this syntax is only supported in 8.2 and up */
1777                 appendPQExpBufferStr(q, "COPY (SELECT ");
1778                 /* klugery to get rid of parens in column list */
1779                 if (strlen(column_list) > 2)
1780                 {
1781                         appendPQExpBufferStr(q, column_list + 1);
1782                         q->data[q->len - 1] = ' ';
1783                 }
1784                 else
1785                         appendPQExpBufferStr(q, "* ");
1786                 appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
1787                                                   fmtQualifiedDumpable(tbinfo),
1788                                                   tdinfo->filtercond);
1789         }
1790         else
1791         {
1792                 appendPQExpBuffer(q, "COPY %s %s TO stdout;",
1793                                                   fmtQualifiedDumpable(tbinfo),
1794                                                   column_list);
1795         }
1796         res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
1797         PQclear(res);
1798         destroyPQExpBuffer(clistBuf);
1799
1800         for (;;)
1801         {
1802                 ret = PQgetCopyData(conn, &copybuf, 0);
1803
1804                 if (ret < 0)
1805                         break;                          /* done or error */
1806
1807                 if (copybuf)
1808                 {
1809                         WriteData(fout, copybuf, ret);
1810                         PQfreemem(copybuf);
1811                 }
1812
1813                 /* ----------
1814                  * THROTTLE:
1815                  *
1816                  * There was considerable discussion in late July, 2000 regarding
1817                  * slowing down pg_dump when backing up large tables. Users with both
1818                  * slow & fast (multi-processor) machines experienced performance
1819                  * degradation when doing a backup.
1820                  *
1821                  * Initial attempts based on sleeping for a number of ms for each ms
1822                  * of work were deemed too complex, then a simple 'sleep in each loop'
1823                  * implementation was suggested. The latter failed because the loop
1824                  * was too tight. Finally, the following was implemented:
1825                  *
1826                  * If throttle is non-zero, then
1827                  *              See how long since the last sleep.
1828                  *              Work out how long to sleep (based on ratio).
1829                  *              If sleep is more than 100ms, then
1830                  *                      sleep
1831                  *                      reset timer
1832                  *              EndIf
1833                  * EndIf
1834                  *
1835                  * where the throttle value was the number of ms to sleep per ms of
1836                  * work. The calculation was done in each loop.
1837                  *
1838                  * Most of the hard work is done in the backend, and this solution
1839                  * still did not work particularly well: on slow machines, the ratio
1840                  * was 50:1, and on medium paced machines, 1:1, and on fast
1841                  * multi-processor machines, it had little or no effect, for reasons
1842                  * that were unclear.
1843                  *
1844                  * Further discussion ensued, and the proposal was dropped.
1845                  *
1846                  * For those people who want this feature, it can be implemented using
1847                  * gettimeofday in each loop, calculating the time since last sleep,
1848                  * multiplying that by the sleep ratio, then if the result is more
1849                  * than a preset 'minimum sleep time' (say 100ms), call the 'select'
1850                  * function to sleep for a subsecond period ie.
1851                  *
1852                  * select(0, NULL, NULL, NULL, &tvi);
1853                  *
1854                  * This will return after the interval specified in the structure tvi.
1855                  * Finally, call gettimeofday again to save the 'last sleep time'.
1856                  * ----------
1857                  */
1858         }
1859         archprintf(fout, "\\.\n\n\n");
1860
1861         if (ret == -2)
1862         {
1863                 /* copy data transfer failed */
1864                 write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.\n", classname);
1865                 write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1866                 write_msg(NULL, "The command was: %s\n", q->data);
1867                 exit_nicely(1);
1868         }
1869
1870         /* Check command status and return to normal libpq state */
1871         res = PQgetResult(conn);
1872         if (PQresultStatus(res) != PGRES_COMMAND_OK)
1873         {
1874                 write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetResult() failed.\n", classname);
1875                 write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1876                 write_msg(NULL, "The command was: %s\n", q->data);
1877                 exit_nicely(1);
1878         }
1879         PQclear(res);
1880
1881         /* Do this to ensure we've pumped libpq back to idle state */
1882         if (PQgetResult(conn) != NULL)
1883                 write_msg(NULL, "WARNING: unexpected extra results during COPY of table \"%s\"\n",
1884                                   classname);
1885
1886         destroyPQExpBuffer(q);
1887         return 1;
1888 }
1889
1890 /*
1891  * Dump table data using INSERT commands.
1892  *
1893  * Caution: when we restore from an archive file direct to database, the
1894  * INSERT commands emitted by this function have to be parsed by
1895  * pg_backup_db.c's ExecuteSimpleCommands(), which will not handle comments,
1896  * E'' strings, or dollar-quoted strings.  So don't emit anything like that.
1897  */
1898 static int
1899 dumpTableData_insert(Archive *fout, void *dcontext)
1900 {
1901         TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1902         TableInfo  *tbinfo = tdinfo->tdtable;
1903         DumpOptions *dopt = fout->dopt;
1904         PQExpBuffer q = createPQExpBuffer();
1905         PQExpBuffer insertStmt = NULL;
1906         PGresult   *res;
1907         int                     tuple;
1908         int                     nfields;
1909         int                     field;
1910
1911         appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
1912                                           "SELECT * FROM ONLY %s",
1913                                           fmtQualifiedDumpable(tbinfo));
1914         if (tdinfo->filtercond)
1915                 appendPQExpBuffer(q, " %s", tdinfo->filtercond);
1916
1917         ExecuteSqlStatement(fout, q->data);
1918
1919         while (1)
1920         {
1921                 res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
1922                                                           PGRES_TUPLES_OK);
1923                 nfields = PQnfields(res);
1924                 for (tuple = 0; tuple < PQntuples(res); tuple++)
1925                 {
1926                         /*
1927                          * First time through, we build as much of the INSERT statement as
1928                          * possible in "insertStmt", which we can then just print for each
1929                          * line. If the table happens to have zero columns then this will
1930                          * be a complete statement, otherwise it will end in "VALUES(" and
1931                          * be ready to have the row's column values appended.
1932                          */
1933                         if (insertStmt == NULL)
1934                         {
1935                                 TableInfo  *targettab;
1936
1937                                 insertStmt = createPQExpBuffer();
1938
1939                                 /*
1940                                  * When load-via-partition-root is set, get the root table
1941                                  * name for the partition table, so that we can reload data
1942                                  * through the root table.
1943                                  */
1944                                 if (dopt->load_via_partition_root && tbinfo->ispartition)
1945                                         targettab = getRootTableInfo(tbinfo);
1946                                 else
1947                                         targettab = tbinfo;
1948
1949                                 appendPQExpBuffer(insertStmt, "INSERT INTO %s ",
1950                                                                   fmtQualifiedDumpable(targettab));
1951
1952                                 /* corner case for zero-column table */
1953                                 if (nfields == 0)
1954                                 {
1955                                         appendPQExpBufferStr(insertStmt, "DEFAULT VALUES;\n");
1956                                 }
1957                                 else
1958                                 {
1959                                         /* append the list of column names if required */
1960                                         if (dopt->column_inserts)
1961                                         {
1962                                                 appendPQExpBufferChar(insertStmt, '(');
1963                                                 for (field = 0; field < nfields; field++)
1964                                                 {
1965                                                         if (field > 0)
1966                                                                 appendPQExpBufferStr(insertStmt, ", ");
1967                                                         appendPQExpBufferStr(insertStmt,
1968                                                                                                  fmtId(PQfname(res, field)));
1969                                                 }
1970                                                 appendPQExpBufferStr(insertStmt, ") ");
1971                                         }
1972
1973                                         if (tbinfo->needs_override)
1974                                                 appendPQExpBufferStr(insertStmt, "OVERRIDING SYSTEM VALUE ");
1975
1976                                         appendPQExpBufferStr(insertStmt, "VALUES (");
1977                                 }
1978                         }
1979
1980                         archputs(insertStmt->data, fout);
1981
1982                         /* if it is zero-column table then we're done */
1983                         if (nfields == 0)
1984                                 continue;
1985
1986                         for (field = 0; field < nfields; field++)
1987                         {
1988                                 if (field > 0)
1989                                         archputs(", ", fout);
1990                                 if (PQgetisnull(res, tuple, field))
1991                                 {
1992                                         archputs("NULL", fout);
1993                                         continue;
1994                                 }
1995
1996                                 /* XXX This code is partially duplicated in ruleutils.c */
1997                                 switch (PQftype(res, field))
1998                                 {
1999                                         case INT2OID:
2000                                         case INT4OID:
2001                                         case INT8OID:
2002                                         case OIDOID:
2003                                         case FLOAT4OID:
2004                                         case FLOAT8OID:
2005                                         case NUMERICOID:
2006                                                 {
2007                                                         /*
2008                                                          * These types are printed without quotes unless
2009                                                          * they contain values that aren't accepted by the
2010                                                          * scanner unquoted (e.g., 'NaN').  Note that
2011                                                          * strtod() and friends might accept NaN, so we
2012                                                          * can't use that to test.
2013                                                          *
2014                                                          * In reality we only need to defend against
2015                                                          * infinity and NaN, so we need not get too crazy
2016                                                          * about pattern matching here.
2017                                                          */
2018                                                         const char *s = PQgetvalue(res, tuple, field);
2019
2020                                                         if (strspn(s, "0123456789 +-eE.") == strlen(s))
2021                                                                 archputs(s, fout);
2022                                                         else
2023                                                                 archprintf(fout, "'%s'", s);
2024                                                 }
2025                                                 break;
2026
2027                                         case BITOID:
2028                                         case VARBITOID:
2029                                                 archprintf(fout, "B'%s'",
2030                                                                    PQgetvalue(res, tuple, field));
2031                                                 break;
2032
2033                                         case BOOLOID:
2034                                                 if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
2035                                                         archputs("true", fout);
2036                                                 else
2037                                                         archputs("false", fout);
2038                                                 break;
2039
2040                                         default:
2041                                                 /* All other types are printed as string literals. */
2042                                                 resetPQExpBuffer(q);
2043                                                 appendStringLiteralAH(q,
2044                                                                                           PQgetvalue(res, tuple, field),
2045                                                                                           fout);
2046                                                 archputs(q->data, fout);
2047                                                 break;
2048                                 }
2049                         }
2050                         archputs(");\n", fout);
2051                 }
2052
2053                 if (PQntuples(res) <= 0)
2054                 {
2055                         PQclear(res);
2056                         break;
2057                 }
2058                 PQclear(res);
2059         }
2060
2061         archputs("\n\n", fout);
2062
2063         ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
2064
2065         destroyPQExpBuffer(q);
2066         if (insertStmt != NULL)
2067                 destroyPQExpBuffer(insertStmt);
2068
2069         return 1;
2070 }
2071
2072 /*
2073  * getRootTableInfo:
2074  *     get the root TableInfo for the given partition table.
2075  */
2076 static TableInfo *
2077 getRootTableInfo(TableInfo *tbinfo)
2078 {
2079         TableInfo  *parentTbinfo;
2080
2081         Assert(tbinfo->ispartition);
2082         Assert(tbinfo->numParents == 1);
2083
2084         parentTbinfo = tbinfo->parents[0];
2085         while (parentTbinfo->ispartition)
2086         {
2087                 Assert(parentTbinfo->numParents == 1);
2088                 parentTbinfo = parentTbinfo->parents[0];
2089         }
2090
2091         return parentTbinfo;
2092 }
2093
2094 /*
2095  * dumpTableData -
2096  *        dump the contents of a single table
2097  *
2098  * Actually, this just makes an ArchiveEntry for the table contents.
2099  */
2100 static void
2101 dumpTableData(Archive *fout, TableDataInfo *tdinfo)
2102 {
2103         DumpOptions *dopt = fout->dopt;
2104         TableInfo  *tbinfo = tdinfo->tdtable;
2105         PQExpBuffer copyBuf = createPQExpBuffer();
2106         PQExpBuffer clistBuf = createPQExpBuffer();
2107         DataDumperPtr dumpFn;
2108         char       *copyStmt;
2109         const char *copyFrom;
2110
2111         if (!dopt->dump_inserts)
2112         {
2113                 /* Dump/restore using COPY */
2114                 dumpFn = dumpTableData_copy;
2115
2116                 /*
2117                  * When load-via-partition-root is set, get the root table name for
2118                  * the partition table, so that we can reload data through the root
2119                  * table.
2120                  */
2121                 if (dopt->load_via_partition_root && tbinfo->ispartition)
2122                 {
2123                         TableInfo  *parentTbinfo;
2124
2125                         parentTbinfo = getRootTableInfo(tbinfo);
2126                         copyFrom = fmtQualifiedDumpable(parentTbinfo);
2127                 }
2128                 else
2129                         copyFrom = fmtQualifiedDumpable(tbinfo);
2130
2131                 /* must use 2 steps here 'cause fmtId is nonreentrant */
2132                 appendPQExpBuffer(copyBuf, "COPY %s ",
2133                                                   copyFrom);
2134                 appendPQExpBuffer(copyBuf, "%s %sFROM stdin;\n",
2135                                                   fmtCopyColumnList(tbinfo, clistBuf),
2136                                                   (tdinfo->oids && tbinfo->hasoids) ? "WITH OIDS " : "");
2137                 copyStmt = copyBuf->data;
2138         }
2139         else
2140         {
2141                 /* Restore using INSERT */
2142                 dumpFn = dumpTableData_insert;
2143                 copyStmt = NULL;
2144         }
2145
2146         /*
2147          * Note: although the TableDataInfo is a full DumpableObject, we treat its
2148          * dependency on its table as "special" and pass it to ArchiveEntry now.
2149          * See comments for BuildArchiveDependencies.
2150          */
2151         if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2152                 ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
2153                                          tbinfo->dobj.name, tbinfo->dobj.namespace->dobj.name,
2154                                          NULL, tbinfo->rolname,
2155                                          false, "TABLE DATA", SECTION_DATA,
2156                                          "", "", copyStmt,
2157                                          &(tbinfo->dobj.dumpId), 1,
2158                                          dumpFn, tdinfo);
2159
2160         destroyPQExpBuffer(copyBuf);
2161         destroyPQExpBuffer(clistBuf);
2162 }
2163
2164 /*
2165  * refreshMatViewData -
2166  *        load or refresh the contents of a single materialized view
2167  *
2168  * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
2169  * statement.
2170  */
2171 static void
2172 refreshMatViewData(Archive *fout, TableDataInfo *tdinfo)
2173 {
2174         TableInfo  *tbinfo = tdinfo->tdtable;
2175         PQExpBuffer q;
2176
2177         /* If the materialized view is not flagged as populated, skip this. */
2178         if (!tbinfo->relispopulated)
2179                 return;
2180
2181         q = createPQExpBuffer();
2182
2183         appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
2184                                           fmtQualifiedDumpable(tbinfo));
2185
2186         if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
2187                 ArchiveEntry(fout,
2188                                          tdinfo->dobj.catId,    /* catalog ID */
2189                                          tdinfo->dobj.dumpId,   /* dump ID */
2190                                          tbinfo->dobj.name, /* Name */
2191                                          tbinfo->dobj.namespace->dobj.name, /* Namespace */
2192                                          NULL,          /* Tablespace */
2193                                          tbinfo->rolname,       /* Owner */
2194                                          false,         /* with oids */
2195                                          "MATERIALIZED VIEW DATA",      /* Desc */
2196                                          SECTION_POST_DATA, /* Section */
2197                                          q->data,       /* Create */
2198                                          "",            /* Del */
2199                                          NULL,          /* Copy */
2200                                          tdinfo->dobj.dependencies, /* Deps */
2201                                          tdinfo->dobj.nDeps,    /* # Deps */
2202                                          NULL,          /* Dumper */
2203                                          NULL);         /* Dumper Arg */
2204
2205         destroyPQExpBuffer(q);
2206 }
2207
2208 /*
2209  * getTableData -
2210  *        set up dumpable objects representing the contents of tables
2211  */
2212 static void
2213 getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, bool oids, char relkind)
2214 {
2215         int                     i;
2216
2217         for (i = 0; i < numTables; i++)
2218         {
2219                 if (tblinfo[i].dobj.dump & DUMP_COMPONENT_DATA &&
2220                         (!relkind || tblinfo[i].relkind == relkind))
2221                         makeTableDataInfo(dopt, &(tblinfo[i]), oids);
2222         }
2223 }
2224
2225 /*
2226  * Make a dumpable object for the data of this specific table
2227  *
2228  * Note: we make a TableDataInfo if and only if we are going to dump the
2229  * table data; the "dump" flag in such objects isn't used.
2230  */
2231 static void
2232 makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids)
2233 {
2234         TableDataInfo *tdinfo;
2235
2236         /*
2237          * Nothing to do if we already decided to dump the table.  This will
2238          * happen for "config" tables.
2239          */
2240         if (tbinfo->dataObj != NULL)
2241                 return;
2242
2243         /* Skip VIEWs (no data to dump) */
2244         if (tbinfo->relkind == RELKIND_VIEW)
2245                 return;
2246         /* Skip FOREIGN TABLEs (no data to dump) */
2247         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
2248                 return;
2249         /* Skip partitioned tables (data in partitions) */
2250         if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
2251                 return;
2252
2253         /* Don't dump data in unlogged tables, if so requested */
2254         if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
2255                 dopt->no_unlogged_table_data)
2256                 return;
2257
2258         /* Check that the data is not explicitly excluded */
2259         if (simple_oid_list_member(&tabledata_exclude_oids,
2260                                                            tbinfo->dobj.catId.oid))
2261                 return;
2262
2263         /* OK, let's dump it */
2264         tdinfo = (TableDataInfo *) pg_malloc(sizeof(TableDataInfo));
2265
2266         if (tbinfo->relkind == RELKIND_MATVIEW)
2267                 tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
2268         else if (tbinfo->relkind == RELKIND_SEQUENCE)
2269                 tdinfo->dobj.objType = DO_SEQUENCE_SET;
2270         else
2271                 tdinfo->dobj.objType = DO_TABLE_DATA;
2272
2273         /*
2274          * Note: use tableoid 0 so that this object won't be mistaken for
2275          * something that pg_depend entries apply to.
2276          */
2277         tdinfo->dobj.catId.tableoid = 0;
2278         tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
2279         AssignDumpId(&tdinfo->dobj);
2280         tdinfo->dobj.name = tbinfo->dobj.name;
2281         tdinfo->dobj.namespace = tbinfo->dobj.namespace;
2282         tdinfo->tdtable = tbinfo;
2283         tdinfo->oids = oids;
2284         tdinfo->filtercond = NULL;      /* might get set later */
2285         addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
2286
2287         tbinfo->dataObj = tdinfo;
2288 }
2289
2290 /*
2291  * The refresh for a materialized view must be dependent on the refresh for
2292  * any materialized view that this one is dependent on.
2293  *
2294  * This must be called after all the objects are created, but before they are
2295  * sorted.
2296  */
2297 static void
2298 buildMatViewRefreshDependencies(Archive *fout)
2299 {
2300         PQExpBuffer query;
2301         PGresult   *res;
2302         int                     ntups,
2303                                 i;
2304         int                     i_classid,
2305                                 i_objid,
2306                                 i_refobjid;
2307
2308         /* No Mat Views before 9.3. */
2309         if (fout->remoteVersion < 90300)
2310                 return;
2311
2312         query = createPQExpBuffer();
2313
2314         appendPQExpBufferStr(query, "WITH RECURSIVE w AS "
2315                                                  "( "
2316                                                  "SELECT d1.objid, d2.refobjid, c2.relkind AS refrelkind "
2317                                                  "FROM pg_depend d1 "
2318                                                  "JOIN pg_class c1 ON c1.oid = d1.objid "
2319                                                  "AND c1.relkind = " CppAsString2(RELKIND_MATVIEW)
2320                                                  " JOIN pg_rewrite r1 ON r1.ev_class = d1.objid "
2321                                                  "JOIN pg_depend d2 ON d2.classid = 'pg_rewrite'::regclass "
2322                                                  "AND d2.objid = r1.oid "
2323                                                  "AND d2.refobjid <> d1.objid "
2324                                                  "JOIN pg_class c2 ON c2.oid = d2.refobjid "
2325                                                  "AND c2.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2326                                                  CppAsString2(RELKIND_VIEW) ") "
2327                                                  "WHERE d1.classid = 'pg_class'::regclass "
2328                                                  "UNION "
2329                                                  "SELECT w.objid, d3.refobjid, c3.relkind "
2330                                                  "FROM w "
2331                                                  "JOIN pg_rewrite r3 ON r3.ev_class = w.refobjid "
2332                                                  "JOIN pg_depend d3 ON d3.classid = 'pg_rewrite'::regclass "
2333                                                  "AND d3.objid = r3.oid "
2334                                                  "AND d3.refobjid <> w.refobjid "
2335                                                  "JOIN pg_class c3 ON c3.oid = d3.refobjid "
2336                                                  "AND c3.relkind IN (" CppAsString2(RELKIND_MATVIEW) ","
2337                                                  CppAsString2(RELKIND_VIEW) ") "
2338                                                  ") "
2339                                                  "SELECT 'pg_class'::regclass::oid AS classid, objid, refobjid "
2340                                                  "FROM w "
2341                                                  "WHERE refrelkind = " CppAsString2(RELKIND_MATVIEW));
2342
2343         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2344
2345         ntups = PQntuples(res);
2346
2347         i_classid = PQfnumber(res, "classid");
2348         i_objid = PQfnumber(res, "objid");
2349         i_refobjid = PQfnumber(res, "refobjid");
2350
2351         for (i = 0; i < ntups; i++)
2352         {
2353                 CatalogId       objId;
2354                 CatalogId       refobjId;
2355                 DumpableObject *dobj;
2356                 DumpableObject *refdobj;
2357                 TableInfo  *tbinfo;
2358                 TableInfo  *reftbinfo;
2359
2360                 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
2361                 objId.oid = atooid(PQgetvalue(res, i, i_objid));
2362                 refobjId.tableoid = objId.tableoid;
2363                 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
2364
2365                 dobj = findObjectByCatalogId(objId);
2366                 if (dobj == NULL)
2367                         continue;
2368
2369                 Assert(dobj->objType == DO_TABLE);
2370                 tbinfo = (TableInfo *) dobj;
2371                 Assert(tbinfo->relkind == RELKIND_MATVIEW);
2372                 dobj = (DumpableObject *) tbinfo->dataObj;
2373                 if (dobj == NULL)
2374                         continue;
2375                 Assert(dobj->objType == DO_REFRESH_MATVIEW);
2376
2377                 refdobj = findObjectByCatalogId(refobjId);
2378                 if (refdobj == NULL)
2379                         continue;
2380
2381                 Assert(refdobj->objType == DO_TABLE);
2382                 reftbinfo = (TableInfo *) refdobj;
2383                 Assert(reftbinfo->relkind == RELKIND_MATVIEW);
2384                 refdobj = (DumpableObject *) reftbinfo->dataObj;
2385                 if (refdobj == NULL)
2386                         continue;
2387                 Assert(refdobj->objType == DO_REFRESH_MATVIEW);
2388
2389                 addObjectDependency(dobj, refdobj->dumpId);
2390
2391                 if (!reftbinfo->relispopulated)
2392                         tbinfo->relispopulated = false;
2393         }
2394
2395         PQclear(res);
2396
2397         destroyPQExpBuffer(query);
2398 }
2399
2400 /*
2401  * getTableDataFKConstraints -
2402  *        add dump-order dependencies reflecting foreign key constraints
2403  *
2404  * This code is executed only in a data-only dump --- in schema+data dumps
2405  * we handle foreign key issues by not creating the FK constraints until
2406  * after the data is loaded.  In a data-only dump, however, we want to
2407  * order the table data objects in such a way that a table's referenced
2408  * tables are restored first.  (In the presence of circular references or
2409  * self-references this may be impossible; we'll detect and complain about
2410  * that during the dependency sorting step.)
2411  */
2412 static void
2413 getTableDataFKConstraints(void)
2414 {
2415         DumpableObject **dobjs;
2416         int                     numObjs;
2417         int                     i;
2418
2419         /* Search through all the dumpable objects for FK constraints */
2420         getDumpableObjects(&dobjs, &numObjs);
2421         for (i = 0; i < numObjs; i++)
2422         {
2423                 if (dobjs[i]->objType == DO_FK_CONSTRAINT)
2424                 {
2425                         ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
2426                         TableInfo  *ftable;
2427
2428                         /* Not interesting unless both tables are to be dumped */
2429                         if (cinfo->contable == NULL ||
2430                                 cinfo->contable->dataObj == NULL)
2431                                 continue;
2432                         ftable = findTableByOid(cinfo->confrelid);
2433                         if (ftable == NULL ||
2434                                 ftable->dataObj == NULL)
2435                                 continue;
2436
2437                         /*
2438                          * Okay, make referencing table's TABLE_DATA object depend on the
2439                          * referenced table's TABLE_DATA object.
2440                          */
2441                         addObjectDependency(&cinfo->contable->dataObj->dobj,
2442                                                                 ftable->dataObj->dobj.dumpId);
2443                 }
2444         }
2445         free(dobjs);
2446 }
2447
2448
2449 /*
2450  * guessConstraintInheritance:
2451  *      In pre-8.4 databases, we can't tell for certain which constraints
2452  *      are inherited.  We assume a CHECK constraint is inherited if its name
2453  *      matches the name of any constraint in the parent.  Originally this code
2454  *      tried to compare the expression texts, but that can fail for various
2455  *      reasons --- for example, if the parent and child tables are in different
2456  *      schemas, reverse-listing of function calls may produce different text
2457  *      (schema-qualified or not) depending on search path.
2458  *
2459  *      In 8.4 and up we can rely on the conislocal field to decide which
2460  *      constraints must be dumped; much safer.
2461  *
2462  *      This function assumes all conislocal flags were initialized to true.
2463  *      It clears the flag on anything that seems to be inherited.
2464  */
2465 static void
2466 guessConstraintInheritance(TableInfo *tblinfo, int numTables)
2467 {
2468         int                     i,
2469                                 j,
2470                                 k;
2471
2472         for (i = 0; i < numTables; i++)
2473         {
2474                 TableInfo  *tbinfo = &(tblinfo[i]);
2475                 int                     numParents;
2476                 TableInfo **parents;
2477                 TableInfo  *parent;
2478
2479                 /* Sequences and views never have parents */
2480                 if (tbinfo->relkind == RELKIND_SEQUENCE ||
2481                         tbinfo->relkind == RELKIND_VIEW)
2482                         continue;
2483
2484                 /* Don't bother computing anything for non-target tables, either */
2485                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
2486                         continue;
2487
2488                 numParents = tbinfo->numParents;
2489                 parents = tbinfo->parents;
2490
2491                 if (numParents == 0)
2492                         continue;                       /* nothing to see here, move along */
2493
2494                 /* scan for inherited CHECK constraints */
2495                 for (j = 0; j < tbinfo->ncheck; j++)
2496                 {
2497                         ConstraintInfo *constr;
2498
2499                         constr = &(tbinfo->checkexprs[j]);
2500
2501                         for (k = 0; k < numParents; k++)
2502                         {
2503                                 int                     l;
2504
2505                                 parent = parents[k];
2506                                 for (l = 0; l < parent->ncheck; l++)
2507                                 {
2508                                         ConstraintInfo *pconstr = &(parent->checkexprs[l]);
2509
2510                                         if (strcmp(pconstr->dobj.name, constr->dobj.name) == 0)
2511                                         {
2512                                                 constr->conislocal = false;
2513                                                 break;
2514                                         }
2515                                 }
2516                                 if (!constr->conislocal)
2517                                         break;
2518                         }
2519                 }
2520         }
2521 }
2522
2523
2524 /*
2525  * dumpDatabase:
2526  *      dump the database definition
2527  */
2528 static void
2529 dumpDatabase(Archive *fout)
2530 {
2531         DumpOptions *dopt = fout->dopt;
2532         PQExpBuffer dbQry = createPQExpBuffer();
2533         PQExpBuffer delQry = createPQExpBuffer();
2534         PQExpBuffer creaQry = createPQExpBuffer();
2535         PQExpBuffer labelq = createPQExpBuffer();
2536         PGconn     *conn = GetConnection(fout);
2537         PGresult   *res;
2538         int                     i_tableoid,
2539                                 i_oid,
2540                                 i_datname,
2541                                 i_dba,
2542                                 i_encoding,
2543                                 i_collate,
2544                                 i_ctype,
2545                                 i_frozenxid,
2546                                 i_minmxid,
2547                                 i_datacl,
2548                                 i_rdatacl,
2549                                 i_datistemplate,
2550                                 i_datconnlimit,
2551                                 i_tablespace;
2552         CatalogId       dbCatId;
2553         DumpId          dbDumpId;
2554         const char *datname,
2555                            *dba,
2556                            *encoding,
2557                            *collate,
2558                            *ctype,
2559                            *datacl,
2560                            *rdatacl,
2561                            *datistemplate,
2562                            *datconnlimit,
2563                            *tablespace;
2564         uint32          frozenxid,
2565                                 minmxid;
2566         char       *qdatname;
2567
2568         if (g_verbose)
2569                 write_msg(NULL, "saving database definition\n");
2570
2571         /* Fetch the database-level properties for this database */
2572         if (fout->remoteVersion >= 90600)
2573         {
2574                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2575                                                   "(%s datdba) AS dba, "
2576                                                   "pg_encoding_to_char(encoding) AS encoding, "
2577                                                   "datcollate, datctype, datfrozenxid, datminmxid, "
2578                                                   "(SELECT array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
2579                                                   "  SELECT unnest(coalesce(datacl,acldefault('d',datdba))) AS acl "
2580                                                   "  EXCEPT SELECT unnest(acldefault('d',datdba))) as datacls)"
2581                                                   " AS datacl, "
2582                                                   "(SELECT array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
2583                                                   "  SELECT unnest(acldefault('d',datdba)) AS acl "
2584                                                   "  EXCEPT SELECT unnest(coalesce(datacl,acldefault('d',datdba)))) as rdatacls)"
2585                                                   " AS rdatacl, "
2586                                                   "datistemplate, datconnlimit, "
2587                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2588                                                   "shobj_description(oid, 'pg_database') AS description "
2589
2590                                                   "FROM pg_database "
2591                                                   "WHERE datname = current_database()",
2592                                                   username_subquery);
2593         }
2594         else if (fout->remoteVersion >= 90300)
2595         {
2596                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2597                                                   "(%s datdba) AS dba, "
2598                                                   "pg_encoding_to_char(encoding) AS encoding, "
2599                                                   "datcollate, datctype, datfrozenxid, datminmxid, "
2600                                                   "datacl, '' as rdatacl, datistemplate, datconnlimit, "
2601                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2602                                                   "shobj_description(oid, 'pg_database') AS description "
2603
2604                                                   "FROM pg_database "
2605                                                   "WHERE datname = current_database()",
2606                                                   username_subquery);
2607         }
2608         else if (fout->remoteVersion >= 80400)
2609         {
2610                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2611                                                   "(%s datdba) AS dba, "
2612                                                   "pg_encoding_to_char(encoding) AS encoding, "
2613                                                   "datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
2614                                                   "datacl, '' as rdatacl, datistemplate, datconnlimit, "
2615                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2616                                                   "shobj_description(oid, 'pg_database') AS description "
2617
2618                                                   "FROM pg_database "
2619                                                   "WHERE datname = current_database()",
2620                                                   username_subquery);
2621         }
2622         else if (fout->remoteVersion >= 80200)
2623         {
2624                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2625                                                   "(%s datdba) AS dba, "
2626                                                   "pg_encoding_to_char(encoding) AS encoding, "
2627                                                   "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2628                                                   "datacl, '' as rdatacl, datistemplate, datconnlimit, "
2629                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2630                                                   "shobj_description(oid, 'pg_database') AS description "
2631
2632                                                   "FROM pg_database "
2633                                                   "WHERE datname = current_database()",
2634                                                   username_subquery);
2635         }
2636         else
2637         {
2638                 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, datname, "
2639                                                   "(%s datdba) AS dba, "
2640                                                   "pg_encoding_to_char(encoding) AS encoding, "
2641                                                   "NULL AS datcollate, NULL AS datctype, datfrozenxid, 0 AS datminmxid, "
2642                                                   "datacl, '' as rdatacl, datistemplate, "
2643                                                   "-1 as datconnlimit, "
2644                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
2645                                                   "FROM pg_database "
2646                                                   "WHERE datname = current_database()",
2647                                                   username_subquery);
2648         }
2649
2650         res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
2651
2652         i_tableoid = PQfnumber(res, "tableoid");
2653         i_oid = PQfnumber(res, "oid");
2654         i_datname = PQfnumber(res, "datname");
2655         i_dba = PQfnumber(res, "dba");
2656         i_encoding = PQfnumber(res, "encoding");
2657         i_collate = PQfnumber(res, "datcollate");
2658         i_ctype = PQfnumber(res, "datctype");
2659         i_frozenxid = PQfnumber(res, "datfrozenxid");
2660         i_minmxid = PQfnumber(res, "datminmxid");
2661         i_datacl = PQfnumber(res, "datacl");
2662         i_rdatacl = PQfnumber(res, "rdatacl");
2663         i_datistemplate = PQfnumber(res, "datistemplate");
2664         i_datconnlimit = PQfnumber(res, "datconnlimit");
2665         i_tablespace = PQfnumber(res, "tablespace");
2666
2667         dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
2668         dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2669         datname = PQgetvalue(res, 0, i_datname);
2670         dba = PQgetvalue(res, 0, i_dba);
2671         encoding = PQgetvalue(res, 0, i_encoding);
2672         collate = PQgetvalue(res, 0, i_collate);
2673         ctype = PQgetvalue(res, 0, i_ctype);
2674         frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
2675         minmxid = atooid(PQgetvalue(res, 0, i_minmxid));
2676         datacl = PQgetvalue(res, 0, i_datacl);
2677         rdatacl = PQgetvalue(res, 0, i_rdatacl);
2678         datistemplate = PQgetvalue(res, 0, i_datistemplate);
2679         datconnlimit = PQgetvalue(res, 0, i_datconnlimit);
2680         tablespace = PQgetvalue(res, 0, i_tablespace);
2681
2682         qdatname = pg_strdup(fmtId(datname));
2683
2684         /*
2685          * Prepare the CREATE DATABASE command.  We must specify encoding, locale,
2686          * and tablespace since those can't be altered later.  Other DB properties
2687          * are left to the DATABASE PROPERTIES entry, so that they can be applied
2688          * after reconnecting to the target DB.
2689          */
2690         appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
2691                                           qdatname);
2692         if (strlen(encoding) > 0)
2693         {
2694                 appendPQExpBufferStr(creaQry, " ENCODING = ");
2695                 appendStringLiteralAH(creaQry, encoding, fout);
2696         }
2697         if (strlen(collate) > 0)
2698         {
2699                 appendPQExpBufferStr(creaQry, " LC_COLLATE = ");
2700                 appendStringLiteralAH(creaQry, collate, fout);
2701         }
2702         if (strlen(ctype) > 0)
2703         {
2704                 appendPQExpBufferStr(creaQry, " LC_CTYPE = ");
2705                 appendStringLiteralAH(creaQry, ctype, fout);
2706         }
2707
2708         /*
2709          * Note: looking at dopt->outputNoTablespaces here is completely the wrong
2710          * thing; the decision whether to specify a tablespace should be left till
2711          * pg_restore, so that pg_restore --no-tablespaces applies.  Ideally we'd
2712          * label the DATABASE entry with the tablespace and let the normal
2713          * tablespace selection logic work ... but CREATE DATABASE doesn't pay
2714          * attention to default_tablespace, so that won't work.
2715          */
2716         if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0 &&
2717                 !dopt->outputNoTablespaces)
2718                 appendPQExpBuffer(creaQry, " TABLESPACE = %s",
2719                                                   fmtId(tablespace));
2720         appendPQExpBufferStr(creaQry, ";\n");
2721
2722         appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
2723                                           qdatname);
2724
2725         dbDumpId = createDumpId();
2726
2727         ArchiveEntry(fout,
2728                                  dbCatId,               /* catalog ID */
2729                                  dbDumpId,              /* dump ID */
2730                                  datname,               /* Name */
2731                                  NULL,                  /* Namespace */
2732                                  NULL,                  /* Tablespace */
2733                                  dba,                   /* Owner */
2734                                  false,                 /* with oids */
2735                                  "DATABASE",    /* Desc */
2736                                  SECTION_PRE_DATA,      /* Section */
2737                                  creaQry->data, /* Create */
2738                                  delQry->data,  /* Del */
2739                                  NULL,                  /* Copy */
2740                                  NULL,                  /* Deps */
2741                                  0,                             /* # Deps */
2742                                  NULL,                  /* Dumper */
2743                                  NULL);                 /* Dumper Arg */
2744
2745         /* Compute correct tag for archive entry */
2746         appendPQExpBuffer(labelq, "DATABASE %s", qdatname);
2747
2748         /* Dump DB comment if any */
2749         if (fout->remoteVersion >= 80200)
2750         {
2751                 /*
2752                  * 8.2 and up keep comments on shared objects in a shared table, so we
2753                  * cannot use the dumpComment() code used for other database objects.
2754                  * Be careful that the ArchiveEntry parameters match that function.
2755                  */
2756                 char       *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
2757
2758                 if (comment && *comment && !dopt->no_comments)
2759                 {
2760                         resetPQExpBuffer(dbQry);
2761
2762                         /*
2763                          * Generates warning when loaded into a differently-named
2764                          * database.
2765                          */
2766                         appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", qdatname);
2767                         appendStringLiteralAH(dbQry, comment, fout);
2768                         appendPQExpBufferStr(dbQry, ";\n");
2769
2770                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
2771                                                  labelq->data, NULL, NULL, dba,
2772                                                  false, "COMMENT", SECTION_NONE,
2773                                                  dbQry->data, "", NULL,
2774                                                  &(dbDumpId), 1,
2775                                                  NULL, NULL);
2776                 }
2777         }
2778         else
2779         {
2780                 dumpComment(fout, "DATABASE", qdatname, NULL, dba,
2781                                         dbCatId, 0, dbDumpId);
2782         }
2783
2784         /* Dump DB security label, if enabled */
2785         if (!dopt->no_security_labels && fout->remoteVersion >= 90200)
2786         {
2787                 PGresult   *shres;
2788                 PQExpBuffer seclabelQry;
2789
2790                 seclabelQry = createPQExpBuffer();
2791
2792                 buildShSecLabelQuery(conn, "pg_database", dbCatId.oid, seclabelQry);
2793                 shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
2794                 resetPQExpBuffer(seclabelQry);
2795                 emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
2796                 if (seclabelQry->len > 0)
2797                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
2798                                                  labelq->data, NULL, NULL, dba,
2799                                                  false, "SECURITY LABEL", SECTION_NONE,
2800                                                  seclabelQry->data, "", NULL,
2801                                                  &(dbDumpId), 1,
2802                                                  NULL, NULL);
2803                 destroyPQExpBuffer(seclabelQry);
2804                 PQclear(shres);
2805         }
2806
2807         /*
2808          * Dump ACL if any.  Note that we do not support initial privileges
2809          * (pg_init_privs) on databases.
2810          */
2811         dumpACL(fout, dbCatId, dbDumpId, "DATABASE",
2812                         qdatname, NULL, NULL,
2813                         dba, datacl, rdatacl, "", "");
2814
2815         /*
2816          * Now construct a DATABASE PROPERTIES archive entry to restore any
2817          * non-default database-level properties.  (The reason this must be
2818          * separate is that we cannot put any additional commands into the TOC
2819          * entry that has CREATE DATABASE.  pg_restore would execute such a group
2820          * in an implicit transaction block, and the backend won't allow CREATE
2821          * DATABASE in that context.)
2822          */
2823         resetPQExpBuffer(creaQry);
2824         resetPQExpBuffer(delQry);
2825
2826         if (strlen(datconnlimit) > 0 && strcmp(datconnlimit, "-1") != 0)
2827                 appendPQExpBuffer(creaQry, "ALTER DATABASE %s CONNECTION LIMIT = %s;\n",
2828                                                   qdatname, datconnlimit);
2829
2830         if (strcmp(datistemplate, "t") == 0)
2831         {
2832                 appendPQExpBuffer(creaQry, "ALTER DATABASE %s IS_TEMPLATE = true;\n",
2833                                                   qdatname);
2834
2835                 /*
2836                  * The backend won't accept DROP DATABASE on a template database.  We
2837                  * can deal with that by removing the template marking before the DROP
2838                  * gets issued.  We'd prefer to use ALTER DATABASE IF EXISTS here, but
2839                  * since no such command is currently supported, fake it with a direct
2840                  * UPDATE on pg_database.
2841                  */
2842                 appendPQExpBufferStr(delQry, "UPDATE pg_catalog.pg_database "
2843                                                          "SET datistemplate = false WHERE datname = ");
2844                 appendStringLiteralAH(delQry, datname, fout);
2845                 appendPQExpBufferStr(delQry, ";\n");
2846         }
2847
2848         /* Add database-specific SET options */
2849         dumpDatabaseConfig(fout, creaQry, datname, dbCatId.oid);
2850
2851         /*
2852          * We stick this binary-upgrade query into the DATABASE PROPERTIES archive
2853          * entry, too, for lack of a better place.
2854          */
2855         if (dopt->binary_upgrade)
2856         {
2857                 appendPQExpBufferStr(creaQry, "\n-- For binary upgrade, set datfrozenxid and datminmxid.\n");
2858                 appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
2859                                                   "SET datfrozenxid = '%u', datminmxid = '%u'\n"
2860                                                   "WHERE datname = ",
2861                                                   frozenxid, minmxid);
2862                 appendStringLiteralAH(creaQry, datname, fout);
2863                 appendPQExpBufferStr(creaQry, ";\n");
2864         }
2865
2866         if (creaQry->len > 0)
2867                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
2868                                          datname, NULL, NULL, dba,
2869                                          false, "DATABASE PROPERTIES", SECTION_PRE_DATA,
2870                                          creaQry->data, delQry->data, NULL,
2871                                          &(dbDumpId), 1,
2872                                          NULL, NULL);
2873
2874         /*
2875          * pg_largeobject and pg_largeobject_metadata come from the old system
2876          * intact, so set their relfrozenxids and relminmxids.
2877          */
2878         if (dopt->binary_upgrade)
2879         {
2880                 PGresult   *lo_res;
2881                 PQExpBuffer loFrozenQry = createPQExpBuffer();
2882                 PQExpBuffer loOutQry = createPQExpBuffer();
2883                 int                     i_relfrozenxid,
2884                                         i_relminmxid;
2885
2886                 /*
2887                  * pg_largeobject
2888                  */
2889                 if (fout->remoteVersion >= 90300)
2890                         appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid\n"
2891                                                           "FROM pg_catalog.pg_class\n"
2892                                                           "WHERE oid = %u;\n",
2893                                                           LargeObjectRelationId);
2894                 else
2895                         appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid\n"
2896                                                           "FROM pg_catalog.pg_class\n"
2897                                                           "WHERE oid = %u;\n",
2898                                                           LargeObjectRelationId);
2899
2900                 lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2901
2902                 i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2903                 i_relminmxid = PQfnumber(lo_res, "relminmxid");
2904
2905                 appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, set pg_largeobject relfrozenxid and relminmxid\n");
2906                 appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2907                                                   "SET relfrozenxid = '%u', relminmxid = '%u'\n"
2908                                                   "WHERE oid = %u;\n",
2909                                                   atooid(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2910                                                   atooid(PQgetvalue(lo_res, 0, i_relminmxid)),
2911                                                   LargeObjectRelationId);
2912                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
2913                                          "pg_largeobject", NULL, NULL, "",
2914                                          false, "pg_largeobject", SECTION_PRE_DATA,
2915                                          loOutQry->data, "", NULL,
2916                                          NULL, 0,
2917                                          NULL, NULL);
2918
2919                 PQclear(lo_res);
2920
2921                 /*
2922                  * pg_largeobject_metadata
2923                  */
2924                 if (fout->remoteVersion >= 90000)
2925                 {
2926                         resetPQExpBuffer(loFrozenQry);
2927                         resetPQExpBuffer(loOutQry);
2928
2929                         if (fout->remoteVersion >= 90300)
2930                                 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, relminmxid\n"
2931                                                                   "FROM pg_catalog.pg_class\n"
2932                                                                   "WHERE oid = %u;\n",
2933                                                                   LargeObjectMetadataRelationId);
2934                         else
2935                                 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid, 0 AS relminmxid\n"
2936                                                                   "FROM pg_catalog.pg_class\n"
2937                                                                   "WHERE oid = %u;\n",
2938                                                                   LargeObjectMetadataRelationId);
2939
2940                         lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2941
2942                         i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2943                         i_relminmxid = PQfnumber(lo_res, "relminmxid");
2944
2945                         appendPQExpBufferStr(loOutQry, "\n-- For binary upgrade, set pg_largeobject_metadata relfrozenxid and relminmxid\n");
2946                         appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2947                                                           "SET relfrozenxid = '%u', relminmxid = '%u'\n"
2948                                                           "WHERE oid = %u;\n",
2949                                                           atooid(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2950                                                           atooid(PQgetvalue(lo_res, 0, i_relminmxid)),
2951                                                           LargeObjectMetadataRelationId);
2952                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
2953                                                  "pg_largeobject_metadata", NULL, NULL, "",
2954                                                  false, "pg_largeobject_metadata", SECTION_PRE_DATA,
2955                                                  loOutQry->data, "", NULL,
2956                                                  NULL, 0,
2957                                                  NULL, NULL);
2958
2959                         PQclear(lo_res);
2960                 }
2961
2962                 destroyPQExpBuffer(loFrozenQry);
2963                 destroyPQExpBuffer(loOutQry);
2964         }
2965
2966         PQclear(res);
2967
2968         free(qdatname);
2969         destroyPQExpBuffer(dbQry);
2970         destroyPQExpBuffer(delQry);
2971         destroyPQExpBuffer(creaQry);
2972         destroyPQExpBuffer(labelq);
2973 }
2974
2975 /*
2976  * Collect any database-specific or role-and-database-specific SET options
2977  * for this database, and append them to outbuf.
2978  */
2979 static void
2980 dumpDatabaseConfig(Archive *AH, PQExpBuffer outbuf,
2981                                    const char *dbname, Oid dboid)
2982 {
2983         PGconn     *conn = GetConnection(AH);
2984         PQExpBuffer buf = createPQExpBuffer();
2985         PGresult   *res;
2986         int                     count = 1;
2987
2988         /*
2989          * First collect database-specific options.  Pre-8.4 server versions lack
2990          * unnest(), so we do this the hard way by querying once per subscript.
2991          */
2992         for (;;)
2993         {
2994                 if (AH->remoteVersion >= 90000)
2995                         printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting "
2996                                                           "WHERE setrole = 0 AND setdatabase = '%u'::oid",
2997                                                           count, dboid);
2998                 else
2999                         printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE oid = '%u'::oid", count, dboid);
3000
3001                 res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3002
3003                 if (PQntuples(res) == 1 &&
3004                         !PQgetisnull(res, 0, 0))
3005                 {
3006                         makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
3007                                                                    "DATABASE", dbname, NULL, NULL,
3008                                                                    outbuf);
3009                         PQclear(res);
3010                         count++;
3011                 }
3012                 else
3013                 {
3014                         PQclear(res);
3015                         break;
3016                 }
3017         }
3018
3019         /* Now look for role-and-database-specific options */
3020         if (AH->remoteVersion >= 90000)
3021         {
3022                 /* Here we can assume we have unnest() */
3023                 printfPQExpBuffer(buf, "SELECT rolname, unnest(setconfig) "
3024                                                   "FROM pg_db_role_setting s, pg_roles r "
3025                                                   "WHERE setrole = r.oid AND setdatabase = '%u'::oid",
3026                                                   dboid);
3027
3028                 res = ExecuteSqlQuery(AH, buf->data, PGRES_TUPLES_OK);
3029
3030                 if (PQntuples(res) > 0)
3031                 {
3032                         int                     i;
3033
3034                         for (i = 0; i < PQntuples(res); i++)
3035                                 makeAlterConfigCommand(conn, PQgetvalue(res, i, 1),
3036                                                                            "ROLE", PQgetvalue(res, i, 0),
3037                                                                            "DATABASE", dbname,
3038                                                                            outbuf);
3039                 }
3040
3041                 PQclear(res);
3042         }
3043
3044         destroyPQExpBuffer(buf);
3045 }
3046
3047 /*
3048  * dumpEncoding: put the correct encoding into the archive
3049  */
3050 static void
3051 dumpEncoding(Archive *AH)
3052 {
3053         const char *encname = pg_encoding_to_char(AH->encoding);
3054         PQExpBuffer qry = createPQExpBuffer();
3055
3056         if (g_verbose)
3057                 write_msg(NULL, "saving encoding = %s\n", encname);
3058
3059         appendPQExpBufferStr(qry, "SET client_encoding = ");
3060         appendStringLiteralAH(qry, encname, AH);
3061         appendPQExpBufferStr(qry, ";\n");
3062
3063         ArchiveEntry(AH, nilCatalogId, createDumpId(),
3064                                  "ENCODING", NULL, NULL, "",
3065                                  false, "ENCODING", SECTION_PRE_DATA,
3066                                  qry->data, "", NULL,
3067                                  NULL, 0,
3068                                  NULL, NULL);
3069
3070         destroyPQExpBuffer(qry);
3071 }
3072
3073
3074 /*
3075  * dumpStdStrings: put the correct escape string behavior into the archive
3076  */
3077 static void
3078 dumpStdStrings(Archive *AH)
3079 {
3080         const char *stdstrings = AH->std_strings ? "on" : "off";
3081         PQExpBuffer qry = createPQExpBuffer();
3082
3083         if (g_verbose)
3084                 write_msg(NULL, "saving standard_conforming_strings = %s\n",
3085                                   stdstrings);
3086
3087         appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
3088                                           stdstrings);
3089
3090         ArchiveEntry(AH, nilCatalogId, createDumpId(),
3091                                  "STDSTRINGS", NULL, NULL, "",
3092                                  false, "STDSTRINGS", SECTION_PRE_DATA,
3093                                  qry->data, "", NULL,
3094                                  NULL, 0,
3095                                  NULL, NULL);
3096
3097         destroyPQExpBuffer(qry);
3098 }
3099
3100 /*
3101  * dumpSearchPath: record the active search_path in the archive
3102  */
3103 static void
3104 dumpSearchPath(Archive *AH)
3105 {
3106         PQExpBuffer qry = createPQExpBuffer();
3107         PQExpBuffer path = createPQExpBuffer();
3108         PGresult   *res;
3109         char      **schemanames = NULL;
3110         int                     nschemanames = 0;
3111         int                     i;
3112
3113         /*
3114          * We use the result of current_schemas(), not the search_path GUC,
3115          * because that might contain wildcards such as "$user", which won't
3116          * necessarily have the same value during restore.  Also, this way avoids
3117          * listing schemas that may appear in search_path but not actually exist,
3118          * which seems like a prudent exclusion.
3119          */
3120         res = ExecuteSqlQueryForSingleRow(AH,
3121                                                                           "SELECT pg_catalog.current_schemas(false)");
3122
3123         if (!parsePGArray(PQgetvalue(res, 0, 0), &schemanames, &nschemanames))
3124                 exit_horribly(NULL, "could not parse result of current_schemas()\n");
3125
3126         /*
3127          * We use set_config(), not a simple "SET search_path" command, because
3128          * the latter has less-clean behavior if the search path is empty.  While
3129          * that's likely to get fixed at some point, it seems like a good idea to
3130          * be as backwards-compatible as possible in what we put into archives.
3131          */
3132         for (i = 0; i < nschemanames; i++)
3133         {
3134                 if (i > 0)
3135                         appendPQExpBufferStr(path, ", ");
3136                 appendPQExpBufferStr(path, fmtId(schemanames[i]));
3137         }
3138
3139         appendPQExpBufferStr(qry, "SELECT pg_catalog.set_config('search_path', ");
3140         appendStringLiteralAH(qry, path->data, AH);
3141         appendPQExpBufferStr(qry, ", false);\n");
3142
3143         if (g_verbose)
3144                 write_msg(NULL, "saving search_path = %s\n", path->data);
3145
3146         ArchiveEntry(AH, nilCatalogId, createDumpId(),
3147                                  "SEARCHPATH", NULL, NULL, "",
3148                                  false, "SEARCHPATH", SECTION_PRE_DATA,
3149                                  qry->data, "", NULL,
3150                                  NULL, 0,
3151                                  NULL, NULL);
3152
3153         /* Also save it in AH->searchpath, in case we're doing plain text dump */
3154         AH->searchpath = pg_strdup(qry->data);
3155
3156         if (schemanames)
3157                 free(schemanames);
3158         PQclear(res);
3159         destroyPQExpBuffer(qry);
3160         destroyPQExpBuffer(path);
3161 }
3162
3163
3164 /*
3165  * getBlobs:
3166  *      Collect schema-level data about large objects
3167  */
3168 static void
3169 getBlobs(Archive *fout)
3170 {
3171         DumpOptions *dopt = fout->dopt;
3172         PQExpBuffer blobQry = createPQExpBuffer();
3173         BlobInfo   *binfo;
3174         DumpableObject *bdata;
3175         PGresult   *res;
3176         int                     ntups;
3177         int                     i;
3178         int                     i_oid;
3179         int                     i_lomowner;
3180         int                     i_lomacl;
3181         int                     i_rlomacl;
3182         int                     i_initlomacl;
3183         int                     i_initrlomacl;
3184
3185         /* Verbose message */
3186         if (g_verbose)
3187                 write_msg(NULL, "reading large objects\n");
3188
3189         /* Fetch BLOB OIDs, and owner/ACL data if >= 9.0 */
3190         if (fout->remoteVersion >= 90600)
3191         {
3192                 PQExpBuffer acl_subquery = createPQExpBuffer();
3193                 PQExpBuffer racl_subquery = createPQExpBuffer();
3194                 PQExpBuffer init_acl_subquery = createPQExpBuffer();
3195                 PQExpBuffer init_racl_subquery = createPQExpBuffer();
3196
3197                 buildACLQueries(acl_subquery, racl_subquery, init_acl_subquery,
3198                                                 init_racl_subquery, "l.lomacl", "l.lomowner", "'L'",
3199                                                 dopt->binary_upgrade);
3200
3201                 appendPQExpBuffer(blobQry,
3202                                                   "SELECT l.oid, (%s l.lomowner) AS rolname, "
3203                                                   "%s AS lomacl, "
3204                                                   "%s AS rlomacl, "
3205                                                   "%s AS initlomacl, "
3206                                                   "%s AS initrlomacl "
3207                                                   "FROM pg_largeobject_metadata l "
3208                                                   "LEFT JOIN pg_init_privs pip ON "
3209                                                   "(l.oid = pip.objoid "
3210                                                   "AND pip.classoid = 'pg_largeobject'::regclass "
3211                                                   "AND pip.objsubid = 0) ",
3212                                                   username_subquery,
3213                                                   acl_subquery->data,
3214                                                   racl_subquery->data,
3215                                                   init_acl_subquery->data,
3216                                                   init_racl_subquery->data);
3217
3218                 destroyPQExpBuffer(acl_subquery);
3219                 destroyPQExpBuffer(racl_subquery);
3220                 destroyPQExpBuffer(init_acl_subquery);
3221                 destroyPQExpBuffer(init_racl_subquery);
3222         }
3223         else if (fout->remoteVersion >= 90000)
3224                 appendPQExpBuffer(blobQry,
3225                                                   "SELECT oid, (%s lomowner) AS rolname, lomacl, "
3226                                                   "NULL AS rlomacl, NULL AS initlomacl, "
3227                                                   "NULL AS initrlomacl "
3228                                                   " FROM pg_largeobject_metadata",
3229                                                   username_subquery);
3230         else
3231                 appendPQExpBufferStr(blobQry,
3232                                                          "SELECT DISTINCT loid AS oid, "
3233                                                          "NULL::name AS rolname, NULL::oid AS lomacl, "
3234                                                          "NULL::oid AS rlomacl, NULL::oid AS initlomacl, "
3235                                                          "NULL::oid AS initrlomacl "
3236                                                          " FROM pg_largeobject");
3237
3238         res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK);
3239
3240         i_oid = PQfnumber(res, "oid");
3241         i_lomowner = PQfnumber(res, "rolname");
3242         i_lomacl = PQfnumber(res, "lomacl");
3243         i_rlomacl = PQfnumber(res, "rlomacl");
3244         i_initlomacl = PQfnumber(res, "initlomacl");
3245         i_initrlomacl = PQfnumber(res, "initrlomacl");
3246
3247         ntups = PQntuples(res);
3248
3249         /*
3250          * Each large object has its own BLOB archive entry.
3251          */
3252         binfo = (BlobInfo *) pg_malloc(ntups * sizeof(BlobInfo));
3253
3254         for (i = 0; i < ntups; i++)
3255         {
3256                 binfo[i].dobj.objType = DO_BLOB;
3257                 binfo[i].dobj.catId.tableoid = LargeObjectRelationId;
3258                 binfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3259                 AssignDumpId(&binfo[i].dobj);
3260
3261                 binfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oid));
3262                 binfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_lomowner));
3263                 binfo[i].blobacl = pg_strdup(PQgetvalue(res, i, i_lomacl));
3264                 binfo[i].rblobacl = pg_strdup(PQgetvalue(res, i, i_rlomacl));
3265                 binfo[i].initblobacl = pg_strdup(PQgetvalue(res, i, i_initlomacl));
3266                 binfo[i].initrblobacl = pg_strdup(PQgetvalue(res, i, i_initrlomacl));
3267
3268                 if (PQgetisnull(res, i, i_lomacl) &&
3269                         PQgetisnull(res, i, i_rlomacl) &&
3270                         PQgetisnull(res, i, i_initlomacl) &&
3271                         PQgetisnull(res, i, i_initrlomacl))
3272                         binfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
3273
3274                 /*
3275                  * In binary-upgrade mode for blobs, we do *not* dump out the data or
3276                  * the ACLs, should any exist.  The data and ACL (if any) will be
3277                  * copied by pg_upgrade, which simply copies the pg_largeobject and
3278                  * pg_largeobject_metadata tables.
3279                  *
3280                  * We *do* dump out the definition of the blob because we need that to
3281                  * make the restoration of the comments, and anything else, work since
3282                  * pg_upgrade copies the files behind pg_largeobject and
3283                  * pg_largeobject_metadata after the dump is restored.
3284                  */
3285                 if (dopt->binary_upgrade)
3286                         binfo[i].dobj.dump &= ~(DUMP_COMPONENT_DATA | DUMP_COMPONENT_ACL);
3287         }
3288
3289         /*
3290          * If we have any large objects, a "BLOBS" archive entry is needed. This
3291          * is just a placeholder for sorting; it carries no data now.
3292          */
3293         if (ntups > 0)
3294         {
3295                 bdata = (DumpableObject *) pg_malloc(sizeof(DumpableObject));
3296                 bdata->objType = DO_BLOB_DATA;
3297                 bdata->catId = nilCatalogId;
3298                 AssignDumpId(bdata);
3299                 bdata->name = pg_strdup("BLOBS");
3300         }
3301
3302         PQclear(res);
3303         destroyPQExpBuffer(blobQry);
3304 }
3305
3306 /*
3307  * dumpBlob
3308  *
3309  * dump the definition (metadata) of the given large object
3310  */
3311 static void
3312 dumpBlob(Archive *fout, BlobInfo *binfo)
3313 {
3314         PQExpBuffer cquery = createPQExpBuffer();
3315         PQExpBuffer dquery = createPQExpBuffer();
3316
3317         appendPQExpBuffer(cquery,
3318                                           "SELECT pg_catalog.lo_create('%s');\n",
3319                                           binfo->dobj.name);
3320
3321         appendPQExpBuffer(dquery,
3322                                           "SELECT pg_catalog.lo_unlink('%s');\n",
3323                                           binfo->dobj.name);
3324
3325         if (binfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
3326                 ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
3327                                          binfo->dobj.name,
3328                                          NULL, NULL,
3329                                          binfo->rolname, false,
3330                                          "BLOB", SECTION_PRE_DATA,
3331                                          cquery->data, dquery->data, NULL,
3332                                          NULL, 0,
3333                                          NULL, NULL);
3334
3335         /* Dump comment if any */
3336         if (binfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3337                 dumpComment(fout, "LARGE OBJECT", binfo->dobj.name,
3338                                         NULL, binfo->rolname,
3339                                         binfo->dobj.catId, 0, binfo->dobj.dumpId);
3340
3341         /* Dump security label if any */
3342         if (binfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3343                 dumpSecLabel(fout, "LARGE OBJECT", binfo->dobj.name,
3344                                          NULL, binfo->rolname,
3345                                          binfo->dobj.catId, 0, binfo->dobj.dumpId);
3346
3347         /* Dump ACL if any */
3348         if (binfo->blobacl && (binfo->dobj.dump & DUMP_COMPONENT_ACL))
3349                 dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
3350                                 binfo->dobj.name, NULL,
3351                                 NULL, binfo->rolname, binfo->blobacl, binfo->rblobacl,
3352                                 binfo->initblobacl, binfo->initrblobacl);
3353
3354         destroyPQExpBuffer(cquery);
3355         destroyPQExpBuffer(dquery);
3356 }
3357
3358 /*
3359  * dumpBlobs:
3360  *      dump the data contents of all large objects
3361  */
3362 static int
3363 dumpBlobs(Archive *fout, void *arg)
3364 {
3365         const char *blobQry;
3366         const char *blobFetchQry;
3367         PGconn     *conn = GetConnection(fout);
3368         PGresult   *res;
3369         char            buf[LOBBUFSIZE];
3370         int                     ntups;
3371         int                     i;
3372         int                     cnt;
3373
3374         if (g_verbose)
3375                 write_msg(NULL, "saving large objects\n");
3376
3377         /*
3378          * Currently, we re-fetch all BLOB OIDs using a cursor.  Consider scanning
3379          * the already-in-memory dumpable objects instead...
3380          */
3381         if (fout->remoteVersion >= 90000)
3382                 blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_largeobject_metadata";
3383         else
3384                 blobQry = "DECLARE bloboid CURSOR FOR SELECT DISTINCT loid FROM pg_largeobject";
3385
3386         ExecuteSqlStatement(fout, blobQry);
3387
3388         /* Command to fetch from cursor */
3389         blobFetchQry = "FETCH 1000 IN bloboid";
3390
3391         do
3392         {
3393                 /* Do a fetch */
3394                 res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
3395
3396                 /* Process the tuples, if any */
3397                 ntups = PQntuples(res);
3398                 for (i = 0; i < ntups; i++)
3399                 {
3400                         Oid                     blobOid;
3401                         int                     loFd;
3402
3403                         blobOid = atooid(PQgetvalue(res, i, 0));
3404                         /* Open the BLOB */
3405                         loFd = lo_open(conn, blobOid, INV_READ);
3406                         if (loFd == -1)
3407                                 exit_horribly(NULL, "could not open large object %u: %s",
3408                                                           blobOid, PQerrorMessage(conn));
3409
3410                         StartBlob(fout, blobOid);
3411
3412                         /* Now read it in chunks, sending data to archive */
3413                         do
3414                         {
3415                                 cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
3416                                 if (cnt < 0)
3417                                         exit_horribly(NULL, "error reading large object %u: %s",
3418                                                                   blobOid, PQerrorMessage(conn));
3419
3420                                 WriteData(fout, buf, cnt);
3421                         } while (cnt > 0);
3422
3423                         lo_close(conn, loFd);
3424
3425                         EndBlob(fout, blobOid);
3426                 }
3427
3428                 PQclear(res);
3429         } while (ntups > 0);
3430
3431         return 1;
3432 }
3433
3434 /*
3435  * getPolicies
3436  *        get information about policies on a dumpable table.
3437  */
3438 void
3439 getPolicies(Archive *fout, TableInfo tblinfo[], int numTables)
3440 {
3441         PQExpBuffer query;
3442         PGresult   *res;
3443         PolicyInfo *polinfo;
3444         int                     i_oid;
3445         int                     i_tableoid;
3446         int                     i_polname;
3447         int                     i_polcmd;
3448         int                     i_polpermissive;
3449         int                     i_polroles;
3450         int                     i_polqual;
3451         int                     i_polwithcheck;
3452         int                     i,
3453                                 j,
3454                                 ntups;
3455
3456         if (fout->remoteVersion < 90500)
3457                 return;
3458
3459         query = createPQExpBuffer();
3460
3461         for (i = 0; i < numTables; i++)
3462         {
3463                 TableInfo  *tbinfo = &tblinfo[i];
3464
3465                 /* Ignore row security on tables not to be dumped */
3466                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_POLICY))
3467                         continue;
3468
3469                 if (g_verbose)
3470                         write_msg(NULL, "reading row security enabled for table \"%s.%s\"\n",
3471                                           tbinfo->dobj.namespace->dobj.name,
3472                                           tbinfo->dobj.name);
3473
3474                 /*
3475                  * Get row security enabled information for the table. We represent
3476                  * RLS enabled on a table by creating PolicyInfo object with an empty
3477                  * policy.
3478                  */
3479                 if (tbinfo->rowsec)
3480                 {
3481                         /*
3482                          * Note: use tableoid 0 so that this object won't be mistaken for
3483                          * something that pg_depend entries apply to.
3484                          */
3485                         polinfo = pg_malloc(sizeof(PolicyInfo));
3486                         polinfo->dobj.objType = DO_POLICY;
3487                         polinfo->dobj.catId.tableoid = 0;
3488                         polinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
3489                         AssignDumpId(&polinfo->dobj);
3490                         polinfo->dobj.namespace = tbinfo->dobj.namespace;
3491                         polinfo->dobj.name = pg_strdup(tbinfo->dobj.name);
3492                         polinfo->poltable = tbinfo;
3493                         polinfo->polname = NULL;
3494                         polinfo->polcmd = '\0';
3495                         polinfo->polpermissive = 0;
3496                         polinfo->polroles = NULL;
3497                         polinfo->polqual = NULL;
3498                         polinfo->polwithcheck = NULL;
3499                 }
3500
3501                 if (g_verbose)
3502                         write_msg(NULL, "reading policies for table \"%s.%s\"\n",
3503                                           tbinfo->dobj.namespace->dobj.name,
3504                                           tbinfo->dobj.name);
3505
3506                 resetPQExpBuffer(query);
3507
3508                 /* Get the policies for the table. */
3509                 if (fout->remoteVersion >= 100000)
3510                         appendPQExpBuffer(query,
3511                                                           "SELECT oid, tableoid, pol.polname, pol.polcmd, pol.polpermissive, "
3512                                                           "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3513                                                           "   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, "
3514                                                           "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3515                                                           "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3516                                                           "FROM pg_catalog.pg_policy pol "
3517                                                           "WHERE polrelid = '%u'",
3518                                                           tbinfo->dobj.catId.oid);
3519                 else
3520                         appendPQExpBuffer(query,
3521                                                           "SELECT oid, tableoid, pol.polname, pol.polcmd, 't' as polpermissive, "
3522                                                           "CASE WHEN pol.polroles = '{0}' THEN NULL ELSE "
3523                                                           "   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, "
3524                                                           "pg_catalog.pg_get_expr(pol.polqual, pol.polrelid) AS polqual, "
3525                                                           "pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid) AS polwithcheck "
3526                                                           "FROM pg_catalog.pg_policy pol "
3527                                                           "WHERE polrelid = '%u'",
3528                                                           tbinfo->dobj.catId.oid);
3529                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3530
3531                 ntups = PQntuples(res);
3532
3533                 if (ntups == 0)
3534                 {
3535                         /*
3536                          * No explicit policies to handle (only the default-deny policy,
3537                          * which is handled as part of the table definition).  Clean up
3538                          * and return.
3539                          */
3540                         PQclear(res);
3541                         continue;
3542                 }
3543
3544                 i_oid = PQfnumber(res, "oid");
3545                 i_tableoid = PQfnumber(res, "tableoid");
3546                 i_polname = PQfnumber(res, "polname");
3547                 i_polcmd = PQfnumber(res, "polcmd");
3548                 i_polpermissive = PQfnumber(res, "polpermissive");
3549                 i_polroles = PQfnumber(res, "polroles");
3550                 i_polqual = PQfnumber(res, "polqual");
3551                 i_polwithcheck = PQfnumber(res, "polwithcheck");
3552
3553                 polinfo = pg_malloc(ntups * sizeof(PolicyInfo));
3554
3555                 for (j = 0; j < ntups; j++)
3556                 {
3557                         polinfo[j].dobj.objType = DO_POLICY;
3558                         polinfo[j].dobj.catId.tableoid =
3559                                 atooid(PQgetvalue(res, j, i_tableoid));
3560                         polinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
3561                         AssignDumpId(&polinfo[j].dobj);
3562                         polinfo[j].dobj.namespace = tbinfo->dobj.namespace;
3563                         polinfo[j].poltable = tbinfo;
3564                         polinfo[j].polname = pg_strdup(PQgetvalue(res, j, i_polname));
3565                         polinfo[j].dobj.name = pg_strdup(polinfo[j].polname);
3566
3567                         polinfo[j].polcmd = *(PQgetvalue(res, j, i_polcmd));
3568                         polinfo[j].polpermissive = *(PQgetvalue(res, j, i_polpermissive)) == 't';
3569
3570                         if (PQgetisnull(res, j, i_polroles))
3571                                 polinfo[j].polroles = NULL;
3572                         else
3573                                 polinfo[j].polroles = pg_strdup(PQgetvalue(res, j, i_polroles));
3574
3575                         if (PQgetisnull(res, j, i_polqual))
3576                                 polinfo[j].polqual = NULL;
3577                         else
3578                                 polinfo[j].polqual = pg_strdup(PQgetvalue(res, j, i_polqual));
3579
3580                         if (PQgetisnull(res, j, i_polwithcheck))
3581                                 polinfo[j].polwithcheck = NULL;
3582                         else
3583                                 polinfo[j].polwithcheck
3584                                         = pg_strdup(PQgetvalue(res, j, i_polwithcheck));
3585                 }
3586                 PQclear(res);
3587         }
3588         destroyPQExpBuffer(query);
3589 }
3590
3591 /*
3592  * dumpPolicy
3593  *        dump the definition of the given policy
3594  */
3595 static void
3596 dumpPolicy(Archive *fout, PolicyInfo *polinfo)
3597 {
3598         DumpOptions *dopt = fout->dopt;
3599         TableInfo  *tbinfo = polinfo->poltable;
3600         PQExpBuffer query;
3601         PQExpBuffer delqry;
3602         const char *cmd;
3603         char       *tag;
3604
3605         if (dopt->dataOnly)
3606                 return;
3607
3608         /*
3609          * If polname is NULL, then this record is just indicating that ROW LEVEL
3610          * SECURITY is enabled for the table. Dump as ALTER TABLE <table> ENABLE
3611          * ROW LEVEL SECURITY.
3612          */
3613         if (polinfo->polname == NULL)
3614         {
3615                 query = createPQExpBuffer();
3616
3617                 appendPQExpBuffer(query, "ALTER TABLE %s ENABLE ROW LEVEL SECURITY;",
3618                                                   fmtQualifiedDumpable(polinfo));
3619
3620                 if (polinfo->dobj.dump & DUMP_COMPONENT_POLICY)
3621                         ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
3622                                                  polinfo->dobj.name,
3623                                                  polinfo->dobj.namespace->dobj.name,
3624                                                  NULL,
3625                                                  tbinfo->rolname, false,
3626                                                  "ROW SECURITY", SECTION_POST_DATA,
3627                                                  query->data, "", NULL,
3628                                                  NULL, 0,
3629                                                  NULL, NULL);
3630
3631                 destroyPQExpBuffer(query);
3632                 return;
3633         }
3634
3635         if (polinfo->polcmd == '*')
3636                 cmd = "";
3637         else if (polinfo->polcmd == 'r')
3638                 cmd = " FOR SELECT";
3639         else if (polinfo->polcmd == 'a')
3640                 cmd = " FOR INSERT";
3641         else if (polinfo->polcmd == 'w')
3642                 cmd = " FOR UPDATE";
3643         else if (polinfo->polcmd == 'd')
3644                 cmd = " FOR DELETE";
3645         else
3646         {
3647                 write_msg(NULL, "unexpected policy command type: %c\n",
3648                                   polinfo->polcmd);
3649                 exit_nicely(1);
3650         }
3651
3652         query = createPQExpBuffer();
3653         delqry = createPQExpBuffer();
3654
3655         appendPQExpBuffer(query, "CREATE POLICY %s", fmtId(polinfo->polname));
3656
3657         appendPQExpBuffer(query, " ON %s%s%s", fmtQualifiedDumpable(tbinfo),
3658                                           !polinfo->polpermissive ? " AS RESTRICTIVE" : "", cmd);
3659
3660         if (polinfo->polroles != NULL)
3661                 appendPQExpBuffer(query, " TO %s", polinfo->polroles);
3662
3663         if (polinfo->polqual != NULL)
3664                 appendPQExpBuffer(query, " USING (%s)", polinfo->polqual);
3665
3666         if (polinfo->polwithcheck != NULL)
3667                 appendPQExpBuffer(query, " WITH CHECK (%s)", polinfo->polwithcheck);
3668
3669         appendPQExpBuffer(query, ";\n");
3670
3671         appendPQExpBuffer(delqry, "DROP POLICY %s", fmtId(polinfo->polname));
3672         appendPQExpBuffer(delqry, " ON %s;\n", fmtQualifiedDumpable(tbinfo));
3673
3674         tag = psprintf("%s %s", tbinfo->dobj.name, polinfo->dobj.name);
3675
3676         if (polinfo->dobj.dump & DUMP_COMPONENT_POLICY)
3677                 ArchiveEntry(fout, polinfo->dobj.catId, polinfo->dobj.dumpId,
3678                                          tag,
3679                                          polinfo->dobj.namespace->dobj.name,
3680                                          NULL,
3681                                          tbinfo->rolname, false,
3682                                          "POLICY", SECTION_POST_DATA,
3683                                          query->data, delqry->data, NULL,
3684                                          NULL, 0,
3685                                          NULL, NULL);
3686
3687         free(tag);
3688         destroyPQExpBuffer(query);
3689         destroyPQExpBuffer(delqry);
3690 }
3691
3692 /*
3693  * getPublications
3694  *        get information about publications
3695  */
3696 void
3697 getPublications(Archive *fout)
3698 {
3699         DumpOptions *dopt = fout->dopt;
3700         PQExpBuffer query;
3701         PGresult   *res;
3702         PublicationInfo *pubinfo;
3703         int                     i_tableoid;
3704         int                     i_oid;
3705         int                     i_pubname;
3706         int                     i_rolname;
3707         int                     i_puballtables;
3708         int                     i_pubinsert;
3709         int                     i_pubupdate;
3710         int                     i_pubdelete;
3711         int                     i_pubtruncate;
3712         int                     i,
3713                                 ntups;
3714
3715         if (dopt->no_publications || fout->remoteVersion < 100000)
3716                 return;
3717
3718         query = createPQExpBuffer();
3719
3720         resetPQExpBuffer(query);
3721
3722         /* Get the publications. */
3723         if (fout->remoteVersion >= 110000)
3724                 appendPQExpBuffer(query,
3725                                                   "SELECT p.tableoid, p.oid, p.pubname, "
3726                                                   "(%s p.pubowner) AS rolname, "
3727                                                   "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate "
3728                                                   "FROM pg_publication p",
3729                                                   username_subquery);
3730         else
3731                 appendPQExpBuffer(query,
3732                                                   "SELECT p.tableoid, p.oid, p.pubname, "
3733                                                   "(%s p.pubowner) AS rolname, "
3734                                                   "p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, false AS pubtruncate "
3735                                                   "FROM pg_publication p",
3736                                                   username_subquery);
3737
3738         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3739
3740         ntups = PQntuples(res);
3741
3742         i_tableoid = PQfnumber(res, "tableoid");
3743         i_oid = PQfnumber(res, "oid");
3744         i_pubname = PQfnumber(res, "pubname");
3745         i_rolname = PQfnumber(res, "rolname");
3746         i_puballtables = PQfnumber(res, "puballtables");
3747         i_pubinsert = PQfnumber(res, "pubinsert");
3748         i_pubupdate = PQfnumber(res, "pubupdate");
3749         i_pubdelete = PQfnumber(res, "pubdelete");
3750         i_pubtruncate = PQfnumber(res, "pubtruncate");
3751
3752         pubinfo = pg_malloc(ntups * sizeof(PublicationInfo));
3753
3754         for (i = 0; i < ntups; i++)
3755         {
3756                 pubinfo[i].dobj.objType = DO_PUBLICATION;
3757                 pubinfo[i].dobj.catId.tableoid =
3758                         atooid(PQgetvalue(res, i, i_tableoid));
3759                 pubinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3760                 AssignDumpId(&pubinfo[i].dobj);
3761                 pubinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_pubname));
3762                 pubinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3763                 pubinfo[i].puballtables =
3764                         (strcmp(PQgetvalue(res, i, i_puballtables), "t") == 0);
3765                 pubinfo[i].pubinsert =
3766                         (strcmp(PQgetvalue(res, i, i_pubinsert), "t") == 0);
3767                 pubinfo[i].pubupdate =
3768                         (strcmp(PQgetvalue(res, i, i_pubupdate), "t") == 0);
3769                 pubinfo[i].pubdelete =
3770                         (strcmp(PQgetvalue(res, i, i_pubdelete), "t") == 0);
3771                 pubinfo[i].pubtruncate =
3772                         (strcmp(PQgetvalue(res, i, i_pubtruncate), "t") == 0);
3773
3774                 if (strlen(pubinfo[i].rolname) == 0)
3775                         write_msg(NULL, "WARNING: owner of publication \"%s\" appears to be invalid\n",
3776                                           pubinfo[i].dobj.name);
3777
3778                 /* Decide whether we want to dump it */
3779                 selectDumpableObject(&(pubinfo[i].dobj), fout);
3780         }
3781         PQclear(res);
3782
3783         destroyPQExpBuffer(query);
3784 }
3785
3786 /*
3787  * dumpPublication
3788  *        dump the definition of the given publication
3789  */
3790 static void
3791 dumpPublication(Archive *fout, PublicationInfo *pubinfo)
3792 {
3793         PQExpBuffer delq;
3794         PQExpBuffer query;
3795         char       *qpubname;
3796         bool            first = true;
3797
3798         if (!(pubinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3799                 return;
3800
3801         delq = createPQExpBuffer();
3802         query = createPQExpBuffer();
3803
3804         qpubname = pg_strdup(fmtId(pubinfo->dobj.name));
3805
3806         appendPQExpBuffer(delq, "DROP PUBLICATION %s;\n",
3807                                           qpubname);
3808
3809         appendPQExpBuffer(query, "CREATE PUBLICATION %s",
3810                                           qpubname);
3811
3812         if (pubinfo->puballtables)
3813                 appendPQExpBufferStr(query, " FOR ALL TABLES");
3814
3815         appendPQExpBufferStr(query, " WITH (publish = '");
3816         if (pubinfo->pubinsert)
3817         {
3818                 appendPQExpBufferStr(query, "insert");
3819                 first = false;
3820         }
3821
3822         if (pubinfo->pubupdate)
3823         {
3824                 if (!first)
3825                         appendPQExpBufferStr(query, ", ");
3826
3827                 appendPQExpBufferStr(query, "update");
3828                 first = false;
3829         }
3830
3831         if (pubinfo->pubdelete)
3832         {
3833                 if (!first)
3834                         appendPQExpBufferStr(query, ", ");
3835
3836                 appendPQExpBufferStr(query, "delete");
3837                 first = false;
3838         }
3839
3840         if (pubinfo->pubtruncate)
3841         {
3842                 if (!first)
3843                         appendPQExpBufferStr(query, ", ");
3844
3845                 appendPQExpBufferStr(query, "truncate");
3846                 first = false;
3847         }
3848
3849         appendPQExpBufferStr(query, "');\n");
3850
3851         ArchiveEntry(fout, pubinfo->dobj.catId, pubinfo->dobj.dumpId,
3852                                  pubinfo->dobj.name,
3853                                  NULL,
3854                                  NULL,
3855                                  pubinfo->rolname, false,
3856                                  "PUBLICATION", SECTION_POST_DATA,
3857                                  query->data, delq->data, NULL,
3858                                  NULL, 0,
3859                                  NULL, NULL);
3860
3861         if (pubinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
3862                 dumpComment(fout, "PUBLICATION", qpubname,
3863                                         NULL, pubinfo->rolname,
3864                                         pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
3865
3866         if (pubinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
3867                 dumpSecLabel(fout, "PUBLICATION", qpubname,
3868                                          NULL, pubinfo->rolname,
3869                                          pubinfo->dobj.catId, 0, pubinfo->dobj.dumpId);
3870
3871         destroyPQExpBuffer(delq);
3872         destroyPQExpBuffer(query);
3873         free(qpubname);
3874 }
3875
3876 /*
3877  * getPublicationTables
3878  *        get information about publication membership for dumpable tables.
3879  */
3880 void
3881 getPublicationTables(Archive *fout, TableInfo tblinfo[], int numTables)
3882 {
3883         PQExpBuffer query;
3884         PGresult   *res;
3885         PublicationRelInfo *pubrinfo;
3886         int                     i_tableoid;
3887         int                     i_oid;
3888         int                     i_pubname;
3889         int                     i,
3890                                 j,
3891                                 ntups;
3892
3893         if (fout->remoteVersion < 100000)
3894                 return;
3895
3896         query = createPQExpBuffer();
3897
3898         for (i = 0; i < numTables; i++)
3899         {
3900                 TableInfo  *tbinfo = &tblinfo[i];
3901
3902                 /* Only plain tables can be aded to publications. */
3903                 if (tbinfo->relkind != RELKIND_RELATION)
3904                         continue;
3905
3906                 /*
3907                  * Ignore publication membership of tables whose definitions are not
3908                  * to be dumped.
3909                  */
3910                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3911                         continue;
3912
3913                 if (g_verbose)
3914                         write_msg(NULL, "reading publication membership for table \"%s.%s\"\n",
3915                                           tbinfo->dobj.namespace->dobj.name,
3916                                           tbinfo->dobj.name);
3917
3918                 resetPQExpBuffer(query);
3919
3920                 /* Get the publication membership for the table. */
3921                 appendPQExpBuffer(query,
3922                                                   "SELECT pr.tableoid, pr.oid, p.pubname "
3923                                                   "FROM pg_publication_rel pr, pg_publication p "
3924                                                   "WHERE pr.prrelid = '%u'"
3925                                                   "  AND p.oid = pr.prpubid",
3926                                                   tbinfo->dobj.catId.oid);
3927                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3928
3929                 ntups = PQntuples(res);
3930
3931                 if (ntups == 0)
3932                 {
3933                         /*
3934                          * Table is not member of any publications. Clean up and return.
3935                          */
3936                         PQclear(res);
3937                         continue;
3938                 }
3939
3940                 i_tableoid = PQfnumber(res, "tableoid");
3941                 i_oid = PQfnumber(res, "oid");
3942                 i_pubname = PQfnumber(res, "pubname");
3943
3944                 pubrinfo = pg_malloc(ntups * sizeof(PublicationRelInfo));
3945
3946                 for (j = 0; j < ntups; j++)
3947                 {
3948                         pubrinfo[j].dobj.objType = DO_PUBLICATION_REL;
3949                         pubrinfo[j].dobj.catId.tableoid =
3950                                 atooid(PQgetvalue(res, j, i_tableoid));
3951                         pubrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
3952                         AssignDumpId(&pubrinfo[j].dobj);
3953                         pubrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
3954                         pubrinfo[j].dobj.name = tbinfo->dobj.name;
3955                         pubrinfo[j].pubname = pg_strdup(PQgetvalue(res, j, i_pubname));
3956                         pubrinfo[j].pubtable = tbinfo;
3957
3958                         /* Decide whether we want to dump it */
3959                         selectDumpablePublicationTable(&(pubrinfo[j].dobj), fout);
3960                 }
3961                 PQclear(res);
3962         }
3963         destroyPQExpBuffer(query);
3964 }
3965
3966 /*
3967  * dumpPublicationTable
3968  *        dump the definition of the given publication table mapping
3969  */
3970 static void
3971 dumpPublicationTable(Archive *fout, PublicationRelInfo *pubrinfo)
3972 {
3973         TableInfo  *tbinfo = pubrinfo->pubtable;
3974         PQExpBuffer query;
3975         char       *tag;
3976
3977         if (!(pubrinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
3978                 return;
3979
3980         tag = psprintf("%s %s", pubrinfo->pubname, tbinfo->dobj.name);
3981
3982         query = createPQExpBuffer();
3983
3984         appendPQExpBuffer(query, "ALTER PUBLICATION %s ADD TABLE ONLY",
3985                                           fmtId(pubrinfo->pubname));
3986         appendPQExpBuffer(query, " %s;\n",
3987                                           fmtQualifiedDumpable(tbinfo));
3988
3989         /*
3990          * There is no point in creating drop query as drop query as the drop is
3991          * done by table drop.
3992          */
3993         ArchiveEntry(fout, pubrinfo->dobj.catId, pubrinfo->dobj.dumpId,
3994                                  tag,
3995                                  tbinfo->dobj.namespace->dobj.name,
3996                                  NULL,
3997                                  "", false,
3998                                  "PUBLICATION TABLE", SECTION_POST_DATA,
3999                                  query->data, "", NULL,
4000                                  NULL, 0,
4001                                  NULL, NULL);
4002
4003         free(tag);
4004         destroyPQExpBuffer(query);
4005 }
4006
4007 /*
4008  * Is the currently connected user a superuser?
4009  */
4010 static bool
4011 is_superuser(Archive *fout)
4012 {
4013         ArchiveHandle *AH = (ArchiveHandle *) fout;
4014         const char *val;
4015
4016         val = PQparameterStatus(AH->connection, "is_superuser");
4017
4018         if (val && strcmp(val, "on") == 0)
4019                 return true;
4020
4021         return false;
4022 }
4023
4024 /*
4025  * getSubscriptions
4026  *        get information about subscriptions
4027  */
4028 void
4029 getSubscriptions(Archive *fout)
4030 {
4031         DumpOptions *dopt = fout->dopt;
4032         PQExpBuffer query;
4033         PGresult   *res;
4034         SubscriptionInfo *subinfo;
4035         int                     i_tableoid;
4036         int                     i_oid;
4037         int                     i_subname;
4038         int                     i_rolname;
4039         int                     i_subconninfo;
4040         int                     i_subslotname;
4041         int                     i_subsynccommit;
4042         int                     i_subpublications;
4043         int                     i,
4044                                 ntups;
4045
4046         if (dopt->no_subscriptions || fout->remoteVersion < 100000)
4047                 return;
4048
4049         if (!is_superuser(fout))
4050         {
4051                 int                     n;
4052
4053                 res = ExecuteSqlQuery(fout,
4054                                                           "SELECT count(*) FROM pg_subscription "
4055                                                           "WHERE subdbid = (SELECT oid FROM pg_database"
4056                                                           "                 WHERE datname = current_database())",
4057                                                           PGRES_TUPLES_OK);
4058                 n = atoi(PQgetvalue(res, 0, 0));
4059                 if (n > 0)
4060                         write_msg(NULL, "WARNING: subscriptions not dumped because current user is not a superuser\n");
4061                 PQclear(res);
4062                 return;
4063         }
4064
4065         query = createPQExpBuffer();
4066
4067         resetPQExpBuffer(query);
4068
4069         /* Get the subscriptions in current database. */
4070         appendPQExpBuffer(query,
4071                                           "SELECT s.tableoid, s.oid, s.subname,"
4072                                           "(%s s.subowner) AS rolname, "
4073                                           " s.subconninfo, s.subslotname, s.subsynccommit, "
4074                                           " s.subpublications "
4075                                           "FROM pg_subscription s "
4076                                           "WHERE s.subdbid = (SELECT oid FROM pg_database"
4077                                           "                   WHERE datname = current_database())",
4078                                           username_subquery);
4079         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4080
4081         ntups = PQntuples(res);
4082
4083         i_tableoid = PQfnumber(res, "tableoid");
4084         i_oid = PQfnumber(res, "oid");
4085         i_subname = PQfnumber(res, "subname");
4086         i_rolname = PQfnumber(res, "rolname");
4087         i_subconninfo = PQfnumber(res, "subconninfo");
4088         i_subslotname = PQfnumber(res, "subslotname");
4089         i_subsynccommit = PQfnumber(res, "subsynccommit");
4090         i_subpublications = PQfnumber(res, "subpublications");
4091
4092         subinfo = pg_malloc(ntups * sizeof(SubscriptionInfo));
4093
4094         for (i = 0; i < ntups; i++)
4095         {
4096                 subinfo[i].dobj.objType = DO_SUBSCRIPTION;
4097                 subinfo[i].dobj.catId.tableoid =
4098                         atooid(PQgetvalue(res, i, i_tableoid));
4099                 subinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4100                 AssignDumpId(&subinfo[i].dobj);
4101                 subinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_subname));
4102                 subinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4103                 subinfo[i].subconninfo = pg_strdup(PQgetvalue(res, i, i_subconninfo));
4104                 if (PQgetisnull(res, i, i_subslotname))
4105                         subinfo[i].subslotname = NULL;
4106                 else
4107                         subinfo[i].subslotname = pg_strdup(PQgetvalue(res, i, i_subslotname));
4108                 subinfo[i].subsynccommit =
4109                         pg_strdup(PQgetvalue(res, i, i_subsynccommit));
4110                 subinfo[i].subpublications =
4111                         pg_strdup(PQgetvalue(res, i, i_subpublications));
4112
4113                 if (strlen(subinfo[i].rolname) == 0)
4114                         write_msg(NULL, "WARNING: owner of subscription \"%s\" appears to be invalid\n",
4115                                           subinfo[i].dobj.name);
4116
4117                 /* Decide whether we want to dump it */
4118                 selectDumpableObject(&(subinfo[i].dobj), fout);
4119         }
4120         PQclear(res);
4121
4122         destroyPQExpBuffer(query);
4123 }
4124
4125 /*
4126  * dumpSubscription
4127  *        dump the definition of the given subscription
4128  */
4129 static void
4130 dumpSubscription(Archive *fout, SubscriptionInfo *subinfo)
4131 {
4132         PQExpBuffer delq;
4133         PQExpBuffer query;
4134         PQExpBuffer publications;
4135         char       *qsubname;
4136         char      **pubnames = NULL;
4137         int                     npubnames = 0;
4138         int                     i;
4139
4140         if (!(subinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
4141                 return;
4142
4143         delq = createPQExpBuffer();
4144         query = createPQExpBuffer();
4145
4146         qsubname = pg_strdup(fmtId(subinfo->dobj.name));
4147
4148         appendPQExpBuffer(delq, "DROP SUBSCRIPTION %s;\n",
4149                                           qsubname);
4150
4151         appendPQExpBuffer(query, "CREATE SUBSCRIPTION %s CONNECTION ",
4152                                           qsubname);
4153         appendStringLiteralAH(query, subinfo->subconninfo, fout);
4154
4155         /* Build list of quoted publications and append them to query. */
4156         if (!parsePGArray(subinfo->subpublications, &pubnames, &npubnames))
4157         {
4158                 write_msg(NULL,
4159                                   "WARNING: could not parse subpublications array\n");
4160                 if (pubnames)
4161                         free(pubnames);
4162                 pubnames = NULL;
4163                 npubnames = 0;
4164         }
4165
4166         publications = createPQExpBuffer();
4167         for (i = 0; i < npubnames; i++)
4168         {
4169                 if (i > 0)
4170                         appendPQExpBufferStr(publications, ", ");
4171
4172                 appendPQExpBufferStr(publications, fmtId(pubnames[i]));
4173         }
4174
4175         appendPQExpBuffer(query, " PUBLICATION %s WITH (connect = false, slot_name = ", publications->data);
4176         if (subinfo->subslotname)
4177                 appendStringLiteralAH(query, subinfo->subslotname, fout);
4178         else
4179                 appendPQExpBufferStr(query, "NONE");
4180
4181         if (strcmp(subinfo->subsynccommit, "off") != 0)
4182                 appendPQExpBuffer(query, ", synchronous_commit = %s", fmtId(subinfo->subsynccommit));
4183
4184         appendPQExpBufferStr(query, ");\n");
4185
4186         ArchiveEntry(fout, subinfo->dobj.catId, subinfo->dobj.dumpId,
4187                                  subinfo->dobj.name,
4188                                  NULL,
4189                                  NULL,
4190                                  subinfo->rolname, false,
4191                                  "SUBSCRIPTION", SECTION_POST_DATA,
4192                                  query->data, delq->data, NULL,
4193                                  NULL, 0,
4194                                  NULL, NULL);
4195
4196         if (subinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
4197                 dumpComment(fout, "SUBSCRIPTION", qsubname,
4198                                         NULL, subinfo->rolname,
4199                                         subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
4200
4201         if (subinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
4202                 dumpSecLabel(fout, "SUBSCRIPTION", qsubname,
4203                                          NULL, subinfo->rolname,
4204                                          subinfo->dobj.catId, 0, subinfo->dobj.dumpId);
4205
4206         destroyPQExpBuffer(publications);
4207         if (pubnames)
4208                 free(pubnames);
4209
4210         destroyPQExpBuffer(delq);
4211         destroyPQExpBuffer(query);
4212         free(qsubname);
4213 }
4214
4215 static void
4216 binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
4217                                                                                  PQExpBuffer upgrade_buffer,
4218                                                                                  Oid pg_type_oid,
4219                                                                                  bool force_array_type)
4220 {
4221         PQExpBuffer upgrade_query = createPQExpBuffer();
4222         PGresult   *res;
4223         Oid                     pg_type_array_oid;
4224
4225         appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
4226         appendPQExpBuffer(upgrade_buffer,
4227                                           "SELECT pg_catalog.binary_upgrade_set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4228                                           pg_type_oid);
4229
4230         /* we only support old >= 8.3 for binary upgrades */
4231         appendPQExpBuffer(upgrade_query,
4232                                           "SELECT typarray "
4233                                           "FROM pg_catalog.pg_type "
4234                                           "WHERE oid = '%u'::pg_catalog.oid;",
4235                                           pg_type_oid);
4236
4237         res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4238
4239         pg_type_array_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typarray")));
4240
4241         PQclear(res);
4242
4243         if (!OidIsValid(pg_type_array_oid) && force_array_type)
4244         {
4245                 /*
4246                  * If the old version didn't assign an array type, but the new version
4247                  * does, we must select an unused type OID to assign.  This currently
4248                  * only happens for domains, when upgrading pre-v11 to v11 and up.
4249                  *
4250                  * Note: local state here is kind of ugly, but we must have some,
4251                  * since we mustn't choose the same unused OID more than once.
4252                  */
4253                 static Oid      next_possible_free_oid = FirstNormalObjectId;
4254                 bool            is_dup;
4255
4256                 do
4257                 {
4258                         ++next_possible_free_oid;
4259                         printfPQExpBuffer(upgrade_query,
4260                                                           "SELECT EXISTS(SELECT 1 "
4261                                                           "FROM pg_catalog.pg_type "
4262                                                           "WHERE oid = '%u'::pg_catalog.oid);",
4263                                                           next_possible_free_oid);
4264                         res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4265                         is_dup = (PQgetvalue(res, 0, 0)[0] == 't');
4266                         PQclear(res);
4267                 } while (is_dup);
4268
4269                 pg_type_array_oid = next_possible_free_oid;
4270         }
4271
4272         if (OidIsValid(pg_type_array_oid))
4273         {
4274                 appendPQExpBufferStr(upgrade_buffer,
4275                                                          "\n-- For binary upgrade, must preserve pg_type array oid\n");
4276                 appendPQExpBuffer(upgrade_buffer,
4277                                                   "SELECT pg_catalog.binary_upgrade_set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4278                                                   pg_type_array_oid);
4279         }
4280
4281         destroyPQExpBuffer(upgrade_query);
4282 }
4283
4284 static bool
4285 binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
4286                                                                                 PQExpBuffer upgrade_buffer,
4287                                                                                 Oid pg_rel_oid)
4288 {
4289         PQExpBuffer upgrade_query = createPQExpBuffer();
4290         PGresult   *upgrade_res;
4291         Oid                     pg_type_oid;
4292         bool            toast_set = false;
4293
4294         /* we only support old >= 8.3 for binary upgrades */
4295         appendPQExpBuffer(upgrade_query,
4296                                           "SELECT c.reltype AS crel, t.reltype AS trel "
4297                                           "FROM pg_catalog.pg_class c "
4298                                           "LEFT JOIN pg_catalog.pg_class t ON "
4299                                           "  (c.reltoastrelid = t.oid) "
4300                                           "WHERE c.oid = '%u'::pg_catalog.oid;",
4301                                           pg_rel_oid);
4302
4303         upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4304
4305         pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
4306
4307         binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
4308                                                                                          pg_type_oid, false);
4309
4310         if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
4311         {
4312                 /* Toast tables do not have pg_type array rows */
4313                 Oid                     pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
4314                                                                                                                   PQfnumber(upgrade_res, "trel")));
4315
4316                 appendPQExpBufferStr(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
4317                 appendPQExpBuffer(upgrade_buffer,
4318                                                   "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
4319                                                   pg_type_toast_oid);
4320
4321                 toast_set = true;
4322         }
4323
4324         PQclear(upgrade_res);
4325         destroyPQExpBuffer(upgrade_query);
4326
4327         return toast_set;
4328 }
4329
4330 static void
4331 binary_upgrade_set_pg_class_oids(Archive *fout,
4332                                                                  PQExpBuffer upgrade_buffer, Oid pg_class_oid,
4333                                                                  bool is_index)
4334 {
4335         PQExpBuffer upgrade_query = createPQExpBuffer();
4336         PGresult   *upgrade_res;
4337         Oid                     pg_class_reltoastrelid;
4338         Oid                     pg_index_indexrelid;
4339
4340         appendPQExpBuffer(upgrade_query,
4341                                           "SELECT c.reltoastrelid, i.indexrelid "
4342                                           "FROM pg_catalog.pg_class c LEFT JOIN "
4343                                           "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
4344                                           "WHERE c.oid = '%u'::pg_catalog.oid;",
4345                                           pg_class_oid);
4346
4347         upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
4348
4349         pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
4350         pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
4351
4352         appendPQExpBufferStr(upgrade_buffer,
4353                                                  "\n-- For binary upgrade, must preserve pg_class oids\n");
4354
4355         if (!is_index)
4356         {
4357                 appendPQExpBuffer(upgrade_buffer,
4358                                                   "SELECT pg_catalog.binary_upgrade_set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
4359                                                   pg_class_oid);
4360                 /* only tables have toast tables, not indexes */
4361                 if (OidIsValid(pg_class_reltoastrelid))
4362                 {
4363                         /*
4364                          * One complexity is that the table definition might not require
4365                          * the creation of a TOAST table, and the TOAST table might have
4366                          * been created long after table creation, when the table was
4367                          * loaded with wide data.  By setting the TOAST oid we force
4368                          * creation of the TOAST heap and TOAST index by the backend so we
4369                          * can cleanly copy the files during binary upgrade.
4370                          */
4371
4372                         appendPQExpBuffer(upgrade_buffer,
4373                                                           "SELECT pg_catalog.binary_upgrade_set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
4374                                                           pg_class_reltoastrelid);
4375
4376                         /* every toast table has an index */
4377                         appendPQExpBuffer(upgrade_buffer,
4378                                                           "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4379                                                           pg_index_indexrelid);
4380                 }
4381         }
4382         else
4383                 appendPQExpBuffer(upgrade_buffer,
4384                                                   "SELECT pg_catalog.binary_upgrade_set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
4385                                                   pg_class_oid);
4386
4387         appendPQExpBufferChar(upgrade_buffer, '\n');
4388
4389         PQclear(upgrade_res);
4390         destroyPQExpBuffer(upgrade_query);
4391 }
4392
4393 /*
4394  * If the DumpableObject is a member of an extension, add a suitable
4395  * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
4396  *
4397  * For somewhat historical reasons, objname should already be quoted,
4398  * but not objnamespace (if any).
4399  */
4400 static void
4401 binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
4402                                                                 DumpableObject *dobj,
4403                                                                 const char *objtype,
4404                                                                 const char *objname,
4405                                                                 const char *objnamespace)
4406 {
4407         DumpableObject *extobj = NULL;
4408         int                     i;
4409
4410         if (!dobj->ext_member)
4411                 return;
4412
4413         /*
4414          * Find the parent extension.  We could avoid this search if we wanted to
4415          * add a link field to DumpableObject, but the space costs of that would
4416          * be considerable.  We assume that member objects could only have a
4417          * direct dependency on their own extension, not any others.
4418          */
4419         for (i = 0; i < dobj->nDeps; i++)
4420         {
4421                 extobj = findObjectByDumpId(dobj->dependencies[i]);
4422                 if (extobj && extobj->objType == DO_EXTENSION)
4423                         break;
4424                 extobj = NULL;
4425         }
4426         if (extobj == NULL)
4427                 exit_horribly(NULL, "could not find parent extension for %s %s\n",
4428                                           objtype, objname);
4429
4430         appendPQExpBufferStr(upgrade_buffer,
4431                                                  "\n-- For binary upgrade, handle extension membership the hard way\n");
4432         appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s ",
4433                                           fmtId(extobj->name),
4434                                           objtype);
4435         if (objnamespace && *objnamespace)
4436                 appendPQExpBuffer(upgrade_buffer, "%s.", fmtId(objnamespace));
4437         appendPQExpBuffer(upgrade_buffer, "%s;\n", objname);
4438 }
4439
4440 /*
4441  * getNamespaces:
4442  *        read all namespaces in the system catalogs and return them in the
4443  * NamespaceInfo* structure
4444  *
4445  *      numNamespaces is set to the number of namespaces read in
4446  */
4447 NamespaceInfo *
4448 getNamespaces(Archive *fout, int *numNamespaces)
4449 {
4450         DumpOptions *dopt = fout->dopt;
4451         PGresult   *res;
4452         int                     ntups;
4453         int                     i;
4454         PQExpBuffer query;
4455         NamespaceInfo *nsinfo;
4456         int                     i_tableoid;
4457         int                     i_oid;
4458         int                     i_nspname;
4459         int                     i_rolname;
4460         int                     i_nspacl;
4461         int                     i_rnspacl;
4462         int                     i_initnspacl;
4463         int                     i_initrnspacl;
4464
4465         query = createPQExpBuffer();
4466
4467         /*
4468          * we fetch all namespaces including system ones, so that every object we
4469          * read in can be linked to a containing namespace.
4470          */
4471         if (fout->remoteVersion >= 90600)
4472         {
4473                 PQExpBuffer acl_subquery = createPQExpBuffer();
4474                 PQExpBuffer racl_subquery = createPQExpBuffer();
4475                 PQExpBuffer init_acl_subquery = createPQExpBuffer();
4476                 PQExpBuffer init_racl_subquery = createPQExpBuffer();
4477
4478                 buildACLQueries(acl_subquery, racl_subquery, init_acl_subquery,
4479                                                 init_racl_subquery, "n.nspacl", "n.nspowner", "'n'",
4480                                                 dopt->binary_upgrade);
4481
4482                 appendPQExpBuffer(query, "SELECT n.tableoid, n.oid, n.nspname, "
4483                                                   "(%s nspowner) AS rolname, "
4484                                                   "%s as nspacl, "
4485                                                   "%s as rnspacl, "
4486                                                   "%s as initnspacl, "
4487                                                   "%s as initrnspacl "
4488                                                   "FROM pg_namespace n "
4489                                                   "LEFT JOIN pg_init_privs pip "
4490                                                   "ON (n.oid = pip.objoid "
4491                                                   "AND pip.classoid = 'pg_namespace'::regclass "
4492                                                   "AND pip.objsubid = 0",
4493                                                   username_subquery,
4494                                                   acl_subquery->data,
4495                                                   racl_subquery->data,
4496                                                   init_acl_subquery->data,
4497                                                   init_racl_subquery->data);
4498
4499                 appendPQExpBuffer(query, ") ");
4500
4501                 destroyPQExpBuffer(acl_subquery);
4502                 destroyPQExpBuffer(racl_subquery);
4503                 destroyPQExpBuffer(init_acl_subquery);
4504                 destroyPQExpBuffer(init_racl_subquery);
4505         }
4506         else
4507                 appendPQExpBuffer(query, "SELECT tableoid, oid, nspname, "
4508                                                   "(%s nspowner) AS rolname, "
4509                                                   "nspacl, NULL as rnspacl, "
4510                                                   "NULL AS initnspacl, NULL as initrnspacl "
4511                                                   "FROM pg_namespace",
4512                                                   username_subquery);
4513
4514         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4515
4516         ntups = PQntuples(res);
4517
4518         nsinfo = (NamespaceInfo *) pg_malloc(ntups * sizeof(NamespaceInfo));
4519
4520         i_tableoid = PQfnumber(res, "tableoid");
4521         i_oid = PQfnumber(res, "oid");
4522         i_nspname = PQfnumber(res, "nspname");
4523         i_rolname = PQfnumber(res, "rolname");
4524         i_nspacl = PQfnumber(res, "nspacl");
4525         i_rnspacl = PQfnumber(res, "rnspacl");
4526         i_initnspacl = PQfnumber(res, "initnspacl");
4527         i_initrnspacl = PQfnumber(res, "initrnspacl");
4528
4529         for (i = 0; i < ntups; i++)
4530         {
4531                 nsinfo[i].dobj.objType = DO_NAMESPACE;
4532                 nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4533                 nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4534                 AssignDumpId(&nsinfo[i].dobj);
4535                 nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
4536                 nsinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4537                 nsinfo[i].nspacl = pg_strdup(PQgetvalue(res, i, i_nspacl));
4538                 nsinfo[i].rnspacl = pg_strdup(PQgetvalue(res, i, i_rnspacl));
4539                 nsinfo[i].initnspacl = pg_strdup(PQgetvalue(res, i, i_initnspacl));
4540                 nsinfo[i].initrnspacl = pg_strdup(PQgetvalue(res, i, i_initrnspacl));
4541
4542                 /* Decide whether to dump this namespace */
4543                 selectDumpableNamespace(&nsinfo[i], fout);
4544
4545                 /*
4546                  * Do not try to dump ACL if the ACL is empty or the default.
4547                  *
4548                  * This is useful because, for some schemas/objects, the only
4549                  * component we are going to try and dump is the ACL and if we can
4550                  * remove that then 'dump' goes to zero/false and we don't consider
4551                  * this object for dumping at all later on.
4552                  */
4553                 if (PQgetisnull(res, i, i_nspacl) && PQgetisnull(res, i, i_rnspacl) &&
4554                         PQgetisnull(res, i, i_initnspacl) &&
4555                         PQgetisnull(res, i, i_initrnspacl))
4556                         nsinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4557
4558                 if (strlen(nsinfo[i].rolname) == 0)
4559                         write_msg(NULL, "WARNING: owner of schema \"%s\" appears to be invalid\n",
4560                                           nsinfo[i].dobj.name);
4561         }
4562
4563         PQclear(res);
4564         destroyPQExpBuffer(query);
4565
4566         *numNamespaces = ntups;
4567
4568         return nsinfo;
4569 }
4570
4571 /*
4572  * findNamespace:
4573  *              given a namespace OID, look up the info read by getNamespaces
4574  */
4575 static NamespaceInfo *
4576 findNamespace(Archive *fout, Oid nsoid)
4577 {
4578         NamespaceInfo *nsinfo;
4579
4580         nsinfo = findNamespaceByOid(nsoid);
4581         if (nsinfo == NULL)
4582                 exit_horribly(NULL, "schema with OID %u does not exist\n", nsoid);
4583         return nsinfo;
4584 }
4585
4586 /*
4587  * getExtensions:
4588  *        read all extensions in the system catalogs and return them in the
4589  * ExtensionInfo* structure
4590  *
4591  *      numExtensions is set to the number of extensions read in
4592  */
4593 ExtensionInfo *
4594 getExtensions(Archive *fout, int *numExtensions)
4595 {
4596         DumpOptions *dopt = fout->dopt;
4597         PGresult   *res;
4598         int                     ntups;
4599         int                     i;
4600         PQExpBuffer query;
4601         ExtensionInfo *extinfo;
4602         int                     i_tableoid;
4603         int                     i_oid;
4604         int                     i_extname;
4605         int                     i_nspname;
4606         int                     i_extrelocatable;
4607         int                     i_extversion;
4608         int                     i_extconfig;
4609         int                     i_extcondition;
4610
4611         /*
4612          * Before 9.1, there are no extensions.
4613          */
4614         if (fout->remoteVersion < 90100)
4615         {
4616                 *numExtensions = 0;
4617                 return NULL;
4618         }
4619
4620         query = createPQExpBuffer();
4621
4622         appendPQExpBufferStr(query, "SELECT x.tableoid, x.oid, "
4623                                                  "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
4624                                                  "FROM pg_extension x "
4625                                                  "JOIN pg_namespace n ON n.oid = x.extnamespace");
4626
4627         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4628
4629         ntups = PQntuples(res);
4630
4631         extinfo = (ExtensionInfo *) pg_malloc(ntups * sizeof(ExtensionInfo));
4632
4633         i_tableoid = PQfnumber(res, "tableoid");
4634         i_oid = PQfnumber(res, "oid");
4635         i_extname = PQfnumber(res, "extname");
4636         i_nspname = PQfnumber(res, "nspname");
4637         i_extrelocatable = PQfnumber(res, "extrelocatable");
4638         i_extversion = PQfnumber(res, "extversion");
4639         i_extconfig = PQfnumber(res, "extconfig");
4640         i_extcondition = PQfnumber(res, "extcondition");
4641
4642         for (i = 0; i < ntups; i++)
4643         {
4644                 extinfo[i].dobj.objType = DO_EXTENSION;
4645                 extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4646                 extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4647                 AssignDumpId(&extinfo[i].dobj);
4648                 extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
4649                 extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
4650                 extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
4651                 extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
4652                 extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
4653                 extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
4654
4655                 /* Decide whether we want to dump it */
4656                 selectDumpableExtension(&(extinfo[i]), dopt);
4657         }
4658
4659         PQclear(res);
4660         destroyPQExpBuffer(query);
4661
4662         *numExtensions = ntups;
4663
4664         return extinfo;
4665 }
4666
4667 /*
4668  * getTypes:
4669  *        read all types in the system catalogs and return them in the
4670  * TypeInfo* structure
4671  *
4672  *      numTypes is set to the number of types read in
4673  *
4674  * NB: this must run after getFuncs() because we assume we can do
4675  * findFuncByOid().
4676  */
4677 TypeInfo *
4678 getTypes(Archive *fout, int *numTypes)
4679 {
4680         DumpOptions *dopt = fout->dopt;
4681         PGresult   *res;
4682         int                     ntups;
4683         int                     i;
4684         PQExpBuffer query = createPQExpBuffer();
4685         TypeInfo   *tyinfo;
4686         ShellTypeInfo *stinfo;
4687         int                     i_tableoid;
4688         int                     i_oid;
4689         int                     i_typname;
4690         int                     i_typnamespace;
4691         int                     i_typacl;
4692         int                     i_rtypacl;
4693         int                     i_inittypacl;
4694         int                     i_initrtypacl;
4695         int                     i_rolname;
4696         int                     i_typelem;
4697         int                     i_typrelid;
4698         int                     i_typrelkind;
4699         int                     i_typtype;
4700         int                     i_typisdefined;
4701         int                     i_isarray;
4702
4703         /*
4704          * we include even the built-in types because those may be used as array
4705          * elements by user-defined types
4706          *
4707          * we filter out the built-in types when we dump out the types
4708          *
4709          * same approach for undefined (shell) types and array types
4710          *
4711          * Note: as of 8.3 we can reliably detect whether a type is an
4712          * auto-generated array type by checking the element type's typarray.
4713          * (Before that the test is capable of generating false positives.) We
4714          * still check for name beginning with '_', though, so as to avoid the
4715          * cost of the subselect probe for all standard types.  This would have to
4716          * be revisited if the backend ever allows renaming of array types.
4717          */
4718
4719         if (fout->remoteVersion >= 90600)
4720         {
4721                 PQExpBuffer acl_subquery = createPQExpBuffer();
4722                 PQExpBuffer racl_subquery = createPQExpBuffer();
4723                 PQExpBuffer initacl_subquery = createPQExpBuffer();
4724                 PQExpBuffer initracl_subquery = createPQExpBuffer();
4725
4726                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
4727                                                 initracl_subquery, "t.typacl", "t.typowner", "'T'",
4728                                                 dopt->binary_upgrade);
4729
4730                 appendPQExpBuffer(query, "SELECT t.tableoid, t.oid, t.typname, "
4731                                                   "t.typnamespace, "
4732                                                   "%s AS typacl, "
4733                                                   "%s AS rtypacl, "
4734                                                   "%s AS inittypacl, "
4735                                                   "%s AS initrtypacl, "
4736                                                   "(%s t.typowner) AS rolname, "
4737                                                   "t.typelem, t.typrelid, "
4738                                                   "CASE WHEN t.typrelid = 0 THEN ' '::\"char\" "
4739                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = t.typrelid) END AS typrelkind, "
4740                                                   "t.typtype, t.typisdefined, "
4741                                                   "t.typname[0] = '_' AND t.typelem != 0 AND "
4742                                                   "(SELECT typarray FROM pg_type te WHERE oid = t.typelem) = t.oid AS isarray "
4743                                                   "FROM pg_type t "
4744                                                   "LEFT JOIN pg_init_privs pip ON "
4745                                                   "(t.oid = pip.objoid "
4746                                                   "AND pip.classoid = 'pg_type'::regclass "
4747                                                   "AND pip.objsubid = 0) ",
4748                                                   acl_subquery->data,
4749                                                   racl_subquery->data,
4750                                                   initacl_subquery->data,
4751                                                   initracl_subquery->data,
4752                                                   username_subquery);
4753
4754                 destroyPQExpBuffer(acl_subquery);
4755                 destroyPQExpBuffer(racl_subquery);
4756                 destroyPQExpBuffer(initacl_subquery);
4757                 destroyPQExpBuffer(initracl_subquery);
4758         }
4759         else if (fout->remoteVersion >= 90200)
4760         {
4761                 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4762                                                   "typnamespace, typacl, NULL as rtypacl, "
4763                                                   "NULL AS inittypacl, NULL AS initrtypacl, "
4764                                                   "(%s typowner) AS rolname, "
4765                                                   "typelem, typrelid, "
4766                                                   "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4767                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4768                                                   "typtype, typisdefined, "
4769                                                   "typname[0] = '_' AND typelem != 0 AND "
4770                                                   "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
4771                                                   "FROM pg_type",
4772                                                   username_subquery);
4773         }
4774         else if (fout->remoteVersion >= 80300)
4775         {
4776                 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4777                                                   "typnamespace, NULL AS typacl, NULL as rtypacl, "
4778                                                   "NULL AS inittypacl, NULL AS initrtypacl, "
4779                                                   "(%s typowner) AS rolname, "
4780                                                   "typelem, typrelid, "
4781                                                   "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4782                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4783                                                   "typtype, typisdefined, "
4784                                                   "typname[0] = '_' AND typelem != 0 AND "
4785                                                   "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
4786                                                   "FROM pg_type",
4787                                                   username_subquery);
4788         }
4789         else
4790         {
4791                 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
4792                                                   "typnamespace, NULL AS typacl, NULL as rtypacl, "
4793                                                   "NULL AS inittypacl, NULL AS initrtypacl, "
4794                                                   "(%s typowner) AS rolname, "
4795                                                   "typelem, typrelid, "
4796                                                   "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
4797                                                   "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
4798                                                   "typtype, typisdefined, "
4799                                                   "typname[0] = '_' AND typelem != 0 AS isarray "
4800                                                   "FROM pg_type",
4801                                                   username_subquery);
4802         }
4803
4804         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4805
4806         ntups = PQntuples(res);
4807
4808         tyinfo = (TypeInfo *) pg_malloc(ntups * sizeof(TypeInfo));
4809
4810         i_tableoid = PQfnumber(res, "tableoid");
4811         i_oid = PQfnumber(res, "oid");
4812         i_typname = PQfnumber(res, "typname");
4813         i_typnamespace = PQfnumber(res, "typnamespace");
4814         i_typacl = PQfnumber(res, "typacl");
4815         i_rtypacl = PQfnumber(res, "rtypacl");
4816         i_inittypacl = PQfnumber(res, "inittypacl");
4817         i_initrtypacl = PQfnumber(res, "initrtypacl");
4818         i_rolname = PQfnumber(res, "rolname");
4819         i_typelem = PQfnumber(res, "typelem");
4820         i_typrelid = PQfnumber(res, "typrelid");
4821         i_typrelkind = PQfnumber(res, "typrelkind");
4822         i_typtype = PQfnumber(res, "typtype");
4823         i_typisdefined = PQfnumber(res, "typisdefined");
4824         i_isarray = PQfnumber(res, "isarray");
4825
4826         for (i = 0; i < ntups; i++)
4827         {
4828                 tyinfo[i].dobj.objType = DO_TYPE;
4829                 tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4830                 tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4831                 AssignDumpId(&tyinfo[i].dobj);
4832                 tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
4833                 tyinfo[i].dobj.namespace =
4834                         findNamespace(fout,
4835                                                   atooid(PQgetvalue(res, i, i_typnamespace)));
4836                 tyinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4837                 tyinfo[i].typacl = pg_strdup(PQgetvalue(res, i, i_typacl));
4838                 tyinfo[i].rtypacl = pg_strdup(PQgetvalue(res, i, i_rtypacl));
4839                 tyinfo[i].inittypacl = pg_strdup(PQgetvalue(res, i, i_inittypacl));
4840                 tyinfo[i].initrtypacl = pg_strdup(PQgetvalue(res, i, i_initrtypacl));
4841                 tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
4842                 tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
4843                 tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
4844                 tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
4845                 tyinfo[i].shellType = NULL;
4846
4847                 if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
4848                         tyinfo[i].isDefined = true;
4849                 else
4850                         tyinfo[i].isDefined = false;
4851
4852                 if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
4853                         tyinfo[i].isArray = true;
4854                 else
4855                         tyinfo[i].isArray = false;
4856
4857                 /* Decide whether we want to dump it */
4858                 selectDumpableType(&tyinfo[i], fout);
4859
4860                 /* Do not try to dump ACL if no ACL exists. */
4861                 if (PQgetisnull(res, i, i_typacl) && PQgetisnull(res, i, i_rtypacl) &&
4862                         PQgetisnull(res, i, i_inittypacl) &&
4863                         PQgetisnull(res, i, i_initrtypacl))
4864                         tyinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4865
4866                 /*
4867                  * If it's a domain, fetch info about its constraints, if any
4868                  */
4869                 tyinfo[i].nDomChecks = 0;
4870                 tyinfo[i].domChecks = NULL;
4871                 if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
4872                         tyinfo[i].typtype == TYPTYPE_DOMAIN)
4873                         getDomainConstraints(fout, &(tyinfo[i]));
4874
4875                 /*
4876                  * If it's a base type, make a DumpableObject representing a shell
4877                  * definition of the type.  We will need to dump that ahead of the I/O
4878                  * functions for the type.  Similarly, range types need a shell
4879                  * definition in case they have a canonicalize function.
4880                  *
4881                  * Note: the shell type doesn't have a catId.  You might think it
4882                  * should copy the base type's catId, but then it might capture the
4883                  * pg_depend entries for the type, which we don't want.
4884                  */
4885                 if ((tyinfo[i].dobj.dump & DUMP_COMPONENT_DEFINITION) &&
4886                         (tyinfo[i].typtype == TYPTYPE_BASE ||
4887                          tyinfo[i].typtype == TYPTYPE_RANGE))
4888                 {
4889                         stinfo = (ShellTypeInfo *) pg_malloc(sizeof(ShellTypeInfo));
4890                         stinfo->dobj.objType = DO_SHELL_TYPE;
4891                         stinfo->dobj.catId = nilCatalogId;
4892                         AssignDumpId(&stinfo->dobj);
4893                         stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
4894                         stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
4895                         stinfo->baseType = &(tyinfo[i]);
4896                         tyinfo[i].shellType = stinfo;
4897
4898                         /*
4899                          * Initially mark the shell type as not to be dumped.  We'll only
4900                          * dump it if the I/O or canonicalize functions need to be dumped;
4901                          * this is taken care of while sorting dependencies.
4902                          */
4903                         stinfo->dobj.dump = DUMP_COMPONENT_NONE;
4904                 }
4905
4906                 if (strlen(tyinfo[i].rolname) == 0)
4907                         write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",
4908                                           tyinfo[i].dobj.name);
4909         }
4910
4911         *numTypes = ntups;
4912
4913         PQclear(res);
4914
4915         destroyPQExpBuffer(query);
4916
4917         return tyinfo;
4918 }
4919
4920 /*
4921  * getOperators:
4922  *        read all operators in the system catalogs and return them in the
4923  * OprInfo* structure
4924  *
4925  *      numOprs is set to the number of operators read in
4926  */
4927 OprInfo *
4928 getOperators(Archive *fout, int *numOprs)
4929 {
4930         PGresult   *res;
4931         int                     ntups;
4932         int                     i;
4933         PQExpBuffer query = createPQExpBuffer();
4934         OprInfo    *oprinfo;
4935         int                     i_tableoid;
4936         int                     i_oid;
4937         int                     i_oprname;
4938         int                     i_oprnamespace;
4939         int                     i_rolname;
4940         int                     i_oprkind;
4941         int                     i_oprcode;
4942
4943         /*
4944          * find all operators, including builtin operators; we filter out
4945          * system-defined operators at dump-out time.
4946          */
4947
4948         appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
4949                                           "oprnamespace, "
4950                                           "(%s oprowner) AS rolname, "
4951                                           "oprkind, "
4952                                           "oprcode::oid AS oprcode "
4953                                           "FROM pg_operator",
4954                                           username_subquery);
4955
4956         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4957
4958         ntups = PQntuples(res);
4959         *numOprs = ntups;
4960
4961         oprinfo = (OprInfo *) pg_malloc(ntups * sizeof(OprInfo));
4962
4963         i_tableoid = PQfnumber(res, "tableoid");
4964         i_oid = PQfnumber(res, "oid");
4965         i_oprname = PQfnumber(res, "oprname");
4966         i_oprnamespace = PQfnumber(res, "oprnamespace");
4967         i_rolname = PQfnumber(res, "rolname");
4968         i_oprkind = PQfnumber(res, "oprkind");
4969         i_oprcode = PQfnumber(res, "oprcode");
4970
4971         for (i = 0; i < ntups; i++)
4972         {
4973                 oprinfo[i].dobj.objType = DO_OPERATOR;
4974                 oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4975                 oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4976                 AssignDumpId(&oprinfo[i].dobj);
4977                 oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
4978                 oprinfo[i].dobj.namespace =
4979                         findNamespace(fout,
4980                                                   atooid(PQgetvalue(res, i, i_oprnamespace)));
4981                 oprinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4982                 oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
4983                 oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
4984
4985                 /* Decide whether we want to dump it */
4986                 selectDumpableObject(&(oprinfo[i].dobj), fout);
4987
4988                 /* Operators do not currently have ACLs. */
4989                 oprinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
4990
4991                 if (strlen(oprinfo[i].rolname) == 0)
4992                         write_msg(NULL, "WARNING: owner of operator \"%s\" appears to be invalid\n",
4993                                           oprinfo[i].dobj.name);
4994         }
4995
4996         PQclear(res);
4997
4998         destroyPQExpBuffer(query);
4999
5000         return oprinfo;
5001 }
5002
5003 /*
5004  * getCollations:
5005  *        read all collations in the system catalogs and return them in the
5006  * CollInfo* structure
5007  *
5008  *      numCollations is set to the number of collations read in
5009  */
5010 CollInfo *
5011 getCollations(Archive *fout, int *numCollations)
5012 {
5013         PGresult   *res;
5014         int                     ntups;
5015         int                     i;
5016         PQExpBuffer query;
5017         CollInfo   *collinfo;
5018         int                     i_tableoid;
5019         int                     i_oid;
5020         int                     i_collname;
5021         int                     i_collnamespace;
5022         int                     i_rolname;
5023
5024         /* Collations didn't exist pre-9.1 */
5025         if (fout->remoteVersion < 90100)
5026         {
5027                 *numCollations = 0;
5028                 return NULL;
5029         }
5030
5031         query = createPQExpBuffer();
5032
5033         /*
5034          * find all collations, including builtin collations; we filter out
5035          * system-defined collations at dump-out time.
5036          */
5037
5038         appendPQExpBuffer(query, "SELECT tableoid, oid, collname, "
5039                                           "collnamespace, "
5040                                           "(%s collowner) AS rolname "
5041                                           "FROM pg_collation",
5042                                           username_subquery);
5043
5044         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5045
5046         ntups = PQntuples(res);
5047         *numCollations = ntups;
5048
5049         collinfo = (CollInfo *) pg_malloc(ntups * sizeof(CollInfo));
5050
5051         i_tableoid = PQfnumber(res, "tableoid");
5052         i_oid = PQfnumber(res, "oid");
5053         i_collname = PQfnumber(res, "collname");
5054         i_collnamespace = PQfnumber(res, "collnamespace");
5055         i_rolname = PQfnumber(res, "rolname");
5056
5057         for (i = 0; i < ntups; i++)
5058         {
5059                 collinfo[i].dobj.objType = DO_COLLATION;
5060                 collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5061                 collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5062                 AssignDumpId(&collinfo[i].dobj);
5063                 collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
5064                 collinfo[i].dobj.namespace =
5065                         findNamespace(fout,
5066                                                   atooid(PQgetvalue(res, i, i_collnamespace)));
5067                 collinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5068
5069                 /* Decide whether we want to dump it */
5070                 selectDumpableObject(&(collinfo[i].dobj), fout);
5071
5072                 /* Collations do not currently have ACLs. */
5073                 collinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5074         }
5075
5076         PQclear(res);
5077
5078         destroyPQExpBuffer(query);
5079
5080         return collinfo;
5081 }
5082
5083 /*
5084  * getConversions:
5085  *        read all conversions in the system catalogs and return them in the
5086  * ConvInfo* structure
5087  *
5088  *      numConversions is set to the number of conversions read in
5089  */
5090 ConvInfo *
5091 getConversions(Archive *fout, int *numConversions)
5092 {
5093         PGresult   *res;
5094         int                     ntups;
5095         int                     i;
5096         PQExpBuffer query;
5097         ConvInfo   *convinfo;
5098         int                     i_tableoid;
5099         int                     i_oid;
5100         int                     i_conname;
5101         int                     i_connamespace;
5102         int                     i_rolname;
5103
5104         query = createPQExpBuffer();
5105
5106         /*
5107          * find all conversions, including builtin conversions; we filter out
5108          * system-defined conversions at dump-out time.
5109          */
5110
5111         appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
5112                                           "connamespace, "
5113                                           "(%s conowner) AS rolname "
5114                                           "FROM pg_conversion",
5115                                           username_subquery);
5116
5117         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5118
5119         ntups = PQntuples(res);
5120         *numConversions = ntups;
5121
5122         convinfo = (ConvInfo *) pg_malloc(ntups * sizeof(ConvInfo));
5123
5124         i_tableoid = PQfnumber(res, "tableoid");
5125         i_oid = PQfnumber(res, "oid");
5126         i_conname = PQfnumber(res, "conname");
5127         i_connamespace = PQfnumber(res, "connamespace");
5128         i_rolname = PQfnumber(res, "rolname");
5129
5130         for (i = 0; i < ntups; i++)
5131         {
5132                 convinfo[i].dobj.objType = DO_CONVERSION;
5133                 convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5134                 convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5135                 AssignDumpId(&convinfo[i].dobj);
5136                 convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
5137                 convinfo[i].dobj.namespace =
5138                         findNamespace(fout,
5139                                                   atooid(PQgetvalue(res, i, i_connamespace)));
5140                 convinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5141
5142                 /* Decide whether we want to dump it */
5143                 selectDumpableObject(&(convinfo[i].dobj), fout);
5144
5145                 /* Conversions do not currently have ACLs. */
5146                 convinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5147         }
5148
5149         PQclear(res);
5150
5151         destroyPQExpBuffer(query);
5152
5153         return convinfo;
5154 }
5155
5156 /*
5157  * getAccessMethods:
5158  *        read all user-defined access methods in the system catalogs and return
5159  *        them in the AccessMethodInfo* structure
5160  *
5161  *      numAccessMethods is set to the number of access methods read in
5162  */
5163 AccessMethodInfo *
5164 getAccessMethods(Archive *fout, int *numAccessMethods)
5165 {
5166         PGresult   *res;
5167         int                     ntups;
5168         int                     i;
5169         PQExpBuffer query;
5170         AccessMethodInfo *aminfo;
5171         int                     i_tableoid;
5172         int                     i_oid;
5173         int                     i_amname;
5174         int                     i_amhandler;
5175         int                     i_amtype;
5176
5177         /* Before 9.6, there are no user-defined access methods */
5178         if (fout->remoteVersion < 90600)
5179         {
5180                 *numAccessMethods = 0;
5181                 return NULL;
5182         }
5183
5184         query = createPQExpBuffer();
5185
5186         /* Select all access methods from pg_am table */
5187         appendPQExpBuffer(query, "SELECT tableoid, oid, amname, amtype, "
5188                                           "amhandler::pg_catalog.regproc AS amhandler "
5189                                           "FROM pg_am");
5190
5191         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5192
5193         ntups = PQntuples(res);
5194         *numAccessMethods = ntups;
5195
5196         aminfo = (AccessMethodInfo *) pg_malloc(ntups * sizeof(AccessMethodInfo));
5197
5198         i_tableoid = PQfnumber(res, "tableoid");
5199         i_oid = PQfnumber(res, "oid");
5200         i_amname = PQfnumber(res, "amname");
5201         i_amhandler = PQfnumber(res, "amhandler");
5202         i_amtype = PQfnumber(res, "amtype");
5203
5204         for (i = 0; i < ntups; i++)
5205         {
5206                 aminfo[i].dobj.objType = DO_ACCESS_METHOD;
5207                 aminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5208                 aminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5209                 AssignDumpId(&aminfo[i].dobj);
5210                 aminfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_amname));
5211                 aminfo[i].dobj.namespace = NULL;
5212                 aminfo[i].amhandler = pg_strdup(PQgetvalue(res, i, i_amhandler));
5213                 aminfo[i].amtype = *(PQgetvalue(res, i, i_amtype));
5214
5215                 /* Decide whether we want to dump it */
5216                 selectDumpableAccessMethod(&(aminfo[i]), fout);
5217
5218                 /* Access methods do not currently have ACLs. */
5219                 aminfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5220         }
5221
5222         PQclear(res);
5223
5224         destroyPQExpBuffer(query);
5225
5226         return aminfo;
5227 }
5228
5229
5230 /*
5231  * getOpclasses:
5232  *        read all opclasses in the system catalogs and return them in the
5233  * OpclassInfo* structure
5234  *
5235  *      numOpclasses is set to the number of opclasses read in
5236  */
5237 OpclassInfo *
5238 getOpclasses(Archive *fout, int *numOpclasses)
5239 {
5240         PGresult   *res;
5241         int                     ntups;
5242         int                     i;
5243         PQExpBuffer query = createPQExpBuffer();
5244         OpclassInfo *opcinfo;
5245         int                     i_tableoid;
5246         int                     i_oid;
5247         int                     i_opcname;
5248         int                     i_opcnamespace;
5249         int                     i_rolname;
5250
5251         /*
5252          * find all opclasses, including builtin opclasses; we filter out
5253          * system-defined opclasses at dump-out time.
5254          */
5255
5256         appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
5257                                           "opcnamespace, "
5258                                           "(%s opcowner) AS rolname "
5259                                           "FROM pg_opclass",
5260                                           username_subquery);
5261
5262         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5263
5264         ntups = PQntuples(res);
5265         *numOpclasses = ntups;
5266
5267         opcinfo = (OpclassInfo *) pg_malloc(ntups * sizeof(OpclassInfo));
5268
5269         i_tableoid = PQfnumber(res, "tableoid");
5270         i_oid = PQfnumber(res, "oid");
5271         i_opcname = PQfnumber(res, "opcname");
5272         i_opcnamespace = PQfnumber(res, "opcnamespace");
5273         i_rolname = PQfnumber(res, "rolname");
5274
5275         for (i = 0; i < ntups; i++)
5276         {
5277                 opcinfo[i].dobj.objType = DO_OPCLASS;
5278                 opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5279                 opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5280                 AssignDumpId(&opcinfo[i].dobj);
5281                 opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
5282                 opcinfo[i].dobj.namespace =
5283                         findNamespace(fout,
5284                                                   atooid(PQgetvalue(res, i, i_opcnamespace)));
5285                 opcinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5286
5287                 /* Decide whether we want to dump it */
5288                 selectDumpableObject(&(opcinfo[i].dobj), fout);
5289
5290                 /* Op Classes do not currently have ACLs. */
5291                 opcinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5292
5293                 if (strlen(opcinfo[i].rolname) == 0)
5294                         write_msg(NULL, "WARNING: owner of operator class \"%s\" appears to be invalid\n",
5295                                           opcinfo[i].dobj.name);
5296         }
5297
5298         PQclear(res);
5299
5300         destroyPQExpBuffer(query);
5301
5302         return opcinfo;
5303 }
5304
5305 /*
5306  * getOpfamilies:
5307  *        read all opfamilies in the system catalogs and return them in the
5308  * OpfamilyInfo* structure
5309  *
5310  *      numOpfamilies is set to the number of opfamilies read in
5311  */
5312 OpfamilyInfo *
5313 getOpfamilies(Archive *fout, int *numOpfamilies)
5314 {
5315         PGresult   *res;
5316         int                     ntups;
5317         int                     i;
5318         PQExpBuffer query;
5319         OpfamilyInfo *opfinfo;
5320         int                     i_tableoid;
5321         int                     i_oid;
5322         int                     i_opfname;
5323         int                     i_opfnamespace;
5324         int                     i_rolname;
5325
5326         /* Before 8.3, there is no separate concept of opfamilies */
5327         if (fout->remoteVersion < 80300)
5328         {
5329                 *numOpfamilies = 0;
5330                 return NULL;
5331         }
5332
5333         query = createPQExpBuffer();
5334
5335         /*
5336          * find all opfamilies, including builtin opfamilies; we filter out
5337          * system-defined opfamilies at dump-out time.
5338          */
5339
5340         appendPQExpBuffer(query, "SELECT tableoid, oid, opfname, "
5341                                           "opfnamespace, "
5342                                           "(%s opfowner) AS rolname "
5343                                           "FROM pg_opfamily",
5344                                           username_subquery);
5345
5346         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5347
5348         ntups = PQntuples(res);
5349         *numOpfamilies = ntups;
5350
5351         opfinfo = (OpfamilyInfo *) pg_malloc(ntups * sizeof(OpfamilyInfo));
5352
5353         i_tableoid = PQfnumber(res, "tableoid");
5354         i_oid = PQfnumber(res, "oid");
5355         i_opfname = PQfnumber(res, "opfname");
5356         i_opfnamespace = PQfnumber(res, "opfnamespace");
5357         i_rolname = PQfnumber(res, "rolname");
5358
5359         for (i = 0; i < ntups; i++)
5360         {
5361                 opfinfo[i].dobj.objType = DO_OPFAMILY;
5362                 opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5363                 opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5364                 AssignDumpId(&opfinfo[i].dobj);
5365                 opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
5366                 opfinfo[i].dobj.namespace =
5367                         findNamespace(fout,
5368                                                   atooid(PQgetvalue(res, i, i_opfnamespace)));
5369                 opfinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5370
5371                 /* Decide whether we want to dump it */
5372                 selectDumpableObject(&(opfinfo[i].dobj), fout);
5373
5374                 /* Extensions do not currently have ACLs. */
5375                 opfinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5376
5377                 if (strlen(opfinfo[i].rolname) == 0)
5378                         write_msg(NULL, "WARNING: owner of operator family \"%s\" appears to be invalid\n",
5379                                           opfinfo[i].dobj.name);
5380         }
5381
5382         PQclear(res);
5383
5384         destroyPQExpBuffer(query);
5385
5386         return opfinfo;
5387 }
5388
5389 /*
5390  * getAggregates:
5391  *        read all the user-defined aggregates in the system catalogs and
5392  * return them in the AggInfo* structure
5393  *
5394  * numAggs is set to the number of aggregates read in
5395  */
5396 AggInfo *
5397 getAggregates(Archive *fout, int *numAggs)
5398 {
5399         DumpOptions *dopt = fout->dopt;
5400         PGresult   *res;
5401         int                     ntups;
5402         int                     i;
5403         PQExpBuffer query = createPQExpBuffer();
5404         AggInfo    *agginfo;
5405         int                     i_tableoid;
5406         int                     i_oid;
5407         int                     i_aggname;
5408         int                     i_aggnamespace;
5409         int                     i_pronargs;
5410         int                     i_proargtypes;
5411         int                     i_rolname;
5412         int                     i_aggacl;
5413         int                     i_raggacl;
5414         int                     i_initaggacl;
5415         int                     i_initraggacl;
5416
5417         /*
5418          * Find all interesting aggregates.  See comment in getFuncs() for the
5419          * rationale behind the filtering logic.
5420          */
5421         if (fout->remoteVersion >= 90600)
5422         {
5423                 PQExpBuffer acl_subquery = createPQExpBuffer();
5424                 PQExpBuffer racl_subquery = createPQExpBuffer();
5425                 PQExpBuffer initacl_subquery = createPQExpBuffer();
5426                 PQExpBuffer initracl_subquery = createPQExpBuffer();
5427                 const char *agg_check;
5428
5429                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
5430                                                 initracl_subquery, "p.proacl", "p.proowner", "'f'",
5431                                                 dopt->binary_upgrade);
5432
5433                 agg_check = (fout->remoteVersion >= 110000 ? "p.prokind = 'a'"
5434                                          : "p.proisagg");
5435
5436                 appendPQExpBuffer(query, "SELECT p.tableoid, p.oid, "
5437                                                   "p.proname AS aggname, "
5438                                                   "p.pronamespace AS aggnamespace, "
5439                                                   "p.pronargs, p.proargtypes, "
5440                                                   "(%s p.proowner) AS rolname, "
5441                                                   "%s AS aggacl, "
5442                                                   "%s AS raggacl, "
5443                                                   "%s AS initaggacl, "
5444                                                   "%s AS initraggacl "
5445                                                   "FROM pg_proc p "
5446                                                   "LEFT JOIN pg_init_privs pip ON "
5447                                                   "(p.oid = pip.objoid "
5448                                                   "AND pip.classoid = 'pg_proc'::regclass "
5449                                                   "AND pip.objsubid = 0) "
5450                                                   "WHERE %s AND ("
5451                                                   "p.pronamespace != "
5452                                                   "(SELECT oid FROM pg_namespace "
5453                                                   "WHERE nspname = 'pg_catalog') OR "
5454                                                   "p.proacl IS DISTINCT FROM pip.initprivs",
5455                                                   username_subquery,
5456                                                   acl_subquery->data,
5457                                                   racl_subquery->data,
5458                                                   initacl_subquery->data,
5459                                                   initracl_subquery->data,
5460                                                   agg_check);
5461                 if (dopt->binary_upgrade)
5462                         appendPQExpBufferStr(query,
5463                                                                  " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5464                                                                  "classid = 'pg_proc'::regclass AND "
5465                                                                  "objid = p.oid AND "
5466                                                                  "refclassid = 'pg_extension'::regclass AND "
5467                                                                  "deptype = 'e')");
5468                 appendPQExpBufferChar(query, ')');
5469
5470                 destroyPQExpBuffer(acl_subquery);
5471                 destroyPQExpBuffer(racl_subquery);
5472                 destroyPQExpBuffer(initacl_subquery);
5473                 destroyPQExpBuffer(initracl_subquery);
5474         }
5475         else if (fout->remoteVersion >= 80200)
5476         {
5477                 appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
5478                                                   "pronamespace AS aggnamespace, "
5479                                                   "pronargs, proargtypes, "
5480                                                   "(%s proowner) AS rolname, "
5481                                                   "proacl AS aggacl, "
5482                                                   "NULL AS raggacl, "
5483                                                   "NULL AS initaggacl, NULL AS initraggacl "
5484                                                   "FROM pg_proc p "
5485                                                   "WHERE proisagg AND ("
5486                                                   "pronamespace != "
5487                                                   "(SELECT oid FROM pg_namespace "
5488                                                   "WHERE nspname = 'pg_catalog')",
5489                                                   username_subquery);
5490                 if (dopt->binary_upgrade && fout->remoteVersion >= 90100)
5491                         appendPQExpBufferStr(query,
5492                                                                  " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5493                                                                  "classid = 'pg_proc'::regclass AND "
5494                                                                  "objid = p.oid AND "
5495                                                                  "refclassid = 'pg_extension'::regclass AND "
5496                                                                  "deptype = 'e')");
5497                 appendPQExpBufferChar(query, ')');
5498         }
5499         else
5500         {
5501                 appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
5502                                                   "pronamespace AS aggnamespace, "
5503                                                   "CASE WHEN proargtypes[0] = 'pg_catalog.\"any\"'::pg_catalog.regtype THEN 0 ELSE 1 END AS pronargs, "
5504                                                   "proargtypes, "
5505                                                   "(%s proowner) AS rolname, "
5506                                                   "proacl AS aggacl, "
5507                                                   "NULL AS raggacl, "
5508                                                   "NULL AS initaggacl, NULL AS initraggacl "
5509                                                   "FROM pg_proc "
5510                                                   "WHERE proisagg "
5511                                                   "AND pronamespace != "
5512                                                   "(SELECT oid FROM pg_namespace WHERE nspname = 'pg_catalog')",
5513                                                   username_subquery);
5514         }
5515
5516         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5517
5518         ntups = PQntuples(res);
5519         *numAggs = ntups;
5520
5521         agginfo = (AggInfo *) pg_malloc(ntups * sizeof(AggInfo));
5522
5523         i_tableoid = PQfnumber(res, "tableoid");
5524         i_oid = PQfnumber(res, "oid");
5525         i_aggname = PQfnumber(res, "aggname");
5526         i_aggnamespace = PQfnumber(res, "aggnamespace");
5527         i_pronargs = PQfnumber(res, "pronargs");
5528         i_proargtypes = PQfnumber(res, "proargtypes");
5529         i_rolname = PQfnumber(res, "rolname");
5530         i_aggacl = PQfnumber(res, "aggacl");
5531         i_raggacl = PQfnumber(res, "raggacl");
5532         i_initaggacl = PQfnumber(res, "initaggacl");
5533         i_initraggacl = PQfnumber(res, "initraggacl");
5534
5535         for (i = 0; i < ntups; i++)
5536         {
5537                 agginfo[i].aggfn.dobj.objType = DO_AGG;
5538                 agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5539                 agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5540                 AssignDumpId(&agginfo[i].aggfn.dobj);
5541                 agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
5542                 agginfo[i].aggfn.dobj.namespace =
5543                         findNamespace(fout,
5544                                                   atooid(PQgetvalue(res, i, i_aggnamespace)));
5545                 agginfo[i].aggfn.rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5546                 if (strlen(agginfo[i].aggfn.rolname) == 0)
5547                         write_msg(NULL, "WARNING: owner of aggregate function \"%s\" appears to be invalid\n",
5548                                           agginfo[i].aggfn.dobj.name);
5549                 agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
5550                 agginfo[i].aggfn.prorettype = InvalidOid;       /* not saved */
5551                 agginfo[i].aggfn.proacl = pg_strdup(PQgetvalue(res, i, i_aggacl));
5552                 agginfo[i].aggfn.rproacl = pg_strdup(PQgetvalue(res, i, i_raggacl));
5553                 agginfo[i].aggfn.initproacl = pg_strdup(PQgetvalue(res, i, i_initaggacl));
5554                 agginfo[i].aggfn.initrproacl = pg_strdup(PQgetvalue(res, i, i_initraggacl));
5555                 agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs));
5556                 if (agginfo[i].aggfn.nargs == 0)
5557                         agginfo[i].aggfn.argtypes = NULL;
5558                 else
5559                 {
5560                         agginfo[i].aggfn.argtypes = (Oid *) pg_malloc(agginfo[i].aggfn.nargs * sizeof(Oid));
5561                         parseOidArray(PQgetvalue(res, i, i_proargtypes),
5562                                                   agginfo[i].aggfn.argtypes,
5563                                                   agginfo[i].aggfn.nargs);
5564                 }
5565
5566                 /* Decide whether we want to dump it */
5567                 selectDumpableObject(&(agginfo[i].aggfn.dobj), fout);
5568
5569                 /* Do not try to dump ACL if no ACL exists. */
5570                 if (PQgetisnull(res, i, i_aggacl) && PQgetisnull(res, i, i_raggacl) &&
5571                         PQgetisnull(res, i, i_initaggacl) &&
5572                         PQgetisnull(res, i, i_initraggacl))
5573                         agginfo[i].aggfn.dobj.dump &= ~DUMP_COMPONENT_ACL;
5574         }
5575
5576         PQclear(res);
5577
5578         destroyPQExpBuffer(query);
5579
5580         return agginfo;
5581 }
5582
5583 /*
5584  * getFuncs:
5585  *        read all the user-defined functions in the system catalogs and
5586  * return them in the FuncInfo* structure
5587  *
5588  * numFuncs is set to the number of functions read in
5589  */
5590 FuncInfo *
5591 getFuncs(Archive *fout, int *numFuncs)
5592 {
5593         DumpOptions *dopt = fout->dopt;
5594         PGresult   *res;
5595         int                     ntups;
5596         int                     i;
5597         PQExpBuffer query = createPQExpBuffer();
5598         FuncInfo   *finfo;
5599         int                     i_tableoid;
5600         int                     i_oid;
5601         int                     i_proname;
5602         int                     i_pronamespace;
5603         int                     i_rolname;
5604         int                     i_prolang;
5605         int                     i_pronargs;
5606         int                     i_proargtypes;
5607         int                     i_prorettype;
5608         int                     i_proacl;
5609         int                     i_rproacl;
5610         int                     i_initproacl;
5611         int                     i_initrproacl;
5612
5613         /*
5614          * Find all interesting functions.  This is a bit complicated:
5615          *
5616          * 1. Always exclude aggregates; those are handled elsewhere.
5617          *
5618          * 2. Always exclude functions that are internally dependent on something
5619          * else, since presumably those will be created as a result of creating
5620          * the something else.  This currently acts only to suppress constructor
5621          * functions for range types (so we only need it in 9.2 and up).  Note
5622          * this is OK only because the constructors don't have any dependencies
5623          * the range type doesn't have; otherwise we might not get creation
5624          * ordering correct.
5625          *
5626          * 3. Otherwise, we normally exclude functions in pg_catalog.  However, if
5627          * they're members of extensions and we are in binary-upgrade mode then
5628          * include them, since we want to dump extension members individually in
5629          * that mode.  Also, if they are used by casts or transforms then we need
5630          * to gather the information about them, though they won't be dumped if
5631          * they are built-in.  Also, in 9.6 and up, include functions in
5632          * pg_catalog if they have an ACL different from what's shown in
5633          * pg_init_privs.
5634          */
5635         if (fout->remoteVersion >= 90600)
5636         {
5637                 PQExpBuffer acl_subquery = createPQExpBuffer();
5638                 PQExpBuffer racl_subquery = createPQExpBuffer();
5639                 PQExpBuffer initacl_subquery = createPQExpBuffer();
5640                 PQExpBuffer initracl_subquery = createPQExpBuffer();
5641                 const char *not_agg_check;
5642
5643                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
5644                                                 initracl_subquery, "p.proacl", "p.proowner", "'f'",
5645                                                 dopt->binary_upgrade);
5646
5647                 not_agg_check = (fout->remoteVersion >= 110000 ? "p.prokind <> 'a'"
5648                                                  : "NOT p.proisagg");
5649
5650                 appendPQExpBuffer(query,
5651                                                   "SELECT p.tableoid, p.oid, p.proname, p.prolang, "
5652                                                   "p.pronargs, p.proargtypes, p.prorettype, "
5653                                                   "%s AS proacl, "
5654                                                   "%s AS rproacl, "
5655                                                   "%s AS initproacl, "
5656                                                   "%s AS initrproacl, "
5657                                                   "p.pronamespace, "
5658                                                   "(%s p.proowner) AS rolname "
5659                                                   "FROM pg_proc p "
5660                                                   "LEFT JOIN pg_init_privs pip ON "
5661                                                   "(p.oid = pip.objoid "
5662                                                   "AND pip.classoid = 'pg_proc'::regclass "
5663                                                   "AND pip.objsubid = 0) "
5664                                                   "WHERE %s"
5665                                                   "\n  AND NOT EXISTS (SELECT 1 FROM pg_depend "
5666                                                   "WHERE classid = 'pg_proc'::regclass AND "
5667                                                   "objid = p.oid AND deptype = 'i')"
5668                                                   "\n  AND ("
5669                                                   "\n  pronamespace != "
5670                                                   "(SELECT oid FROM pg_namespace "
5671                                                   "WHERE nspname = 'pg_catalog')"
5672                                                   "\n  OR EXISTS (SELECT 1 FROM pg_cast"
5673                                                   "\n  WHERE pg_cast.oid > %u "
5674                                                   "\n  AND p.oid = pg_cast.castfunc)"
5675                                                   "\n  OR EXISTS (SELECT 1 FROM pg_transform"
5676                                                   "\n  WHERE pg_transform.oid > %u AND "
5677                                                   "\n  (p.oid = pg_transform.trffromsql"
5678                                                   "\n  OR p.oid = pg_transform.trftosql))",
5679                                                   acl_subquery->data,
5680                                                   racl_subquery->data,
5681                                                   initacl_subquery->data,
5682                                                   initracl_subquery->data,
5683                                                   username_subquery,
5684                                                   not_agg_check,
5685                                                   g_last_builtin_oid,
5686                                                   g_last_builtin_oid);
5687                 if (dopt->binary_upgrade)
5688                         appendPQExpBufferStr(query,
5689                                                                  "\n  OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5690                                                                  "classid = 'pg_proc'::regclass AND "
5691                                                                  "objid = p.oid AND "
5692                                                                  "refclassid = 'pg_extension'::regclass AND "
5693                                                                  "deptype = 'e')");
5694                 appendPQExpBufferStr(query,
5695                                                          "\n  OR p.proacl IS DISTINCT FROM pip.initprivs");
5696                 appendPQExpBufferChar(query, ')');
5697
5698                 destroyPQExpBuffer(acl_subquery);
5699                 destroyPQExpBuffer(racl_subquery);
5700                 destroyPQExpBuffer(initacl_subquery);
5701                 destroyPQExpBuffer(initracl_subquery);
5702         }
5703         else
5704         {
5705                 appendPQExpBuffer(query,
5706                                                   "SELECT tableoid, oid, proname, prolang, "
5707                                                   "pronargs, proargtypes, prorettype, proacl, "
5708                                                   "NULL as rproacl, "
5709                                                   "NULL as initproacl, NULL AS initrproacl, "
5710                                                   "pronamespace, "
5711                                                   "(%s proowner) AS rolname "
5712                                                   "FROM pg_proc p "
5713                                                   "WHERE NOT proisagg",
5714                                                   username_subquery);
5715                 if (fout->remoteVersion >= 90200)
5716                         appendPQExpBufferStr(query,
5717                                                                  "\n  AND NOT EXISTS (SELECT 1 FROM pg_depend "
5718                                                                  "WHERE classid = 'pg_proc'::regclass AND "
5719                                                                  "objid = p.oid AND deptype = 'i')");
5720                 appendPQExpBuffer(query,
5721                                                   "\n  AND ("
5722                                                   "\n  pronamespace != "
5723                                                   "(SELECT oid FROM pg_namespace "
5724                                                   "WHERE nspname = 'pg_catalog')"
5725                                                   "\n  OR EXISTS (SELECT 1 FROM pg_cast"
5726                                                   "\n  WHERE pg_cast.oid > '%u'::oid"
5727                                                   "\n  AND p.oid = pg_cast.castfunc)",
5728                                                   g_last_builtin_oid);
5729
5730                 if (fout->remoteVersion >= 90500)
5731                         appendPQExpBuffer(query,
5732                                                           "\n  OR EXISTS (SELECT 1 FROM pg_transform"
5733                                                           "\n  WHERE pg_transform.oid > '%u'::oid"
5734                                                           "\n  AND (p.oid = pg_transform.trffromsql"
5735                                                           "\n  OR p.oid = pg_transform.trftosql))",
5736                                                           g_last_builtin_oid);
5737
5738                 if (dopt->binary_upgrade && fout->remoteVersion >= 90100)
5739                         appendPQExpBufferStr(query,
5740                                                                  "\n  OR EXISTS(SELECT 1 FROM pg_depend WHERE "
5741                                                                  "classid = 'pg_proc'::regclass AND "
5742                                                                  "objid = p.oid AND "
5743                                                                  "refclassid = 'pg_extension'::regclass AND "
5744                                                                  "deptype = 'e')");
5745                 appendPQExpBufferChar(query, ')');
5746         }
5747
5748         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5749
5750         ntups = PQntuples(res);
5751
5752         *numFuncs = ntups;
5753
5754         finfo = (FuncInfo *) pg_malloc0(ntups * sizeof(FuncInfo));
5755
5756         i_tableoid = PQfnumber(res, "tableoid");
5757         i_oid = PQfnumber(res, "oid");
5758         i_proname = PQfnumber(res, "proname");
5759         i_pronamespace = PQfnumber(res, "pronamespace");
5760         i_rolname = PQfnumber(res, "rolname");
5761         i_prolang = PQfnumber(res, "prolang");
5762         i_pronargs = PQfnumber(res, "pronargs");
5763         i_proargtypes = PQfnumber(res, "proargtypes");
5764         i_prorettype = PQfnumber(res, "prorettype");
5765         i_proacl = PQfnumber(res, "proacl");
5766         i_rproacl = PQfnumber(res, "rproacl");
5767         i_initproacl = PQfnumber(res, "initproacl");
5768         i_initrproacl = PQfnumber(res, "initrproacl");
5769
5770         for (i = 0; i < ntups; i++)
5771         {
5772                 finfo[i].dobj.objType = DO_FUNC;
5773                 finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5774                 finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5775                 AssignDumpId(&finfo[i].dobj);
5776                 finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
5777                 finfo[i].dobj.namespace =
5778                         findNamespace(fout,
5779                                                   atooid(PQgetvalue(res, i, i_pronamespace)));
5780                 finfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
5781                 finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
5782                 finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
5783                 finfo[i].proacl = pg_strdup(PQgetvalue(res, i, i_proacl));
5784                 finfo[i].rproacl = pg_strdup(PQgetvalue(res, i, i_rproacl));
5785                 finfo[i].initproacl = pg_strdup(PQgetvalue(res, i, i_initproacl));
5786                 finfo[i].initrproacl = pg_strdup(PQgetvalue(res, i, i_initrproacl));
5787                 finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
5788                 if (finfo[i].nargs == 0)
5789                         finfo[i].argtypes = NULL;
5790                 else
5791                 {
5792                         finfo[i].argtypes = (Oid *) pg_malloc(finfo[i].nargs * sizeof(Oid));
5793                         parseOidArray(PQgetvalue(res, i, i_proargtypes),
5794                                                   finfo[i].argtypes, finfo[i].nargs);
5795                 }
5796
5797                 /* Decide whether we want to dump it */
5798                 selectDumpableObject(&(finfo[i].dobj), fout);
5799
5800                 /* Do not try to dump ACL if no ACL exists. */
5801                 if (PQgetisnull(res, i, i_proacl) && PQgetisnull(res, i, i_rproacl) &&
5802                         PQgetisnull(res, i, i_initproacl) &&
5803                         PQgetisnull(res, i, i_initrproacl))
5804                         finfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
5805
5806                 if (strlen(finfo[i].rolname) == 0)
5807                         write_msg(NULL,
5808                                           "WARNING: owner of function \"%s\" appears to be invalid\n",
5809                                           finfo[i].dobj.name);
5810         }
5811
5812         PQclear(res);
5813
5814         destroyPQExpBuffer(query);
5815
5816         return finfo;
5817 }
5818
5819 /*
5820  * getTables
5821  *        read all the tables (no indexes)
5822  * in the system catalogs return them in the TableInfo* structure
5823  *
5824  * numTables is set to the number of tables read in
5825  */
5826 TableInfo *
5827 getTables(Archive *fout, int *numTables)
5828 {
5829         DumpOptions *dopt = fout->dopt;
5830         PGresult   *res;
5831         int                     ntups;
5832         int                     i;
5833         PQExpBuffer query = createPQExpBuffer();
5834         TableInfo  *tblinfo;
5835         int                     i_reltableoid;
5836         int                     i_reloid;
5837         int                     i_relname;
5838         int                     i_relnamespace;
5839         int                     i_relkind;
5840         int                     i_relacl;
5841         int                     i_rrelacl;
5842         int                     i_initrelacl;
5843         int                     i_initrrelacl;
5844         int                     i_rolname;
5845         int                     i_relchecks;
5846         int                     i_relhastriggers;
5847         int                     i_relhasindex;
5848         int                     i_relhasrules;
5849         int                     i_relrowsec;
5850         int                     i_relforcerowsec;
5851         int                     i_relhasoids;
5852         int                     i_relfrozenxid;
5853         int                     i_relminmxid;
5854         int                     i_toastoid;
5855         int                     i_toastfrozenxid;
5856         int                     i_toastminmxid;
5857         int                     i_relpersistence;
5858         int                     i_relispopulated;
5859         int                     i_relreplident;
5860         int                     i_owning_tab;
5861         int                     i_owning_col;
5862         int                     i_reltablespace;
5863         int                     i_reloptions;
5864         int                     i_checkoption;
5865         int                     i_toastreloptions;
5866         int                     i_reloftype;
5867         int                     i_relpages;
5868         int                     i_is_identity_sequence;
5869         int                     i_changed_acl;
5870         int                     i_partkeydef;
5871         int                     i_ispartition;
5872         int                     i_partbound;
5873
5874         /*
5875          * Find all the tables and table-like objects.
5876          *
5877          * We include system catalogs, so that we can work if a user table is
5878          * defined to inherit from a system catalog (pretty weird, but...)
5879          *
5880          * We ignore relations that are not ordinary tables, sequences, views,
5881          * materialized views, composite types, or foreign tables.
5882          *
5883          * Composite-type table entries won't be dumped as such, but we have to
5884          * make a DumpableObject for them so that we can track dependencies of the
5885          * composite type (pg_depend entries for columns of the composite type
5886          * link to the pg_class entry not the pg_type entry).
5887          *
5888          * Note: in this phase we should collect only a minimal amount of
5889          * information about each table, basically just enough to decide if it is
5890          * interesting. We must fetch all tables in this phase because otherwise
5891          * we cannot correctly identify inherited columns, owned sequences, etc.
5892          */
5893
5894         if (fout->remoteVersion >= 90600)
5895         {
5896                 char       *partkeydef = "NULL";
5897                 char       *ispartition = "false";
5898                 char       *partbound = "NULL";
5899
5900                 PQExpBuffer acl_subquery = createPQExpBuffer();
5901                 PQExpBuffer racl_subquery = createPQExpBuffer();
5902                 PQExpBuffer initacl_subquery = createPQExpBuffer();
5903                 PQExpBuffer initracl_subquery = createPQExpBuffer();
5904
5905                 PQExpBuffer attacl_subquery = createPQExpBuffer();
5906                 PQExpBuffer attracl_subquery = createPQExpBuffer();
5907                 PQExpBuffer attinitacl_subquery = createPQExpBuffer();
5908                 PQExpBuffer attinitracl_subquery = createPQExpBuffer();
5909
5910                 /*
5911                  * Collect the information about any partitioned tables, which were
5912                  * added in PG10.
5913                  */
5914
5915                 if (fout->remoteVersion >= 100000)
5916                 {
5917                         partkeydef = "pg_get_partkeydef(c.oid)";
5918                         ispartition = "c.relispartition";
5919                         partbound = "pg_get_expr(c.relpartbound, c.oid)";
5920                 }
5921
5922                 /*
5923                  * Left join to pick up dependency info linking sequences to their
5924                  * owning column, if any (note this dependency is AUTO as of 8.2)
5925                  *
5926                  * Left join to detect if any privileges are still as-set-at-init, in
5927                  * which case we won't dump out ACL commands for those.
5928                  */
5929
5930                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
5931                                                 initracl_subquery, "c.relacl", "c.relowner",
5932                                                 "CASE WHEN c.relkind = " CppAsString2(RELKIND_SEQUENCE)
5933                                                 " THEN 's' ELSE 'r' END::\"char\"",
5934                                                 dopt->binary_upgrade);
5935
5936                 buildACLQueries(attacl_subquery, attracl_subquery, attinitacl_subquery,
5937                                                 attinitracl_subquery, "at.attacl", "c.relowner", "'c'",
5938                                                 dopt->binary_upgrade);
5939
5940                 appendPQExpBuffer(query,
5941                                                   "SELECT c.tableoid, c.oid, c.relname, "
5942                                                   "%s AS relacl, %s as rrelacl, "
5943                                                   "%s AS initrelacl, %s as initrrelacl, "
5944                                                   "c.relkind, c.relnamespace, "
5945                                                   "(%s c.relowner) AS rolname, "
5946                                                   "c.relchecks, c.relhastriggers, "
5947                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
5948                                                   "c.relrowsecurity, c.relforcerowsecurity, "
5949                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
5950                                                   "tc.relfrozenxid AS tfrozenxid, "
5951                                                   "tc.relminmxid AS tminmxid, "
5952                                                   "c.relpersistence, c.relispopulated, "
5953                                                   "c.relreplident, c.relpages, "
5954                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
5955                                                   "d.refobjid AS owning_tab, "
5956                                                   "d.refobjsubid AS owning_col, "
5957                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
5958                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
5959                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
5960                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
5961                                                   "tc.reloptions AS toast_reloptions, "
5962                                                   "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, "
5963                                                   "EXISTS (SELECT 1 FROM pg_attribute at LEFT JOIN pg_init_privs pip ON "
5964                                                   "(c.oid = pip.objoid "
5965                                                   "AND pip.classoid = 'pg_class'::regclass "
5966                                                   "AND pip.objsubid = at.attnum)"
5967                                                   "WHERE at.attrelid = c.oid AND ("
5968                                                   "%s IS NOT NULL "
5969                                                   "OR %s IS NOT NULL "
5970                                                   "OR %s IS NOT NULL "
5971                                                   "OR %s IS NOT NULL"
5972                                                   "))"
5973                                                   "AS changed_acl, "
5974                                                   "%s AS partkeydef, "
5975                                                   "%s AS ispartition, "
5976                                                   "%s AS partbound "
5977                                                   "FROM pg_class c "
5978                                                   "LEFT JOIN pg_depend d ON "
5979                                                   "(c.relkind = '%c' AND "
5980                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
5981                                                   "d.objsubid = 0 AND "
5982                                                   "d.refclassid = c.tableoid AND d.deptype IN ('a', 'i')) "
5983                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
5984                                                   "LEFT JOIN pg_init_privs pip ON "
5985                                                   "(c.oid = pip.objoid "
5986                                                   "AND pip.classoid = 'pg_class'::regclass "
5987                                                   "AND pip.objsubid = 0) "
5988                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c', '%c') "
5989                                                   "ORDER BY c.oid",
5990                                                   acl_subquery->data,
5991                                                   racl_subquery->data,
5992                                                   initacl_subquery->data,
5993                                                   initracl_subquery->data,
5994                                                   username_subquery,
5995                                                   RELKIND_SEQUENCE,
5996                                                   attacl_subquery->data,
5997                                                   attracl_subquery->data,
5998                                                   attinitacl_subquery->data,
5999                                                   attinitracl_subquery->data,
6000                                                   partkeydef,
6001                                                   ispartition,
6002                                                   partbound,
6003                                                   RELKIND_SEQUENCE,
6004                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6005                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
6006                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE,
6007                                                   RELKIND_PARTITIONED_TABLE);
6008
6009                 destroyPQExpBuffer(acl_subquery);
6010                 destroyPQExpBuffer(racl_subquery);
6011                 destroyPQExpBuffer(initacl_subquery);
6012                 destroyPQExpBuffer(initracl_subquery);
6013
6014                 destroyPQExpBuffer(attacl_subquery);
6015                 destroyPQExpBuffer(attracl_subquery);
6016                 destroyPQExpBuffer(attinitacl_subquery);
6017                 destroyPQExpBuffer(attinitracl_subquery);
6018         }
6019         else if (fout->remoteVersion >= 90500)
6020         {
6021                 /*
6022                  * Left join to pick up dependency info linking sequences to their
6023                  * owning column, if any (note this dependency is AUTO as of 8.2)
6024                  */
6025                 appendPQExpBuffer(query,
6026                                                   "SELECT c.tableoid, c.oid, c.relname, "
6027                                                   "c.relacl, NULL as rrelacl, "
6028                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6029                                                   "c.relkind, "
6030                                                   "c.relnamespace, "
6031                                                   "(%s c.relowner) AS rolname, "
6032                                                   "c.relchecks, c.relhastriggers, "
6033                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6034                                                   "c.relrowsecurity, c.relforcerowsecurity, "
6035                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
6036                                                   "tc.relfrozenxid AS tfrozenxid, "
6037                                                   "tc.relminmxid AS tminmxid, "
6038                                                   "c.relpersistence, c.relispopulated, "
6039                                                   "c.relreplident, c.relpages, "
6040                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
6041                                                   "d.refobjid AS owning_tab, "
6042                                                   "d.refobjsubid AS owning_col, "
6043                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6044                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
6045                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
6046                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
6047                                                   "tc.reloptions AS toast_reloptions, "
6048                                                   "NULL AS changed_acl, "
6049                                                   "NULL AS partkeydef, "
6050                                                   "false AS ispartition, "
6051                                                   "NULL AS partbound "
6052                                                   "FROM pg_class c "
6053                                                   "LEFT JOIN pg_depend d ON "
6054                                                   "(c.relkind = '%c' AND "
6055                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6056                                                   "d.objsubid = 0 AND "
6057                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6058                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6059                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
6060                                                   "ORDER BY c.oid",
6061                                                   username_subquery,
6062                                                   RELKIND_SEQUENCE,
6063                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6064                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
6065                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
6066         }
6067         else if (fout->remoteVersion >= 90400)
6068         {
6069                 /*
6070                  * Left join to pick up dependency info linking sequences to their
6071                  * owning column, if any (note this dependency is AUTO as of 8.2)
6072                  */
6073                 appendPQExpBuffer(query,
6074                                                   "SELECT c.tableoid, c.oid, c.relname, "
6075                                                   "c.relacl, NULL as rrelacl, "
6076                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6077                                                   "c.relkind, "
6078                                                   "c.relnamespace, "
6079                                                   "(%s c.relowner) AS rolname, "
6080                                                   "c.relchecks, c.relhastriggers, "
6081                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6082                                                   "'f'::bool AS relrowsecurity, "
6083                                                   "'f'::bool AS relforcerowsecurity, "
6084                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
6085                                                   "tc.relfrozenxid AS tfrozenxid, "
6086                                                   "tc.relminmxid AS tminmxid, "
6087                                                   "c.relpersistence, c.relispopulated, "
6088                                                   "c.relreplident, c.relpages, "
6089                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
6090                                                   "d.refobjid AS owning_tab, "
6091                                                   "d.refobjsubid AS owning_col, "
6092                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6093                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
6094                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
6095                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
6096                                                   "tc.reloptions AS toast_reloptions, "
6097                                                   "NULL AS changed_acl, "
6098                                                   "NULL AS partkeydef, "
6099                                                   "false AS ispartition, "
6100                                                   "NULL AS partbound "
6101                                                   "FROM pg_class c "
6102                                                   "LEFT JOIN pg_depend d ON "
6103                                                   "(c.relkind = '%c' AND "
6104                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6105                                                   "d.objsubid = 0 AND "
6106                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6107                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6108                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
6109                                                   "ORDER BY c.oid",
6110                                                   username_subquery,
6111                                                   RELKIND_SEQUENCE,
6112                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6113                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
6114                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
6115         }
6116         else if (fout->remoteVersion >= 90300)
6117         {
6118                 /*
6119                  * Left join to pick up dependency info linking sequences to their
6120                  * owning column, if any (note this dependency is AUTO as of 8.2)
6121                  */
6122                 appendPQExpBuffer(query,
6123                                                   "SELECT c.tableoid, c.oid, c.relname, "
6124                                                   "c.relacl, NULL as rrelacl, "
6125                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6126                                                   "c.relkind, "
6127                                                   "c.relnamespace, "
6128                                                   "(%s c.relowner) AS rolname, "
6129                                                   "c.relchecks, c.relhastriggers, "
6130                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6131                                                   "'f'::bool AS relrowsecurity, "
6132                                                   "'f'::bool AS relforcerowsecurity, "
6133                                                   "c.relfrozenxid, c.relminmxid, tc.oid AS toid, "
6134                                                   "tc.relfrozenxid AS tfrozenxid, "
6135                                                   "tc.relminmxid AS tminmxid, "
6136                                                   "c.relpersistence, c.relispopulated, "
6137                                                   "'d' AS relreplident, c.relpages, "
6138                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
6139                                                   "d.refobjid AS owning_tab, "
6140                                                   "d.refobjsubid AS owning_col, "
6141                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6142                                                   "array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
6143                                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
6144                                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
6145                                                   "tc.reloptions AS toast_reloptions, "
6146                                                   "NULL AS changed_acl, "
6147                                                   "NULL AS partkeydef, "
6148                                                   "false AS ispartition, "
6149                                                   "NULL AS partbound "
6150                                                   "FROM pg_class c "
6151                                                   "LEFT JOIN pg_depend d ON "
6152                                                   "(c.relkind = '%c' AND "
6153                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6154                                                   "d.objsubid = 0 AND "
6155                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6156                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6157                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
6158                                                   "ORDER BY c.oid",
6159                                                   username_subquery,
6160                                                   RELKIND_SEQUENCE,
6161                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6162                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
6163                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
6164         }
6165         else if (fout->remoteVersion >= 90100)
6166         {
6167                 /*
6168                  * Left join to pick up dependency info linking sequences to their
6169                  * owning column, if any (note this dependency is AUTO as of 8.2)
6170                  */
6171                 appendPQExpBuffer(query,
6172                                                   "SELECT c.tableoid, c.oid, c.relname, "
6173                                                   "c.relacl, NULL as rrelacl, "
6174                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6175                                                   "c.relkind, "
6176                                                   "c.relnamespace, "
6177                                                   "(%s c.relowner) AS rolname, "
6178                                                   "c.relchecks, c.relhastriggers, "
6179                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6180                                                   "'f'::bool AS relrowsecurity, "
6181                                                   "'f'::bool AS relforcerowsecurity, "
6182                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
6183                                                   "tc.relfrozenxid AS tfrozenxid, "
6184                                                   "0 AS tminmxid, "
6185                                                   "c.relpersistence, 't' as relispopulated, "
6186                                                   "'d' AS relreplident, c.relpages, "
6187                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
6188                                                   "d.refobjid AS owning_tab, "
6189                                                   "d.refobjsubid AS owning_col, "
6190                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6191                                                   "c.reloptions AS reloptions, "
6192                                                   "tc.reloptions AS toast_reloptions, "
6193                                                   "NULL AS changed_acl, "
6194                                                   "NULL AS partkeydef, "
6195                                                   "false AS ispartition, "
6196                                                   "NULL AS partbound "
6197                                                   "FROM pg_class c "
6198                                                   "LEFT JOIN pg_depend d ON "
6199                                                   "(c.relkind = '%c' AND "
6200                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6201                                                   "d.objsubid = 0 AND "
6202                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6203                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6204                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
6205                                                   "ORDER BY c.oid",
6206                                                   username_subquery,
6207                                                   RELKIND_SEQUENCE,
6208                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6209                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
6210                                                   RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
6211         }
6212         else if (fout->remoteVersion >= 90000)
6213         {
6214                 /*
6215                  * Left join to pick up dependency info linking sequences to their
6216                  * owning column, if any (note this dependency is AUTO as of 8.2)
6217                  */
6218                 appendPQExpBuffer(query,
6219                                                   "SELECT c.tableoid, c.oid, c.relname, "
6220                                                   "c.relacl, NULL as rrelacl, "
6221                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6222                                                   "c.relkind, "
6223                                                   "c.relnamespace, "
6224                                                   "(%s c.relowner) AS rolname, "
6225                                                   "c.relchecks, c.relhastriggers, "
6226                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6227                                                   "'f'::bool AS relrowsecurity, "
6228                                                   "'f'::bool AS relforcerowsecurity, "
6229                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
6230                                                   "tc.relfrozenxid AS tfrozenxid, "
6231                                                   "0 AS tminmxid, "
6232                                                   "'p' AS relpersistence, 't' as relispopulated, "
6233                                                   "'d' AS relreplident, c.relpages, "
6234                                                   "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
6235                                                   "d.refobjid AS owning_tab, "
6236                                                   "d.refobjsubid AS owning_col, "
6237                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6238                                                   "c.reloptions AS reloptions, "
6239                                                   "tc.reloptions AS toast_reloptions, "
6240                                                   "NULL AS changed_acl, "
6241                                                   "NULL AS partkeydef, "
6242                                                   "false AS ispartition, "
6243                                                   "NULL AS partbound "
6244                                                   "FROM pg_class c "
6245                                                   "LEFT JOIN pg_depend d ON "
6246                                                   "(c.relkind = '%c' AND "
6247                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6248                                                   "d.objsubid = 0 AND "
6249                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6250                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6251                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
6252                                                   "ORDER BY c.oid",
6253                                                   username_subquery,
6254                                                   RELKIND_SEQUENCE,
6255                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6256                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6257         }
6258         else if (fout->remoteVersion >= 80400)
6259         {
6260                 /*
6261                  * Left join to pick up dependency info linking sequences to their
6262                  * owning column, if any (note this dependency is AUTO as of 8.2)
6263                  */
6264                 appendPQExpBuffer(query,
6265                                                   "SELECT c.tableoid, c.oid, c.relname, "
6266                                                   "c.relacl, NULL as rrelacl, "
6267                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6268                                                   "c.relkind, "
6269                                                   "c.relnamespace, "
6270                                                   "(%s c.relowner) AS rolname, "
6271                                                   "c.relchecks, c.relhastriggers, "
6272                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6273                                                   "'f'::bool AS relrowsecurity, "
6274                                                   "'f'::bool AS relforcerowsecurity, "
6275                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
6276                                                   "tc.relfrozenxid AS tfrozenxid, "
6277                                                   "0 AS tminmxid, "
6278                                                   "'p' AS relpersistence, 't' as relispopulated, "
6279                                                   "'d' AS relreplident, c.relpages, "
6280                                                   "NULL AS reloftype, "
6281                                                   "d.refobjid AS owning_tab, "
6282                                                   "d.refobjsubid AS owning_col, "
6283                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6284                                                   "c.reloptions AS reloptions, "
6285                                                   "tc.reloptions AS toast_reloptions, "
6286                                                   "NULL AS changed_acl, "
6287                                                   "NULL AS partkeydef, "
6288                                                   "false AS ispartition, "
6289                                                   "NULL AS partbound "
6290                                                   "FROM pg_class c "
6291                                                   "LEFT JOIN pg_depend d ON "
6292                                                   "(c.relkind = '%c' AND "
6293                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6294                                                   "d.objsubid = 0 AND "
6295                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6296                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6297                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
6298                                                   "ORDER BY c.oid",
6299                                                   username_subquery,
6300                                                   RELKIND_SEQUENCE,
6301                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6302                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6303         }
6304         else if (fout->remoteVersion >= 80200)
6305         {
6306                 /*
6307                  * Left join to pick up dependency info linking sequences to their
6308                  * owning column, if any (note this dependency is AUTO as of 8.2)
6309                  */
6310                 appendPQExpBuffer(query,
6311                                                   "SELECT c.tableoid, c.oid, c.relname, "
6312                                                   "c.relacl, NULL as rrelacl, "
6313                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6314                                                   "c.relkind, "
6315                                                   "c.relnamespace, "
6316                                                   "(%s c.relowner) AS rolname, "
6317                                                   "c.relchecks, (c.reltriggers <> 0) AS relhastriggers, "
6318                                                   "c.relhasindex, c.relhasrules, c.relhasoids, "
6319                                                   "'f'::bool AS relrowsecurity, "
6320                                                   "'f'::bool AS relforcerowsecurity, "
6321                                                   "c.relfrozenxid, 0 AS relminmxid, tc.oid AS toid, "
6322                                                   "tc.relfrozenxid AS tfrozenxid, "
6323                                                   "0 AS tminmxid, "
6324                                                   "'p' AS relpersistence, 't' as relispopulated, "
6325                                                   "'d' AS relreplident, c.relpages, "
6326                                                   "NULL AS reloftype, "
6327                                                   "d.refobjid AS owning_tab, "
6328                                                   "d.refobjsubid AS owning_col, "
6329                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6330                                                   "c.reloptions AS reloptions, "
6331                                                   "NULL AS toast_reloptions, "
6332                                                   "NULL AS changed_acl, "
6333                                                   "NULL AS partkeydef, "
6334                                                   "false AS ispartition, "
6335                                                   "NULL AS partbound "
6336                                                   "FROM pg_class c "
6337                                                   "LEFT JOIN pg_depend d ON "
6338                                                   "(c.relkind = '%c' AND "
6339                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6340                                                   "d.objsubid = 0 AND "
6341                                                   "d.refclassid = c.tableoid AND d.deptype = 'a') "
6342                                                   "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
6343                                                   "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
6344                                                   "ORDER BY c.oid",
6345                                                   username_subquery,
6346                                                   RELKIND_SEQUENCE,
6347                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6348                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6349         }
6350         else
6351         {
6352                 /*
6353                  * Left join to pick up dependency info linking sequences to their
6354                  * owning column, if any
6355                  */
6356                 appendPQExpBuffer(query,
6357                                                   "SELECT c.tableoid, c.oid, relname, "
6358                                                   "relacl, NULL as rrelacl, "
6359                                                   "NULL AS initrelacl, NULL AS initrrelacl, "
6360                                                   "relkind, relnamespace, "
6361                                                   "(%s relowner) AS rolname, "
6362                                                   "relchecks, (reltriggers <> 0) AS relhastriggers, "
6363                                                   "relhasindex, relhasrules, relhasoids, "
6364                                                   "'f'::bool AS relrowsecurity, "
6365                                                   "'f'::bool AS relforcerowsecurity, "
6366                                                   "0 AS relfrozenxid, 0 AS relminmxid,"
6367                                                   "0 AS toid, "
6368                                                   "0 AS tfrozenxid, 0 AS tminmxid,"
6369                                                   "'p' AS relpersistence, 't' as relispopulated, "
6370                                                   "'d' AS relreplident, relpages, "
6371                                                   "NULL AS reloftype, "
6372                                                   "d.refobjid AS owning_tab, "
6373                                                   "d.refobjsubid AS owning_col, "
6374                                                   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
6375                                                   "NULL AS reloptions, "
6376                                                   "NULL AS toast_reloptions, "
6377                                                   "NULL AS changed_acl, "
6378                                                   "NULL AS partkeydef, "
6379                                                   "false AS ispartition, "
6380                                                   "NULL AS partbound "
6381                                                   "FROM pg_class c "
6382                                                   "LEFT JOIN pg_depend d ON "
6383                                                   "(c.relkind = '%c' AND "
6384                                                   "d.classid = c.tableoid AND d.objid = c.oid AND "
6385                                                   "d.objsubid = 0 AND "
6386                                                   "d.refclassid = c.tableoid AND d.deptype = 'i') "
6387                                                   "WHERE relkind in ('%c', '%c', '%c', '%c') "
6388                                                   "ORDER BY c.oid",
6389                                                   username_subquery,
6390                                                   RELKIND_SEQUENCE,
6391                                                   RELKIND_RELATION, RELKIND_SEQUENCE,
6392                                                   RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
6393         }
6394
6395         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6396
6397         ntups = PQntuples(res);
6398
6399         *numTables = ntups;
6400
6401         /*
6402          * Extract data from result and lock dumpable tables.  We do the locking
6403          * before anything else, to minimize the window wherein a table could
6404          * disappear under us.
6405          *
6406          * Note that we have to save info about all tables here, even when dumping
6407          * only one, because we don't yet know which tables might be inheritance
6408          * ancestors of the target table.
6409          */
6410         tblinfo = (TableInfo *) pg_malloc0(ntups * sizeof(TableInfo));
6411
6412         i_reltableoid = PQfnumber(res, "tableoid");
6413         i_reloid = PQfnumber(res, "oid");
6414         i_relname = PQfnumber(res, "relname");
6415         i_relnamespace = PQfnumber(res, "relnamespace");
6416         i_relacl = PQfnumber(res, "relacl");
6417         i_rrelacl = PQfnumber(res, "rrelacl");
6418         i_initrelacl = PQfnumber(res, "initrelacl");
6419         i_initrrelacl = PQfnumber(res, "initrrelacl");
6420         i_relkind = PQfnumber(res, "relkind");
6421         i_rolname = PQfnumber(res, "rolname");
6422         i_relchecks = PQfnumber(res, "relchecks");
6423         i_relhastriggers = PQfnumber(res, "relhastriggers");
6424         i_relhasindex = PQfnumber(res, "relhasindex");
6425         i_relhasrules = PQfnumber(res, "relhasrules");
6426         i_relrowsec = PQfnumber(res, "relrowsecurity");
6427         i_relforcerowsec = PQfnumber(res, "relforcerowsecurity");
6428         i_relhasoids = PQfnumber(res, "relhasoids");
6429         i_relfrozenxid = PQfnumber(res, "relfrozenxid");
6430         i_relminmxid = PQfnumber(res, "relminmxid");
6431         i_toastoid = PQfnumber(res, "toid");
6432         i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
6433         i_toastminmxid = PQfnumber(res, "tminmxid");
6434         i_relpersistence = PQfnumber(res, "relpersistence");
6435         i_relispopulated = PQfnumber(res, "relispopulated");
6436         i_relreplident = PQfnumber(res, "relreplident");
6437         i_relpages = PQfnumber(res, "relpages");
6438         i_owning_tab = PQfnumber(res, "owning_tab");
6439         i_owning_col = PQfnumber(res, "owning_col");
6440         i_reltablespace = PQfnumber(res, "reltablespace");
6441         i_reloptions = PQfnumber(res, "reloptions");
6442         i_checkoption = PQfnumber(res, "checkoption");
6443         i_toastreloptions = PQfnumber(res, "toast_reloptions");
6444         i_reloftype = PQfnumber(res, "reloftype");
6445         i_is_identity_sequence = PQfnumber(res, "is_identity_sequence");
6446         i_changed_acl = PQfnumber(res, "changed_acl");
6447         i_partkeydef = PQfnumber(res, "partkeydef");
6448         i_ispartition = PQfnumber(res, "ispartition");
6449         i_partbound = PQfnumber(res, "partbound");
6450
6451         if (dopt->lockWaitTimeout)
6452         {
6453                 /*
6454                  * Arrange to fail instead of waiting forever for a table lock.
6455                  *
6456                  * NB: this coding assumes that the only queries issued within the
6457                  * following loop are LOCK TABLEs; else the timeout may be undesirably
6458                  * applied to other things too.
6459                  */
6460                 resetPQExpBuffer(query);
6461                 appendPQExpBufferStr(query, "SET statement_timeout = ");
6462                 appendStringLiteralConn(query, dopt->lockWaitTimeout, GetConnection(fout));
6463                 ExecuteSqlStatement(fout, query->data);
6464         }
6465
6466         for (i = 0; i < ntups; i++)
6467         {
6468                 tblinfo[i].dobj.objType = DO_TABLE;
6469                 tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
6470                 tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
6471                 AssignDumpId(&tblinfo[i].dobj);
6472                 tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
6473                 tblinfo[i].dobj.namespace =
6474                         findNamespace(fout,
6475                                                   atooid(PQgetvalue(res, i, i_relnamespace)));
6476                 tblinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
6477                 tblinfo[i].relacl = pg_strdup(PQgetvalue(res, i, i_relacl));
6478                 tblinfo[i].rrelacl = pg_strdup(PQgetvalue(res, i, i_rrelacl));
6479                 tblinfo[i].initrelacl = pg_strdup(PQgetvalue(res, i, i_initrelacl));
6480                 tblinfo[i].initrrelacl = pg_strdup(PQgetvalue(res, i, i_initrrelacl));
6481                 tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
6482                 tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence));
6483                 tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
6484                 tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
6485                 tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
6486                 tblinfo[i].rowsec = (strcmp(PQgetvalue(res, i, i_relrowsec), "t") == 0);
6487                 tblinfo[i].forcerowsec = (strcmp(PQgetvalue(res, i, i_relforcerowsec), "t") == 0);
6488                 tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
6489                 tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0);
6490                 tblinfo[i].relreplident = *(PQgetvalue(res, i, i_relreplident));
6491                 tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages));
6492                 tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
6493                 tblinfo[i].minmxid = atooid(PQgetvalue(res, i, i_relminmxid));
6494                 tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
6495                 tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
6496                 tblinfo[i].toast_minmxid = atooid(PQgetvalue(res, i, i_toastminmxid));
6497                 if (PQgetisnull(res, i, i_reloftype))
6498                         tblinfo[i].reloftype = NULL;
6499                 else
6500                         tblinfo[i].reloftype = pg_strdup(PQgetvalue(res, i, i_reloftype));
6501                 tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
6502                 if (PQgetisnull(res, i, i_owning_tab))
6503                 {
6504                         tblinfo[i].owning_tab = InvalidOid;
6505                         tblinfo[i].owning_col = 0;
6506                 }
6507                 else
6508                 {
6509                         tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
6510                         tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
6511                 }
6512                 tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace));
6513                 tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions));
6514                 if (i_checkoption == -1 || PQgetisnull(res, i, i_checkoption))
6515                         tblinfo[i].checkoption = NULL;
6516                 else
6517                         tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption));
6518                 tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions));
6519
6520                 /* other fields were zeroed above */
6521
6522                 /*
6523                  * Decide whether we want to dump this table.
6524                  */
6525                 if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
6526                         tblinfo[i].dobj.dump = DUMP_COMPONENT_NONE;
6527                 else
6528                         selectDumpableTable(&tblinfo[i], fout);
6529
6530                 /*
6531                  * If the table-level and all column-level ACLs for this table are
6532                  * unchanged, then we don't need to worry about including the ACLs for
6533                  * this table.  If any column-level ACLs have been changed, the
6534                  * 'changed_acl' column from the query will indicate that.
6535                  *
6536                  * This can result in a significant performance improvement in cases
6537                  * where we are only looking to dump out the ACL (eg: pg_catalog).
6538                  */
6539                 if (PQgetisnull(res, i, i_relacl) && PQgetisnull(res, i, i_rrelacl) &&
6540                         PQgetisnull(res, i, i_initrelacl) &&
6541                         PQgetisnull(res, i, i_initrrelacl) &&
6542                         strcmp(PQgetvalue(res, i, i_changed_acl), "f") == 0)
6543                         tblinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
6544
6545                 tblinfo[i].interesting = tblinfo[i].dobj.dump ? true : false;
6546                 tblinfo[i].dummy_view = false;  /* might get set during sort */
6547                 tblinfo[i].postponed_def = false;       /* might get set during sort */
6548
6549                 tblinfo[i].is_identity_sequence = (i_is_identity_sequence >= 0 &&
6550                                                                                    strcmp(PQgetvalue(res, i, i_is_identity_sequence), "t") == 0);
6551
6552                 /* Partition key string or NULL */
6553                 tblinfo[i].partkeydef = pg_strdup(PQgetvalue(res, i, i_partkeydef));
6554                 tblinfo[i].ispartition = (strcmp(PQgetvalue(res, i, i_ispartition), "t") == 0);
6555                 tblinfo[i].partbound = pg_strdup(PQgetvalue(res, i, i_partbound));
6556
6557                 /*
6558                  * Read-lock target tables to make sure they aren't DROPPED or altered
6559                  * in schema before we get around to dumping them.
6560                  *
6561                  * Note that we don't explicitly lock parents of the target tables; we
6562                  * assume our lock on the child is enough to prevent schema
6563                  * alterations to parent tables.
6564                  *
6565                  * NOTE: it'd be kinda nice to lock other relations too, not only
6566                  * plain or partitioned tables, but the backend doesn't presently
6567                  * allow that.
6568                  *
6569                  * We only need to lock the table for certain components; see
6570                  * pg_dump.h
6571                  */
6572                 if (tblinfo[i].dobj.dump &&
6573                         (tblinfo[i].relkind == RELKIND_RELATION ||
6574                          tblinfo->relkind == RELKIND_PARTITIONED_TABLE) &&
6575                         (tblinfo[i].dobj.dump & DUMP_COMPONENTS_REQUIRING_LOCK))
6576                 {
6577                         resetPQExpBuffer(query);
6578                         appendPQExpBuffer(query,
6579                                                           "LOCK TABLE %s IN ACCESS SHARE MODE",
6580                                                           fmtQualifiedDumpable(&tblinfo[i]));
6581                         ExecuteSqlStatement(fout, query->data);
6582                 }
6583
6584                 /* Emit notice if join for owner failed */
6585                 if (strlen(tblinfo[i].rolname) == 0)
6586                         write_msg(NULL, "WARNING: owner of table \"%s\" appears to be invalid\n",
6587                                           tblinfo[i].dobj.name);
6588         }
6589
6590         if (dopt->lockWaitTimeout)
6591         {
6592                 ExecuteSqlStatement(fout, "SET statement_timeout = 0");
6593         }
6594
6595         PQclear(res);
6596
6597         destroyPQExpBuffer(query);
6598
6599         return tblinfo;
6600 }
6601
6602 /*
6603  * getOwnedSeqs
6604  *        identify owned sequences and mark them as dumpable if owning table is
6605  *
6606  * We used to do this in getTables(), but it's better to do it after the
6607  * index used by findTableByOid() has been set up.
6608  */
6609 void
6610 getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables)
6611 {
6612         int                     i;
6613
6614         /*
6615          * Force sequences that are "owned" by table columns to be dumped whenever
6616          * their owning table is being dumped.
6617          */
6618         for (i = 0; i < numTables; i++)
6619         {
6620                 TableInfo  *seqinfo = &tblinfo[i];
6621                 TableInfo  *owning_tab;
6622
6623                 if (!OidIsValid(seqinfo->owning_tab))
6624                         continue;                       /* not an owned sequence */
6625
6626                 owning_tab = findTableByOid(seqinfo->owning_tab);
6627                 if (owning_tab == NULL)
6628                         exit_horribly(NULL, "failed sanity check, parent table with OID %u of sequence with OID %u not found\n",
6629                                                   seqinfo->owning_tab, seqinfo->dobj.catId.oid);
6630
6631                 /*
6632                  * We need to dump the components that are being dumped for the table
6633                  * and any components which the sequence is explicitly marked with.
6634                  *
6635                  * We can't simply use the set of components which are being dumped
6636                  * for the table as the table might be in an extension (and only the
6637                  * non-extension components, eg: ACLs if changed, security labels, and
6638                  * policies, are being dumped) while the sequence is not (and
6639                  * therefore the definition and other components should also be
6640                  * dumped).
6641                  *
6642                  * If the sequence is part of the extension then it should be properly
6643                  * marked by checkExtensionMembership() and this will be a no-op as
6644                  * the table will be equivalently marked.
6645                  */
6646                 seqinfo->dobj.dump = seqinfo->dobj.dump | owning_tab->dobj.dump;
6647
6648                 if (seqinfo->dobj.dump != DUMP_COMPONENT_NONE)
6649                         seqinfo->interesting = true;
6650         }
6651 }
6652
6653 /*
6654  * getInherits
6655  *        read all the inheritance information
6656  * from the system catalogs return them in the InhInfo* structure
6657  *
6658  * numInherits is set to the number of pairs read in
6659  */
6660 InhInfo *
6661 getInherits(Archive *fout, int *numInherits)
6662 {
6663         PGresult   *res;
6664         int                     ntups;
6665         int                     i;
6666         PQExpBuffer query = createPQExpBuffer();
6667         InhInfo    *inhinfo;
6668
6669         int                     i_inhrelid;
6670         int                     i_inhparent;
6671
6672         /*
6673          * Find all the inheritance information, excluding implicit inheritance
6674          * via partitioning.  We handle that case using getPartitions(), because
6675          * we want more information about partitions than just the parent-child
6676          * relationship.
6677          */
6678         appendPQExpBufferStr(query, "SELECT inhrelid, inhparent FROM pg_inherits");
6679
6680         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6681
6682         ntups = PQntuples(res);
6683
6684         *numInherits = ntups;
6685
6686         inhinfo = (InhInfo *) pg_malloc(ntups * sizeof(InhInfo));
6687
6688         i_inhrelid = PQfnumber(res, "inhrelid");
6689         i_inhparent = PQfnumber(res, "inhparent");
6690
6691         for (i = 0; i < ntups; i++)
6692         {
6693                 inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
6694                 inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
6695         }
6696
6697         PQclear(res);
6698
6699         destroyPQExpBuffer(query);
6700
6701         return inhinfo;
6702 }
6703
6704 /*
6705  * getIndexes
6706  *        get information about every index on a dumpable table
6707  *
6708  * Note: index data is not returned directly to the caller, but it
6709  * does get entered into the DumpableObject tables.
6710  */
6711 void
6712 getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
6713 {
6714         int                     i,
6715                                 j;
6716         PQExpBuffer query = createPQExpBuffer();
6717         PGresult   *res;
6718         IndxInfo   *indxinfo;
6719         ConstraintInfo *constrinfo;
6720         int                     i_tableoid,
6721                                 i_oid,
6722                                 i_indexname,
6723                                 i_parentidx,
6724                                 i_indexdef,
6725                                 i_indnkeyatts,
6726                                 i_indnatts,
6727                                 i_indkey,
6728                                 i_indisclustered,
6729                                 i_indisreplident,
6730                                 i_contype,
6731                                 i_conname,
6732                                 i_condeferrable,
6733                                 i_condeferred,
6734                                 i_contableoid,
6735                                 i_conoid,
6736                                 i_condef,
6737                                 i_tablespace,
6738                                 i_indreloptions,
6739                                 i_relpages;
6740         int                     ntups;
6741
6742         for (i = 0; i < numTables; i++)
6743         {
6744                 TableInfo  *tbinfo = &tblinfo[i];
6745
6746                 if (!tbinfo->hasindex)
6747                         continue;
6748
6749                 /*
6750                  * Ignore indexes of tables whose definitions are not to be dumped.
6751                  *
6752                  * We also need indexes on partitioned tables which have partitions to
6753                  * be dumped, in order to dump the indexes on the partitions.
6754                  */
6755                 if (!(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION) &&
6756                         !tbinfo->interesting)
6757                         continue;
6758
6759                 if (g_verbose)
6760                         write_msg(NULL, "reading indexes for table \"%s.%s\"\n",
6761                                           tbinfo->dobj.namespace->dobj.name,
6762                                           tbinfo->dobj.name);
6763
6764                 /*
6765                  * The point of the messy-looking outer join is to find a constraint
6766                  * that is related by an internal dependency link to the index. If we
6767                  * find one, create a CONSTRAINT entry linked to the INDEX entry.  We
6768                  * assume an index won't have more than one internal dependency.
6769                  *
6770                  * As of 9.0 we don't need to look at pg_depend but can check for a
6771                  * match to pg_constraint.conindid.  The check on conrelid is
6772                  * redundant but useful because that column is indexed while conindid
6773                  * is not.
6774                  */
6775                 resetPQExpBuffer(query);
6776                 if (fout->remoteVersion >= 110000)
6777                 {
6778                         appendPQExpBuffer(query,
6779                                                           "SELECT t.tableoid, t.oid, "
6780                                                           "t.relname AS indexname, "
6781                                                           "inh.inhparent AS parentidx, "
6782                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6783                                                           "i.indnkeyatts AS indnkeyatts, "
6784                                                           "i.indnatts AS indnatts, "
6785                                                           "i.indkey, i.indisclustered, "
6786                                                           "i.indisreplident, t.relpages, "
6787                                                           "c.contype, c.conname, "
6788                                                           "c.condeferrable, c.condeferred, "
6789                                                           "c.tableoid AS contableoid, "
6790                                                           "c.oid AS conoid, "
6791                                                           "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6792                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6793                                                           "t.reloptions AS indreloptions "
6794                                                           "FROM pg_catalog.pg_index i "
6795                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6796                                                           "JOIN pg_catalog.pg_class t2 ON (t2.oid = i.indrelid) "
6797                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6798                                                           "ON (i.indrelid = c.conrelid AND "
6799                                                           "i.indexrelid = c.conindid AND "
6800                                                           "c.contype IN ('p','u','x')) "
6801                                                           "LEFT JOIN pg_catalog.pg_inherits inh "
6802                                                           "ON (inh.inhrelid = indexrelid) "
6803                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6804                                                           "AND (i.indisvalid OR t2.relkind = 'p') "
6805                                                           "AND i.indisready "
6806                                                           "ORDER BY indexname",
6807                                                           tbinfo->dobj.catId.oid);
6808                 }
6809                 else if (fout->remoteVersion >= 90400)
6810                 {
6811                         /*
6812                          * the test on indisready is necessary in 9.2, and harmless in
6813                          * earlier/later versions
6814                          */
6815                         appendPQExpBuffer(query,
6816                                                           "SELECT t.tableoid, t.oid, "
6817                                                           "t.relname AS indexname, "
6818                                                           "0 AS parentidx, "
6819                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6820                                                           "i.indnatts AS indnkeyatts, "
6821                                                           "i.indnatts AS indnatts, "
6822                                                           "i.indkey, i.indisclustered, "
6823                                                           "i.indisreplident, t.relpages, "
6824                                                           "c.contype, c.conname, "
6825                                                           "c.condeferrable, c.condeferred, "
6826                                                           "c.tableoid AS contableoid, "
6827                                                           "c.oid AS conoid, "
6828                                                           "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6829                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6830                                                           "t.reloptions AS indreloptions "
6831                                                           "FROM pg_catalog.pg_index i "
6832                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6833                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6834                                                           "ON (i.indrelid = c.conrelid AND "
6835                                                           "i.indexrelid = c.conindid AND "
6836                                                           "c.contype IN ('p','u','x')) "
6837                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6838                                                           "AND i.indisvalid AND i.indisready "
6839                                                           "ORDER BY indexname",
6840                                                           tbinfo->dobj.catId.oid);
6841                 }
6842                 else if (fout->remoteVersion >= 90000)
6843                 {
6844                         /*
6845                          * the test on indisready is necessary in 9.2, and harmless in
6846                          * earlier/later versions
6847                          */
6848                         appendPQExpBuffer(query,
6849                                                           "SELECT t.tableoid, t.oid, "
6850                                                           "t.relname AS indexname, "
6851                                                           "0 AS parentidx, "
6852                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6853                                                           "i.indnatts AS indnkeyatts, "
6854                                                           "i.indnatts AS indnatts, "
6855                                                           "i.indkey, i.indisclustered, "
6856                                                           "false AS indisreplident, t.relpages, "
6857                                                           "c.contype, c.conname, "
6858                                                           "c.condeferrable, c.condeferred, "
6859                                                           "c.tableoid AS contableoid, "
6860                                                           "c.oid AS conoid, "
6861                                                           "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
6862                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6863                                                           "t.reloptions AS indreloptions "
6864                                                           "FROM pg_catalog.pg_index i "
6865                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6866                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6867                                                           "ON (i.indrelid = c.conrelid AND "
6868                                                           "i.indexrelid = c.conindid AND "
6869                                                           "c.contype IN ('p','u','x')) "
6870                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6871                                                           "AND i.indisvalid AND i.indisready "
6872                                                           "ORDER BY indexname",
6873                                                           tbinfo->dobj.catId.oid);
6874                 }
6875                 else if (fout->remoteVersion >= 80200)
6876                 {
6877                         appendPQExpBuffer(query,
6878                                                           "SELECT t.tableoid, t.oid, "
6879                                                           "t.relname AS indexname, "
6880                                                           "0 AS parentidx, "
6881                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6882                                                           "i.indnatts AS indnkeyatts, "
6883                                                           "i.indnatts AS indnatts, "
6884                                                           "i.indkey, i.indisclustered, "
6885                                                           "false AS indisreplident, t.relpages, "
6886                                                           "c.contype, c.conname, "
6887                                                           "c.condeferrable, c.condeferred, "
6888                                                           "c.tableoid AS contableoid, "
6889                                                           "c.oid AS conoid, "
6890                                                           "null AS condef, "
6891                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6892                                                           "t.reloptions AS indreloptions "
6893                                                           "FROM pg_catalog.pg_index i "
6894                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6895                                                           "LEFT JOIN pg_catalog.pg_depend d "
6896                                                           "ON (d.classid = t.tableoid "
6897                                                           "AND d.objid = t.oid "
6898                                                           "AND d.deptype = 'i') "
6899                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6900                                                           "ON (d.refclassid = c.tableoid "
6901                                                           "AND d.refobjid = c.oid) "
6902                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6903                                                           "AND i.indisvalid "
6904                                                           "ORDER BY indexname",
6905                                                           tbinfo->dobj.catId.oid);
6906                 }
6907                 else
6908                 {
6909                         appendPQExpBuffer(query,
6910                                                           "SELECT t.tableoid, t.oid, "
6911                                                           "t.relname AS indexname, "
6912                                                           "0 AS parentidx, "
6913                                                           "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
6914                                                           "t.relnatts AS indnkeyatts, "
6915                                                           "t.relnatts AS indnatts, "
6916                                                           "i.indkey, i.indisclustered, "
6917                                                           "false AS indisreplident, t.relpages, "
6918                                                           "c.contype, c.conname, "
6919                                                           "c.condeferrable, c.condeferred, "
6920                                                           "c.tableoid AS contableoid, "
6921                                                           "c.oid AS conoid, "
6922                                                           "null AS condef, "
6923                                                           "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
6924                                                           "null AS indreloptions "
6925                                                           "FROM pg_catalog.pg_index i "
6926                                                           "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
6927                                                           "LEFT JOIN pg_catalog.pg_depend d "
6928                                                           "ON (d.classid = t.tableoid "
6929                                                           "AND d.objid = t.oid "
6930                                                           "AND d.deptype = 'i') "
6931                                                           "LEFT JOIN pg_catalog.pg_constraint c "
6932                                                           "ON (d.refclassid = c.tableoid "
6933                                                           "AND d.refobjid = c.oid) "
6934                                                           "WHERE i.indrelid = '%u'::pg_catalog.oid "
6935                                                           "ORDER BY indexname",
6936                                                           tbinfo->dobj.catId.oid);
6937                 }
6938
6939                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6940
6941                 ntups = PQntuples(res);
6942
6943                 i_tableoid = PQfnumber(res, "tableoid");
6944                 i_oid = PQfnumber(res, "oid");
6945                 i_indexname = PQfnumber(res, "indexname");
6946                 i_parentidx = PQfnumber(res, "parentidx");
6947                 i_indexdef = PQfnumber(res, "indexdef");
6948                 i_indnkeyatts = PQfnumber(res, "indnkeyatts");
6949                 i_indnatts = PQfnumber(res, "indnatts");
6950                 i_indkey = PQfnumber(res, "indkey");
6951                 i_indisclustered = PQfnumber(res, "indisclustered");
6952                 i_indisreplident = PQfnumber(res, "indisreplident");
6953                 i_relpages = PQfnumber(res, "relpages");
6954                 i_contype = PQfnumber(res, "contype");
6955                 i_conname = PQfnumber(res, "conname");
6956                 i_condeferrable = PQfnumber(res, "condeferrable");
6957                 i_condeferred = PQfnumber(res, "condeferred");
6958                 i_contableoid = PQfnumber(res, "contableoid");
6959                 i_conoid = PQfnumber(res, "conoid");
6960                 i_condef = PQfnumber(res, "condef");
6961                 i_tablespace = PQfnumber(res, "tablespace");
6962                 i_indreloptions = PQfnumber(res, "indreloptions");
6963
6964                 tbinfo->indexes = indxinfo =
6965                         (IndxInfo *) pg_malloc(ntups * sizeof(IndxInfo));
6966                 constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
6967                 tbinfo->numIndexes = ntups;
6968
6969                 for (j = 0; j < ntups; j++)
6970                 {
6971                         char            contype;
6972
6973                         indxinfo[j].dobj.objType = DO_INDEX;
6974                         indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
6975                         indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
6976                         AssignDumpId(&indxinfo[j].dobj);
6977                         indxinfo[j].dobj.dump = tbinfo->dobj.dump;
6978                         indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname));
6979                         indxinfo[j].dobj.namespace = tbinfo->dobj.namespace;
6980                         indxinfo[j].indextable = tbinfo;
6981                         indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef));
6982                         indxinfo[j].indnkeyattrs = atoi(PQgetvalue(res, j, i_indnkeyatts));
6983                         indxinfo[j].indnattrs = atoi(PQgetvalue(res, j, i_indnatts));
6984                         indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
6985                         indxinfo[j].indreloptions = pg_strdup(PQgetvalue(res, j, i_indreloptions));
6986                         indxinfo[j].indkeys = (Oid *) pg_malloc(indxinfo[j].indnattrs * sizeof(Oid));
6987                         parseOidArray(PQgetvalue(res, j, i_indkey),
6988                                                   indxinfo[j].indkeys, indxinfo[j].indnattrs);
6989                         indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
6990                         indxinfo[j].indisreplident = (PQgetvalue(res, j, i_indisreplident)[0] == 't');
6991                         indxinfo[j].parentidx = atooid(PQgetvalue(res, j, i_parentidx));
6992                         indxinfo[j].relpages = atoi(PQgetvalue(res, j, i_relpages));
6993                         contype = *(PQgetvalue(res, j, i_contype));
6994
6995                         if (contype == 'p' || contype == 'u' || contype == 'x')
6996                         {
6997                                 /*
6998                                  * If we found a constraint matching the index, create an
6999                                  * entry for it.
7000                                  */
7001                                 constrinfo[j].dobj.objType = DO_CONSTRAINT;
7002                                 constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
7003                                 constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
7004                                 AssignDumpId(&constrinfo[j].dobj);
7005                                 constrinfo[j].dobj.dump = tbinfo->dobj.dump;
7006                                 constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
7007                                 constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
7008                                 constrinfo[j].contable = tbinfo;
7009                                 constrinfo[j].condomain = NULL;
7010                                 constrinfo[j].contype = contype;
7011                                 if (contype == 'x')
7012                                         constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
7013                                 else
7014                                         constrinfo[j].condef = NULL;
7015                                 constrinfo[j].confrelid = InvalidOid;
7016                                 constrinfo[j].conindex = indxinfo[j].dobj.dumpId;
7017                                 constrinfo[j].condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't';
7018                                 constrinfo[j].condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't';
7019                                 constrinfo[j].conislocal = true;
7020                                 constrinfo[j].separate = true;
7021
7022                                 indxinfo[j].indexconstraint = constrinfo[j].dobj.dumpId;
7023                         }
7024                         else
7025                         {
7026                                 /* Plain secondary index */
7027                                 indxinfo[j].indexconstraint = 0;
7028                         }
7029                 }
7030
7031                 PQclear(res);
7032         }
7033
7034         destroyPQExpBuffer(query);
7035 }
7036
7037 /*
7038  * getExtendedStatistics
7039  *        get information about extended-statistics objects.
7040  *
7041  * Note: extended statistics data is not returned directly to the caller, but
7042  * it does get entered into the DumpableObject tables.
7043  */
7044 void
7045 getExtendedStatistics(Archive *fout)
7046 {
7047         PQExpBuffer query;
7048         PGresult   *res;
7049         StatsExtInfo *statsextinfo;
7050         int                     ntups;
7051         int                     i_tableoid;
7052         int                     i_oid;
7053         int                     i_stxname;
7054         int                     i_stxnamespace;
7055         int                     i_rolname;
7056         int                     i;
7057
7058         /* Extended statistics were new in v10 */
7059         if (fout->remoteVersion < 100000)
7060                 return;
7061
7062         query = createPQExpBuffer();
7063
7064         appendPQExpBuffer(query, "SELECT tableoid, oid, stxname, "
7065                                           "stxnamespace, (%s stxowner) AS rolname "
7066                                           "FROM pg_catalog.pg_statistic_ext",
7067                                           username_subquery);
7068
7069         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7070
7071         ntups = PQntuples(res);
7072
7073         i_tableoid = PQfnumber(res, "tableoid");
7074         i_oid = PQfnumber(res, "oid");
7075         i_stxname = PQfnumber(res, "stxname");
7076         i_stxnamespace = PQfnumber(res, "stxnamespace");
7077         i_rolname = PQfnumber(res, "rolname");
7078
7079         statsextinfo = (StatsExtInfo *) pg_malloc(ntups * sizeof(StatsExtInfo));
7080
7081         for (i = 0; i < ntups; i++)
7082         {
7083                 statsextinfo[i].dobj.objType = DO_STATSEXT;
7084                 statsextinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7085                 statsextinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7086                 AssignDumpId(&statsextinfo[i].dobj);
7087                 statsextinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_stxname));
7088                 statsextinfo[i].dobj.namespace =
7089                         findNamespace(fout,
7090                                                   atooid(PQgetvalue(res, i, i_stxnamespace)));
7091                 statsextinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
7092
7093                 /* Decide whether we want to dump it */
7094                 selectDumpableObject(&(statsextinfo[i].dobj), fout);
7095
7096                 /* Stats objects do not currently have ACLs. */
7097                 statsextinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
7098         }
7099
7100         PQclear(res);
7101         destroyPQExpBuffer(query);
7102 }
7103
7104 /*
7105  * getConstraints
7106  *
7107  * Get info about constraints on dumpable tables.
7108  *
7109  * Currently handles foreign keys only.
7110  * Unique and primary key constraints are handled with indexes,
7111  * while check constraints are processed in getTableAttrs().
7112  */
7113 void
7114 getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
7115 {
7116         int                     i,
7117                                 j;
7118         ConstraintInfo *constrinfo;
7119         PQExpBuffer query;
7120         PGresult   *res;
7121         int                     i_contableoid,
7122                                 i_conoid,
7123                                 i_conname,
7124                                 i_confrelid,
7125                                 i_condef;
7126         int                     ntups;
7127
7128         query = createPQExpBuffer();
7129
7130         for (i = 0; i < numTables; i++)
7131         {
7132                 TableInfo  *tbinfo = &tblinfo[i];
7133
7134                 if (!tbinfo->hastriggers ||
7135                         !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
7136                         continue;
7137
7138                 if (g_verbose)
7139                         write_msg(NULL, "reading foreign key constraints for table \"%s.%s\"\n",
7140                                           tbinfo->dobj.namespace->dobj.name,
7141                                           tbinfo->dobj.name);
7142
7143                 resetPQExpBuffer(query);
7144                 if (fout->remoteVersion >= 110000)
7145                         appendPQExpBuffer(query,
7146                                                           "SELECT tableoid, oid, conname, confrelid, "
7147                                                           "pg_catalog.pg_get_constraintdef(oid) AS condef "
7148                                                           "FROM pg_catalog.pg_constraint "
7149                                                           "WHERE conrelid = '%u'::pg_catalog.oid "
7150                                                           "AND conparentid = 0 "
7151                                                           "AND contype = 'f'",
7152                                                           tbinfo->dobj.catId.oid);
7153                 else
7154                         appendPQExpBuffer(query,
7155                                                           "SELECT tableoid, oid, conname, confrelid, "
7156                                                           "pg_catalog.pg_get_constraintdef(oid) AS condef "
7157                                                           "FROM pg_catalog.pg_constraint "
7158                                                           "WHERE conrelid = '%u'::pg_catalog.oid "
7159                                                           "AND contype = 'f'",
7160                                                           tbinfo->dobj.catId.oid);
7161                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7162
7163                 ntups = PQntuples(res);
7164
7165                 i_contableoid = PQfnumber(res, "tableoid");
7166                 i_conoid = PQfnumber(res, "oid");
7167                 i_conname = PQfnumber(res, "conname");
7168                 i_confrelid = PQfnumber(res, "confrelid");
7169                 i_condef = PQfnumber(res, "condef");
7170
7171                 constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
7172
7173                 for (j = 0; j < ntups; j++)
7174                 {
7175                         constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
7176                         constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
7177                         constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
7178                         AssignDumpId(&constrinfo[j].dobj);
7179                         constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
7180                         constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
7181                         constrinfo[j].contable = tbinfo;
7182                         constrinfo[j].condomain = NULL;
7183                         constrinfo[j].contype = 'f';
7184                         constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
7185                         constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid));
7186                         constrinfo[j].conindex = 0;
7187                         constrinfo[j].condeferrable = false;
7188                         constrinfo[j].condeferred = false;
7189                         constrinfo[j].conislocal = true;
7190                         constrinfo[j].separate = true;
7191                 }
7192
7193                 PQclear(res);
7194         }
7195
7196         destroyPQExpBuffer(query);
7197 }
7198
7199 /*
7200  * getDomainConstraints
7201  *
7202  * Get info about constraints on a domain.
7203  */
7204 static void
7205 getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
7206 {
7207         int                     i;
7208         ConstraintInfo *constrinfo;
7209         PQExpBuffer query;
7210         PGresult   *res;
7211         int                     i_tableoid,
7212                                 i_oid,
7213                                 i_conname,
7214                                 i_consrc;
7215         int                     ntups;
7216
7217         query = createPQExpBuffer();
7218
7219         if (fout->remoteVersion >= 90100)
7220                 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
7221                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
7222                                                   "convalidated "
7223                                                   "FROM pg_catalog.pg_constraint "
7224                                                   "WHERE contypid = '%u'::pg_catalog.oid "
7225                                                   "ORDER BY conname",
7226                                                   tyinfo->dobj.catId.oid);
7227
7228         else
7229                 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
7230                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
7231                                                   "true as convalidated "
7232                                                   "FROM pg_catalog.pg_constraint "
7233                                                   "WHERE contypid = '%u'::pg_catalog.oid "
7234                                                   "ORDER BY conname",
7235                                                   tyinfo->dobj.catId.oid);
7236
7237         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7238
7239         ntups = PQntuples(res);
7240
7241         i_tableoid = PQfnumber(res, "tableoid");
7242         i_oid = PQfnumber(res, "oid");
7243         i_conname = PQfnumber(res, "conname");
7244         i_consrc = PQfnumber(res, "consrc");
7245
7246         constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
7247
7248         tyinfo->nDomChecks = ntups;
7249         tyinfo->domChecks = constrinfo;
7250
7251         for (i = 0; i < ntups; i++)
7252         {
7253                 bool            validated = PQgetvalue(res, i, 4)[0] == 't';
7254
7255                 constrinfo[i].dobj.objType = DO_CONSTRAINT;
7256                 constrinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7257                 constrinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7258                 AssignDumpId(&constrinfo[i].dobj);
7259                 constrinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
7260                 constrinfo[i].dobj.namespace = tyinfo->dobj.namespace;
7261                 constrinfo[i].contable = NULL;
7262                 constrinfo[i].condomain = tyinfo;
7263                 constrinfo[i].contype = 'c';
7264                 constrinfo[i].condef = pg_strdup(PQgetvalue(res, i, i_consrc));
7265                 constrinfo[i].confrelid = InvalidOid;
7266                 constrinfo[i].conindex = 0;
7267                 constrinfo[i].condeferrable = false;
7268                 constrinfo[i].condeferred = false;
7269                 constrinfo[i].conislocal = true;
7270
7271                 constrinfo[i].separate = !validated;
7272
7273                 /*
7274                  * Make the domain depend on the constraint, ensuring it won't be
7275                  * output till any constraint dependencies are OK.  If the constraint
7276                  * has not been validated, it's going to be dumped after the domain
7277                  * anyway, so this doesn't matter.
7278                  */
7279                 if (validated)
7280                         addObjectDependency(&tyinfo->dobj,
7281                                                                 constrinfo[i].dobj.dumpId);
7282         }
7283
7284         PQclear(res);
7285
7286         destroyPQExpBuffer(query);
7287 }
7288
7289 /*
7290  * getRules
7291  *        get basic information about every rule in the system
7292  *
7293  * numRules is set to the number of rules read in
7294  */
7295 RuleInfo *
7296 getRules(Archive *fout, int *numRules)
7297 {
7298         PGresult   *res;
7299         int                     ntups;
7300         int                     i;
7301         PQExpBuffer query = createPQExpBuffer();
7302         RuleInfo   *ruleinfo;
7303         int                     i_tableoid;
7304         int                     i_oid;
7305         int                     i_rulename;
7306         int                     i_ruletable;
7307         int                     i_ev_type;
7308         int                     i_is_instead;
7309         int                     i_ev_enabled;
7310
7311         if (fout->remoteVersion >= 80300)
7312         {
7313                 appendPQExpBufferStr(query, "SELECT "
7314                                                          "tableoid, oid, rulename, "
7315                                                          "ev_class AS ruletable, ev_type, is_instead, "
7316                                                          "ev_enabled "
7317                                                          "FROM pg_rewrite "
7318                                                          "ORDER BY oid");
7319         }
7320         else
7321         {
7322                 appendPQExpBufferStr(query, "SELECT "
7323                                                          "tableoid, oid, rulename, "
7324                                                          "ev_class AS ruletable, ev_type, is_instead, "
7325                                                          "'O'::char AS ev_enabled "
7326                                                          "FROM pg_rewrite "
7327                                                          "ORDER BY oid");
7328         }
7329
7330         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7331
7332         ntups = PQntuples(res);
7333
7334         *numRules = ntups;
7335
7336         ruleinfo = (RuleInfo *) pg_malloc(ntups * sizeof(RuleInfo));
7337
7338         i_tableoid = PQfnumber(res, "tableoid");
7339         i_oid = PQfnumber(res, "oid");
7340         i_rulename = PQfnumber(res, "rulename");
7341         i_ruletable = PQfnumber(res, "ruletable");
7342         i_ev_type = PQfnumber(res, "ev_type");
7343         i_is_instead = PQfnumber(res, "is_instead");
7344         i_ev_enabled = PQfnumber(res, "ev_enabled");
7345
7346         for (i = 0; i < ntups; i++)
7347         {
7348                 Oid                     ruletableoid;
7349
7350                 ruleinfo[i].dobj.objType = DO_RULE;
7351                 ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7352                 ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7353                 AssignDumpId(&ruleinfo[i].dobj);
7354                 ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename));
7355                 ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
7356                 ruleinfo[i].ruletable = findTableByOid(ruletableoid);
7357                 if (ruleinfo[i].ruletable == NULL)
7358                         exit_horribly(NULL, "failed sanity check, parent table with OID %u of pg_rewrite entry with OID %u not found\n",
7359                                                   ruletableoid, ruleinfo[i].dobj.catId.oid);
7360                 ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace;
7361                 ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump;
7362                 ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
7363                 ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
7364                 ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled));
7365                 if (ruleinfo[i].ruletable)
7366                 {
7367                         /*
7368                          * If the table is a view or materialized view, force its ON
7369                          * SELECT rule to be sorted before the view itself --- this
7370                          * ensures that any dependencies for the rule affect the table's
7371                          * positioning. Other rules are forced to appear after their
7372                          * table.
7373                          */
7374                         if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW ||
7375                                  ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) &&
7376                                 ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
7377                         {
7378                                 addObjectDependency(&ruleinfo[i].ruletable->dobj,
7379                                                                         ruleinfo[i].dobj.dumpId);
7380                                 /* We'll merge the rule into CREATE VIEW, if possible */
7381                                 ruleinfo[i].separate = false;
7382                         }
7383                         else
7384                         {
7385                                 addObjectDependency(&ruleinfo[i].dobj,
7386                                                                         ruleinfo[i].ruletable->dobj.dumpId);
7387                                 ruleinfo[i].separate = true;
7388                         }
7389                 }
7390                 else
7391                         ruleinfo[i].separate = true;
7392         }
7393
7394         PQclear(res);
7395
7396         destroyPQExpBuffer(query);
7397
7398         return ruleinfo;
7399 }
7400
7401 /*
7402  * getTriggers
7403  *        get information about every trigger on a dumpable table
7404  *
7405  * Note: trigger data is not returned directly to the caller, but it
7406  * does get entered into the DumpableObject tables.
7407  */
7408 void
7409 getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
7410 {
7411         int                     i,
7412                                 j;
7413         PQExpBuffer query = createPQExpBuffer();
7414         PGresult   *res;
7415         TriggerInfo *tginfo;
7416         int                     i_tableoid,
7417                                 i_oid,
7418                                 i_tgname,
7419                                 i_tgfname,
7420                                 i_tgtype,
7421                                 i_tgnargs,
7422                                 i_tgargs,
7423                                 i_tgisconstraint,
7424                                 i_tgconstrname,
7425                                 i_tgconstrrelid,
7426                                 i_tgconstrrelname,
7427                                 i_tgenabled,
7428                                 i_tgdeferrable,
7429                                 i_tginitdeferred,
7430                                 i_tgdef;
7431         int                     ntups;
7432
7433         for (i = 0; i < numTables; i++)
7434         {
7435                 TableInfo  *tbinfo = &tblinfo[i];
7436
7437                 if (!tbinfo->hastriggers ||
7438                         !(tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
7439                         continue;
7440
7441                 if (g_verbose)
7442                         write_msg(NULL, "reading triggers for table \"%s.%s\"\n",
7443                                           tbinfo->dobj.namespace->dobj.name,
7444                                           tbinfo->dobj.name);
7445
7446                 resetPQExpBuffer(query);
7447                 if (fout->remoteVersion >= 90000)
7448                 {
7449                         /*
7450                          * NB: think not to use pretty=true in pg_get_triggerdef.  It
7451                          * could result in non-forward-compatible dumps of WHEN clauses
7452                          * due to under-parenthesization.
7453                          */
7454                         appendPQExpBuffer(query,
7455                                                           "SELECT tgname, "
7456                                                           "tgfoid::pg_catalog.regproc AS tgfname, "
7457                                                           "pg_catalog.pg_get_triggerdef(oid, false) AS tgdef, "
7458                                                           "tgenabled, tableoid, oid "
7459                                                           "FROM pg_catalog.pg_trigger t "
7460                                                           "WHERE tgrelid = '%u'::pg_catalog.oid "
7461                                                           "AND NOT tgisinternal",
7462                                                           tbinfo->dobj.catId.oid);
7463                 }
7464                 else if (fout->remoteVersion >= 80300)
7465                 {
7466                         /*
7467                          * We ignore triggers that are tied to a foreign-key constraint
7468                          */
7469                         appendPQExpBuffer(query,
7470                                                           "SELECT tgname, "
7471                                                           "tgfoid::pg_catalog.regproc AS tgfname, "
7472                                                           "tgtype, tgnargs, tgargs, tgenabled, "
7473                                                           "tgisconstraint, tgconstrname, tgdeferrable, "
7474                                                           "tgconstrrelid, tginitdeferred, tableoid, oid, "
7475                                                           "tgconstrrelid::pg_catalog.regclass AS tgconstrrelname "
7476                                                           "FROM pg_catalog.pg_trigger t "
7477                                                           "WHERE tgrelid = '%u'::pg_catalog.oid "
7478                                                           "AND tgconstraint = 0",
7479                                                           tbinfo->dobj.catId.oid);
7480                 }
7481                 else
7482                 {
7483                         /*
7484                          * We ignore triggers that are tied to a foreign-key constraint,
7485                          * but in these versions we have to grovel through pg_constraint
7486                          * to find out
7487                          */
7488                         appendPQExpBuffer(query,
7489                                                           "SELECT tgname, "
7490                                                           "tgfoid::pg_catalog.regproc AS tgfname, "
7491                                                           "tgtype, tgnargs, tgargs, tgenabled, "
7492                                                           "tgisconstraint, tgconstrname, tgdeferrable, "
7493                                                           "tgconstrrelid, tginitdeferred, tableoid, oid, "
7494                                                           "tgconstrrelid::pg_catalog.regclass AS tgconstrrelname "
7495                                                           "FROM pg_catalog.pg_trigger t "
7496                                                           "WHERE tgrelid = '%u'::pg_catalog.oid "
7497                                                           "AND (NOT tgisconstraint "
7498                                                           " OR NOT EXISTS"
7499                                                           "  (SELECT 1 FROM pg_catalog.pg_depend d "
7500                                                           "   JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
7501                                                           "   WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))",
7502                                                           tbinfo->dobj.catId.oid);
7503                 }
7504
7505                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7506
7507                 ntups = PQntuples(res);
7508
7509                 i_tableoid = PQfnumber(res, "tableoid");
7510                 i_oid = PQfnumber(res, "oid");
7511                 i_tgname = PQfnumber(res, "tgname");
7512                 i_tgfname = PQfnumber(res, "tgfname");
7513                 i_tgtype = PQfnumber(res, "tgtype");
7514                 i_tgnargs = PQfnumber(res, "tgnargs");
7515                 i_tgargs = PQfnumber(res, "tgargs");
7516                 i_tgisconstraint = PQfnumber(res, "tgisconstraint");
7517                 i_tgconstrname = PQfnumber(res, "tgconstrname");
7518                 i_tgconstrrelid = PQfnumber(res, "tgconstrrelid");
7519                 i_tgconstrrelname = PQfnumber(res, "tgconstrrelname");
7520                 i_tgenabled = PQfnumber(res, "tgenabled");
7521                 i_tgdeferrable = PQfnumber(res, "tgdeferrable");
7522                 i_tginitdeferred = PQfnumber(res, "tginitdeferred");
7523                 i_tgdef = PQfnumber(res, "tgdef");
7524
7525                 tginfo = (TriggerInfo *) pg_malloc(ntups * sizeof(TriggerInfo));
7526
7527                 tbinfo->numTriggers = ntups;
7528                 tbinfo->triggers = tginfo;
7529
7530                 for (j = 0; j < ntups; j++)
7531                 {
7532                         tginfo[j].dobj.objType = DO_TRIGGER;
7533                         tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
7534                         tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
7535                         AssignDumpId(&tginfo[j].dobj);
7536                         tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname));
7537                         tginfo[j].dobj.namespace = tbinfo->dobj.namespace;
7538                         tginfo[j].tgtable = tbinfo;
7539                         tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled));
7540                         if (i_tgdef >= 0)
7541                         {
7542                                 tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef));
7543
7544                                 /* remaining fields are not valid if we have tgdef */
7545                                 tginfo[j].tgfname = NULL;
7546                                 tginfo[j].tgtype = 0;
7547                                 tginfo[j].tgnargs = 0;
7548                                 tginfo[j].tgargs = NULL;
7549                                 tginfo[j].tgisconstraint = false;
7550                                 tginfo[j].tgdeferrable = false;
7551                                 tginfo[j].tginitdeferred = false;
7552                                 tginfo[j].tgconstrname = NULL;
7553                                 tginfo[j].tgconstrrelid = InvalidOid;
7554                                 tginfo[j].tgconstrrelname = NULL;
7555                         }
7556                         else
7557                         {
7558                                 tginfo[j].tgdef = NULL;
7559
7560                                 tginfo[j].tgfname = pg_strdup(PQgetvalue(res, j, i_tgfname));
7561                                 tginfo[j].tgtype = atoi(PQgetvalue(res, j, i_tgtype));
7562                                 tginfo[j].tgnargs = atoi(PQgetvalue(res, j, i_tgnargs));
7563                                 tginfo[j].tgargs = pg_strdup(PQgetvalue(res, j, i_tgargs));
7564                                 tginfo[j].tgisconstraint = *(PQgetvalue(res, j, i_tgisconstraint)) == 't';
7565                                 tginfo[j].tgdeferrable = *(PQgetvalue(res, j, i_tgdeferrable)) == 't';
7566                                 tginfo[j].tginitdeferred = *(PQgetvalue(res, j, i_tginitdeferred)) == 't';
7567
7568                                 if (tginfo[j].tgisconstraint)
7569                                 {
7570                                         tginfo[j].tgconstrname = pg_strdup(PQgetvalue(res, j, i_tgconstrname));
7571                                         tginfo[j].tgconstrrelid = atooid(PQgetvalue(res, j, i_tgconstrrelid));
7572                                         if (OidIsValid(tginfo[j].tgconstrrelid))
7573                                         {
7574                                                 if (PQgetisnull(res, j, i_tgconstrrelname))
7575                                                         exit_horribly(NULL, "query produced null referenced table name for foreign key trigger \"%s\" on table \"%s\" (OID of table: %u)\n",
7576                                                                                   tginfo[j].dobj.name,
7577                                                                                   tbinfo->dobj.name,
7578                                                                                   tginfo[j].tgconstrrelid);
7579                                                 tginfo[j].tgconstrrelname = pg_strdup(PQgetvalue(res, j, i_tgconstrrelname));
7580                                         }
7581                                         else
7582                                                 tginfo[j].tgconstrrelname = NULL;
7583                                 }
7584                                 else
7585                                 {
7586                                         tginfo[j].tgconstrname = NULL;
7587                                         tginfo[j].tgconstrrelid = InvalidOid;
7588                                         tginfo[j].tgconstrrelname = NULL;
7589                                 }
7590                         }
7591                 }
7592
7593                 PQclear(res);
7594         }
7595
7596         destroyPQExpBuffer(query);
7597 }
7598
7599 /*
7600  * getEventTriggers
7601  *        get information about event triggers
7602  */
7603 EventTriggerInfo *
7604 getEventTriggers(Archive *fout, int *numEventTriggers)
7605 {
7606         int                     i;
7607         PQExpBuffer query;
7608         PGresult   *res;
7609         EventTriggerInfo *evtinfo;
7610         int                     i_tableoid,
7611                                 i_oid,
7612                                 i_evtname,
7613                                 i_evtevent,
7614                                 i_evtowner,
7615                                 i_evttags,
7616                                 i_evtfname,
7617                                 i_evtenabled;
7618         int                     ntups;
7619
7620         /* Before 9.3, there are no event triggers */
7621         if (fout->remoteVersion < 90300)
7622         {
7623                 *numEventTriggers = 0;
7624                 return NULL;
7625         }
7626
7627         query = createPQExpBuffer();
7628
7629         appendPQExpBuffer(query,
7630                                           "SELECT e.tableoid, e.oid, evtname, evtenabled, "
7631                                           "evtevent, (%s evtowner) AS evtowner, "
7632                                           "array_to_string(array("
7633                                           "select quote_literal(x) "
7634                                           " from unnest(evttags) as t(x)), ', ') as evttags, "
7635                                           "e.evtfoid::regproc as evtfname "
7636                                           "FROM pg_event_trigger e "
7637                                           "ORDER BY e.oid",
7638                                           username_subquery);
7639
7640         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7641
7642         ntups = PQntuples(res);
7643
7644         *numEventTriggers = ntups;
7645
7646         evtinfo = (EventTriggerInfo *) pg_malloc(ntups * sizeof(EventTriggerInfo));
7647
7648         i_tableoid = PQfnumber(res, "tableoid");
7649         i_oid = PQfnumber(res, "oid");
7650         i_evtname = PQfnumber(res, "evtname");
7651         i_evtevent = PQfnumber(res, "evtevent");
7652         i_evtowner = PQfnumber(res, "evtowner");
7653         i_evttags = PQfnumber(res, "evttags");
7654         i_evtfname = PQfnumber(res, "evtfname");
7655         i_evtenabled = PQfnumber(res, "evtenabled");
7656
7657         for (i = 0; i < ntups; i++)
7658         {
7659                 evtinfo[i].dobj.objType = DO_EVENT_TRIGGER;
7660                 evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7661                 evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7662                 AssignDumpId(&evtinfo[i].dobj);
7663                 evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname));
7664                 evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname));
7665                 evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent));
7666                 evtinfo[i].evtowner = pg_strdup(PQgetvalue(res, i, i_evtowner));
7667                 evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags));
7668                 evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname));
7669                 evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled));
7670
7671                 /* Decide whether we want to dump it */
7672                 selectDumpableObject(&(evtinfo[i].dobj), fout);
7673
7674                 /* Event Triggers do not currently have ACLs. */
7675                 evtinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
7676         }
7677
7678         PQclear(res);
7679
7680         destroyPQExpBuffer(query);
7681
7682         return evtinfo;
7683 }
7684
7685 /*
7686  * getProcLangs
7687  *        get basic information about every procedural language in the system
7688  *
7689  * numProcLangs is set to the number of langs read in
7690  *
7691  * NB: this must run after getFuncs() because we assume we can do
7692  * findFuncByOid().
7693  */
7694 ProcLangInfo *
7695 getProcLangs(Archive *fout, int *numProcLangs)
7696 {
7697         DumpOptions *dopt = fout->dopt;
7698         PGresult   *res;
7699         int                     ntups;
7700         int                     i;
7701         PQExpBuffer query = createPQExpBuffer();
7702         ProcLangInfo *planginfo;
7703         int                     i_tableoid;
7704         int                     i_oid;
7705         int                     i_lanname;
7706         int                     i_lanpltrusted;
7707         int                     i_lanplcallfoid;
7708         int                     i_laninline;
7709         int                     i_lanvalidator;
7710         int                     i_lanacl;
7711         int                     i_rlanacl;
7712         int                     i_initlanacl;
7713         int                     i_initrlanacl;
7714         int                     i_lanowner;
7715
7716         if (fout->remoteVersion >= 90600)
7717         {
7718                 PQExpBuffer acl_subquery = createPQExpBuffer();
7719                 PQExpBuffer racl_subquery = createPQExpBuffer();
7720                 PQExpBuffer initacl_subquery = createPQExpBuffer();
7721                 PQExpBuffer initracl_subquery = createPQExpBuffer();
7722
7723                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
7724                                                 initracl_subquery, "l.lanacl", "l.lanowner", "'l'",
7725                                                 dopt->binary_upgrade);
7726
7727                 /* pg_language has a laninline column */
7728                 appendPQExpBuffer(query, "SELECT l.tableoid, l.oid, "
7729                                                   "l.lanname, l.lanpltrusted, l.lanplcallfoid, "
7730                                                   "l.laninline, l.lanvalidator, "
7731                                                   "%s AS lanacl, "
7732                                                   "%s AS rlanacl, "
7733                                                   "%s AS initlanacl, "
7734                                                   "%s AS initrlanacl, "
7735                                                   "(%s l.lanowner) AS lanowner "
7736                                                   "FROM pg_language l "
7737                                                   "LEFT JOIN pg_init_privs pip ON "
7738                                                   "(l.oid = pip.objoid "
7739                                                   "AND pip.classoid = 'pg_language'::regclass "
7740                                                   "AND pip.objsubid = 0) "
7741                                                   "WHERE l.lanispl "
7742                                                   "ORDER BY l.oid",
7743                                                   acl_subquery->data,
7744                                                   racl_subquery->data,
7745                                                   initacl_subquery->data,
7746                                                   initracl_subquery->data,
7747                                                   username_subquery);
7748
7749                 destroyPQExpBuffer(acl_subquery);
7750                 destroyPQExpBuffer(racl_subquery);
7751                 destroyPQExpBuffer(initacl_subquery);
7752                 destroyPQExpBuffer(initracl_subquery);
7753         }
7754         else if (fout->remoteVersion >= 90000)
7755         {
7756                 /* pg_language has a laninline column */
7757                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7758                                                   "lanname, lanpltrusted, lanplcallfoid, "
7759                                                   "laninline, lanvalidator, lanacl, NULL AS rlanacl, "
7760                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7761                                                   "(%s lanowner) AS lanowner "
7762                                                   "FROM pg_language "
7763                                                   "WHERE lanispl "
7764                                                   "ORDER BY oid",
7765                                                   username_subquery);
7766         }
7767         else if (fout->remoteVersion >= 80300)
7768         {
7769                 /* pg_language has a lanowner column */
7770                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7771                                                   "lanname, lanpltrusted, lanplcallfoid, "
7772                                                   "0 AS laninline, lanvalidator, lanacl, "
7773                                                   "NULL AS rlanacl, "
7774                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7775                                                   "(%s lanowner) AS lanowner "
7776                                                   "FROM pg_language "
7777                                                   "WHERE lanispl "
7778                                                   "ORDER BY oid",
7779                                                   username_subquery);
7780         }
7781         else if (fout->remoteVersion >= 80100)
7782         {
7783                 /* Languages are owned by the bootstrap superuser, OID 10 */
7784                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7785                                                   "lanname, lanpltrusted, lanplcallfoid, "
7786                                                   "0 AS laninline, lanvalidator, lanacl, "
7787                                                   "NULL AS rlanacl, "
7788                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7789                                                   "(%s '10') AS lanowner "
7790                                                   "FROM pg_language "
7791                                                   "WHERE lanispl "
7792                                                   "ORDER BY oid",
7793                                                   username_subquery);
7794         }
7795         else
7796         {
7797                 /* Languages are owned by the bootstrap superuser, sysid 1 */
7798                 appendPQExpBuffer(query, "SELECT tableoid, oid, "
7799                                                   "lanname, lanpltrusted, lanplcallfoid, "
7800                                                   "0 AS laninline, lanvalidator, lanacl, "
7801                                                   "NULL AS rlanacl, "
7802                                                   "NULL AS initlanacl, NULL AS initrlanacl, "
7803                                                   "(%s '1') AS lanowner "
7804                                                   "FROM pg_language "
7805                                                   "WHERE lanispl "
7806                                                   "ORDER BY oid",
7807                                                   username_subquery);
7808         }
7809
7810         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7811
7812         ntups = PQntuples(res);
7813
7814         *numProcLangs = ntups;
7815
7816         planginfo = (ProcLangInfo *) pg_malloc(ntups * sizeof(ProcLangInfo));
7817
7818         i_tableoid = PQfnumber(res, "tableoid");
7819         i_oid = PQfnumber(res, "oid");
7820         i_lanname = PQfnumber(res, "lanname");
7821         i_lanpltrusted = PQfnumber(res, "lanpltrusted");
7822         i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
7823         i_laninline = PQfnumber(res, "laninline");
7824         i_lanvalidator = PQfnumber(res, "lanvalidator");
7825         i_lanacl = PQfnumber(res, "lanacl");
7826         i_rlanacl = PQfnumber(res, "rlanacl");
7827         i_initlanacl = PQfnumber(res, "initlanacl");
7828         i_initrlanacl = PQfnumber(res, "initrlanacl");
7829         i_lanowner = PQfnumber(res, "lanowner");
7830
7831         for (i = 0; i < ntups; i++)
7832         {
7833                 planginfo[i].dobj.objType = DO_PROCLANG;
7834                 planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7835                 planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7836                 AssignDumpId(&planginfo[i].dobj);
7837
7838                 planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
7839                 planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
7840                 planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
7841                 planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
7842                 planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
7843                 planginfo[i].lanacl = pg_strdup(PQgetvalue(res, i, i_lanacl));
7844                 planginfo[i].rlanacl = pg_strdup(PQgetvalue(res, i, i_rlanacl));
7845                 planginfo[i].initlanacl = pg_strdup(PQgetvalue(res, i, i_initlanacl));
7846                 planginfo[i].initrlanacl = pg_strdup(PQgetvalue(res, i, i_initrlanacl));
7847                 planginfo[i].lanowner = pg_strdup(PQgetvalue(res, i, i_lanowner));
7848
7849                 /* Decide whether we want to dump it */
7850                 selectDumpableProcLang(&(planginfo[i]), fout);
7851
7852                 /* Do not try to dump ACL if no ACL exists. */
7853                 if (PQgetisnull(res, i, i_lanacl) && PQgetisnull(res, i, i_rlanacl) &&
7854                         PQgetisnull(res, i, i_initlanacl) &&
7855                         PQgetisnull(res, i, i_initrlanacl))
7856                         planginfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
7857         }
7858
7859         PQclear(res);
7860
7861         destroyPQExpBuffer(query);
7862
7863         return planginfo;
7864 }
7865
7866 /*
7867  * getCasts
7868  *        get basic information about every cast in the system
7869  *
7870  * numCasts is set to the number of casts read in
7871  */
7872 CastInfo *
7873 getCasts(Archive *fout, int *numCasts)
7874 {
7875         PGresult   *res;
7876         int                     ntups;
7877         int                     i;
7878         PQExpBuffer query = createPQExpBuffer();
7879         CastInfo   *castinfo;
7880         int                     i_tableoid;
7881         int                     i_oid;
7882         int                     i_castsource;
7883         int                     i_casttarget;
7884         int                     i_castfunc;
7885         int                     i_castcontext;
7886         int                     i_castmethod;
7887
7888         if (fout->remoteVersion >= 80400)
7889         {
7890                 appendPQExpBufferStr(query, "SELECT tableoid, oid, "
7891                                                          "castsource, casttarget, castfunc, castcontext, "
7892                                                          "castmethod "
7893                                                          "FROM pg_cast ORDER BY 3,4");
7894         }
7895         else
7896         {
7897                 appendPQExpBufferStr(query, "SELECT tableoid, oid, "
7898                                                          "castsource, casttarget, castfunc, castcontext, "
7899                                                          "CASE WHEN castfunc = 0 THEN 'b' ELSE 'f' END AS castmethod "
7900                                                          "FROM pg_cast ORDER BY 3,4");
7901         }
7902
7903         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7904
7905         ntups = PQntuples(res);
7906
7907         *numCasts = ntups;
7908
7909         castinfo = (CastInfo *) pg_malloc(ntups * sizeof(CastInfo));
7910
7911         i_tableoid = PQfnumber(res, "tableoid");
7912         i_oid = PQfnumber(res, "oid");
7913         i_castsource = PQfnumber(res, "castsource");
7914         i_casttarget = PQfnumber(res, "casttarget");
7915         i_castfunc = PQfnumber(res, "castfunc");
7916         i_castcontext = PQfnumber(res, "castcontext");
7917         i_castmethod = PQfnumber(res, "castmethod");
7918
7919         for (i = 0; i < ntups; i++)
7920         {
7921                 PQExpBufferData namebuf;
7922                 TypeInfo   *sTypeInfo;
7923                 TypeInfo   *tTypeInfo;
7924
7925                 castinfo[i].dobj.objType = DO_CAST;
7926                 castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7927                 castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7928                 AssignDumpId(&castinfo[i].dobj);
7929                 castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
7930                 castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
7931                 castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
7932                 castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
7933                 castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod));
7934
7935                 /*
7936                  * Try to name cast as concatenation of typnames.  This is only used
7937                  * for purposes of sorting.  If we fail to find either type, the name
7938                  * will be an empty string.
7939                  */
7940                 initPQExpBuffer(&namebuf);
7941                 sTypeInfo = findTypeByOid(castinfo[i].castsource);
7942                 tTypeInfo = findTypeByOid(castinfo[i].casttarget);
7943                 if (sTypeInfo && tTypeInfo)
7944                         appendPQExpBuffer(&namebuf, "%s %s",
7945                                                           sTypeInfo->dobj.name, tTypeInfo->dobj.name);
7946                 castinfo[i].dobj.name = namebuf.data;
7947
7948                 /* Decide whether we want to dump it */
7949                 selectDumpableCast(&(castinfo[i]), fout);
7950
7951                 /* Casts do not currently have ACLs. */
7952                 castinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
7953         }
7954
7955         PQclear(res);
7956
7957         destroyPQExpBuffer(query);
7958
7959         return castinfo;
7960 }
7961
7962 static char *
7963 get_language_name(Archive *fout, Oid langid)
7964 {
7965         PQExpBuffer query;
7966         PGresult   *res;
7967         char       *lanname;
7968
7969         query = createPQExpBuffer();
7970         appendPQExpBuffer(query, "SELECT lanname FROM pg_language WHERE oid = %u", langid);
7971         res = ExecuteSqlQueryForSingleRow(fout, query->data);
7972         lanname = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
7973         destroyPQExpBuffer(query);
7974         PQclear(res);
7975
7976         return lanname;
7977 }
7978
7979 /*
7980  * getTransforms
7981  *        get basic information about every transform in the system
7982  *
7983  * numTransforms is set to the number of transforms read in
7984  */
7985 TransformInfo *
7986 getTransforms(Archive *fout, int *numTransforms)
7987 {
7988         PGresult   *res;
7989         int                     ntups;
7990         int                     i;
7991         PQExpBuffer query;
7992         TransformInfo *transforminfo;
7993         int                     i_tableoid;
7994         int                     i_oid;
7995         int                     i_trftype;
7996         int                     i_trflang;
7997         int                     i_trffromsql;
7998         int                     i_trftosql;
7999
8000         /* Transforms didn't exist pre-9.5 */
8001         if (fout->remoteVersion < 90500)
8002         {
8003                 *numTransforms = 0;
8004                 return NULL;
8005         }
8006
8007         query = createPQExpBuffer();
8008
8009         appendPQExpBuffer(query, "SELECT tableoid, oid, "
8010                                           "trftype, trflang, trffromsql::oid, trftosql::oid "
8011                                           "FROM pg_transform "
8012                                           "ORDER BY 3,4");
8013
8014         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8015
8016         ntups = PQntuples(res);
8017
8018         *numTransforms = ntups;
8019
8020         transforminfo = (TransformInfo *) pg_malloc(ntups * sizeof(TransformInfo));
8021
8022         i_tableoid = PQfnumber(res, "tableoid");
8023         i_oid = PQfnumber(res, "oid");
8024         i_trftype = PQfnumber(res, "trftype");
8025         i_trflang = PQfnumber(res, "trflang");
8026         i_trffromsql = PQfnumber(res, "trffromsql");
8027         i_trftosql = PQfnumber(res, "trftosql");
8028
8029         for (i = 0; i < ntups; i++)
8030         {
8031                 PQExpBufferData namebuf;
8032                 TypeInfo   *typeInfo;
8033                 char       *lanname;
8034
8035                 transforminfo[i].dobj.objType = DO_TRANSFORM;
8036                 transforminfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8037                 transforminfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8038                 AssignDumpId(&transforminfo[i].dobj);
8039                 transforminfo[i].trftype = atooid(PQgetvalue(res, i, i_trftype));
8040                 transforminfo[i].trflang = atooid(PQgetvalue(res, i, i_trflang));
8041                 transforminfo[i].trffromsql = atooid(PQgetvalue(res, i, i_trffromsql));
8042                 transforminfo[i].trftosql = atooid(PQgetvalue(res, i, i_trftosql));
8043
8044                 /*
8045                  * Try to name transform as concatenation of type and language name.
8046                  * This is only used for purposes of sorting.  If we fail to find
8047                  * either, the name will be an empty string.
8048                  */
8049                 initPQExpBuffer(&namebuf);
8050                 typeInfo = findTypeByOid(transforminfo[i].trftype);
8051                 lanname = get_language_name(fout, transforminfo[i].trflang);
8052                 if (typeInfo && lanname)
8053                         appendPQExpBuffer(&namebuf, "%s %s",
8054                                                           typeInfo->dobj.name, lanname);
8055                 transforminfo[i].dobj.name = namebuf.data;
8056                 free(lanname);
8057
8058                 /* Decide whether we want to dump it */
8059                 selectDumpableObject(&(transforminfo[i].dobj), fout);
8060         }
8061
8062         PQclear(res);
8063
8064         destroyPQExpBuffer(query);
8065
8066         return transforminfo;
8067 }
8068
8069 /*
8070  * getTableAttrs -
8071  *        for each interesting table, read info about its attributes
8072  *        (names, types, default values, CHECK constraints, etc)
8073  *
8074  * This is implemented in a very inefficient way right now, looping
8075  * through the tblinfo and doing a join per table to find the attrs and their
8076  * types.  However, because we want type names and so forth to be named
8077  * relative to the schema of each table, we couldn't do it in just one
8078  * query.  (Maybe one query per schema?)
8079  *
8080  *      modifies tblinfo
8081  */
8082 void
8083 getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
8084 {
8085         DumpOptions *dopt = fout->dopt;
8086         int                     i,
8087                                 j;
8088         PQExpBuffer q = createPQExpBuffer();
8089         int                     i_attnum;
8090         int                     i_attname;
8091         int                     i_atttypname;
8092         int                     i_atttypmod;
8093         int                     i_attstattarget;
8094         int                     i_attstorage;
8095         int                     i_typstorage;
8096         int                     i_attnotnull;
8097         int                     i_atthasdef;
8098         int                     i_attidentity;
8099         int                     i_attisdropped;
8100         int                     i_attlen;
8101         int                     i_attalign;
8102         int                     i_attislocal;
8103         int                     i_attoptions;
8104         int                     i_attcollation;
8105         int                     i_attfdwoptions;
8106         PGresult   *res;
8107         int                     ntups;
8108         bool            hasdefaults;
8109
8110         for (i = 0; i < numTables; i++)
8111         {
8112                 TableInfo  *tbinfo = &tblinfo[i];
8113
8114                 /* Don't bother to collect info for sequences */
8115                 if (tbinfo->relkind == RELKIND_SEQUENCE)
8116                         continue;
8117
8118                 /* Don't bother with uninteresting tables, either */
8119                 if (!tbinfo->interesting)
8120                         continue;
8121
8122                 /* find all the user attributes and their types */
8123
8124                 /*
8125                  * we must read the attribute names in attribute number order! because
8126                  * we will use the attnum to index into the attnames array later.
8127                  */
8128                 if (g_verbose)
8129                         write_msg(NULL, "finding the columns and types of table \"%s.%s\"\n",
8130                                           tbinfo->dobj.namespace->dobj.name,
8131                                           tbinfo->dobj.name);
8132
8133                 resetPQExpBuffer(q);
8134
8135                 if (fout->remoteVersion >= 100000)
8136                 {
8137                         /*
8138                          * attidentity is new in version 10.
8139                          */
8140                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8141                                                           "a.attstattarget, a.attstorage, t.typstorage, "
8142                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
8143                                                           "a.attlen, a.attalign, a.attislocal, "
8144                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8145                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
8146                                                           "CASE WHEN a.attcollation <> t.typcollation "
8147                                                           "THEN a.attcollation ELSE 0 END AS attcollation, "
8148                                                           "a.attidentity, "
8149                                                           "pg_catalog.array_to_string(ARRAY("
8150                                                           "SELECT pg_catalog.quote_ident(option_name) || "
8151                                                           "' ' || pg_catalog.quote_literal(option_value) "
8152                                                           "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
8153                                                           "ORDER BY option_name"
8154                                                           "), E',\n    ') AS attfdwoptions "
8155                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8156                                                           "ON a.atttypid = t.oid "
8157                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
8158                                                           "AND a.attnum > 0::pg_catalog.int2 "
8159                                                           "ORDER BY a.attnum",
8160                                                           tbinfo->dobj.catId.oid);
8161                 }
8162                 else if (fout->remoteVersion >= 90200)
8163                 {
8164                         /*
8165                          * attfdwoptions is new in 9.2.
8166                          */
8167                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8168                                                           "a.attstattarget, a.attstorage, t.typstorage, "
8169                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
8170                                                           "a.attlen, a.attalign, a.attislocal, "
8171                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8172                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
8173                                                           "CASE WHEN a.attcollation <> t.typcollation "
8174                                                           "THEN a.attcollation ELSE 0 END AS attcollation, "
8175                                                           "pg_catalog.array_to_string(ARRAY("
8176                                                           "SELECT pg_catalog.quote_ident(option_name) || "
8177                                                           "' ' || pg_catalog.quote_literal(option_value) "
8178                                                           "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
8179                                                           "ORDER BY option_name"
8180                                                           "), E',\n    ') AS attfdwoptions "
8181                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8182                                                           "ON a.atttypid = t.oid "
8183                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
8184                                                           "AND a.attnum > 0::pg_catalog.int2 "
8185                                                           "ORDER BY a.attnum",
8186                                                           tbinfo->dobj.catId.oid);
8187                 }
8188                 else if (fout->remoteVersion >= 90100)
8189                 {
8190                         /*
8191                          * attcollation is new in 9.1.  Since we only want to dump COLLATE
8192                          * clauses for attributes whose collation is different from their
8193                          * type's default, we use a CASE here to suppress uninteresting
8194                          * attcollations cheaply.
8195                          */
8196                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8197                                                           "a.attstattarget, a.attstorage, t.typstorage, "
8198                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
8199                                                           "a.attlen, a.attalign, a.attislocal, "
8200                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8201                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
8202                                                           "CASE WHEN a.attcollation <> t.typcollation "
8203                                                           "THEN a.attcollation ELSE 0 END AS attcollation, "
8204                                                           "NULL AS attfdwoptions "
8205                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8206                                                           "ON a.atttypid = t.oid "
8207                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
8208                                                           "AND a.attnum > 0::pg_catalog.int2 "
8209                                                           "ORDER BY a.attnum",
8210                                                           tbinfo->dobj.catId.oid);
8211                 }
8212                 else if (fout->remoteVersion >= 90000)
8213                 {
8214                         /* attoptions is new in 9.0 */
8215                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8216                                                           "a.attstattarget, a.attstorage, t.typstorage, "
8217                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
8218                                                           "a.attlen, a.attalign, a.attislocal, "
8219                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8220                                                           "array_to_string(a.attoptions, ', ') AS attoptions, "
8221                                                           "0 AS attcollation, "
8222                                                           "NULL AS attfdwoptions "
8223                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8224                                                           "ON a.atttypid = t.oid "
8225                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
8226                                                           "AND a.attnum > 0::pg_catalog.int2 "
8227                                                           "ORDER BY a.attnum",
8228                                                           tbinfo->dobj.catId.oid);
8229                 }
8230                 else
8231                 {
8232                         /* need left join here to not fail on dropped columns ... */
8233                         appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
8234                                                           "a.attstattarget, a.attstorage, t.typstorage, "
8235                                                           "a.attnotnull, a.atthasdef, a.attisdropped, "
8236                                                           "a.attlen, a.attalign, a.attislocal, "
8237                                                           "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
8238                                                           "'' AS attoptions, 0 AS attcollation, "
8239                                                           "NULL AS attfdwoptions "
8240                                                           "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
8241                                                           "ON a.atttypid = t.oid "
8242                                                           "WHERE a.attrelid = '%u'::pg_catalog.oid "
8243                                                           "AND a.attnum > 0::pg_catalog.int2 "
8244                                                           "ORDER BY a.attnum",
8245                                                           tbinfo->dobj.catId.oid);
8246                 }
8247
8248                 res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
8249
8250                 ntups = PQntuples(res);
8251
8252                 i_attnum = PQfnumber(res, "attnum");
8253                 i_attname = PQfnumber(res, "attname");
8254                 i_atttypname = PQfnumber(res, "atttypname");
8255                 i_atttypmod = PQfnumber(res, "atttypmod");
8256                 i_attstattarget = PQfnumber(res, "attstattarget");
8257                 i_attstorage = PQfnumber(res, "attstorage");
8258                 i_typstorage = PQfnumber(res, "typstorage");
8259                 i_attnotnull = PQfnumber(res, "attnotnull");
8260                 i_atthasdef = PQfnumber(res, "atthasdef");
8261                 i_attidentity = PQfnumber(res, "attidentity");
8262                 i_attisdropped = PQfnumber(res, "attisdropped");
8263                 i_attlen = PQfnumber(res, "attlen");
8264                 i_attalign = PQfnumber(res, "attalign");
8265                 i_attislocal = PQfnumber(res, "attislocal");
8266                 i_attoptions = PQfnumber(res, "attoptions");
8267                 i_attcollation = PQfnumber(res, "attcollation");
8268                 i_attfdwoptions = PQfnumber(res, "attfdwoptions");
8269
8270                 tbinfo->numatts = ntups;
8271                 tbinfo->attnames = (char **) pg_malloc(ntups * sizeof(char *));
8272                 tbinfo->atttypnames = (char **) pg_malloc(ntups * sizeof(char *));
8273                 tbinfo->atttypmod = (int *) pg_malloc(ntups * sizeof(int));
8274                 tbinfo->attstattarget = (int *) pg_malloc(ntups * sizeof(int));
8275                 tbinfo->attstorage = (char *) pg_malloc(ntups * sizeof(char));
8276                 tbinfo->typstorage = (char *) pg_malloc(ntups * sizeof(char));
8277                 tbinfo->attidentity = (char *) pg_malloc(ntups * sizeof(char));
8278                 tbinfo->attisdropped = (bool *) pg_malloc(ntups * sizeof(bool));
8279                 tbinfo->attlen = (int *) pg_malloc(ntups * sizeof(int));
8280                 tbinfo->attalign = (char *) pg_malloc(ntups * sizeof(char));
8281                 tbinfo->attislocal = (bool *) pg_malloc(ntups * sizeof(bool));
8282                 tbinfo->attoptions = (char **) pg_malloc(ntups * sizeof(char *));
8283                 tbinfo->attcollation = (Oid *) pg_malloc(ntups * sizeof(Oid));
8284                 tbinfo->attfdwoptions = (char **) pg_malloc(ntups * sizeof(char *));
8285                 tbinfo->notnull = (bool *) pg_malloc(ntups * sizeof(bool));
8286                 tbinfo->inhNotNull = (bool *) pg_malloc(ntups * sizeof(bool));
8287                 tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
8288                 hasdefaults = false;
8289
8290                 for (j = 0; j < ntups; j++)
8291                 {
8292                         if (j + 1 != atoi(PQgetvalue(res, j, i_attnum)))
8293                                 exit_horribly(NULL,
8294                                                           "invalid column numbering in table \"%s\"\n",
8295                                                           tbinfo->dobj.name);
8296                         tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, i_attname));
8297                         tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, i_atttypname));
8298                         tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
8299                         tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
8300                         tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
8301                         tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
8302                         tbinfo->attidentity[j] = (i_attidentity >= 0 ? *(PQgetvalue(res, j, i_attidentity)) : '\0');
8303                         tbinfo->needs_override = tbinfo->needs_override || (tbinfo->attidentity[j] == ATTRIBUTE_IDENTITY_ALWAYS);
8304                         tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
8305                         tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
8306                         tbinfo->attalign[j] = *(PQgetvalue(res, j, i_attalign));
8307                         tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
8308                         tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
8309                         tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, i_attoptions));
8310                         tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
8311                         tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, i_attfdwoptions));
8312                         tbinfo->attrdefs[j] = NULL; /* fix below */
8313                         if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
8314                                 hasdefaults = true;
8315                         /* these flags will be set in flagInhAttrs() */
8316                         tbinfo->inhNotNull[j] = false;
8317                 }
8318
8319                 PQclear(res);
8320
8321                 /*
8322                  * Get info about column defaults
8323                  */
8324                 if (hasdefaults)
8325                 {
8326                         AttrDefInfo *attrdefs;
8327                         int                     numDefaults;
8328
8329                         if (g_verbose)
8330                                 write_msg(NULL, "finding default expressions of table \"%s.%s\"\n",
8331                                                   tbinfo->dobj.namespace->dobj.name,
8332                                                   tbinfo->dobj.name);
8333
8334                         printfPQExpBuffer(q, "SELECT tableoid, oid, adnum, "
8335                                                           "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc "
8336                                                           "FROM pg_catalog.pg_attrdef "
8337                                                           "WHERE adrelid = '%u'::pg_catalog.oid",
8338                                                           tbinfo->dobj.catId.oid);
8339
8340                         res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
8341
8342                         numDefaults = PQntuples(res);
8343                         attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
8344
8345                         for (j = 0; j < numDefaults; j++)
8346                         {
8347                                 int                     adnum;
8348
8349                                 adnum = atoi(PQgetvalue(res, j, 2));
8350
8351                                 if (adnum <= 0 || adnum > ntups)
8352                                         exit_horribly(NULL,
8353                                                                   "invalid adnum value %d for table \"%s\"\n",
8354                                                                   adnum, tbinfo->dobj.name);
8355
8356                                 /*
8357                                  * dropped columns shouldn't have defaults, but just in case,
8358                                  * ignore 'em
8359                                  */
8360                                 if (tbinfo->attisdropped[adnum - 1])
8361                                         continue;
8362
8363                                 attrdefs[j].dobj.objType = DO_ATTRDEF;
8364                                 attrdefs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
8365                                 attrdefs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
8366                                 AssignDumpId(&attrdefs[j].dobj);
8367                                 attrdefs[j].adtable = tbinfo;
8368                                 attrdefs[j].adnum = adnum;
8369                                 attrdefs[j].adef_expr = pg_strdup(PQgetvalue(res, j, 3));
8370
8371                                 attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name);
8372                                 attrdefs[j].dobj.namespace = tbinfo->dobj.namespace;
8373
8374                                 attrdefs[j].dobj.dump = tbinfo->dobj.dump;
8375
8376                                 /*
8377                                  * Defaults on a VIEW must always be dumped as separate ALTER
8378                                  * TABLE commands.  Defaults on regular tables are dumped as
8379                                  * part of the CREATE TABLE if possible, which it won't be if
8380                                  * the column is not going to be emitted explicitly.
8381                                  */
8382                                 if (tbinfo->relkind == RELKIND_VIEW)
8383                                 {
8384                                         attrdefs[j].separate = true;
8385                                 }
8386                                 else if (!shouldPrintColumn(dopt, tbinfo, adnum - 1))
8387                                 {
8388                                         /* column will be suppressed, print default separately */
8389                                         attrdefs[j].separate = true;
8390                                 }
8391                                 else
8392                                 {
8393                                         attrdefs[j].separate = false;
8394
8395                                         /*
8396                                          * Mark the default as needing to appear before the table,
8397                                          * so that any dependencies it has must be emitted before
8398                                          * the CREATE TABLE.  If this is not possible, we'll
8399                                          * change to "separate" mode while sorting dependencies.
8400                                          */
8401                                         addObjectDependency(&tbinfo->dobj,
8402                                                                                 attrdefs[j].dobj.dumpId);
8403                                 }
8404
8405                                 tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
8406                         }
8407                         PQclear(res);
8408                 }
8409
8410                 /*
8411                  * Get info about table CHECK constraints
8412                  */
8413                 if (tbinfo->ncheck > 0)
8414                 {
8415                         ConstraintInfo *constrs;
8416                         int                     numConstrs;
8417
8418                         if (g_verbose)
8419                                 write_msg(NULL, "finding check constraints for table \"%s.%s\"\n",
8420                                                   tbinfo->dobj.namespace->dobj.name,
8421                                                   tbinfo->dobj.name);
8422
8423                         resetPQExpBuffer(q);
8424                         if (fout->remoteVersion >= 90200)
8425                         {
8426                                 /*
8427                                  * convalidated is new in 9.2 (actually, it is there in 9.1,
8428                                  * but it wasn't ever false for check constraints until 9.2).
8429                                  */
8430                                 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
8431                                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8432                                                                   "conislocal, convalidated "
8433                                                                   "FROM pg_catalog.pg_constraint "
8434                                                                   "WHERE conrelid = '%u'::pg_catalog.oid "
8435                                                                   "   AND contype = 'c' "
8436                                                                   "ORDER BY conname",
8437                                                                   tbinfo->dobj.catId.oid);
8438                         }
8439                         else if (fout->remoteVersion >= 80400)
8440                         {
8441                                 /* conislocal is new in 8.4 */
8442                                 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
8443                                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8444                                                                   "conislocal, true AS convalidated "
8445                                                                   "FROM pg_catalog.pg_constraint "
8446                                                                   "WHERE conrelid = '%u'::pg_catalog.oid "
8447                                                                   "   AND contype = 'c' "
8448                                                                   "ORDER BY conname",
8449                                                                   tbinfo->dobj.catId.oid);
8450                         }
8451                         else
8452                         {
8453                                 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
8454                                                                   "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
8455                                                                   "true AS conislocal, true AS convalidated "
8456                                                                   "FROM pg_catalog.pg_constraint "
8457                                                                   "WHERE conrelid = '%u'::pg_catalog.oid "
8458                                                                   "   AND contype = 'c' "
8459                                                                   "ORDER BY conname",
8460                                                                   tbinfo->dobj.catId.oid);
8461                         }
8462
8463                         res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
8464
8465                         numConstrs = PQntuples(res);
8466                         if (numConstrs != tbinfo->ncheck)
8467                         {
8468                                 write_msg(NULL, ngettext("expected %d check constraint on table \"%s\" but found %d\n",
8469                                                                                  "expected %d check constraints on table \"%s\" but found %d\n",
8470                                                                                  tbinfo->ncheck),
8471                                                   tbinfo->ncheck, tbinfo->dobj.name, numConstrs);
8472                                 write_msg(NULL, "(The system catalogs might be corrupted.)\n");
8473                                 exit_nicely(1);
8474                         }
8475
8476                         constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo));
8477                         tbinfo->checkexprs = constrs;
8478
8479                         for (j = 0; j < numConstrs; j++)
8480                         {
8481                                 bool            validated = PQgetvalue(res, j, 5)[0] == 't';
8482
8483                                 constrs[j].dobj.objType = DO_CONSTRAINT;
8484                                 constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
8485                                 constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
8486                                 AssignDumpId(&constrs[j].dobj);
8487                                 constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, 2));
8488                                 constrs[j].dobj.namespace = tbinfo->dobj.namespace;
8489                                 constrs[j].contable = tbinfo;
8490                                 constrs[j].condomain = NULL;
8491                                 constrs[j].contype = 'c';
8492                                 constrs[j].condef = pg_strdup(PQgetvalue(res, j, 3));
8493                                 constrs[j].confrelid = InvalidOid;
8494                                 constrs[j].conindex = 0;
8495                                 constrs[j].condeferrable = false;
8496                                 constrs[j].condeferred = false;
8497                                 constrs[j].conislocal = (PQgetvalue(res, j, 4)[0] == 't');
8498
8499                                 /*
8500                                  * An unvalidated constraint needs to be dumped separately, so
8501                                  * that potentially-violating existing data is loaded before
8502                                  * the constraint.
8503                                  */
8504                                 constrs[j].separate = !validated;
8505
8506                                 constrs[j].dobj.dump = tbinfo->dobj.dump;
8507
8508                                 /*
8509                                  * Mark the constraint as needing to appear before the table
8510                                  * --- this is so that any other dependencies of the
8511                                  * constraint will be emitted before we try to create the
8512                                  * table.  If the constraint is to be dumped separately, it
8513                                  * will be dumped after data is loaded anyway, so don't do it.
8514                                  * (There's an automatic dependency in the opposite direction
8515                                  * anyway, so don't need to add one manually here.)
8516                                  */
8517                                 if (!constrs[j].separate)
8518                                         addObjectDependency(&tbinfo->dobj,
8519                                                                                 constrs[j].dobj.dumpId);
8520
8521                                 /*
8522                                  * If the constraint is inherited, this will be detected later
8523                                  * (in pre-8.4 databases).  We also detect later if the
8524                                  * constraint must be split out from the table definition.
8525                                  */
8526                         }
8527                         PQclear(res);
8528                 }
8529         }
8530
8531         destroyPQExpBuffer(q);
8532 }
8533
8534 /*
8535  * Test whether a column should be printed as part of table's CREATE TABLE.
8536  * Column number is zero-based.
8537  *
8538  * Normally this is always true, but it's false for dropped columns, as well
8539  * as those that were inherited without any local definition.  (If we print
8540  * such a column it will mistakenly get pg_attribute.attislocal set to true.)
8541  * However, in binary_upgrade mode, we must print all such columns anyway and
8542  * fix the attislocal/attisdropped state later, so as to keep control of the
8543  * physical column order.
8544  *
8545  * This function exists because there are scattered nonobvious places that
8546  * must be kept in sync with this decision.
8547  */
8548 bool
8549 shouldPrintColumn(DumpOptions *dopt, TableInfo *tbinfo, int colno)
8550 {
8551         if (dopt->binary_upgrade)
8552                 return true;
8553         return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
8554 }
8555
8556
8557 /*
8558  * getTSParsers:
8559  *        read all text search parsers in the system catalogs and return them
8560  *        in the TSParserInfo* structure
8561  *
8562  *      numTSParsers is set to the number of parsers read in
8563  */
8564 TSParserInfo *
8565 getTSParsers(Archive *fout, int *numTSParsers)
8566 {
8567         PGresult   *res;
8568         int                     ntups;
8569         int                     i;
8570         PQExpBuffer query;
8571         TSParserInfo *prsinfo;
8572         int                     i_tableoid;
8573         int                     i_oid;
8574         int                     i_prsname;
8575         int                     i_prsnamespace;
8576         int                     i_prsstart;
8577         int                     i_prstoken;
8578         int                     i_prsend;
8579         int                     i_prsheadline;
8580         int                     i_prslextype;
8581
8582         /* Before 8.3, there is no built-in text search support */
8583         if (fout->remoteVersion < 80300)
8584         {
8585                 *numTSParsers = 0;
8586                 return NULL;
8587         }
8588
8589         query = createPQExpBuffer();
8590
8591         /*
8592          * find all text search objects, including builtin ones; we filter out
8593          * system-defined objects at dump-out time.
8594          */
8595
8596         appendPQExpBufferStr(query, "SELECT tableoid, oid, prsname, prsnamespace, "
8597                                                  "prsstart::oid, prstoken::oid, "
8598                                                  "prsend::oid, prsheadline::oid, prslextype::oid "
8599                                                  "FROM pg_ts_parser");
8600
8601         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8602
8603         ntups = PQntuples(res);
8604         *numTSParsers = ntups;
8605
8606         prsinfo = (TSParserInfo *) pg_malloc(ntups * sizeof(TSParserInfo));
8607
8608         i_tableoid = PQfnumber(res, "tableoid");
8609         i_oid = PQfnumber(res, "oid");
8610         i_prsname = PQfnumber(res, "prsname");
8611         i_prsnamespace = PQfnumber(res, "prsnamespace");
8612         i_prsstart = PQfnumber(res, "prsstart");
8613         i_prstoken = PQfnumber(res, "prstoken");
8614         i_prsend = PQfnumber(res, "prsend");
8615         i_prsheadline = PQfnumber(res, "prsheadline");
8616         i_prslextype = PQfnumber(res, "prslextype");
8617
8618         for (i = 0; i < ntups; i++)
8619         {
8620                 prsinfo[i].dobj.objType = DO_TSPARSER;
8621                 prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8622                 prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8623                 AssignDumpId(&prsinfo[i].dobj);
8624                 prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
8625                 prsinfo[i].dobj.namespace =
8626                         findNamespace(fout,
8627                                                   atooid(PQgetvalue(res, i, i_prsnamespace)));
8628                 prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
8629                 prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
8630                 prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
8631                 prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline));
8632                 prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype));
8633
8634                 /* Decide whether we want to dump it */
8635                 selectDumpableObject(&(prsinfo[i].dobj), fout);
8636
8637                 /* Text Search Parsers do not currently have ACLs. */
8638                 prsinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8639         }
8640
8641         PQclear(res);
8642
8643         destroyPQExpBuffer(query);
8644
8645         return prsinfo;
8646 }
8647
8648 /*
8649  * getTSDictionaries:
8650  *        read all text search dictionaries in the system catalogs and return them
8651  *        in the TSDictInfo* structure
8652  *
8653  *      numTSDicts is set to the number of dictionaries read in
8654  */
8655 TSDictInfo *
8656 getTSDictionaries(Archive *fout, int *numTSDicts)
8657 {
8658         PGresult   *res;
8659         int                     ntups;
8660         int                     i;
8661         PQExpBuffer query;
8662         TSDictInfo *dictinfo;
8663         int                     i_tableoid;
8664         int                     i_oid;
8665         int                     i_dictname;
8666         int                     i_dictnamespace;
8667         int                     i_rolname;
8668         int                     i_dicttemplate;
8669         int                     i_dictinitoption;
8670
8671         /* Before 8.3, there is no built-in text search support */
8672         if (fout->remoteVersion < 80300)
8673         {
8674                 *numTSDicts = 0;
8675                 return NULL;
8676         }
8677
8678         query = createPQExpBuffer();
8679
8680         appendPQExpBuffer(query, "SELECT tableoid, oid, dictname, "
8681                                           "dictnamespace, (%s dictowner) AS rolname, "
8682                                           "dicttemplate, dictinitoption "
8683                                           "FROM pg_ts_dict",
8684                                           username_subquery);
8685
8686         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8687
8688         ntups = PQntuples(res);
8689         *numTSDicts = ntups;
8690
8691         dictinfo = (TSDictInfo *) pg_malloc(ntups * sizeof(TSDictInfo));
8692
8693         i_tableoid = PQfnumber(res, "tableoid");
8694         i_oid = PQfnumber(res, "oid");
8695         i_dictname = PQfnumber(res, "dictname");
8696         i_dictnamespace = PQfnumber(res, "dictnamespace");
8697         i_rolname = PQfnumber(res, "rolname");
8698         i_dictinitoption = PQfnumber(res, "dictinitoption");
8699         i_dicttemplate = PQfnumber(res, "dicttemplate");
8700
8701         for (i = 0; i < ntups; i++)
8702         {
8703                 dictinfo[i].dobj.objType = DO_TSDICT;
8704                 dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8705                 dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8706                 AssignDumpId(&dictinfo[i].dobj);
8707                 dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
8708                 dictinfo[i].dobj.namespace =
8709                         findNamespace(fout,
8710                                                   atooid(PQgetvalue(res, i, i_dictnamespace)));
8711                 dictinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
8712                 dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
8713                 if (PQgetisnull(res, i, i_dictinitoption))
8714                         dictinfo[i].dictinitoption = NULL;
8715                 else
8716                         dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption));
8717
8718                 /* Decide whether we want to dump it */
8719                 selectDumpableObject(&(dictinfo[i].dobj), fout);
8720
8721                 /* Text Search Dictionaries do not currently have ACLs. */
8722                 dictinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8723         }
8724
8725         PQclear(res);
8726
8727         destroyPQExpBuffer(query);
8728
8729         return dictinfo;
8730 }
8731
8732 /*
8733  * getTSTemplates:
8734  *        read all text search templates in the system catalogs and return them
8735  *        in the TSTemplateInfo* structure
8736  *
8737  *      numTSTemplates is set to the number of templates read in
8738  */
8739 TSTemplateInfo *
8740 getTSTemplates(Archive *fout, int *numTSTemplates)
8741 {
8742         PGresult   *res;
8743         int                     ntups;
8744         int                     i;
8745         PQExpBuffer query;
8746         TSTemplateInfo *tmplinfo;
8747         int                     i_tableoid;
8748         int                     i_oid;
8749         int                     i_tmplname;
8750         int                     i_tmplnamespace;
8751         int                     i_tmplinit;
8752         int                     i_tmpllexize;
8753
8754         /* Before 8.3, there is no built-in text search support */
8755         if (fout->remoteVersion < 80300)
8756         {
8757                 *numTSTemplates = 0;
8758                 return NULL;
8759         }
8760
8761         query = createPQExpBuffer();
8762
8763         appendPQExpBufferStr(query, "SELECT tableoid, oid, tmplname, "
8764                                                  "tmplnamespace, tmplinit::oid, tmpllexize::oid "
8765                                                  "FROM pg_ts_template");
8766
8767         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8768
8769         ntups = PQntuples(res);
8770         *numTSTemplates = ntups;
8771
8772         tmplinfo = (TSTemplateInfo *) pg_malloc(ntups * sizeof(TSTemplateInfo));
8773
8774         i_tableoid = PQfnumber(res, "tableoid");
8775         i_oid = PQfnumber(res, "oid");
8776         i_tmplname = PQfnumber(res, "tmplname");
8777         i_tmplnamespace = PQfnumber(res, "tmplnamespace");
8778         i_tmplinit = PQfnumber(res, "tmplinit");
8779         i_tmpllexize = PQfnumber(res, "tmpllexize");
8780
8781         for (i = 0; i < ntups; i++)
8782         {
8783                 tmplinfo[i].dobj.objType = DO_TSTEMPLATE;
8784                 tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8785                 tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8786                 AssignDumpId(&tmplinfo[i].dobj);
8787                 tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
8788                 tmplinfo[i].dobj.namespace =
8789                         findNamespace(fout,
8790                                                   atooid(PQgetvalue(res, i, i_tmplnamespace)));
8791                 tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
8792                 tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
8793
8794                 /* Decide whether we want to dump it */
8795                 selectDumpableObject(&(tmplinfo[i].dobj), fout);
8796
8797                 /* Text Search Templates do not currently have ACLs. */
8798                 tmplinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8799         }
8800
8801         PQclear(res);
8802
8803         destroyPQExpBuffer(query);
8804
8805         return tmplinfo;
8806 }
8807
8808 /*
8809  * getTSConfigurations:
8810  *        read all text search configurations in the system catalogs and return
8811  *        them in the TSConfigInfo* structure
8812  *
8813  *      numTSConfigs is set to the number of configurations read in
8814  */
8815 TSConfigInfo *
8816 getTSConfigurations(Archive *fout, int *numTSConfigs)
8817 {
8818         PGresult   *res;
8819         int                     ntups;
8820         int                     i;
8821         PQExpBuffer query;
8822         TSConfigInfo *cfginfo;
8823         int                     i_tableoid;
8824         int                     i_oid;
8825         int                     i_cfgname;
8826         int                     i_cfgnamespace;
8827         int                     i_rolname;
8828         int                     i_cfgparser;
8829
8830         /* Before 8.3, there is no built-in text search support */
8831         if (fout->remoteVersion < 80300)
8832         {
8833                 *numTSConfigs = 0;
8834                 return NULL;
8835         }
8836
8837         query = createPQExpBuffer();
8838
8839         appendPQExpBuffer(query, "SELECT tableoid, oid, cfgname, "
8840                                           "cfgnamespace, (%s cfgowner) AS rolname, cfgparser "
8841                                           "FROM pg_ts_config",
8842                                           username_subquery);
8843
8844         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8845
8846         ntups = PQntuples(res);
8847         *numTSConfigs = ntups;
8848
8849         cfginfo = (TSConfigInfo *) pg_malloc(ntups * sizeof(TSConfigInfo));
8850
8851         i_tableoid = PQfnumber(res, "tableoid");
8852         i_oid = PQfnumber(res, "oid");
8853         i_cfgname = PQfnumber(res, "cfgname");
8854         i_cfgnamespace = PQfnumber(res, "cfgnamespace");
8855         i_rolname = PQfnumber(res, "rolname");
8856         i_cfgparser = PQfnumber(res, "cfgparser");
8857
8858         for (i = 0; i < ntups; i++)
8859         {
8860                 cfginfo[i].dobj.objType = DO_TSCONFIG;
8861                 cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
8862                 cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
8863                 AssignDumpId(&cfginfo[i].dobj);
8864                 cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
8865                 cfginfo[i].dobj.namespace =
8866                         findNamespace(fout,
8867                                                   atooid(PQgetvalue(res, i, i_cfgnamespace)));
8868                 cfginfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
8869                 cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
8870
8871                 /* Decide whether we want to dump it */
8872                 selectDumpableObject(&(cfginfo[i].dobj), fout);
8873
8874                 /* Text Search Configurations do not currently have ACLs. */
8875                 cfginfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
8876         }
8877
8878         PQclear(res);
8879
8880         destroyPQExpBuffer(query);
8881
8882         return cfginfo;
8883 }
8884
8885 /*
8886  * getForeignDataWrappers:
8887  *        read all foreign-data wrappers in the system catalogs and return
8888  *        them in the FdwInfo* structure
8889  *
8890  *      numForeignDataWrappers is set to the number of fdws read in
8891  */
8892 FdwInfo *
8893 getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
8894 {
8895         DumpOptions *dopt = fout->dopt;
8896         PGresult   *res;
8897         int                     ntups;
8898         int                     i;
8899         PQExpBuffer query;
8900         FdwInfo    *fdwinfo;
8901         int                     i_tableoid;
8902         int                     i_oid;
8903         int                     i_fdwname;
8904         int                     i_rolname;
8905         int                     i_fdwhandler;
8906         int                     i_fdwvalidator;
8907         int                     i_fdwacl;
8908         int                     i_rfdwacl;
8909         int                     i_initfdwacl;
8910         int                     i_initrfdwacl;
8911         int                     i_fdwoptions;
8912
8913         /* Before 8.4, there are no foreign-data wrappers */
8914         if (fout->remoteVersion < 80400)
8915         {
8916                 *numForeignDataWrappers = 0;
8917                 return NULL;
8918         }
8919
8920         query = createPQExpBuffer();
8921
8922         if (fout->remoteVersion >= 90600)
8923         {
8924                 PQExpBuffer acl_subquery = createPQExpBuffer();
8925                 PQExpBuffer racl_subquery = createPQExpBuffer();
8926                 PQExpBuffer initacl_subquery = createPQExpBuffer();
8927                 PQExpBuffer initracl_subquery = createPQExpBuffer();
8928
8929                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
8930                                                 initracl_subquery, "f.fdwacl", "f.fdwowner", "'F'",
8931                                                 dopt->binary_upgrade);
8932
8933                 appendPQExpBuffer(query, "SELECT f.tableoid, f.oid, f.fdwname, "
8934                                                   "(%s f.fdwowner) AS rolname, "
8935                                                   "f.fdwhandler::pg_catalog.regproc, "
8936                                                   "f.fdwvalidator::pg_catalog.regproc, "
8937                                                   "%s AS fdwacl, "
8938                                                   "%s AS rfdwacl, "
8939                                                   "%s AS initfdwacl, "
8940                                                   "%s AS initrfdwacl, "
8941                                                   "array_to_string(ARRAY("
8942                                                   "SELECT quote_ident(option_name) || ' ' || "
8943                                                   "quote_literal(option_value) "
8944                                                   "FROM pg_options_to_table(f.fdwoptions) "
8945                                                   "ORDER BY option_name"
8946                                                   "), E',\n    ') AS fdwoptions "
8947                                                   "FROM pg_foreign_data_wrapper f "
8948                                                   "LEFT JOIN pg_init_privs pip ON "
8949                                                   "(f.oid = pip.objoid "
8950                                                   "AND pip.classoid = 'pg_foreign_data_wrapper'::regclass "
8951                                                   "AND pip.objsubid = 0) ",
8952                                                   username_subquery,
8953                                                   acl_subquery->data,
8954                                                   racl_subquery->data,
8955                                                   initacl_subquery->data,
8956                                                   initracl_subquery->data);
8957
8958                 destroyPQExpBuffer(acl_subquery);
8959                 destroyPQExpBuffer(racl_subquery);
8960                 destroyPQExpBuffer(initacl_subquery);
8961                 destroyPQExpBuffer(initracl_subquery);
8962         }
8963         else if (fout->remoteVersion >= 90100)
8964         {
8965                 appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
8966                                                   "(%s fdwowner) AS rolname, "
8967                                                   "fdwhandler::pg_catalog.regproc, "
8968                                                   "fdwvalidator::pg_catalog.regproc, fdwacl, "
8969                                                   "NULL as rfdwacl, "
8970                                                   "NULL as initfdwacl, NULL AS initrfdwacl, "
8971                                                   "array_to_string(ARRAY("
8972                                                   "SELECT quote_ident(option_name) || ' ' || "
8973                                                   "quote_literal(option_value) "
8974                                                   "FROM pg_options_to_table(fdwoptions) "
8975                                                   "ORDER BY option_name"
8976                                                   "), E',\n    ') AS fdwoptions "
8977                                                   "FROM pg_foreign_data_wrapper",
8978                                                   username_subquery);
8979         }
8980         else
8981         {
8982                 appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
8983                                                   "(%s fdwowner) AS rolname, "
8984                                                   "'-' AS fdwhandler, "
8985                                                   "fdwvalidator::pg_catalog.regproc, fdwacl, "
8986                                                   "NULL as rfdwacl, "
8987                                                   "NULL as initfdwacl, NULL AS initrfdwacl, "
8988                                                   "array_to_string(ARRAY("
8989                                                   "SELECT quote_ident(option_name) || ' ' || "
8990                                                   "quote_literal(option_value) "
8991                                                   "FROM pg_options_to_table(fdwoptions) "
8992                                                   "ORDER BY option_name"
8993                                                   "), E',\n    ') AS fdwoptions "
8994                                                   "FROM pg_foreign_data_wrapper",
8995                                                   username_subquery);
8996         }
8997
8998         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8999
9000         ntups = PQntuples(res);
9001         *numForeignDataWrappers = ntups;
9002
9003         fdwinfo = (FdwInfo *) pg_malloc(ntups * sizeof(FdwInfo));
9004
9005         i_tableoid = PQfnumber(res, "tableoid");
9006         i_oid = PQfnumber(res, "oid");
9007         i_fdwname = PQfnumber(res, "fdwname");
9008         i_rolname = PQfnumber(res, "rolname");
9009         i_fdwhandler = PQfnumber(res, "fdwhandler");
9010         i_fdwvalidator = PQfnumber(res, "fdwvalidator");
9011         i_fdwacl = PQfnumber(res, "fdwacl");
9012         i_rfdwacl = PQfnumber(res, "rfdwacl");
9013         i_initfdwacl = PQfnumber(res, "initfdwacl");
9014         i_initrfdwacl = PQfnumber(res, "initrfdwacl");
9015         i_fdwoptions = PQfnumber(res, "fdwoptions");
9016
9017         for (i = 0; i < ntups; i++)
9018         {
9019                 fdwinfo[i].dobj.objType = DO_FDW;
9020                 fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9021                 fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9022                 AssignDumpId(&fdwinfo[i].dobj);
9023                 fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname));
9024                 fdwinfo[i].dobj.namespace = NULL;
9025                 fdwinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
9026                 fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler));
9027                 fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator));
9028                 fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions));
9029                 fdwinfo[i].fdwacl = pg_strdup(PQgetvalue(res, i, i_fdwacl));
9030                 fdwinfo[i].rfdwacl = pg_strdup(PQgetvalue(res, i, i_rfdwacl));
9031                 fdwinfo[i].initfdwacl = pg_strdup(PQgetvalue(res, i, i_initfdwacl));
9032                 fdwinfo[i].initrfdwacl = pg_strdup(PQgetvalue(res, i, i_initrfdwacl));
9033
9034                 /* Decide whether we want to dump it */
9035                 selectDumpableObject(&(fdwinfo[i].dobj), fout);
9036
9037                 /* Do not try to dump ACL if no ACL exists. */
9038                 if (PQgetisnull(res, i, i_fdwacl) && PQgetisnull(res, i, i_rfdwacl) &&
9039                         PQgetisnull(res, i, i_initfdwacl) &&
9040                         PQgetisnull(res, i, i_initrfdwacl))
9041                         fdwinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
9042         }
9043
9044         PQclear(res);
9045
9046         destroyPQExpBuffer(query);
9047
9048         return fdwinfo;
9049 }
9050
9051 /*
9052  * getForeignServers:
9053  *        read all foreign servers in the system catalogs and return
9054  *        them in the ForeignServerInfo * structure
9055  *
9056  *      numForeignServers is set to the number of servers read in
9057  */
9058 ForeignServerInfo *
9059 getForeignServers(Archive *fout, int *numForeignServers)
9060 {
9061         DumpOptions *dopt = fout->dopt;
9062         PGresult   *res;
9063         int                     ntups;
9064         int                     i;
9065         PQExpBuffer query;
9066         ForeignServerInfo *srvinfo;
9067         int                     i_tableoid;
9068         int                     i_oid;
9069         int                     i_srvname;
9070         int                     i_rolname;
9071         int                     i_srvfdw;
9072         int                     i_srvtype;
9073         int                     i_srvversion;
9074         int                     i_srvacl;
9075         int                     i_rsrvacl;
9076         int                     i_initsrvacl;
9077         int                     i_initrsrvacl;
9078         int                     i_srvoptions;
9079
9080         /* Before 8.4, there are no foreign servers */
9081         if (fout->remoteVersion < 80400)
9082         {
9083                 *numForeignServers = 0;
9084                 return NULL;
9085         }
9086
9087         query = createPQExpBuffer();
9088
9089         if (fout->remoteVersion >= 90600)
9090         {
9091                 PQExpBuffer acl_subquery = createPQExpBuffer();
9092                 PQExpBuffer racl_subquery = createPQExpBuffer();
9093                 PQExpBuffer initacl_subquery = createPQExpBuffer();
9094                 PQExpBuffer initracl_subquery = createPQExpBuffer();
9095
9096                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
9097                                                 initracl_subquery, "f.srvacl", "f.srvowner", "'S'",
9098                                                 dopt->binary_upgrade);
9099
9100                 appendPQExpBuffer(query, "SELECT f.tableoid, f.oid, f.srvname, "
9101                                                   "(%s f.srvowner) AS rolname, "
9102                                                   "f.srvfdw, f.srvtype, f.srvversion, "
9103                                                   "%s AS srvacl, "
9104                                                   "%s AS rsrvacl, "
9105                                                   "%s AS initsrvacl, "
9106                                                   "%s AS initrsrvacl, "
9107                                                   "array_to_string(ARRAY("
9108                                                   "SELECT quote_ident(option_name) || ' ' || "
9109                                                   "quote_literal(option_value) "
9110                                                   "FROM pg_options_to_table(f.srvoptions) "
9111                                                   "ORDER BY option_name"
9112                                                   "), E',\n    ') AS srvoptions "
9113                                                   "FROM pg_foreign_server f "
9114                                                   "LEFT JOIN pg_init_privs pip "
9115                                                   "ON (f.oid = pip.objoid "
9116                                                   "AND pip.classoid = 'pg_foreign_server'::regclass "
9117                                                   "AND pip.objsubid = 0) ",
9118                                                   username_subquery,
9119                                                   acl_subquery->data,
9120                                                   racl_subquery->data,
9121                                                   initacl_subquery->data,
9122                                                   initracl_subquery->data);
9123
9124                 destroyPQExpBuffer(acl_subquery);
9125                 destroyPQExpBuffer(racl_subquery);
9126                 destroyPQExpBuffer(initacl_subquery);
9127                 destroyPQExpBuffer(initracl_subquery);
9128         }
9129         else
9130         {
9131                 appendPQExpBuffer(query, "SELECT tableoid, oid, srvname, "
9132                                                   "(%s srvowner) AS rolname, "
9133                                                   "srvfdw, srvtype, srvversion, srvacl, "
9134                                                   "NULL AS rsrvacl, "
9135                                                   "NULL AS initsrvacl, NULL AS initrsrvacl, "
9136                                                   "array_to_string(ARRAY("
9137                                                   "SELECT quote_ident(option_name) || ' ' || "
9138                                                   "quote_literal(option_value) "
9139                                                   "FROM pg_options_to_table(srvoptions) "
9140                                                   "ORDER BY option_name"
9141                                                   "), E',\n    ') AS srvoptions "
9142                                                   "FROM pg_foreign_server",
9143                                                   username_subquery);
9144         }
9145
9146         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9147
9148         ntups = PQntuples(res);
9149         *numForeignServers = ntups;
9150
9151         srvinfo = (ForeignServerInfo *) pg_malloc(ntups * sizeof(ForeignServerInfo));
9152
9153         i_tableoid = PQfnumber(res, "tableoid");
9154         i_oid = PQfnumber(res, "oid");
9155         i_srvname = PQfnumber(res, "srvname");
9156         i_rolname = PQfnumber(res, "rolname");
9157         i_srvfdw = PQfnumber(res, "srvfdw");
9158         i_srvtype = PQfnumber(res, "srvtype");
9159         i_srvversion = PQfnumber(res, "srvversion");
9160         i_srvacl = PQfnumber(res, "srvacl");
9161         i_rsrvacl = PQfnumber(res, "rsrvacl");
9162         i_initsrvacl = PQfnumber(res, "initsrvacl");
9163         i_initrsrvacl = PQfnumber(res, "initrsrvacl");
9164         i_srvoptions = PQfnumber(res, "srvoptions");
9165
9166         for (i = 0; i < ntups; i++)
9167         {
9168                 srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
9169                 srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9170                 srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9171                 AssignDumpId(&srvinfo[i].dobj);
9172                 srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname));
9173                 srvinfo[i].dobj.namespace = NULL;
9174                 srvinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
9175                 srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw));
9176                 srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype));
9177                 srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion));
9178                 srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions));
9179                 srvinfo[i].srvacl = pg_strdup(PQgetvalue(res, i, i_srvacl));
9180                 srvinfo[i].rsrvacl = pg_strdup(PQgetvalue(res, i, i_rsrvacl));
9181                 srvinfo[i].initsrvacl = pg_strdup(PQgetvalue(res, i, i_initsrvacl));
9182                 srvinfo[i].initrsrvacl = pg_strdup(PQgetvalue(res, i, i_initrsrvacl));
9183
9184                 /* Decide whether we want to dump it */
9185                 selectDumpableObject(&(srvinfo[i].dobj), fout);
9186
9187                 /* Do not try to dump ACL if no ACL exists. */
9188                 if (PQgetisnull(res, i, i_srvacl) && PQgetisnull(res, i, i_rsrvacl) &&
9189                         PQgetisnull(res, i, i_initsrvacl) &&
9190                         PQgetisnull(res, i, i_initrsrvacl))
9191                         srvinfo[i].dobj.dump &= ~DUMP_COMPONENT_ACL;
9192         }
9193
9194         PQclear(res);
9195
9196         destroyPQExpBuffer(query);
9197
9198         return srvinfo;
9199 }
9200
9201 /*
9202  * getDefaultACLs:
9203  *        read all default ACL information in the system catalogs and return
9204  *        them in the DefaultACLInfo structure
9205  *
9206  *      numDefaultACLs is set to the number of ACLs read in
9207  */
9208 DefaultACLInfo *
9209 getDefaultACLs(Archive *fout, int *numDefaultACLs)
9210 {
9211         DumpOptions *dopt = fout->dopt;
9212         DefaultACLInfo *daclinfo;
9213         PQExpBuffer query;
9214         PGresult   *res;
9215         int                     i_oid;
9216         int                     i_tableoid;
9217         int                     i_defaclrole;
9218         int                     i_defaclnamespace;
9219         int                     i_defaclobjtype;
9220         int                     i_defaclacl;
9221         int                     i_rdefaclacl;
9222         int                     i_initdefaclacl;
9223         int                     i_initrdefaclacl;
9224         int                     i,
9225                                 ntups;
9226
9227         if (fout->remoteVersion < 90000)
9228         {
9229                 *numDefaultACLs = 0;
9230                 return NULL;
9231         }
9232
9233         query = createPQExpBuffer();
9234
9235         if (fout->remoteVersion >= 90600)
9236         {
9237                 PQExpBuffer acl_subquery = createPQExpBuffer();
9238                 PQExpBuffer racl_subquery = createPQExpBuffer();
9239                 PQExpBuffer initacl_subquery = createPQExpBuffer();
9240                 PQExpBuffer initracl_subquery = createPQExpBuffer();
9241
9242                 buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
9243                                                 initracl_subquery, "defaclacl", "defaclrole",
9244                                                 "CASE WHEN defaclobjtype = 'S' THEN 's' ELSE defaclobjtype END::\"char\"",
9245                                                 dopt->binary_upgrade);
9246
9247                 appendPQExpBuffer(query, "SELECT d.oid, d.tableoid, "
9248                                                   "(%s d.defaclrole) AS defaclrole, "
9249                                                   "d.defaclnamespace, "
9250                                                   "d.defaclobjtype, "
9251                                                   "%s AS defaclacl, "
9252                                                   "%s AS rdefaclacl, "
9253                                                   "%s AS initdefaclacl, "
9254                                                   "%s AS initrdefaclacl "
9255                                                   "FROM pg_default_acl d "
9256                                                   "LEFT JOIN pg_init_privs pip ON "
9257                                                   "(d.oid = pip.objoid "
9258                                                   "AND pip.classoid = 'pg_default_acl'::regclass "
9259                                                   "AND pip.objsubid = 0) ",
9260                                                   username_subquery,
9261                                                   acl_subquery->data,
9262                                                   racl_subquery->data,
9263                                                   initacl_subquery->data,
9264                                                   initracl_subquery->data);
9265         }
9266         else
9267         {
9268                 appendPQExpBuffer(query, "SELECT oid, tableoid, "
9269                                                   "(%s defaclrole) AS defaclrole, "
9270                                                   "defaclnamespace, "
9271                                                   "defaclobjtype, "
9272                                                   "defaclacl, "
9273                                                   "NULL AS rdefaclacl, "
9274                                                   "NULL AS initdefaclacl, "
9275                                                   "NULL AS initrdefaclacl "
9276                                                   "FROM pg_default_acl",
9277                                                   username_subquery);
9278         }
9279
9280         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9281
9282         ntups = PQntuples(res);
9283         *numDefaultACLs = ntups;
9284
9285         daclinfo = (DefaultACLInfo *) pg_malloc(ntups * sizeof(DefaultACLInfo));
9286
9287         i_oid = PQfnumber(res, "oid");
9288         i_tableoid = PQfnumber(res, "tableoid");
9289         i_defaclrole = PQfnumber(res, "defaclrole");
9290         i_defaclnamespace = PQfnumber(res, "defaclnamespace");
9291         i_defaclobjtype = PQfnumber(res, "defaclobjtype");
9292         i_defaclacl = PQfnumber(res, "defaclacl");
9293         i_rdefaclacl = PQfnumber(res, "rdefaclacl");
9294         i_initdefaclacl = PQfnumber(res, "initdefaclacl");
9295         i_initrdefaclacl = PQfnumber(res, "initrdefaclacl");
9296
9297         for (i = 0; i < ntups; i++)
9298         {
9299                 Oid                     nspid = atooid(PQgetvalue(res, i, i_defaclnamespace));
9300
9301                 daclinfo[i].dobj.objType = DO_DEFAULT_ACL;
9302                 daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
9303                 daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
9304                 AssignDumpId(&daclinfo[i].dobj);
9305                 /* cheesy ... is it worth coming up with a better object name? */
9306                 daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
9307
9308                 if (nspid != InvalidOid)
9309                         daclinfo[i].dobj.namespace = findNamespace(fout, nspid);
9310                 else
9311                         daclinfo[i].dobj.namespace = NULL;
9312
9313                 daclinfo[i].defaclrole = pg_strdup(PQgetvalue(res, i, i_defaclrole));
9314                 daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype));
9315                 daclinfo[i].defaclacl = pg_strdup(PQgetvalue(res, i, i_defaclacl));
9316                 daclinfo[i].rdefaclacl = pg_strdup(PQgetvalue(res, i, i_rdefaclacl));
9317                 daclinfo[i].initdefaclacl = pg_strdup(PQgetvalue(res, i, i_initdefaclacl));
9318                 daclinfo[i].initrdefaclacl = pg_strdup(PQgetvalue(res, i, i_initrdefaclacl));
9319
9320                 /* Decide whether we want to dump it */
9321                 selectDumpableDefaultACL(&(daclinfo[i]), dopt);
9322         }
9323
9324         PQclear(res);
9325
9326         destroyPQExpBuffer(query);
9327
9328         return daclinfo;
9329 }
9330
9331 /*
9332  * dumpComment --
9333  *
9334  * This routine is used to dump any comments associated with the
9335  * object handed to this routine. The routine takes the object type
9336  * and object name (ready to print, except for schema decoration), plus
9337  * the namespace and owner of the object (for labeling the ArchiveEntry),
9338  * plus catalog ID and subid which are the lookup key for pg_description,
9339  * plus the dump ID for the object (for setting a dependency).
9340  * If a matching pg_description entry is found, it is dumped.
9341  *
9342  * Note: in some cases, such as comments for triggers and rules, the "type"
9343  * string really looks like, e.g., "TRIGGER name ON".  This is a bit of a hack
9344  * but it doesn't seem worth complicating the API for all callers to make
9345  * it cleaner.
9346  *
9347  * Note: although this routine takes a dumpId for dependency purposes,
9348  * that purpose is just to mark the dependency in the emitted dump file
9349  * for possible future use by pg_restore.  We do NOT use it for determining
9350  * ordering of the comment in the dump file, because this routine is called
9351  * after dependency sorting occurs.  This routine should be called just after
9352  * calling ArchiveEntry() for the specified object.
9353  */
9354 static void
9355 dumpComment(Archive *fout, const char *type, const char *name,
9356                         const char *namespace, const char *owner,
9357                         CatalogId catalogId, int subid, DumpId dumpId)
9358 {
9359         DumpOptions *dopt = fout->dopt;
9360         CommentItem *comments;
9361         int                     ncomments;
9362
9363         /* do nothing, if --no-comments is supplied */
9364         if (dopt->no_comments)
9365                 return;
9366
9367         /* Comments are schema not data ... except blob comments are data */
9368         if (strcmp(type, "LARGE OBJECT") != 0)
9369         {
9370                 if (dopt->dataOnly)
9371                         return;
9372         }
9373         else
9374         {
9375                 /* We do dump blob comments in binary-upgrade mode */
9376                 if (dopt->schemaOnly && !dopt->binary_upgrade)
9377                         return;
9378         }
9379
9380         /* Search for comments associated with catalogId, using table */
9381         ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
9382                                                          &comments);
9383
9384         /* Is there one matching the subid? */
9385         while (ncomments > 0)
9386         {
9387                 if (comments->objsubid == subid)
9388                         break;
9389                 comments++;
9390                 ncomments--;
9391         }
9392
9393         /* If a comment exists, build COMMENT ON statement */
9394         if (ncomments > 0)
9395         {
9396                 PQExpBuffer query = createPQExpBuffer();
9397                 PQExpBuffer tag = createPQExpBuffer();
9398
9399                 appendPQExpBuffer(query, "COMMENT ON %s ", type);
9400                 if (namespace && *namespace)
9401                         appendPQExpBuffer(query, "%s.", fmtId(namespace));
9402                 appendPQExpBuffer(query, "%s IS ", name);
9403                 appendStringLiteralAH(query, comments->descr, fout);
9404                 appendPQExpBufferStr(query, ";\n");
9405
9406                 appendPQExpBuffer(tag, "%s %s", type, name);
9407
9408                 /*
9409                  * We mark comments as SECTION_NONE because they really belong in the
9410                  * same section as their parent, whether that is pre-data or
9411                  * post-data.
9412                  */
9413                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
9414                                          tag->data, namespace, NULL, owner,
9415                                          false, "COMMENT", SECTION_NONE,
9416                                          query->data, "", NULL,
9417                                          &(dumpId), 1,
9418                                          NULL, NULL);
9419
9420                 destroyPQExpBuffer(query);
9421                 destroyPQExpBuffer(tag);
9422         }
9423 }
9424
9425 /*
9426  * dumpTableComment --
9427  *
9428  * As above, but dump comments for both the specified table (or view)
9429  * and its columns.
9430  */
9431 static void
9432 dumpTableComment(Archive *fout, TableInfo *tbinfo,
9433                                  const char *reltypename)
9434 {
9435         DumpOptions *dopt = fout->dopt;
9436         CommentItem *comments;
9437         int                     ncomments;
9438         PQExpBuffer query;
9439         PQExpBuffer tag;
9440
9441         /* do nothing, if --no-comments is supplied */
9442         if (dopt->no_comments)
9443                 return;
9444
9445         /* Comments are SCHEMA not data */
9446         if (dopt->dataOnly)
9447                 return;
9448
9449         /* Search for comments associated with relation, using table */
9450         ncomments = findComments(fout,
9451                                                          tbinfo->dobj.catId.tableoid,
9452                                                          tbinfo->dobj.catId.oid,
9453                                                          &comments);
9454
9455         /* If comments exist, build COMMENT ON statements */
9456         if (ncomments <= 0)
9457                 return;
9458
9459         query = createPQExpBuffer();
9460         tag = createPQExpBuffer();
9461
9462         while (ncomments > 0)
9463         {
9464                 const char *descr = comments->descr;
9465                 int                     objsubid = comments->objsubid;
9466
9467                 if (objsubid == 0)
9468                 {
9469                         resetPQExpBuffer(tag);
9470                         appendPQExpBuffer(tag, "%s %s", reltypename,
9471                                                           fmtId(tbinfo->dobj.name));
9472
9473                         resetPQExpBuffer(query);
9474                         appendPQExpBuffer(query, "COMMENT ON %s %s IS ", reltypename,
9475                                                           fmtQualifiedDumpable(tbinfo));
9476                         appendStringLiteralAH(query, descr, fout);
9477                         appendPQExpBufferStr(query, ";\n");
9478
9479                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
9480                                                  tag->data,
9481                                                  tbinfo->dobj.namespace->dobj.name,
9482                                                  NULL, tbinfo->rolname,
9483                                                  false, "COMMENT", SECTION_NONE,
9484                                                  query->data, "", NULL,
9485                                                  &(tbinfo->dobj.dumpId), 1,
9486                                                  NULL, NULL);
9487                 }
9488                 else if (objsubid > 0 && objsubid <= tbinfo->numatts)
9489                 {
9490                         resetPQExpBuffer(tag);
9491                         appendPQExpBuffer(tag, "COLUMN %s.",
9492                                                           fmtId(tbinfo->dobj.name));
9493                         appendPQExpBufferStr(tag, fmtId(tbinfo->attnames[objsubid - 1]));
9494
9495                         resetPQExpBuffer(query);
9496                         appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
9497                                                           fmtQualifiedDumpable(tbinfo));
9498                         appendPQExpBuffer(query, "%s IS ",
9499                                                           fmtId(tbinfo->attnames[objsubid - 1]));
9500                         appendStringLiteralAH(query, descr, fout);
9501                         appendPQExpBufferStr(query, ";\n");
9502
9503                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
9504                                                  tag->data,
9505                                                  tbinfo->dobj.namespace->dobj.name,
9506                                                  NULL, tbinfo->rolname,
9507                                                  false, "COMMENT", SECTION_NONE,
9508                                                  query->data, "", NULL,
9509                                                  &(tbinfo->dobj.dumpId), 1,
9510                                                  NULL, NULL);
9511                 }
9512
9513                 comments++;
9514                 ncomments--;
9515         }
9516
9517         destroyPQExpBuffer(query);
9518         destroyPQExpBuffer(tag);
9519 }
9520
9521 /*
9522  * findComments --
9523  *
9524  * Find the comment(s), if any, associated with the given object.  All the
9525  * objsubid values associated with the given classoid/objoid are found with
9526  * one search.
9527  */
9528 static int
9529 findComments(Archive *fout, Oid classoid, Oid objoid,
9530                          CommentItem **items)
9531 {
9532         /* static storage for table of comments */
9533         static CommentItem *comments = NULL;
9534         static int      ncomments = -1;
9535
9536         CommentItem *middle = NULL;
9537         CommentItem *low;
9538         CommentItem *high;
9539         int                     nmatch;
9540
9541         /* Get comments if we didn't already */
9542         if (ncomments < 0)
9543                 ncomments = collectComments(fout, &comments);
9544
9545         /*
9546          * Do binary search to find some item matching the object.
9547          */
9548         low = &comments[0];
9549         high = &comments[ncomments - 1];
9550         while (low <= high)
9551         {
9552                 middle = low + (high - low) / 2;
9553
9554                 if (classoid < middle->classoid)
9555                         high = middle - 1;
9556                 else if (classoid > middle->classoid)
9557                         low = middle + 1;
9558                 else if (objoid < middle->objoid)
9559                         high = middle - 1;
9560                 else if (objoid > middle->objoid)
9561                         low = middle + 1;
9562                 else
9563                         break;                          /* found a match */
9564         }
9565
9566         if (low > high)                         /* no matches */
9567         {
9568                 *items = NULL;
9569                 return 0;
9570         }
9571
9572         /*
9573          * Now determine how many items match the object.  The search loop
9574          * invariant still holds: only items between low and high inclusive could
9575          * match.
9576          */
9577         nmatch = 1;
9578         while (middle > low)
9579         {
9580                 if (classoid != middle[-1].classoid ||
9581                         objoid != middle[-1].objoid)
9582                         break;
9583                 middle--;
9584                 nmatch++;
9585         }
9586
9587         *items = middle;
9588
9589         middle += nmatch;
9590         while (middle <= high)
9591         {
9592                 if (classoid != middle->classoid ||
9593                         objoid != middle->objoid)
9594                         break;
9595                 middle++;
9596                 nmatch++;
9597         }
9598
9599         return nmatch;
9600 }
9601
9602 /*
9603  * collectComments --
9604  *
9605  * Construct a table of all comments available for database objects.
9606  * We used to do per-object queries for the comments, but it's much faster
9607  * to pull them all over at once, and on most databases the memory cost
9608  * isn't high.
9609  *
9610  * The table is sorted by classoid/objid/objsubid for speed in lookup.
9611  */
9612 static int
9613 collectComments(Archive *fout, CommentItem **items)
9614 {
9615         PGresult   *res;
9616         PQExpBuffer query;
9617         int                     i_description;
9618         int                     i_classoid;
9619         int                     i_objoid;
9620         int                     i_objsubid;
9621         int                     ntups;
9622         int                     i;
9623         CommentItem *comments;
9624
9625         query = createPQExpBuffer();
9626
9627         appendPQExpBufferStr(query, "SELECT description, classoid, objoid, objsubid "
9628                                                  "FROM pg_catalog.pg_description "
9629                                                  "ORDER BY classoid, objoid, objsubid");
9630
9631         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9632
9633         /* Construct lookup table containing OIDs in numeric form */
9634
9635         i_description = PQfnumber(res, "description");
9636         i_classoid = PQfnumber(res, "classoid");
9637         i_objoid = PQfnumber(res, "objoid");
9638         i_objsubid = PQfnumber(res, "objsubid");
9639
9640         ntups = PQntuples(res);
9641
9642         comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
9643
9644         for (i = 0; i < ntups; i++)
9645         {
9646                 comments[i].descr = PQgetvalue(res, i, i_description);
9647                 comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
9648                 comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
9649                 comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
9650         }
9651
9652         /* Do NOT free the PGresult since we are keeping pointers into it */
9653         destroyPQExpBuffer(query);
9654
9655         *items = comments;
9656         return ntups;
9657 }
9658
9659 /*
9660  * dumpDumpableObject
9661  *
9662  * This routine and its subsidiaries are responsible for creating
9663  * ArchiveEntries (TOC objects) for each object to be dumped.
9664  */
9665 static void
9666 dumpDumpableObject(Archive *fout, DumpableObject *dobj)
9667 {
9668         switch (dobj->objType)
9669         {
9670                 case DO_NAMESPACE:
9671                         dumpNamespace(fout, (NamespaceInfo *) dobj);
9672                         break;
9673                 case DO_EXTENSION:
9674                         dumpExtension(fout, (ExtensionInfo *) dobj);
9675                         break;
9676                 case DO_TYPE:
9677                         dumpType(fout, (TypeInfo *) dobj);
9678                         break;
9679                 case DO_SHELL_TYPE:
9680                         dumpShellType(fout, (ShellTypeInfo *) dobj);
9681                         break;
9682                 case DO_FUNC:
9683                         dumpFunc(fout, (FuncInfo *) dobj);
9684                         break;
9685                 case DO_AGG:
9686                         dumpAgg(fout, (AggInfo *) dobj);
9687                         break;
9688                 case DO_OPERATOR:
9689                         dumpOpr(fout, (OprInfo *) dobj);
9690                         break;
9691                 case DO_ACCESS_METHOD:
9692                         dumpAccessMethod(fout, (AccessMethodInfo *) dobj);
9693                         break;
9694                 case DO_OPCLASS:
9695                         dumpOpclass(fout, (OpclassInfo *) dobj);
9696                         break;
9697                 case DO_OPFAMILY:
9698                         dumpOpfamily(fout, (OpfamilyInfo *) dobj);
9699                         break;
9700                 case DO_COLLATION:
9701                         dumpCollation(fout, (CollInfo *) dobj);
9702                         break;
9703                 case DO_CONVERSION:
9704                         dumpConversion(fout, (ConvInfo *) dobj);
9705                         break;
9706                 case DO_TABLE:
9707                         dumpTable(fout, (TableInfo *) dobj);
9708                         break;
9709                 case DO_ATTRDEF:
9710                         dumpAttrDef(fout, (AttrDefInfo *) dobj);
9711                         break;
9712                 case DO_INDEX:
9713                         dumpIndex(fout, (IndxInfo *) dobj);
9714                         break;
9715                 case DO_INDEX_ATTACH:
9716                         dumpIndexAttach(fout, (IndexAttachInfo *) dobj);
9717                         break;
9718                 case DO_STATSEXT:
9719                         dumpStatisticsExt(fout, (StatsExtInfo *) dobj);
9720                         break;
9721                 case DO_REFRESH_MATVIEW:
9722                         refreshMatViewData(fout, (TableDataInfo *) dobj);
9723                         break;
9724                 case DO_RULE:
9725                         dumpRule(fout, (RuleInfo *) dobj);
9726                         break;
9727                 case DO_TRIGGER:
9728                         dumpTrigger(fout, (TriggerInfo *) dobj);
9729                         break;
9730                 case DO_EVENT_TRIGGER:
9731                         dumpEventTrigger(fout, (EventTriggerInfo *) dobj);
9732                         break;
9733                 case DO_CONSTRAINT:
9734                         dumpConstraint(fout, (ConstraintInfo *) dobj);
9735                         break;
9736                 case DO_FK_CONSTRAINT:
9737                         dumpConstraint(fout, (ConstraintInfo *) dobj);
9738                         break;
9739                 case DO_PROCLANG:
9740                         dumpProcLang(fout, (ProcLangInfo *) dobj);
9741                         break;
9742                 case DO_CAST:
9743                         dumpCast(fout, (CastInfo *) dobj);
9744                         break;
9745                 case DO_TRANSFORM:
9746                         dumpTransform(fout, (TransformInfo *) dobj);
9747                         break;
9748                 case DO_SEQUENCE_SET:
9749                         dumpSequenceData(fout, (TableDataInfo *) dobj);
9750                         break;
9751                 case DO_TABLE_DATA:
9752                         dumpTableData(fout, (TableDataInfo *) dobj);
9753                         break;
9754                 case DO_DUMMY_TYPE:
9755                         /* table rowtypes and array types are never dumped separately */
9756                         break;
9757                 case DO_TSPARSER:
9758                         dumpTSParser(fout, (TSParserInfo *) dobj);
9759                         break;
9760                 case DO_TSDICT:
9761                         dumpTSDictionary(fout, (TSDictInfo *) dobj);
9762                         break;
9763                 case DO_TSTEMPLATE:
9764                         dumpTSTemplate(fout, (TSTemplateInfo *) dobj);
9765                         break;
9766                 case DO_TSCONFIG:
9767                         dumpTSConfig(fout, (TSConfigInfo *) dobj);
9768                         break;
9769                 case DO_FDW:
9770                         dumpForeignDataWrapper(fout, (FdwInfo *) dobj);
9771                         break;
9772                 case DO_FOREIGN_SERVER:
9773                         dumpForeignServer(fout, (ForeignServerInfo *) dobj);
9774                         break;
9775                 case DO_DEFAULT_ACL:
9776                         dumpDefaultACL(fout, (DefaultACLInfo *) dobj);
9777                         break;
9778                 case DO_BLOB:
9779                         dumpBlob(fout, (BlobInfo *) dobj);
9780                         break;
9781                 case DO_BLOB_DATA:
9782                         if (dobj->dump & DUMP_COMPONENT_DATA)
9783                                 ArchiveEntry(fout, dobj->catId, dobj->dumpId,
9784                                                          dobj->name, NULL, NULL, "",
9785                                                          false, "BLOBS", SECTION_DATA,
9786                                                          "", "", NULL,
9787                                                          NULL, 0,
9788                                                          dumpBlobs, NULL);
9789                         break;
9790                 case DO_POLICY:
9791                         dumpPolicy(fout, (PolicyInfo *) dobj);
9792                         break;
9793                 case DO_PUBLICATION:
9794                         dumpPublication(fout, (PublicationInfo *) dobj);
9795                         break;
9796                 case DO_PUBLICATION_REL:
9797                         dumpPublicationTable(fout, (PublicationRelInfo *) dobj);
9798                         break;
9799                 case DO_SUBSCRIPTION:
9800                         dumpSubscription(fout, (SubscriptionInfo *) dobj);
9801                         break;
9802                 case DO_PRE_DATA_BOUNDARY:
9803                 case DO_POST_DATA_BOUNDARY:
9804                         /* never dumped, nothing to do */
9805                         break;
9806         }
9807 }
9808
9809 /*
9810  * dumpNamespace
9811  *        writes out to fout the queries to recreate a user-defined namespace
9812  */
9813 static void
9814 dumpNamespace(Archive *fout, NamespaceInfo *nspinfo)
9815 {
9816         DumpOptions *dopt = fout->dopt;
9817         PQExpBuffer q;
9818         PQExpBuffer delq;
9819         char       *qnspname;
9820
9821         /* Skip if not to be dumped */
9822         if (!nspinfo->dobj.dump || dopt->dataOnly)
9823                 return;
9824
9825         q = createPQExpBuffer();
9826         delq = createPQExpBuffer();
9827
9828         qnspname = pg_strdup(fmtId(nspinfo->dobj.name));
9829
9830         appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
9831
9832         appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname);
9833
9834         if (dopt->binary_upgrade)
9835                 binary_upgrade_extension_member(q, &nspinfo->dobj,
9836                                                                                 "SCHEMA", qnspname, NULL);
9837
9838         if (nspinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
9839                 ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
9840                                          nspinfo->dobj.name,
9841                                          NULL, NULL,
9842                                          nspinfo->rolname,
9843                                          false, "SCHEMA", SECTION_PRE_DATA,
9844                                          q->data, delq->data, NULL,
9845                                          NULL, 0,
9846                                          NULL, NULL);
9847
9848         /* Dump Schema Comments and Security Labels */
9849         if (nspinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
9850                 dumpComment(fout, "SCHEMA", qnspname,
9851                                         NULL, nspinfo->rolname,
9852                                         nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
9853
9854         if (nspinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
9855                 dumpSecLabel(fout, "SCHEMA", qnspname,
9856                                          NULL, nspinfo->rolname,
9857                                          nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
9858
9859         if (nspinfo->dobj.dump & DUMP_COMPONENT_ACL)
9860                 dumpACL(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId, "SCHEMA",
9861                                 qnspname, NULL, NULL,
9862                                 nspinfo->rolname, nspinfo->nspacl, nspinfo->rnspacl,
9863                                 nspinfo->initnspacl, nspinfo->initrnspacl);
9864
9865         free(qnspname);
9866
9867         destroyPQExpBuffer(q);
9868         destroyPQExpBuffer(delq);
9869 }
9870
9871 /*
9872  * dumpExtension
9873  *        writes out to fout the queries to recreate an extension
9874  */
9875 static void
9876 dumpExtension(Archive *fout, ExtensionInfo *extinfo)
9877 {
9878         DumpOptions *dopt = fout->dopt;
9879         PQExpBuffer q;
9880         PQExpBuffer delq;
9881         char       *qextname;
9882
9883         /* Skip if not to be dumped */
9884         if (!extinfo->dobj.dump || dopt->dataOnly)
9885                 return;
9886
9887         q = createPQExpBuffer();
9888         delq = createPQExpBuffer();
9889
9890         qextname = pg_strdup(fmtId(extinfo->dobj.name));
9891
9892         appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname);
9893
9894         if (!dopt->binary_upgrade)
9895         {
9896                 /*
9897                  * In a regular dump, we simply create the extension, intentionally
9898                  * not specifying a version, so that the destination installation's
9899                  * default version is used.
9900                  *
9901                  * Use of IF NOT EXISTS here is unlike our behavior for other object
9902                  * types; but there are various scenarios in which it's convenient to
9903                  * manually create the desired extension before restoring, so we
9904                  * prefer to allow it to exist already.
9905                  */
9906                 appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n",
9907                                                   qextname, fmtId(extinfo->namespace));
9908         }
9909         else
9910         {
9911                 /*
9912                  * In binary-upgrade mode, it's critical to reproduce the state of the
9913                  * database exactly, so our procedure is to create an empty extension,
9914                  * restore all the contained objects normally, and add them to the
9915                  * extension one by one.  This function performs just the first of
9916                  * those steps.  binary_upgrade_extension_member() takes care of
9917                  * adding member objects as they're created.
9918                  */
9919                 int                     i;
9920                 int                     n;
9921
9922                 appendPQExpBufferStr(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
9923
9924                 /*
9925                  * We unconditionally create the extension, so we must drop it if it
9926                  * exists.  This could happen if the user deleted 'plpgsql' and then
9927                  * readded it, causing its oid to be greater than g_last_builtin_oid.
9928                  */
9929                 appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
9930
9931                 appendPQExpBufferStr(q,
9932                                                          "SELECT pg_catalog.binary_upgrade_create_empty_extension(");
9933                 appendStringLiteralAH(q, extinfo->dobj.name, fout);
9934                 appendPQExpBufferStr(q, ", ");
9935                 appendStringLiteralAH(q, extinfo->namespace, fout);
9936                 appendPQExpBufferStr(q, ", ");
9937                 appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false");
9938                 appendStringLiteralAH(q, extinfo->extversion, fout);
9939                 appendPQExpBufferStr(q, ", ");
9940
9941                 /*
9942                  * Note that we're pushing extconfig (an OID array) back into
9943                  * pg_extension exactly as-is.  This is OK because pg_class OIDs are
9944                  * preserved in binary upgrade.
9945                  */
9946                 if (strlen(extinfo->extconfig) > 2)
9947                         appendStringLiteralAH(q, extinfo->extconfig, fout);
9948                 else
9949                         appendPQExpBufferStr(q, "NULL");
9950                 appendPQExpBufferStr(q, ", ");
9951                 if (strlen(extinfo->extcondition) > 2)
9952                         appendStringLiteralAH(q, extinfo->extcondition, fout);
9953                 else
9954                         appendPQExpBufferStr(q, "NULL");
9955                 appendPQExpBufferStr(q, ", ");
9956                 appendPQExpBufferStr(q, "ARRAY[");
9957                 n = 0;
9958                 for (i = 0; i < extinfo->dobj.nDeps; i++)
9959                 {
9960                         DumpableObject *extobj;
9961
9962                         extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]);
9963                         if (extobj && extobj->objType == DO_EXTENSION)
9964                         {
9965                                 if (n++ > 0)
9966                                         appendPQExpBufferChar(q, ',');
9967                                 appendStringLiteralAH(q, extobj->name, fout);
9968                         }
9969                 }
9970                 appendPQExpBufferStr(q, "]::pg_catalog.text[]");
9971                 appendPQExpBufferStr(q, ");\n");
9972         }
9973
9974         if (extinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
9975                 ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId,
9976                                          extinfo->dobj.name,
9977                                          NULL, NULL,
9978                                          "",
9979                                          false, "EXTENSION", SECTION_PRE_DATA,
9980                                          q->data, delq->data, NULL,
9981                                          NULL, 0,
9982                                          NULL, NULL);
9983
9984         /* Dump Extension Comments and Security Labels */
9985         if (extinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
9986                 dumpComment(fout, "EXTENSION", qextname,
9987                                         NULL, "",
9988                                         extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
9989
9990         if (extinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
9991                 dumpSecLabel(fout, "EXTENSION", qextname,
9992                                          NULL, "",
9993                                          extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
9994
9995         free(qextname);
9996
9997         destroyPQExpBuffer(q);
9998         destroyPQExpBuffer(delq);
9999 }
10000
10001 /*
10002  * dumpType
10003  *        writes out to fout the queries to recreate a user-defined type
10004  */
10005 static void
10006 dumpType(Archive *fout, TypeInfo *tyinfo)
10007 {
10008         DumpOptions *dopt = fout->dopt;
10009
10010         /* Skip if not to be dumped */
10011         if (!tyinfo->dobj.dump || dopt->dataOnly)
10012                 return;
10013
10014         /* Dump out in proper style */
10015         if (tyinfo->typtype == TYPTYPE_BASE)
10016                 dumpBaseType(fout, tyinfo);
10017         else if (tyinfo->typtype == TYPTYPE_DOMAIN)
10018                 dumpDomain(fout, tyinfo);
10019         else if (tyinfo->typtype == TYPTYPE_COMPOSITE)
10020                 dumpCompositeType(fout, tyinfo);
10021         else if (tyinfo->typtype == TYPTYPE_ENUM)
10022                 dumpEnumType(fout, tyinfo);
10023         else if (tyinfo->typtype == TYPTYPE_RANGE)
10024                 dumpRangeType(fout, tyinfo);
10025         else if (tyinfo->typtype == TYPTYPE_PSEUDO && !tyinfo->isDefined)
10026                 dumpUndefinedType(fout, tyinfo);
10027         else
10028                 write_msg(NULL, "WARNING: typtype of data type \"%s\" appears to be invalid\n",
10029                                   tyinfo->dobj.name);
10030 }
10031
10032 /*
10033  * dumpEnumType
10034  *        writes out to fout the queries to recreate a user-defined enum type
10035  */
10036 static void
10037 dumpEnumType(Archive *fout, TypeInfo *tyinfo)
10038 {
10039         DumpOptions *dopt = fout->dopt;
10040         PQExpBuffer q = createPQExpBuffer();
10041         PQExpBuffer delq = createPQExpBuffer();
10042         PQExpBuffer query = createPQExpBuffer();
10043         PGresult   *res;
10044         int                     num,
10045                                 i;
10046         Oid                     enum_oid;
10047         char       *qtypname;
10048         char       *qualtypname;
10049         char       *label;
10050
10051         if (fout->remoteVersion >= 90100)
10052                 appendPQExpBuffer(query, "SELECT oid, enumlabel "
10053                                                   "FROM pg_catalog.pg_enum "
10054                                                   "WHERE enumtypid = '%u'"
10055                                                   "ORDER BY enumsortorder",
10056                                                   tyinfo->dobj.catId.oid);
10057         else
10058                 appendPQExpBuffer(query, "SELECT oid, enumlabel "
10059                                                   "FROM pg_catalog.pg_enum "
10060                                                   "WHERE enumtypid = '%u'"
10061                                                   "ORDER BY oid",
10062                                                   tyinfo->dobj.catId.oid);
10063
10064         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10065
10066         num = PQntuples(res);
10067
10068         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10069         qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10070
10071         /*
10072          * CASCADE shouldn't be required here as for normal types since the I/O
10073          * functions are generic and do not get dropped.
10074          */
10075         appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
10076
10077         if (dopt->binary_upgrade)
10078                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10079                                                                                                  tyinfo->dobj.catId.oid,
10080                                                                                                  false);
10081
10082         appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
10083                                           qualtypname);
10084
10085         if (!dopt->binary_upgrade)
10086         {
10087                 /* Labels with server-assigned oids */
10088                 for (i = 0; i < num; i++)
10089                 {
10090                         label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
10091                         if (i > 0)
10092                                 appendPQExpBufferChar(q, ',');
10093                         appendPQExpBufferStr(q, "\n    ");
10094                         appendStringLiteralAH(q, label, fout);
10095                 }
10096         }
10097
10098         appendPQExpBufferStr(q, "\n);\n");
10099
10100         if (dopt->binary_upgrade)
10101         {
10102                 /* Labels with dump-assigned (preserved) oids */
10103                 for (i = 0; i < num; i++)
10104                 {
10105                         enum_oid = atooid(PQgetvalue(res, i, PQfnumber(res, "oid")));
10106                         label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
10107
10108                         if (i == 0)
10109                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
10110                         appendPQExpBuffer(q,
10111                                                           "SELECT pg_catalog.binary_upgrade_set_next_pg_enum_oid('%u'::pg_catalog.oid);\n",
10112                                                           enum_oid);
10113                         appendPQExpBuffer(q, "ALTER TYPE %s ADD VALUE ", qualtypname);
10114                         appendStringLiteralAH(q, label, fout);
10115                         appendPQExpBufferStr(q, ";\n\n");
10116                 }
10117         }
10118
10119         if (dopt->binary_upgrade)
10120                 binary_upgrade_extension_member(q, &tyinfo->dobj,
10121                                                                                 "TYPE", qtypname,
10122                                                                                 tyinfo->dobj.namespace->dobj.name);
10123
10124         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10125                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10126                                          tyinfo->dobj.name,
10127                                          tyinfo->dobj.namespace->dobj.name,
10128                                          NULL,
10129                                          tyinfo->rolname, false,
10130                                          "TYPE", SECTION_PRE_DATA,
10131                                          q->data, delq->data, NULL,
10132                                          NULL, 0,
10133                                          NULL, NULL);
10134
10135         /* Dump Type Comments and Security Labels */
10136         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10137                 dumpComment(fout, "TYPE", qtypname,
10138                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10139                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10140
10141         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10142                 dumpSecLabel(fout, "TYPE", qtypname,
10143                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10144                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10145
10146         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10147                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10148                                 qtypname, NULL,
10149                                 tyinfo->dobj.namespace->dobj.name,
10150                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10151                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10152
10153         PQclear(res);
10154         destroyPQExpBuffer(q);
10155         destroyPQExpBuffer(delq);
10156         destroyPQExpBuffer(query);
10157         free(qtypname);
10158         free(qualtypname);
10159 }
10160
10161 /*
10162  * dumpRangeType
10163  *        writes out to fout the queries to recreate a user-defined range type
10164  */
10165 static void
10166 dumpRangeType(Archive *fout, TypeInfo *tyinfo)
10167 {
10168         DumpOptions *dopt = fout->dopt;
10169         PQExpBuffer q = createPQExpBuffer();
10170         PQExpBuffer delq = createPQExpBuffer();
10171         PQExpBuffer query = createPQExpBuffer();
10172         PGresult   *res;
10173         Oid                     collationOid;
10174         char       *qtypname;
10175         char       *qualtypname;
10176         char       *procname;
10177
10178         appendPQExpBuffer(query,
10179                                           "SELECT pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
10180                                           "opc.opcname AS opcname, "
10181                                           "(SELECT nspname FROM pg_catalog.pg_namespace nsp "
10182                                           "  WHERE nsp.oid = opc.opcnamespace) AS opcnsp, "
10183                                           "opc.opcdefault, "
10184                                           "CASE WHEN rngcollation = st.typcollation THEN 0 "
10185                                           "     ELSE rngcollation END AS collation, "
10186                                           "rngcanonical, rngsubdiff "
10187                                           "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, "
10188                                           "     pg_catalog.pg_opclass opc "
10189                                           "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND "
10190                                           "rngtypid = '%u'",
10191                                           tyinfo->dobj.catId.oid);
10192
10193         res = ExecuteSqlQueryForSingleRow(fout, query->data);
10194
10195         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10196         qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10197
10198         /*
10199          * CASCADE shouldn't be required here as for normal types since the I/O
10200          * functions are generic and do not get dropped.
10201          */
10202         appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
10203
10204         if (dopt->binary_upgrade)
10205                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10206                                                                                                  tyinfo->dobj.catId.oid,
10207                                                                                                  false);
10208
10209         appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
10210                                           qualtypname);
10211
10212         appendPQExpBuffer(q, "\n    subtype = %s",
10213                                           PQgetvalue(res, 0, PQfnumber(res, "rngsubtype")));
10214
10215         /* print subtype_opclass only if not default for subtype */
10216         if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't')
10217         {
10218                 char       *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname"));
10219                 char       *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp"));
10220
10221                 appendPQExpBuffer(q, ",\n    subtype_opclass = %s.",
10222                                                   fmtId(nspname));
10223                 appendPQExpBufferStr(q, fmtId(opcname));
10224         }
10225
10226         collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation")));
10227         if (OidIsValid(collationOid))
10228         {
10229                 CollInfo   *coll = findCollationByOid(collationOid);
10230
10231                 if (coll)
10232                         appendPQExpBuffer(q, ",\n    collation = %s",
10233                                                           fmtQualifiedDumpable(coll));
10234         }
10235
10236         procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical"));
10237         if (strcmp(procname, "-") != 0)
10238                 appendPQExpBuffer(q, ",\n    canonical = %s", procname);
10239
10240         procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff"));
10241         if (strcmp(procname, "-") != 0)
10242                 appendPQExpBuffer(q, ",\n    subtype_diff = %s", procname);
10243
10244         appendPQExpBufferStr(q, "\n);\n");
10245
10246         if (dopt->binary_upgrade)
10247                 binary_upgrade_extension_member(q, &tyinfo->dobj,
10248                                                                                 "TYPE", qtypname,
10249                                                                                 tyinfo->dobj.namespace->dobj.name);
10250
10251         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10252                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10253                                          tyinfo->dobj.name,
10254                                          tyinfo->dobj.namespace->dobj.name,
10255                                          NULL,
10256                                          tyinfo->rolname, false,
10257                                          "TYPE", SECTION_PRE_DATA,
10258                                          q->data, delq->data, NULL,
10259                                          NULL, 0,
10260                                          NULL, NULL);
10261
10262         /* Dump Type Comments and Security Labels */
10263         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10264                 dumpComment(fout, "TYPE", qtypname,
10265                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10266                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10267
10268         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10269                 dumpSecLabel(fout, "TYPE", qtypname,
10270                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10271                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10272
10273         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10274                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10275                                 qtypname, NULL,
10276                                 tyinfo->dobj.namespace->dobj.name,
10277                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10278                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10279
10280         PQclear(res);
10281         destroyPQExpBuffer(q);
10282         destroyPQExpBuffer(delq);
10283         destroyPQExpBuffer(query);
10284         free(qtypname);
10285         free(qualtypname);
10286 }
10287
10288 /*
10289  * dumpUndefinedType
10290  *        writes out to fout the queries to recreate a !typisdefined type
10291  *
10292  * This is a shell type, but we use different terminology to distinguish
10293  * this case from where we have to emit a shell type definition to break
10294  * circular dependencies.  An undefined type shouldn't ever have anything
10295  * depending on it.
10296  */
10297 static void
10298 dumpUndefinedType(Archive *fout, TypeInfo *tyinfo)
10299 {
10300         DumpOptions *dopt = fout->dopt;
10301         PQExpBuffer q = createPQExpBuffer();
10302         PQExpBuffer delq = createPQExpBuffer();
10303         char       *qtypname;
10304         char       *qualtypname;
10305
10306         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10307         qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10308
10309         appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
10310
10311         if (dopt->binary_upgrade)
10312                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10313                                                                                                  tyinfo->dobj.catId.oid,
10314                                                                                                  false);
10315
10316         appendPQExpBuffer(q, "CREATE TYPE %s;\n",
10317                                           qualtypname);
10318
10319         if (dopt->binary_upgrade)
10320                 binary_upgrade_extension_member(q, &tyinfo->dobj,
10321                                                                                 "TYPE", qtypname,
10322                                                                                 tyinfo->dobj.namespace->dobj.name);
10323
10324         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10325                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10326                                          tyinfo->dobj.name,
10327                                          tyinfo->dobj.namespace->dobj.name,
10328                                          NULL,
10329                                          tyinfo->rolname, false,
10330                                          "TYPE", SECTION_PRE_DATA,
10331                                          q->data, delq->data, NULL,
10332                                          NULL, 0,
10333                                          NULL, NULL);
10334
10335         /* Dump Type Comments and Security Labels */
10336         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10337                 dumpComment(fout, "TYPE", qtypname,
10338                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10339                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10340
10341         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10342                 dumpSecLabel(fout, "TYPE", qtypname,
10343                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10344                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10345
10346         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10347                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10348                                 qtypname, NULL,
10349                                 tyinfo->dobj.namespace->dobj.name,
10350                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10351                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10352
10353         destroyPQExpBuffer(q);
10354         destroyPQExpBuffer(delq);
10355         free(qtypname);
10356         free(qualtypname);
10357 }
10358
10359 /*
10360  * dumpBaseType
10361  *        writes out to fout the queries to recreate a user-defined base type
10362  */
10363 static void
10364 dumpBaseType(Archive *fout, TypeInfo *tyinfo)
10365 {
10366         DumpOptions *dopt = fout->dopt;
10367         PQExpBuffer q = createPQExpBuffer();
10368         PQExpBuffer delq = createPQExpBuffer();
10369         PQExpBuffer query = createPQExpBuffer();
10370         PGresult   *res;
10371         char       *qtypname;
10372         char       *qualtypname;
10373         char       *typlen;
10374         char       *typinput;
10375         char       *typoutput;
10376         char       *typreceive;
10377         char       *typsend;
10378         char       *typmodin;
10379         char       *typmodout;
10380         char       *typanalyze;
10381         Oid                     typreceiveoid;
10382         Oid                     typsendoid;
10383         Oid                     typmodinoid;
10384         Oid                     typmodoutoid;
10385         Oid                     typanalyzeoid;
10386         char       *typcategory;
10387         char       *typispreferred;
10388         char       *typdelim;
10389         char       *typbyval;
10390         char       *typalign;
10391         char       *typstorage;
10392         char       *typcollatable;
10393         char       *typdefault;
10394         bool            typdefault_is_literal = false;
10395
10396         /* Fetch type-specific details */
10397         if (fout->remoteVersion >= 90100)
10398         {
10399                 appendPQExpBuffer(query, "SELECT typlen, "
10400                                                   "typinput, typoutput, typreceive, typsend, "
10401                                                   "typmodin, typmodout, typanalyze, "
10402                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10403                                                   "typsend::pg_catalog.oid AS typsendoid, "
10404                                                   "typmodin::pg_catalog.oid AS typmodinoid, "
10405                                                   "typmodout::pg_catalog.oid AS typmodoutoid, "
10406                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10407                                                   "typcategory, typispreferred, "
10408                                                   "typdelim, typbyval, typalign, typstorage, "
10409                                                   "(typcollation <> 0) AS typcollatable, "
10410                                                   "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10411                                                   "FROM pg_catalog.pg_type "
10412                                                   "WHERE oid = '%u'::pg_catalog.oid",
10413                                                   tyinfo->dobj.catId.oid);
10414         }
10415         else if (fout->remoteVersion >= 80400)
10416         {
10417                 appendPQExpBuffer(query, "SELECT typlen, "
10418                                                   "typinput, typoutput, typreceive, typsend, "
10419                                                   "typmodin, typmodout, typanalyze, "
10420                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10421                                                   "typsend::pg_catalog.oid AS typsendoid, "
10422                                                   "typmodin::pg_catalog.oid AS typmodinoid, "
10423                                                   "typmodout::pg_catalog.oid AS typmodoutoid, "
10424                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10425                                                   "typcategory, typispreferred, "
10426                                                   "typdelim, typbyval, typalign, typstorage, "
10427                                                   "false AS typcollatable, "
10428                                                   "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
10429                                                   "FROM pg_catalog.pg_type "
10430                                                   "WHERE oid = '%u'::pg_catalog.oid",
10431                                                   tyinfo->dobj.catId.oid);
10432         }
10433         else if (fout->remoteVersion >= 80300)
10434         {
10435                 /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
10436                 appendPQExpBuffer(query, "SELECT typlen, "
10437                                                   "typinput, typoutput, typreceive, typsend, "
10438                                                   "typmodin, typmodout, typanalyze, "
10439                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10440                                                   "typsend::pg_catalog.oid AS typsendoid, "
10441                                                   "typmodin::pg_catalog.oid AS typmodinoid, "
10442                                                   "typmodout::pg_catalog.oid AS typmodoutoid, "
10443                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10444                                                   "'U' AS typcategory, false AS typispreferred, "
10445                                                   "typdelim, typbyval, typalign, typstorage, "
10446                                                   "false AS typcollatable, "
10447                                                   "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10448                                                   "FROM pg_catalog.pg_type "
10449                                                   "WHERE oid = '%u'::pg_catalog.oid",
10450                                                   tyinfo->dobj.catId.oid);
10451         }
10452         else
10453         {
10454                 appendPQExpBuffer(query, "SELECT typlen, "
10455                                                   "typinput, typoutput, typreceive, typsend, "
10456                                                   "'-' AS typmodin, '-' AS typmodout, "
10457                                                   "typanalyze, "
10458                                                   "typreceive::pg_catalog.oid AS typreceiveoid, "
10459                                                   "typsend::pg_catalog.oid AS typsendoid, "
10460                                                   "0 AS typmodinoid, 0 AS typmodoutoid, "
10461                                                   "typanalyze::pg_catalog.oid AS typanalyzeoid, "
10462                                                   "'U' AS typcategory, false AS typispreferred, "
10463                                                   "typdelim, typbyval, typalign, typstorage, "
10464                                                   "false AS typcollatable, "
10465                                                   "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
10466                                                   "FROM pg_catalog.pg_type "
10467                                                   "WHERE oid = '%u'::pg_catalog.oid",
10468                                                   tyinfo->dobj.catId.oid);
10469         }
10470
10471         res = ExecuteSqlQueryForSingleRow(fout, query->data);
10472
10473         typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
10474         typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
10475         typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
10476         typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
10477         typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
10478         typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
10479         typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
10480         typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
10481         typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
10482         typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
10483         typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
10484         typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
10485         typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
10486         typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
10487         typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
10488         typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
10489         typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
10490         typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
10491         typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
10492         typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
10493         if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10494                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10495         else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10496         {
10497                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10498                 typdefault_is_literal = true;   /* it needs quotes */
10499         }
10500         else
10501                 typdefault = NULL;
10502
10503         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10504         qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10505
10506         /*
10507          * The reason we include CASCADE is that the circular dependency between
10508          * the type and its I/O functions makes it impossible to drop the type any
10509          * other way.
10510          */
10511         appendPQExpBuffer(delq, "DROP TYPE %s CASCADE;\n", qualtypname);
10512
10513         /*
10514          * We might already have a shell type, but setting pg_type_oid is
10515          * harmless, and in any case we'd better set the array type OID.
10516          */
10517         if (dopt->binary_upgrade)
10518                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10519                                                                                                  tyinfo->dobj.catId.oid,
10520                                                                                                  false);
10521
10522         appendPQExpBuffer(q,
10523                                           "CREATE TYPE %s (\n"
10524                                           "    INTERNALLENGTH = %s",
10525                                           qualtypname,
10526                                           (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
10527
10528         /* regproc result is sufficiently quoted already */
10529         appendPQExpBuffer(q, ",\n    INPUT = %s", typinput);
10530         appendPQExpBuffer(q, ",\n    OUTPUT = %s", typoutput);
10531         if (OidIsValid(typreceiveoid))
10532                 appendPQExpBuffer(q, ",\n    RECEIVE = %s", typreceive);
10533         if (OidIsValid(typsendoid))
10534                 appendPQExpBuffer(q, ",\n    SEND = %s", typsend);
10535         if (OidIsValid(typmodinoid))
10536                 appendPQExpBuffer(q, ",\n    TYPMOD_IN = %s", typmodin);
10537         if (OidIsValid(typmodoutoid))
10538                 appendPQExpBuffer(q, ",\n    TYPMOD_OUT = %s", typmodout);
10539         if (OidIsValid(typanalyzeoid))
10540                 appendPQExpBuffer(q, ",\n    ANALYZE = %s", typanalyze);
10541
10542         if (strcmp(typcollatable, "t") == 0)
10543                 appendPQExpBufferStr(q, ",\n    COLLATABLE = true");
10544
10545         if (typdefault != NULL)
10546         {
10547                 appendPQExpBufferStr(q, ",\n    DEFAULT = ");
10548                 if (typdefault_is_literal)
10549                         appendStringLiteralAH(q, typdefault, fout);
10550                 else
10551                         appendPQExpBufferStr(q, typdefault);
10552         }
10553
10554         if (OidIsValid(tyinfo->typelem))
10555         {
10556                 char       *elemType;
10557
10558                 elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroAsOpaque);
10559                 appendPQExpBuffer(q, ",\n    ELEMENT = %s", elemType);
10560                 free(elemType);
10561         }
10562
10563         if (strcmp(typcategory, "U") != 0)
10564         {
10565                 appendPQExpBufferStr(q, ",\n    CATEGORY = ");
10566                 appendStringLiteralAH(q, typcategory, fout);
10567         }
10568
10569         if (strcmp(typispreferred, "t") == 0)
10570                 appendPQExpBufferStr(q, ",\n    PREFERRED = true");
10571
10572         if (typdelim && strcmp(typdelim, ",") != 0)
10573         {
10574                 appendPQExpBufferStr(q, ",\n    DELIMITER = ");
10575                 appendStringLiteralAH(q, typdelim, fout);
10576         }
10577
10578         if (strcmp(typalign, "c") == 0)
10579                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = char");
10580         else if (strcmp(typalign, "s") == 0)
10581                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = int2");
10582         else if (strcmp(typalign, "i") == 0)
10583                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = int4");
10584         else if (strcmp(typalign, "d") == 0)
10585                 appendPQExpBufferStr(q, ",\n    ALIGNMENT = double");
10586
10587         if (strcmp(typstorage, "p") == 0)
10588                 appendPQExpBufferStr(q, ",\n    STORAGE = plain");
10589         else if (strcmp(typstorage, "e") == 0)
10590                 appendPQExpBufferStr(q, ",\n    STORAGE = external");
10591         else if (strcmp(typstorage, "x") == 0)
10592                 appendPQExpBufferStr(q, ",\n    STORAGE = extended");
10593         else if (strcmp(typstorage, "m") == 0)
10594                 appendPQExpBufferStr(q, ",\n    STORAGE = main");
10595
10596         if (strcmp(typbyval, "t") == 0)
10597                 appendPQExpBufferStr(q, ",\n    PASSEDBYVALUE");
10598
10599         appendPQExpBufferStr(q, "\n);\n");
10600
10601         if (dopt->binary_upgrade)
10602                 binary_upgrade_extension_member(q, &tyinfo->dobj,
10603                                                                                 "TYPE", qtypname,
10604                                                                                 tyinfo->dobj.namespace->dobj.name);
10605
10606         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10607                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10608                                          tyinfo->dobj.name,
10609                                          tyinfo->dobj.namespace->dobj.name,
10610                                          NULL,
10611                                          tyinfo->rolname, false,
10612                                          "TYPE", SECTION_PRE_DATA,
10613                                          q->data, delq->data, NULL,
10614                                          NULL, 0,
10615                                          NULL, NULL);
10616
10617         /* Dump Type Comments and Security Labels */
10618         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10619                 dumpComment(fout, "TYPE", qtypname,
10620                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10621                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10622
10623         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10624                 dumpSecLabel(fout, "TYPE", qtypname,
10625                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10626                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10627
10628         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10629                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10630                                 qtypname, NULL,
10631                                 tyinfo->dobj.namespace->dobj.name,
10632                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10633                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10634
10635         PQclear(res);
10636         destroyPQExpBuffer(q);
10637         destroyPQExpBuffer(delq);
10638         destroyPQExpBuffer(query);
10639         free(qtypname);
10640         free(qualtypname);
10641 }
10642
10643 /*
10644  * dumpDomain
10645  *        writes out to fout the queries to recreate a user-defined domain
10646  */
10647 static void
10648 dumpDomain(Archive *fout, TypeInfo *tyinfo)
10649 {
10650         DumpOptions *dopt = fout->dopt;
10651         PQExpBuffer q = createPQExpBuffer();
10652         PQExpBuffer delq = createPQExpBuffer();
10653         PQExpBuffer query = createPQExpBuffer();
10654         PGresult   *res;
10655         int                     i;
10656         char       *qtypname;
10657         char       *qualtypname;
10658         char       *typnotnull;
10659         char       *typdefn;
10660         char       *typdefault;
10661         Oid                     typcollation;
10662         bool            typdefault_is_literal = false;
10663
10664         /* Fetch domain specific details */
10665         if (fout->remoteVersion >= 90100)
10666         {
10667                 /* typcollation is new in 9.1 */
10668                 appendPQExpBuffer(query, "SELECT t.typnotnull, "
10669                                                   "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
10670                                                   "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
10671                                                   "t.typdefault, "
10672                                                   "CASE WHEN t.typcollation <> u.typcollation "
10673                                                   "THEN t.typcollation ELSE 0 END AS typcollation "
10674                                                   "FROM pg_catalog.pg_type t "
10675                                                   "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
10676                                                   "WHERE t.oid = '%u'::pg_catalog.oid",
10677                                                   tyinfo->dobj.catId.oid);
10678         }
10679         else
10680         {
10681                 appendPQExpBuffer(query, "SELECT typnotnull, "
10682                                                   "pg_catalog.format_type(typbasetype, typtypmod) AS typdefn, "
10683                                                   "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
10684                                                   "typdefault, 0 AS typcollation "
10685                                                   "FROM pg_catalog.pg_type "
10686                                                   "WHERE oid = '%u'::pg_catalog.oid",
10687                                                   tyinfo->dobj.catId.oid);
10688         }
10689
10690         res = ExecuteSqlQueryForSingleRow(fout, query->data);
10691
10692         typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
10693         typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
10694         if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
10695                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
10696         else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
10697         {
10698                 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
10699                 typdefault_is_literal = true;   /* it needs quotes */
10700         }
10701         else
10702                 typdefault = NULL;
10703         typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
10704
10705         if (dopt->binary_upgrade)
10706                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10707                                                                                                  tyinfo->dobj.catId.oid,
10708                                                                                                  true); /* force array type */
10709
10710         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10711         qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10712
10713         appendPQExpBuffer(q,
10714                                           "CREATE DOMAIN %s AS %s",
10715                                           qualtypname,
10716                                           typdefn);
10717
10718         /* Print collation only if different from base type's collation */
10719         if (OidIsValid(typcollation))
10720         {
10721                 CollInfo   *coll;
10722
10723                 coll = findCollationByOid(typcollation);
10724                 if (coll)
10725                         appendPQExpBuffer(q, " COLLATE %s", fmtQualifiedDumpable(coll));
10726         }
10727
10728         if (typnotnull[0] == 't')
10729                 appendPQExpBufferStr(q, " NOT NULL");
10730
10731         if (typdefault != NULL)
10732         {
10733                 appendPQExpBufferStr(q, " DEFAULT ");
10734                 if (typdefault_is_literal)
10735                         appendStringLiteralAH(q, typdefault, fout);
10736                 else
10737                         appendPQExpBufferStr(q, typdefault);
10738         }
10739
10740         PQclear(res);
10741
10742         /*
10743          * Add any CHECK constraints for the domain
10744          */
10745         for (i = 0; i < tyinfo->nDomChecks; i++)
10746         {
10747                 ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
10748
10749                 if (!domcheck->separate)
10750                         appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
10751                                                           fmtId(domcheck->dobj.name), domcheck->condef);
10752         }
10753
10754         appendPQExpBufferStr(q, ";\n");
10755
10756         appendPQExpBuffer(delq, "DROP DOMAIN %s;\n", qualtypname);
10757
10758         if (dopt->binary_upgrade)
10759                 binary_upgrade_extension_member(q, &tyinfo->dobj,
10760                                                                                 "DOMAIN", qtypname,
10761                                                                                 tyinfo->dobj.namespace->dobj.name);
10762
10763         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10764                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10765                                          tyinfo->dobj.name,
10766                                          tyinfo->dobj.namespace->dobj.name,
10767                                          NULL,
10768                                          tyinfo->rolname, false,
10769                                          "DOMAIN", SECTION_PRE_DATA,
10770                                          q->data, delq->data, NULL,
10771                                          NULL, 0,
10772                                          NULL, NULL);
10773
10774         /* Dump Domain Comments and Security Labels */
10775         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10776                 dumpComment(fout, "DOMAIN", qtypname,
10777                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10778                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10779
10780         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
10781                 dumpSecLabel(fout, "DOMAIN", qtypname,
10782                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
10783                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
10784
10785         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
10786                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
10787                                 qtypname, NULL,
10788                                 tyinfo->dobj.namespace->dobj.name,
10789                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
10790                                 tyinfo->inittypacl, tyinfo->initrtypacl);
10791
10792         /* Dump any per-constraint comments */
10793         for (i = 0; i < tyinfo->nDomChecks; i++)
10794         {
10795                 ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
10796                 PQExpBuffer conprefix = createPQExpBuffer();
10797
10798                 appendPQExpBuffer(conprefix, "CONSTRAINT %s ON DOMAIN",
10799                                                   fmtId(domcheck->dobj.name));
10800
10801                 if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10802                         dumpComment(fout, conprefix->data, qtypname,
10803                                                 tyinfo->dobj.namespace->dobj.name,
10804                                                 tyinfo->rolname,
10805                                                 domcheck->dobj.catId, 0, tyinfo->dobj.dumpId);
10806
10807                 destroyPQExpBuffer(conprefix);
10808         }
10809
10810         destroyPQExpBuffer(q);
10811         destroyPQExpBuffer(delq);
10812         destroyPQExpBuffer(query);
10813         free(qtypname);
10814         free(qualtypname);
10815 }
10816
10817 /*
10818  * dumpCompositeType
10819  *        writes out to fout the queries to recreate a user-defined stand-alone
10820  *        composite type
10821  */
10822 static void
10823 dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
10824 {
10825         DumpOptions *dopt = fout->dopt;
10826         PQExpBuffer q = createPQExpBuffer();
10827         PQExpBuffer dropped = createPQExpBuffer();
10828         PQExpBuffer delq = createPQExpBuffer();
10829         PQExpBuffer query = createPQExpBuffer();
10830         PGresult   *res;
10831         char       *qtypname;
10832         char       *qualtypname;
10833         int                     ntups;
10834         int                     i_attname;
10835         int                     i_atttypdefn;
10836         int                     i_attlen;
10837         int                     i_attalign;
10838         int                     i_attisdropped;
10839         int                     i_attcollation;
10840         int                     i;
10841         int                     actual_atts;
10842
10843         /* Fetch type specific details */
10844         if (fout->remoteVersion >= 90100)
10845         {
10846                 /*
10847                  * attcollation is new in 9.1.  Since we only want to dump COLLATE
10848                  * clauses for attributes whose collation is different from their
10849                  * type's default, we use a CASE here to suppress uninteresting
10850                  * attcollations cheaply.  atttypid will be 0 for dropped columns;
10851                  * collation does not matter for those.
10852                  */
10853                 appendPQExpBuffer(query, "SELECT a.attname, "
10854                                                   "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10855                                                   "a.attlen, a.attalign, a.attisdropped, "
10856                                                   "CASE WHEN a.attcollation <> at.typcollation "
10857                                                   "THEN a.attcollation ELSE 0 END AS attcollation "
10858                                                   "FROM pg_catalog.pg_type ct "
10859                                                   "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
10860                                                   "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
10861                                                   "WHERE ct.oid = '%u'::pg_catalog.oid "
10862                                                   "ORDER BY a.attnum ",
10863                                                   tyinfo->dobj.catId.oid);
10864         }
10865         else
10866         {
10867                 /*
10868                  * Since ALTER TYPE could not drop columns until 9.1, attisdropped
10869                  * should always be false.
10870                  */
10871                 appendPQExpBuffer(query, "SELECT a.attname, "
10872                                                   "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
10873                                                   "a.attlen, a.attalign, a.attisdropped, "
10874                                                   "0 AS attcollation "
10875                                                   "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
10876                                                   "WHERE ct.oid = '%u'::pg_catalog.oid "
10877                                                   "AND a.attrelid = ct.typrelid "
10878                                                   "ORDER BY a.attnum ",
10879                                                   tyinfo->dobj.catId.oid);
10880         }
10881
10882         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10883
10884         ntups = PQntuples(res);
10885
10886         i_attname = PQfnumber(res, "attname");
10887         i_atttypdefn = PQfnumber(res, "atttypdefn");
10888         i_attlen = PQfnumber(res, "attlen");
10889         i_attalign = PQfnumber(res, "attalign");
10890         i_attisdropped = PQfnumber(res, "attisdropped");
10891         i_attcollation = PQfnumber(res, "attcollation");
10892
10893         if (dopt->binary_upgrade)
10894         {
10895                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
10896                                                                                                  tyinfo->dobj.catId.oid,
10897                                                                                                  false);
10898                 binary_upgrade_set_pg_class_oids(fout, q, tyinfo->typrelid, false);
10899         }
10900
10901         qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
10902         qualtypname = pg_strdup(fmtQualifiedDumpable(tyinfo));
10903
10904         appendPQExpBuffer(q, "CREATE TYPE %s AS (",
10905                                           qualtypname);
10906
10907         actual_atts = 0;
10908         for (i = 0; i < ntups; i++)
10909         {
10910                 char       *attname;
10911                 char       *atttypdefn;
10912                 char       *attlen;
10913                 char       *attalign;
10914                 bool            attisdropped;
10915                 Oid                     attcollation;
10916
10917                 attname = PQgetvalue(res, i, i_attname);
10918                 atttypdefn = PQgetvalue(res, i, i_atttypdefn);
10919                 attlen = PQgetvalue(res, i, i_attlen);
10920                 attalign = PQgetvalue(res, i, i_attalign);
10921                 attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
10922                 attcollation = atooid(PQgetvalue(res, i, i_attcollation));
10923
10924                 if (attisdropped && !dopt->binary_upgrade)
10925                         continue;
10926
10927                 /* Format properly if not first attr */
10928                 if (actual_atts++ > 0)
10929                         appendPQExpBufferChar(q, ',');
10930                 appendPQExpBufferStr(q, "\n\t");
10931
10932                 if (!attisdropped)
10933                 {
10934                         appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
10935
10936                         /* Add collation if not default for the column type */
10937                         if (OidIsValid(attcollation))
10938                         {
10939                                 CollInfo   *coll;
10940
10941                                 coll = findCollationByOid(attcollation);
10942                                 if (coll)
10943                                         appendPQExpBuffer(q, " COLLATE %s",
10944                                                                           fmtQualifiedDumpable(coll));
10945                         }
10946                 }
10947                 else
10948                 {
10949                         /*
10950                          * This is a dropped attribute and we're in binary_upgrade mode.
10951                          * Insert a placeholder for it in the CREATE TYPE command, and set
10952                          * length and alignment with direct UPDATE to the catalogs
10953                          * afterwards. See similar code in dumpTableSchema().
10954                          */
10955                         appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
10956
10957                         /* stash separately for insertion after the CREATE TYPE */
10958                         appendPQExpBufferStr(dropped,
10959                                                                  "\n-- For binary upgrade, recreate dropped column.\n");
10960                         appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
10961                                                           "SET attlen = %s, "
10962                                                           "attalign = '%s', attbyval = false\n"
10963                                                           "WHERE attname = ", attlen, attalign);
10964                         appendStringLiteralAH(dropped, attname, fout);
10965                         appendPQExpBufferStr(dropped, "\n  AND attrelid = ");
10966                         appendStringLiteralAH(dropped, qualtypname, fout);
10967                         appendPQExpBufferStr(dropped, "::pg_catalog.regclass;\n");
10968
10969                         appendPQExpBuffer(dropped, "ALTER TYPE %s ",
10970                                                           qualtypname);
10971                         appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
10972                                                           fmtId(attname));
10973                 }
10974         }
10975         appendPQExpBufferStr(q, "\n);\n");
10976         appendPQExpBufferStr(q, dropped->data);
10977
10978         appendPQExpBuffer(delq, "DROP TYPE %s;\n", qualtypname);
10979
10980         if (dopt->binary_upgrade)
10981                 binary_upgrade_extension_member(q, &tyinfo->dobj,
10982                                                                                 "TYPE", qtypname,
10983                                                                                 tyinfo->dobj.namespace->dobj.name);
10984
10985         if (tyinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
10986                 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
10987                                          tyinfo->dobj.name,
10988                                          tyinfo->dobj.namespace->dobj.name,
10989                                          NULL,
10990                                          tyinfo->rolname, false,
10991                                          "TYPE", SECTION_PRE_DATA,
10992                                          q->data, delq->data, NULL,
10993                                          NULL, 0,
10994                                          NULL, NULL);
10995
10996
10997         /* Dump Type Comments and Security Labels */
10998         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
10999                 dumpComment(fout, "TYPE", qtypname,
11000                                         tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11001                                         tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11002
11003         if (tyinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11004                 dumpSecLabel(fout, "TYPE", qtypname,
11005                                          tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
11006                                          tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
11007
11008         if (tyinfo->dobj.dump & DUMP_COMPONENT_ACL)
11009                 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
11010                                 qtypname, NULL,
11011                                 tyinfo->dobj.namespace->dobj.name,
11012                                 tyinfo->rolname, tyinfo->typacl, tyinfo->rtypacl,
11013                                 tyinfo->inittypacl, tyinfo->initrtypacl);
11014
11015         PQclear(res);
11016         destroyPQExpBuffer(q);
11017         destroyPQExpBuffer(dropped);
11018         destroyPQExpBuffer(delq);
11019         destroyPQExpBuffer(query);
11020         free(qtypname);
11021         free(qualtypname);
11022
11023         /* Dump any per-column comments */
11024         if (tyinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11025                 dumpCompositeTypeColComments(fout, tyinfo);
11026 }
11027
11028 /*
11029  * dumpCompositeTypeColComments
11030  *        writes out to fout the queries to recreate comments on the columns of
11031  *        a user-defined stand-alone composite type
11032  */
11033 static void
11034 dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
11035 {
11036         CommentItem *comments;
11037         int                     ncomments;
11038         PGresult   *res;
11039         PQExpBuffer query;
11040         PQExpBuffer target;
11041         Oid                     pgClassOid;
11042         int                     i;
11043         int                     ntups;
11044         int                     i_attname;
11045         int                     i_attnum;
11046
11047         /* do nothing, if --no-comments is supplied */
11048         if (fout->dopt->no_comments)
11049                 return;
11050
11051         query = createPQExpBuffer();
11052
11053         appendPQExpBuffer(query,
11054                                           "SELECT c.tableoid, a.attname, a.attnum "
11055                                           "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
11056                                           "WHERE c.oid = '%u' AND c.oid = a.attrelid "
11057                                           "  AND NOT a.attisdropped "
11058                                           "ORDER BY a.attnum ",
11059                                           tyinfo->typrelid);
11060
11061         /* Fetch column attnames */
11062         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11063
11064         ntups = PQntuples(res);
11065         if (ntups < 1)
11066         {
11067                 PQclear(res);
11068                 destroyPQExpBuffer(query);
11069                 return;
11070         }
11071
11072         pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
11073
11074         /* Search for comments associated with type's pg_class OID */
11075         ncomments = findComments(fout,
11076                                                          pgClassOid,
11077                                                          tyinfo->typrelid,
11078                                                          &comments);
11079
11080         /* If no comments exist, we're done */
11081         if (ncomments <= 0)
11082         {
11083                 PQclear(res);
11084                 destroyPQExpBuffer(query);
11085                 return;
11086         }
11087
11088         /* Build COMMENT ON statements */
11089         target = createPQExpBuffer();
11090
11091         i_attnum = PQfnumber(res, "attnum");
11092         i_attname = PQfnumber(res, "attname");
11093         while (ncomments > 0)
11094         {
11095                 const char *attname;
11096
11097                 attname = NULL;
11098                 for (i = 0; i < ntups; i++)
11099                 {
11100                         if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
11101                         {
11102                                 attname = PQgetvalue(res, i, i_attname);
11103                                 break;
11104                         }
11105                 }
11106                 if (attname)                    /* just in case we don't find it */
11107                 {
11108                         const char *descr = comments->descr;
11109
11110                         resetPQExpBuffer(target);
11111                         appendPQExpBuffer(target, "COLUMN %s.",
11112                                                           fmtId(tyinfo->dobj.name));
11113                         appendPQExpBufferStr(target, fmtId(attname));
11114
11115                         resetPQExpBuffer(query);
11116                         appendPQExpBuffer(query, "COMMENT ON COLUMN %s.",
11117                                                           fmtQualifiedDumpable(tyinfo));
11118                         appendPQExpBuffer(query, "%s IS ", fmtId(attname));
11119                         appendStringLiteralAH(query, descr, fout);
11120                         appendPQExpBufferStr(query, ";\n");
11121
11122                         ArchiveEntry(fout, nilCatalogId, createDumpId(),
11123                                                  target->data,
11124                                                  tyinfo->dobj.namespace->dobj.name,
11125                                                  NULL, tyinfo->rolname,
11126                                                  false, "COMMENT", SECTION_NONE,
11127                                                  query->data, "", NULL,
11128                                                  &(tyinfo->dobj.dumpId), 1,
11129                                                  NULL, NULL);
11130                 }
11131
11132                 comments++;
11133                 ncomments--;
11134         }
11135
11136         PQclear(res);
11137         destroyPQExpBuffer(query);
11138         destroyPQExpBuffer(target);
11139 }
11140
11141 /*
11142  * dumpShellType
11143  *        writes out to fout the queries to create a shell type
11144  *
11145  * We dump a shell definition in advance of the I/O functions for the type.
11146  */
11147 static void
11148 dumpShellType(Archive *fout, ShellTypeInfo *stinfo)
11149 {
11150         DumpOptions *dopt = fout->dopt;
11151         PQExpBuffer q;
11152
11153         /* Skip if not to be dumped */
11154         if (!stinfo->dobj.dump || dopt->dataOnly)
11155                 return;
11156
11157         q = createPQExpBuffer();
11158
11159         /*
11160          * Note the lack of a DROP command for the shell type; any required DROP
11161          * is driven off the base type entry, instead.  This interacts with
11162          * _printTocEntry()'s use of the presence of a DROP command to decide
11163          * whether an entry needs an ALTER OWNER command.  We don't want to alter
11164          * the shell type's owner immediately on creation; that should happen only
11165          * after it's filled in, otherwise the backend complains.
11166          */
11167
11168         if (dopt->binary_upgrade)
11169                 binary_upgrade_set_type_oids_by_type_oid(fout, q,
11170                                                                                                  stinfo->baseType->dobj.catId.oid,
11171                                                                                                  false);
11172
11173         appendPQExpBuffer(q, "CREATE TYPE %s;\n",
11174                                           fmtQualifiedDumpable(stinfo));
11175
11176         if (stinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11177                 ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId,
11178                                          stinfo->dobj.name,
11179                                          stinfo->dobj.namespace->dobj.name,
11180                                          NULL,
11181                                          stinfo->baseType->rolname, false,
11182                                          "SHELL TYPE", SECTION_PRE_DATA,
11183                                          q->data, "", NULL,
11184                                          NULL, 0,
11185                                          NULL, NULL);
11186
11187         destroyPQExpBuffer(q);
11188 }
11189
11190 /*
11191  * dumpProcLang
11192  *                writes out to fout the queries to recreate a user-defined
11193  *                procedural language
11194  */
11195 static void
11196 dumpProcLang(Archive *fout, ProcLangInfo *plang)
11197 {
11198         DumpOptions *dopt = fout->dopt;
11199         PQExpBuffer defqry;
11200         PQExpBuffer delqry;
11201         bool            useParams;
11202         char       *qlanname;
11203         FuncInfo   *funcInfo;
11204         FuncInfo   *inlineInfo = NULL;
11205         FuncInfo   *validatorInfo = NULL;
11206
11207         /* Skip if not to be dumped */
11208         if (!plang->dobj.dump || dopt->dataOnly)
11209                 return;
11210
11211         /*
11212          * Try to find the support function(s).  It is not an error if we don't
11213          * find them --- if the functions are in the pg_catalog schema, as is
11214          * standard in 8.1 and up, then we won't have loaded them. (In this case
11215          * we will emit a parameterless CREATE LANGUAGE command, which will
11216          * require PL template knowledge in the backend to reload.)
11217          */
11218
11219         funcInfo = findFuncByOid(plang->lanplcallfoid);
11220         if (funcInfo != NULL && !funcInfo->dobj.dump)
11221                 funcInfo = NULL;                /* treat not-dumped same as not-found */
11222
11223         if (OidIsValid(plang->laninline))
11224         {
11225                 inlineInfo = findFuncByOid(plang->laninline);
11226                 if (inlineInfo != NULL && !inlineInfo->dobj.dump)
11227                         inlineInfo = NULL;
11228         }
11229
11230         if (OidIsValid(plang->lanvalidator))
11231         {
11232                 validatorInfo = findFuncByOid(plang->lanvalidator);
11233                 if (validatorInfo != NULL && !validatorInfo->dobj.dump)
11234                         validatorInfo = NULL;
11235         }
11236
11237         /*
11238          * If the functions are dumpable then emit a traditional CREATE LANGUAGE
11239          * with parameters.  Otherwise, we'll write a parameterless command, which
11240          * will rely on data from pg_pltemplate.
11241          */
11242         useParams = (funcInfo != NULL &&
11243                                  (inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
11244                                  (validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
11245
11246         defqry = createPQExpBuffer();
11247         delqry = createPQExpBuffer();
11248
11249         qlanname = pg_strdup(fmtId(plang->dobj.name));
11250
11251         appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
11252                                           qlanname);
11253
11254         if (useParams)
11255         {
11256                 appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
11257                                                   plang->lanpltrusted ? "TRUSTED " : "",
11258                                                   qlanname);
11259                 appendPQExpBuffer(defqry, " HANDLER %s",
11260                                                   fmtQualifiedDumpable(funcInfo));
11261                 if (OidIsValid(plang->laninline))
11262                         appendPQExpBuffer(defqry, " INLINE %s",
11263                                                           fmtQualifiedDumpable(inlineInfo));
11264                 if (OidIsValid(plang->lanvalidator))
11265                         appendPQExpBuffer(defqry, " VALIDATOR %s",
11266                                                           fmtQualifiedDumpable(validatorInfo));
11267         }
11268         else
11269         {
11270                 /*
11271                  * If not dumping parameters, then use CREATE OR REPLACE so that the
11272                  * command will not fail if the language is preinstalled in the target
11273                  * database.  We restrict the use of REPLACE to this case so as to
11274                  * eliminate the risk of replacing a language with incompatible
11275                  * parameter settings: this command will only succeed at all if there
11276                  * is a pg_pltemplate entry, and if there is one, the existing entry
11277                  * must match it too.
11278                  */
11279                 appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s",
11280                                                   qlanname);
11281         }
11282         appendPQExpBufferStr(defqry, ";\n");
11283
11284         if (dopt->binary_upgrade)
11285                 binary_upgrade_extension_member(defqry, &plang->dobj,
11286                                                                                 "LANGUAGE", qlanname, NULL);
11287
11288         if (plang->dobj.dump & DUMP_COMPONENT_DEFINITION)
11289                 ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
11290                                          plang->dobj.name,
11291                                          NULL, NULL, plang->lanowner,
11292                                          false, "PROCEDURAL LANGUAGE", SECTION_PRE_DATA,
11293                                          defqry->data, delqry->data, NULL,
11294                                          NULL, 0,
11295                                          NULL, NULL);
11296
11297         /* Dump Proc Lang Comments and Security Labels */
11298         if (plang->dobj.dump & DUMP_COMPONENT_COMMENT)
11299                 dumpComment(fout, "LANGUAGE", qlanname,
11300                                         NULL, plang->lanowner,
11301                                         plang->dobj.catId, 0, plang->dobj.dumpId);
11302
11303         if (plang->dobj.dump & DUMP_COMPONENT_SECLABEL)
11304                 dumpSecLabel(fout, "LANGUAGE", qlanname,
11305                                          NULL, plang->lanowner,
11306                                          plang->dobj.catId, 0, plang->dobj.dumpId);
11307
11308         if (plang->lanpltrusted && plang->dobj.dump & DUMP_COMPONENT_ACL)
11309                 dumpACL(fout, plang->dobj.catId, plang->dobj.dumpId, "LANGUAGE",
11310                                 qlanname, NULL, NULL,
11311                                 plang->lanowner, plang->lanacl, plang->rlanacl,
11312                                 plang->initlanacl, plang->initrlanacl);
11313
11314         free(qlanname);
11315
11316         destroyPQExpBuffer(defqry);
11317         destroyPQExpBuffer(delqry);
11318 }
11319
11320 /*
11321  * format_function_arguments: generate function name and argument list
11322  *
11323  * This is used when we can rely on pg_get_function_arguments to format
11324  * the argument list.  Note, however, that pg_get_function_arguments
11325  * does not special-case zero-argument aggregates.
11326  */
11327 static char *
11328 format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
11329 {
11330         PQExpBufferData fn;
11331
11332         initPQExpBuffer(&fn);
11333         appendPQExpBufferStr(&fn, fmtId(finfo->dobj.name));
11334         if (is_agg && finfo->nargs == 0)
11335                 appendPQExpBufferStr(&fn, "(*)");
11336         else
11337                 appendPQExpBuffer(&fn, "(%s)", funcargs);
11338         return fn.data;
11339 }
11340
11341 /*
11342  * format_function_arguments_old: generate function name and argument list
11343  *
11344  * The argument type names are qualified if needed.  The function name
11345  * is never qualified.
11346  *
11347  * This is used only with pre-8.4 servers, so we aren't expecting to see
11348  * VARIADIC or TABLE arguments, nor are there any defaults for arguments.
11349  *
11350  * Any or all of allargtypes, argmodes, argnames may be NULL.
11351  */
11352 static char *
11353 format_function_arguments_old(Archive *fout,
11354                                                           FuncInfo *finfo, int nallargs,
11355                                                           char **allargtypes,
11356                                                           char **argmodes,
11357                                                           char **argnames)
11358 {
11359         PQExpBufferData fn;
11360         int                     j;
11361
11362         initPQExpBuffer(&fn);
11363         appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
11364         for (j = 0; j < nallargs; j++)
11365         {
11366                 Oid                     typid;
11367                 char       *typname;
11368                 const char *argmode;
11369                 const char *argname;
11370
11371                 typid = allargtypes ? atooid(allargtypes[j]) : finfo->argtypes[j];
11372                 typname = getFormattedTypeName(fout, typid, zeroAsOpaque);
11373
11374                 if (argmodes)
11375                 {
11376                         switch (argmodes[j][0])
11377                         {
11378                                 case PROARGMODE_IN:
11379                                         argmode = "";
11380                                         break;
11381                                 case PROARGMODE_OUT:
11382                                         argmode = "OUT ";
11383                                         break;
11384                                 case PROARGMODE_INOUT:
11385                                         argmode = "INOUT ";
11386                                         break;
11387                                 default:
11388                                         write_msg(NULL, "WARNING: bogus value in proargmodes array\n");
11389                                         argmode = "";
11390                                         break;
11391                         }
11392                 }
11393                 else
11394                         argmode = "";
11395
11396                 argname = argnames ? argnames[j] : (char *) NULL;
11397                 if (argname && argname[0] == '\0')
11398                         argname = NULL;
11399
11400                 appendPQExpBuffer(&fn, "%s%s%s%s%s",
11401                                                   (j > 0) ? ", " : "",
11402                                                   argmode,
11403                                                   argname ? fmtId(argname) : "",
11404                                                   argname ? " " : "",
11405                                                   typname);
11406                 free(typname);
11407         }
11408         appendPQExpBufferChar(&fn, ')');
11409         return fn.data;
11410 }
11411
11412 /*
11413  * format_function_signature: generate function name and argument list
11414  *
11415  * This is like format_function_arguments_old except that only a minimal
11416  * list of input argument types is generated; this is sufficient to
11417  * reference the function, but not to define it.
11418  *
11419  * If honor_quotes is false then the function name is never quoted.
11420  * This is appropriate for use in TOC tags, but not in SQL commands.
11421  */
11422 static char *
11423 format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
11424 {
11425         PQExpBufferData fn;
11426         int                     j;
11427
11428         initPQExpBuffer(&fn);
11429         if (honor_quotes)
11430                 appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
11431         else
11432                 appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
11433         for (j = 0; j < finfo->nargs; j++)
11434         {
11435                 char       *typname;
11436
11437                 if (j > 0)
11438                         appendPQExpBufferStr(&fn, ", ");
11439
11440                 typname = getFormattedTypeName(fout, finfo->argtypes[j],
11441                                                                            zeroAsOpaque);
11442                 appendPQExpBufferStr(&fn, typname);
11443                 free(typname);
11444         }
11445         appendPQExpBufferChar(&fn, ')');
11446         return fn.data;
11447 }
11448
11449
11450 /*
11451  * dumpFunc:
11452  *        dump out one function
11453  */
11454 static void
11455 dumpFunc(Archive *fout, FuncInfo *finfo)
11456 {
11457         DumpOptions *dopt = fout->dopt;
11458         PQExpBuffer query;
11459         PQExpBuffer q;
11460         PQExpBuffer delqry;
11461         PQExpBuffer asPart;
11462         PGresult   *res;
11463         char       *funcsig;            /* identity signature */
11464         char       *funcfullsig = NULL; /* full signature */
11465         char       *funcsig_tag;
11466         char       *proretset;
11467         char       *prosrc;
11468         char       *probin;
11469         char       *funcargs;
11470         char       *funciargs;
11471         char       *funcresult;
11472         char       *proallargtypes;
11473         char       *proargmodes;
11474         char       *proargnames;
11475         char       *protrftypes;
11476         char       *prokind;
11477         char       *provolatile;
11478         char       *proisstrict;
11479         char       *prosecdef;
11480         char       *proleakproof;
11481         char       *proconfig;
11482         char       *procost;
11483         char       *prorows;
11484         char       *proparallel;
11485         char       *lanname;
11486         char       *rettypename;
11487         int                     nallargs;
11488         char      **allargtypes = NULL;
11489         char      **argmodes = NULL;
11490         char      **argnames = NULL;
11491         char      **configitems = NULL;
11492         int                     nconfigitems = 0;
11493         const char *keyword;
11494         int                     i;
11495
11496         /* Skip if not to be dumped */
11497         if (!finfo->dobj.dump || dopt->dataOnly)
11498                 return;
11499
11500         query = createPQExpBuffer();
11501         q = createPQExpBuffer();
11502         delqry = createPQExpBuffer();
11503         asPart = createPQExpBuffer();
11504
11505         /* Fetch function-specific details */
11506         if (fout->remoteVersion >= 110000)
11507         {
11508                 /*
11509                  * prokind was added in 11
11510                  */
11511                 appendPQExpBuffer(query,
11512                                                   "SELECT proretset, prosrc, probin, "
11513                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11514                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11515                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11516                                                   "array_to_string(protrftypes, ' ') AS protrftypes, "
11517                                                   "prokind, provolatile, proisstrict, prosecdef, "
11518                                                   "proleakproof, proconfig, procost, prorows, "
11519                                                   "proparallel, "
11520                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11521                                                   "FROM pg_catalog.pg_proc "
11522                                                   "WHERE oid = '%u'::pg_catalog.oid",
11523                                                   finfo->dobj.catId.oid);
11524         }
11525         else if (fout->remoteVersion >= 90600)
11526         {
11527                 /*
11528                  * proparallel was added in 9.6
11529                  */
11530                 appendPQExpBuffer(query,
11531                                                   "SELECT proretset, prosrc, probin, "
11532                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11533                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11534                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11535                                                   "array_to_string(protrftypes, ' ') AS protrftypes, "
11536                                                   "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind, "
11537                                                   "provolatile, proisstrict, prosecdef, "
11538                                                   "proleakproof, proconfig, procost, prorows, "
11539                                                   "proparallel, "
11540                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11541                                                   "FROM pg_catalog.pg_proc "
11542                                                   "WHERE oid = '%u'::pg_catalog.oid",
11543                                                   finfo->dobj.catId.oid);
11544         }
11545         else if (fout->remoteVersion >= 90500)
11546         {
11547                 /*
11548                  * protrftypes was added in 9.5
11549                  */
11550                 appendPQExpBuffer(query,
11551                                                   "SELECT proretset, prosrc, probin, "
11552                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11553                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11554                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11555                                                   "array_to_string(protrftypes, ' ') AS protrftypes, "
11556                                                   "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind, "
11557                                                   "provolatile, proisstrict, prosecdef, "
11558                                                   "proleakproof, proconfig, procost, prorows, "
11559                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11560                                                   "FROM pg_catalog.pg_proc "
11561                                                   "WHERE oid = '%u'::pg_catalog.oid",
11562                                                   finfo->dobj.catId.oid);
11563         }
11564         else if (fout->remoteVersion >= 90200)
11565         {
11566                 /*
11567                  * proleakproof was added in 9.2
11568                  */
11569                 appendPQExpBuffer(query,
11570                                                   "SELECT proretset, prosrc, probin, "
11571                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11572                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11573                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11574                                                   "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind, "
11575                                                   "provolatile, proisstrict, prosecdef, "
11576                                                   "proleakproof, proconfig, procost, prorows, "
11577                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11578                                                   "FROM pg_catalog.pg_proc "
11579                                                   "WHERE oid = '%u'::pg_catalog.oid",
11580                                                   finfo->dobj.catId.oid);
11581         }
11582         else if (fout->remoteVersion >= 80400)
11583         {
11584                 /*
11585                  * In 8.4 and up we rely on pg_get_function_arguments and
11586                  * pg_get_function_result instead of examining proallargtypes etc.
11587                  */
11588                 appendPQExpBuffer(query,
11589                                                   "SELECT proretset, prosrc, probin, "
11590                                                   "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
11591                                                   "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
11592                                                   "pg_catalog.pg_get_function_result(oid) AS funcresult, "
11593                                                   "CASE WHEN proiswindow THEN 'w' ELSE 'f' END AS prokind, "
11594                                                   "provolatile, proisstrict, prosecdef, "
11595                                                   "false AS proleakproof, "
11596                                                   " proconfig, procost, prorows, "
11597                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11598                                                   "FROM pg_catalog.pg_proc "
11599                                                   "WHERE oid = '%u'::pg_catalog.oid",
11600                                                   finfo->dobj.catId.oid);
11601         }
11602         else if (fout->remoteVersion >= 80300)
11603         {
11604                 appendPQExpBuffer(query,
11605                                                   "SELECT proretset, prosrc, probin, "
11606                                                   "proallargtypes, proargmodes, proargnames, "
11607                                                   "'f' AS prokind, "
11608                                                   "provolatile, proisstrict, prosecdef, "
11609                                                   "false AS proleakproof, "
11610                                                   "proconfig, procost, prorows, "
11611                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11612                                                   "FROM pg_catalog.pg_proc "
11613                                                   "WHERE oid = '%u'::pg_catalog.oid",
11614                                                   finfo->dobj.catId.oid);
11615         }
11616         else if (fout->remoteVersion >= 80100)
11617         {
11618                 appendPQExpBuffer(query,
11619                                                   "SELECT proretset, prosrc, probin, "
11620                                                   "proallargtypes, proargmodes, proargnames, "
11621                                                   "'f' AS prokind, "
11622                                                   "provolatile, proisstrict, prosecdef, "
11623                                                   "false AS proleakproof, "
11624                                                   "null AS proconfig, 0 AS procost, 0 AS prorows, "
11625                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11626                                                   "FROM pg_catalog.pg_proc "
11627                                                   "WHERE oid = '%u'::pg_catalog.oid",
11628                                                   finfo->dobj.catId.oid);
11629         }
11630         else
11631         {
11632                 appendPQExpBuffer(query,
11633                                                   "SELECT proretset, prosrc, probin, "
11634                                                   "null AS proallargtypes, "
11635                                                   "null AS proargmodes, "
11636                                                   "proargnames, "
11637                                                   "'f' AS prokind, "
11638                                                   "provolatile, proisstrict, prosecdef, "
11639                                                   "false AS proleakproof, "
11640                                                   "null AS proconfig, 0 AS procost, 0 AS prorows, "
11641                                                   "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
11642                                                   "FROM pg_catalog.pg_proc "
11643                                                   "WHERE oid = '%u'::pg_catalog.oid",
11644                                                   finfo->dobj.catId.oid);
11645         }
11646
11647         res = ExecuteSqlQueryForSingleRow(fout, query->data);
11648
11649         proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
11650         prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
11651         probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
11652         if (fout->remoteVersion >= 80400)
11653         {
11654                 funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
11655                 funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
11656                 funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult"));
11657                 proallargtypes = proargmodes = proargnames = NULL;
11658         }
11659         else
11660         {
11661                 proallargtypes = PQgetvalue(res, 0, PQfnumber(res, "proallargtypes"));
11662                 proargmodes = PQgetvalue(res, 0, PQfnumber(res, "proargmodes"));
11663                 proargnames = PQgetvalue(res, 0, PQfnumber(res, "proargnames"));
11664                 funcargs = funciargs = funcresult = NULL;
11665         }
11666         if (PQfnumber(res, "protrftypes") != -1)
11667                 protrftypes = PQgetvalue(res, 0, PQfnumber(res, "protrftypes"));
11668         else
11669                 protrftypes = NULL;
11670         prokind = PQgetvalue(res, 0, PQfnumber(res, "prokind"));
11671         provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
11672         proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
11673         prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
11674         proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof"));
11675         proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig"));
11676         procost = PQgetvalue(res, 0, PQfnumber(res, "procost"));
11677         prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows"));
11678
11679         if (PQfnumber(res, "proparallel") != -1)
11680                 proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
11681         else
11682                 proparallel = NULL;
11683
11684         lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
11685
11686         /*
11687          * See backend/commands/functioncmds.c for details of how the 'AS' clause
11688          * is used.  In 8.4 and up, an unused probin is NULL (here ""); previous
11689          * versions would set it to "-".  There are no known cases in which prosrc
11690          * is unused, so the tests below for "-" are probably useless.
11691          */
11692         if (probin[0] != '\0' && strcmp(probin, "-") != 0)
11693         {
11694                 appendPQExpBufferStr(asPart, "AS ");
11695                 appendStringLiteralAH(asPart, probin, fout);
11696                 if (strcmp(prosrc, "-") != 0)
11697                 {
11698                         appendPQExpBufferStr(asPart, ", ");
11699
11700                         /*
11701                          * where we have bin, use dollar quoting if allowed and src
11702                          * contains quote or backslash; else use regular quoting.
11703                          */
11704                         if (dopt->disable_dollar_quoting ||
11705                                 (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL))
11706                                 appendStringLiteralAH(asPart, prosrc, fout);
11707                         else
11708                                 appendStringLiteralDQ(asPart, prosrc, NULL);
11709                 }
11710         }
11711         else
11712         {
11713                 if (strcmp(prosrc, "-") != 0)
11714                 {
11715                         appendPQExpBufferStr(asPart, "AS ");
11716                         /* with no bin, dollar quote src unconditionally if allowed */
11717                         if (dopt->disable_dollar_quoting)
11718                                 appendStringLiteralAH(asPart, prosrc, fout);
11719                         else
11720                                 appendStringLiteralDQ(asPart, prosrc, NULL);
11721                 }
11722         }
11723
11724         nallargs = finfo->nargs;        /* unless we learn different from allargs */
11725
11726         if (proallargtypes && *proallargtypes)
11727         {
11728                 int                     nitems = 0;
11729
11730                 if (!parsePGArray(proallargtypes, &allargtypes, &nitems) ||
11731                         nitems < finfo->nargs)
11732                 {
11733                         write_msg(NULL, "WARNING: could not parse proallargtypes array\n");
11734                         if (allargtypes)
11735                                 free(allargtypes);
11736                         allargtypes = NULL;
11737                 }
11738                 else
11739                         nallargs = nitems;
11740         }
11741
11742         if (proargmodes && *proargmodes)
11743         {
11744                 int                     nitems = 0;
11745
11746                 if (!parsePGArray(proargmodes, &argmodes, &nitems) ||
11747                         nitems != nallargs)
11748                 {
11749                         write_msg(NULL, "WARNING: could not parse proargmodes array\n");
11750                         if (argmodes)
11751                                 free(argmodes);
11752                         argmodes = NULL;
11753                 }
11754         }
11755
11756         if (proargnames && *proargnames)
11757         {
11758                 int                     nitems = 0;
11759
11760                 if (!parsePGArray(proargnames, &argnames, &nitems) ||
11761                         nitems != nallargs)
11762                 {
11763                         write_msg(NULL, "WARNING: could not parse proargnames array\n");
11764                         if (argnames)
11765                                 free(argnames);
11766                         argnames = NULL;
11767                 }
11768         }
11769
11770         if (proconfig && *proconfig)
11771         {
11772                 if (!parsePGArray(proconfig, &configitems, &nconfigitems))
11773                 {
11774                         write_msg(NULL, "WARNING: could not parse proconfig array\n");
11775                         if (configitems)
11776                                 free(configitems);
11777                         configitems = NULL;
11778                         nconfigitems = 0;
11779                 }
11780         }
11781
11782         if (funcargs)
11783         {
11784                 /* 8.4 or later; we rely on server-side code for most of the work */
11785                 funcfullsig = format_function_arguments(finfo, funcargs, false);
11786                 funcsig = format_function_arguments(finfo, funciargs, false);
11787         }
11788         else
11789                 /* pre-8.4, do it ourselves */
11790                 funcsig = format_function_arguments_old(fout,
11791                                                                                                 finfo, nallargs, allargtypes,
11792                                                                                                 argmodes, argnames);
11793
11794         funcsig_tag = format_function_signature(fout, finfo, false);
11795
11796         if (prokind[0] == PROKIND_PROCEDURE)
11797                 keyword = "PROCEDURE";
11798         else
11799                 keyword = "FUNCTION";   /* works for window functions too */
11800
11801         appendPQExpBuffer(delqry, "DROP %s %s.%s;\n",
11802                                           keyword,
11803                                           fmtId(finfo->dobj.namespace->dobj.name),
11804                                           funcsig);
11805
11806         appendPQExpBuffer(q, "CREATE %s %s.%s",
11807                                           keyword,
11808                                           fmtId(finfo->dobj.namespace->dobj.name),
11809                                           funcfullsig ? funcfullsig :
11810                                           funcsig);
11811
11812         if (prokind[0] == PROKIND_PROCEDURE)
11813                  /* no result type to output */ ;
11814         else if (funcresult)
11815                 appendPQExpBuffer(q, " RETURNS %s", funcresult);
11816         else
11817         {
11818                 rettypename = getFormattedTypeName(fout, finfo->prorettype,
11819                                                                                    zeroAsOpaque);
11820                 appendPQExpBuffer(q, " RETURNS %s%s",
11821                                                   (proretset[0] == 't') ? "SETOF " : "",
11822                                                   rettypename);
11823                 free(rettypename);
11824         }
11825
11826         appendPQExpBuffer(q, "\n    LANGUAGE %s", fmtId(lanname));
11827
11828         if (protrftypes != NULL && strcmp(protrftypes, "") != 0)
11829         {
11830                 Oid                *typeids = palloc(FUNC_MAX_ARGS * sizeof(Oid));
11831                 int                     i;
11832
11833                 appendPQExpBufferStr(q, " TRANSFORM ");
11834                 parseOidArray(protrftypes, typeids, FUNC_MAX_ARGS);
11835                 for (i = 0; typeids[i]; i++)
11836                 {
11837                         if (i != 0)
11838                                 appendPQExpBufferStr(q, ", ");
11839                         appendPQExpBuffer(q, "FOR TYPE %s",
11840                                                           getFormattedTypeName(fout, typeids[i], zeroAsNone));
11841                 }
11842         }
11843
11844         if (prokind[0] == PROKIND_WINDOW)
11845                 appendPQExpBufferStr(q, " WINDOW");
11846
11847         if (provolatile[0] != PROVOLATILE_VOLATILE)
11848         {
11849                 if (provolatile[0] == PROVOLATILE_IMMUTABLE)
11850                         appendPQExpBufferStr(q, " IMMUTABLE");
11851                 else if (provolatile[0] == PROVOLATILE_STABLE)
11852                         appendPQExpBufferStr(q, " STABLE");
11853                 else if (provolatile[0] != PROVOLATILE_VOLATILE)
11854                         exit_horribly(NULL, "unrecognized provolatile value for function \"%s\"\n",
11855                                                   finfo->dobj.name);
11856         }
11857
11858         if (proisstrict[0] == 't')
11859                 appendPQExpBufferStr(q, " STRICT");
11860
11861         if (prosecdef[0] == 't')
11862                 appendPQExpBufferStr(q, " SECURITY DEFINER");
11863
11864         if (proleakproof[0] == 't')
11865                 appendPQExpBufferStr(q, " LEAKPROOF");
11866
11867         /*
11868          * COST and ROWS are emitted only if present and not default, so as not to
11869          * break backwards-compatibility of the dump without need.  Keep this code
11870          * in sync with the defaults in functioncmds.c.
11871          */
11872         if (strcmp(procost, "0") != 0)
11873         {
11874                 if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0)
11875                 {
11876                         /* default cost is 1 */
11877                         if (strcmp(procost, "1") != 0)
11878                                 appendPQExpBuffer(q, " COST %s", procost);
11879                 }
11880                 else
11881                 {
11882                         /* default cost is 100 */
11883                         if (strcmp(procost, "100") != 0)
11884                                 appendPQExpBuffer(q, " COST %s", procost);
11885                 }
11886         }
11887         if (proretset[0] == 't' &&
11888                 strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0)
11889                 appendPQExpBuffer(q, " ROWS %s", prorows);
11890
11891         if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
11892         {
11893                 if (proparallel[0] == PROPARALLEL_SAFE)
11894                         appendPQExpBufferStr(q, " PARALLEL SAFE");
11895                 else if (proparallel[0] == PROPARALLEL_RESTRICTED)
11896                         appendPQExpBufferStr(q, " PARALLEL RESTRICTED");
11897                 else if (proparallel[0] != PROPARALLEL_UNSAFE)
11898                         exit_horribly(NULL, "unrecognized proparallel value for function \"%s\"\n",
11899                                                   finfo->dobj.name);
11900         }
11901
11902         for (i = 0; i < nconfigitems; i++)
11903         {
11904                 /* we feel free to scribble on configitems[] here */
11905                 char       *configitem = configitems[i];
11906                 char       *pos;
11907
11908                 pos = strchr(configitem, '=');
11909                 if (pos == NULL)
11910                         continue;
11911                 *pos++ = '\0';
11912                 appendPQExpBuffer(q, "\n    SET %s TO ", fmtId(configitem));
11913
11914                 /*
11915                  * Variables that are marked GUC_LIST_QUOTE were already fully quoted
11916                  * by flatten_set_variable_args() before they were put into the
11917                  * proconfig array; we mustn't re-quote them or we'll make a mess.
11918                  * Variables that are not so marked should just be emitted as simple
11919                  * string literals.  If the variable is not known to
11920                  * variable_is_guc_list_quote(), we'll do the latter; this makes it
11921                  * unsafe to use GUC_LIST_QUOTE for extension variables.
11922                  */
11923                 if (variable_is_guc_list_quote(configitem))
11924                         appendPQExpBufferStr(q, pos);
11925                 else
11926                         appendStringLiteralAH(q, pos, fout);
11927         }
11928
11929         appendPQExpBuffer(q, "\n    %s;\n", asPart->data);
11930
11931         if (dopt->binary_upgrade)
11932                 binary_upgrade_extension_member(q, &finfo->dobj,
11933                                                                                 keyword, funcsig,
11934                                                                                 finfo->dobj.namespace->dobj.name);
11935
11936         if (finfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
11937                 ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
11938                                          funcsig_tag,
11939                                          finfo->dobj.namespace->dobj.name,
11940                                          NULL,
11941                                          finfo->rolname, false,
11942                                          keyword, SECTION_PRE_DATA,
11943                                          q->data, delqry->data, NULL,
11944                                          NULL, 0,
11945                                          NULL, NULL);
11946
11947         /* Dump Function Comments and Security Labels */
11948         if (finfo->dobj.dump & DUMP_COMPONENT_COMMENT)
11949                 dumpComment(fout, keyword, funcsig,
11950                                         finfo->dobj.namespace->dobj.name, finfo->rolname,
11951                                         finfo->dobj.catId, 0, finfo->dobj.dumpId);
11952
11953         if (finfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
11954                 dumpSecLabel(fout, keyword, funcsig,
11955                                          finfo->dobj.namespace->dobj.name, finfo->rolname,
11956                                          finfo->dobj.catId, 0, finfo->dobj.dumpId);
11957
11958         if (finfo->dobj.dump & DUMP_COMPONENT_ACL)
11959                 dumpACL(fout, finfo->dobj.catId, finfo->dobj.dumpId, keyword,
11960                                 funcsig, NULL,
11961                                 finfo->dobj.namespace->dobj.name,
11962                                 finfo->rolname, finfo->proacl, finfo->rproacl,
11963                                 finfo->initproacl, finfo->initrproacl);
11964
11965         PQclear(res);
11966
11967         destroyPQExpBuffer(query);
11968         destroyPQExpBuffer(q);
11969         destroyPQExpBuffer(delqry);
11970         destroyPQExpBuffer(asPart);
11971         free(funcsig);
11972         if (funcfullsig)
11973                 free(funcfullsig);
11974         free(funcsig_tag);
11975         if (allargtypes)
11976                 free(allargtypes);
11977         if (argmodes)
11978                 free(argmodes);
11979         if (argnames)
11980                 free(argnames);
11981         if (configitems)
11982                 free(configitems);
11983 }
11984
11985
11986 /*
11987  * Dump a user-defined cast
11988  */
11989 static void
11990 dumpCast(Archive *fout, CastInfo *cast)
11991 {
11992         DumpOptions *dopt = fout->dopt;
11993         PQExpBuffer defqry;
11994         PQExpBuffer delqry;
11995         PQExpBuffer labelq;
11996         PQExpBuffer castargs;
11997         FuncInfo   *funcInfo = NULL;
11998         char       *sourceType;
11999         char       *targetType;
12000
12001         /* Skip if not to be dumped */
12002         if (!cast->dobj.dump || dopt->dataOnly)
12003                 return;
12004
12005         /* Cannot dump if we don't have the cast function's info */
12006         if (OidIsValid(cast->castfunc))
12007         {
12008                 funcInfo = findFuncByOid(cast->castfunc);
12009                 if (funcInfo == NULL)
12010                         exit_horribly(NULL, "could not find function definition for function with OID %u\n",
12011                                                   cast->castfunc);
12012         }
12013
12014         defqry = createPQExpBuffer();
12015         delqry = createPQExpBuffer();
12016         labelq = createPQExpBuffer();
12017         castargs = createPQExpBuffer();
12018
12019         sourceType = getFormattedTypeName(fout, cast->castsource, zeroAsNone);
12020         targetType = getFormattedTypeName(fout, cast->casttarget, zeroAsNone);
12021         appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
12022                                           sourceType, targetType);
12023
12024         appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
12025                                           sourceType, targetType);
12026
12027         switch (cast->castmethod)
12028         {
12029                 case COERCION_METHOD_BINARY:
12030                         appendPQExpBufferStr(defqry, "WITHOUT FUNCTION");
12031                         break;
12032                 case COERCION_METHOD_INOUT:
12033                         appendPQExpBufferStr(defqry, "WITH INOUT");
12034                         break;
12035                 case COERCION_METHOD_FUNCTION:
12036                         if (funcInfo)
12037                         {
12038                                 char       *fsig = format_function_signature(fout, funcInfo, true);
12039
12040                                 /*
12041                                  * Always qualify the function name (format_function_signature
12042                                  * won't qualify it).
12043                                  */
12044                                 appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
12045                                                                   fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
12046                                 free(fsig);
12047                         }
12048                         else
12049                                 write_msg(NULL, "WARNING: bogus value in pg_cast.castfunc or pg_cast.castmethod field\n");
12050                         break;
12051                 default:
12052                         write_msg(NULL, "WARNING: bogus value in pg_cast.castmethod field\n");
12053         }
12054
12055         if (cast->castcontext == 'a')
12056                 appendPQExpBufferStr(defqry, " AS ASSIGNMENT");
12057         else if (cast->castcontext == 'i')
12058                 appendPQExpBufferStr(defqry, " AS IMPLICIT");
12059         appendPQExpBufferStr(defqry, ";\n");
12060
12061         appendPQExpBuffer(labelq, "CAST (%s AS %s)",
12062                                           sourceType, targetType);
12063
12064         appendPQExpBuffer(castargs, "(%s AS %s)",
12065                                           sourceType, targetType);
12066
12067         if (dopt->binary_upgrade)
12068                 binary_upgrade_extension_member(defqry, &cast->dobj,
12069                                                                                 "CAST", castargs->data, NULL);
12070
12071         if (cast->dobj.dump & DUMP_COMPONENT_DEFINITION)
12072                 ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
12073                                          labelq->data,
12074                                          NULL, NULL, "",
12075                                          false, "CAST", SECTION_PRE_DATA,
12076                                          defqry->data, delqry->data, NULL,
12077                                          NULL, 0,
12078                                          NULL, NULL);
12079
12080         /* Dump Cast Comments */
12081         if (cast->dobj.dump & DUMP_COMPONENT_COMMENT)
12082                 dumpComment(fout, "CAST", castargs->data,
12083                                         NULL, "",
12084                                         cast->dobj.catId, 0, cast->dobj.dumpId);
12085
12086         free(sourceType);
12087         free(targetType);
12088
12089         destroyPQExpBuffer(defqry);
12090         destroyPQExpBuffer(delqry);
12091         destroyPQExpBuffer(labelq);
12092         destroyPQExpBuffer(castargs);
12093 }
12094
12095 /*
12096  * Dump a transform
12097  */
12098 static void
12099 dumpTransform(Archive *fout, TransformInfo *transform)
12100 {
12101         DumpOptions *dopt = fout->dopt;
12102         PQExpBuffer defqry;
12103         PQExpBuffer delqry;
12104         PQExpBuffer labelq;
12105         PQExpBuffer transformargs;
12106         FuncInfo   *fromsqlFuncInfo = NULL;
12107         FuncInfo   *tosqlFuncInfo = NULL;
12108         char       *lanname;
12109         char       *transformType;
12110
12111         /* Skip if not to be dumped */
12112         if (!transform->dobj.dump || dopt->dataOnly)
12113                 return;
12114
12115         /* Cannot dump if we don't have the transform functions' info */
12116         if (OidIsValid(transform->trffromsql))
12117         {
12118                 fromsqlFuncInfo = findFuncByOid(transform->trffromsql);
12119                 if (fromsqlFuncInfo == NULL)
12120                         exit_horribly(NULL, "could not find function definition for function with OID %u\n",
12121                                                   transform->trffromsql);
12122         }
12123         if (OidIsValid(transform->trftosql))
12124         {
12125                 tosqlFuncInfo = findFuncByOid(transform->trftosql);
12126                 if (tosqlFuncInfo == NULL)
12127                         exit_horribly(NULL, "could not find function definition for function with OID %u\n",
12128                                                   transform->trftosql);
12129         }
12130
12131         defqry = createPQExpBuffer();
12132         delqry = createPQExpBuffer();
12133         labelq = createPQExpBuffer();
12134         transformargs = createPQExpBuffer();
12135
12136         lanname = get_language_name(fout, transform->trflang);
12137         transformType = getFormattedTypeName(fout, transform->trftype, zeroAsNone);
12138
12139         appendPQExpBuffer(delqry, "DROP TRANSFORM FOR %s LANGUAGE %s;\n",
12140                                           transformType, lanname);
12141
12142         appendPQExpBuffer(defqry, "CREATE TRANSFORM FOR %s LANGUAGE %s (",
12143                                           transformType, lanname);
12144
12145         if (!transform->trffromsql && !transform->trftosql)
12146                 write_msg(NULL, "WARNING: bogus transform definition, at least one of trffromsql and trftosql should be nonzero\n");
12147
12148         if (transform->trffromsql)
12149         {
12150                 if (fromsqlFuncInfo)
12151                 {
12152                         char       *fsig = format_function_signature(fout, fromsqlFuncInfo, true);
12153
12154                         /*
12155                          * Always qualify the function name (format_function_signature
12156                          * won't qualify it).
12157                          */
12158                         appendPQExpBuffer(defqry, "FROM SQL WITH FUNCTION %s.%s",
12159                                                           fmtId(fromsqlFuncInfo->dobj.namespace->dobj.name), fsig);
12160                         free(fsig);
12161                 }
12162                 else
12163                         write_msg(NULL, "WARNING: bogus value in pg_transform.trffromsql field\n");
12164         }
12165
12166         if (transform->trftosql)
12167         {
12168                 if (transform->trffromsql)
12169                         appendPQExpBuffer(defqry, ", ");
12170
12171                 if (tosqlFuncInfo)
12172                 {
12173                         char       *fsig = format_function_signature(fout, tosqlFuncInfo, true);
12174
12175                         /*
12176                          * Always qualify the function name (format_function_signature
12177                          * won't qualify it).
12178                          */
12179                         appendPQExpBuffer(defqry, "TO SQL WITH FUNCTION %s.%s",
12180                                                           fmtId(tosqlFuncInfo->dobj.namespace->dobj.name), fsig);
12181                         free(fsig);
12182                 }
12183                 else
12184                         write_msg(NULL, "WARNING: bogus value in pg_transform.trftosql field\n");
12185         }
12186
12187         appendPQExpBuffer(defqry, ");\n");
12188
12189         appendPQExpBuffer(labelq, "TRANSFORM FOR %s LANGUAGE %s",
12190                                           transformType, lanname);
12191
12192         appendPQExpBuffer(transformargs, "FOR %s LANGUAGE %s",
12193                                           transformType, lanname);
12194
12195         if (dopt->binary_upgrade)
12196                 binary_upgrade_extension_member(defqry, &transform->dobj,
12197                                                                                 "TRANSFORM", transformargs->data, NULL);
12198
12199         if (transform->dobj.dump & DUMP_COMPONENT_DEFINITION)
12200                 ArchiveEntry(fout, transform->dobj.catId, transform->dobj.dumpId,
12201                                          labelq->data,
12202                                          NULL, NULL, "",
12203                                          false, "TRANSFORM", SECTION_PRE_DATA,
12204                                          defqry->data, delqry->data, NULL,
12205                                          transform->dobj.dependencies, transform->dobj.nDeps,
12206                                          NULL, NULL);
12207
12208         /* Dump Transform Comments */
12209         if (transform->dobj.dump & DUMP_COMPONENT_COMMENT)
12210                 dumpComment(fout, "TRANSFORM", transformargs->data,
12211                                         NULL, "",
12212                                         transform->dobj.catId, 0, transform->dobj.dumpId);
12213
12214         free(lanname);
12215         free(transformType);
12216         destroyPQExpBuffer(defqry);
12217         destroyPQExpBuffer(delqry);
12218         destroyPQExpBuffer(labelq);
12219         destroyPQExpBuffer(transformargs);
12220 }
12221
12222
12223 /*
12224  * dumpOpr
12225  *        write out a single operator definition
12226  */
12227 static void
12228 dumpOpr(Archive *fout, OprInfo *oprinfo)
12229 {
12230         DumpOptions *dopt = fout->dopt;
12231         PQExpBuffer query;
12232         PQExpBuffer q;
12233         PQExpBuffer delq;
12234         PQExpBuffer oprid;
12235         PQExpBuffer details;
12236         PGresult   *res;
12237         int                     i_oprkind;
12238         int                     i_oprcode;
12239         int                     i_oprleft;
12240         int                     i_oprright;
12241         int                     i_oprcom;
12242         int                     i_oprnegate;
12243         int                     i_oprrest;
12244         int                     i_oprjoin;
12245         int                     i_oprcanmerge;
12246         int                     i_oprcanhash;
12247         char       *oprkind;
12248         char       *oprcode;
12249         char       *oprleft;
12250         char       *oprright;
12251         char       *oprcom;
12252         char       *oprnegate;
12253         char       *oprrest;
12254         char       *oprjoin;
12255         char       *oprcanmerge;
12256         char       *oprcanhash;
12257         char       *oprregproc;
12258         char       *oprref;
12259
12260         /* Skip if not to be dumped */
12261         if (!oprinfo->dobj.dump || dopt->dataOnly)
12262                 return;
12263
12264         /*
12265          * some operators are invalid because they were the result of user
12266          * defining operators before commutators exist
12267          */
12268         if (!OidIsValid(oprinfo->oprcode))
12269                 return;
12270
12271         query = createPQExpBuffer();
12272         q = createPQExpBuffer();
12273         delq = createPQExpBuffer();
12274         oprid = createPQExpBuffer();
12275         details = createPQExpBuffer();
12276
12277         if (fout->remoteVersion >= 80300)
12278         {
12279                 appendPQExpBuffer(query, "SELECT oprkind, "
12280                                                   "oprcode::pg_catalog.regprocedure, "
12281                                                   "oprleft::pg_catalog.regtype, "
12282                                                   "oprright::pg_catalog.regtype, "
12283                                                   "oprcom, "
12284                                                   "oprnegate, "
12285                                                   "oprrest::pg_catalog.regprocedure, "
12286                                                   "oprjoin::pg_catalog.regprocedure, "
12287                                                   "oprcanmerge, oprcanhash "
12288                                                   "FROM pg_catalog.pg_operator "
12289                                                   "WHERE oid = '%u'::pg_catalog.oid",
12290                                                   oprinfo->dobj.catId.oid);
12291         }
12292         else
12293         {
12294                 appendPQExpBuffer(query, "SELECT oprkind, "
12295                                                   "oprcode::pg_catalog.regprocedure, "
12296                                                   "oprleft::pg_catalog.regtype, "
12297                                                   "oprright::pg_catalog.regtype, "
12298                                                   "oprcom, "
12299                                                   "oprnegate, "
12300                                                   "oprrest::pg_catalog.regprocedure, "
12301                                                   "oprjoin::pg_catalog.regprocedure, "
12302                                                   "(oprlsortop != 0) AS oprcanmerge, "
12303                                                   "oprcanhash "
12304                                                   "FROM pg_catalog.pg_operator "
12305                                                   "WHERE oid = '%u'::pg_catalog.oid",
12306                                                   oprinfo->dobj.catId.oid);
12307         }
12308
12309         res = ExecuteSqlQueryForSingleRow(fout, query->data);
12310
12311         i_oprkind = PQfnumber(res, "oprkind");
12312         i_oprcode = PQfnumber(res, "oprcode");
12313         i_oprleft = PQfnumber(res, "oprleft");
12314         i_oprright = PQfnumber(res, "oprright");
12315         i_oprcom = PQfnumber(res, "oprcom");
12316         i_oprnegate = PQfnumber(res, "oprnegate");
12317         i_oprrest = PQfnumber(res, "oprrest");
12318         i_oprjoin = PQfnumber(res, "oprjoin");
12319         i_oprcanmerge = PQfnumber(res, "oprcanmerge");
12320         i_oprcanhash = PQfnumber(res, "oprcanhash");
12321
12322         oprkind = PQgetvalue(res, 0, i_oprkind);
12323         oprcode = PQgetvalue(res, 0, i_oprcode);
12324         oprleft = PQgetvalue(res, 0, i_oprleft);
12325         oprright = PQgetvalue(res, 0, i_oprright);
12326         oprcom = PQgetvalue(res, 0, i_oprcom);
12327         oprnegate = PQgetvalue(res, 0, i_oprnegate);
12328         oprrest = PQgetvalue(res, 0, i_oprrest);
12329         oprjoin = PQgetvalue(res, 0, i_oprjoin);
12330         oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
12331         oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
12332
12333         oprregproc = convertRegProcReference(fout, oprcode);
12334         if (oprregproc)
12335         {
12336                 appendPQExpBuffer(details, "    PROCEDURE = %s", oprregproc);
12337                 free(oprregproc);
12338         }
12339
12340         appendPQExpBuffer(oprid, "%s (",
12341                                           oprinfo->dobj.name);
12342
12343         /*
12344          * right unary means there's a left arg and left unary means there's a
12345          * right arg
12346          */
12347         if (strcmp(oprkind, "r") == 0 ||
12348                 strcmp(oprkind, "b") == 0)
12349         {
12350                 appendPQExpBuffer(details, ",\n    LEFTARG = %s", oprleft);
12351                 appendPQExpBufferStr(oprid, oprleft);
12352         }
12353         else
12354                 appendPQExpBufferStr(oprid, "NONE");
12355
12356         if (strcmp(oprkind, "l") == 0 ||
12357                 strcmp(oprkind, "b") == 0)
12358         {
12359                 appendPQExpBuffer(details, ",\n    RIGHTARG = %s", oprright);
12360                 appendPQExpBuffer(oprid, ", %s)", oprright);
12361         }
12362         else
12363                 appendPQExpBufferStr(oprid, ", NONE)");
12364
12365         oprref = getFormattedOperatorName(fout, oprcom);
12366         if (oprref)
12367         {
12368                 appendPQExpBuffer(details, ",\n    COMMUTATOR = %s", oprref);
12369                 free(oprref);
12370         }
12371
12372         oprref = getFormattedOperatorName(fout, oprnegate);
12373         if (oprref)
12374         {
12375                 appendPQExpBuffer(details, ",\n    NEGATOR = %s", oprref);
12376                 free(oprref);
12377         }
12378
12379         if (strcmp(oprcanmerge, "t") == 0)
12380                 appendPQExpBufferStr(details, ",\n    MERGES");
12381
12382         if (strcmp(oprcanhash, "t") == 0)
12383                 appendPQExpBufferStr(details, ",\n    HASHES");
12384
12385         oprregproc = convertRegProcReference(fout, oprrest);
12386         if (oprregproc)
12387         {
12388                 appendPQExpBuffer(details, ",\n    RESTRICT = %s", oprregproc);
12389                 free(oprregproc);
12390         }
12391
12392         oprregproc = convertRegProcReference(fout, oprjoin);
12393         if (oprregproc)
12394         {
12395                 appendPQExpBuffer(details, ",\n    JOIN = %s", oprregproc);
12396                 free(oprregproc);
12397         }
12398
12399         appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
12400                                           fmtId(oprinfo->dobj.namespace->dobj.name),
12401                                           oprid->data);
12402
12403         appendPQExpBuffer(q, "CREATE OPERATOR %s.%s (\n%s\n);\n",
12404                                           fmtId(oprinfo->dobj.namespace->dobj.name),
12405                                           oprinfo->dobj.name, details->data);
12406
12407         if (dopt->binary_upgrade)
12408                 binary_upgrade_extension_member(q, &oprinfo->dobj,
12409                                                                                 "OPERATOR", oprid->data,
12410                                                                                 oprinfo->dobj.namespace->dobj.name);
12411
12412         if (oprinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12413                 ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
12414                                          oprinfo->dobj.name,
12415                                          oprinfo->dobj.namespace->dobj.name,
12416                                          NULL,
12417                                          oprinfo->rolname,
12418                                          false, "OPERATOR", SECTION_PRE_DATA,
12419                                          q->data, delq->data, NULL,
12420                                          NULL, 0,
12421                                          NULL, NULL);
12422
12423         /* Dump Operator Comments */
12424         if (oprinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12425                 dumpComment(fout, "OPERATOR", oprid->data,
12426                                         oprinfo->dobj.namespace->dobj.name, oprinfo->rolname,
12427                                         oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
12428
12429         PQclear(res);
12430
12431         destroyPQExpBuffer(query);
12432         destroyPQExpBuffer(q);
12433         destroyPQExpBuffer(delq);
12434         destroyPQExpBuffer(oprid);
12435         destroyPQExpBuffer(details);
12436 }
12437
12438 /*
12439  * Convert a function reference obtained from pg_operator
12440  *
12441  * Returns allocated string of what to print, or NULL if function references
12442  * is InvalidOid. Returned string is expected to be free'd by the caller.
12443  *
12444  * The input is a REGPROCEDURE display; we have to strip the argument-types
12445  * part.
12446  */
12447 static char *
12448 convertRegProcReference(Archive *fout, const char *proc)
12449 {
12450         char       *name;
12451         char       *paren;
12452         bool            inquote;
12453
12454         /* In all cases "-" means a null reference */
12455         if (strcmp(proc, "-") == 0)
12456                 return NULL;
12457
12458         name = pg_strdup(proc);
12459         /* find non-double-quoted left paren */
12460         inquote = false;
12461         for (paren = name; *paren; paren++)
12462         {
12463                 if (*paren == '(' && !inquote)
12464                 {
12465                         *paren = '\0';
12466                         break;
12467                 }
12468                 if (*paren == '"')
12469                         inquote = !inquote;
12470         }
12471         return name;
12472 }
12473
12474 /*
12475  * getFormattedOperatorName - retrieve the operator name for the
12476  * given operator OID (presented in string form).
12477  *
12478  * Returns an allocated string, or NULL if the given OID is invalid.
12479  * Caller is responsible for free'ing result string.
12480  *
12481  * What we produce has the format "OPERATOR(schema.oprname)".  This is only
12482  * useful in commands where the operator's argument types can be inferred from
12483  * context.  We always schema-qualify the name, though.  The predecessor to
12484  * this code tried to skip the schema qualification if possible, but that led
12485  * to wrong results in corner cases, such as if an operator and its negator
12486  * are in different schemas.
12487  */
12488 static char *
12489 getFormattedOperatorName(Archive *fout, const char *oproid)
12490 {
12491         OprInfo    *oprInfo;
12492
12493         /* In all cases "0" means a null reference */
12494         if (strcmp(oproid, "0") == 0)
12495                 return NULL;
12496
12497         oprInfo = findOprByOid(atooid(oproid));
12498         if (oprInfo == NULL)
12499         {
12500                 write_msg(NULL, "WARNING: could not find operator with OID %s\n",
12501                                   oproid);
12502                 return NULL;
12503         }
12504
12505         return psprintf("OPERATOR(%s.%s)",
12506                                         fmtId(oprInfo->dobj.namespace->dobj.name),
12507                                         oprInfo->dobj.name);
12508 }
12509
12510 /*
12511  * Convert a function OID obtained from pg_ts_parser or pg_ts_template
12512  *
12513  * It is sufficient to use REGPROC rather than REGPROCEDURE, since the
12514  * argument lists of these functions are predetermined.  Note that the
12515  * caller should ensure we are in the proper schema, because the results
12516  * are search path dependent!
12517  */
12518 static char *
12519 convertTSFunction(Archive *fout, Oid funcOid)
12520 {
12521         char       *result;
12522         char            query[128];
12523         PGresult   *res;
12524
12525         snprintf(query, sizeof(query),
12526                          "SELECT '%u'::pg_catalog.regproc", funcOid);
12527         res = ExecuteSqlQueryForSingleRow(fout, query);
12528
12529         result = pg_strdup(PQgetvalue(res, 0, 0));
12530
12531         PQclear(res);
12532
12533         return result;
12534 }
12535
12536 /*
12537  * dumpAccessMethod
12538  *        write out a single access method definition
12539  */
12540 static void
12541 dumpAccessMethod(Archive *fout, AccessMethodInfo *aminfo)
12542 {
12543         DumpOptions *dopt = fout->dopt;
12544         PQExpBuffer q;
12545         PQExpBuffer delq;
12546         char       *qamname;
12547
12548         /* Skip if not to be dumped */
12549         if (!aminfo->dobj.dump || dopt->dataOnly)
12550                 return;
12551
12552         q = createPQExpBuffer();
12553         delq = createPQExpBuffer();
12554
12555         qamname = pg_strdup(fmtId(aminfo->dobj.name));
12556
12557         appendPQExpBuffer(q, "CREATE ACCESS METHOD %s ", qamname);
12558
12559         switch (aminfo->amtype)
12560         {
12561                 case AMTYPE_INDEX:
12562                         appendPQExpBuffer(q, "TYPE INDEX ");
12563                         break;
12564                 default:
12565                         write_msg(NULL, "WARNING: invalid type \"%c\" of access method \"%s\"\n",
12566                                           aminfo->amtype, qamname);
12567                         destroyPQExpBuffer(q);
12568                         destroyPQExpBuffer(delq);
12569                         free(qamname);
12570                         return;
12571         }
12572
12573         appendPQExpBuffer(q, "HANDLER %s;\n", aminfo->amhandler);
12574
12575         appendPQExpBuffer(delq, "DROP ACCESS METHOD %s;\n",
12576                                           qamname);
12577
12578         if (dopt->binary_upgrade)
12579                 binary_upgrade_extension_member(q, &aminfo->dobj,
12580                                                                                 "ACCESS METHOD", qamname, NULL);
12581
12582         if (aminfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12583                 ArchiveEntry(fout, aminfo->dobj.catId, aminfo->dobj.dumpId,
12584                                          aminfo->dobj.name,
12585                                          NULL,
12586                                          NULL,
12587                                          "",
12588                                          false, "ACCESS METHOD", SECTION_PRE_DATA,
12589                                          q->data, delq->data, NULL,
12590                                          NULL, 0,
12591                                          NULL, NULL);
12592
12593         /* Dump Access Method Comments */
12594         if (aminfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12595                 dumpComment(fout, "ACCESS METHOD", qamname,
12596                                         NULL, "",
12597                                         aminfo->dobj.catId, 0, aminfo->dobj.dumpId);
12598
12599         destroyPQExpBuffer(q);
12600         destroyPQExpBuffer(delq);
12601         free(qamname);
12602 }
12603
12604 /*
12605  * dumpOpclass
12606  *        write out a single operator class definition
12607  */
12608 static void
12609 dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
12610 {
12611         DumpOptions *dopt = fout->dopt;
12612         PQExpBuffer query;
12613         PQExpBuffer q;
12614         PQExpBuffer delq;
12615         PQExpBuffer nameusing;
12616         PGresult   *res;
12617         int                     ntups;
12618         int                     i_opcintype;
12619         int                     i_opckeytype;
12620         int                     i_opcdefault;
12621         int                     i_opcfamily;
12622         int                     i_opcfamilyname;
12623         int                     i_opcfamilynsp;
12624         int                     i_amname;
12625         int                     i_amopstrategy;
12626         int                     i_amopreqcheck;
12627         int                     i_amopopr;
12628         int                     i_sortfamily;
12629         int                     i_sortfamilynsp;
12630         int                     i_amprocnum;
12631         int                     i_amproc;
12632         int                     i_amproclefttype;
12633         int                     i_amprocrighttype;
12634         char       *opcintype;
12635         char       *opckeytype;
12636         char       *opcdefault;
12637         char       *opcfamily;
12638         char       *opcfamilyname;
12639         char       *opcfamilynsp;
12640         char       *amname;
12641         char       *amopstrategy;
12642         char       *amopreqcheck;
12643         char       *amopopr;
12644         char       *sortfamily;
12645         char       *sortfamilynsp;
12646         char       *amprocnum;
12647         char       *amproc;
12648         char       *amproclefttype;
12649         char       *amprocrighttype;
12650         bool            needComma;
12651         int                     i;
12652
12653         /* Skip if not to be dumped */
12654         if (!opcinfo->dobj.dump || dopt->dataOnly)
12655                 return;
12656
12657         query = createPQExpBuffer();
12658         q = createPQExpBuffer();
12659         delq = createPQExpBuffer();
12660         nameusing = createPQExpBuffer();
12661
12662         /* Get additional fields from the pg_opclass row */
12663         if (fout->remoteVersion >= 80300)
12664         {
12665                 appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
12666                                                   "opckeytype::pg_catalog.regtype, "
12667                                                   "opcdefault, opcfamily, "
12668                                                   "opfname AS opcfamilyname, "
12669                                                   "nspname AS opcfamilynsp, "
12670                                                   "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
12671                                                   "FROM pg_catalog.pg_opclass c "
12672                                                   "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
12673                                                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
12674                                                   "WHERE c.oid = '%u'::pg_catalog.oid",
12675                                                   opcinfo->dobj.catId.oid);
12676         }
12677         else
12678         {
12679                 appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
12680                                                   "opckeytype::pg_catalog.regtype, "
12681                                                   "opcdefault, NULL AS opcfamily, "
12682                                                   "NULL AS opcfamilyname, "
12683                                                   "NULL AS opcfamilynsp, "
12684                                                   "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcamid) AS amname "
12685                                                   "FROM pg_catalog.pg_opclass "
12686                                                   "WHERE oid = '%u'::pg_catalog.oid",
12687                                                   opcinfo->dobj.catId.oid);
12688         }
12689
12690         res = ExecuteSqlQueryForSingleRow(fout, query->data);
12691
12692         i_opcintype = PQfnumber(res, "opcintype");
12693         i_opckeytype = PQfnumber(res, "opckeytype");
12694         i_opcdefault = PQfnumber(res, "opcdefault");
12695         i_opcfamily = PQfnumber(res, "opcfamily");
12696         i_opcfamilyname = PQfnumber(res, "opcfamilyname");
12697         i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
12698         i_amname = PQfnumber(res, "amname");
12699
12700         /* opcintype may still be needed after we PQclear res */
12701         opcintype = pg_strdup(PQgetvalue(res, 0, i_opcintype));
12702         opckeytype = PQgetvalue(res, 0, i_opckeytype);
12703         opcdefault = PQgetvalue(res, 0, i_opcdefault);
12704         /* opcfamily will still be needed after we PQclear res */
12705         opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily));
12706         opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname);
12707         opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
12708         /* amname will still be needed after we PQclear res */
12709         amname = pg_strdup(PQgetvalue(res, 0, i_amname));
12710
12711         appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
12712                                           fmtQualifiedDumpable(opcinfo));
12713         appendPQExpBuffer(delq, " USING %s;\n",
12714                                           fmtId(amname));
12715
12716         /* Build the fixed portion of the CREATE command */
12717         appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n    ",
12718                                           fmtQualifiedDumpable(opcinfo));
12719         if (strcmp(opcdefault, "t") == 0)
12720                 appendPQExpBufferStr(q, "DEFAULT ");
12721         appendPQExpBuffer(q, "FOR TYPE %s USING %s",
12722                                           opcintype,
12723                                           fmtId(amname));
12724         if (strlen(opcfamilyname) > 0)
12725         {
12726                 appendPQExpBufferStr(q, " FAMILY ");
12727                 appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
12728                 appendPQExpBufferStr(q, fmtId(opcfamilyname));
12729         }
12730         appendPQExpBufferStr(q, " AS\n    ");
12731
12732         needComma = false;
12733
12734         if (strcmp(opckeytype, "-") != 0)
12735         {
12736                 appendPQExpBuffer(q, "STORAGE %s",
12737                                                   opckeytype);
12738                 needComma = true;
12739         }
12740
12741         PQclear(res);
12742
12743         /*
12744          * Now fetch and print the OPERATOR entries (pg_amop rows).
12745          *
12746          * Print only those opfamily members that are tied to the opclass by
12747          * pg_depend entries.
12748          *
12749          * XXX RECHECK is gone as of 8.4, but we'll still print it if dumping an
12750          * older server's opclass in which it is used.  This is to avoid
12751          * hard-to-detect breakage if a newer pg_dump is used to dump from an
12752          * older server and then reload into that old version.  This can go away
12753          * once 8.3 is so old as to not be of interest to anyone.
12754          */
12755         resetPQExpBuffer(query);
12756
12757         if (fout->remoteVersion >= 90100)
12758         {
12759                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
12760                                                   "amopopr::pg_catalog.regoperator, "
12761                                                   "opfname AS sortfamily, "
12762                                                   "nspname AS sortfamilynsp "
12763                                                   "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
12764                                                   "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
12765                                                   "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
12766                                                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
12767                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12768                                                   "AND refobjid = '%u'::pg_catalog.oid "
12769                                                   "AND amopfamily = '%s'::pg_catalog.oid "
12770                                                   "ORDER BY amopstrategy",
12771                                                   opcinfo->dobj.catId.oid,
12772                                                   opcfamily);
12773         }
12774         else if (fout->remoteVersion >= 80400)
12775         {
12776                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
12777                                                   "amopopr::pg_catalog.regoperator, "
12778                                                   "NULL AS sortfamily, "
12779                                                   "NULL AS sortfamilynsp "
12780                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
12781                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12782                                                   "AND refobjid = '%u'::pg_catalog.oid "
12783                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
12784                                                   "AND objid = ao.oid "
12785                                                   "ORDER BY amopstrategy",
12786                                                   opcinfo->dobj.catId.oid);
12787         }
12788         else if (fout->remoteVersion >= 80300)
12789         {
12790                 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
12791                                                   "amopopr::pg_catalog.regoperator, "
12792                                                   "NULL AS sortfamily, "
12793                                                   "NULL AS sortfamilynsp "
12794                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
12795                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12796                                                   "AND refobjid = '%u'::pg_catalog.oid "
12797                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
12798                                                   "AND objid = ao.oid "
12799                                                   "ORDER BY amopstrategy",
12800                                                   opcinfo->dobj.catId.oid);
12801         }
12802         else
12803         {
12804                 /*
12805                  * Here, we print all entries since there are no opfamilies and hence
12806                  * no loose operators to worry about.
12807                  */
12808                 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
12809                                                   "amopopr::pg_catalog.regoperator, "
12810                                                   "NULL AS sortfamily, "
12811                                                   "NULL AS sortfamilynsp "
12812                                                   "FROM pg_catalog.pg_amop "
12813                                                   "WHERE amopclaid = '%u'::pg_catalog.oid "
12814                                                   "ORDER BY amopstrategy",
12815                                                   opcinfo->dobj.catId.oid);
12816         }
12817
12818         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12819
12820         ntups = PQntuples(res);
12821
12822         i_amopstrategy = PQfnumber(res, "amopstrategy");
12823         i_amopreqcheck = PQfnumber(res, "amopreqcheck");
12824         i_amopopr = PQfnumber(res, "amopopr");
12825         i_sortfamily = PQfnumber(res, "sortfamily");
12826         i_sortfamilynsp = PQfnumber(res, "sortfamilynsp");
12827
12828         for (i = 0; i < ntups; i++)
12829         {
12830                 amopstrategy = PQgetvalue(res, i, i_amopstrategy);
12831                 amopreqcheck = PQgetvalue(res, i, i_amopreqcheck);
12832                 amopopr = PQgetvalue(res, i, i_amopopr);
12833                 sortfamily = PQgetvalue(res, i, i_sortfamily);
12834                 sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp);
12835
12836                 if (needComma)
12837                         appendPQExpBufferStr(q, " ,\n    ");
12838
12839                 appendPQExpBuffer(q, "OPERATOR %s %s",
12840                                                   amopstrategy, amopopr);
12841
12842                 if (strlen(sortfamily) > 0)
12843                 {
12844                         appendPQExpBufferStr(q, " FOR ORDER BY ");
12845                         appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
12846                         appendPQExpBufferStr(q, fmtId(sortfamily));
12847                 }
12848
12849                 if (strcmp(amopreqcheck, "t") == 0)
12850                         appendPQExpBufferStr(q, " RECHECK");
12851
12852                 needComma = true;
12853         }
12854
12855         PQclear(res);
12856
12857         /*
12858          * Now fetch and print the FUNCTION entries (pg_amproc rows).
12859          *
12860          * Print only those opfamily members that are tied to the opclass by
12861          * pg_depend entries.
12862          *
12863          * We print the amproclefttype/amprocrighttype even though in most cases
12864          * the backend could deduce the right values, because of the corner case
12865          * of a btree sort support function for a cross-type comparison.  That's
12866          * only allowed in 9.2 and later, but for simplicity print them in all
12867          * versions that have the columns.
12868          */
12869         resetPQExpBuffer(query);
12870
12871         if (fout->remoteVersion >= 80300)
12872         {
12873                 appendPQExpBuffer(query, "SELECT amprocnum, "
12874                                                   "amproc::pg_catalog.regprocedure, "
12875                                                   "amproclefttype::pg_catalog.regtype, "
12876                                                   "amprocrighttype::pg_catalog.regtype "
12877                                                   "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
12878                                                   "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
12879                                                   "AND refobjid = '%u'::pg_catalog.oid "
12880                                                   "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
12881                                                   "AND objid = ap.oid "
12882                                                   "ORDER BY amprocnum",
12883                                                   opcinfo->dobj.catId.oid);
12884         }
12885         else
12886         {
12887                 appendPQExpBuffer(query, "SELECT amprocnum, "
12888                                                   "amproc::pg_catalog.regprocedure, "
12889                                                   "'' AS amproclefttype, "
12890                                                   "'' AS amprocrighttype "
12891                                                   "FROM pg_catalog.pg_amproc "
12892                                                   "WHERE amopclaid = '%u'::pg_catalog.oid "
12893                                                   "ORDER BY amprocnum",
12894                                                   opcinfo->dobj.catId.oid);
12895         }
12896
12897         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12898
12899         ntups = PQntuples(res);
12900
12901         i_amprocnum = PQfnumber(res, "amprocnum");
12902         i_amproc = PQfnumber(res, "amproc");
12903         i_amproclefttype = PQfnumber(res, "amproclefttype");
12904         i_amprocrighttype = PQfnumber(res, "amprocrighttype");
12905
12906         for (i = 0; i < ntups; i++)
12907         {
12908                 amprocnum = PQgetvalue(res, i, i_amprocnum);
12909                 amproc = PQgetvalue(res, i, i_amproc);
12910                 amproclefttype = PQgetvalue(res, i, i_amproclefttype);
12911                 amprocrighttype = PQgetvalue(res, i, i_amprocrighttype);
12912
12913                 if (needComma)
12914                         appendPQExpBufferStr(q, " ,\n    ");
12915
12916                 appendPQExpBuffer(q, "FUNCTION %s", amprocnum);
12917
12918                 if (*amproclefttype && *amprocrighttype)
12919                         appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype);
12920
12921                 appendPQExpBuffer(q, " %s", amproc);
12922
12923                 needComma = true;
12924         }
12925
12926         PQclear(res);
12927
12928         /*
12929          * If needComma is still false it means we haven't added anything after
12930          * the AS keyword.  To avoid printing broken SQL, append a dummy STORAGE
12931          * clause with the same datatype.  This isn't sanctioned by the
12932          * documentation, but actually DefineOpClass will treat it as a no-op.
12933          */
12934         if (!needComma)
12935                 appendPQExpBuffer(q, "STORAGE %s", opcintype);
12936
12937         appendPQExpBufferStr(q, ";\n");
12938
12939         appendPQExpBufferStr(nameusing, fmtId(opcinfo->dobj.name));
12940         appendPQExpBuffer(nameusing, " USING %s",
12941                                           fmtId(amname));
12942
12943         if (dopt->binary_upgrade)
12944                 binary_upgrade_extension_member(q, &opcinfo->dobj,
12945                                                                                 "OPERATOR CLASS", nameusing->data,
12946                                                                                 opcinfo->dobj.namespace->dobj.name);
12947
12948         if (opcinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
12949                 ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
12950                                          opcinfo->dobj.name,
12951                                          opcinfo->dobj.namespace->dobj.name,
12952                                          NULL,
12953                                          opcinfo->rolname,
12954                                          false, "OPERATOR CLASS", SECTION_PRE_DATA,
12955                                          q->data, delq->data, NULL,
12956                                          NULL, 0,
12957                                          NULL, NULL);
12958
12959         /* Dump Operator Class Comments */
12960         if (opcinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
12961                 dumpComment(fout, "OPERATOR CLASS", nameusing->data,
12962                                         opcinfo->dobj.namespace->dobj.name, opcinfo->rolname,
12963                                         opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
12964
12965         free(opcintype);
12966         free(opcfamily);
12967         free(amname);
12968         destroyPQExpBuffer(query);
12969         destroyPQExpBuffer(q);
12970         destroyPQExpBuffer(delq);
12971         destroyPQExpBuffer(nameusing);
12972 }
12973
12974 /*
12975  * dumpOpfamily
12976  *        write out a single operator family definition
12977  *
12978  * Note: this also dumps any "loose" operator members that aren't bound to a
12979  * specific opclass within the opfamily.
12980  */
12981 static void
12982 dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
12983 {
12984         DumpOptions *dopt = fout->dopt;
12985         PQExpBuffer query;
12986         PQExpBuffer q;
12987         PQExpBuffer delq;
12988         PQExpBuffer nameusing;
12989         PGresult   *res;
12990         PGresult   *res_ops;
12991         PGresult   *res_procs;
12992         int                     ntups;
12993         int                     i_amname;
12994         int                     i_amopstrategy;
12995         int                     i_amopreqcheck;
12996         int                     i_amopopr;
12997         int                     i_sortfamily;
12998         int                     i_sortfamilynsp;
12999         int                     i_amprocnum;
13000         int                     i_amproc;
13001         int                     i_amproclefttype;
13002         int                     i_amprocrighttype;
13003         char       *amname;
13004         char       *amopstrategy;
13005         char       *amopreqcheck;
13006         char       *amopopr;
13007         char       *sortfamily;
13008         char       *sortfamilynsp;
13009         char       *amprocnum;
13010         char       *amproc;
13011         char       *amproclefttype;
13012         char       *amprocrighttype;
13013         bool            needComma;
13014         int                     i;
13015
13016         /* Skip if not to be dumped */
13017         if (!opfinfo->dobj.dump || dopt->dataOnly)
13018                 return;
13019
13020         query = createPQExpBuffer();
13021         q = createPQExpBuffer();
13022         delq = createPQExpBuffer();
13023         nameusing = createPQExpBuffer();
13024
13025         /*
13026          * Fetch only those opfamily members that are tied directly to the
13027          * opfamily by pg_depend entries.
13028          *
13029          * XXX RECHECK is gone as of 8.4, but we'll still print it if dumping an
13030          * older server's opclass in which it is used.  This is to avoid
13031          * hard-to-detect breakage if a newer pg_dump is used to dump from an
13032          * older server and then reload into that old version.  This can go away
13033          * once 8.3 is so old as to not be of interest to anyone.
13034          */
13035         if (fout->remoteVersion >= 90100)
13036         {
13037                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
13038                                                   "amopopr::pg_catalog.regoperator, "
13039                                                   "opfname AS sortfamily, "
13040                                                   "nspname AS sortfamilynsp "
13041                                                   "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
13042                                                   "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
13043                                                   "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
13044                                                   "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
13045                                                   "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
13046                                                   "AND refobjid = '%u'::pg_catalog.oid "
13047                                                   "AND amopfamily = '%u'::pg_catalog.oid "
13048                                                   "ORDER BY amopstrategy",
13049                                                   opfinfo->dobj.catId.oid,
13050                                                   opfinfo->dobj.catId.oid);
13051         }
13052         else if (fout->remoteVersion >= 80400)
13053         {
13054                 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
13055                                                   "amopopr::pg_catalog.regoperator, "
13056                                                   "NULL AS sortfamily, "
13057                                                   "NULL AS sortfamilynsp "
13058                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
13059                                                   "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
13060                                                   "AND refobjid = '%u'::pg_catalog.oid "
13061                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
13062                                                   "AND objid = ao.oid "
13063                                                   "ORDER BY amopstrategy",
13064                                                   opfinfo->dobj.catId.oid);
13065         }
13066         else
13067         {
13068                 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
13069                                                   "amopopr::pg_catalog.regoperator, "
13070                                                   "NULL AS sortfamily, "
13071                                                   "NULL AS sortfamilynsp "
13072                                                   "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
13073                                                   "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
13074                                                   "AND refobjid = '%u'::pg_catalog.oid "
13075                                                   "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
13076                                                   "AND objid = ao.oid "
13077                                                   "ORDER BY amopstrategy",
13078                                                   opfinfo->dobj.catId.oid);
13079         }
13080
13081         res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
13082
13083         resetPQExpBuffer(query);
13084
13085         appendPQExpBuffer(query, "SELECT amprocnum, "
13086                                           "amproc::pg_catalog.regprocedure, "
13087                                           "amproclefttype::pg_catalog.regtype, "
13088                                           "amprocrighttype::pg_catalog.regtype "
13089                                           "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
13090                                           "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
13091                                           "AND refobjid = '%u'::pg_catalog.oid "
13092                                           "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
13093                                           "AND objid = ap.oid "
13094                                           "ORDER BY amprocnum",
13095                                           opfinfo->dobj.catId.oid);
13096
13097         res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
13098
13099         /* Get additional fields from the pg_opfamily row */
13100         resetPQExpBuffer(query);
13101
13102         appendPQExpBuffer(query, "SELECT "
13103                                           "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
13104                                           "FROM pg_catalog.pg_opfamily "
13105                                           "WHERE oid = '%u'::pg_catalog.oid",
13106                                           opfinfo->dobj.catId.oid);
13107
13108         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13109
13110         i_amname = PQfnumber(res, "amname");
13111
13112         /* amname will still be needed after we PQclear res */
13113         amname = pg_strdup(PQgetvalue(res, 0, i_amname));
13114
13115         appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
13116                                           fmtQualifiedDumpable(opfinfo));
13117         appendPQExpBuffer(delq, " USING %s;\n",
13118                                           fmtId(amname));
13119
13120         /* Build the fixed portion of the CREATE command */
13121         appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
13122                                           fmtQualifiedDumpable(opfinfo));
13123         appendPQExpBuffer(q, " USING %s;\n",
13124                                           fmtId(amname));
13125
13126         PQclear(res);
13127
13128         /* Do we need an ALTER to add loose members? */
13129         if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
13130         {
13131                 appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
13132                                                   fmtQualifiedDumpable(opfinfo));
13133                 appendPQExpBuffer(q, " USING %s ADD\n    ",
13134                                                   fmtId(amname));
13135
13136                 needComma = false;
13137
13138                 /*
13139                  * Now fetch and print the OPERATOR entries (pg_amop rows).
13140                  */
13141                 ntups = PQntuples(res_ops);
13142
13143                 i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
13144                 i_amopreqcheck = PQfnumber(res_ops, "amopreqcheck");
13145                 i_amopopr = PQfnumber(res_ops, "amopopr");
13146                 i_sortfamily = PQfnumber(res_ops, "sortfamily");
13147                 i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp");
13148
13149                 for (i = 0; i < ntups; i++)
13150                 {
13151                         amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
13152                         amopreqcheck = PQgetvalue(res_ops, i, i_amopreqcheck);
13153                         amopopr = PQgetvalue(res_ops, i, i_amopopr);
13154                         sortfamily = PQgetvalue(res_ops, i, i_sortfamily);
13155                         sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp);
13156
13157                         if (needComma)
13158                                 appendPQExpBufferStr(q, " ,\n    ");
13159
13160                         appendPQExpBuffer(q, "OPERATOR %s %s",
13161                                                           amopstrategy, amopopr);
13162
13163                         if (strlen(sortfamily) > 0)
13164                         {
13165                                 appendPQExpBufferStr(q, " FOR ORDER BY ");
13166                                 appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
13167                                 appendPQExpBufferStr(q, fmtId(sortfamily));
13168                         }
13169
13170                         if (strcmp(amopreqcheck, "t") == 0)
13171                                 appendPQExpBufferStr(q, " RECHECK");
13172
13173                         needComma = true;
13174                 }
13175
13176                 /*
13177                  * Now fetch and print the FUNCTION entries (pg_amproc rows).
13178                  */
13179                 ntups = PQntuples(res_procs);
13180
13181                 i_amprocnum = PQfnumber(res_procs, "amprocnum");
13182                 i_amproc = PQfnumber(res_procs, "amproc");
13183                 i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
13184                 i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
13185
13186                 for (i = 0; i < ntups; i++)
13187                 {
13188                         amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
13189                         amproc = PQgetvalue(res_procs, i, i_amproc);
13190                         amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
13191                         amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
13192
13193                         if (needComma)
13194                                 appendPQExpBufferStr(q, " ,\n    ");
13195
13196                         appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
13197                                                           amprocnum, amproclefttype, amprocrighttype,
13198                                                           amproc);
13199
13200                         needComma = true;
13201                 }
13202
13203                 appendPQExpBufferStr(q, ";\n");
13204         }
13205
13206         appendPQExpBufferStr(nameusing, fmtId(opfinfo->dobj.name));
13207         appendPQExpBuffer(nameusing, " USING %s",
13208                                           fmtId(amname));
13209
13210         if (dopt->binary_upgrade)
13211                 binary_upgrade_extension_member(q, &opfinfo->dobj,
13212                                                                                 "OPERATOR FAMILY", nameusing->data,
13213                                                                                 opfinfo->dobj.namespace->dobj.name);
13214
13215         if (opfinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13216                 ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
13217                                          opfinfo->dobj.name,
13218                                          opfinfo->dobj.namespace->dobj.name,
13219                                          NULL,
13220                                          opfinfo->rolname,
13221                                          false, "OPERATOR FAMILY", SECTION_PRE_DATA,
13222                                          q->data, delq->data, NULL,
13223                                          NULL, 0,
13224                                          NULL, NULL);
13225
13226         /* Dump Operator Family Comments */
13227         if (opfinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13228                 dumpComment(fout, "OPERATOR FAMILY", nameusing->data,
13229                                         opfinfo->dobj.namespace->dobj.name, opfinfo->rolname,
13230                                         opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
13231
13232         free(amname);
13233         PQclear(res_ops);
13234         PQclear(res_procs);
13235         destroyPQExpBuffer(query);
13236         destroyPQExpBuffer(q);
13237         destroyPQExpBuffer(delq);
13238         destroyPQExpBuffer(nameusing);
13239 }
13240
13241 /*
13242  * dumpCollation
13243  *        write out a single collation definition
13244  */
13245 static void
13246 dumpCollation(Archive *fout, CollInfo *collinfo)
13247 {
13248         DumpOptions *dopt = fout->dopt;
13249         PQExpBuffer query;
13250         PQExpBuffer q;
13251         PQExpBuffer delq;
13252         char       *qcollname;
13253         PGresult   *res;
13254         int                     i_collprovider;
13255         int                     i_collcollate;
13256         int                     i_collctype;
13257         const char *collprovider;
13258         const char *collcollate;
13259         const char *collctype;
13260
13261         /* Skip if not to be dumped */
13262         if (!collinfo->dobj.dump || dopt->dataOnly)
13263                 return;
13264
13265         query = createPQExpBuffer();
13266         q = createPQExpBuffer();
13267         delq = createPQExpBuffer();
13268
13269         qcollname = pg_strdup(fmtId(collinfo->dobj.name));
13270
13271         /* Get collation-specific details */
13272         if (fout->remoteVersion >= 100000)
13273                 appendPQExpBuffer(query, "SELECT "
13274                                                   "collprovider, "
13275                                                   "collcollate, "
13276                                                   "collctype, "
13277                                                   "collversion "
13278                                                   "FROM pg_catalog.pg_collation c "
13279                                                   "WHERE c.oid = '%u'::pg_catalog.oid",
13280                                                   collinfo->dobj.catId.oid);
13281         else
13282                 appendPQExpBuffer(query, "SELECT "
13283                                                   "'c' AS collprovider, "
13284                                                   "collcollate, "
13285                                                   "collctype, "
13286                                                   "NULL AS collversion "
13287                                                   "FROM pg_catalog.pg_collation c "
13288                                                   "WHERE c.oid = '%u'::pg_catalog.oid",
13289                                                   collinfo->dobj.catId.oid);
13290
13291         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13292
13293         i_collprovider = PQfnumber(res, "collprovider");
13294         i_collcollate = PQfnumber(res, "collcollate");
13295         i_collctype = PQfnumber(res, "collctype");
13296
13297         collprovider = PQgetvalue(res, 0, i_collprovider);
13298         collcollate = PQgetvalue(res, 0, i_collcollate);
13299         collctype = PQgetvalue(res, 0, i_collctype);
13300
13301         appendPQExpBuffer(delq, "DROP COLLATION %s;\n",
13302                                           fmtQualifiedDumpable(collinfo));
13303
13304         appendPQExpBuffer(q, "CREATE COLLATION %s (",
13305                                           fmtQualifiedDumpable(collinfo));
13306
13307         appendPQExpBufferStr(q, "provider = ");
13308         if (collprovider[0] == 'c')
13309                 appendPQExpBufferStr(q, "libc");
13310         else if (collprovider[0] == 'i')
13311                 appendPQExpBufferStr(q, "icu");
13312         else if (collprovider[0] == 'd')
13313                 /* to allow dumping pg_catalog; not accepted on input */
13314                 appendPQExpBufferStr(q, "default");
13315         else
13316                 exit_horribly(NULL,
13317                                           "unrecognized collation provider: %s\n",
13318                                           collprovider);
13319
13320         if (strcmp(collcollate, collctype) == 0)
13321         {
13322                 appendPQExpBufferStr(q, ", locale = ");
13323                 appendStringLiteralAH(q, collcollate, fout);
13324         }
13325         else
13326         {
13327                 appendPQExpBufferStr(q, ", lc_collate = ");
13328                 appendStringLiteralAH(q, collcollate, fout);
13329                 appendPQExpBufferStr(q, ", lc_ctype = ");
13330                 appendStringLiteralAH(q, collctype, fout);
13331         }
13332
13333         /*
13334          * For binary upgrade, carry over the collation version.  For normal
13335          * dump/restore, omit the version, so that it is computed upon restore.
13336          */
13337         if (dopt->binary_upgrade)
13338         {
13339                 int                     i_collversion;
13340
13341                 i_collversion = PQfnumber(res, "collversion");
13342                 if (!PQgetisnull(res, 0, i_collversion))
13343                 {
13344                         appendPQExpBufferStr(q, ", version = ");
13345                         appendStringLiteralAH(q,
13346                                                                   PQgetvalue(res, 0, i_collversion),
13347                                                                   fout);
13348                 }
13349         }
13350
13351         appendPQExpBufferStr(q, ");\n");
13352
13353         if (dopt->binary_upgrade)
13354                 binary_upgrade_extension_member(q, &collinfo->dobj,
13355                                                                                 "COLLATION", qcollname,
13356                                                                                 collinfo->dobj.namespace->dobj.name);
13357
13358         if (collinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13359                 ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
13360                                          collinfo->dobj.name,
13361                                          collinfo->dobj.namespace->dobj.name,
13362                                          NULL,
13363                                          collinfo->rolname,
13364                                          false, "COLLATION", SECTION_PRE_DATA,
13365                                          q->data, delq->data, NULL,
13366                                          NULL, 0,
13367                                          NULL, NULL);
13368
13369         /* Dump Collation Comments */
13370         if (collinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13371                 dumpComment(fout, "COLLATION", qcollname,
13372                                         collinfo->dobj.namespace->dobj.name, collinfo->rolname,
13373                                         collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
13374
13375         PQclear(res);
13376
13377         destroyPQExpBuffer(query);
13378         destroyPQExpBuffer(q);
13379         destroyPQExpBuffer(delq);
13380         free(qcollname);
13381 }
13382
13383 /*
13384  * dumpConversion
13385  *        write out a single conversion definition
13386  */
13387 static void
13388 dumpConversion(Archive *fout, ConvInfo *convinfo)
13389 {
13390         DumpOptions *dopt = fout->dopt;
13391         PQExpBuffer query;
13392         PQExpBuffer q;
13393         PQExpBuffer delq;
13394         char       *qconvname;
13395         PGresult   *res;
13396         int                     i_conforencoding;
13397         int                     i_contoencoding;
13398         int                     i_conproc;
13399         int                     i_condefault;
13400         const char *conforencoding;
13401         const char *contoencoding;
13402         const char *conproc;
13403         bool            condefault;
13404
13405         /* Skip if not to be dumped */
13406         if (!convinfo->dobj.dump || dopt->dataOnly)
13407                 return;
13408
13409         query = createPQExpBuffer();
13410         q = createPQExpBuffer();
13411         delq = createPQExpBuffer();
13412
13413         qconvname = pg_strdup(fmtId(convinfo->dobj.name));
13414
13415         /* Get conversion-specific details */
13416         appendPQExpBuffer(query, "SELECT "
13417                                           "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
13418                                           "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
13419                                           "conproc, condefault "
13420                                           "FROM pg_catalog.pg_conversion c "
13421                                           "WHERE c.oid = '%u'::pg_catalog.oid",
13422                                           convinfo->dobj.catId.oid);
13423
13424         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13425
13426         i_conforencoding = PQfnumber(res, "conforencoding");
13427         i_contoencoding = PQfnumber(res, "contoencoding");
13428         i_conproc = PQfnumber(res, "conproc");
13429         i_condefault = PQfnumber(res, "condefault");
13430
13431         conforencoding = PQgetvalue(res, 0, i_conforencoding);
13432         contoencoding = PQgetvalue(res, 0, i_contoencoding);
13433         conproc = PQgetvalue(res, 0, i_conproc);
13434         condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
13435
13436         appendPQExpBuffer(delq, "DROP CONVERSION %s;\n",
13437                                           fmtQualifiedDumpable(convinfo));
13438
13439         appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
13440                                           (condefault) ? "DEFAULT " : "",
13441                                           fmtQualifiedDumpable(convinfo));
13442         appendStringLiteralAH(q, conforencoding, fout);
13443         appendPQExpBufferStr(q, " TO ");
13444         appendStringLiteralAH(q, contoencoding, fout);
13445         /* regproc output is already sufficiently quoted */
13446         appendPQExpBuffer(q, " FROM %s;\n", conproc);
13447
13448         if (dopt->binary_upgrade)
13449                 binary_upgrade_extension_member(q, &convinfo->dobj,
13450                                                                                 "CONVERSION", qconvname,
13451                                                                                 convinfo->dobj.namespace->dobj.name);
13452
13453         if (convinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
13454                 ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
13455                                          convinfo->dobj.name,
13456                                          convinfo->dobj.namespace->dobj.name,
13457                                          NULL,
13458                                          convinfo->rolname,
13459                                          false, "CONVERSION", SECTION_PRE_DATA,
13460                                          q->data, delq->data, NULL,
13461                                          NULL, 0,
13462                                          NULL, NULL);
13463
13464         /* Dump Conversion Comments */
13465         if (convinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
13466                 dumpComment(fout, "CONVERSION", qconvname,
13467                                         convinfo->dobj.namespace->dobj.name, convinfo->rolname,
13468                                         convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
13469
13470         PQclear(res);
13471
13472         destroyPQExpBuffer(query);
13473         destroyPQExpBuffer(q);
13474         destroyPQExpBuffer(delq);
13475         free(qconvname);
13476 }
13477
13478 /*
13479  * format_aggregate_signature: generate aggregate name and argument list
13480  *
13481  * The argument type names are qualified if needed.  The aggregate name
13482  * is never qualified.
13483  */
13484 static char *
13485 format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
13486 {
13487         PQExpBufferData buf;
13488         int                     j;
13489
13490         initPQExpBuffer(&buf);
13491         if (honor_quotes)
13492                 appendPQExpBufferStr(&buf, fmtId(agginfo->aggfn.dobj.name));
13493         else
13494                 appendPQExpBufferStr(&buf, agginfo->aggfn.dobj.name);
13495
13496         if (agginfo->aggfn.nargs == 0)
13497                 appendPQExpBuffer(&buf, "(*)");
13498         else
13499         {
13500                 appendPQExpBufferChar(&buf, '(');
13501                 for (j = 0; j < agginfo->aggfn.nargs; j++)
13502                 {
13503                         char       *typname;
13504
13505                         typname = getFormattedTypeName(fout, agginfo->aggfn.argtypes[j],
13506                                                                                    zeroAsOpaque);
13507
13508                         appendPQExpBuffer(&buf, "%s%s",
13509                                                           (j > 0) ? ", " : "",
13510                                                           typname);
13511                         free(typname);
13512                 }
13513                 appendPQExpBufferChar(&buf, ')');
13514         }
13515         return buf.data;
13516 }
13517
13518 /*
13519  * dumpAgg
13520  *        write out a single aggregate definition
13521  */
13522 static void
13523 dumpAgg(Archive *fout, AggInfo *agginfo)
13524 {
13525         DumpOptions *dopt = fout->dopt;
13526         PQExpBuffer query;
13527         PQExpBuffer q;
13528         PQExpBuffer delq;
13529         PQExpBuffer details;
13530         char       *aggsig;                     /* identity signature */
13531         char       *aggfullsig = NULL;  /* full signature */
13532         char       *aggsig_tag;
13533         PGresult   *res;
13534         int                     i_aggtransfn;
13535         int                     i_aggfinalfn;
13536         int                     i_aggcombinefn;
13537         int                     i_aggserialfn;
13538         int                     i_aggdeserialfn;
13539         int                     i_aggmtransfn;
13540         int                     i_aggminvtransfn;
13541         int                     i_aggmfinalfn;
13542         int                     i_aggfinalextra;
13543         int                     i_aggmfinalextra;
13544         int                     i_aggfinalmodify;
13545         int                     i_aggmfinalmodify;
13546         int                     i_aggsortop;
13547         int                     i_aggkind;
13548         int                     i_aggtranstype;
13549         int                     i_aggtransspace;
13550         int                     i_aggmtranstype;
13551         int                     i_aggmtransspace;
13552         int                     i_agginitval;
13553         int                     i_aggminitval;
13554         int                     i_convertok;
13555         int                     i_proparallel;
13556         const char *aggtransfn;
13557         const char *aggfinalfn;
13558         const char *aggcombinefn;
13559         const char *aggserialfn;
13560         const char *aggdeserialfn;
13561         const char *aggmtransfn;
13562         const char *aggminvtransfn;
13563         const char *aggmfinalfn;
13564         bool            aggfinalextra;
13565         bool            aggmfinalextra;
13566         char            aggfinalmodify;
13567         char            aggmfinalmodify;
13568         const char *aggsortop;
13569         char       *aggsortconvop;
13570         char            aggkind;
13571         const char *aggtranstype;
13572         const char *aggtransspace;
13573         const char *aggmtranstype;
13574         const char *aggmtransspace;
13575         const char *agginitval;
13576         const char *aggminitval;
13577         bool            convertok;
13578         const char *proparallel;
13579         char            defaultfinalmodify;
13580
13581         /* Skip if not to be dumped */
13582         if (!agginfo->aggfn.dobj.dump || dopt->dataOnly)
13583                 return;
13584
13585         query = createPQExpBuffer();
13586         q = createPQExpBuffer();
13587         delq = createPQExpBuffer();
13588         details = createPQExpBuffer();
13589
13590         /* Get aggregate-specific details */
13591         if (fout->remoteVersion >= 110000)
13592         {
13593                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13594                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13595                                                   "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13596                                                   "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13597                                                   "aggfinalextra, aggmfinalextra, "
13598                                                   "aggfinalmodify, aggmfinalmodify, "
13599                                                   "aggsortop, "
13600                                                   "aggkind, "
13601                                                   "aggtransspace, agginitval, "
13602                                                   "aggmtransspace, aggminitval, "
13603                                                   "true AS convertok, "
13604                                                   "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13605                                                   "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13606                                                   "p.proparallel "
13607                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13608                                                   "WHERE a.aggfnoid = p.oid "
13609                                                   "AND p.oid = '%u'::pg_catalog.oid",
13610                                                   agginfo->aggfn.dobj.catId.oid);
13611         }
13612         else if (fout->remoteVersion >= 90600)
13613         {
13614                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13615                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13616                                                   "aggcombinefn, aggserialfn, aggdeserialfn, aggmtransfn, "
13617                                                   "aggminvtransfn, aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13618                                                   "aggfinalextra, aggmfinalextra, "
13619                                                   "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13620                                                   "aggsortop, "
13621                                                   "aggkind, "
13622                                                   "aggtransspace, agginitval, "
13623                                                   "aggmtransspace, aggminitval, "
13624                                                   "true AS convertok, "
13625                                                   "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13626                                                   "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs, "
13627                                                   "p.proparallel "
13628                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13629                                                   "WHERE a.aggfnoid = p.oid "
13630                                                   "AND p.oid = '%u'::pg_catalog.oid",
13631                                                   agginfo->aggfn.dobj.catId.oid);
13632         }
13633         else if (fout->remoteVersion >= 90400)
13634         {
13635                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13636                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13637                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13638                                                   "'-' AS aggdeserialfn, aggmtransfn, aggminvtransfn, "
13639                                                   "aggmfinalfn, aggmtranstype::pg_catalog.regtype, "
13640                                                   "aggfinalextra, aggmfinalextra, "
13641                                                   "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13642                                                   "aggsortop, "
13643                                                   "aggkind, "
13644                                                   "aggtransspace, agginitval, "
13645                                                   "aggmtransspace, aggminitval, "
13646                                                   "true AS convertok, "
13647                                                   "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13648                                                   "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13649                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13650                                                   "WHERE a.aggfnoid = p.oid "
13651                                                   "AND p.oid = '%u'::pg_catalog.oid",
13652                                                   agginfo->aggfn.dobj.catId.oid);
13653         }
13654         else if (fout->remoteVersion >= 80400)
13655         {
13656                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13657                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13658                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13659                                                   "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13660                                                   "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13661                                                   "0 AS aggmtranstype, false AS aggfinalextra, "
13662                                                   "false AS aggmfinalextra, "
13663                                                   "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13664                                                   "aggsortop, "
13665                                                   "'n' AS aggkind, "
13666                                                   "0 AS aggtransspace, agginitval, "
13667                                                   "0 AS aggmtransspace, NULL AS aggminitval, "
13668                                                   "true AS convertok, "
13669                                                   "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
13670                                                   "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
13671                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13672                                                   "WHERE a.aggfnoid = p.oid "
13673                                                   "AND p.oid = '%u'::pg_catalog.oid",
13674                                                   agginfo->aggfn.dobj.catId.oid);
13675         }
13676         else if (fout->remoteVersion >= 80100)
13677         {
13678                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13679                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13680                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13681                                                   "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13682                                                   "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13683                                                   "0 AS aggmtranstype, false AS aggfinalextra, "
13684                                                   "false AS aggmfinalextra, "
13685                                                   "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13686                                                   "aggsortop, "
13687                                                   "'n' AS aggkind, "
13688                                                   "0 AS aggtransspace, agginitval, "
13689                                                   "0 AS aggmtransspace, NULL AS aggminitval, "
13690                                                   "true AS convertok "
13691                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13692                                                   "WHERE a.aggfnoid = p.oid "
13693                                                   "AND p.oid = '%u'::pg_catalog.oid",
13694                                                   agginfo->aggfn.dobj.catId.oid);
13695         }
13696         else
13697         {
13698                 appendPQExpBuffer(query, "SELECT aggtransfn, "
13699                                                   "aggfinalfn, aggtranstype::pg_catalog.regtype, "
13700                                                   "'-' AS aggcombinefn, '-' AS aggserialfn, "
13701                                                   "'-' AS aggdeserialfn, '-' AS aggmtransfn, "
13702                                                   "'-' AS aggminvtransfn, '-' AS aggmfinalfn, "
13703                                                   "0 AS aggmtranstype, false AS aggfinalextra, "
13704                                                   "false AS aggmfinalextra, "
13705                                                   "'0' AS aggfinalmodify, '0' AS aggmfinalmodify, "
13706                                                   "0 AS aggsortop, "
13707                                                   "'n' AS aggkind, "
13708                                                   "0 AS aggtransspace, agginitval, "
13709                                                   "0 AS aggmtransspace, NULL AS aggminitval, "
13710                                                   "true AS convertok "
13711                                                   "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
13712                                                   "WHERE a.aggfnoid = p.oid "
13713                                                   "AND p.oid = '%u'::pg_catalog.oid",
13714                                                   agginfo->aggfn.dobj.catId.oid);
13715         }
13716
13717         res = ExecuteSqlQueryForSingleRow(fout, query->data);
13718
13719         i_aggtransfn = PQfnumber(res, "aggtransfn");
13720         i_aggfinalfn = PQfnumber(res, "aggfinalfn");
13721         i_aggcombinefn = PQfnumber(res, "aggcombinefn");
13722         i_aggserialfn = PQfnumber(res, "aggserialfn");
13723         i_aggdeserialfn = PQfnumber(res, "aggdeserialfn");
13724         i_aggmtransfn = PQfnumber(res, "aggmtransfn");
13725         i_aggminvtransfn = PQfnumber(res, "aggminvtransfn");
13726         i_aggmfinalfn = PQfnumber(res, "aggmfinalfn");
13727         i_aggfinalextra = PQfnumber(res, "aggfinalextra");
13728         i_aggmfinalextra = PQfnumber(res, "aggmfinalextra");
13729         i_aggfinalmodify = PQfnumber(res, "aggfinalmodify");
13730         i_aggmfinalmodify = PQfnumber(res, "aggmfinalmodify");
13731         i_aggsortop = PQfnumber(res, "aggsortop");
13732         i_aggkind = PQfnumber(res, "aggkind");
13733         i_aggtranstype = PQfnumber(res, "aggtranstype");
13734         i_aggtransspace = PQfnumber(res, "aggtransspace");
13735         i_aggmtranstype = PQfnumber(res, "aggmtranstype");
13736         i_aggmtransspace = PQfnumber(res, "aggmtransspace");
13737         i_agginitval = PQfnumber(res, "agginitval");
13738         i_aggminitval = PQfnumber(res, "aggminitval");
13739         i_convertok = PQfnumber(res, "convertok");
13740         i_proparallel = PQfnumber(res, "proparallel");
13741
13742         aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
13743         aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
13744         aggcombinefn = PQgetvalue(res, 0, i_aggcombinefn);
13745         aggserialfn = PQgetvalue(res, 0, i_aggserialfn);
13746         aggdeserialfn = PQgetvalue(res, 0, i_aggdeserialfn);
13747         aggmtransfn = PQgetvalue(res, 0, i_aggmtransfn);
13748         aggminvtransfn = PQgetvalue(res, 0, i_aggminvtransfn);
13749         aggmfinalfn = PQgetvalue(res, 0, i_aggmfinalfn);
13750         aggfinalextra = (PQgetvalue(res, 0, i_aggfinalextra)[0] == 't');
13751         aggmfinalextra = (PQgetvalue(res, 0, i_aggmfinalextra)[0] == 't');
13752         aggfinalmodify = PQgetvalue(res, 0, i_aggfinalmodify)[0];
13753         aggmfinalmodify = PQgetvalue(res, 0, i_aggmfinalmodify)[0];
13754         aggsortop = PQgetvalue(res, 0, i_aggsortop);
13755         aggkind = PQgetvalue(res, 0, i_aggkind)[0];
13756         aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
13757         aggtransspace = PQgetvalue(res, 0, i_aggtransspace);
13758         aggmtranstype = PQgetvalue(res, 0, i_aggmtranstype);
13759         aggmtransspace = PQgetvalue(res, 0, i_aggmtransspace);
13760         agginitval = PQgetvalue(res, 0, i_agginitval);
13761         aggminitval = PQgetvalue(res, 0, i_aggminitval);
13762         convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
13763
13764         if (fout->remoteVersion >= 80400)
13765         {
13766                 /* 8.4 or later; we rely on server-side code for most of the work */
13767                 char       *funcargs;
13768                 char       *funciargs;
13769
13770                 funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
13771                 funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
13772                 aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
13773                 aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
13774         }
13775         else
13776                 /* pre-8.4, do it ourselves */
13777                 aggsig = format_aggregate_signature(agginfo, fout, true);
13778
13779         aggsig_tag = format_aggregate_signature(agginfo, fout, false);
13780
13781         if (i_proparallel != -1)
13782                 proparallel = PQgetvalue(res, 0, PQfnumber(res, "proparallel"));
13783         else
13784                 proparallel = NULL;
13785
13786         if (!convertok)
13787         {
13788                 write_msg(NULL, "WARNING: aggregate function %s could not be dumped correctly for this database version; ignored\n",
13789                                   aggsig);
13790
13791                 if (aggfullsig)
13792                         free(aggfullsig);
13793
13794                 free(aggsig);
13795
13796                 return;
13797         }
13798
13799         /* identify default modify flag for aggkind (must match DefineAggregate) */
13800         defaultfinalmodify = (aggkind == AGGKIND_NORMAL) ? AGGMODIFY_READ_ONLY : AGGMODIFY_READ_WRITE;
13801         /* replace omitted flags for old versions */
13802         if (aggfinalmodify == '0')
13803                 aggfinalmodify = defaultfinalmodify;
13804         if (aggmfinalmodify == '0')
13805                 aggmfinalmodify = defaultfinalmodify;
13806
13807         /* regproc and regtype output is already sufficiently quoted */
13808         appendPQExpBuffer(details, "    SFUNC = %s,\n    STYPE = %s",
13809                                           aggtransfn, aggtranstype);
13810
13811         if (strcmp(aggtransspace, "0") != 0)
13812         {
13813                 appendPQExpBuffer(details, ",\n    SSPACE = %s",
13814                                                   aggtransspace);
13815         }
13816
13817         if (!PQgetisnull(res, 0, i_agginitval))
13818         {
13819                 appendPQExpBufferStr(details, ",\n    INITCOND = ");
13820                 appendStringLiteralAH(details, agginitval, fout);
13821         }
13822
13823         if (strcmp(aggfinalfn, "-") != 0)
13824         {
13825                 appendPQExpBuffer(details, ",\n    FINALFUNC = %s",
13826                                                   aggfinalfn);
13827                 if (aggfinalextra)
13828                         appendPQExpBufferStr(details, ",\n    FINALFUNC_EXTRA");
13829                 if (aggfinalmodify != defaultfinalmodify)
13830                 {
13831                         switch (aggfinalmodify)
13832                         {
13833                                 case AGGMODIFY_READ_ONLY:
13834                                         appendPQExpBufferStr(details, ",\n    FINALFUNC_MODIFY = READ_ONLY");
13835                                         break;
13836                                 case AGGMODIFY_SHAREABLE:
13837                                         appendPQExpBufferStr(details, ",\n    FINALFUNC_MODIFY = SHAREABLE");
13838                                         break;
13839                                 case AGGMODIFY_READ_WRITE:
13840                                         appendPQExpBufferStr(details, ",\n    FINALFUNC_MODIFY = READ_WRITE");
13841                                         break;
13842                                 default:
13843                                         exit_horribly(NULL, "unrecognized aggfinalmodify value for aggregate \"%s\"\n",
13844                                                                   agginfo->aggfn.dobj.name);
13845                                         break;
13846                         }
13847                 }
13848         }
13849
13850         if (strcmp(aggcombinefn, "-") != 0)
13851                 appendPQExpBuffer(details, ",\n    COMBINEFUNC = %s", aggcombinefn);
13852
13853         if (strcmp(aggserialfn, "-") != 0)
13854                 appendPQExpBuffer(details, ",\n    SERIALFUNC = %s", aggserialfn);
13855
13856         if (strcmp(aggdeserialfn, "-") != 0)
13857                 appendPQExpBuffer(details, ",\n    DESERIALFUNC = %s", aggdeserialfn);
13858
13859         if (strcmp(aggmtransfn, "-") != 0)
13860         {
13861                 appendPQExpBuffer(details, ",\n    MSFUNC = %s,\n    MINVFUNC = %s,\n    MSTYPE = %s",
13862                                                   aggmtransfn,
13863                                                   aggminvtransfn,
13864                                                   aggmtranstype);
13865         }
13866
13867         if (strcmp(aggmtransspace, "0") != 0)
13868         {
13869                 appendPQExpBuffer(details, ",\n    MSSPACE = %s",
13870                                                   aggmtransspace);
13871         }
13872
13873         if (!PQgetisnull(res, 0, i_aggminitval))
13874         {
13875                 appendPQExpBufferStr(details, ",\n    MINITCOND = ");
13876                 appendStringLiteralAH(details, aggminitval, fout);
13877         }
13878
13879         if (strcmp(aggmfinalfn, "-") != 0)
13880         {
13881                 appendPQExpBuffer(details, ",\n    MFINALFUNC = %s",
13882                                                   aggmfinalfn);
13883                 if (aggmfinalextra)
13884                         appendPQExpBufferStr(details, ",\n    MFINALFUNC_EXTRA");
13885                 if (aggmfinalmodify != defaultfinalmodify)
13886                 {
13887                         switch (aggmfinalmodify)
13888                         {
13889                                 case AGGMODIFY_READ_ONLY:
13890                                         appendPQExpBufferStr(details, ",\n    MFINALFUNC_MODIFY = READ_ONLY");
13891                                         break;
13892                                 case AGGMODIFY_SHAREABLE:
13893                                         appendPQExpBufferStr(details, ",\n    MFINALFUNC_MODIFY = SHAREABLE");
13894                                         break;
13895                                 case AGGMODIFY_READ_WRITE:
13896                                         appendPQExpBufferStr(details, ",\n    MFINALFUNC_MODIFY = READ_WRITE");
13897                                         break;
13898                                 default:
13899                                         exit_horribly(NULL, "unrecognized aggmfinalmodify value for aggregate \"%s\"\n",
13900                                                                   agginfo->aggfn.dobj.name);
13901                                         break;
13902                         }
13903                 }
13904         }
13905
13906         aggsortconvop = getFormattedOperatorName(fout, aggsortop);
13907         if (aggsortconvop)
13908         {
13909                 appendPQExpBuffer(details, ",\n    SORTOP = %s",
13910                                                   aggsortconvop);
13911                 free(aggsortconvop);
13912         }
13913
13914         if (aggkind == AGGKIND_HYPOTHETICAL)
13915                 appendPQExpBufferStr(details, ",\n    HYPOTHETICAL");
13916
13917         if (proparallel != NULL && proparallel[0] != PROPARALLEL_UNSAFE)
13918         {
13919                 if (proparallel[0] == PROPARALLEL_SAFE)
13920                         appendPQExpBufferStr(details, ",\n    PARALLEL = safe");
13921                 else if (proparallel[0] == PROPARALLEL_RESTRICTED)
13922                         appendPQExpBufferStr(details, ",\n    PARALLEL = restricted");
13923                 else if (proparallel[0] != PROPARALLEL_UNSAFE)
13924                         exit_horribly(NULL, "unrecognized proparallel value for function \"%s\"\n",
13925                                                   agginfo->aggfn.dobj.name);
13926         }
13927
13928         appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
13929                                           fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13930                                           aggsig);
13931
13932         appendPQExpBuffer(q, "CREATE AGGREGATE %s.%s (\n%s\n);\n",
13933                                           fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
13934                                           aggfullsig ? aggfullsig : aggsig, details->data);
13935
13936         if (dopt->binary_upgrade)
13937                 binary_upgrade_extension_member(q, &agginfo->aggfn.dobj,
13938                                                                                 "AGGREGATE", aggsig,
13939                                                                                 agginfo->aggfn.dobj.namespace->dobj.name);
13940
13941         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_DEFINITION)
13942                 ArchiveEntry(fout, agginfo->aggfn.dobj.catId,
13943                                          agginfo->aggfn.dobj.dumpId,
13944                                          aggsig_tag,
13945                                          agginfo->aggfn.dobj.namespace->dobj.name,
13946                                          NULL,
13947                                          agginfo->aggfn.rolname,
13948                                          false, "AGGREGATE", SECTION_PRE_DATA,
13949                                          q->data, delq->data, NULL,
13950                                          NULL, 0,
13951                                          NULL, NULL);
13952
13953         /* Dump Aggregate Comments */
13954         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_COMMENT)
13955                 dumpComment(fout, "AGGREGATE", aggsig,
13956                                         agginfo->aggfn.dobj.namespace->dobj.name,
13957                                         agginfo->aggfn.rolname,
13958                                         agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13959
13960         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_SECLABEL)
13961                 dumpSecLabel(fout, "AGGREGATE", aggsig,
13962                                          agginfo->aggfn.dobj.namespace->dobj.name,
13963                                          agginfo->aggfn.rolname,
13964                                          agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
13965
13966         /*
13967          * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
13968          * command look like a function's GRANT; in particular this affects the
13969          * syntax for zero-argument aggregates and ordered-set aggregates.
13970          */
13971         free(aggsig);
13972
13973         aggsig = format_function_signature(fout, &agginfo->aggfn, true);
13974
13975         if (agginfo->aggfn.dobj.dump & DUMP_COMPONENT_ACL)
13976                 dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
13977                                 "FUNCTION", aggsig, NULL,
13978                                 agginfo->aggfn.dobj.namespace->dobj.name,
13979                                 agginfo->aggfn.rolname, agginfo->aggfn.proacl,
13980                                 agginfo->aggfn.rproacl,
13981                                 agginfo->aggfn.initproacl, agginfo->aggfn.initrproacl);
13982
13983         free(aggsig);
13984         if (aggfullsig)
13985                 free(aggfullsig);
13986         free(aggsig_tag);
13987
13988         PQclear(res);
13989
13990         destroyPQExpBuffer(query);
13991         destroyPQExpBuffer(q);
13992         destroyPQExpBuffer(delq);
13993         destroyPQExpBuffer(details);
13994 }
13995
13996 /*
13997  * dumpTSParser
13998  *        write out a single text search parser
13999  */
14000 static void
14001 dumpTSParser(Archive *fout, TSParserInfo *prsinfo)
14002 {
14003         DumpOptions *dopt = fout->dopt;
14004         PQExpBuffer q;
14005         PQExpBuffer delq;
14006         char       *qprsname;
14007
14008         /* Skip if not to be dumped */
14009         if (!prsinfo->dobj.dump || dopt->dataOnly)
14010                 return;
14011
14012         q = createPQExpBuffer();
14013         delq = createPQExpBuffer();
14014
14015         qprsname = pg_strdup(fmtId(prsinfo->dobj.name));
14016
14017         appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n",
14018                                           fmtQualifiedDumpable(prsinfo));
14019
14020         appendPQExpBuffer(q, "    START = %s,\n",
14021                                           convertTSFunction(fout, prsinfo->prsstart));
14022         appendPQExpBuffer(q, "    GETTOKEN = %s,\n",
14023                                           convertTSFunction(fout, prsinfo->prstoken));
14024         appendPQExpBuffer(q, "    END = %s,\n",
14025                                           convertTSFunction(fout, prsinfo->prsend));
14026         if (prsinfo->prsheadline != InvalidOid)
14027                 appendPQExpBuffer(q, "    HEADLINE = %s,\n",
14028                                                   convertTSFunction(fout, prsinfo->prsheadline));
14029         appendPQExpBuffer(q, "    LEXTYPES = %s );\n",
14030                                           convertTSFunction(fout, prsinfo->prslextype));
14031
14032         appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s;\n",
14033                                           fmtQualifiedDumpable(prsinfo));
14034
14035         if (dopt->binary_upgrade)
14036                 binary_upgrade_extension_member(q, &prsinfo->dobj,
14037                                                                                 "TEXT SEARCH PARSER", qprsname,
14038                                                                                 prsinfo->dobj.namespace->dobj.name);
14039
14040         if (prsinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14041                 ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId,
14042                                          prsinfo->dobj.name,
14043                                          prsinfo->dobj.namespace->dobj.name,
14044                                          NULL,
14045                                          "",
14046                                          false, "TEXT SEARCH PARSER", SECTION_PRE_DATA,
14047                                          q->data, delq->data, NULL,
14048                                          NULL, 0,
14049                                          NULL, NULL);
14050
14051         /* Dump Parser Comments */
14052         if (prsinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14053                 dumpComment(fout, "TEXT SEARCH PARSER", qprsname,
14054                                         prsinfo->dobj.namespace->dobj.name, "",
14055                                         prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId);
14056
14057         destroyPQExpBuffer(q);
14058         destroyPQExpBuffer(delq);
14059         free(qprsname);
14060 }
14061
14062 /*
14063  * dumpTSDictionary
14064  *        write out a single text search dictionary
14065  */
14066 static void
14067 dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo)
14068 {
14069         DumpOptions *dopt = fout->dopt;
14070         PQExpBuffer q;
14071         PQExpBuffer delq;
14072         PQExpBuffer query;
14073         char       *qdictname;
14074         PGresult   *res;
14075         char       *nspname;
14076         char       *tmplname;
14077
14078         /* Skip if not to be dumped */
14079         if (!dictinfo->dobj.dump || dopt->dataOnly)
14080                 return;
14081
14082         q = createPQExpBuffer();
14083         delq = createPQExpBuffer();
14084         query = createPQExpBuffer();
14085
14086         qdictname = pg_strdup(fmtId(dictinfo->dobj.name));
14087
14088         /* Fetch name and namespace of the dictionary's template */
14089         appendPQExpBuffer(query, "SELECT nspname, tmplname "
14090                                           "FROM pg_ts_template p, pg_namespace n "
14091                                           "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
14092                                           dictinfo->dicttemplate);
14093         res = ExecuteSqlQueryForSingleRow(fout, query->data);
14094         nspname = PQgetvalue(res, 0, 0);
14095         tmplname = PQgetvalue(res, 0, 1);
14096
14097         appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n",
14098                                           fmtQualifiedDumpable(dictinfo));
14099
14100         appendPQExpBufferStr(q, "    TEMPLATE = ");
14101         appendPQExpBuffer(q, "%s.", fmtId(nspname));
14102         appendPQExpBufferStr(q, fmtId(tmplname));
14103
14104         PQclear(res);
14105
14106         /* the dictinitoption can be dumped straight into the command */
14107         if (dictinfo->dictinitoption)
14108                 appendPQExpBuffer(q, ",\n    %s", dictinfo->dictinitoption);
14109
14110         appendPQExpBufferStr(q, " );\n");
14111
14112         appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s;\n",
14113                                           fmtQualifiedDumpable(dictinfo));
14114
14115         if (dopt->binary_upgrade)
14116                 binary_upgrade_extension_member(q, &dictinfo->dobj,
14117                                                                                 "TEXT SEARCH DICTIONARY", qdictname,
14118                                                                                 dictinfo->dobj.namespace->dobj.name);
14119
14120         if (dictinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14121                 ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId,
14122                                          dictinfo->dobj.name,
14123                                          dictinfo->dobj.namespace->dobj.name,
14124                                          NULL,
14125                                          dictinfo->rolname,
14126                                          false, "TEXT SEARCH DICTIONARY", SECTION_PRE_DATA,
14127                                          q->data, delq->data, NULL,
14128                                          NULL, 0,
14129                                          NULL, NULL);
14130
14131         /* Dump Dictionary Comments */
14132         if (dictinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14133                 dumpComment(fout, "TEXT SEARCH DICTIONARY", qdictname,
14134                                         dictinfo->dobj.namespace->dobj.name, dictinfo->rolname,
14135                                         dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId);
14136
14137         destroyPQExpBuffer(q);
14138         destroyPQExpBuffer(delq);
14139         destroyPQExpBuffer(query);
14140         free(qdictname);
14141 }
14142
14143 /*
14144  * dumpTSTemplate
14145  *        write out a single text search template
14146  */
14147 static void
14148 dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo)
14149 {
14150         DumpOptions *dopt = fout->dopt;
14151         PQExpBuffer q;
14152         PQExpBuffer delq;
14153         char       *qtmplname;
14154
14155         /* Skip if not to be dumped */
14156         if (!tmplinfo->dobj.dump || dopt->dataOnly)
14157                 return;
14158
14159         q = createPQExpBuffer();
14160         delq = createPQExpBuffer();
14161
14162         qtmplname = pg_strdup(fmtId(tmplinfo->dobj.name));
14163
14164         appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n",
14165                                           fmtQualifiedDumpable(tmplinfo));
14166
14167         if (tmplinfo->tmplinit != InvalidOid)
14168                 appendPQExpBuffer(q, "    INIT = %s,\n",
14169                                                   convertTSFunction(fout, tmplinfo->tmplinit));
14170         appendPQExpBuffer(q, "    LEXIZE = %s );\n",
14171                                           convertTSFunction(fout, tmplinfo->tmpllexize));
14172
14173         appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s;\n",
14174                                           fmtQualifiedDumpable(tmplinfo));
14175
14176         if (dopt->binary_upgrade)
14177                 binary_upgrade_extension_member(q, &tmplinfo->dobj,
14178                                                                                 "TEXT SEARCH TEMPLATE", qtmplname,
14179                                                                                 tmplinfo->dobj.namespace->dobj.name);
14180
14181         if (tmplinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14182                 ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId,
14183                                          tmplinfo->dobj.name,
14184                                          tmplinfo->dobj.namespace->dobj.name,
14185                                          NULL,
14186                                          "",
14187                                          false, "TEXT SEARCH TEMPLATE", SECTION_PRE_DATA,
14188                                          q->data, delq->data, NULL,
14189                                          NULL, 0,
14190                                          NULL, NULL);
14191
14192         /* Dump Template Comments */
14193         if (tmplinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14194                 dumpComment(fout, "TEXT SEARCH TEMPLATE", qtmplname,
14195                                         tmplinfo->dobj.namespace->dobj.name, "",
14196                                         tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId);
14197
14198         destroyPQExpBuffer(q);
14199         destroyPQExpBuffer(delq);
14200         free(qtmplname);
14201 }
14202
14203 /*
14204  * dumpTSConfig
14205  *        write out a single text search configuration
14206  */
14207 static void
14208 dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo)
14209 {
14210         DumpOptions *dopt = fout->dopt;
14211         PQExpBuffer q;
14212         PQExpBuffer delq;
14213         PQExpBuffer query;
14214         char       *qcfgname;
14215         PGresult   *res;
14216         char       *nspname;
14217         char       *prsname;
14218         int                     ntups,
14219                                 i;
14220         int                     i_tokenname;
14221         int                     i_dictname;
14222
14223         /* Skip if not to be dumped */
14224         if (!cfginfo->dobj.dump || dopt->dataOnly)
14225                 return;
14226
14227         q = createPQExpBuffer();
14228         delq = createPQExpBuffer();
14229         query = createPQExpBuffer();
14230
14231         qcfgname = pg_strdup(fmtId(cfginfo->dobj.name));
14232
14233         /* Fetch name and namespace of the config's parser */
14234         appendPQExpBuffer(query, "SELECT nspname, prsname "
14235                                           "FROM pg_ts_parser p, pg_namespace n "
14236                                           "WHERE p.oid = '%u' AND n.oid = prsnamespace",
14237                                           cfginfo->cfgparser);
14238         res = ExecuteSqlQueryForSingleRow(fout, query->data);
14239         nspname = PQgetvalue(res, 0, 0);
14240         prsname = PQgetvalue(res, 0, 1);
14241
14242         appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n",
14243                                           fmtQualifiedDumpable(cfginfo));
14244
14245         appendPQExpBuffer(q, "    PARSER = %s.", fmtId(nspname));
14246         appendPQExpBuffer(q, "%s );\n", fmtId(prsname));
14247
14248         PQclear(res);
14249
14250         resetPQExpBuffer(query);
14251         appendPQExpBuffer(query,
14252                                           "SELECT\n"
14253                                           "  ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t\n"
14254                                           "    WHERE t.tokid = m.maptokentype ) AS tokenname,\n"
14255                                           "  m.mapdict::pg_catalog.regdictionary AS dictname\n"
14256                                           "FROM pg_catalog.pg_ts_config_map AS m\n"
14257                                           "WHERE m.mapcfg = '%u'\n"
14258                                           "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
14259                                           cfginfo->cfgparser, cfginfo->dobj.catId.oid);
14260
14261         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14262         ntups = PQntuples(res);
14263
14264         i_tokenname = PQfnumber(res, "tokenname");
14265         i_dictname = PQfnumber(res, "dictname");
14266
14267         for (i = 0; i < ntups; i++)
14268         {
14269                 char       *tokenname = PQgetvalue(res, i, i_tokenname);
14270                 char       *dictname = PQgetvalue(res, i, i_dictname);
14271
14272                 if (i == 0 ||
14273                         strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0)
14274                 {
14275                         /* starting a new token type, so start a new command */
14276                         if (i > 0)
14277                                 appendPQExpBufferStr(q, ";\n");
14278                         appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n",
14279                                                           fmtQualifiedDumpable(cfginfo));
14280                         /* tokenname needs quoting, dictname does NOT */
14281                         appendPQExpBuffer(q, "    ADD MAPPING FOR %s WITH %s",
14282                                                           fmtId(tokenname), dictname);
14283                 }
14284                 else
14285                         appendPQExpBuffer(q, ", %s", dictname);
14286         }
14287
14288         if (ntups > 0)
14289                 appendPQExpBufferStr(q, ";\n");
14290
14291         PQclear(res);
14292
14293         appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s;\n",
14294                                           fmtQualifiedDumpable(cfginfo));
14295
14296         if (dopt->binary_upgrade)
14297                 binary_upgrade_extension_member(q, &cfginfo->dobj,
14298                                                                                 "TEXT SEARCH CONFIGURATION", qcfgname,
14299                                                                                 cfginfo->dobj.namespace->dobj.name);
14300
14301         if (cfginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14302                 ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId,
14303                                          cfginfo->dobj.name,
14304                                          cfginfo->dobj.namespace->dobj.name,
14305                                          NULL,
14306                                          cfginfo->rolname,
14307                                          false, "TEXT SEARCH CONFIGURATION", SECTION_PRE_DATA,
14308                                          q->data, delq->data, NULL,
14309                                          NULL, 0,
14310                                          NULL, NULL);
14311
14312         /* Dump Configuration Comments */
14313         if (cfginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14314                 dumpComment(fout, "TEXT SEARCH CONFIGURATION", qcfgname,
14315                                         cfginfo->dobj.namespace->dobj.name, cfginfo->rolname,
14316                                         cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId);
14317
14318         destroyPQExpBuffer(q);
14319         destroyPQExpBuffer(delq);
14320         destroyPQExpBuffer(query);
14321         free(qcfgname);
14322 }
14323
14324 /*
14325  * dumpForeignDataWrapper
14326  *        write out a single foreign-data wrapper definition
14327  */
14328 static void
14329 dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo)
14330 {
14331         DumpOptions *dopt = fout->dopt;
14332         PQExpBuffer q;
14333         PQExpBuffer delq;
14334         char       *qfdwname;
14335
14336         /* Skip if not to be dumped */
14337         if (!fdwinfo->dobj.dump || dopt->dataOnly)
14338                 return;
14339
14340         q = createPQExpBuffer();
14341         delq = createPQExpBuffer();
14342
14343         qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name));
14344
14345         appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s",
14346                                           qfdwname);
14347
14348         if (strcmp(fdwinfo->fdwhandler, "-") != 0)
14349                 appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler);
14350
14351         if (strcmp(fdwinfo->fdwvalidator, "-") != 0)
14352                 appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator);
14353
14354         if (strlen(fdwinfo->fdwoptions) > 0)
14355                 appendPQExpBuffer(q, " OPTIONS (\n    %s\n)", fdwinfo->fdwoptions);
14356
14357         appendPQExpBufferStr(q, ";\n");
14358
14359         appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n",
14360                                           qfdwname);
14361
14362         if (dopt->binary_upgrade)
14363                 binary_upgrade_extension_member(q, &fdwinfo->dobj,
14364                                                                                 "FOREIGN DATA WRAPPER", qfdwname,
14365                                                                                 NULL);
14366
14367         if (fdwinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14368                 ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
14369                                          fdwinfo->dobj.name,
14370                                          NULL,
14371                                          NULL,
14372                                          fdwinfo->rolname,
14373                                          false, "FOREIGN DATA WRAPPER", SECTION_PRE_DATA,
14374                                          q->data, delq->data, NULL,
14375                                          NULL, 0,
14376                                          NULL, NULL);
14377
14378         /* Dump Foreign Data Wrapper Comments */
14379         if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14380                 dumpComment(fout, "FOREIGN DATA WRAPPER", qfdwname,
14381                                         NULL, fdwinfo->rolname,
14382                                         fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
14383
14384         /* Handle the ACL */
14385         if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL)
14386                 dumpACL(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
14387                                 "FOREIGN DATA WRAPPER", qfdwname, NULL,
14388                                 NULL, fdwinfo->rolname,
14389                                 fdwinfo->fdwacl, fdwinfo->rfdwacl,
14390                                 fdwinfo->initfdwacl, fdwinfo->initrfdwacl);
14391
14392         free(qfdwname);
14393
14394         destroyPQExpBuffer(q);
14395         destroyPQExpBuffer(delq);
14396 }
14397
14398 /*
14399  * dumpForeignServer
14400  *        write out a foreign server definition
14401  */
14402 static void
14403 dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo)
14404 {
14405         DumpOptions *dopt = fout->dopt;
14406         PQExpBuffer q;
14407         PQExpBuffer delq;
14408         PQExpBuffer query;
14409         PGresult   *res;
14410         char       *qsrvname;
14411         char       *fdwname;
14412
14413         /* Skip if not to be dumped */
14414         if (!srvinfo->dobj.dump || dopt->dataOnly)
14415                 return;
14416
14417         q = createPQExpBuffer();
14418         delq = createPQExpBuffer();
14419         query = createPQExpBuffer();
14420
14421         qsrvname = pg_strdup(fmtId(srvinfo->dobj.name));
14422
14423         /* look up the foreign-data wrapper */
14424         appendPQExpBuffer(query, "SELECT fdwname "
14425                                           "FROM pg_foreign_data_wrapper w "
14426                                           "WHERE w.oid = '%u'",
14427                                           srvinfo->srvfdw);
14428         res = ExecuteSqlQueryForSingleRow(fout, query->data);
14429         fdwname = PQgetvalue(res, 0, 0);
14430
14431         appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
14432         if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0)
14433         {
14434                 appendPQExpBufferStr(q, " TYPE ");
14435                 appendStringLiteralAH(q, srvinfo->srvtype, fout);
14436         }
14437         if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0)
14438         {
14439                 appendPQExpBufferStr(q, " VERSION ");
14440                 appendStringLiteralAH(q, srvinfo->srvversion, fout);
14441         }
14442
14443         appendPQExpBufferStr(q, " FOREIGN DATA WRAPPER ");
14444         appendPQExpBufferStr(q, fmtId(fdwname));
14445
14446         if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0)
14447                 appendPQExpBuffer(q, " OPTIONS (\n    %s\n)", srvinfo->srvoptions);
14448
14449         appendPQExpBufferStr(q, ";\n");
14450
14451         appendPQExpBuffer(delq, "DROP SERVER %s;\n",
14452                                           qsrvname);
14453
14454         if (dopt->binary_upgrade)
14455                 binary_upgrade_extension_member(q, &srvinfo->dobj,
14456                                                                                 "SERVER", qsrvname, NULL);
14457
14458         if (srvinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
14459                 ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
14460                                          srvinfo->dobj.name,
14461                                          NULL,
14462                                          NULL,
14463                                          srvinfo->rolname,
14464                                          false, "SERVER", SECTION_PRE_DATA,
14465                                          q->data, delq->data, NULL,
14466                                          NULL, 0,
14467                                          NULL, NULL);
14468
14469         /* Dump Foreign Server Comments */
14470         if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
14471                 dumpComment(fout, "SERVER", qsrvname,
14472                                         NULL, srvinfo->rolname,
14473                                         srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
14474
14475         /* Handle the ACL */
14476         if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL)
14477                 dumpACL(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
14478                                 "FOREIGN SERVER", qsrvname, NULL,
14479                                 NULL, srvinfo->rolname,
14480                                 srvinfo->srvacl, srvinfo->rsrvacl,
14481                                 srvinfo->initsrvacl, srvinfo->initrsrvacl);
14482
14483         /* Dump user mappings */
14484         if (srvinfo->dobj.dump & DUMP_COMPONENT_USERMAP)
14485                 dumpUserMappings(fout,
14486                                                  srvinfo->dobj.name, NULL,
14487                                                  srvinfo->rolname,
14488                                                  srvinfo->dobj.catId, srvinfo->dobj.dumpId);
14489
14490         free(qsrvname);
14491
14492         destroyPQExpBuffer(q);
14493         destroyPQExpBuffer(delq);
14494         destroyPQExpBuffer(query);
14495 }
14496
14497 /*
14498  * dumpUserMappings
14499  *
14500  * This routine is used to dump any user mappings associated with the
14501  * server handed to this routine. Should be called after ArchiveEntry()
14502  * for the server.
14503  */
14504 static void
14505 dumpUserMappings(Archive *fout,
14506                                  const char *servername, const char *namespace,
14507                                  const char *owner,
14508                                  CatalogId catalogId, DumpId dumpId)
14509 {
14510         PQExpBuffer q;
14511         PQExpBuffer delq;
14512         PQExpBuffer query;
14513         PQExpBuffer tag;
14514         PGresult   *res;
14515         int                     ntups;
14516         int                     i_usename;
14517         int                     i_umoptions;
14518         int                     i;
14519
14520         q = createPQExpBuffer();
14521         tag = createPQExpBuffer();
14522         delq = createPQExpBuffer();
14523         query = createPQExpBuffer();
14524
14525         /*
14526          * We read from the publicly accessible view pg_user_mappings, so as not
14527          * to fail if run by a non-superuser.  Note that the view will show
14528          * umoptions as null if the user hasn't got privileges for the associated
14529          * server; this means that pg_dump will dump such a mapping, but with no
14530          * OPTIONS clause.  A possible alternative is to skip such mappings
14531          * altogether, but it's not clear that that's an improvement.
14532          */
14533         appendPQExpBuffer(query,
14534                                           "SELECT usename, "
14535                                           "array_to_string(ARRAY("
14536                                           "SELECT quote_ident(option_name) || ' ' || "
14537                                           "quote_literal(option_value) "
14538                                           "FROM pg_options_to_table(umoptions) "
14539                                           "ORDER BY option_name"
14540                                           "), E',\n    ') AS umoptions "
14541                                           "FROM pg_user_mappings "
14542                                           "WHERE srvid = '%u' "
14543                                           "ORDER BY usename",
14544                                           catalogId.oid);
14545
14546         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14547
14548         ntups = PQntuples(res);
14549         i_usename = PQfnumber(res, "usename");
14550         i_umoptions = PQfnumber(res, "umoptions");
14551
14552         for (i = 0; i < ntups; i++)
14553         {
14554                 char       *usename;
14555                 char       *umoptions;
14556
14557                 usename = PQgetvalue(res, i, i_usename);
14558                 umoptions = PQgetvalue(res, i, i_umoptions);
14559
14560                 resetPQExpBuffer(q);
14561                 appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
14562                 appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
14563
14564                 if (umoptions && strlen(umoptions) > 0)
14565                         appendPQExpBuffer(q, " OPTIONS (\n    %s\n)", umoptions);
14566
14567                 appendPQExpBufferStr(q, ";\n");
14568
14569                 resetPQExpBuffer(delq);
14570                 appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
14571                 appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
14572
14573                 resetPQExpBuffer(tag);
14574                 appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
14575                                                   usename, servername);
14576
14577                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14578                                          tag->data,
14579                                          namespace,
14580                                          NULL,
14581                                          owner, false,
14582                                          "USER MAPPING", SECTION_PRE_DATA,
14583                                          q->data, delq->data, NULL,
14584                                          &dumpId, 1,
14585                                          NULL, NULL);
14586         }
14587
14588         PQclear(res);
14589
14590         destroyPQExpBuffer(query);
14591         destroyPQExpBuffer(delq);
14592         destroyPQExpBuffer(tag);
14593         destroyPQExpBuffer(q);
14594 }
14595
14596 /*
14597  * Write out default privileges information
14598  */
14599 static void
14600 dumpDefaultACL(Archive *fout, DefaultACLInfo *daclinfo)
14601 {
14602         DumpOptions *dopt = fout->dopt;
14603         PQExpBuffer q;
14604         PQExpBuffer tag;
14605         const char *type;
14606
14607         /* Skip if not to be dumped */
14608         if (!daclinfo->dobj.dump || dopt->dataOnly || dopt->aclsSkip)
14609                 return;
14610
14611         q = createPQExpBuffer();
14612         tag = createPQExpBuffer();
14613
14614         switch (daclinfo->defaclobjtype)
14615         {
14616                 case DEFACLOBJ_RELATION:
14617                         type = "TABLES";
14618                         break;
14619                 case DEFACLOBJ_SEQUENCE:
14620                         type = "SEQUENCES";
14621                         break;
14622                 case DEFACLOBJ_FUNCTION:
14623                         type = "FUNCTIONS";
14624                         break;
14625                 case DEFACLOBJ_TYPE:
14626                         type = "TYPES";
14627                         break;
14628                 case DEFACLOBJ_NAMESPACE:
14629                         type = "SCHEMAS";
14630                         break;
14631                 default:
14632                         /* shouldn't get here */
14633                         exit_horribly(NULL,
14634                                                   "unrecognized object type in default privileges: %d\n",
14635                                                   (int) daclinfo->defaclobjtype);
14636                         type = "";                      /* keep compiler quiet */
14637         }
14638
14639         appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
14640
14641         /* build the actual command(s) for this tuple */
14642         if (!buildDefaultACLCommands(type,
14643                                                                  daclinfo->dobj.namespace != NULL ?
14644                                                                  daclinfo->dobj.namespace->dobj.name : NULL,
14645                                                                  daclinfo->defaclacl,
14646                                                                  daclinfo->rdefaclacl,
14647                                                                  daclinfo->initdefaclacl,
14648                                                                  daclinfo->initrdefaclacl,
14649                                                                  daclinfo->defaclrole,
14650                                                                  fout->remoteVersion,
14651                                                                  q))
14652                 exit_horribly(NULL, "could not parse default ACL list (%s)\n",
14653                                           daclinfo->defaclacl);
14654
14655         if (daclinfo->dobj.dump & DUMP_COMPONENT_ACL)
14656                 ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
14657                                          tag->data,
14658                                          daclinfo->dobj.namespace ? daclinfo->dobj.namespace->dobj.name : NULL,
14659                                          NULL,
14660                                          daclinfo->defaclrole,
14661                                          false, "DEFAULT ACL", SECTION_POST_DATA,
14662                                          q->data, "", NULL,
14663                                          NULL, 0,
14664                                          NULL, NULL);
14665
14666         destroyPQExpBuffer(tag);
14667         destroyPQExpBuffer(q);
14668 }
14669
14670 /*----------
14671  * Write out grant/revoke information
14672  *
14673  * 'objCatId' is the catalog ID of the underlying object.
14674  * 'objDumpId' is the dump ID of the underlying object.
14675  * 'type' must be one of
14676  *              TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
14677  *              FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT.
14678  * 'name' is the formatted name of the object.  Must be quoted etc. already.
14679  * 'subname' is the formatted name of the sub-object, if any.  Must be quoted.
14680  *              (Currently we assume that subname is only provided for table columns.)
14681  * 'nspname' is the namespace the object is in (NULL if none).
14682  * 'owner' is the owner, NULL if there is no owner (for languages).
14683  * 'acls' contains the ACL string of the object from the appropriate system
14684  *              catalog field; it will be passed to buildACLCommands for building the
14685  *              appropriate GRANT commands.
14686  * 'racls' contains the ACL string of any initial-but-now-revoked ACLs of the
14687  *              object; it will be passed to buildACLCommands for building the
14688  *              appropriate REVOKE commands.
14689  * 'initacls' In binary-upgrade mode, ACL string of the object's initial
14690  *              privileges, to be recorded into pg_init_privs
14691  * 'initracls' In binary-upgrade mode, ACL string of the object's
14692  *              revoked-from-default privileges, to be recorded into pg_init_privs
14693  *
14694  * NB: initacls/initracls are needed because extensions can set privileges on
14695  * an object during the extension's script file and we record those into
14696  * pg_init_privs as that object's initial privileges.
14697  *----------
14698  */
14699 static void
14700 dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
14701                 const char *type, const char *name, const char *subname,
14702                 const char *nspname, const char *owner,
14703                 const char *acls, const char *racls,
14704                 const char *initacls, const char *initracls)
14705 {
14706         DumpOptions *dopt = fout->dopt;
14707         PQExpBuffer sql;
14708
14709         /* Do nothing if ACL dump is not enabled */
14710         if (dopt->aclsSkip)
14711                 return;
14712
14713         /* --data-only skips ACLs *except* BLOB ACLs */
14714         if (dopt->dataOnly && strcmp(type, "LARGE OBJECT") != 0)
14715                 return;
14716
14717         sql = createPQExpBuffer();
14718
14719         /*
14720          * Check to see if this object has had any initial ACLs included for it.
14721          * If so, we are in binary upgrade mode and these are the ACLs to turn
14722          * into GRANT and REVOKE statements to set and record the initial
14723          * privileges for an extension object.  Let the backend know that these
14724          * are to be recorded by calling binary_upgrade_set_record_init_privs()
14725          * before and after.
14726          */
14727         if (strlen(initacls) != 0 || strlen(initracls) != 0)
14728         {
14729                 appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(true);\n");
14730                 if (!buildACLCommands(name, subname, nspname, type,
14731                                                           initacls, initracls, owner,
14732                                                           "", fout->remoteVersion, sql))
14733                         exit_horribly(NULL,
14734                                                   "could not parse initial GRANT ACL list (%s) or initial REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14735                                                   initacls, initracls, name, type);
14736                 appendPQExpBuffer(sql, "SELECT pg_catalog.binary_upgrade_set_record_init_privs(false);\n");
14737         }
14738
14739         if (!buildACLCommands(name, subname, nspname, type,
14740                                                   acls, racls, owner,
14741                                                   "", fout->remoteVersion, sql))
14742                 exit_horribly(NULL,
14743                                           "could not parse GRANT ACL list (%s) or REVOKE ACL list (%s) for object \"%s\" (%s)\n",
14744                                           acls, racls, name, type);
14745
14746         if (sql->len > 0)
14747         {
14748                 PQExpBuffer tag = createPQExpBuffer();
14749
14750                 if (subname)
14751                         appendPQExpBuffer(tag, "COLUMN %s.%s", name, subname);
14752                 else
14753                         appendPQExpBuffer(tag, "%s %s", type, name);
14754
14755                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14756                                          tag->data, nspname,
14757                                          NULL,
14758                                          owner ? owner : "",
14759                                          false, "ACL", SECTION_NONE,
14760                                          sql->data, "", NULL,
14761                                          &(objDumpId), 1,
14762                                          NULL, NULL);
14763                 destroyPQExpBuffer(tag);
14764         }
14765
14766         destroyPQExpBuffer(sql);
14767 }
14768
14769 /*
14770  * dumpSecLabel
14771  *
14772  * This routine is used to dump any security labels associated with the
14773  * object handed to this routine. The routine takes the object type
14774  * and object name (ready to print, except for schema decoration), plus
14775  * the namespace and owner of the object (for labeling the ArchiveEntry),
14776  * plus catalog ID and subid which are the lookup key for pg_seclabel,
14777  * plus the dump ID for the object (for setting a dependency).
14778  * If a matching pg_seclabel entry is found, it is dumped.
14779  *
14780  * Note: although this routine takes a dumpId for dependency purposes,
14781  * that purpose is just to mark the dependency in the emitted dump file
14782  * for possible future use by pg_restore.  We do NOT use it for determining
14783  * ordering of the label in the dump file, because this routine is called
14784  * after dependency sorting occurs.  This routine should be called just after
14785  * calling ArchiveEntry() for the specified object.
14786  */
14787 static void
14788 dumpSecLabel(Archive *fout, const char *type, const char *name,
14789                          const char *namespace, const char *owner,
14790                          CatalogId catalogId, int subid, DumpId dumpId)
14791 {
14792         DumpOptions *dopt = fout->dopt;
14793         SecLabelItem *labels;
14794         int                     nlabels;
14795         int                     i;
14796         PQExpBuffer query;
14797
14798         /* do nothing, if --no-security-labels is supplied */
14799         if (dopt->no_security_labels)
14800                 return;
14801
14802         /* Security labels are schema not data ... except blob labels are data */
14803         if (strcmp(type, "LARGE OBJECT") != 0)
14804         {
14805                 if (dopt->dataOnly)
14806                         return;
14807         }
14808         else
14809         {
14810                 /* We do dump blob security labels in binary-upgrade mode */
14811                 if (dopt->schemaOnly && !dopt->binary_upgrade)
14812                         return;
14813         }
14814
14815         /* Search for security labels associated with catalogId, using table */
14816         nlabels = findSecLabels(fout, catalogId.tableoid, catalogId.oid, &labels);
14817
14818         query = createPQExpBuffer();
14819
14820         for (i = 0; i < nlabels; i++)
14821         {
14822                 /*
14823                  * Ignore label entries for which the subid doesn't match.
14824                  */
14825                 if (labels[i].objsubid != subid)
14826                         continue;
14827
14828                 appendPQExpBuffer(query,
14829                                                   "SECURITY LABEL FOR %s ON %s ",
14830                                                   fmtId(labels[i].provider), type);
14831                 if (namespace && *namespace)
14832                         appendPQExpBuffer(query, "%s.", fmtId(namespace));
14833                 appendPQExpBuffer(query, "%s IS ", name);
14834                 appendStringLiteralAH(query, labels[i].label, fout);
14835                 appendPQExpBufferStr(query, ";\n");
14836         }
14837
14838         if (query->len > 0)
14839         {
14840                 PQExpBuffer tag = createPQExpBuffer();
14841
14842                 appendPQExpBuffer(tag, "%s %s", type, name);
14843                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14844                                          tag->data, namespace, NULL, owner,
14845                                          false, "SECURITY LABEL", SECTION_NONE,
14846                                          query->data, "", NULL,
14847                                          &(dumpId), 1,
14848                                          NULL, NULL);
14849                 destroyPQExpBuffer(tag);
14850         }
14851
14852         destroyPQExpBuffer(query);
14853 }
14854
14855 /*
14856  * dumpTableSecLabel
14857  *
14858  * As above, but dump security label for both the specified table (or view)
14859  * and its columns.
14860  */
14861 static void
14862 dumpTableSecLabel(Archive *fout, TableInfo *tbinfo, const char *reltypename)
14863 {
14864         DumpOptions *dopt = fout->dopt;
14865         SecLabelItem *labels;
14866         int                     nlabels;
14867         int                     i;
14868         PQExpBuffer query;
14869         PQExpBuffer target;
14870
14871         /* do nothing, if --no-security-labels is supplied */
14872         if (dopt->no_security_labels)
14873                 return;
14874
14875         /* SecLabel are SCHEMA not data */
14876         if (dopt->dataOnly)
14877                 return;
14878
14879         /* Search for comments associated with relation, using table */
14880         nlabels = findSecLabels(fout,
14881                                                         tbinfo->dobj.catId.tableoid,
14882                                                         tbinfo->dobj.catId.oid,
14883                                                         &labels);
14884
14885         /* If security labels exist, build SECURITY LABEL statements */
14886         if (nlabels <= 0)
14887                 return;
14888
14889         query = createPQExpBuffer();
14890         target = createPQExpBuffer();
14891
14892         for (i = 0; i < nlabels; i++)
14893         {
14894                 const char *colname;
14895                 const char *provider = labels[i].provider;
14896                 const char *label = labels[i].label;
14897                 int                     objsubid = labels[i].objsubid;
14898
14899                 resetPQExpBuffer(target);
14900                 if (objsubid == 0)
14901                 {
14902                         appendPQExpBuffer(target, "%s %s", reltypename,
14903                                                           fmtQualifiedDumpable(tbinfo));
14904                 }
14905                 else
14906                 {
14907                         colname = getAttrName(objsubid, tbinfo);
14908                         /* first fmtXXX result must be consumed before calling again */
14909                         appendPQExpBuffer(target, "COLUMN %s",
14910                                                           fmtQualifiedDumpable(tbinfo));
14911                         appendPQExpBuffer(target, ".%s", fmtId(colname));
14912                 }
14913                 appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ",
14914                                                   fmtId(provider), target->data);
14915                 appendStringLiteralAH(query, label, fout);
14916                 appendPQExpBufferStr(query, ";\n");
14917         }
14918         if (query->len > 0)
14919         {
14920                 resetPQExpBuffer(target);
14921                 appendPQExpBuffer(target, "%s %s", reltypename,
14922                                                   fmtId(tbinfo->dobj.name));
14923                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14924                                          target->data,
14925                                          tbinfo->dobj.namespace->dobj.name,
14926                                          NULL, tbinfo->rolname,
14927                                          false, "SECURITY LABEL", SECTION_NONE,
14928                                          query->data, "", NULL,
14929                                          &(tbinfo->dobj.dumpId), 1,
14930                                          NULL, NULL);
14931         }
14932         destroyPQExpBuffer(query);
14933         destroyPQExpBuffer(target);
14934 }
14935
14936 /*
14937  * findSecLabels
14938  *
14939  * Find the security label(s), if any, associated with the given object.
14940  * All the objsubid values associated with the given classoid/objoid are
14941  * found with one search.
14942  */
14943 static int
14944 findSecLabels(Archive *fout, Oid classoid, Oid objoid, SecLabelItem **items)
14945 {
14946         /* static storage for table of security labels */
14947         static SecLabelItem *labels = NULL;
14948         static int      nlabels = -1;
14949
14950         SecLabelItem *middle = NULL;
14951         SecLabelItem *low;
14952         SecLabelItem *high;
14953         int                     nmatch;
14954
14955         /* Get security labels if we didn't already */
14956         if (nlabels < 0)
14957                 nlabels = collectSecLabels(fout, &labels);
14958
14959         if (nlabels <= 0)                       /* no labels, so no match is possible */
14960         {
14961                 *items = NULL;
14962                 return 0;
14963         }
14964
14965         /*
14966          * Do binary search to find some item matching the object.
14967          */
14968         low = &labels[0];
14969         high = &labels[nlabels - 1];
14970         while (low <= high)
14971         {
14972                 middle = low + (high - low) / 2;
14973
14974                 if (classoid < middle->classoid)
14975                         high = middle - 1;
14976                 else if (classoid > middle->classoid)
14977                         low = middle + 1;
14978                 else if (objoid < middle->objoid)
14979                         high = middle - 1;
14980                 else if (objoid > middle->objoid)
14981                         low = middle + 1;
14982                 else
14983                         break;                          /* found a match */
14984         }
14985
14986         if (low > high)                         /* no matches */
14987         {
14988                 *items = NULL;
14989                 return 0;
14990         }
14991
14992         /*
14993          * Now determine how many items match the object.  The search loop
14994          * invariant still holds: only items between low and high inclusive could
14995          * match.
14996          */
14997         nmatch = 1;
14998         while (middle > low)
14999         {
15000                 if (classoid != middle[-1].classoid ||
15001                         objoid != middle[-1].objoid)
15002                         break;
15003                 middle--;
15004                 nmatch++;
15005         }
15006
15007         *items = middle;
15008
15009         middle += nmatch;
15010         while (middle <= high)
15011         {
15012                 if (classoid != middle->classoid ||
15013                         objoid != middle->objoid)
15014                         break;
15015                 middle++;
15016                 nmatch++;
15017         }
15018
15019         return nmatch;
15020 }
15021
15022 /*
15023  * collectSecLabels
15024  *
15025  * Construct a table of all security labels available for database objects.
15026  * It's much faster to pull them all at once.
15027  *
15028  * The table is sorted by classoid/objid/objsubid for speed in lookup.
15029  */
15030 static int
15031 collectSecLabels(Archive *fout, SecLabelItem **items)
15032 {
15033         PGresult   *res;
15034         PQExpBuffer query;
15035         int                     i_label;
15036         int                     i_provider;
15037         int                     i_classoid;
15038         int                     i_objoid;
15039         int                     i_objsubid;
15040         int                     ntups;
15041         int                     i;
15042         SecLabelItem *labels;
15043
15044         query = createPQExpBuffer();
15045
15046         appendPQExpBufferStr(query,
15047                                                  "SELECT label, provider, classoid, objoid, objsubid "
15048                                                  "FROM pg_catalog.pg_seclabel "
15049                                                  "ORDER BY classoid, objoid, objsubid");
15050
15051         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15052
15053         /* Construct lookup table containing OIDs in numeric form */
15054         i_label = PQfnumber(res, "label");
15055         i_provider = PQfnumber(res, "provider");
15056         i_classoid = PQfnumber(res, "classoid");
15057         i_objoid = PQfnumber(res, "objoid");
15058         i_objsubid = PQfnumber(res, "objsubid");
15059
15060         ntups = PQntuples(res);
15061
15062         labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
15063
15064         for (i = 0; i < ntups; i++)
15065         {
15066                 labels[i].label = PQgetvalue(res, i, i_label);
15067                 labels[i].provider = PQgetvalue(res, i, i_provider);
15068                 labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
15069                 labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
15070                 labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
15071         }
15072
15073         /* Do NOT free the PGresult since we are keeping pointers into it */
15074         destroyPQExpBuffer(query);
15075
15076         *items = labels;
15077         return ntups;
15078 }
15079
15080 /*
15081  * dumpTable
15082  *        write out to fout the declarations (not data) of a user-defined table
15083  */
15084 static void
15085 dumpTable(Archive *fout, TableInfo *tbinfo)
15086 {
15087         DumpOptions *dopt = fout->dopt;
15088         char       *namecopy;
15089
15090         /*
15091          * noop if we are not dumping anything about this table, or if we are
15092          * doing a data-only dump
15093          */
15094         if (!tbinfo->dobj.dump || dopt->dataOnly)
15095                 return;
15096
15097         if (tbinfo->relkind == RELKIND_SEQUENCE)
15098                 dumpSequence(fout, tbinfo);
15099         else
15100                 dumpTableSchema(fout, tbinfo);
15101
15102         /* Handle the ACL here */
15103         namecopy = pg_strdup(fmtId(tbinfo->dobj.name));
15104         if (tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
15105         {
15106                 const char *objtype =
15107                 (tbinfo->relkind == RELKIND_SEQUENCE) ? "SEQUENCE" : "TABLE";
15108
15109                 dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
15110                                 objtype, namecopy, NULL,
15111                                 tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
15112                                 tbinfo->relacl, tbinfo->rrelacl,
15113                                 tbinfo->initrelacl, tbinfo->initrrelacl);
15114         }
15115
15116         /*
15117          * Handle column ACLs, if any.  Note: we pull these with a separate query
15118          * rather than trying to fetch them during getTableAttrs, so that we won't
15119          * miss ACLs on system columns.
15120          */
15121         if (fout->remoteVersion >= 80400 && tbinfo->dobj.dump & DUMP_COMPONENT_ACL)
15122         {
15123                 PQExpBuffer query = createPQExpBuffer();
15124                 PGresult   *res;
15125                 int                     i;
15126
15127                 if (fout->remoteVersion >= 90600)
15128                 {
15129                         PQExpBuffer acl_subquery = createPQExpBuffer();
15130                         PQExpBuffer racl_subquery = createPQExpBuffer();
15131                         PQExpBuffer initacl_subquery = createPQExpBuffer();
15132                         PQExpBuffer initracl_subquery = createPQExpBuffer();
15133
15134                         buildACLQueries(acl_subquery, racl_subquery, initacl_subquery,
15135                                                         initracl_subquery, "at.attacl", "c.relowner", "'c'",
15136                                                         dopt->binary_upgrade);
15137
15138                         appendPQExpBuffer(query,
15139                                                           "SELECT at.attname, "
15140                                                           "%s AS attacl, "
15141                                                           "%s AS rattacl, "
15142                                                           "%s AS initattacl, "
15143                                                           "%s AS initrattacl "
15144                                                           "FROM pg_catalog.pg_attribute at "
15145                                                           "JOIN pg_catalog.pg_class c ON (at.attrelid = c.oid) "
15146                                                           "LEFT JOIN pg_catalog.pg_init_privs pip ON "
15147                                                           "(at.attrelid = pip.objoid "
15148                                                           "AND pip.classoid = 'pg_catalog.pg_class'::pg_catalog.regclass "
15149                                                           "AND at.attnum = pip.objsubid) "
15150                                                           "WHERE at.attrelid = '%u'::pg_catalog.oid AND "
15151                                                           "NOT at.attisdropped "
15152                                                           "AND ("
15153                                                           "%s IS NOT NULL OR "
15154                                                           "%s IS NOT NULL OR "
15155                                                           "%s IS NOT NULL OR "
15156                                                           "%s IS NOT NULL)"
15157                                                           "ORDER BY at.attnum",
15158                                                           acl_subquery->data,
15159                                                           racl_subquery->data,
15160                                                           initacl_subquery->data,
15161                                                           initracl_subquery->data,
15162                                                           tbinfo->dobj.catId.oid,
15163                                                           acl_subquery->data,
15164                                                           racl_subquery->data,
15165                                                           initacl_subquery->data,
15166                                                           initracl_subquery->data);
15167
15168                         destroyPQExpBuffer(acl_subquery);
15169                         destroyPQExpBuffer(racl_subquery);
15170                         destroyPQExpBuffer(initacl_subquery);
15171                         destroyPQExpBuffer(initracl_subquery);
15172                 }
15173                 else
15174                 {
15175                         appendPQExpBuffer(query,
15176                                                           "SELECT attname, attacl, NULL as rattacl, "
15177                                                           "NULL AS initattacl, NULL AS initrattacl "
15178                                                           "FROM pg_catalog.pg_attribute "
15179                                                           "WHERE attrelid = '%u'::pg_catalog.oid AND NOT attisdropped "
15180                                                           "AND attacl IS NOT NULL "
15181                                                           "ORDER BY attnum",
15182                                                           tbinfo->dobj.catId.oid);
15183                 }
15184
15185                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15186
15187                 for (i = 0; i < PQntuples(res); i++)
15188                 {
15189                         char       *attname = PQgetvalue(res, i, 0);
15190                         char       *attacl = PQgetvalue(res, i, 1);
15191                         char       *rattacl = PQgetvalue(res, i, 2);
15192                         char       *initattacl = PQgetvalue(res, i, 3);
15193                         char       *initrattacl = PQgetvalue(res, i, 4);
15194                         char       *attnamecopy;
15195
15196                         attnamecopy = pg_strdup(fmtId(attname));
15197                         /* Column's GRANT type is always TABLE */
15198                         dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
15199                                         "TABLE", namecopy, attnamecopy,
15200                                         tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
15201                                         attacl, rattacl, initattacl, initrattacl);
15202                         free(attnamecopy);
15203                 }
15204                 PQclear(res);
15205                 destroyPQExpBuffer(query);
15206         }
15207
15208         free(namecopy);
15209
15210         return;
15211 }
15212
15213 /*
15214  * Create the AS clause for a view or materialized view. The semicolon is
15215  * stripped because a materialized view must add a WITH NO DATA clause.
15216  *
15217  * This returns a new buffer which must be freed by the caller.
15218  */
15219 static PQExpBuffer
15220 createViewAsClause(Archive *fout, TableInfo *tbinfo)
15221 {
15222         PQExpBuffer query = createPQExpBuffer();
15223         PQExpBuffer result = createPQExpBuffer();
15224         PGresult   *res;
15225         int                     len;
15226
15227         /* Fetch the view definition */
15228         appendPQExpBuffer(query,
15229                                           "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
15230                                           tbinfo->dobj.catId.oid);
15231
15232         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
15233
15234         if (PQntuples(res) != 1)
15235         {
15236                 if (PQntuples(res) < 1)
15237                         exit_horribly(NULL, "query to obtain definition of view \"%s\" returned no data\n",
15238                                                   tbinfo->dobj.name);
15239                 else
15240                         exit_horribly(NULL, "query to obtain definition of view \"%s\" returned more than one definition\n",
15241                                                   tbinfo->dobj.name);
15242         }
15243
15244         len = PQgetlength(res, 0, 0);
15245
15246         if (len == 0)
15247                 exit_horribly(NULL, "definition of view \"%s\" appears to be empty (length zero)\n",
15248                                           tbinfo->dobj.name);
15249
15250         /* Strip off the trailing semicolon so that other things may follow. */
15251         Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
15252         appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
15253
15254         PQclear(res);
15255         destroyPQExpBuffer(query);
15256
15257         return result;
15258 }
15259
15260 /*
15261  * Create a dummy AS clause for a view.  This is used when the real view
15262  * definition has to be postponed because of circular dependencies.
15263  * We must duplicate the view's external properties -- column names and types
15264  * (including collation) -- so that it works for subsequent references.
15265  *
15266  * This returns a new buffer which must be freed by the caller.
15267  */
15268 static PQExpBuffer
15269 createDummyViewAsClause(Archive *fout, TableInfo *tbinfo)
15270 {
15271         PQExpBuffer result = createPQExpBuffer();
15272         int                     j;
15273
15274         appendPQExpBufferStr(result, "SELECT");
15275
15276         for (j = 0; j < tbinfo->numatts; j++)
15277         {
15278                 if (j > 0)
15279                         appendPQExpBufferChar(result, ',');
15280                 appendPQExpBufferStr(result, "\n    ");
15281
15282                 appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]);
15283
15284                 /*
15285                  * Must add collation if not default for the type, because CREATE OR
15286                  * REPLACE VIEW won't change it
15287                  */
15288                 if (OidIsValid(tbinfo->attcollation[j]))
15289                 {
15290                         CollInfo   *coll;
15291
15292                         coll = findCollationByOid(tbinfo->attcollation[j]);
15293                         if (coll)
15294                                 appendPQExpBuffer(result, " COLLATE %s",
15295                                                                   fmtQualifiedDumpable(coll));
15296                 }
15297
15298                 appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j]));
15299         }
15300
15301         return result;
15302 }
15303
15304 /*
15305  * dumpTableSchema
15306  *        write the declaration (not data) of one user-defined table or view
15307  */
15308 static void
15309 dumpTableSchema(Archive *fout, TableInfo *tbinfo)
15310 {
15311         DumpOptions *dopt = fout->dopt;
15312         PQExpBuffer q = createPQExpBuffer();
15313         PQExpBuffer delq = createPQExpBuffer();
15314         char       *qrelname;
15315         char       *qualrelname;
15316         int                     numParents;
15317         TableInfo **parents;
15318         int                     actual_atts;    /* number of attrs in this CREATE statement */
15319         const char *reltypename;
15320         char       *storage;
15321         char       *srvname;
15322         char       *ftoptions;
15323         int                     j,
15324                                 k;
15325
15326         qrelname = pg_strdup(fmtId(tbinfo->dobj.name));
15327         qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
15328
15329         if (dopt->binary_upgrade)
15330                 binary_upgrade_set_type_oids_by_rel_oid(fout, q,
15331                                                                                                 tbinfo->dobj.catId.oid);
15332
15333         /* Is it a table or a view? */
15334         if (tbinfo->relkind == RELKIND_VIEW)
15335         {
15336                 PQExpBuffer result;
15337
15338                 /*
15339                  * Note: keep this code in sync with the is_view case in dumpRule()
15340                  */
15341
15342                 reltypename = "VIEW";
15343
15344                 appendPQExpBuffer(delq, "DROP VIEW %s;\n", qualrelname);
15345
15346                 if (dopt->binary_upgrade)
15347                         binary_upgrade_set_pg_class_oids(fout, q,
15348                                                                                          tbinfo->dobj.catId.oid, false);
15349
15350                 appendPQExpBuffer(q, "CREATE VIEW %s", qualrelname);
15351
15352                 if (tbinfo->dummy_view)
15353                         result = createDummyViewAsClause(fout, tbinfo);
15354                 else
15355                 {
15356                         if (nonemptyReloptions(tbinfo->reloptions))
15357                         {
15358                                 appendPQExpBufferStr(q, " WITH (");
15359                                 appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
15360                                 appendPQExpBufferChar(q, ')');
15361                         }
15362                         result = createViewAsClause(fout, tbinfo);
15363                 }
15364                 appendPQExpBuffer(q, " AS\n%s", result->data);
15365                 destroyPQExpBuffer(result);
15366
15367                 if (tbinfo->checkoption != NULL && !tbinfo->dummy_view)
15368                         appendPQExpBuffer(q, "\n  WITH %s CHECK OPTION", tbinfo->checkoption);
15369                 appendPQExpBufferStr(q, ";\n");
15370         }
15371         else
15372         {
15373                 switch (tbinfo->relkind)
15374                 {
15375                         case RELKIND_FOREIGN_TABLE:
15376                                 {
15377                                         PQExpBuffer query = createPQExpBuffer();
15378                                         PGresult   *res;
15379                                         int                     i_srvname;
15380                                         int                     i_ftoptions;
15381
15382                                         reltypename = "FOREIGN TABLE";
15383
15384                                         /* retrieve name of foreign server and generic options */
15385                                         appendPQExpBuffer(query,
15386                                                                           "SELECT fs.srvname, "
15387                                                                           "pg_catalog.array_to_string(ARRAY("
15388                                                                           "SELECT pg_catalog.quote_ident(option_name) || "
15389                                                                           "' ' || pg_catalog.quote_literal(option_value) "
15390                                                                           "FROM pg_catalog.pg_options_to_table(ftoptions) "
15391                                                                           "ORDER BY option_name"
15392                                                                           "), E',\n    ') AS ftoptions "
15393                                                                           "FROM pg_catalog.pg_foreign_table ft "
15394                                                                           "JOIN pg_catalog.pg_foreign_server fs "
15395                                                                           "ON (fs.oid = ft.ftserver) "
15396                                                                           "WHERE ft.ftrelid = '%u'",
15397                                                                           tbinfo->dobj.catId.oid);
15398                                         res = ExecuteSqlQueryForSingleRow(fout, query->data);
15399                                         i_srvname = PQfnumber(res, "srvname");
15400                                         i_ftoptions = PQfnumber(res, "ftoptions");
15401                                         srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
15402                                         ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
15403                                         PQclear(res);
15404                                         destroyPQExpBuffer(query);
15405                                         break;
15406                                 }
15407                         case RELKIND_MATVIEW:
15408                                 reltypename = "MATERIALIZED VIEW";
15409                                 srvname = NULL;
15410                                 ftoptions = NULL;
15411                                 break;
15412                         default:
15413                                 reltypename = "TABLE";
15414                                 srvname = NULL;
15415                                 ftoptions = NULL;
15416                 }
15417
15418                 numParents = tbinfo->numParents;
15419                 parents = tbinfo->parents;
15420
15421                 appendPQExpBuffer(delq, "DROP %s %s;\n", reltypename, qualrelname);
15422
15423                 if (dopt->binary_upgrade)
15424                         binary_upgrade_set_pg_class_oids(fout, q,
15425                                                                                          tbinfo->dobj.catId.oid, false);
15426
15427                 appendPQExpBuffer(q, "CREATE %s%s %s",
15428                                                   tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
15429                                                   "UNLOGGED " : "",
15430                                                   reltypename,
15431                                                   qualrelname);
15432
15433                 /*
15434                  * Attach to type, if reloftype; except in case of a binary upgrade,
15435                  * we dump the table normally and attach it to the type afterward.
15436                  */
15437                 if (tbinfo->reloftype && !dopt->binary_upgrade)
15438                         appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
15439
15440                 /*
15441                  * If the table is a partition, dump it as such; except in the case of
15442                  * a binary upgrade, we dump the table normally and attach it to the
15443                  * parent afterward.
15444                  */
15445                 if (tbinfo->ispartition && !dopt->binary_upgrade)
15446                 {
15447                         TableInfo  *parentRel = tbinfo->parents[0];
15448
15449                         /*
15450                          * With partitions, unlike inheritance, there can only be one
15451                          * parent.
15452                          */
15453                         if (tbinfo->numParents != 1)
15454                                 exit_horribly(NULL, "invalid number of parents %d for table \"%s\"\n",
15455                                                           tbinfo->numParents, tbinfo->dobj.name);
15456
15457                         appendPQExpBuffer(q, " PARTITION OF %s",
15458                                                           fmtQualifiedDumpable(parentRel));
15459                 }
15460
15461                 if (tbinfo->relkind != RELKIND_MATVIEW)
15462                 {
15463                         /* Dump the attributes */
15464                         actual_atts = 0;
15465                         for (j = 0; j < tbinfo->numatts; j++)
15466                         {
15467                                 /*
15468                                  * Normally, dump if it's locally defined in this table, and
15469                                  * not dropped.  But for binary upgrade, we'll dump all the
15470                                  * columns, and then fix up the dropped and nonlocal cases
15471                                  * below.
15472                                  */
15473                                 if (shouldPrintColumn(dopt, tbinfo, j))
15474                                 {
15475                                         /*
15476                                          * Default value --- suppress if to be printed separately.
15477                                          */
15478                                         bool            has_default = (tbinfo->attrdefs[j] != NULL &&
15479                                                                                            !tbinfo->attrdefs[j]->separate);
15480
15481                                         /*
15482                                          * Not Null constraint --- suppress if inherited, except
15483                                          * in binary-upgrade case where that won't work.
15484                                          */
15485                                         bool            has_notnull = (tbinfo->notnull[j] &&
15486                                                                                            (!tbinfo->inhNotNull[j] ||
15487                                                                                                 dopt->binary_upgrade));
15488
15489                                         /*
15490                                          * Skip column if fully defined by reloftype or the
15491                                          * partition parent.
15492                                          */
15493                                         if ((tbinfo->reloftype || tbinfo->ispartition) &&
15494                                                 !has_default && !has_notnull && !dopt->binary_upgrade)
15495                                                 continue;
15496
15497                                         /* Format properly if not first attr */
15498                                         if (actual_atts == 0)
15499                                                 appendPQExpBufferStr(q, " (");
15500                                         else
15501                                                 appendPQExpBufferChar(q, ',');
15502                                         appendPQExpBufferStr(q, "\n    ");
15503                                         actual_atts++;
15504
15505                                         /* Attribute name */
15506                                         appendPQExpBufferStr(q, fmtId(tbinfo->attnames[j]));
15507
15508                                         if (tbinfo->attisdropped[j])
15509                                         {
15510                                                 /*
15511                                                  * ALTER TABLE DROP COLUMN clears
15512                                                  * pg_attribute.atttypid, so we will not have gotten a
15513                                                  * valid type name; insert INTEGER as a stopgap. We'll
15514                                                  * clean things up later.
15515                                                  */
15516                                                 appendPQExpBufferStr(q, " INTEGER /* dummy */");
15517                                                 /* Skip all the rest, too */
15518                                                 continue;
15519                                         }
15520
15521                                         /*
15522                                          * Attribute type
15523                                          *
15524                                          * In binary-upgrade mode, we always include the type. If
15525                                          * we aren't in binary-upgrade mode, then we skip the type
15526                                          * when creating a typed table ('OF type_name') or a
15527                                          * partition ('PARTITION OF'), since the type comes from
15528                                          * the parent/partitioned table.
15529                                          */
15530                                         if (dopt->binary_upgrade || (!tbinfo->reloftype && !tbinfo->ispartition))
15531                                         {
15532                                                 appendPQExpBuffer(q, " %s",
15533                                                                                   tbinfo->atttypnames[j]);
15534                                         }
15535
15536                                         /* Add collation if not default for the type */
15537                                         if (OidIsValid(tbinfo->attcollation[j]))
15538                                         {
15539                                                 CollInfo   *coll;
15540
15541                                                 coll = findCollationByOid(tbinfo->attcollation[j]);
15542                                                 if (coll)
15543                                                         appendPQExpBuffer(q, " COLLATE %s",
15544                                                                                           fmtQualifiedDumpable(coll));
15545                                         }
15546
15547                                         if (has_default)
15548                                                 appendPQExpBuffer(q, " DEFAULT %s",
15549                                                                                   tbinfo->attrdefs[j]->adef_expr);
15550
15551                                         if (has_notnull)
15552                                                 appendPQExpBufferStr(q, " NOT NULL");
15553                                 }
15554                         }
15555
15556                         /*
15557                          * Add non-inherited CHECK constraints, if any.
15558                          */
15559                         for (j = 0; j < tbinfo->ncheck; j++)
15560                         {
15561                                 ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
15562
15563                                 if (constr->separate || !constr->conislocal)
15564                                         continue;
15565
15566                                 if (actual_atts == 0)
15567                                         appendPQExpBufferStr(q, " (\n    ");
15568                                 else
15569                                         appendPQExpBufferStr(q, ",\n    ");
15570
15571                                 appendPQExpBuffer(q, "CONSTRAINT %s ",
15572                                                                   fmtId(constr->dobj.name));
15573                                 appendPQExpBufferStr(q, constr->condef);
15574
15575                                 actual_atts++;
15576                         }
15577
15578                         if (actual_atts)
15579                                 appendPQExpBufferStr(q, "\n)");
15580                         else if (!((tbinfo->reloftype || tbinfo->ispartition) &&
15581                                            !dopt->binary_upgrade))
15582                         {
15583                                 /*
15584                                  * We must have a parenthesized attribute list, even though
15585                                  * empty, when not using the OF TYPE or PARTITION OF syntax.
15586                                  */
15587                                 appendPQExpBufferStr(q, " (\n)");
15588                         }
15589
15590                         if (tbinfo->ispartition && !dopt->binary_upgrade)
15591                         {
15592                                 appendPQExpBufferChar(q, '\n');
15593                                 appendPQExpBufferStr(q, tbinfo->partbound);
15594                         }
15595
15596                         /* Emit the INHERITS clause, except if this is a partition. */
15597                         if (numParents > 0 &&
15598                                 !tbinfo->ispartition &&
15599                                 !dopt->binary_upgrade)
15600                         {
15601                                 appendPQExpBufferStr(q, "\nINHERITS (");
15602                                 for (k = 0; k < numParents; k++)
15603                                 {
15604                                         TableInfo  *parentRel = parents[k];
15605
15606                                         if (k > 0)
15607                                                 appendPQExpBufferStr(q, ", ");
15608                                         appendPQExpBufferStr(q, fmtQualifiedDumpable(parentRel));
15609                                 }
15610                                 appendPQExpBufferChar(q, ')');
15611                         }
15612
15613                         if (tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
15614                                 appendPQExpBuffer(q, "\nPARTITION BY %s", tbinfo->partkeydef);
15615
15616                         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
15617                                 appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
15618                 }
15619
15620                 if (nonemptyReloptions(tbinfo->reloptions) ||
15621                         nonemptyReloptions(tbinfo->toast_reloptions))
15622                 {
15623                         bool            addcomma = false;
15624
15625                         appendPQExpBufferStr(q, "\nWITH (");
15626                         if (nonemptyReloptions(tbinfo->reloptions))
15627                         {
15628                                 addcomma = true;
15629                                 appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout);
15630                         }
15631                         if (nonemptyReloptions(tbinfo->toast_reloptions))
15632                         {
15633                                 if (addcomma)
15634                                         appendPQExpBufferStr(q, ", ");
15635                                 appendReloptionsArrayAH(q, tbinfo->toast_reloptions, "toast.",
15636                                                                                 fout);
15637                         }
15638                         appendPQExpBufferChar(q, ')');
15639                 }
15640
15641                 /* Dump generic options if any */
15642                 if (ftoptions && ftoptions[0])
15643                         appendPQExpBuffer(q, "\nOPTIONS (\n    %s\n)", ftoptions);
15644
15645                 /*
15646                  * For materialized views, create the AS clause just like a view. At
15647                  * this point, we always mark the view as not populated.
15648                  */
15649                 if (tbinfo->relkind == RELKIND_MATVIEW)
15650                 {
15651                         PQExpBuffer result;
15652
15653                         result = createViewAsClause(fout, tbinfo);
15654                         appendPQExpBuffer(q, " AS\n%s\n  WITH NO DATA;\n",
15655                                                           result->data);
15656                         destroyPQExpBuffer(result);
15657                 }
15658                 else
15659                         appendPQExpBufferStr(q, ";\n");
15660
15661                 /*
15662                  * To create binary-compatible heap files, we have to ensure the same
15663                  * physical column order, including dropped columns, as in the
15664                  * original.  Therefore, we create dropped columns above and drop them
15665                  * here, also updating their attlen/attalign values so that the
15666                  * dropped column can be skipped properly.  (We do not bother with
15667                  * restoring the original attbyval setting.)  Also, inheritance
15668                  * relationships are set up by doing ALTER TABLE INHERIT rather than
15669                  * using an INHERITS clause --- the latter would possibly mess up the
15670                  * column order.  That also means we have to take care about setting
15671                  * attislocal correctly, plus fix up any inherited CHECK constraints.
15672                  * Analogously, we set up typed tables using ALTER TABLE / OF here.
15673                  *
15674                  * We process foreign and partitioned tables here, even though they
15675                  * lack heap storage, because they can participate in inheritance
15676                  * relationships and we want this stuff to be consistent across the
15677                  * inheritance tree.  We can exclude indexes, toast tables, sequences
15678                  * and matviews, even though they have storage, because we don't
15679                  * support altering or dropping columns in them, nor can they be part
15680                  * of inheritance trees.
15681                  */
15682                 if (dopt->binary_upgrade &&
15683                         (tbinfo->relkind == RELKIND_RELATION ||
15684                          tbinfo->relkind == RELKIND_FOREIGN_TABLE ||
15685                          tbinfo->relkind == RELKIND_PARTITIONED_TABLE))
15686                 {
15687                         for (j = 0; j < tbinfo->numatts; j++)
15688                         {
15689                                 if (tbinfo->attisdropped[j])
15690                                 {
15691                                         appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate dropped column.\n");
15692                                         appendPQExpBuffer(q, "UPDATE pg_catalog.pg_attribute\n"
15693                                                                           "SET attlen = %d, "
15694                                                                           "attalign = '%c', attbyval = false\n"
15695                                                                           "WHERE attname = ",
15696                                                                           tbinfo->attlen[j],
15697                                                                           tbinfo->attalign[j]);
15698                                         appendStringLiteralAH(q, tbinfo->attnames[j], fout);
15699                                         appendPQExpBufferStr(q, "\n  AND attrelid = ");
15700                                         appendStringLiteralAH(q, qualrelname, fout);
15701                                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15702
15703                                         if (tbinfo->relkind == RELKIND_RELATION ||
15704                                                 tbinfo->relkind == RELKIND_PARTITIONED_TABLE)
15705                                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15706                                                                                   qualrelname);
15707                                         else
15708                                                 appendPQExpBuffer(q, "ALTER FOREIGN TABLE ONLY %s ",
15709                                                                                   qualrelname);
15710                                         appendPQExpBuffer(q, "DROP COLUMN %s;\n",
15711                                                                           fmtId(tbinfo->attnames[j]));
15712                                 }
15713                                 else if (!tbinfo->attislocal[j])
15714                                 {
15715                                         appendPQExpBufferStr(q, "\n-- For binary upgrade, recreate inherited column.\n");
15716                                         appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_attribute\n"
15717                                                                                  "SET attislocal = false\n"
15718                                                                                  "WHERE attname = ");
15719                                         appendStringLiteralAH(q, tbinfo->attnames[j], fout);
15720                                         appendPQExpBufferStr(q, "\n  AND attrelid = ");
15721                                         appendStringLiteralAH(q, qualrelname, fout);
15722                                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15723                                 }
15724                         }
15725
15726                         for (k = 0; k < tbinfo->ncheck; k++)
15727                         {
15728                                 ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
15729
15730                                 if (constr->separate || constr->conislocal)
15731                                         continue;
15732
15733                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inherited constraint.\n");
15734                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15735                                                                   qualrelname);
15736                                 appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
15737                                                                   fmtId(constr->dobj.name));
15738                                 appendPQExpBuffer(q, "%s;\n", constr->condef);
15739                                 appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_constraint\n"
15740                                                                          "SET conislocal = false\n"
15741                                                                          "WHERE contype = 'c' AND conname = ");
15742                                 appendStringLiteralAH(q, constr->dobj.name, fout);
15743                                 appendPQExpBufferStr(q, "\n  AND conrelid = ");
15744                                 appendStringLiteralAH(q, qualrelname, fout);
15745                                 appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15746                         }
15747
15748                         if (numParents > 0)
15749                         {
15750                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set up inheritance and partitioning this way.\n");
15751                                 for (k = 0; k < numParents; k++)
15752                                 {
15753                                         TableInfo  *parentRel = parents[k];
15754
15755                                         /* In the partitioning case, we alter the parent */
15756                                         if (tbinfo->ispartition)
15757                                                 appendPQExpBuffer(q,
15758                                                                                   "ALTER TABLE ONLY %s ATTACH PARTITION ",
15759                                                                                   fmtQualifiedDumpable(parentRel));
15760                                         else
15761                                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
15762                                                                                   qualrelname);
15763
15764                                         /* Partition needs specifying the bounds */
15765                                         if (tbinfo->ispartition)
15766                                                 appendPQExpBuffer(q, "%s %s;\n",
15767                                                                                   qualrelname,
15768                                                                                   tbinfo->partbound);
15769                                         else
15770                                                 appendPQExpBuffer(q, "%s;\n",
15771                                                                                   fmtQualifiedDumpable(parentRel));
15772                                 }
15773                         }
15774
15775                         if (tbinfo->reloftype)
15776                         {
15777                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set up typed tables this way.\n");
15778                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
15779                                                                   qualrelname,
15780                                                                   tbinfo->reloftype);
15781                         }
15782                 }
15783
15784                 /*
15785                  * In binary_upgrade mode, arrange to restore the old relfrozenxid and
15786                  * relminmxid of all vacuumable relations.  (While vacuum.c processes
15787                  * TOAST tables semi-independently, here we see them only as children
15788                  * of other relations; so this "if" lacks RELKIND_TOASTVALUE, and the
15789                  * child toast table is handled below.)
15790                  */
15791                 if (dopt->binary_upgrade &&
15792                         (tbinfo->relkind == RELKIND_RELATION ||
15793                          tbinfo->relkind == RELKIND_MATVIEW))
15794                 {
15795                         appendPQExpBufferStr(q, "\n-- For binary upgrade, set heap's relfrozenxid and relminmxid\n");
15796                         appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
15797                                                           "SET relfrozenxid = '%u', relminmxid = '%u'\n"
15798                                                           "WHERE oid = ",
15799                                                           tbinfo->frozenxid, tbinfo->minmxid);
15800                         appendStringLiteralAH(q, qualrelname, fout);
15801                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15802
15803                         if (tbinfo->toast_oid)
15804                         {
15805                                 /*
15806                                  * The toast table will have the same OID at restore, so we
15807                                  * can safely target it by OID.
15808                                  */
15809                                 appendPQExpBufferStr(q, "\n-- For binary upgrade, set toast's relfrozenxid and relminmxid\n");
15810                                 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
15811                                                                   "SET relfrozenxid = '%u', relminmxid = '%u'\n"
15812                                                                   "WHERE oid = '%u';\n",
15813                                                                   tbinfo->toast_frozenxid,
15814                                                                   tbinfo->toast_minmxid, tbinfo->toast_oid);
15815                         }
15816                 }
15817
15818                 /*
15819                  * In binary_upgrade mode, restore matviews' populated status by
15820                  * poking pg_class directly.  This is pretty ugly, but we can't use
15821                  * REFRESH MATERIALIZED VIEW since it's possible that some underlying
15822                  * matview is not populated even though this matview is; in any case,
15823                  * we want to transfer the matview's heap storage, not run REFRESH.
15824                  */
15825                 if (dopt->binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
15826                         tbinfo->relispopulated)
15827                 {
15828                         appendPQExpBufferStr(q, "\n-- For binary upgrade, mark materialized view as populated\n");
15829                         appendPQExpBufferStr(q, "UPDATE pg_catalog.pg_class\n"
15830                                                                  "SET relispopulated = 't'\n"
15831                                                                  "WHERE oid = ");
15832                         appendStringLiteralAH(q, qualrelname, fout);
15833                         appendPQExpBufferStr(q, "::pg_catalog.regclass;\n");
15834                 }
15835
15836                 /*
15837                  * Dump additional per-column properties that we can't handle in the
15838                  * main CREATE TABLE command.
15839                  */
15840                 for (j = 0; j < tbinfo->numatts; j++)
15841                 {
15842                         /* None of this applies to dropped columns */
15843                         if (tbinfo->attisdropped[j])
15844                                 continue;
15845
15846                         /*
15847                          * If we didn't dump the column definition explicitly above, and
15848                          * it is NOT NULL and did not inherit that property from a parent,
15849                          * we have to mark it separately.
15850                          */
15851                         if (!shouldPrintColumn(dopt, tbinfo, j) &&
15852                                 tbinfo->notnull[j] && !tbinfo->inhNotNull[j])
15853                         {
15854                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15855                                                                   qualrelname);
15856                                 appendPQExpBuffer(q, "ALTER COLUMN %s SET NOT NULL;\n",
15857                                                                   fmtId(tbinfo->attnames[j]));
15858                         }
15859
15860                         /*
15861                          * Dump per-column statistics information. We only issue an ALTER
15862                          * TABLE statement if the attstattarget entry for this column is
15863                          * non-negative (i.e. it's not the default value)
15864                          */
15865                         if (tbinfo->attstattarget[j] >= 0)
15866                         {
15867                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15868                                                                   qualrelname);
15869                                 appendPQExpBuffer(q, "ALTER COLUMN %s ",
15870                                                                   fmtId(tbinfo->attnames[j]));
15871                                 appendPQExpBuffer(q, "SET STATISTICS %d;\n",
15872                                                                   tbinfo->attstattarget[j]);
15873                         }
15874
15875                         /*
15876                          * Dump per-column storage information.  The statement is only
15877                          * dumped if the storage has been changed from the type's default.
15878                          */
15879                         if (tbinfo->attstorage[j] != tbinfo->typstorage[j])
15880                         {
15881                                 switch (tbinfo->attstorage[j])
15882                                 {
15883                                         case 'p':
15884                                                 storage = "PLAIN";
15885                                                 break;
15886                                         case 'e':
15887                                                 storage = "EXTERNAL";
15888                                                 break;
15889                                         case 'm':
15890                                                 storage = "MAIN";
15891                                                 break;
15892                                         case 'x':
15893                                                 storage = "EXTENDED";
15894                                                 break;
15895                                         default:
15896                                                 storage = NULL;
15897                                 }
15898
15899                                 /*
15900                                  * Only dump the statement if it's a storage type we recognize
15901                                  */
15902                                 if (storage != NULL)
15903                                 {
15904                                         appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15905                                                                           qualrelname);
15906                                         appendPQExpBuffer(q, "ALTER COLUMN %s ",
15907                                                                           fmtId(tbinfo->attnames[j]));
15908                                         appendPQExpBuffer(q, "SET STORAGE %s;\n",
15909                                                                           storage);
15910                                 }
15911                         }
15912
15913                         /*
15914                          * Dump per-column attributes.
15915                          */
15916                         if (tbinfo->attoptions[j] && tbinfo->attoptions[j][0] != '\0')
15917                         {
15918                                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
15919                                                                   qualrelname);
15920                                 appendPQExpBuffer(q, "ALTER COLUMN %s ",
15921                                                                   fmtId(tbinfo->attnames[j]));
15922                                 appendPQExpBuffer(q, "SET (%s);\n",
15923                                                                   tbinfo->attoptions[j]);
15924                         }
15925
15926                         /*
15927                          * Dump per-column fdw options.
15928                          */
15929                         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
15930                                 tbinfo->attfdwoptions[j] &&
15931                                 tbinfo->attfdwoptions[j][0] != '\0')
15932                         {
15933                                 appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
15934                                                                   qualrelname);
15935                                 appendPQExpBuffer(q, "ALTER COLUMN %s ",
15936                                                                   fmtId(tbinfo->attnames[j]));
15937                                 appendPQExpBuffer(q, "OPTIONS (\n    %s\n);\n",
15938                                                                   tbinfo->attfdwoptions[j]);
15939                         }
15940                 }
15941         }
15942
15943         /*
15944          * dump properties we only have ALTER TABLE syntax for
15945          */
15946         if ((tbinfo->relkind == RELKIND_RELATION ||
15947                  tbinfo->relkind == RELKIND_PARTITIONED_TABLE ||
15948                  tbinfo->relkind == RELKIND_MATVIEW) &&
15949                 tbinfo->relreplident != REPLICA_IDENTITY_DEFAULT)
15950         {
15951                 if (tbinfo->relreplident == REPLICA_IDENTITY_INDEX)
15952                 {
15953                         /* nothing to do, will be set when the index is dumped */
15954                 }
15955                 else if (tbinfo->relreplident == REPLICA_IDENTITY_NOTHING)
15956                 {
15957                         appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY NOTHING;\n",
15958                                                           qualrelname);
15959                 }
15960                 else if (tbinfo->relreplident == REPLICA_IDENTITY_FULL)
15961                 {
15962                         appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY FULL;\n",
15963                                                           qualrelname);
15964                 }
15965         }
15966
15967         if (tbinfo->relkind == RELKIND_FOREIGN_TABLE && tbinfo->hasoids)
15968                 appendPQExpBuffer(q, "\nALTER TABLE ONLY %s SET WITH OIDS;\n",
15969                                                   qualrelname);
15970
15971         if (tbinfo->forcerowsec)
15972                 appendPQExpBuffer(q, "\nALTER TABLE ONLY %s FORCE ROW LEVEL SECURITY;\n",
15973                                                   qualrelname);
15974
15975         if (dopt->binary_upgrade)
15976                 binary_upgrade_extension_member(q, &tbinfo->dobj,
15977                                                                                 reltypename, qrelname,
15978                                                                                 tbinfo->dobj.namespace->dobj.name);
15979
15980         if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
15981                 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
15982                                          tbinfo->dobj.name,
15983                                          tbinfo->dobj.namespace->dobj.name,
15984                                          (tbinfo->relkind == RELKIND_VIEW) ? NULL : tbinfo->reltablespace,
15985                                          tbinfo->rolname,
15986                                          (strcmp(reltypename, "TABLE") == 0) ? tbinfo->hasoids : false,
15987                                          reltypename,
15988                                          tbinfo->postponed_def ?
15989                                          SECTION_POST_DATA : SECTION_PRE_DATA,
15990                                          q->data, delq->data, NULL,
15991                                          NULL, 0,
15992                                          NULL, NULL);
15993
15994
15995         /* Dump Table Comments */
15996         if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
15997                 dumpTableComment(fout, tbinfo, reltypename);
15998
15999         /* Dump Table Security Labels */
16000         if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
16001                 dumpTableSecLabel(fout, tbinfo, reltypename);
16002
16003         /* Dump comments on inlined table constraints */
16004         for (j = 0; j < tbinfo->ncheck; j++)
16005         {
16006                 ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
16007
16008                 if (constr->separate || !constr->conislocal)
16009                         continue;
16010
16011                 if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16012                         dumpTableConstraintComment(fout, constr);
16013         }
16014
16015         destroyPQExpBuffer(q);
16016         destroyPQExpBuffer(delq);
16017         free(qrelname);
16018         free(qualrelname);
16019 }
16020
16021 /*
16022  * dumpAttrDef --- dump an attribute's default-value declaration
16023  */
16024 static void
16025 dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
16026 {
16027         DumpOptions *dopt = fout->dopt;
16028         TableInfo  *tbinfo = adinfo->adtable;
16029         int                     adnum = adinfo->adnum;
16030         PQExpBuffer q;
16031         PQExpBuffer delq;
16032         char       *qualrelname;
16033         char       *tag;
16034
16035         /* Skip if table definition not to be dumped */
16036         if (!tbinfo->dobj.dump || dopt->dataOnly)
16037                 return;
16038
16039         /* Skip if not "separate"; it was dumped in the table's definition */
16040         if (!adinfo->separate)
16041                 return;
16042
16043         q = createPQExpBuffer();
16044         delq = createPQExpBuffer();
16045
16046         qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
16047
16048         appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
16049                                           qualrelname);
16050         appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
16051                                           fmtId(tbinfo->attnames[adnum - 1]),
16052                                           adinfo->adef_expr);
16053
16054         appendPQExpBuffer(delq, "ALTER TABLE %s ",
16055                                           qualrelname);
16056         appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
16057                                           fmtId(tbinfo->attnames[adnum - 1]));
16058
16059         tag = psprintf("%s %s", tbinfo->dobj.name, tbinfo->attnames[adnum - 1]);
16060
16061         if (adinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16062                 ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
16063                                          tag,
16064                                          tbinfo->dobj.namespace->dobj.name,
16065                                          NULL,
16066                                          tbinfo->rolname,
16067                                          false, "DEFAULT", SECTION_PRE_DATA,
16068                                          q->data, delq->data, NULL,
16069                                          NULL, 0,
16070                                          NULL, NULL);
16071
16072         free(tag);
16073         destroyPQExpBuffer(q);
16074         destroyPQExpBuffer(delq);
16075         free(qualrelname);
16076 }
16077
16078 /*
16079  * getAttrName: extract the correct name for an attribute
16080  *
16081  * The array tblInfo->attnames[] only provides names of user attributes;
16082  * if a system attribute number is supplied, we have to fake it.
16083  * We also do a little bit of bounds checking for safety's sake.
16084  */
16085 static const char *
16086 getAttrName(int attrnum, TableInfo *tblInfo)
16087 {
16088         if (attrnum > 0 && attrnum <= tblInfo->numatts)
16089                 return tblInfo->attnames[attrnum - 1];
16090         switch (attrnum)
16091         {
16092                 case SelfItemPointerAttributeNumber:
16093                         return "ctid";
16094                 case ObjectIdAttributeNumber:
16095                         return "oid";
16096                 case MinTransactionIdAttributeNumber:
16097                         return "xmin";
16098                 case MinCommandIdAttributeNumber:
16099                         return "cmin";
16100                 case MaxTransactionIdAttributeNumber:
16101                         return "xmax";
16102                 case MaxCommandIdAttributeNumber:
16103                         return "cmax";
16104                 case TableOidAttributeNumber:
16105                         return "tableoid";
16106         }
16107         exit_horribly(NULL, "invalid column number %d for table \"%s\"\n",
16108                                   attrnum, tblInfo->dobj.name);
16109         return NULL;                            /* keep compiler quiet */
16110 }
16111
16112 /*
16113  * dumpIndex
16114  *        write out to fout a user-defined index
16115  */
16116 static void
16117 dumpIndex(Archive *fout, IndxInfo *indxinfo)
16118 {
16119         DumpOptions *dopt = fout->dopt;
16120         TableInfo  *tbinfo = indxinfo->indextable;
16121         bool            is_constraint = (indxinfo->indexconstraint != 0);
16122         PQExpBuffer q;
16123         PQExpBuffer delq;
16124         char       *qindxname;
16125
16126         if (dopt->dataOnly)
16127                 return;
16128
16129         q = createPQExpBuffer();
16130         delq = createPQExpBuffer();
16131
16132         qindxname = pg_strdup(fmtId(indxinfo->dobj.name));
16133
16134         /*
16135          * If there's an associated constraint, don't dump the index per se, but
16136          * do dump any comment for it.  (This is safe because dependency ordering
16137          * will have ensured the constraint is emitted first.)  Note that the
16138          * emitted comment has to be shown as depending on the constraint, not the
16139          * index, in such cases.
16140          */
16141         if (!is_constraint)
16142         {
16143                 if (dopt->binary_upgrade)
16144                         binary_upgrade_set_pg_class_oids(fout, q,
16145                                                                                          indxinfo->dobj.catId.oid, true);
16146
16147                 /* Plain secondary index */
16148                 appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
16149
16150                 /* If the index is clustered, we need to record that. */
16151                 if (indxinfo->indisclustered)
16152                 {
16153                         appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16154                                                           fmtQualifiedDumpable(tbinfo));
16155                         /* index name is not qualified in this syntax */
16156                         appendPQExpBuffer(q, " ON %s;\n",
16157                                                           qindxname);
16158                 }
16159
16160                 /* If the index defines identity, we need to record that. */
16161                 if (indxinfo->indisreplident)
16162                 {
16163                         appendPQExpBuffer(q, "\nALTER TABLE ONLY %s REPLICA IDENTITY USING",
16164                                                           fmtQualifiedDumpable(tbinfo));
16165                         /* index name is not qualified in this syntax */
16166                         appendPQExpBuffer(q, " INDEX %s;\n",
16167                                                           qindxname);
16168                 }
16169
16170                 appendPQExpBuffer(delq, "DROP INDEX %s;\n",
16171                                                   fmtQualifiedDumpable(indxinfo));
16172
16173                 if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16174                         ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
16175                                                  indxinfo->dobj.name,
16176                                                  tbinfo->dobj.namespace->dobj.name,
16177                                                  indxinfo->tablespace,
16178                                                  tbinfo->rolname, false,
16179                                                  "INDEX", SECTION_POST_DATA,
16180                                                  q->data, delq->data, NULL,
16181                                                  NULL, 0,
16182                                                  NULL, NULL);
16183         }
16184
16185         /* Dump Index Comments */
16186         if (indxinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16187                 dumpComment(fout, "INDEX", qindxname,
16188                                         tbinfo->dobj.namespace->dobj.name,
16189                                         tbinfo->rolname,
16190                                         indxinfo->dobj.catId, 0,
16191                                         is_constraint ? indxinfo->indexconstraint :
16192                                         indxinfo->dobj.dumpId);
16193
16194         destroyPQExpBuffer(q);
16195         destroyPQExpBuffer(delq);
16196         free(qindxname);
16197 }
16198
16199 /*
16200  * dumpIndexAttach
16201  *        write out to fout a partitioned-index attachment clause
16202  */
16203 static void
16204 dumpIndexAttach(Archive *fout, IndexAttachInfo *attachinfo)
16205 {
16206         if (fout->dopt->dataOnly)
16207                 return;
16208
16209         if (attachinfo->partitionIdx->dobj.dump & DUMP_COMPONENT_DEFINITION)
16210         {
16211                 PQExpBuffer q = createPQExpBuffer();
16212
16213                 appendPQExpBuffer(q, "\nALTER INDEX %s ",
16214                                                   fmtQualifiedDumpable(attachinfo->parentIdx));
16215                 appendPQExpBuffer(q, "ATTACH PARTITION %s;\n",
16216                                                   fmtQualifiedDumpable(attachinfo->partitionIdx));
16217
16218                 ArchiveEntry(fout, attachinfo->dobj.catId, attachinfo->dobj.dumpId,
16219                                          attachinfo->dobj.name,
16220                                          NULL, NULL,
16221                                          "",
16222                                          false, "INDEX ATTACH", SECTION_POST_DATA,
16223                                          q->data, "", NULL,
16224                                          NULL, 0,
16225                                          NULL, NULL);
16226
16227                 destroyPQExpBuffer(q);
16228         }
16229 }
16230
16231 /*
16232  * dumpStatisticsExt
16233  *        write out to fout an extended statistics object
16234  */
16235 static void
16236 dumpStatisticsExt(Archive *fout, StatsExtInfo *statsextinfo)
16237 {
16238         DumpOptions *dopt = fout->dopt;
16239         PQExpBuffer q;
16240         PQExpBuffer delq;
16241         PQExpBuffer query;
16242         char       *qstatsextname;
16243         PGresult   *res;
16244         char       *stxdef;
16245
16246         /* Skip if not to be dumped */
16247         if (!statsextinfo->dobj.dump || dopt->dataOnly)
16248                 return;
16249
16250         q = createPQExpBuffer();
16251         delq = createPQExpBuffer();
16252         query = createPQExpBuffer();
16253
16254         qstatsextname = pg_strdup(fmtId(statsextinfo->dobj.name));
16255
16256         appendPQExpBuffer(query, "SELECT "
16257                                           "pg_catalog.pg_get_statisticsobjdef('%u'::pg_catalog.oid)",
16258                                           statsextinfo->dobj.catId.oid);
16259
16260         res = ExecuteSqlQueryForSingleRow(fout, query->data);
16261
16262         stxdef = PQgetvalue(res, 0, 0);
16263
16264         /* Result of pg_get_statisticsobjdef is complete except for semicolon */
16265         appendPQExpBuffer(q, "%s;\n", stxdef);
16266
16267         appendPQExpBuffer(delq, "DROP STATISTICS %s;\n",
16268                                           fmtQualifiedDumpable(statsextinfo));
16269
16270         if (statsextinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16271                 ArchiveEntry(fout, statsextinfo->dobj.catId,
16272                                          statsextinfo->dobj.dumpId,
16273                                          statsextinfo->dobj.name,
16274                                          statsextinfo->dobj.namespace->dobj.name,
16275                                          NULL,
16276                                          statsextinfo->rolname, false,
16277                                          "STATISTICS", SECTION_POST_DATA,
16278                                          q->data, delq->data, NULL,
16279                                          NULL, 0,
16280                                          NULL, NULL);
16281
16282         /* Dump Statistics Comments */
16283         if (statsextinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16284                 dumpComment(fout, "STATISTICS", qstatsextname,
16285                                         statsextinfo->dobj.namespace->dobj.name,
16286                                         statsextinfo->rolname,
16287                                         statsextinfo->dobj.catId, 0,
16288                                         statsextinfo->dobj.dumpId);
16289
16290         PQclear(res);
16291         destroyPQExpBuffer(q);
16292         destroyPQExpBuffer(delq);
16293         destroyPQExpBuffer(query);
16294         free(qstatsextname);
16295 }
16296
16297 /*
16298  * dumpConstraint
16299  *        write out to fout a user-defined constraint
16300  */
16301 static void
16302 dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
16303 {
16304         DumpOptions *dopt = fout->dopt;
16305         TableInfo  *tbinfo = coninfo->contable;
16306         PQExpBuffer q;
16307         PQExpBuffer delq;
16308         char       *tag = NULL;
16309
16310         /* Skip if not to be dumped */
16311         if (!coninfo->dobj.dump || dopt->dataOnly)
16312                 return;
16313
16314         q = createPQExpBuffer();
16315         delq = createPQExpBuffer();
16316
16317         if (coninfo->contype == 'p' ||
16318                 coninfo->contype == 'u' ||
16319                 coninfo->contype == 'x')
16320         {
16321                 /* Index-related constraint */
16322                 IndxInfo   *indxinfo;
16323                 int                     k;
16324
16325                 indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
16326
16327                 if (indxinfo == NULL)
16328                         exit_horribly(NULL, "missing index for constraint \"%s\"\n",
16329                                                   coninfo->dobj.name);
16330
16331                 if (dopt->binary_upgrade)
16332                         binary_upgrade_set_pg_class_oids(fout, q,
16333                                                                                          indxinfo->dobj.catId.oid, true);
16334
16335                 appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
16336                                                   fmtQualifiedDumpable(tbinfo));
16337                 appendPQExpBuffer(q, "    ADD CONSTRAINT %s ",
16338                                                   fmtId(coninfo->dobj.name));
16339
16340                 if (coninfo->condef)
16341                 {
16342                         /* pg_get_constraintdef should have provided everything */
16343                         appendPQExpBuffer(q, "%s;\n", coninfo->condef);
16344                 }
16345                 else
16346                 {
16347                         appendPQExpBuffer(q, "%s (",
16348                                                           coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
16349                         for (k = 0; k < indxinfo->indnkeyattrs; k++)
16350                         {
16351                                 int                     indkey = (int) indxinfo->indkeys[k];
16352                                 const char *attname;
16353
16354                                 if (indkey == InvalidAttrNumber)
16355                                         break;
16356                                 attname = getAttrName(indkey, tbinfo);
16357
16358                                 appendPQExpBuffer(q, "%s%s",
16359                                                                   (k == 0) ? "" : ", ",
16360                                                                   fmtId(attname));
16361                         }
16362
16363                         if (indxinfo->indnkeyattrs < indxinfo->indnattrs)
16364                                 appendPQExpBuffer(q, ") INCLUDE (");
16365
16366                         for (k = indxinfo->indnkeyattrs; k < indxinfo->indnattrs; k++)
16367                         {
16368                                 int                     indkey = (int) indxinfo->indkeys[k];
16369                                 const char *attname;
16370
16371                                 if (indkey == InvalidAttrNumber)
16372                                         break;
16373                                 attname = getAttrName(indkey, tbinfo);
16374
16375                                 appendPQExpBuffer(q, "%s%s",
16376                                                                   (k == indxinfo->indnkeyattrs) ? "" : ", ",
16377                                                                   fmtId(attname));
16378                         }
16379
16380                         appendPQExpBufferChar(q, ')');
16381
16382                         if (nonemptyReloptions(indxinfo->indreloptions))
16383                         {
16384                                 appendPQExpBufferStr(q, " WITH (");
16385                                 appendReloptionsArrayAH(q, indxinfo->indreloptions, "", fout);
16386                                 appendPQExpBufferChar(q, ')');
16387                         }
16388
16389                         if (coninfo->condeferrable)
16390                         {
16391                                 appendPQExpBufferStr(q, " DEFERRABLE");
16392                                 if (coninfo->condeferred)
16393                                         appendPQExpBufferStr(q, " INITIALLY DEFERRED");
16394                         }
16395
16396                         appendPQExpBufferStr(q, ";\n");
16397                 }
16398
16399                 /* If the index is clustered, we need to record that. */
16400                 if (indxinfo->indisclustered)
16401                 {
16402                         appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
16403                                                           fmtQualifiedDumpable(tbinfo));
16404                         /* index name is not qualified in this syntax */
16405                         appendPQExpBuffer(q, " ON %s;\n",
16406                                                           fmtId(indxinfo->dobj.name));
16407                 }
16408
16409                 appendPQExpBuffer(delq, "ALTER TABLE ONLY %s ",
16410                                                   fmtQualifiedDumpable(tbinfo));
16411                 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16412                                                   fmtId(coninfo->dobj.name));
16413
16414                 tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16415
16416                 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16417                         ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16418                                                  tag,
16419                                                  tbinfo->dobj.namespace->dobj.name,
16420                                                  indxinfo->tablespace,
16421                                                  tbinfo->rolname, false,
16422                                                  "CONSTRAINT", SECTION_POST_DATA,
16423                                                  q->data, delq->data, NULL,
16424                                                  NULL, 0,
16425                                                  NULL, NULL);
16426         }
16427         else if (coninfo->contype == 'f')
16428         {
16429                 char       *only;
16430
16431                 /*
16432                  * Foreign keys on partitioned tables are always declared as
16433                  * inheriting to partitions; for all other cases, emit them as
16434                  * applying ONLY directly to the named table, because that's how they
16435                  * work for regular inherited tables.
16436                  */
16437                 only = tbinfo->relkind == RELKIND_PARTITIONED_TABLE ? "" : "ONLY ";
16438
16439                 /*
16440                  * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
16441                  * current table data is not processed
16442                  */
16443                 appendPQExpBuffer(q, "ALTER TABLE %s%s\n",
16444                                                   only, fmtQualifiedDumpable(tbinfo));
16445                 appendPQExpBuffer(q, "    ADD CONSTRAINT %s %s;\n",
16446                                                   fmtId(coninfo->dobj.name),
16447                                                   coninfo->condef);
16448
16449                 appendPQExpBuffer(delq, "ALTER TABLE %s%s ",
16450                                                   only, fmtQualifiedDumpable(tbinfo));
16451                 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16452                                                   fmtId(coninfo->dobj.name));
16453
16454                 tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16455
16456                 if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16457                         ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16458                                                  tag,
16459                                                  tbinfo->dobj.namespace->dobj.name,
16460                                                  NULL,
16461                                                  tbinfo->rolname, false,
16462                                                  "FK CONSTRAINT", SECTION_POST_DATA,
16463                                                  q->data, delq->data, NULL,
16464                                                  NULL, 0,
16465                                                  NULL, NULL);
16466         }
16467         else if (coninfo->contype == 'c' && tbinfo)
16468         {
16469                 /* CHECK constraint on a table */
16470
16471                 /* Ignore if not to be dumped separately, or if it was inherited */
16472                 if (coninfo->separate && coninfo->conislocal)
16473                 {
16474                         /* not ONLY since we want it to propagate to children */
16475                         appendPQExpBuffer(q, "ALTER TABLE %s\n",
16476                                                           fmtQualifiedDumpable(tbinfo));
16477                         appendPQExpBuffer(q, "    ADD CONSTRAINT %s %s;\n",
16478                                                           fmtId(coninfo->dobj.name),
16479                                                           coninfo->condef);
16480
16481                         appendPQExpBuffer(delq, "ALTER TABLE %s ",
16482                                                           fmtQualifiedDumpable(tbinfo));
16483                         appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16484                                                           fmtId(coninfo->dobj.name));
16485
16486                         tag = psprintf("%s %s", tbinfo->dobj.name, coninfo->dobj.name);
16487
16488                         if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16489                                 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16490                                                          tag,
16491                                                          tbinfo->dobj.namespace->dobj.name,
16492                                                          NULL,
16493                                                          tbinfo->rolname, false,
16494                                                          "CHECK CONSTRAINT", SECTION_POST_DATA,
16495                                                          q->data, delq->data, NULL,
16496                                                          NULL, 0,
16497                                                          NULL, NULL);
16498                 }
16499         }
16500         else if (coninfo->contype == 'c' && tbinfo == NULL)
16501         {
16502                 /* CHECK constraint on a domain */
16503                 TypeInfo   *tyinfo = coninfo->condomain;
16504
16505                 /* Ignore if not to be dumped separately */
16506                 if (coninfo->separate)
16507                 {
16508                         appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
16509                                                           fmtQualifiedDumpable(tyinfo));
16510                         appendPQExpBuffer(q, "    ADD CONSTRAINT %s %s;\n",
16511                                                           fmtId(coninfo->dobj.name),
16512                                                           coninfo->condef);
16513
16514                         appendPQExpBuffer(delq, "ALTER DOMAIN %s ",
16515                                                           fmtQualifiedDumpable(tyinfo));
16516                         appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
16517                                                           fmtId(coninfo->dobj.name));
16518
16519                         tag = psprintf("%s %s", tyinfo->dobj.name, coninfo->dobj.name);
16520
16521                         if (coninfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16522                                 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
16523                                                          tag,
16524                                                          tyinfo->dobj.namespace->dobj.name,
16525                                                          NULL,
16526                                                          tyinfo->rolname, false,
16527                                                          "CHECK CONSTRAINT", SECTION_POST_DATA,
16528                                                          q->data, delq->data, NULL,
16529                                                          NULL, 0,
16530                                                          NULL, NULL);
16531                 }
16532         }
16533         else
16534         {
16535                 exit_horribly(NULL, "unrecognized constraint type: %c\n",
16536                                           coninfo->contype);
16537         }
16538
16539         /* Dump Constraint Comments --- only works for table constraints */
16540         if (tbinfo && coninfo->separate &&
16541                 coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16542                 dumpTableConstraintComment(fout, coninfo);
16543
16544         free(tag);
16545         destroyPQExpBuffer(q);
16546         destroyPQExpBuffer(delq);
16547 }
16548
16549 /*
16550  * dumpTableConstraintComment --- dump a constraint's comment if any
16551  *
16552  * This is split out because we need the function in two different places
16553  * depending on whether the constraint is dumped as part of CREATE TABLE
16554  * or as a separate ALTER command.
16555  */
16556 static void
16557 dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
16558 {
16559         TableInfo  *tbinfo = coninfo->contable;
16560         PQExpBuffer conprefix = createPQExpBuffer();
16561         char       *qtabname;
16562
16563         qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
16564
16565         appendPQExpBuffer(conprefix, "CONSTRAINT %s ON",
16566                                           fmtId(coninfo->dobj.name));
16567
16568         if (coninfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16569                 dumpComment(fout, conprefix->data, qtabname,
16570                                         tbinfo->dobj.namespace->dobj.name,
16571                                         tbinfo->rolname,
16572                                         coninfo->dobj.catId, 0,
16573                                         coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
16574
16575         destroyPQExpBuffer(conprefix);
16576         free(qtabname);
16577 }
16578
16579 /*
16580  * findLastBuiltinOid_V71 -
16581  *
16582  * find the last built in oid
16583  *
16584  * For 7.1 through 8.0, we do this by retrieving datlastsysoid from the
16585  * pg_database entry for the current database.  (Note: current_database()
16586  * requires 7.3; pg_dump requires 8.0 now.)
16587  */
16588 static Oid
16589 findLastBuiltinOid_V71(Archive *fout)
16590 {
16591         PGresult   *res;
16592         Oid                     last_oid;
16593
16594         res = ExecuteSqlQueryForSingleRow(fout,
16595                                                                           "SELECT datlastsysoid FROM pg_database WHERE datname = current_database()");
16596         last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
16597         PQclear(res);
16598
16599         return last_oid;
16600 }
16601
16602 /*
16603  * dumpSequence
16604  *        write the declaration (not data) of one user-defined sequence
16605  */
16606 static void
16607 dumpSequence(Archive *fout, TableInfo *tbinfo)
16608 {
16609         DumpOptions *dopt = fout->dopt;
16610         PGresult   *res;
16611         char       *startv,
16612                            *incby,
16613                            *maxv,
16614                            *minv,
16615                            *cache,
16616                            *seqtype;
16617         bool            cycled;
16618         bool            is_ascending;
16619         int64           default_minv,
16620                                 default_maxv;
16621         char            bufm[32],
16622                                 bufx[32];
16623         PQExpBuffer query = createPQExpBuffer();
16624         PQExpBuffer delqry = createPQExpBuffer();
16625         char       *qseqname;
16626
16627         qseqname = pg_strdup(fmtId(tbinfo->dobj.name));
16628
16629         if (fout->remoteVersion >= 100000)
16630         {
16631                 appendPQExpBuffer(query,
16632                                                   "SELECT format_type(seqtypid, NULL), "
16633                                                   "seqstart, seqincrement, "
16634                                                   "seqmax, seqmin, "
16635                                                   "seqcache, seqcycle "
16636                                                   "FROM pg_catalog.pg_sequence "
16637                                                   "WHERE seqrelid = '%u'::oid",
16638                                                   tbinfo->dobj.catId.oid);
16639         }
16640         else if (fout->remoteVersion >= 80400)
16641         {
16642                 /*
16643                  * Before PostgreSQL 10, sequence metadata is in the sequence itself.
16644                  *
16645                  * Note: it might seem that 'bigint' potentially needs to be
16646                  * schema-qualified, but actually that's a keyword.
16647                  */
16648                 appendPQExpBuffer(query,
16649                                                   "SELECT 'bigint' AS sequence_type, "
16650                                                   "start_value, increment_by, max_value, min_value, "
16651                                                   "cache_value, is_cycled FROM %s",
16652                                                   fmtQualifiedDumpable(tbinfo));
16653         }
16654         else
16655         {
16656                 appendPQExpBuffer(query,
16657                                                   "SELECT 'bigint' AS sequence_type, "
16658                                                   "0 AS start_value, increment_by, max_value, min_value, "
16659                                                   "cache_value, is_cycled FROM %s",
16660                                                   fmtQualifiedDumpable(tbinfo));
16661         }
16662
16663         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16664
16665         if (PQntuples(res) != 1)
16666         {
16667                 write_msg(NULL, ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)\n",
16668                                                                  "query to get data of sequence \"%s\" returned %d rows (expected 1)\n",
16669                                                                  PQntuples(res)),
16670                                   tbinfo->dobj.name, PQntuples(res));
16671                 exit_nicely(1);
16672         }
16673
16674         seqtype = PQgetvalue(res, 0, 0);
16675         startv = PQgetvalue(res, 0, 1);
16676         incby = PQgetvalue(res, 0, 2);
16677         maxv = PQgetvalue(res, 0, 3);
16678         minv = PQgetvalue(res, 0, 4);
16679         cache = PQgetvalue(res, 0, 5);
16680         cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
16681
16682         /* Calculate default limits for a sequence of this type */
16683         is_ascending = (incby[0] != '-');
16684         if (strcmp(seqtype, "smallint") == 0)
16685         {
16686                 default_minv = is_ascending ? 1 : PG_INT16_MIN;
16687                 default_maxv = is_ascending ? PG_INT16_MAX : -1;
16688         }
16689         else if (strcmp(seqtype, "integer") == 0)
16690         {
16691                 default_minv = is_ascending ? 1 : PG_INT32_MIN;
16692                 default_maxv = is_ascending ? PG_INT32_MAX : -1;
16693         }
16694         else if (strcmp(seqtype, "bigint") == 0)
16695         {
16696                 default_minv = is_ascending ? 1 : PG_INT64_MIN;
16697                 default_maxv = is_ascending ? PG_INT64_MAX : -1;
16698         }
16699         else
16700         {
16701                 exit_horribly(NULL, "unrecognized sequence type: %s\n", seqtype);
16702                 default_minv = default_maxv = 0;        /* keep compiler quiet */
16703         }
16704
16705         /*
16706          * 64-bit strtol() isn't very portable, so convert the limits to strings
16707          * and compare that way.
16708          */
16709         snprintf(bufm, sizeof(bufm), INT64_FORMAT, default_minv);
16710         snprintf(bufx, sizeof(bufx), INT64_FORMAT, default_maxv);
16711
16712         /* Don't print minv/maxv if they match the respective default limit */
16713         if (strcmp(minv, bufm) == 0)
16714                 minv = NULL;
16715         if (strcmp(maxv, bufx) == 0)
16716                 maxv = NULL;
16717
16718         /*
16719          * Identity sequences are not to be dropped separately.
16720          */
16721         if (!tbinfo->is_identity_sequence)
16722         {
16723                 appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n",
16724                                                   fmtQualifiedDumpable(tbinfo));
16725         }
16726
16727         resetPQExpBuffer(query);
16728
16729         if (dopt->binary_upgrade)
16730         {
16731                 binary_upgrade_set_pg_class_oids(fout, query,
16732                                                                                  tbinfo->dobj.catId.oid, false);
16733                 binary_upgrade_set_type_oids_by_rel_oid(fout, query,
16734                                                                                                 tbinfo->dobj.catId.oid);
16735         }
16736
16737         if (tbinfo->is_identity_sequence)
16738         {
16739                 TableInfo  *owning_tab = findTableByOid(tbinfo->owning_tab);
16740
16741                 appendPQExpBuffer(query,
16742                                                   "ALTER TABLE %s ",
16743                                                   fmtQualifiedDumpable(owning_tab));
16744                 appendPQExpBuffer(query,
16745                                                   "ALTER COLUMN %s ADD GENERATED ",
16746                                                   fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
16747                 if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_ALWAYS)
16748                         appendPQExpBuffer(query, "ALWAYS");
16749                 else if (owning_tab->attidentity[tbinfo->owning_col - 1] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
16750                         appendPQExpBuffer(query, "BY DEFAULT");
16751                 appendPQExpBuffer(query, " AS IDENTITY (\n    SEQUENCE NAME %s\n",
16752                                                   fmtQualifiedDumpable(tbinfo));
16753         }
16754         else
16755         {
16756                 appendPQExpBuffer(query,
16757                                                   "CREATE SEQUENCE %s\n",
16758                                                   fmtQualifiedDumpable(tbinfo));
16759
16760                 if (strcmp(seqtype, "bigint") != 0)
16761                         appendPQExpBuffer(query, "    AS %s\n", seqtype);
16762         }
16763
16764         if (fout->remoteVersion >= 80400)
16765                 appendPQExpBuffer(query, "    START WITH %s\n", startv);
16766
16767         appendPQExpBuffer(query, "    INCREMENT BY %s\n", incby);
16768
16769         if (minv)
16770                 appendPQExpBuffer(query, "    MINVALUE %s\n", minv);
16771         else
16772                 appendPQExpBufferStr(query, "    NO MINVALUE\n");
16773
16774         if (maxv)
16775                 appendPQExpBuffer(query, "    MAXVALUE %s\n", maxv);
16776         else
16777                 appendPQExpBufferStr(query, "    NO MAXVALUE\n");
16778
16779         appendPQExpBuffer(query,
16780                                           "    CACHE %s%s",
16781                                           cache, (cycled ? "\n    CYCLE" : ""));
16782
16783         if (tbinfo->is_identity_sequence)
16784                 appendPQExpBufferStr(query, "\n);\n");
16785         else
16786                 appendPQExpBufferStr(query, ";\n");
16787
16788         /* binary_upgrade:      no need to clear TOAST table oid */
16789
16790         if (dopt->binary_upgrade)
16791                 binary_upgrade_extension_member(query, &tbinfo->dobj,
16792                                                                                 "SEQUENCE", qseqname,
16793                                                                                 tbinfo->dobj.namespace->dobj.name);
16794
16795         if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16796                 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
16797                                          tbinfo->dobj.name,
16798                                          tbinfo->dobj.namespace->dobj.name,
16799                                          NULL,
16800                                          tbinfo->rolname,
16801                                          false, "SEQUENCE", SECTION_PRE_DATA,
16802                                          query->data, delqry->data, NULL,
16803                                          NULL, 0,
16804                                          NULL, NULL);
16805
16806         /*
16807          * If the sequence is owned by a table column, emit the ALTER for it as a
16808          * separate TOC entry immediately following the sequence's own entry. It's
16809          * OK to do this rather than using full sorting logic, because the
16810          * dependency that tells us it's owned will have forced the table to be
16811          * created first.  We can't just include the ALTER in the TOC entry
16812          * because it will fail if we haven't reassigned the sequence owner to
16813          * match the table's owner.
16814          *
16815          * We need not schema-qualify the table reference because both sequence
16816          * and table must be in the same schema.
16817          */
16818         if (OidIsValid(tbinfo->owning_tab) && !tbinfo->is_identity_sequence)
16819         {
16820                 TableInfo  *owning_tab = findTableByOid(tbinfo->owning_tab);
16821
16822                 if (owning_tab == NULL)
16823                         exit_horribly(NULL, "failed sanity check, parent table with OID %u of sequence with OID %u not found\n",
16824                                                   tbinfo->owning_tab, tbinfo->dobj.catId.oid);
16825
16826                 if (owning_tab->dobj.dump & DUMP_COMPONENT_DEFINITION)
16827                 {
16828                         resetPQExpBuffer(query);
16829                         appendPQExpBuffer(query, "ALTER SEQUENCE %s",
16830                                                           fmtQualifiedDumpable(tbinfo));
16831                         appendPQExpBuffer(query, " OWNED BY %s",
16832                                                           fmtQualifiedDumpable(owning_tab));
16833                         appendPQExpBuffer(query, ".%s;\n",
16834                                                           fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
16835
16836                         if (tbinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
16837                                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
16838                                                          tbinfo->dobj.name,
16839                                                          tbinfo->dobj.namespace->dobj.name,
16840                                                          NULL,
16841                                                          tbinfo->rolname,
16842                                                          false, "SEQUENCE OWNED BY", SECTION_PRE_DATA,
16843                                                          query->data, "", NULL,
16844                                                          &(tbinfo->dobj.dumpId), 1,
16845                                                          NULL, NULL);
16846                 }
16847         }
16848
16849         /* Dump Sequence Comments and Security Labels */
16850         if (tbinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
16851                 dumpComment(fout, "SEQUENCE", qseqname,
16852                                         tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
16853                                         tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
16854
16855         if (tbinfo->dobj.dump & DUMP_COMPONENT_SECLABEL)
16856                 dumpSecLabel(fout, "SEQUENCE", qseqname,
16857                                          tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
16858                                          tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
16859
16860         PQclear(res);
16861
16862         destroyPQExpBuffer(query);
16863         destroyPQExpBuffer(delqry);
16864         free(qseqname);
16865 }
16866
16867 /*
16868  * dumpSequenceData
16869  *        write the data of one user-defined sequence
16870  */
16871 static void
16872 dumpSequenceData(Archive *fout, TableDataInfo *tdinfo)
16873 {
16874         TableInfo  *tbinfo = tdinfo->tdtable;
16875         PGresult   *res;
16876         char       *last;
16877         bool            called;
16878         PQExpBuffer query = createPQExpBuffer();
16879
16880         appendPQExpBuffer(query,
16881                                           "SELECT last_value, is_called FROM %s",
16882                                           fmtQualifiedDumpable(tbinfo));
16883
16884         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
16885
16886         if (PQntuples(res) != 1)
16887         {
16888                 write_msg(NULL, ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)\n",
16889                                                                  "query to get data of sequence \"%s\" returned %d rows (expected 1)\n",
16890                                                                  PQntuples(res)),
16891                                   tbinfo->dobj.name, PQntuples(res));
16892                 exit_nicely(1);
16893         }
16894
16895         last = PQgetvalue(res, 0, 0);
16896         called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
16897
16898         resetPQExpBuffer(query);
16899         appendPQExpBufferStr(query, "SELECT pg_catalog.setval(");
16900         appendStringLiteralAH(query, fmtQualifiedDumpable(tbinfo), fout);
16901         appendPQExpBuffer(query, ", %s, %s);\n",
16902                                           last, (called ? "true" : "false"));
16903
16904         if (tdinfo->dobj.dump & DUMP_COMPONENT_DATA)
16905                 ArchiveEntry(fout, nilCatalogId, createDumpId(),
16906                                          tbinfo->dobj.name,
16907                                          tbinfo->dobj.namespace->dobj.name,
16908                                          NULL,
16909                                          tbinfo->rolname,
16910                                          false, "SEQUENCE SET", SECTION_DATA,
16911                                          query->data, "", NULL,
16912                                          &(tbinfo->dobj.dumpId), 1,
16913                                          NULL, NULL);
16914
16915         PQclear(res);
16916
16917         destroyPQExpBuffer(query);
16918 }
16919
16920 /*
16921  * dumpTrigger
16922  *        write the declaration of one user-defined table trigger
16923  */
16924 static void
16925 dumpTrigger(Archive *fout, TriggerInfo *tginfo)
16926 {
16927         DumpOptions *dopt = fout->dopt;
16928         TableInfo  *tbinfo = tginfo->tgtable;
16929         PQExpBuffer query;
16930         PQExpBuffer delqry;
16931         PQExpBuffer trigprefix;
16932         char       *qtabname;
16933         char       *tgargs;
16934         size_t          lentgargs;
16935         const char *p;
16936         int                     findx;
16937         char       *tag;
16938
16939         /*
16940          * we needn't check dobj.dump because TriggerInfo wouldn't have been
16941          * created in the first place for non-dumpable triggers
16942          */
16943         if (dopt->dataOnly)
16944                 return;
16945
16946         query = createPQExpBuffer();
16947         delqry = createPQExpBuffer();
16948         trigprefix = createPQExpBuffer();
16949
16950         qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
16951
16952         appendPQExpBuffer(delqry, "DROP TRIGGER %s ",
16953                                           fmtId(tginfo->dobj.name));
16954         appendPQExpBuffer(delqry, "ON %s;\n",
16955                                           fmtQualifiedDumpable(tbinfo));
16956
16957         if (tginfo->tgdef)
16958         {
16959                 appendPQExpBuffer(query, "%s;\n", tginfo->tgdef);
16960         }
16961         else
16962         {
16963                 if (tginfo->tgisconstraint)
16964                 {
16965                         appendPQExpBufferStr(query, "CREATE CONSTRAINT TRIGGER ");
16966                         appendPQExpBufferStr(query, fmtId(tginfo->tgconstrname));
16967                 }
16968                 else
16969                 {
16970                         appendPQExpBufferStr(query, "CREATE TRIGGER ");
16971                         appendPQExpBufferStr(query, fmtId(tginfo->dobj.name));
16972                 }
16973                 appendPQExpBufferStr(query, "\n    ");
16974
16975                 /* Trigger type */
16976                 if (TRIGGER_FOR_BEFORE(tginfo->tgtype))
16977                         appendPQExpBufferStr(query, "BEFORE");
16978                 else if (TRIGGER_FOR_AFTER(tginfo->tgtype))
16979                         appendPQExpBufferStr(query, "AFTER");
16980                 else if (TRIGGER_FOR_INSTEAD(tginfo->tgtype))
16981                         appendPQExpBufferStr(query, "INSTEAD OF");
16982                 else
16983                 {
16984                         write_msg(NULL, "unexpected tgtype value: %d\n", tginfo->tgtype);
16985                         exit_nicely(1);
16986                 }
16987
16988                 findx = 0;
16989                 if (TRIGGER_FOR_INSERT(tginfo->tgtype))
16990                 {
16991                         appendPQExpBufferStr(query, " INSERT");
16992                         findx++;
16993                 }
16994                 if (TRIGGER_FOR_DELETE(tginfo->tgtype))
16995                 {
16996                         if (findx > 0)
16997                                 appendPQExpBufferStr(query, " OR DELETE");
16998                         else
16999                                 appendPQExpBufferStr(query, " DELETE");
17000                         findx++;
17001                 }
17002                 if (TRIGGER_FOR_UPDATE(tginfo->tgtype))
17003                 {
17004                         if (findx > 0)
17005                                 appendPQExpBufferStr(query, " OR UPDATE");
17006                         else
17007                                 appendPQExpBufferStr(query, " UPDATE");
17008                         findx++;
17009                 }
17010                 if (TRIGGER_FOR_TRUNCATE(tginfo->tgtype))
17011                 {
17012                         if (findx > 0)
17013                                 appendPQExpBufferStr(query, " OR TRUNCATE");
17014                         else
17015                                 appendPQExpBufferStr(query, " TRUNCATE");
17016                         findx++;
17017                 }
17018                 appendPQExpBuffer(query, " ON %s\n",
17019                                                   fmtQualifiedDumpable(tbinfo));
17020
17021                 if (tginfo->tgisconstraint)
17022                 {
17023                         if (OidIsValid(tginfo->tgconstrrelid))
17024                         {
17025                                 /* regclass output is already quoted */
17026                                 appendPQExpBuffer(query, "    FROM %s\n    ",
17027                                                                   tginfo->tgconstrrelname);
17028                         }
17029                         if (!tginfo->tgdeferrable)
17030                                 appendPQExpBufferStr(query, "NOT ");
17031                         appendPQExpBufferStr(query, "DEFERRABLE INITIALLY ");
17032                         if (tginfo->tginitdeferred)
17033                                 appendPQExpBufferStr(query, "DEFERRED\n");
17034                         else
17035                                 appendPQExpBufferStr(query, "IMMEDIATE\n");
17036                 }
17037
17038                 if (TRIGGER_FOR_ROW(tginfo->tgtype))
17039                         appendPQExpBufferStr(query, "    FOR EACH ROW\n    ");
17040                 else
17041                         appendPQExpBufferStr(query, "    FOR EACH STATEMENT\n    ");
17042
17043                 /* regproc output is already sufficiently quoted */
17044                 appendPQExpBuffer(query, "EXECUTE PROCEDURE %s(",
17045                                                   tginfo->tgfname);
17046
17047                 tgargs = (char *) PQunescapeBytea((unsigned char *) tginfo->tgargs,
17048                                                                                   &lentgargs);
17049                 p = tgargs;
17050                 for (findx = 0; findx < tginfo->tgnargs; findx++)
17051                 {
17052                         /* find the embedded null that terminates this trigger argument */
17053                         size_t          tlen = strlen(p);
17054
17055                         if (p + tlen >= tgargs + lentgargs)
17056                         {
17057                                 /* hm, not found before end of bytea value... */
17058                                 write_msg(NULL, "invalid argument string (%s) for trigger \"%s\" on table \"%s\"\n",
17059                                                   tginfo->tgargs,
17060                                                   tginfo->dobj.name,
17061                                                   tbinfo->dobj.name);
17062                                 exit_nicely(1);
17063                         }
17064
17065                         if (findx > 0)
17066                                 appendPQExpBufferStr(query, ", ");
17067                         appendStringLiteralAH(query, p, fout);
17068                         p += tlen + 1;
17069                 }
17070                 free(tgargs);
17071                 appendPQExpBufferStr(query, ");\n");
17072         }
17073
17074         if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
17075         {
17076                 appendPQExpBuffer(query, "\nALTER TABLE %s ",
17077                                                   fmtQualifiedDumpable(tbinfo));
17078                 switch (tginfo->tgenabled)
17079                 {
17080                         case 'D':
17081                         case 'f':
17082                                 appendPQExpBufferStr(query, "DISABLE");
17083                                 break;
17084                         case 'A':
17085                                 appendPQExpBufferStr(query, "ENABLE ALWAYS");
17086                                 break;
17087                         case 'R':
17088                                 appendPQExpBufferStr(query, "ENABLE REPLICA");
17089                                 break;
17090                         default:
17091                                 appendPQExpBufferStr(query, "ENABLE");
17092                                 break;
17093                 }
17094                 appendPQExpBuffer(query, " TRIGGER %s;\n",
17095                                                   fmtId(tginfo->dobj.name));
17096         }
17097
17098         appendPQExpBuffer(trigprefix, "TRIGGER %s ON",
17099                                           fmtId(tginfo->dobj.name));
17100
17101         tag = psprintf("%s %s", tbinfo->dobj.name, tginfo->dobj.name);
17102
17103         if (tginfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17104                 ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
17105                                          tag,
17106                                          tbinfo->dobj.namespace->dobj.name,
17107                                          NULL,
17108                                          tbinfo->rolname, false,
17109                                          "TRIGGER", SECTION_POST_DATA,
17110                                          query->data, delqry->data, NULL,
17111                                          NULL, 0,
17112                                          NULL, NULL);
17113
17114         if (tginfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17115                 dumpComment(fout, trigprefix->data, qtabname,
17116                                         tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
17117                                         tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
17118
17119         free(tag);
17120         destroyPQExpBuffer(query);
17121         destroyPQExpBuffer(delqry);
17122         destroyPQExpBuffer(trigprefix);
17123         free(qtabname);
17124 }
17125
17126 /*
17127  * dumpEventTrigger
17128  *        write the declaration of one user-defined event trigger
17129  */
17130 static void
17131 dumpEventTrigger(Archive *fout, EventTriggerInfo *evtinfo)
17132 {
17133         DumpOptions *dopt = fout->dopt;
17134         PQExpBuffer query;
17135         PQExpBuffer delqry;
17136         char       *qevtname;
17137
17138         /* Skip if not to be dumped */
17139         if (!evtinfo->dobj.dump || dopt->dataOnly)
17140                 return;
17141
17142         query = createPQExpBuffer();
17143         delqry = createPQExpBuffer();
17144
17145         qevtname = pg_strdup(fmtId(evtinfo->dobj.name));
17146
17147         appendPQExpBufferStr(query, "CREATE EVENT TRIGGER ");
17148         appendPQExpBufferStr(query, qevtname);
17149         appendPQExpBufferStr(query, " ON ");
17150         appendPQExpBufferStr(query, fmtId(evtinfo->evtevent));
17151
17152         if (strcmp("", evtinfo->evttags) != 0)
17153         {
17154                 appendPQExpBufferStr(query, "\n         WHEN TAG IN (");
17155                 appendPQExpBufferStr(query, evtinfo->evttags);
17156                 appendPQExpBufferChar(query, ')');
17157         }
17158
17159         appendPQExpBufferStr(query, "\n   EXECUTE PROCEDURE ");
17160         appendPQExpBufferStr(query, evtinfo->evtfname);
17161         appendPQExpBufferStr(query, "();\n");
17162
17163         if (evtinfo->evtenabled != 'O')
17164         {
17165                 appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ",
17166                                                   qevtname);
17167                 switch (evtinfo->evtenabled)
17168                 {
17169                         case 'D':
17170                                 appendPQExpBufferStr(query, "DISABLE");
17171                                 break;
17172                         case 'A':
17173                                 appendPQExpBufferStr(query, "ENABLE ALWAYS");
17174                                 break;
17175                         case 'R':
17176                                 appendPQExpBufferStr(query, "ENABLE REPLICA");
17177                                 break;
17178                         default:
17179                                 appendPQExpBufferStr(query, "ENABLE");
17180                                 break;
17181                 }
17182                 appendPQExpBufferStr(query, ";\n");
17183         }
17184
17185         appendPQExpBuffer(delqry, "DROP EVENT TRIGGER %s;\n",
17186                                           qevtname);
17187
17188         if (evtinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17189                 ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId,
17190                                          evtinfo->dobj.name, NULL, NULL,
17191                                          evtinfo->evtowner, false,
17192                                          "EVENT TRIGGER", SECTION_POST_DATA,
17193                                          query->data, delqry->data, NULL,
17194                                          NULL, 0,
17195                                          NULL, NULL);
17196
17197         if (evtinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17198                 dumpComment(fout, "EVENT TRIGGER", qevtname,
17199                                         NULL, evtinfo->evtowner,
17200                                         evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
17201
17202         destroyPQExpBuffer(query);
17203         destroyPQExpBuffer(delqry);
17204         free(qevtname);
17205 }
17206
17207 /*
17208  * dumpRule
17209  *              Dump a rule
17210  */
17211 static void
17212 dumpRule(Archive *fout, RuleInfo *rinfo)
17213 {
17214         DumpOptions *dopt = fout->dopt;
17215         TableInfo  *tbinfo = rinfo->ruletable;
17216         bool            is_view;
17217         PQExpBuffer query;
17218         PQExpBuffer cmd;
17219         PQExpBuffer delcmd;
17220         PQExpBuffer ruleprefix;
17221         char       *qtabname;
17222         PGresult   *res;
17223         char       *tag;
17224
17225         /* Skip if not to be dumped */
17226         if (!rinfo->dobj.dump || dopt->dataOnly)
17227                 return;
17228
17229         /*
17230          * If it is an ON SELECT rule that is created implicitly by CREATE VIEW,
17231          * we do not want to dump it as a separate object.
17232          */
17233         if (!rinfo->separate)
17234                 return;
17235
17236         /*
17237          * If it's an ON SELECT rule, we want to print it as a view definition,
17238          * instead of a rule.
17239          */
17240         is_view = (rinfo->ev_type == '1' && rinfo->is_instead);
17241
17242         query = createPQExpBuffer();
17243         cmd = createPQExpBuffer();
17244         delcmd = createPQExpBuffer();
17245         ruleprefix = createPQExpBuffer();
17246
17247         qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
17248
17249         if (is_view)
17250         {
17251                 PQExpBuffer result;
17252
17253                 /*
17254                  * We need OR REPLACE here because we'll be replacing a dummy view.
17255                  * Otherwise this should look largely like the regular view dump code.
17256                  */
17257                 appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s",
17258                                                   fmtQualifiedDumpable(tbinfo));
17259                 if (nonemptyReloptions(tbinfo->reloptions))
17260                 {
17261                         appendPQExpBufferStr(cmd, " WITH (");
17262                         appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout);
17263                         appendPQExpBufferChar(cmd, ')');
17264                 }
17265                 result = createViewAsClause(fout, tbinfo);
17266                 appendPQExpBuffer(cmd, " AS\n%s", result->data);
17267                 destroyPQExpBuffer(result);
17268                 if (tbinfo->checkoption != NULL)
17269                         appendPQExpBuffer(cmd, "\n  WITH %s CHECK OPTION",
17270                                                           tbinfo->checkoption);
17271                 appendPQExpBufferStr(cmd, ";\n");
17272         }
17273         else
17274         {
17275                 /* In the rule case, just print pg_get_ruledef's result verbatim */
17276                 appendPQExpBuffer(query,
17277                                                   "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)",
17278                                                   rinfo->dobj.catId.oid);
17279
17280                 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17281
17282                 if (PQntuples(res) != 1)
17283                 {
17284                         write_msg(NULL, "query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned\n",
17285                                           rinfo->dobj.name, tbinfo->dobj.name);
17286                         exit_nicely(1);
17287                 }
17288
17289                 printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
17290
17291                 PQclear(res);
17292         }
17293
17294         /*
17295          * Add the command to alter the rules replication firing semantics if it
17296          * differs from the default.
17297          */
17298         if (rinfo->ev_enabled != 'O')
17299         {
17300                 appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtQualifiedDumpable(tbinfo));
17301                 switch (rinfo->ev_enabled)
17302                 {
17303                         case 'A':
17304                                 appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n",
17305                                                                   fmtId(rinfo->dobj.name));
17306                                 break;
17307                         case 'R':
17308                                 appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n",
17309                                                                   fmtId(rinfo->dobj.name));
17310                                 break;
17311                         case 'D':
17312                                 appendPQExpBuffer(cmd, "DISABLE RULE %s;\n",
17313                                                                   fmtId(rinfo->dobj.name));
17314                                 break;
17315                 }
17316         }
17317
17318         if (is_view)
17319         {
17320                 /*
17321                  * We can't DROP a view's ON SELECT rule.  Instead, use CREATE OR
17322                  * REPLACE VIEW to replace the rule with something with minimal
17323                  * dependencies.
17324                  */
17325                 PQExpBuffer result;
17326
17327                 appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s",
17328                                                   fmtQualifiedDumpable(tbinfo));
17329                 result = createDummyViewAsClause(fout, tbinfo);
17330                 appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data);
17331                 destroyPQExpBuffer(result);
17332         }
17333         else
17334         {
17335                 appendPQExpBuffer(delcmd, "DROP RULE %s ",
17336                                                   fmtId(rinfo->dobj.name));
17337                 appendPQExpBuffer(delcmd, "ON %s;\n",
17338                                                   fmtQualifiedDumpable(tbinfo));
17339         }
17340
17341         appendPQExpBuffer(ruleprefix, "RULE %s ON",
17342                                           fmtId(rinfo->dobj.name));
17343
17344         tag = psprintf("%s %s", tbinfo->dobj.name, rinfo->dobj.name);
17345
17346         if (rinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
17347                 ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
17348                                          tag,
17349                                          tbinfo->dobj.namespace->dobj.name,
17350                                          NULL,
17351                                          tbinfo->rolname, false,
17352                                          "RULE", SECTION_POST_DATA,
17353                                          cmd->data, delcmd->data, NULL,
17354                                          NULL, 0,
17355                                          NULL, NULL);
17356
17357         /* Dump rule comments */
17358         if (rinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
17359                 dumpComment(fout, ruleprefix->data, qtabname,
17360                                         tbinfo->dobj.namespace->dobj.name,
17361                                         tbinfo->rolname,
17362                                         rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
17363
17364         free(tag);
17365         destroyPQExpBuffer(query);
17366         destroyPQExpBuffer(cmd);
17367         destroyPQExpBuffer(delcmd);
17368         destroyPQExpBuffer(ruleprefix);
17369         free(qtabname);
17370 }
17371
17372 /*
17373  * getExtensionMembership --- obtain extension membership data
17374  *
17375  * We need to identify objects that are extension members as soon as they're
17376  * loaded, so that we can correctly determine whether they need to be dumped.
17377  * Generally speaking, extension member objects will get marked as *not* to
17378  * be dumped, as they will be recreated by the single CREATE EXTENSION
17379  * command.  However, in binary upgrade mode we still need to dump the members
17380  * individually.
17381  */
17382 void
17383 getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
17384                                            int numExtensions)
17385 {
17386         PQExpBuffer query;
17387         PGresult   *res;
17388         int                     ntups,
17389                                 nextmembers,
17390                                 i;
17391         int                     i_classid,
17392                                 i_objid,
17393                                 i_refobjid;
17394         ExtensionMemberId *extmembers;
17395         ExtensionInfo *ext;
17396
17397         /* Nothing to do if no extensions */
17398         if (numExtensions == 0)
17399                 return;
17400
17401         query = createPQExpBuffer();
17402
17403         /* refclassid constraint is redundant but may speed the search */
17404         appendPQExpBufferStr(query, "SELECT "
17405                                                  "classid, objid, refobjid "
17406                                                  "FROM pg_depend "
17407                                                  "WHERE refclassid = 'pg_extension'::regclass "
17408                                                  "AND deptype = 'e' "
17409                                                  "ORDER BY 3");
17410
17411         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17412
17413         ntups = PQntuples(res);
17414
17415         i_classid = PQfnumber(res, "classid");
17416         i_objid = PQfnumber(res, "objid");
17417         i_refobjid = PQfnumber(res, "refobjid");
17418
17419         extmembers = (ExtensionMemberId *) pg_malloc(ntups * sizeof(ExtensionMemberId));
17420         nextmembers = 0;
17421
17422         /*
17423          * Accumulate data into extmembers[].
17424          *
17425          * Since we ordered the SELECT by referenced ID, we can expect that
17426          * multiple entries for the same extension will appear together; this
17427          * saves on searches.
17428          */
17429         ext = NULL;
17430
17431         for (i = 0; i < ntups; i++)
17432         {
17433                 CatalogId       objId;
17434                 Oid                     extId;
17435
17436                 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
17437                 objId.oid = atooid(PQgetvalue(res, i, i_objid));
17438                 extId = atooid(PQgetvalue(res, i, i_refobjid));
17439
17440                 if (ext == NULL ||
17441                         ext->dobj.catId.oid != extId)
17442                         ext = findExtensionByOid(extId);
17443
17444                 if (ext == NULL)
17445                 {
17446                         /* shouldn't happen */
17447                         fprintf(stderr, "could not find referenced extension %u\n", extId);
17448                         continue;
17449                 }
17450
17451                 extmembers[nextmembers].catId = objId;
17452                 extmembers[nextmembers].ext = ext;
17453                 nextmembers++;
17454         }
17455
17456         PQclear(res);
17457
17458         /* Remember the data for use later */
17459         setExtensionMembership(extmembers, nextmembers);
17460
17461         destroyPQExpBuffer(query);
17462 }
17463
17464 /*
17465  * processExtensionTables --- deal with extension configuration tables
17466  *
17467  * There are two parts to this process:
17468  *
17469  * 1. Identify and create dump records for extension configuration tables.
17470  *
17471  *        Extensions can mark tables as "configuration", which means that the user
17472  *        is able and expected to modify those tables after the extension has been
17473  *        loaded.  For these tables, we dump out only the data- the structure is
17474  *        expected to be handled at CREATE EXTENSION time, including any indexes or
17475  *        foreign keys, which brings us to-
17476  *
17477  * 2. Record FK dependencies between configuration tables.
17478  *
17479  *        Due to the FKs being created at CREATE EXTENSION time and therefore before
17480  *        the data is loaded, we have to work out what the best order for reloading
17481  *        the data is, to avoid FK violations when the tables are restored.  This is
17482  *        not perfect- we can't handle circular dependencies and if any exist they
17483  *        will cause an invalid dump to be produced (though at least all of the data
17484  *        is included for a user to manually restore).  This is currently documented
17485  *        but perhaps we can provide a better solution in the future.
17486  */
17487 void
17488 processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
17489                                            int numExtensions)
17490 {
17491         DumpOptions *dopt = fout->dopt;
17492         PQExpBuffer query;
17493         PGresult   *res;
17494         int                     ntups,
17495                                 i;
17496         int                     i_conrelid,
17497                                 i_confrelid;
17498
17499         /* Nothing to do if no extensions */
17500         if (numExtensions == 0)
17501                 return;
17502
17503         /*
17504          * Identify extension configuration tables and create TableDataInfo
17505          * objects for them, ensuring their data will be dumped even though the
17506          * tables themselves won't be.
17507          *
17508          * Note that we create TableDataInfo objects even in schemaOnly mode, ie,
17509          * user data in a configuration table is treated like schema data. This
17510          * seems appropriate since system data in a config table would get
17511          * reloaded by CREATE EXTENSION.
17512          */
17513         for (i = 0; i < numExtensions; i++)
17514         {
17515                 ExtensionInfo *curext = &(extinfo[i]);
17516                 char       *extconfig = curext->extconfig;
17517                 char       *extcondition = curext->extcondition;
17518                 char      **extconfigarray = NULL;
17519                 char      **extconditionarray = NULL;
17520                 int                     nconfigitems;
17521                 int                     nconditionitems;
17522
17523                 if (parsePGArray(extconfig, &extconfigarray, &nconfigitems) &&
17524                         parsePGArray(extcondition, &extconditionarray, &nconditionitems) &&
17525                         nconfigitems == nconditionitems)
17526                 {
17527                         int                     j;
17528
17529                         for (j = 0; j < nconfigitems; j++)
17530                         {
17531                                 TableInfo  *configtbl;
17532                                 Oid                     configtbloid = atooid(extconfigarray[j]);
17533                                 bool            dumpobj =
17534                                 curext->dobj.dump & DUMP_COMPONENT_DEFINITION;
17535
17536                                 configtbl = findTableByOid(configtbloid);
17537                                 if (configtbl == NULL)
17538                                         continue;
17539
17540                                 /*
17541                                  * Tables of not-to-be-dumped extensions shouldn't be dumped
17542                                  * unless the table or its schema is explicitly included
17543                                  */
17544                                 if (!(curext->dobj.dump & DUMP_COMPONENT_DEFINITION))
17545                                 {
17546                                         /* check table explicitly requested */
17547                                         if (table_include_oids.head != NULL &&
17548                                                 simple_oid_list_member(&table_include_oids,
17549                                                                                            configtbloid))
17550                                                 dumpobj = true;
17551
17552                                         /* check table's schema explicitly requested */
17553                                         if (configtbl->dobj.namespace->dobj.dump &
17554                                                 DUMP_COMPONENT_DATA)
17555                                                 dumpobj = true;
17556                                 }
17557
17558                                 /* check table excluded by an exclusion switch */
17559                                 if (table_exclude_oids.head != NULL &&
17560                                         simple_oid_list_member(&table_exclude_oids,
17561                                                                                    configtbloid))
17562                                         dumpobj = false;
17563
17564                                 /* check schema excluded by an exclusion switch */
17565                                 if (simple_oid_list_member(&schema_exclude_oids,
17566                                                                                    configtbl->dobj.namespace->dobj.catId.oid))
17567                                         dumpobj = false;
17568
17569                                 if (dumpobj)
17570                                 {
17571                                         /*
17572                                          * Note: config tables are dumped without OIDs regardless
17573                                          * of the --oids setting.  This is because row filtering
17574                                          * conditions aren't compatible with dumping OIDs.
17575                                          */
17576                                         makeTableDataInfo(dopt, configtbl, false);
17577                                         if (configtbl->dataObj != NULL)
17578                                         {
17579                                                 if (strlen(extconditionarray[j]) > 0)
17580                                                         configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
17581                                         }
17582                                 }
17583                         }
17584                 }
17585                 if (extconfigarray)
17586                         free(extconfigarray);
17587                 if (extconditionarray)
17588                         free(extconditionarray);
17589         }
17590
17591         /*
17592          * Now that all the TableInfoData objects have been created for all the
17593          * extensions, check their FK dependencies and register them to try and
17594          * dump the data out in an order that they can be restored in.
17595          *
17596          * Note that this is not a problem for user tables as their FKs are
17597          * recreated after the data has been loaded.
17598          */
17599
17600         query = createPQExpBuffer();
17601
17602         printfPQExpBuffer(query,
17603                                           "SELECT conrelid, confrelid "
17604                                           "FROM pg_constraint "
17605                                           "JOIN pg_depend ON (objid = confrelid) "
17606                                           "WHERE contype = 'f' "
17607                                           "AND refclassid = 'pg_extension'::regclass "
17608                                           "AND classid = 'pg_class'::regclass;");
17609
17610         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17611         ntups = PQntuples(res);
17612
17613         i_conrelid = PQfnumber(res, "conrelid");
17614         i_confrelid = PQfnumber(res, "confrelid");
17615
17616         /* Now get the dependencies and register them */
17617         for (i = 0; i < ntups; i++)
17618         {
17619                 Oid                     conrelid,
17620                                         confrelid;
17621                 TableInfo  *reftable,
17622                                    *contable;
17623
17624                 conrelid = atooid(PQgetvalue(res, i, i_conrelid));
17625                 confrelid = atooid(PQgetvalue(res, i, i_confrelid));
17626                 contable = findTableByOid(conrelid);
17627                 reftable = findTableByOid(confrelid);
17628
17629                 if (reftable == NULL ||
17630                         reftable->dataObj == NULL ||
17631                         contable == NULL ||
17632                         contable->dataObj == NULL)
17633                         continue;
17634
17635                 /*
17636                  * Make referencing TABLE_DATA object depend on the referenced table's
17637                  * TABLE_DATA object.
17638                  */
17639                 addObjectDependency(&contable->dataObj->dobj,
17640                                                         reftable->dataObj->dobj.dumpId);
17641         }
17642         PQclear(res);
17643         destroyPQExpBuffer(query);
17644 }
17645
17646 /*
17647  * getDependencies --- obtain available dependency data
17648  */
17649 static void
17650 getDependencies(Archive *fout)
17651 {
17652         PQExpBuffer query;
17653         PGresult   *res;
17654         int                     ntups,
17655                                 i;
17656         int                     i_classid,
17657                                 i_objid,
17658                                 i_refclassid,
17659                                 i_refobjid,
17660                                 i_deptype;
17661         DumpableObject *dobj,
17662                            *refdobj;
17663
17664         if (g_verbose)
17665                 write_msg(NULL, "reading dependency data\n");
17666
17667         query = createPQExpBuffer();
17668
17669         /*
17670          * PIN dependencies aren't interesting, and EXTENSION dependencies were
17671          * already processed by getExtensionMembership.
17672          */
17673         appendPQExpBufferStr(query, "SELECT "
17674                                                  "classid, objid, refclassid, refobjid, deptype "
17675                                                  "FROM pg_depend "
17676                                                  "WHERE deptype != 'p' AND deptype != 'e' "
17677                                                  "ORDER BY 1,2");
17678
17679         res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
17680
17681         ntups = PQntuples(res);
17682
17683         i_classid = PQfnumber(res, "classid");
17684         i_objid = PQfnumber(res, "objid");
17685         i_refclassid = PQfnumber(res, "refclassid");
17686         i_refobjid = PQfnumber(res, "refobjid");
17687         i_deptype = PQfnumber(res, "deptype");
17688
17689         /*
17690          * Since we ordered the SELECT by referencing ID, we can expect that
17691          * multiple entries for the same object will appear together; this saves
17692          * on searches.
17693          */
17694         dobj = NULL;
17695
17696         for (i = 0; i < ntups; i++)
17697         {
17698                 CatalogId       objId;
17699                 CatalogId       refobjId;
17700                 char            deptype;
17701
17702                 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
17703                 objId.oid = atooid(PQgetvalue(res, i, i_objid));
17704                 refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
17705                 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
17706                 deptype = *(PQgetvalue(res, i, i_deptype));
17707
17708                 if (dobj == NULL ||
17709                         dobj->catId.tableoid != objId.tableoid ||
17710                         dobj->catId.oid != objId.oid)
17711                         dobj = findObjectByCatalogId(objId);
17712
17713                 /*
17714                  * Failure to find objects mentioned in pg_depend is not unexpected,
17715                  * since for example we don't collect info about TOAST tables.
17716                  */
17717                 if (dobj == NULL)
17718                 {
17719 #ifdef NOT_USED
17720                         fprintf(stderr, "no referencing object %u %u\n",
17721                                         objId.tableoid, objId.oid);
17722 #endif
17723                         continue;
17724                 }
17725
17726                 refdobj = findObjectByCatalogId(refobjId);
17727
17728                 if (refdobj == NULL)
17729                 {
17730 #ifdef NOT_USED
17731                         fprintf(stderr, "no referenced object %u %u\n",
17732                                         refobjId.tableoid, refobjId.oid);
17733 #endif
17734                         continue;
17735                 }
17736
17737                 /*
17738                  * Ordinarily, table rowtypes have implicit dependencies on their
17739                  * tables.  However, for a composite type the implicit dependency goes
17740                  * the other way in pg_depend; which is the right thing for DROP but
17741                  * it doesn't produce the dependency ordering we need. So in that one
17742                  * case, we reverse the direction of the dependency.
17743                  */
17744                 if (deptype == 'i' &&
17745                         dobj->objType == DO_TABLE &&
17746                         refdobj->objType == DO_TYPE)
17747                         addObjectDependency(refdobj, dobj->dumpId);
17748                 else
17749                         /* normal case */
17750                         addObjectDependency(dobj, refdobj->dumpId);
17751         }
17752
17753         PQclear(res);
17754
17755         destroyPQExpBuffer(query);
17756 }
17757
17758
17759 /*
17760  * createBoundaryObjects - create dummy DumpableObjects to represent
17761  * dump section boundaries.
17762  */
17763 static DumpableObject *
17764 createBoundaryObjects(void)
17765 {
17766         DumpableObject *dobjs;
17767
17768         dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
17769
17770         dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
17771         dobjs[0].catId = nilCatalogId;
17772         AssignDumpId(dobjs + 0);
17773         dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
17774
17775         dobjs[1].objType = DO_POST_DATA_BOUNDARY;
17776         dobjs[1].catId = nilCatalogId;
17777         AssignDumpId(dobjs + 1);
17778         dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
17779
17780         return dobjs;
17781 }
17782
17783 /*
17784  * addBoundaryDependencies - add dependencies as needed to enforce the dump
17785  * section boundaries.
17786  */
17787 static void
17788 addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
17789                                                 DumpableObject *boundaryObjs)
17790 {
17791         DumpableObject *preDataBound = boundaryObjs + 0;
17792         DumpableObject *postDataBound = boundaryObjs + 1;
17793         int                     i;
17794
17795         for (i = 0; i < numObjs; i++)
17796         {
17797                 DumpableObject *dobj = dobjs[i];
17798
17799                 /*
17800                  * The classification of object types here must match the SECTION_xxx
17801                  * values assigned during subsequent ArchiveEntry calls!
17802                  */
17803                 switch (dobj->objType)
17804                 {
17805                         case DO_NAMESPACE:
17806                         case DO_EXTENSION:
17807                         case DO_TYPE:
17808                         case DO_SHELL_TYPE:
17809                         case DO_FUNC:
17810                         case DO_AGG:
17811                         case DO_OPERATOR:
17812                         case DO_ACCESS_METHOD:
17813                         case DO_OPCLASS:
17814                         case DO_OPFAMILY:
17815                         case DO_COLLATION:
17816                         case DO_CONVERSION:
17817                         case DO_TABLE:
17818                         case DO_ATTRDEF:
17819                         case DO_PROCLANG:
17820                         case DO_CAST:
17821                         case DO_DUMMY_TYPE:
17822                         case DO_TSPARSER:
17823                         case DO_TSDICT:
17824                         case DO_TSTEMPLATE:
17825                         case DO_TSCONFIG:
17826                         case DO_FDW:
17827                         case DO_FOREIGN_SERVER:
17828                         case DO_TRANSFORM:
17829                         case DO_BLOB:
17830                                 /* Pre-data objects: must come before the pre-data boundary */
17831                                 addObjectDependency(preDataBound, dobj->dumpId);
17832                                 break;
17833                         case DO_TABLE_DATA:
17834                         case DO_SEQUENCE_SET:
17835                         case DO_BLOB_DATA:
17836                                 /* Data objects: must come between the boundaries */
17837                                 addObjectDependency(dobj, preDataBound->dumpId);
17838                                 addObjectDependency(postDataBound, dobj->dumpId);
17839                                 break;
17840                         case DO_INDEX:
17841                         case DO_INDEX_ATTACH:
17842                         case DO_STATSEXT:
17843                         case DO_REFRESH_MATVIEW:
17844                         case DO_TRIGGER:
17845                         case DO_EVENT_TRIGGER:
17846                         case DO_DEFAULT_ACL:
17847                         case DO_POLICY:
17848                         case DO_PUBLICATION:
17849                         case DO_PUBLICATION_REL:
17850                         case DO_SUBSCRIPTION:
17851                                 /* Post-data objects: must come after the post-data boundary */
17852                                 addObjectDependency(dobj, postDataBound->dumpId);
17853                                 break;
17854                         case DO_RULE:
17855                                 /* Rules are post-data, but only if dumped separately */
17856                                 if (((RuleInfo *) dobj)->separate)
17857                                         addObjectDependency(dobj, postDataBound->dumpId);
17858                                 break;
17859                         case DO_CONSTRAINT:
17860                         case DO_FK_CONSTRAINT:
17861                                 /* Constraints are post-data, but only if dumped separately */
17862                                 if (((ConstraintInfo *) dobj)->separate)
17863                                         addObjectDependency(dobj, postDataBound->dumpId);
17864                                 break;
17865                         case DO_PRE_DATA_BOUNDARY:
17866                                 /* nothing to do */
17867                                 break;
17868                         case DO_POST_DATA_BOUNDARY:
17869                                 /* must come after the pre-data boundary */
17870                                 addObjectDependency(dobj, preDataBound->dumpId);
17871                                 break;
17872                 }
17873         }
17874 }
17875
17876
17877 /*
17878  * BuildArchiveDependencies - create dependency data for archive TOC entries
17879  *
17880  * The raw dependency data obtained by getDependencies() is not terribly
17881  * useful in an archive dump, because in many cases there are dependency
17882  * chains linking through objects that don't appear explicitly in the dump.
17883  * For example, a view will depend on its _RETURN rule while the _RETURN rule
17884  * will depend on other objects --- but the rule will not appear as a separate
17885  * object in the dump.  We need to adjust the view's dependencies to include
17886  * whatever the rule depends on that is included in the dump.
17887  *
17888  * Just to make things more complicated, there are also "special" dependencies
17889  * such as the dependency of a TABLE DATA item on its TABLE, which we must
17890  * not rearrange because pg_restore knows that TABLE DATA only depends on
17891  * its table.  In these cases we must leave the dependencies strictly as-is
17892  * even if they refer to not-to-be-dumped objects.
17893  *
17894  * To handle this, the convention is that "special" dependencies are created
17895  * during ArchiveEntry calls, and an archive TOC item that has any such
17896  * entries will not be touched here.  Otherwise, we recursively search the
17897  * DumpableObject data structures to build the correct dependencies for each
17898  * archive TOC item.
17899  */
17900 static void
17901 BuildArchiveDependencies(Archive *fout)
17902 {
17903         ArchiveHandle *AH = (ArchiveHandle *) fout;
17904         TocEntry   *te;
17905
17906         /* Scan all TOC entries in the archive */
17907         for (te = AH->toc->next; te != AH->toc; te = te->next)
17908         {
17909                 DumpableObject *dobj;
17910                 DumpId     *dependencies;
17911                 int                     nDeps;
17912                 int                     allocDeps;
17913
17914                 /* No need to process entries that will not be dumped */
17915                 if (te->reqs == 0)
17916                         continue;
17917                 /* Ignore entries that already have "special" dependencies */
17918                 if (te->nDeps > 0)
17919                         continue;
17920                 /* Otherwise, look up the item's original DumpableObject, if any */
17921                 dobj = findObjectByDumpId(te->dumpId);
17922                 if (dobj == NULL)
17923                         continue;
17924                 /* No work if it has no dependencies */
17925                 if (dobj->nDeps <= 0)
17926                         continue;
17927                 /* Set up work array */
17928                 allocDeps = 64;
17929                 dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
17930                 nDeps = 0;
17931                 /* Recursively find all dumpable dependencies */
17932                 findDumpableDependencies(AH, dobj,
17933                                                                  &dependencies, &nDeps, &allocDeps);
17934                 /* And save 'em ... */
17935                 if (nDeps > 0)
17936                 {
17937                         dependencies = (DumpId *) pg_realloc(dependencies,
17938                                                                                                  nDeps * sizeof(DumpId));
17939                         te->dependencies = dependencies;
17940                         te->nDeps = nDeps;
17941                 }
17942                 else
17943                         free(dependencies);
17944         }
17945 }
17946
17947 /* Recursive search subroutine for BuildArchiveDependencies */
17948 static void
17949 findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj,
17950                                                  DumpId **dependencies, int *nDeps, int *allocDeps)
17951 {
17952         int                     i;
17953
17954         /*
17955          * Ignore section boundary objects: if we search through them, we'll
17956          * report lots of bogus dependencies.
17957          */
17958         if (dobj->objType == DO_PRE_DATA_BOUNDARY ||
17959                 dobj->objType == DO_POST_DATA_BOUNDARY)
17960                 return;
17961
17962         for (i = 0; i < dobj->nDeps; i++)
17963         {
17964                 DumpId          depid = dobj->dependencies[i];
17965
17966                 if (TocIDRequired(AH, depid) != 0)
17967                 {
17968                         /* Object will be dumped, so just reference it as a dependency */
17969                         if (*nDeps >= *allocDeps)
17970                         {
17971                                 *allocDeps *= 2;
17972                                 *dependencies = (DumpId *) pg_realloc(*dependencies,
17973                                                                                                           *allocDeps * sizeof(DumpId));
17974                         }
17975                         (*dependencies)[*nDeps] = depid;
17976                         (*nDeps)++;
17977                 }
17978                 else
17979                 {
17980                         /*
17981                          * Object will not be dumped, so recursively consider its deps. We
17982                          * rely on the assumption that sortDumpableObjects already broke
17983                          * any dependency loops, else we might recurse infinitely.
17984                          */
17985                         DumpableObject *otherdobj = findObjectByDumpId(depid);
17986
17987                         if (otherdobj)
17988                                 findDumpableDependencies(AH, otherdobj,
17989                                                                                  dependencies, nDeps, allocDeps);
17990                 }
17991         }
17992 }
17993
17994
17995 /*
17996  * getFormattedTypeName - retrieve a nicely-formatted type name for the
17997  * given type OID.
17998  *
17999  * This does not guarantee to schema-qualify the output, so it should not
18000  * be used to create the target object name for CREATE or ALTER commands.
18001  *
18002  * TODO: there might be some value in caching the results.
18003  */
18004 static char *
18005 getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
18006 {
18007         char       *result;
18008         PQExpBuffer query;
18009         PGresult   *res;
18010
18011         if (oid == 0)
18012         {
18013                 if ((opts & zeroAsOpaque) != 0)
18014                         return pg_strdup(g_opaque_type);
18015                 else if ((opts & zeroAsAny) != 0)
18016                         return pg_strdup("'any'");
18017                 else if ((opts & zeroAsStar) != 0)
18018                         return pg_strdup("*");
18019                 else if ((opts & zeroAsNone) != 0)
18020                         return pg_strdup("NONE");
18021         }
18022
18023         query = createPQExpBuffer();
18024         appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
18025                                           oid);
18026
18027         res = ExecuteSqlQueryForSingleRow(fout, query->data);
18028
18029         /* result of format_type is already quoted */
18030         result = pg_strdup(PQgetvalue(res, 0, 0));
18031
18032         PQclear(res);
18033         destroyPQExpBuffer(query);
18034
18035         return result;
18036 }
18037
18038 /*
18039  * Return a column list clause for the given relation.
18040  *
18041  * Special case: if there are no undropped columns in the relation, return
18042  * "", not an invalid "()" column list.
18043  */
18044 static const char *
18045 fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer)
18046 {
18047         int                     numatts = ti->numatts;
18048         char      **attnames = ti->attnames;
18049         bool       *attisdropped = ti->attisdropped;
18050         bool            needComma;
18051         int                     i;
18052
18053         appendPQExpBufferChar(buffer, '(');
18054         needComma = false;
18055         for (i = 0; i < numatts; i++)
18056         {
18057                 if (attisdropped[i])
18058                         continue;
18059                 if (needComma)
18060                         appendPQExpBufferStr(buffer, ", ");
18061                 appendPQExpBufferStr(buffer, fmtId(attnames[i]));
18062                 needComma = true;
18063         }
18064
18065         if (!needComma)
18066                 return "";                              /* no undropped columns */
18067
18068         appendPQExpBufferChar(buffer, ')');
18069         return buffer->data;
18070 }
18071
18072 /*
18073  * Check if a reloptions array is nonempty.
18074  */
18075 static bool
18076 nonemptyReloptions(const char *reloptions)
18077 {
18078         /* Don't want to print it if it's just "{}" */
18079         return (reloptions != NULL && strlen(reloptions) > 2);
18080 }
18081
18082 /*
18083  * Format a reloptions array and append it to the given buffer.
18084  *
18085  * "prefix" is prepended to the option names; typically it's "" or "toast.".
18086  */
18087 static void
18088 appendReloptionsArrayAH(PQExpBuffer buffer, const char *reloptions,
18089                                                 const char *prefix, Archive *fout)
18090 {
18091         bool            res;
18092
18093         res = appendReloptionsArray(buffer, reloptions, prefix, fout->encoding,
18094                                                                 fout->std_strings);
18095         if (!res)
18096                 write_msg(NULL, "WARNING: could not parse reloptions array\n");
18097 }