1 /*-------------------------------------------------------------------------
4 * pg_dump is a utility for dumping out a postgres database
7 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
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
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,
25 * http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
28 * src/bin/pg_dump/pg_dump.c
30 *-------------------------------------------------------------------------
33 #include "postgres_fe.h"
44 #include "getopt_long.h"
46 #include "access/attnum.h"
47 #include "access/sysattr.h"
48 #include "access/transam.h"
49 #include "catalog/pg_cast.h"
50 #include "catalog/pg_class.h"
51 #include "catalog/pg_default_acl.h"
52 #include "catalog/pg_event_trigger.h"
53 #include "catalog/pg_largeobject.h"
54 #include "catalog/pg_largeobject_metadata.h"
55 #include "catalog/pg_proc.h"
56 #include "catalog/pg_trigger.h"
57 #include "catalog/pg_type.h"
58 #include "libpq/libpq-fs.h"
60 #include "pg_backup_archiver.h"
61 #include "pg_backup_db.h"
62 #include "pg_backup_utils.h"
63 #include "dumputils.h"
73 const char *descr; /* comment for an object */
74 Oid classoid; /* object class (catalog OID) */
75 Oid objoid; /* object OID */
76 int objsubid; /* subobject (table column #) */
81 const char *provider; /* label provider of this security label */
82 const char *label; /* security label for an object */
83 Oid classoid; /* object class (catalog OID) */
84 Oid objoid; /* object OID */
85 int objsubid; /* subobject (table column #) */
89 bool g_verbose; /* User wants verbose narration of our
92 /* various user-settable parameters */
95 int dumpSections; /* bitmask of chosen sections */
97 const char *lockWaitTimeout;
99 /* subquery used to convert user ID (eg, datdba) to user name */
100 static const char *username_subquery;
102 /* obsolete as of 7.3: */
103 static Oid g_last_builtin_oid; /* value of the last builtin oid */
106 * Object inclusion/exclusion lists
108 * The string lists record the patterns given by command-line switches,
109 * which we then convert to lists of OIDs of matching objects.
111 static SimpleStringList schema_include_patterns = {NULL, NULL};
112 static SimpleOidList schema_include_oids = {NULL, NULL};
113 static SimpleStringList schema_exclude_patterns = {NULL, NULL};
114 static SimpleOidList schema_exclude_oids = {NULL, NULL};
116 static SimpleStringList table_include_patterns = {NULL, NULL};
117 static SimpleOidList table_include_oids = {NULL, NULL};
118 static SimpleStringList table_exclude_patterns = {NULL, NULL};
119 static SimpleOidList table_exclude_oids = {NULL, NULL};
120 static SimpleStringList tabledata_exclude_patterns = {NULL, NULL};
121 static SimpleOidList tabledata_exclude_oids = {NULL, NULL};
123 /* default, if no "inclusion" switches appear, is to dump everything */
124 static bool include_everything = true;
126 char g_opaque_type[10]; /* name for the opaque type */
128 /* placeholders for the delimiters for comments */
129 char g_comment_start[10];
130 char g_comment_end[10];
132 static const CatalogId nilCatalogId = {0, 0};
134 /* flags for various command-line long options */
135 static int binary_upgrade = 0;
136 static int disable_dollar_quoting = 0;
137 static int dump_inserts = 0;
138 static int column_inserts = 0;
139 static int no_security_labels = 0;
140 static int no_synchronized_snapshots = 0;
141 static int no_unlogged_table_data = 0;
142 static int serializable_deferrable = 0;
145 static void help(const char *progname);
146 static void setup_connection(Archive *AH, const char *dumpencoding,
148 static ArchiveFormat parseArchiveFormat(const char *format, ArchiveMode *mode);
149 static void expand_schema_name_patterns(Archive *fout,
150 SimpleStringList *patterns,
151 SimpleOidList *oids);
152 static void expand_table_name_patterns(Archive *fout,
153 SimpleStringList *patterns,
154 SimpleOidList *oids);
155 static NamespaceInfo *findNamespace(Archive *fout, Oid nsoid, Oid objoid);
156 static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
157 static void refreshMatViewData(Archive *fout, TableDataInfo *tdinfo);
158 static void guessConstraintInheritance(TableInfo *tblinfo, int numTables);
159 static void dumpComment(Archive *fout, const char *target,
160 const char *namespace, const char *owner,
161 CatalogId catalogId, int subid, DumpId dumpId);
162 static int findComments(Archive *fout, Oid classoid, Oid objoid,
163 CommentItem **items);
164 static int collectComments(Archive *fout, CommentItem **items);
165 static void dumpSecLabel(Archive *fout, const char *target,
166 const char *namespace, const char *owner,
167 CatalogId catalogId, int subid, DumpId dumpId);
168 static int findSecLabels(Archive *fout, Oid classoid, Oid objoid,
169 SecLabelItem **items);
170 static int collectSecLabels(Archive *fout, SecLabelItem **items);
171 static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
172 static void dumpNamespace(Archive *fout, NamespaceInfo *nspinfo);
173 static void dumpExtension(Archive *fout, ExtensionInfo *extinfo);
174 static void dumpType(Archive *fout, TypeInfo *tyinfo);
175 static void dumpBaseType(Archive *fout, TypeInfo *tyinfo);
176 static void dumpEnumType(Archive *fout, TypeInfo *tyinfo);
177 static void dumpRangeType(Archive *fout, TypeInfo *tyinfo);
178 static void dumpDomain(Archive *fout, TypeInfo *tyinfo);
179 static void dumpCompositeType(Archive *fout, TypeInfo *tyinfo);
180 static void dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo);
181 static void dumpShellType(Archive *fout, ShellTypeInfo *stinfo);
182 static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
183 static void dumpFunc(Archive *fout, FuncInfo *finfo);
184 static void dumpCast(Archive *fout, CastInfo *cast);
185 static void dumpOpr(Archive *fout, OprInfo *oprinfo);
186 static void dumpOpclass(Archive *fout, OpclassInfo *opcinfo);
187 static void dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo);
188 static void dumpCollation(Archive *fout, CollInfo *convinfo);
189 static void dumpConversion(Archive *fout, ConvInfo *convinfo);
190 static void dumpRule(Archive *fout, RuleInfo *rinfo);
191 static void dumpAgg(Archive *fout, AggInfo *agginfo);
192 static void dumpTrigger(Archive *fout, TriggerInfo *tginfo);
193 static void dumpEventTrigger(Archive *fout, EventTriggerInfo *evtinfo);
194 static void dumpTable(Archive *fout, TableInfo *tbinfo);
195 static void dumpTableSchema(Archive *fout, TableInfo *tbinfo);
196 static void dumpAttrDef(Archive *fout, AttrDefInfo *adinfo);
197 static void dumpSequence(Archive *fout, TableInfo *tbinfo);
198 static void dumpSequenceData(Archive *fout, TableDataInfo *tdinfo);
199 static void dumpIndex(Archive *fout, IndxInfo *indxinfo);
200 static void dumpConstraint(Archive *fout, ConstraintInfo *coninfo);
201 static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo);
202 static void dumpTSParser(Archive *fout, TSParserInfo *prsinfo);
203 static void dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo);
204 static void dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo);
205 static void dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo);
206 static void dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo);
207 static void dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo);
208 static void dumpUserMappings(Archive *fout,
209 const char *servername, const char *namespace,
210 const char *owner, CatalogId catalogId, DumpId dumpId);
211 static void dumpDefaultACL(Archive *fout, DefaultACLInfo *daclinfo);
213 static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
214 const char *type, const char *name, const char *subname,
215 const char *tag, const char *nspname, const char *owner,
218 static void getDependencies(Archive *fout);
219 static void BuildArchiveDependencies(Archive *fout);
220 static void findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj,
221 DumpId **dependencies, int *nDeps, int *allocDeps);
223 static DumpableObject *createBoundaryObjects(void);
224 static void addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
225 DumpableObject *boundaryObjs);
227 static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
228 static void getTableData(TableInfo *tblinfo, int numTables, bool oids);
229 static void makeTableDataInfo(TableInfo *tbinfo, bool oids);
230 static void buildMatViewRefreshDependencies(Archive *fout);
231 static void getTableDataFKConstraints(void);
232 static char *format_function_arguments(FuncInfo *finfo, char *funcargs,
234 static char *format_function_arguments_old(Archive *fout,
235 FuncInfo *finfo, int nallargs,
239 static char *format_function_signature(Archive *fout,
240 FuncInfo *finfo, bool honor_quotes);
241 static const char *convertRegProcReference(Archive *fout,
243 static const char *convertOperatorReference(Archive *fout, const char *opr);
244 static const char *convertTSFunction(Archive *fout, Oid funcOid);
245 static Oid findLastBuiltinOid_V71(Archive *fout, const char *);
246 static Oid findLastBuiltinOid_V70(Archive *fout);
247 static void selectSourceSchema(Archive *fout, const char *schemaName);
248 static char *getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts);
249 static char *myFormatType(const char *typname, int32 typmod);
250 static void getBlobs(Archive *fout);
251 static void dumpBlob(Archive *fout, BlobInfo *binfo);
252 static int dumpBlobs(Archive *fout, void *arg);
253 static void dumpDatabase(Archive *AH);
254 static void dumpEncoding(Archive *AH);
255 static void dumpStdStrings(Archive *AH);
256 static void binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
257 PQExpBuffer upgrade_buffer, Oid pg_type_oid);
258 static bool binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
259 PQExpBuffer upgrade_buffer, Oid pg_rel_oid);
260 static void binary_upgrade_set_pg_class_oids(Archive *fout,
261 PQExpBuffer upgrade_buffer,
262 Oid pg_class_oid, bool is_index);
263 static void binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
264 DumpableObject *dobj,
265 const char *objlabel);
266 static const char *getAttrName(int attrnum, TableInfo *tblInfo);
267 static const char *fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer);
268 static char *get_synchronized_snapshot(Archive *fout);
269 static PGresult *ExecuteSqlQueryForSingleRow(Archive *fout, char *query);
270 static void setupDumpWorker(Archive *AHX, RestoreOptions *ropt);
274 main(int argc, char **argv)
277 const char *filename = NULL;
278 const char *format = "p";
279 const char *dbname = NULL;
280 const char *pghost = NULL;
281 const char *pgport = NULL;
282 const char *username = NULL;
283 const char *dumpencoding = NULL;
287 DumpableObject **dobjs;
289 DumpableObject *boundaryObjs;
292 enum trivalue prompt_password = TRI_DEFAULT;
293 int compressLevel = -1;
296 int outputCreateDB = 0;
297 bool outputBlobs = false;
298 int outputNoOwner = 0;
299 char *outputSuperuser = NULL;
300 char *use_role = NULL;
302 RestoreOptions *ropt;
303 ArchiveFormat archiveFormat = archUnknown;
304 ArchiveMode archiveMode;
305 Archive *fout; /* the script file */
307 static int disable_triggers = 0;
308 static int outputNoTablespaces = 0;
309 static int use_setsessauth = 0;
311 static struct option long_options[] = {
312 {"data-only", no_argument, NULL, 'a'},
313 {"blobs", no_argument, NULL, 'b'},
314 {"clean", no_argument, NULL, 'c'},
315 {"create", no_argument, NULL, 'C'},
316 {"dbname", required_argument, NULL, 'd'},
317 {"file", required_argument, NULL, 'f'},
318 {"format", required_argument, NULL, 'F'},
319 {"host", required_argument, NULL, 'h'},
320 {"ignore-version", no_argument, NULL, 'i'},
321 {"jobs", 1, NULL, 'j'},
322 {"no-reconnect", no_argument, NULL, 'R'},
323 {"oids", no_argument, NULL, 'o'},
324 {"no-owner", no_argument, NULL, 'O'},
325 {"port", required_argument, NULL, 'p'},
326 {"schema", required_argument, NULL, 'n'},
327 {"exclude-schema", required_argument, NULL, 'N'},
328 {"schema-only", no_argument, NULL, 's'},
329 {"superuser", required_argument, NULL, 'S'},
330 {"table", required_argument, NULL, 't'},
331 {"exclude-table", required_argument, NULL, 'T'},
332 {"no-password", no_argument, NULL, 'w'},
333 {"password", no_argument, NULL, 'W'},
334 {"username", required_argument, NULL, 'U'},
335 {"verbose", no_argument, NULL, 'v'},
336 {"no-privileges", no_argument, NULL, 'x'},
337 {"no-acl", no_argument, NULL, 'x'},
338 {"compress", required_argument, NULL, 'Z'},
339 {"encoding", required_argument, NULL, 'E'},
340 {"help", no_argument, NULL, '?'},
341 {"version", no_argument, NULL, 'V'},
344 * the following options don't have an equivalent short option letter
346 {"attribute-inserts", no_argument, &column_inserts, 1},
347 {"binary-upgrade", no_argument, &binary_upgrade, 1},
348 {"column-inserts", no_argument, &column_inserts, 1},
349 {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
350 {"disable-triggers", no_argument, &disable_triggers, 1},
351 {"exclude-table-data", required_argument, NULL, 4},
352 {"inserts", no_argument, &dump_inserts, 1},
353 {"lock-wait-timeout", required_argument, NULL, 2},
354 {"no-tablespaces", no_argument, &outputNoTablespaces, 1},
355 {"quote-all-identifiers", no_argument, "e_all_identifiers, 1},
356 {"role", required_argument, NULL, 3},
357 {"section", required_argument, NULL, 5},
358 {"serializable-deferrable", no_argument, &serializable_deferrable, 1},
359 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
360 {"no-security-labels", no_argument, &no_security_labels, 1},
361 {"no-synchronized-snapshots", no_argument, &no_synchronized_snapshots, 1},
362 {"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
367 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
370 * Initialize what we need for parallel execution, especially for thread
371 * support on Windows.
373 init_parallel_dump_utils();
377 strcpy(g_comment_start, "-- ");
378 g_comment_end[0] = '\0';
379 strcpy(g_opaque_type, "opaque");
381 dataOnly = schemaOnly = false;
382 dumpSections = DUMP_UNSECTIONED;
383 lockWaitTimeout = NULL;
385 progname = get_progname(argv[0]);
387 /* Set default options based on progname */
388 if (strcmp(progname, "pg_backup") == 0)
393 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
398 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
400 puts("pg_dump (PostgreSQL) " PG_VERSION);
405 while ((c = getopt_long(argc, argv, "abcCd:E:f:F:h:ij:K:n:N:oOp:RsS:t:T:U:vwWxZ:",
406 long_options, &optindex)) != -1)
410 case 'a': /* Dump data only */
414 case 'b': /* Dump blobs */
418 case 'c': /* clean (i.e., drop) schema prior to create */
422 case 'C': /* Create DB */
426 case 'd': /* database name */
427 dbname = pg_strdup(optarg);
430 case 'E': /* Dump encoding */
431 dumpencoding = pg_strdup(optarg);
435 filename = pg_strdup(optarg);
439 format = pg_strdup(optarg);
442 case 'h': /* server host */
443 pghost = pg_strdup(optarg);
447 /* ignored, deprecated option */
450 case 'j': /* number of dump jobs */
451 numWorkers = atoi(optarg);
454 case 'n': /* include schema(s) */
455 simple_string_list_append(&schema_include_patterns, optarg);
456 include_everything = false;
459 case 'N': /* exclude schema(s) */
460 simple_string_list_append(&schema_exclude_patterns, optarg);
463 case 'o': /* Dump oids */
467 case 'O': /* Don't reconnect to match owner */
471 case 'p': /* server port */
472 pgport = pg_strdup(optarg);
476 /* no-op, still accepted for backwards compatibility */
479 case 's': /* dump schema only */
483 case 'S': /* Username for superuser in plain text output */
484 outputSuperuser = pg_strdup(optarg);
487 case 't': /* include table(s) */
488 simple_string_list_append(&table_include_patterns, optarg);
489 include_everything = false;
492 case 'T': /* exclude table(s) */
493 simple_string_list_append(&table_exclude_patterns, optarg);
497 username = pg_strdup(optarg);
500 case 'v': /* verbose */
505 prompt_password = TRI_NO;
509 prompt_password = TRI_YES;
512 case 'x': /* skip ACL dump */
516 case 'Z': /* Compression Level */
517 compressLevel = atoi(optarg);
521 /* This covers the long options. */
524 case 2: /* lock-wait-timeout */
525 lockWaitTimeout = pg_strdup(optarg);
528 case 3: /* SET ROLE */
529 use_role = pg_strdup(optarg);
532 case 4: /* exclude table(s) data */
533 simple_string_list_append(&tabledata_exclude_patterns, optarg);
536 case 5: /* section */
537 set_dump_section(optarg, &dumpSections);
541 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
547 * Non-option argument specifies database name as long as it wasn't
548 * already specified with -d / --dbname
550 if (optind < argc && dbname == NULL)
551 dbname = argv[optind++];
553 /* Complain if any arguments remain */
556 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
557 progname, argv[optind]);
558 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
563 /* --column-inserts implies --inserts */
567 if (dataOnly && schemaOnly)
568 exit_horribly(NULL, "options -s/--schema-only and -a/--data-only cannot be used together\n");
570 if (dataOnly && outputClean)
571 exit_horribly(NULL, "options -c/--clean and -a/--data-only cannot be used together\n");
573 if (dump_inserts && oids)
575 write_msg(NULL, "options --inserts/--column-inserts and -o/--oids cannot be used together\n");
576 write_msg(NULL, "(The INSERT command cannot set OIDs.)\n");
580 /* Identify archive format to emit */
581 archiveFormat = parseArchiveFormat(format, &archiveMode);
583 /* archiveFormat specific setup */
584 if (archiveFormat == archNull)
587 /* Custom and directory formats are compressed by default, others not */
588 if (compressLevel == -1)
590 if (archiveFormat == archCustom || archiveFormat == archDirectory)
591 compressLevel = Z_DEFAULT_COMPRESSION;
597 * On Windows we can only have at most MAXIMUM_WAIT_OBJECTS (= 64 usually)
598 * parallel jobs because that's the maximum limit for the
599 * WaitForMultipleObjects() call.
603 || numWorkers > MAXIMUM_WAIT_OBJECTS
606 exit_horribly(NULL, "%s: invalid number of parallel jobs\n", progname);
608 /* Parallel backup only in the directory archive format so far */
609 if (archiveFormat != archDirectory && numWorkers > 1)
610 exit_horribly(NULL, "parallel backup only supported by the directory format\n");
612 /* Open the output file */
613 fout = CreateArchive(filename, archiveFormat, compressLevel, archiveMode,
616 /* Register the cleanup hook */
617 on_exit_close_archive(fout);
620 exit_horribly(NULL, "could not open output file \"%s\" for writing\n", filename);
622 /* Let the archiver know how noisy to be */
623 fout->verbose = g_verbose;
626 * We allow the server to be back to 7.0, and up to any minor release of
627 * our own major version. (See also version check in pg_dumpall.c.)
629 fout->minRemoteVersion = 70000;
630 fout->maxRemoteVersion = (PG_VERSION_NUM / 100) * 100 + 99;
632 fout->numWorkers = numWorkers;
635 * Open the database using the Archiver, so it knows about it. Errors mean
638 ConnectDatabase(fout, dbname, pghost, pgport, username, prompt_password);
639 setup_connection(fout, dumpencoding, use_role);
642 * Disable security label support if server version < v9.1.x (prevents
643 * access to nonexistent pg_seclabel catalog)
645 if (fout->remoteVersion < 90100)
646 no_security_labels = 1;
649 * When running against 9.0 or later, check if we are in recovery mode,
650 * which means we are on a hot standby.
652 if (fout->remoteVersion >= 90000)
654 PGresult *res = ExecuteSqlQueryForSingleRow(fout, "SELECT pg_catalog.pg_is_in_recovery()");
656 if (strcmp(PQgetvalue(res, 0, 0), "t") == 0)
659 * On hot standby slaves, never try to dump unlogged table data,
660 * since it will just throw an error.
662 no_unlogged_table_data = true;
667 /* Select the appropriate subquery to convert user IDs to names */
668 if (fout->remoteVersion >= 80100)
669 username_subquery = "SELECT rolname FROM pg_catalog.pg_roles WHERE oid =";
670 else if (fout->remoteVersion >= 70300)
671 username_subquery = "SELECT usename FROM pg_catalog.pg_user WHERE usesysid =";
673 username_subquery = "SELECT usename FROM pg_user WHERE usesysid =";
675 /* check the version for the synchronized snapshots feature */
676 if (numWorkers > 1 && fout->remoteVersion < 90200
677 && !no_synchronized_snapshots)
679 "Synchronized snapshots are not supported by this server version.\n"
680 "Run with --no-synchronized-snapshots instead if you do not need\n"
681 "synchronized snapshots.\n");
683 /* Find the last built-in OID, if needed */
684 if (fout->remoteVersion < 70300)
686 if (fout->remoteVersion >= 70100)
687 g_last_builtin_oid = findLastBuiltinOid_V71(fout,
688 PQdb(GetConnection(fout)));
690 g_last_builtin_oid = findLastBuiltinOid_V70(fout);
692 write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
695 /* Expand schema selection patterns into OID lists */
696 if (schema_include_patterns.head != NULL)
698 expand_schema_name_patterns(fout, &schema_include_patterns,
699 &schema_include_oids);
700 if (schema_include_oids.head == NULL)
701 exit_horribly(NULL, "No matching schemas were found\n");
703 expand_schema_name_patterns(fout, &schema_exclude_patterns,
704 &schema_exclude_oids);
705 /* non-matching exclusion patterns aren't an error */
707 /* Expand table selection patterns into OID lists */
708 if (table_include_patterns.head != NULL)
710 expand_table_name_patterns(fout, &table_include_patterns,
711 &table_include_oids);
712 if (table_include_oids.head == NULL)
713 exit_horribly(NULL, "No matching tables were found\n");
715 expand_table_name_patterns(fout, &table_exclude_patterns,
716 &table_exclude_oids);
718 expand_table_name_patterns(fout, &tabledata_exclude_patterns,
719 &tabledata_exclude_oids);
721 /* non-matching exclusion patterns aren't an error */
724 * Dumping blobs is now default unless we saw an inclusion switch or -s
725 * ... but even if we did see one of these, -b turns it back on.
727 if (include_everything && !schemaOnly)
731 * Now scan the database and create DumpableObject structs for all the
732 * objects we intend to dump.
734 tblinfo = getSchemaData(fout, &numTables);
736 if (fout->remoteVersion < 80400)
737 guessConstraintInheritance(tblinfo, numTables);
741 getTableData(tblinfo, numTables, oids);
742 buildMatViewRefreshDependencies(fout);
744 getTableDataFKConstraints();
751 * Collect dependency data to assist in ordering the objects.
753 getDependencies(fout);
755 /* Lastly, create dummy objects to represent the section boundaries */
756 boundaryObjs = createBoundaryObjects();
758 /* Get pointers to all the known DumpableObjects */
759 getDumpableObjects(&dobjs, &numObjs);
762 * Add dummy dependencies to enforce the dump section ordering.
764 addBoundaryDependencies(dobjs, numObjs, boundaryObjs);
767 * Sort the objects into a safe dump order (no forward references).
769 * In 7.3 or later, we can rely on dependency information to help us
770 * determine a safe order, so the initial sort is mostly for cosmetic
771 * purposes: we sort by name to ensure that logically identical schemas
772 * will dump identically. Before 7.3 we don't have dependencies and we
773 * use OID ordering as an (unreliable) guide to creation order.
775 if (fout->remoteVersion >= 70300)
776 sortDumpableObjectsByTypeName(dobjs, numObjs);
778 sortDumpableObjectsByTypeOid(dobjs, numObjs);
780 /* If we do a parallel dump, we want the largest tables to go first */
781 if (archiveFormat == archDirectory && numWorkers > 1)
782 sortDataAndIndexObjectsBySize(dobjs, numObjs);
784 sortDumpableObjects(dobjs, numObjs,
785 boundaryObjs[0].dumpId, boundaryObjs[1].dumpId);
788 * Create archive TOC entries for all the objects to be dumped, in a safe
792 /* First the special ENCODING and STDSTRINGS entries. */
794 dumpStdStrings(fout);
796 /* The database item is always next, unless we don't want it at all */
797 if (include_everything && !dataOnly)
800 /* Now the rearrangeable objects. */
801 for (i = 0; i < numObjs; i++)
802 dumpDumpableObject(fout, dobjs[i]);
805 * Set up options info to ensure we dump what we want.
807 ropt = NewRestoreOptions();
808 ropt->filename = filename;
809 ropt->dropSchema = outputClean;
810 ropt->dataOnly = dataOnly;
811 ropt->schemaOnly = schemaOnly;
812 ropt->dumpSections = dumpSections;
813 ropt->aclsSkip = aclsSkip;
814 ropt->superuser = outputSuperuser;
815 ropt->createDB = outputCreateDB;
816 ropt->noOwner = outputNoOwner;
817 ropt->noTablespace = outputNoTablespaces;
818 ropt->disable_triggers = disable_triggers;
819 ropt->use_setsessauth = use_setsessauth;
821 if (compressLevel == -1)
822 ropt->compression = 0;
824 ropt->compression = compressLevel;
826 ropt->suppressDumpWarnings = true; /* We've already shown them */
828 SetArchiveRestoreOptions(fout, ropt);
831 * The archive's TOC entries are now marked as to which ones will actually
832 * be output, so we can set up their dependency lists properly. This isn't
833 * necessary for plain-text output, though.
836 BuildArchiveDependencies(fout);
839 * And finally we can do the actual output.
841 * Note: for non-plain-text output formats, the output file is written
842 * inside CloseArchive(). This is, um, bizarre; but not worth changing
846 RestoreArchive(fout);
855 help(const char *progname)
857 printf(_("%s dumps a database as a text file or to other formats.\n\n"), progname);
858 printf(_("Usage:\n"));
859 printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
861 printf(_("\nGeneral options:\n"));
862 printf(_(" -f, --file=FILENAME output file or directory name\n"));
863 printf(_(" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
864 " plain text (default))\n"));
865 printf(_(" -j, --jobs=NUM use this many parallel jobs to dump\n"));
866 printf(_(" -v, --verbose verbose mode\n"));
867 printf(_(" -V, --version output version information, then exit\n"));
868 printf(_(" -Z, --compress=0-9 compression level for compressed formats\n"));
869 printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
870 printf(_(" -?, --help show this help, then exit\n"));
872 printf(_("\nOptions controlling the output content:\n"));
873 printf(_(" -a, --data-only dump only the data, not the schema\n"));
874 printf(_(" -b, --blobs include large objects in dump\n"));
875 printf(_(" -c, --clean clean (drop) database objects before recreating\n"));
876 printf(_(" -C, --create include commands to create database in dump\n"));
877 printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
878 printf(_(" -n, --schema=SCHEMA dump the named schema(s) only\n"));
879 printf(_(" -N, --exclude-schema=SCHEMA do NOT dump the named schema(s)\n"));
880 printf(_(" -o, --oids include OIDs in dump\n"));
881 printf(_(" -O, --no-owner skip restoration of object ownership in\n"
882 " plain-text format\n"));
883 printf(_(" -s, --schema-only dump only the schema, no data\n"));
884 printf(_(" -S, --superuser=NAME superuser user name to use in plain-text format\n"));
885 printf(_(" -t, --table=TABLE dump the named table(s) only\n"));
886 printf(_(" -T, --exclude-table=TABLE do NOT dump the named table(s)\n"));
887 printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
888 printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
889 printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
890 printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
891 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
892 printf(_(" --exclude-table-data=TABLE do NOT dump data for the named table(s)\n"));
893 printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
894 printf(_(" --no-security-labels do not dump security label assignments\n"));
895 printf(_(" --no-synchronized-snapshots do not use synchronized snapshots in parallel jobs\n"));
896 printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
897 printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
898 printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
899 printf(_(" --section=SECTION dump named section (pre-data, data, or post-data)\n"));
900 printf(_(" --serializable-deferrable wait until the dump can run without anomalies\n"));
901 printf(_(" --use-set-session-authorization\n"
902 " use SET SESSION AUTHORIZATION commands instead of\n"
903 " ALTER OWNER commands to set ownership\n"));
905 printf(_("\nConnection options:\n"));
906 printf(_(" -d, --dbname=DBNAME database to dump\n"));
907 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
908 printf(_(" -p, --port=PORT database server port number\n"));
909 printf(_(" -U, --username=NAME connect as specified database user\n"));
910 printf(_(" -w, --no-password never prompt for password\n"));
911 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
912 printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
914 printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
915 "variable value is used.\n\n"));
916 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
920 setup_connection(Archive *AH, const char *dumpencoding, char *use_role)
922 PGconn *conn = GetConnection(AH);
923 const char *std_strings;
926 * Set the client encoding if requested. If dumpencoding == NULL then
927 * either it hasn't been requested or we're a cloned connection and then
928 * this has already been set in CloneArchive according to the original
929 * connection encoding.
933 if (PQsetClientEncoding(conn, dumpencoding) < 0)
934 exit_horribly(NULL, "invalid client encoding \"%s\" specified\n",
939 * Get the active encoding and the standard_conforming_strings setting, so
940 * we know how to escape strings.
942 AH->encoding = PQclientEncoding(conn);
944 std_strings = PQparameterStatus(conn, "standard_conforming_strings");
945 AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);
947 /* Set the role if requested */
948 if (!use_role && AH->use_role)
949 use_role = AH->use_role;
951 /* Set the role if requested */
952 if (use_role && AH->remoteVersion >= 80100)
954 PQExpBuffer query = createPQExpBuffer();
956 appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
957 ExecuteSqlStatement(AH, query->data);
958 destroyPQExpBuffer(query);
960 /* save this for later use on parallel connections */
962 AH->use_role = strdup(use_role);
965 /* Set the datestyle to ISO to ensure the dump's portability */
966 ExecuteSqlStatement(AH, "SET DATESTYLE = ISO");
968 /* Likewise, avoid using sql_standard intervalstyle */
969 if (AH->remoteVersion >= 80400)
970 ExecuteSqlStatement(AH, "SET INTERVALSTYLE = POSTGRES");
973 * If supported, set extra_float_digits so that we can dump float data
974 * exactly (given correctly implemented float I/O code, anyway)
976 if (AH->remoteVersion >= 90000)
977 ExecuteSqlStatement(AH, "SET extra_float_digits TO 3");
978 else if (AH->remoteVersion >= 70400)
979 ExecuteSqlStatement(AH, "SET extra_float_digits TO 2");
982 * If synchronized scanning is supported, disable it, to prevent
983 * unpredictable changes in row ordering across a dump and reload.
985 if (AH->remoteVersion >= 80300)
986 ExecuteSqlStatement(AH, "SET synchronize_seqscans TO off");
989 * Disable timeouts if supported.
991 if (AH->remoteVersion >= 70300)
992 ExecuteSqlStatement(AH, "SET statement_timeout = 0");
993 if (AH->remoteVersion >= 90300)
994 ExecuteSqlStatement(AH, "SET lock_timeout = 0");
997 * Quote all identifiers, if requested.
999 if (quote_all_identifiers && AH->remoteVersion >= 90100)
1000 ExecuteSqlStatement(AH, "SET quote_all_identifiers = true");
1003 * Start transaction-snapshot mode transaction to dump consistent data.
1005 ExecuteSqlStatement(AH, "BEGIN");
1006 if (AH->remoteVersion >= 90100)
1008 if (serializable_deferrable)
1009 ExecuteSqlStatement(AH,
1010 "SET TRANSACTION ISOLATION LEVEL "
1011 "SERIALIZABLE, READ ONLY, DEFERRABLE");
1013 ExecuteSqlStatement(AH,
1014 "SET TRANSACTION ISOLATION LEVEL "
1015 "REPEATABLE READ, READ ONLY");
1017 else if (AH->remoteVersion >= 70400)
1019 /* note: comma was not accepted in SET TRANSACTION before 8.0 */
1020 ExecuteSqlStatement(AH,
1021 "SET TRANSACTION ISOLATION LEVEL "
1022 "SERIALIZABLE READ ONLY");
1025 ExecuteSqlStatement(AH,
1026 "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
1030 if (AH->numWorkers > 1 && AH->remoteVersion >= 90200 && !no_synchronized_snapshots)
1032 if (AH->sync_snapshot_id)
1034 PQExpBuffer query = createPQExpBuffer();
1036 appendPQExpBuffer(query, "SET TRANSACTION SNAPSHOT ");
1037 appendStringLiteralConn(query, AH->sync_snapshot_id, conn);
1038 ExecuteSqlStatement(AH, query->data);
1039 destroyPQExpBuffer(query);
1042 AH->sync_snapshot_id = get_synchronized_snapshot(AH);
1047 setupDumpWorker(Archive *AHX, RestoreOptions *ropt)
1049 setup_connection(AHX, NULL, NULL);
1053 get_synchronized_snapshot(Archive *fout)
1055 char *query = "SELECT pg_export_snapshot()";
1059 res = ExecuteSqlQueryForSingleRow(fout, query);
1060 result = strdup(PQgetvalue(res, 0, 0));
1066 static ArchiveFormat
1067 parseArchiveFormat(const char *format, ArchiveMode *mode)
1069 ArchiveFormat archiveFormat;
1071 *mode = archModeWrite;
1073 if (pg_strcasecmp(format, "a") == 0 || pg_strcasecmp(format, "append") == 0)
1075 /* This is used by pg_dumpall, and is not documented */
1076 archiveFormat = archNull;
1077 *mode = archModeAppend;
1079 else if (pg_strcasecmp(format, "c") == 0)
1080 archiveFormat = archCustom;
1081 else if (pg_strcasecmp(format, "custom") == 0)
1082 archiveFormat = archCustom;
1083 else if (pg_strcasecmp(format, "d") == 0)
1084 archiveFormat = archDirectory;
1085 else if (pg_strcasecmp(format, "directory") == 0)
1086 archiveFormat = archDirectory;
1087 else if (pg_strcasecmp(format, "p") == 0)
1088 archiveFormat = archNull;
1089 else if (pg_strcasecmp(format, "plain") == 0)
1090 archiveFormat = archNull;
1091 else if (pg_strcasecmp(format, "t") == 0)
1092 archiveFormat = archTar;
1093 else if (pg_strcasecmp(format, "tar") == 0)
1094 archiveFormat = archTar;
1096 exit_horribly(NULL, "invalid output format \"%s\" specified\n", format);
1097 return archiveFormat;
1101 * Find the OIDs of all schemas matching the given list of patterns,
1102 * and append them to the given OID list.
1105 expand_schema_name_patterns(Archive *fout,
1106 SimpleStringList *patterns,
1107 SimpleOidList *oids)
1111 SimpleStringListCell *cell;
1114 if (patterns->head == NULL)
1115 return; /* nothing to do */
1117 if (fout->remoteVersion < 70300)
1118 exit_horribly(NULL, "server version must be at least 7.3 to use schema selection switches\n");
1120 query = createPQExpBuffer();
1123 * We use UNION ALL rather than UNION; this might sometimes result in
1124 * duplicate entries in the OID list, but we don't care.
1127 for (cell = patterns->head; cell; cell = cell->next)
1129 if (cell != patterns->head)
1130 appendPQExpBuffer(query, "UNION ALL\n");
1131 appendPQExpBuffer(query,
1132 "SELECT oid FROM pg_catalog.pg_namespace n\n");
1133 processSQLNamePattern(GetConnection(fout), query, cell->val, false,
1134 false, NULL, "n.nspname", NULL, NULL);
1137 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1139 for (i = 0; i < PQntuples(res); i++)
1141 simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1145 destroyPQExpBuffer(query);
1149 * Find the OIDs of all tables matching the given list of patterns,
1150 * and append them to the given OID list.
1153 expand_table_name_patterns(Archive *fout,
1154 SimpleStringList *patterns, SimpleOidList *oids)
1158 SimpleStringListCell *cell;
1161 if (patterns->head == NULL)
1162 return; /* nothing to do */
1164 query = createPQExpBuffer();
1167 * We use UNION ALL rather than UNION; this might sometimes result in
1168 * duplicate entries in the OID list, but we don't care.
1171 for (cell = patterns->head; cell; cell = cell->next)
1173 if (cell != patterns->head)
1174 appendPQExpBuffer(query, "UNION ALL\n");
1175 appendPQExpBuffer(query,
1177 "\nFROM pg_catalog.pg_class c"
1178 "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace"
1179 "\nWHERE c.relkind in ('%c', '%c', '%c', '%c', '%c')\n",
1180 RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW,
1181 RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
1182 processSQLNamePattern(GetConnection(fout), query, cell->val, true,
1183 false, "n.nspname", "c.relname", NULL,
1184 "pg_catalog.pg_table_is_visible(c.oid)");
1187 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1189 for (i = 0; i < PQntuples(res); i++)
1191 simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0)));
1195 destroyPQExpBuffer(query);
1199 * selectDumpableNamespace: policy-setting subroutine
1200 * Mark a namespace as to be dumped or not
1203 selectDumpableNamespace(NamespaceInfo *nsinfo)
1206 * If specific tables are being dumped, do not dump any complete
1207 * namespaces. If specific namespaces are being dumped, dump just those
1208 * namespaces. Otherwise, dump all non-system namespaces.
1210 if (table_include_oids.head != NULL)
1211 nsinfo->dobj.dump = false;
1212 else if (schema_include_oids.head != NULL)
1213 nsinfo->dobj.dump = simple_oid_list_member(&schema_include_oids,
1214 nsinfo->dobj.catId.oid);
1215 else if (strncmp(nsinfo->dobj.name, "pg_", 3) == 0 ||
1216 strcmp(nsinfo->dobj.name, "information_schema") == 0)
1217 nsinfo->dobj.dump = false;
1219 nsinfo->dobj.dump = true;
1222 * In any case, a namespace can be excluded by an exclusion switch
1224 if (nsinfo->dobj.dump &&
1225 simple_oid_list_member(&schema_exclude_oids,
1226 nsinfo->dobj.catId.oid))
1227 nsinfo->dobj.dump = false;
1231 * selectDumpableTable: policy-setting subroutine
1232 * Mark a table as to be dumped or not
1235 selectDumpableTable(TableInfo *tbinfo)
1238 * If specific tables are being dumped, dump just those tables; else, dump
1239 * according to the parent namespace's dump flag.
1241 if (table_include_oids.head != NULL)
1242 tbinfo->dobj.dump = simple_oid_list_member(&table_include_oids,
1243 tbinfo->dobj.catId.oid);
1245 tbinfo->dobj.dump = tbinfo->dobj.namespace->dobj.dump;
1248 * In any case, a table can be excluded by an exclusion switch
1250 if (tbinfo->dobj.dump &&
1251 simple_oid_list_member(&table_exclude_oids,
1252 tbinfo->dobj.catId.oid))
1253 tbinfo->dobj.dump = false;
1257 * selectDumpableType: policy-setting subroutine
1258 * Mark a type as to be dumped or not
1260 * If it's a table's rowtype or an autogenerated array type, we also apply a
1261 * special type code to facilitate sorting into the desired order. (We don't
1262 * want to consider those to be ordinary types because that would bring tables
1263 * up into the datatype part of the dump order.) We still set the object's
1264 * dump flag; that's not going to cause the dummy type to be dumped, but we
1265 * need it so that casts involving such types will be dumped correctly -- see
1266 * dumpCast. This means the flag should be set the same as for the underlying
1267 * object (the table or base type).
1270 selectDumpableType(TypeInfo *tyinfo)
1272 /* skip complex types, except for standalone composite types */
1273 if (OidIsValid(tyinfo->typrelid) &&
1274 tyinfo->typrelkind != RELKIND_COMPOSITE_TYPE)
1276 TableInfo *tytable = findTableByOid(tyinfo->typrelid);
1278 tyinfo->dobj.objType = DO_DUMMY_TYPE;
1279 if (tytable != NULL)
1280 tyinfo->dobj.dump = tytable->dobj.dump;
1282 tyinfo->dobj.dump = false;
1286 /* skip auto-generated array types */
1287 if (tyinfo->isArray)
1289 tyinfo->dobj.objType = DO_DUMMY_TYPE;
1292 * Fall through to set the dump flag; we assume that the subsequent
1293 * rules will do the same thing as they would for the array's base
1294 * type. (We cannot reliably look up the base type here, since
1295 * getTypes may not have processed it yet.)
1299 /* dump only types in dumpable namespaces */
1300 if (!tyinfo->dobj.namespace->dobj.dump)
1301 tyinfo->dobj.dump = false;
1303 /* skip undefined placeholder types */
1304 else if (!tyinfo->isDefined)
1305 tyinfo->dobj.dump = false;
1308 tyinfo->dobj.dump = true;
1312 * selectDumpableDefaultACL: policy-setting subroutine
1313 * Mark a default ACL as to be dumped or not
1315 * For per-schema default ACLs, dump if the schema is to be dumped.
1316 * Otherwise dump if we are dumping "everything". Note that dataOnly
1317 * and aclsSkip are checked separately.
1320 selectDumpableDefaultACL(DefaultACLInfo *dinfo)
1322 if (dinfo->dobj.namespace)
1323 dinfo->dobj.dump = dinfo->dobj.namespace->dobj.dump;
1325 dinfo->dobj.dump = include_everything;
1329 * selectDumpableExtension: policy-setting subroutine
1330 * Mark an extension as to be dumped or not
1332 * Normally, we dump all extensions, or none of them if include_everything
1333 * is false (i.e., a --schema or --table switch was given). However, in
1334 * binary-upgrade mode it's necessary to skip built-in extensions, since we
1335 * assume those will already be installed in the target database. We identify
1336 * such extensions by their having OIDs in the range reserved for initdb.
1339 selectDumpableExtension(ExtensionInfo *extinfo)
1341 if (binary_upgrade && extinfo->dobj.catId.oid < (Oid) FirstNormalObjectId)
1342 extinfo->dobj.dump = false;
1344 extinfo->dobj.dump = include_everything;
1348 * selectDumpableObject: policy-setting subroutine
1349 * Mark a generic dumpable object as to be dumped or not
1351 * Use this only for object types without a special-case routine above.
1354 selectDumpableObject(DumpableObject *dobj)
1357 * Default policy is to dump if parent namespace is dumpable, or always
1358 * for non-namespace-associated items.
1360 if (dobj->namespace)
1361 dobj->dump = dobj->namespace->dobj.dump;
1367 * Dump a table's contents for loading using the COPY command
1368 * - this routine is called by the Archiver when it wants the table
1373 dumpTableData_copy(Archive *fout, void *dcontext)
1375 TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1376 TableInfo *tbinfo = tdinfo->tdtable;
1377 const char *classname = tbinfo->dobj.name;
1378 const bool hasoids = tbinfo->hasoids;
1379 const bool oids = tdinfo->oids;
1380 PQExpBuffer q = createPQExpBuffer();
1383 * Note: can't use getThreadLocalPQExpBuffer() here, we're calling fmtId
1384 * which uses it already.
1386 PQExpBuffer clistBuf = createPQExpBuffer();
1387 PGconn *conn = GetConnection(fout);
1391 const char *column_list;
1394 write_msg(NULL, "dumping contents of table %s\n", classname);
1397 * Make sure we are in proper schema. We will qualify the table name
1398 * below anyway (in case its name conflicts with a pg_catalog table); but
1399 * this ensures reproducible results in case the table contains regproc,
1400 * regclass, etc columns.
1402 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
1405 * If possible, specify the column list explicitly so that we have no
1406 * possibility of retrieving data in the wrong column order. (The default
1407 * column ordering of COPY will not be what we want in certain corner
1408 * cases involving ADD COLUMN and inheritance.)
1410 if (fout->remoteVersion >= 70300)
1411 column_list = fmtCopyColumnList(tbinfo, clistBuf);
1413 column_list = ""; /* can't select columns in COPY */
1415 if (oids && hasoids)
1417 appendPQExpBuffer(q, "COPY %s %s WITH OIDS TO stdout;",
1418 fmtQualifiedId(fout->remoteVersion,
1419 tbinfo->dobj.namespace->dobj.name,
1423 else if (tdinfo->filtercond)
1425 /* Note: this syntax is only supported in 8.2 and up */
1426 appendPQExpBufferStr(q, "COPY (SELECT ");
1427 /* klugery to get rid of parens in column list */
1428 if (strlen(column_list) > 2)
1430 appendPQExpBufferStr(q, column_list + 1);
1431 q->data[q->len - 1] = ' ';
1434 appendPQExpBufferStr(q, "* ");
1435 appendPQExpBuffer(q, "FROM %s %s) TO stdout;",
1436 fmtQualifiedId(fout->remoteVersion,
1437 tbinfo->dobj.namespace->dobj.name,
1439 tdinfo->filtercond);
1443 appendPQExpBuffer(q, "COPY %s %s TO stdout;",
1444 fmtQualifiedId(fout->remoteVersion,
1445 tbinfo->dobj.namespace->dobj.name,
1449 res = ExecuteSqlQuery(fout, q->data, PGRES_COPY_OUT);
1451 destroyPQExpBuffer(clistBuf);
1455 ret = PQgetCopyData(conn, ©buf, 0);
1458 break; /* done or error */
1462 WriteData(fout, copybuf, ret);
1469 * There was considerable discussion in late July, 2000 regarding
1470 * slowing down pg_dump when backing up large tables. Users with both
1471 * slow & fast (multi-processor) machines experienced performance
1472 * degradation when doing a backup.
1474 * Initial attempts based on sleeping for a number of ms for each ms
1475 * of work were deemed too complex, then a simple 'sleep in each loop'
1476 * implementation was suggested. The latter failed because the loop
1477 * was too tight. Finally, the following was implemented:
1479 * If throttle is non-zero, then
1480 * See how long since the last sleep.
1481 * Work out how long to sleep (based on ratio).
1482 * If sleep is more than 100ms, then
1488 * where the throttle value was the number of ms to sleep per ms of
1489 * work. The calculation was done in each loop.
1491 * Most of the hard work is done in the backend, and this solution
1492 * still did not work particularly well: on slow machines, the ratio
1493 * was 50:1, and on medium paced machines, 1:1, and on fast
1494 * multi-processor machines, it had little or no effect, for reasons
1495 * that were unclear.
1497 * Further discussion ensued, and the proposal was dropped.
1499 * For those people who want this feature, it can be implemented using
1500 * gettimeofday in each loop, calculating the time since last sleep,
1501 * multiplying that by the sleep ratio, then if the result is more
1502 * than a preset 'minimum sleep time' (say 100ms), call the 'select'
1503 * function to sleep for a subsecond period ie.
1505 * select(0, NULL, NULL, NULL, &tvi);
1507 * This will return after the interval specified in the structure tvi.
1508 * Finally, call gettimeofday again to save the 'last sleep time'.
1512 archprintf(fout, "\\.\n\n\n");
1516 /* copy data transfer failed */
1517 write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetCopyData() failed.\n", classname);
1518 write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1519 write_msg(NULL, "The command was: %s\n", q->data);
1523 /* Check command status and return to normal libpq state */
1524 res = PQgetResult(conn);
1525 if (PQresultStatus(res) != PGRES_COMMAND_OK)
1527 write_msg(NULL, "Dumping the contents of table \"%s\" failed: PQgetResult() failed.\n", classname);
1528 write_msg(NULL, "Error message from server: %s", PQerrorMessage(conn));
1529 write_msg(NULL, "The command was: %s\n", q->data);
1534 destroyPQExpBuffer(q);
1539 * Dump table data using INSERT commands.
1541 * Caution: when we restore from an archive file direct to database, the
1542 * INSERT commands emitted by this function have to be parsed by
1543 * pg_backup_db.c's ExecuteInsertCommands(), which will not handle comments,
1544 * E'' strings, or dollar-quoted strings. So don't emit anything like that.
1547 dumpTableData_insert(Archive *fout, void *dcontext)
1549 TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
1550 TableInfo *tbinfo = tdinfo->tdtable;
1551 const char *classname = tbinfo->dobj.name;
1552 PQExpBuffer q = createPQExpBuffer();
1559 * Make sure we are in proper schema. We will qualify the table name
1560 * below anyway (in case its name conflicts with a pg_catalog table); but
1561 * this ensures reproducible results in case the table contains regproc,
1562 * regclass, etc columns.
1564 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
1566 if (fout->remoteVersion >= 70100)
1568 appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
1569 "SELECT * FROM ONLY %s",
1570 fmtQualifiedId(fout->remoteVersion,
1571 tbinfo->dobj.namespace->dobj.name,
1576 appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
1578 fmtQualifiedId(fout->remoteVersion,
1579 tbinfo->dobj.namespace->dobj.name,
1582 if (tdinfo->filtercond)
1583 appendPQExpBuffer(q, " %s", tdinfo->filtercond);
1585 ExecuteSqlStatement(fout, q->data);
1589 res = ExecuteSqlQuery(fout, "FETCH 100 FROM _pg_dump_cursor",
1591 nfields = PQnfields(res);
1592 for (tuple = 0; tuple < PQntuples(res); tuple++)
1594 archprintf(fout, "INSERT INTO %s ", fmtId(classname));
1597 /* corner case for zero-column table */
1598 archprintf(fout, "DEFAULT VALUES;\n");
1603 resetPQExpBuffer(q);
1604 appendPQExpBuffer(q, "(");
1605 for (field = 0; field < nfields; field++)
1608 appendPQExpBuffer(q, ", ");
1609 appendPQExpBufferStr(q, fmtId(PQfname(res, field)));
1611 appendPQExpBuffer(q, ") ");
1612 archputs(q->data, fout);
1614 archprintf(fout, "VALUES (");
1615 for (field = 0; field < nfields; field++)
1618 archprintf(fout, ", ");
1619 if (PQgetisnull(res, tuple, field))
1621 archprintf(fout, "NULL");
1625 /* XXX This code is partially duplicated in ruleutils.c */
1626 switch (PQftype(res, field))
1637 * These types are printed without quotes unless
1638 * they contain values that aren't accepted by the
1639 * scanner unquoted (e.g., 'NaN'). Note that
1640 * strtod() and friends might accept NaN, so we
1641 * can't use that to test.
1643 * In reality we only need to defend against
1644 * infinity and NaN, so we need not get too crazy
1645 * about pattern matching here.
1647 const char *s = PQgetvalue(res, tuple, field);
1649 if (strspn(s, "0123456789 +-eE.") == strlen(s))
1650 archprintf(fout, "%s", s);
1652 archprintf(fout, "'%s'", s);
1658 archprintf(fout, "B'%s'",
1659 PQgetvalue(res, tuple, field));
1663 if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
1664 archprintf(fout, "true");
1666 archprintf(fout, "false");
1670 /* All other types are printed as string literals. */
1671 resetPQExpBuffer(q);
1672 appendStringLiteralAH(q,
1673 PQgetvalue(res, tuple, field),
1675 archputs(q->data, fout);
1679 archprintf(fout, ");\n");
1682 if (PQntuples(res) <= 0)
1690 archprintf(fout, "\n\n");
1692 ExecuteSqlStatement(fout, "CLOSE _pg_dump_cursor");
1694 destroyPQExpBuffer(q);
1701 * dump the contents of a single table
1703 * Actually, this just makes an ArchiveEntry for the table contents.
1706 dumpTableData(Archive *fout, TableDataInfo *tdinfo)
1708 TableInfo *tbinfo = tdinfo->tdtable;
1709 PQExpBuffer copyBuf = createPQExpBuffer();
1710 PQExpBuffer clistBuf = createPQExpBuffer();
1711 DataDumperPtr dumpFn;
1716 /* Dump/restore using COPY */
1717 dumpFn = dumpTableData_copy;
1718 /* must use 2 steps here 'cause fmtId is nonreentrant */
1719 appendPQExpBuffer(copyBuf, "COPY %s ",
1720 fmtId(tbinfo->dobj.name));
1721 appendPQExpBuffer(copyBuf, "%s %sFROM stdin;\n",
1722 fmtCopyColumnList(tbinfo, clistBuf),
1723 (tdinfo->oids && tbinfo->hasoids) ? "WITH OIDS " : "");
1724 copyStmt = copyBuf->data;
1728 /* Restore using INSERT */
1729 dumpFn = dumpTableData_insert;
1734 * Note: although the TableDataInfo is a full DumpableObject, we treat its
1735 * dependency on its table as "special" and pass it to ArchiveEntry now.
1736 * See comments for BuildArchiveDependencies.
1738 ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
1739 tbinfo->dobj.name, tbinfo->dobj.namespace->dobj.name,
1740 NULL, tbinfo->rolname,
1741 false, "TABLE DATA", SECTION_DATA,
1743 &(tbinfo->dobj.dumpId), 1,
1746 destroyPQExpBuffer(copyBuf);
1747 destroyPQExpBuffer(clistBuf);
1751 * refreshMatViewData -
1752 * load or refresh the contents of a single materialized view
1754 * Actually, this just makes an ArchiveEntry for the REFRESH MATERIALIZED VIEW
1758 refreshMatViewData(Archive *fout, TableDataInfo *tdinfo)
1760 TableInfo *tbinfo = tdinfo->tdtable;
1763 /* If the materialized view is not flagged as populated, skip this. */
1764 if (!tbinfo->relispopulated)
1767 q = createPQExpBuffer();
1769 appendPQExpBuffer(q, "REFRESH MATERIALIZED VIEW %s;\n",
1770 fmtId(tbinfo->dobj.name));
1773 tdinfo->dobj.catId, /* catalog ID */
1774 tdinfo->dobj.dumpId, /* dump ID */
1775 tbinfo->dobj.name, /* Name */
1776 tbinfo->dobj.namespace->dobj.name, /* Namespace */
1777 NULL, /* Tablespace */
1778 tbinfo->rolname, /* Owner */
1779 false, /* with oids */
1780 "MATERIALIZED VIEW DATA", /* Desc */
1781 SECTION_POST_DATA, /* Section */
1782 q->data, /* Create */
1785 tdinfo->dobj.dependencies, /* Deps */
1786 tdinfo->dobj.nDeps, /* # Deps */
1788 NULL); /* Dumper Arg */
1790 destroyPQExpBuffer(q);
1795 * set up dumpable objects representing the contents of tables
1798 getTableData(TableInfo *tblinfo, int numTables, bool oids)
1802 for (i = 0; i < numTables; i++)
1804 if (tblinfo[i].dobj.dump)
1805 makeTableDataInfo(&(tblinfo[i]), oids);
1810 * Make a dumpable object for the data of this specific table
1812 * Note: we make a TableDataInfo if and only if we are going to dump the
1813 * table data; the "dump" flag in such objects isn't used.
1816 makeTableDataInfo(TableInfo *tbinfo, bool oids)
1818 TableDataInfo *tdinfo;
1821 * Nothing to do if we already decided to dump the table. This will
1822 * happen for "config" tables.
1824 if (tbinfo->dataObj != NULL)
1827 /* Skip VIEWs (no data to dump) */
1828 if (tbinfo->relkind == RELKIND_VIEW)
1830 /* Skip FOREIGN TABLEs (no data to dump) */
1831 if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
1834 /* Don't dump data in unlogged tables, if so requested */
1835 if (tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED &&
1836 no_unlogged_table_data)
1839 /* Check that the data is not explicitly excluded */
1840 if (simple_oid_list_member(&tabledata_exclude_oids,
1841 tbinfo->dobj.catId.oid))
1844 /* OK, let's dump it */
1845 tdinfo = (TableDataInfo *) pg_malloc(sizeof(TableDataInfo));
1847 if (tbinfo->relkind == RELKIND_MATVIEW)
1848 tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
1850 tdinfo->dobj.objType = DO_TABLE_DATA;
1853 * Note: use tableoid 0 so that this object won't be mistaken for
1854 * something that pg_depend entries apply to.
1856 tdinfo->dobj.catId.tableoid = 0;
1857 tdinfo->dobj.catId.oid = tbinfo->dobj.catId.oid;
1858 AssignDumpId(&tdinfo->dobj);
1859 tdinfo->dobj.name = tbinfo->dobj.name;
1860 tdinfo->dobj.namespace = tbinfo->dobj.namespace;
1861 tdinfo->tdtable = tbinfo;
1862 tdinfo->oids = oids;
1863 tdinfo->filtercond = NULL; /* might get set later */
1864 addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
1866 tbinfo->dataObj = tdinfo;
1870 * The refresh for a materialized view must be dependent on the refresh for
1871 * any materialized view that this one is dependent on.
1873 * This must be called after all the objects are created, but before they are
1877 buildMatViewRefreshDependencies(Archive *fout)
1887 /* No Mat Views before 9.3. */
1888 if (fout->remoteVersion < 90300)
1891 /* Make sure we are in proper schema */
1892 selectSourceSchema(fout, "pg_catalog");
1894 query = createPQExpBuffer();
1896 appendPQExpBuffer(query, "with recursive w as "
1898 "select d1.objid, d2.refobjid, c2.relkind as refrelkind "
1899 "from pg_depend d1 "
1900 "join pg_class c1 on c1.oid = d1.objid "
1901 "and c1.relkind = 'm' "
1902 "join pg_rewrite r1 on r1.ev_class = d1.objid "
1903 "join pg_depend d2 on d2.classid = 'pg_rewrite'::regclass "
1904 "and d2.objid = r1.oid "
1905 "and d2.refobjid <> d1.objid "
1906 "join pg_class c2 on c2.oid = d2.refobjid "
1907 "and c2.relkind in ('m','v') "
1908 "where d1.classid = 'pg_class'::regclass "
1910 "select w.objid, d3.refobjid, c3.relkind "
1912 "join pg_rewrite r3 on r3.ev_class = w.refobjid "
1913 "join pg_depend d3 on d3.classid = 'pg_rewrite'::regclass "
1914 "and d3.objid = r3.oid "
1915 "and d3.refobjid <> w.refobjid "
1916 "join pg_class c3 on c3.oid = d3.refobjid "
1917 "and c3.relkind in ('m','v') "
1919 "select 'pg_class'::regclass::oid as classid, objid, refobjid "
1921 "where refrelkind = 'm'");
1923 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
1925 ntups = PQntuples(res);
1927 i_classid = PQfnumber(res, "classid");
1928 i_objid = PQfnumber(res, "objid");
1929 i_refobjid = PQfnumber(res, "refobjid");
1931 for (i = 0; i < ntups; i++)
1935 DumpableObject *dobj;
1936 DumpableObject *refdobj;
1938 TableInfo *reftbinfo;
1940 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
1941 objId.oid = atooid(PQgetvalue(res, i, i_objid));
1942 refobjId.tableoid = objId.tableoid;
1943 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
1945 dobj = findObjectByCatalogId(objId);
1949 Assert(dobj->objType == DO_TABLE);
1950 tbinfo = (TableInfo *) dobj;
1951 Assert(tbinfo->relkind == RELKIND_MATVIEW);
1952 dobj = (DumpableObject *) tbinfo->dataObj;
1955 Assert(dobj->objType == DO_REFRESH_MATVIEW);
1957 refdobj = findObjectByCatalogId(refobjId);
1958 if (refdobj == NULL)
1961 Assert(refdobj->objType == DO_TABLE);
1962 reftbinfo = (TableInfo *) refdobj;
1963 Assert(reftbinfo->relkind == RELKIND_MATVIEW);
1964 refdobj = (DumpableObject *) reftbinfo->dataObj;
1965 if (refdobj == NULL)
1967 Assert(refdobj->objType == DO_REFRESH_MATVIEW);
1969 addObjectDependency(dobj, refdobj->dumpId);
1971 if (!reftbinfo->relispopulated)
1972 tbinfo->relispopulated = false;
1977 destroyPQExpBuffer(query);
1981 * getTableDataFKConstraints -
1982 * add dump-order dependencies reflecting foreign key constraints
1984 * This code is executed only in a data-only dump --- in schema+data dumps
1985 * we handle foreign key issues by not creating the FK constraints until
1986 * after the data is loaded. In a data-only dump, however, we want to
1987 * order the table data objects in such a way that a table's referenced
1988 * tables are restored first. (In the presence of circular references or
1989 * self-references this may be impossible; we'll detect and complain about
1990 * that during the dependency sorting step.)
1993 getTableDataFKConstraints(void)
1995 DumpableObject **dobjs;
1999 /* Search through all the dumpable objects for FK constraints */
2000 getDumpableObjects(&dobjs, &numObjs);
2001 for (i = 0; i < numObjs; i++)
2003 if (dobjs[i]->objType == DO_FK_CONSTRAINT)
2005 ConstraintInfo *cinfo = (ConstraintInfo *) dobjs[i];
2008 /* Not interesting unless both tables are to be dumped */
2009 if (cinfo->contable == NULL ||
2010 cinfo->contable->dataObj == NULL)
2012 ftable = findTableByOid(cinfo->confrelid);
2013 if (ftable == NULL ||
2014 ftable->dataObj == NULL)
2018 * Okay, make referencing table's TABLE_DATA object depend on the
2019 * referenced table's TABLE_DATA object.
2021 addObjectDependency(&cinfo->contable->dataObj->dobj,
2022 ftable->dataObj->dobj.dumpId);
2030 * guessConstraintInheritance:
2031 * In pre-8.4 databases, we can't tell for certain which constraints
2032 * are inherited. We assume a CHECK constraint is inherited if its name
2033 * matches the name of any constraint in the parent. Originally this code
2034 * tried to compare the expression texts, but that can fail for various
2035 * reasons --- for example, if the parent and child tables are in different
2036 * schemas, reverse-listing of function calls may produce different text
2037 * (schema-qualified or not) depending on search path.
2039 * In 8.4 and up we can rely on the conislocal field to decide which
2040 * constraints must be dumped; much safer.
2042 * This function assumes all conislocal flags were initialized to TRUE.
2043 * It clears the flag on anything that seems to be inherited.
2046 guessConstraintInheritance(TableInfo *tblinfo, int numTables)
2052 for (i = 0; i < numTables; i++)
2054 TableInfo *tbinfo = &(tblinfo[i]);
2056 TableInfo **parents;
2059 /* Sequences and views never have parents */
2060 if (tbinfo->relkind == RELKIND_SEQUENCE ||
2061 tbinfo->relkind == RELKIND_VIEW)
2064 /* Don't bother computing anything for non-target tables, either */
2065 if (!tbinfo->dobj.dump)
2068 numParents = tbinfo->numParents;
2069 parents = tbinfo->parents;
2071 if (numParents == 0)
2072 continue; /* nothing to see here, move along */
2074 /* scan for inherited CHECK constraints */
2075 for (j = 0; j < tbinfo->ncheck; j++)
2077 ConstraintInfo *constr;
2079 constr = &(tbinfo->checkexprs[j]);
2081 for (k = 0; k < numParents; k++)
2085 parent = parents[k];
2086 for (l = 0; l < parent->ncheck; l++)
2088 ConstraintInfo *pconstr = &(parent->checkexprs[l]);
2090 if (strcmp(pconstr->dobj.name, constr->dobj.name) == 0)
2092 constr->conislocal = false;
2096 if (!constr->conislocal)
2106 * dump the database definition
2109 dumpDatabase(Archive *fout)
2111 PQExpBuffer dbQry = createPQExpBuffer();
2112 PQExpBuffer delQry = createPQExpBuffer();
2113 PQExpBuffer creaQry = createPQExpBuffer();
2114 PGconn *conn = GetConnection(fout);
2126 const char *datname,
2134 datname = PQdb(conn);
2137 write_msg(NULL, "saving database definition\n");
2139 /* Make sure we are in proper schema */
2140 selectSourceSchema(fout, "pg_catalog");
2142 /* Get the database owner and parameters from pg_database */
2143 if (fout->remoteVersion >= 80400)
2145 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2146 "(%s datdba) AS dba, "
2147 "pg_encoding_to_char(encoding) AS encoding, "
2148 "datcollate, datctype, datfrozenxid, "
2149 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2150 "shobj_description(oid, 'pg_database') AS description "
2155 appendStringLiteralAH(dbQry, datname, fout);
2157 else if (fout->remoteVersion >= 80200)
2159 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2160 "(%s datdba) AS dba, "
2161 "pg_encoding_to_char(encoding) AS encoding, "
2162 "NULL AS datcollate, NULL AS datctype, datfrozenxid, "
2163 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace, "
2164 "shobj_description(oid, 'pg_database') AS description "
2169 appendStringLiteralAH(dbQry, datname, fout);
2171 else if (fout->remoteVersion >= 80000)
2173 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2174 "(%s datdba) AS dba, "
2175 "pg_encoding_to_char(encoding) AS encoding, "
2176 "NULL AS datcollate, NULL AS datctype, datfrozenxid, "
2177 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = dattablespace) AS tablespace "
2181 appendStringLiteralAH(dbQry, datname, fout);
2183 else if (fout->remoteVersion >= 70100)
2185 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
2186 "(%s datdba) AS dba, "
2187 "pg_encoding_to_char(encoding) AS encoding, "
2188 "NULL AS datcollate, NULL AS datctype, "
2189 "0 AS datfrozenxid, "
2190 "NULL AS tablespace "
2194 appendStringLiteralAH(dbQry, datname, fout);
2198 appendPQExpBuffer(dbQry, "SELECT "
2199 "(SELECT oid FROM pg_class WHERE relname = 'pg_database') AS tableoid, "
2201 "(%s datdba) AS dba, "
2202 "pg_encoding_to_char(encoding) AS encoding, "
2203 "NULL AS datcollate, NULL AS datctype, "
2204 "0 AS datfrozenxid, "
2205 "NULL AS tablespace "
2209 appendStringLiteralAH(dbQry, datname, fout);
2212 res = ExecuteSqlQueryForSingleRow(fout, dbQry->data);
2214 i_tableoid = PQfnumber(res, "tableoid");
2215 i_oid = PQfnumber(res, "oid");
2216 i_dba = PQfnumber(res, "dba");
2217 i_encoding = PQfnumber(res, "encoding");
2218 i_collate = PQfnumber(res, "datcollate");
2219 i_ctype = PQfnumber(res, "datctype");
2220 i_frozenxid = PQfnumber(res, "datfrozenxid");
2221 i_tablespace = PQfnumber(res, "tablespace");
2223 dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
2224 dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
2225 dba = PQgetvalue(res, 0, i_dba);
2226 encoding = PQgetvalue(res, 0, i_encoding);
2227 collate = PQgetvalue(res, 0, i_collate);
2228 ctype = PQgetvalue(res, 0, i_ctype);
2229 frozenxid = atooid(PQgetvalue(res, 0, i_frozenxid));
2230 tablespace = PQgetvalue(res, 0, i_tablespace);
2232 appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
2234 if (strlen(encoding) > 0)
2236 appendPQExpBuffer(creaQry, " ENCODING = ");
2237 appendStringLiteralAH(creaQry, encoding, fout);
2239 if (strlen(collate) > 0)
2241 appendPQExpBuffer(creaQry, " LC_COLLATE = ");
2242 appendStringLiteralAH(creaQry, collate, fout);
2244 if (strlen(ctype) > 0)
2246 appendPQExpBuffer(creaQry, " LC_CTYPE = ");
2247 appendStringLiteralAH(creaQry, ctype, fout);
2249 if (strlen(tablespace) > 0 && strcmp(tablespace, "pg_default") != 0)
2250 appendPQExpBuffer(creaQry, " TABLESPACE = %s",
2252 appendPQExpBuffer(creaQry, ";\n");
2256 appendPQExpBuffer(creaQry, "\n-- For binary upgrade, set datfrozenxid.\n");
2257 appendPQExpBuffer(creaQry, "UPDATE pg_catalog.pg_database\n"
2258 "SET datfrozenxid = '%u'\n"
2261 appendStringLiteralAH(creaQry, datname, fout);
2262 appendPQExpBuffer(creaQry, ";\n");
2266 appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
2269 dbDumpId = createDumpId();
2272 dbCatId, /* catalog ID */
2273 dbDumpId, /* dump ID */
2275 NULL, /* Namespace */
2276 NULL, /* Tablespace */
2278 false, /* with oids */
2279 "DATABASE", /* Desc */
2280 SECTION_PRE_DATA, /* Section */
2281 creaQry->data, /* Create */
2282 delQry->data, /* Del */
2287 NULL); /* Dumper Arg */
2290 * pg_largeobject and pg_largeobject_metadata come from the old system
2291 * intact, so set their relfrozenxids.
2296 PQExpBuffer loFrozenQry = createPQExpBuffer();
2297 PQExpBuffer loOutQry = createPQExpBuffer();
2303 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid\n"
2304 "FROM pg_catalog.pg_class\n"
2305 "WHERE oid = %u;\n",
2306 LargeObjectRelationId);
2308 lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2310 i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2312 appendPQExpBuffer(loOutQry, "\n-- For binary upgrade, set pg_largeobject.relfrozenxid\n");
2313 appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2314 "SET relfrozenxid = '%u'\n"
2315 "WHERE oid = %u;\n",
2316 atoi(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2317 LargeObjectRelationId);
2318 ArchiveEntry(fout, nilCatalogId, createDumpId(),
2319 "pg_largeobject", NULL, NULL, "",
2320 false, "pg_largeobject", SECTION_PRE_DATA,
2321 loOutQry->data, "", NULL,
2328 * pg_largeobject_metadata
2330 if (fout->remoteVersion >= 90000)
2332 resetPQExpBuffer(loFrozenQry);
2333 resetPQExpBuffer(loOutQry);
2335 appendPQExpBuffer(loFrozenQry, "SELECT relfrozenxid\n"
2336 "FROM pg_catalog.pg_class\n"
2337 "WHERE oid = %u;\n",
2338 LargeObjectMetadataRelationId);
2340 lo_res = ExecuteSqlQueryForSingleRow(fout, loFrozenQry->data);
2342 i_relfrozenxid = PQfnumber(lo_res, "relfrozenxid");
2344 appendPQExpBuffer(loOutQry, "\n-- For binary upgrade, set pg_largeobject_metadata.relfrozenxid\n");
2345 appendPQExpBuffer(loOutQry, "UPDATE pg_catalog.pg_class\n"
2346 "SET relfrozenxid = '%u'\n"
2347 "WHERE oid = %u;\n",
2348 atoi(PQgetvalue(lo_res, 0, i_relfrozenxid)),
2349 LargeObjectMetadataRelationId);
2350 ArchiveEntry(fout, nilCatalogId, createDumpId(),
2351 "pg_largeobject_metadata", NULL, NULL, "",
2352 false, "pg_largeobject_metadata", SECTION_PRE_DATA,
2353 loOutQry->data, "", NULL,
2360 destroyPQExpBuffer(loFrozenQry);
2361 destroyPQExpBuffer(loOutQry);
2364 /* Dump DB comment if any */
2365 if (fout->remoteVersion >= 80200)
2368 * 8.2 keeps comments on shared objects in a shared table, so we
2369 * cannot use the dumpComment used for other database objects.
2371 char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
2373 if (comment && strlen(comment))
2375 resetPQExpBuffer(dbQry);
2378 * Generates warning when loaded into a differently-named
2381 appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", fmtId(datname));
2382 appendStringLiteralAH(dbQry, comment, fout);
2383 appendPQExpBuffer(dbQry, ";\n");
2385 ArchiveEntry(fout, dbCatId, createDumpId(), datname, NULL, NULL,
2386 dba, false, "COMMENT", SECTION_NONE,
2387 dbQry->data, "", NULL,
2388 &dbDumpId, 1, NULL, NULL);
2393 resetPQExpBuffer(dbQry);
2394 appendPQExpBuffer(dbQry, "DATABASE %s", fmtId(datname));
2395 dumpComment(fout, dbQry->data, NULL, "",
2396 dbCatId, 0, dbDumpId);
2401 /* Dump shared security label. */
2402 if (!no_security_labels && fout->remoteVersion >= 90200)
2404 PQExpBuffer seclabelQry = createPQExpBuffer();
2406 buildShSecLabelQuery(conn, "pg_database", dbCatId.oid, seclabelQry);
2407 res = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
2408 resetPQExpBuffer(seclabelQry);
2409 emitShSecLabels(conn, res, seclabelQry, "DATABASE", datname);
2410 if (strlen(seclabelQry->data))
2411 ArchiveEntry(fout, dbCatId, createDumpId(), datname, NULL, NULL,
2412 dba, false, "SECURITY LABEL", SECTION_NONE,
2413 seclabelQry->data, "", NULL,
2414 &dbDumpId, 1, NULL, NULL);
2415 destroyPQExpBuffer(seclabelQry);
2418 destroyPQExpBuffer(dbQry);
2419 destroyPQExpBuffer(delQry);
2420 destroyPQExpBuffer(creaQry);
2425 * dumpEncoding: put the correct encoding into the archive
2428 dumpEncoding(Archive *AH)
2430 const char *encname = pg_encoding_to_char(AH->encoding);
2431 PQExpBuffer qry = createPQExpBuffer();
2434 write_msg(NULL, "saving encoding = %s\n", encname);
2436 appendPQExpBuffer(qry, "SET client_encoding = ");
2437 appendStringLiteralAH(qry, encname, AH);
2438 appendPQExpBuffer(qry, ";\n");
2440 ArchiveEntry(AH, nilCatalogId, createDumpId(),
2441 "ENCODING", NULL, NULL, "",
2442 false, "ENCODING", SECTION_PRE_DATA,
2443 qry->data, "", NULL,
2447 destroyPQExpBuffer(qry);
2452 * dumpStdStrings: put the correct escape string behavior into the archive
2455 dumpStdStrings(Archive *AH)
2457 const char *stdstrings = AH->std_strings ? "on" : "off";
2458 PQExpBuffer qry = createPQExpBuffer();
2461 write_msg(NULL, "saving standard_conforming_strings = %s\n",
2464 appendPQExpBuffer(qry, "SET standard_conforming_strings = '%s';\n",
2467 ArchiveEntry(AH, nilCatalogId, createDumpId(),
2468 "STDSTRINGS", NULL, NULL, "",
2469 false, "STDSTRINGS", SECTION_PRE_DATA,
2470 qry->data, "", NULL,
2474 destroyPQExpBuffer(qry);
2480 * Collect schema-level data about large objects
2483 getBlobs(Archive *fout)
2485 PQExpBuffer blobQry = createPQExpBuffer();
2487 DumpableObject *bdata;
2492 /* Verbose message */
2494 write_msg(NULL, "reading large objects\n");
2496 /* Make sure we are in proper schema */
2497 selectSourceSchema(fout, "pg_catalog");
2499 /* Fetch BLOB OIDs, and owner/ACL data if >= 9.0 */
2500 if (fout->remoteVersion >= 90000)
2501 appendPQExpBuffer(blobQry,
2502 "SELECT oid, (%s lomowner) AS rolname, lomacl"
2503 " FROM pg_largeobject_metadata",
2505 else if (fout->remoteVersion >= 70100)
2506 appendPQExpBuffer(blobQry,
2507 "SELECT DISTINCT loid, NULL::oid, NULL::oid"
2508 " FROM pg_largeobject");
2510 appendPQExpBuffer(blobQry,
2511 "SELECT oid, NULL::oid, NULL::oid"
2512 " FROM pg_class WHERE relkind = 'l'");
2514 res = ExecuteSqlQuery(fout, blobQry->data, PGRES_TUPLES_OK);
2516 ntups = PQntuples(res);
2520 * Each large object has its own BLOB archive entry.
2522 binfo = (BlobInfo *) pg_malloc(ntups * sizeof(BlobInfo));
2524 for (i = 0; i < ntups; i++)
2526 binfo[i].dobj.objType = DO_BLOB;
2527 binfo[i].dobj.catId.tableoid = LargeObjectRelationId;
2528 binfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, 0));
2529 AssignDumpId(&binfo[i].dobj);
2531 binfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, 0));
2532 if (!PQgetisnull(res, i, 1))
2533 binfo[i].rolname = pg_strdup(PQgetvalue(res, i, 1));
2535 binfo[i].rolname = "";
2536 if (!PQgetisnull(res, i, 2))
2537 binfo[i].blobacl = pg_strdup(PQgetvalue(res, i, 2));
2539 binfo[i].blobacl = NULL;
2543 * If we have any large objects, a "BLOBS" archive entry is needed.
2544 * This is just a placeholder for sorting; it carries no data now.
2546 bdata = (DumpableObject *) pg_malloc(sizeof(DumpableObject));
2547 bdata->objType = DO_BLOB_DATA;
2548 bdata->catId = nilCatalogId;
2549 AssignDumpId(bdata);
2550 bdata->name = pg_strdup("BLOBS");
2554 destroyPQExpBuffer(blobQry);
2560 * dump the definition (metadata) of the given large object
2563 dumpBlob(Archive *fout, BlobInfo *binfo)
2565 PQExpBuffer cquery = createPQExpBuffer();
2566 PQExpBuffer dquery = createPQExpBuffer();
2568 appendPQExpBuffer(cquery,
2569 "SELECT pg_catalog.lo_create('%s');\n",
2572 appendPQExpBuffer(dquery,
2573 "SELECT pg_catalog.lo_unlink('%s');\n",
2576 ArchiveEntry(fout, binfo->dobj.catId, binfo->dobj.dumpId,
2579 binfo->rolname, false,
2580 "BLOB", SECTION_PRE_DATA,
2581 cquery->data, dquery->data, NULL,
2585 /* set up tag for comment and/or ACL */
2586 resetPQExpBuffer(cquery);
2587 appendPQExpBuffer(cquery, "LARGE OBJECT %s", binfo->dobj.name);
2589 /* Dump comment if any */
2590 dumpComment(fout, cquery->data,
2591 NULL, binfo->rolname,
2592 binfo->dobj.catId, 0, binfo->dobj.dumpId);
2594 /* Dump security label if any */
2595 dumpSecLabel(fout, cquery->data,
2596 NULL, binfo->rolname,
2597 binfo->dobj.catId, 0, binfo->dobj.dumpId);
2599 /* Dump ACL if any */
2601 dumpACL(fout, binfo->dobj.catId, binfo->dobj.dumpId, "LARGE OBJECT",
2602 binfo->dobj.name, NULL, cquery->data,
2603 NULL, binfo->rolname, binfo->blobacl);
2605 destroyPQExpBuffer(cquery);
2606 destroyPQExpBuffer(dquery);
2611 * dump the data contents of all large objects
2614 dumpBlobs(Archive *fout, void *arg)
2616 const char *blobQry;
2617 const char *blobFetchQry;
2618 PGconn *conn = GetConnection(fout);
2620 char buf[LOBBUFSIZE];
2626 write_msg(NULL, "saving large objects\n");
2628 /* Make sure we are in proper schema */
2629 selectSourceSchema(fout, "pg_catalog");
2632 * Currently, we re-fetch all BLOB OIDs using a cursor. Consider scanning
2633 * the already-in-memory dumpable objects instead...
2635 if (fout->remoteVersion >= 90000)
2636 blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_largeobject_metadata";
2637 else if (fout->remoteVersion >= 70100)
2638 blobQry = "DECLARE bloboid CURSOR FOR SELECT DISTINCT loid FROM pg_largeobject";
2640 blobQry = "DECLARE bloboid CURSOR FOR SELECT oid FROM pg_class WHERE relkind = 'l'";
2642 ExecuteSqlStatement(fout, blobQry);
2644 /* Command to fetch from cursor */
2645 blobFetchQry = "FETCH 1000 IN bloboid";
2650 res = ExecuteSqlQuery(fout, blobFetchQry, PGRES_TUPLES_OK);
2652 /* Process the tuples, if any */
2653 ntups = PQntuples(res);
2654 for (i = 0; i < ntups; i++)
2659 blobOid = atooid(PQgetvalue(res, i, 0));
2661 loFd = lo_open(conn, blobOid, INV_READ);
2663 exit_horribly(NULL, "could not open large object %u: %s",
2664 blobOid, PQerrorMessage(conn));
2666 StartBlob(fout, blobOid);
2668 /* Now read it in chunks, sending data to archive */
2671 cnt = lo_read(conn, loFd, buf, LOBBUFSIZE);
2673 exit_horribly(NULL, "error reading large object %u: %s",
2674 blobOid, PQerrorMessage(conn));
2676 WriteData(fout, buf, cnt);
2679 lo_close(conn, loFd);
2681 EndBlob(fout, blobOid);
2685 } while (ntups > 0);
2691 binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
2692 PQExpBuffer upgrade_buffer,
2695 PQExpBuffer upgrade_query = createPQExpBuffer();
2696 PGresult *upgrade_res;
2697 Oid pg_type_array_oid;
2699 appendPQExpBuffer(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type oid\n");
2700 appendPQExpBuffer(upgrade_buffer,
2701 "SELECT binary_upgrade.set_next_pg_type_oid('%u'::pg_catalog.oid);\n\n",
2704 /* we only support old >= 8.3 for binary upgrades */
2705 appendPQExpBuffer(upgrade_query,
2707 "FROM pg_catalog.pg_type "
2708 "WHERE pg_type.oid = '%u'::pg_catalog.oid;",
2711 upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
2713 pg_type_array_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "typarray")));
2715 if (OidIsValid(pg_type_array_oid))
2717 appendPQExpBuffer(upgrade_buffer,
2718 "\n-- For binary upgrade, must preserve pg_type array oid\n");
2719 appendPQExpBuffer(upgrade_buffer,
2720 "SELECT binary_upgrade.set_next_array_pg_type_oid('%u'::pg_catalog.oid);\n\n",
2724 PQclear(upgrade_res);
2725 destroyPQExpBuffer(upgrade_query);
2729 binary_upgrade_set_type_oids_by_rel_oid(Archive *fout,
2730 PQExpBuffer upgrade_buffer,
2733 PQExpBuffer upgrade_query = createPQExpBuffer();
2734 PGresult *upgrade_res;
2736 bool toast_set = false;
2738 /* we only support old >= 8.3 for binary upgrades */
2739 appendPQExpBuffer(upgrade_query,
2740 "SELECT c.reltype AS crel, t.reltype AS trel "
2741 "FROM pg_catalog.pg_class c "
2742 "LEFT JOIN pg_catalog.pg_class t ON "
2743 " (c.reltoastrelid = t.oid) "
2744 "WHERE c.oid = '%u'::pg_catalog.oid;",
2747 upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
2749 pg_type_oid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "crel")));
2751 binary_upgrade_set_type_oids_by_type_oid(fout, upgrade_buffer,
2754 if (!PQgetisnull(upgrade_res, 0, PQfnumber(upgrade_res, "trel")))
2756 /* Toast tables do not have pg_type array rows */
2757 Oid pg_type_toast_oid = atooid(PQgetvalue(upgrade_res, 0,
2758 PQfnumber(upgrade_res, "trel")));
2760 appendPQExpBuffer(upgrade_buffer, "\n-- For binary upgrade, must preserve pg_type toast oid\n");
2761 appendPQExpBuffer(upgrade_buffer,
2762 "SELECT binary_upgrade.set_next_toast_pg_type_oid('%u'::pg_catalog.oid);\n\n",
2768 PQclear(upgrade_res);
2769 destroyPQExpBuffer(upgrade_query);
2775 binary_upgrade_set_pg_class_oids(Archive *fout,
2776 PQExpBuffer upgrade_buffer, Oid pg_class_oid,
2779 PQExpBuffer upgrade_query = createPQExpBuffer();
2780 PGresult *upgrade_res;
2781 Oid pg_class_reltoastrelid;
2782 Oid pg_index_indexrelid;
2784 appendPQExpBuffer(upgrade_query,
2785 "SELECT c.reltoastrelid, i.indexrelid "
2786 "FROM pg_catalog.pg_class c LEFT JOIN "
2787 "pg_catalog.pg_index i ON (c.reltoastrelid = i.indrelid AND i.indisvalid) "
2788 "WHERE c.oid = '%u'::pg_catalog.oid;",
2791 upgrade_res = ExecuteSqlQueryForSingleRow(fout, upgrade_query->data);
2793 pg_class_reltoastrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "reltoastrelid")));
2794 pg_index_indexrelid = atooid(PQgetvalue(upgrade_res, 0, PQfnumber(upgrade_res, "indexrelid")));
2796 appendPQExpBuffer(upgrade_buffer,
2797 "\n-- For binary upgrade, must preserve pg_class oids\n");
2801 appendPQExpBuffer(upgrade_buffer,
2802 "SELECT binary_upgrade.set_next_heap_pg_class_oid('%u'::pg_catalog.oid);\n",
2804 /* only tables have toast tables, not indexes */
2805 if (OidIsValid(pg_class_reltoastrelid))
2808 * One complexity is that the table definition might not require
2809 * the creation of a TOAST table, and the TOAST table might have
2810 * been created long after table creation, when the table was
2811 * loaded with wide data. By setting the TOAST oid we force
2812 * creation of the TOAST heap and TOAST index by the backend so we
2813 * can cleanly copy the files during binary upgrade.
2816 appendPQExpBuffer(upgrade_buffer,
2817 "SELECT binary_upgrade.set_next_toast_pg_class_oid('%u'::pg_catalog.oid);\n",
2818 pg_class_reltoastrelid);
2820 /* every toast table has an index */
2821 appendPQExpBuffer(upgrade_buffer,
2822 "SELECT binary_upgrade.set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
2823 pg_index_indexrelid);
2827 appendPQExpBuffer(upgrade_buffer,
2828 "SELECT binary_upgrade.set_next_index_pg_class_oid('%u'::pg_catalog.oid);\n",
2831 appendPQExpBuffer(upgrade_buffer, "\n");
2833 PQclear(upgrade_res);
2834 destroyPQExpBuffer(upgrade_query);
2838 * If the DumpableObject is a member of an extension, add a suitable
2839 * ALTER EXTENSION ADD command to the creation commands in upgrade_buffer.
2842 binary_upgrade_extension_member(PQExpBuffer upgrade_buffer,
2843 DumpableObject *dobj,
2844 const char *objlabel)
2846 DumpableObject *extobj = NULL;
2849 if (!dobj->ext_member)
2853 * Find the parent extension. We could avoid this search if we wanted to
2854 * add a link field to DumpableObject, but the space costs of that would
2855 * be considerable. We assume that member objects could only have a
2856 * direct dependency on their own extension, not any others.
2858 for (i = 0; i < dobj->nDeps; i++)
2860 extobj = findObjectByDumpId(dobj->dependencies[i]);
2861 if (extobj && extobj->objType == DO_EXTENSION)
2866 exit_horribly(NULL, "could not find parent extension for %s\n", objlabel);
2868 appendPQExpBuffer(upgrade_buffer,
2869 "\n-- For binary upgrade, handle extension membership the hard way\n");
2870 appendPQExpBuffer(upgrade_buffer, "ALTER EXTENSION %s ADD %s;\n",
2871 fmtId(extobj->name),
2877 * read all namespaces in the system catalogs and return them in the
2878 * NamespaceInfo* structure
2880 * numNamespaces is set to the number of namespaces read in
2883 getNamespaces(Archive *fout, int *numNamespaces)
2889 NamespaceInfo *nsinfo;
2897 * Before 7.3, there are no real namespaces; create two dummy entries, one
2898 * for user stuff and one for system stuff.
2900 if (fout->remoteVersion < 70300)
2902 nsinfo = (NamespaceInfo *) pg_malloc(2 * sizeof(NamespaceInfo));
2904 nsinfo[0].dobj.objType = DO_NAMESPACE;
2905 nsinfo[0].dobj.catId.tableoid = 0;
2906 nsinfo[0].dobj.catId.oid = 0;
2907 AssignDumpId(&nsinfo[0].dobj);
2908 nsinfo[0].dobj.name = pg_strdup("public");
2909 nsinfo[0].rolname = pg_strdup("");
2910 nsinfo[0].nspacl = pg_strdup("");
2912 selectDumpableNamespace(&nsinfo[0]);
2914 nsinfo[1].dobj.objType = DO_NAMESPACE;
2915 nsinfo[1].dobj.catId.tableoid = 0;
2916 nsinfo[1].dobj.catId.oid = 1;
2917 AssignDumpId(&nsinfo[1].dobj);
2918 nsinfo[1].dobj.name = pg_strdup("pg_catalog");
2919 nsinfo[1].rolname = pg_strdup("");
2920 nsinfo[1].nspacl = pg_strdup("");
2922 selectDumpableNamespace(&nsinfo[1]);
2929 query = createPQExpBuffer();
2931 /* Make sure we are in proper schema */
2932 selectSourceSchema(fout, "pg_catalog");
2935 * we fetch all namespaces including system ones, so that every object we
2936 * read in can be linked to a containing namespace.
2938 appendPQExpBuffer(query, "SELECT tableoid, oid, nspname, "
2939 "(%s nspowner) AS rolname, "
2940 "nspacl FROM pg_namespace",
2943 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
2945 ntups = PQntuples(res);
2947 nsinfo = (NamespaceInfo *) pg_malloc(ntups * sizeof(NamespaceInfo));
2949 i_tableoid = PQfnumber(res, "tableoid");
2950 i_oid = PQfnumber(res, "oid");
2951 i_nspname = PQfnumber(res, "nspname");
2952 i_rolname = PQfnumber(res, "rolname");
2953 i_nspacl = PQfnumber(res, "nspacl");
2955 for (i = 0; i < ntups; i++)
2957 nsinfo[i].dobj.objType = DO_NAMESPACE;
2958 nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
2959 nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
2960 AssignDumpId(&nsinfo[i].dobj);
2961 nsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_nspname));
2962 nsinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
2963 nsinfo[i].nspacl = pg_strdup(PQgetvalue(res, i, i_nspacl));
2965 /* Decide whether to dump this namespace */
2966 selectDumpableNamespace(&nsinfo[i]);
2968 if (strlen(nsinfo[i].rolname) == 0)
2969 write_msg(NULL, "WARNING: owner of schema \"%s\" appears to be invalid\n",
2970 nsinfo[i].dobj.name);
2974 destroyPQExpBuffer(query);
2976 *numNamespaces = ntups;
2983 * given a namespace OID and an object OID, look up the info read by
2986 * NB: for pre-7.3 source database, we use object OID to guess whether it's
2987 * a system object or not. In 7.3 and later there is no guessing, and we
2988 * don't use objoid at all.
2990 static NamespaceInfo *
2991 findNamespace(Archive *fout, Oid nsoid, Oid objoid)
2993 NamespaceInfo *nsinfo;
2995 if (fout->remoteVersion >= 70300)
2997 nsinfo = findNamespaceByOid(nsoid);
3001 /* This code depends on the dummy objects set up by getNamespaces. */
3004 if (objoid > g_last_builtin_oid)
3005 i = 0; /* user object */
3007 i = 1; /* system object */
3008 nsinfo = findNamespaceByOid(i);
3012 exit_horribly(NULL, "schema with OID %u does not exist\n", nsoid);
3019 * read all extensions in the system catalogs and return them in the
3020 * ExtensionInfo* structure
3022 * numExtensions is set to the number of extensions read in
3025 getExtensions(Archive *fout, int *numExtensions)
3031 ExtensionInfo *extinfo;
3036 int i_extrelocatable;
3042 * Before 9.1, there are no extensions.
3044 if (fout->remoteVersion < 90100)
3050 query = createPQExpBuffer();
3052 /* Make sure we are in proper schema */
3053 selectSourceSchema(fout, "pg_catalog");
3055 appendPQExpBuffer(query, "SELECT x.tableoid, x.oid, "
3056 "x.extname, n.nspname, x.extrelocatable, x.extversion, x.extconfig, x.extcondition "
3057 "FROM pg_extension x "
3058 "JOIN pg_namespace n ON n.oid = x.extnamespace");
3060 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3062 ntups = PQntuples(res);
3064 extinfo = (ExtensionInfo *) pg_malloc(ntups * sizeof(ExtensionInfo));
3066 i_tableoid = PQfnumber(res, "tableoid");
3067 i_oid = PQfnumber(res, "oid");
3068 i_extname = PQfnumber(res, "extname");
3069 i_nspname = PQfnumber(res, "nspname");
3070 i_extrelocatable = PQfnumber(res, "extrelocatable");
3071 i_extversion = PQfnumber(res, "extversion");
3072 i_extconfig = PQfnumber(res, "extconfig");
3073 i_extcondition = PQfnumber(res, "extcondition");
3075 for (i = 0; i < ntups; i++)
3077 extinfo[i].dobj.objType = DO_EXTENSION;
3078 extinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3079 extinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3080 AssignDumpId(&extinfo[i].dobj);
3081 extinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_extname));
3082 extinfo[i].namespace = pg_strdup(PQgetvalue(res, i, i_nspname));
3083 extinfo[i].relocatable = *(PQgetvalue(res, i, i_extrelocatable)) == 't';
3084 extinfo[i].extversion = pg_strdup(PQgetvalue(res, i, i_extversion));
3085 extinfo[i].extconfig = pg_strdup(PQgetvalue(res, i, i_extconfig));
3086 extinfo[i].extcondition = pg_strdup(PQgetvalue(res, i, i_extcondition));
3088 /* Decide whether we want to dump it */
3089 selectDumpableExtension(&(extinfo[i]));
3093 destroyPQExpBuffer(query);
3095 *numExtensions = ntups;
3102 * read all types in the system catalogs and return them in the
3103 * TypeInfo* structure
3105 * numTypes is set to the number of types read in
3107 * NB: this must run after getFuncs() because we assume we can do
3111 getTypes(Archive *fout, int *numTypes)
3116 PQExpBuffer query = createPQExpBuffer();
3118 ShellTypeInfo *stinfo;
3135 * we include even the built-in types because those may be used as array
3136 * elements by user-defined types
3138 * we filter out the built-in types when we dump out the types
3140 * same approach for undefined (shell) types and array types
3142 * Note: as of 8.3 we can reliably detect whether a type is an
3143 * auto-generated array type by checking the element type's typarray.
3144 * (Before that the test is capable of generating false positives.) We
3145 * still check for name beginning with '_', though, so as to avoid the
3146 * cost of the subselect probe for all standard types. This would have to
3147 * be revisited if the backend ever allows renaming of array types.
3150 /* Make sure we are in proper schema */
3151 selectSourceSchema(fout, "pg_catalog");
3153 if (fout->remoteVersion >= 90200)
3155 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3156 "typnamespace, typacl, "
3157 "(%s typowner) AS rolname, "
3158 "typinput::oid AS typinput, "
3159 "typoutput::oid AS typoutput, typelem, typrelid, "
3160 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
3161 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
3162 "typtype, typisdefined, "
3163 "typname[0] = '_' AND typelem != 0 AND "
3164 "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
3168 else if (fout->remoteVersion >= 80300)
3170 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3171 "typnamespace, '{=U}' AS typacl, "
3172 "(%s typowner) AS rolname, "
3173 "typinput::oid AS typinput, "
3174 "typoutput::oid AS typoutput, typelem, typrelid, "
3175 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
3176 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
3177 "typtype, typisdefined, "
3178 "typname[0] = '_' AND typelem != 0 AND "
3179 "(SELECT typarray FROM pg_type te WHERE oid = pg_type.typelem) = oid AS isarray "
3183 else if (fout->remoteVersion >= 70300)
3185 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3186 "typnamespace, '{=U}' AS typacl, "
3187 "(%s typowner) AS rolname, "
3188 "typinput::oid AS typinput, "
3189 "typoutput::oid AS typoutput, typelem, typrelid, "
3190 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
3191 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
3192 "typtype, typisdefined, "
3193 "typname[0] = '_' AND typelem != 0 AS isarray "
3197 else if (fout->remoteVersion >= 70100)
3199 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
3200 "0::oid AS typnamespace, '{=U}' AS typacl, "
3201 "(%s typowner) AS rolname, "
3202 "typinput::oid AS typinput, "
3203 "typoutput::oid AS typoutput, typelem, typrelid, "
3204 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
3205 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
3206 "typtype, typisdefined, "
3207 "typname[0] = '_' AND typelem != 0 AS isarray "
3213 appendPQExpBuffer(query, "SELECT "
3214 "(SELECT oid FROM pg_class WHERE relname = 'pg_type') AS tableoid, "
3216 "0::oid AS typnamespace, '{=U}' AS typacl, "
3217 "(%s typowner) AS rolname, "
3218 "typinput::oid AS typinput, "
3219 "typoutput::oid AS typoutput, typelem, typrelid, "
3220 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
3221 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END AS typrelkind, "
3222 "typtype, typisdefined, "
3223 "typname[0] = '_' AND typelem != 0 AS isarray "
3228 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3230 ntups = PQntuples(res);
3232 tyinfo = (TypeInfo *) pg_malloc(ntups * sizeof(TypeInfo));
3234 i_tableoid = PQfnumber(res, "tableoid");
3235 i_oid = PQfnumber(res, "oid");
3236 i_typname = PQfnumber(res, "typname");
3237 i_typnamespace = PQfnumber(res, "typnamespace");
3238 i_typacl = PQfnumber(res, "typacl");
3239 i_rolname = PQfnumber(res, "rolname");
3240 i_typinput = PQfnumber(res, "typinput");
3241 i_typoutput = PQfnumber(res, "typoutput");
3242 i_typelem = PQfnumber(res, "typelem");
3243 i_typrelid = PQfnumber(res, "typrelid");
3244 i_typrelkind = PQfnumber(res, "typrelkind");
3245 i_typtype = PQfnumber(res, "typtype");
3246 i_typisdefined = PQfnumber(res, "typisdefined");
3247 i_isarray = PQfnumber(res, "isarray");
3249 for (i = 0; i < ntups; i++)
3251 tyinfo[i].dobj.objType = DO_TYPE;
3252 tyinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3253 tyinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3254 AssignDumpId(&tyinfo[i].dobj);
3255 tyinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_typname));
3256 tyinfo[i].dobj.namespace =
3258 atooid(PQgetvalue(res, i, i_typnamespace)),
3259 tyinfo[i].dobj.catId.oid);
3260 tyinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3261 tyinfo[i].typacl = pg_strdup(PQgetvalue(res, i, i_typacl));
3262 tyinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
3263 tyinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
3264 tyinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
3265 tyinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
3266 tyinfo[i].shellType = NULL;
3268 if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
3269 tyinfo[i].isDefined = true;
3271 tyinfo[i].isDefined = false;
3273 if (strcmp(PQgetvalue(res, i, i_isarray), "t") == 0)
3274 tyinfo[i].isArray = true;
3276 tyinfo[i].isArray = false;
3278 /* Decide whether we want to dump it */
3279 selectDumpableType(&tyinfo[i]);
3282 * If it's a domain, fetch info about its constraints, if any
3284 tyinfo[i].nDomChecks = 0;
3285 tyinfo[i].domChecks = NULL;
3286 if (tyinfo[i].dobj.dump && tyinfo[i].typtype == TYPTYPE_DOMAIN)
3287 getDomainConstraints(fout, &(tyinfo[i]));
3290 * If it's a base type, make a DumpableObject representing a shell
3291 * definition of the type. We will need to dump that ahead of the I/O
3292 * functions for the type. Similarly, range types need a shell
3293 * definition in case they have a canonicalize function.
3295 * Note: the shell type doesn't have a catId. You might think it
3296 * should copy the base type's catId, but then it might capture the
3297 * pg_depend entries for the type, which we don't want.
3299 if (tyinfo[i].dobj.dump && (tyinfo[i].typtype == TYPTYPE_BASE ||
3300 tyinfo[i].typtype == TYPTYPE_RANGE))
3302 stinfo = (ShellTypeInfo *) pg_malloc(sizeof(ShellTypeInfo));
3303 stinfo->dobj.objType = DO_SHELL_TYPE;
3304 stinfo->dobj.catId = nilCatalogId;
3305 AssignDumpId(&stinfo->dobj);
3306 stinfo->dobj.name = pg_strdup(tyinfo[i].dobj.name);
3307 stinfo->dobj.namespace = tyinfo[i].dobj.namespace;
3308 stinfo->baseType = &(tyinfo[i]);
3309 tyinfo[i].shellType = stinfo;
3312 * Initially mark the shell type as not to be dumped. We'll only
3313 * dump it if the I/O or canonicalize functions need to be dumped;
3314 * this is taken care of while sorting dependencies.
3316 stinfo->dobj.dump = false;
3319 * However, if dumping from pre-7.3, there will be no dependency
3320 * info so we have to fake it here. We only need to worry about
3321 * typinput and typoutput since the other functions only exist
3324 if (fout->remoteVersion < 70300)
3330 typinput = atooid(PQgetvalue(res, i, i_typinput));
3331 typoutput = atooid(PQgetvalue(res, i, i_typoutput));
3333 funcInfo = findFuncByOid(typinput);
3334 if (funcInfo && funcInfo->dobj.dump)
3336 /* base type depends on function */
3337 addObjectDependency(&tyinfo[i].dobj,
3338 funcInfo->dobj.dumpId);
3339 /* function depends on shell type */
3340 addObjectDependency(&funcInfo->dobj,
3341 stinfo->dobj.dumpId);
3342 /* mark shell type as to be dumped */
3343 stinfo->dobj.dump = true;
3346 funcInfo = findFuncByOid(typoutput);
3347 if (funcInfo && funcInfo->dobj.dump)
3349 /* base type depends on function */
3350 addObjectDependency(&tyinfo[i].dobj,
3351 funcInfo->dobj.dumpId);
3352 /* function depends on shell type */
3353 addObjectDependency(&funcInfo->dobj,
3354 stinfo->dobj.dumpId);
3355 /* mark shell type as to be dumped */
3356 stinfo->dobj.dump = true;
3361 if (strlen(tyinfo[i].rolname) == 0 && tyinfo[i].isDefined)
3362 write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",
3363 tyinfo[i].dobj.name);
3370 destroyPQExpBuffer(query);
3377 * read all operators in the system catalogs and return them in the
3378 * OprInfo* structure
3380 * numOprs is set to the number of operators read in
3383 getOperators(Archive *fout, int *numOprs)
3388 PQExpBuffer query = createPQExpBuffer();
3399 * find all operators, including builtin operators; we filter out
3400 * system-defined operators at dump-out time.
3403 /* Make sure we are in proper schema */
3404 selectSourceSchema(fout, "pg_catalog");
3406 if (fout->remoteVersion >= 70300)
3408 appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
3410 "(%s oprowner) AS rolname, "
3412 "oprcode::oid AS oprcode "
3416 else if (fout->remoteVersion >= 70100)
3418 appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
3419 "0::oid AS oprnamespace, "
3420 "(%s oprowner) AS rolname, "
3422 "oprcode::oid AS oprcode "
3428 appendPQExpBuffer(query, "SELECT "
3429 "(SELECT oid FROM pg_class WHERE relname = 'pg_operator') AS tableoid, "
3431 "0::oid AS oprnamespace, "
3432 "(%s oprowner) AS rolname, "
3434 "oprcode::oid AS oprcode "
3439 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3441 ntups = PQntuples(res);
3444 oprinfo = (OprInfo *) pg_malloc(ntups * sizeof(OprInfo));
3446 i_tableoid = PQfnumber(res, "tableoid");
3447 i_oid = PQfnumber(res, "oid");
3448 i_oprname = PQfnumber(res, "oprname");
3449 i_oprnamespace = PQfnumber(res, "oprnamespace");
3450 i_rolname = PQfnumber(res, "rolname");
3451 i_oprkind = PQfnumber(res, "oprkind");
3452 i_oprcode = PQfnumber(res, "oprcode");
3454 for (i = 0; i < ntups; i++)
3456 oprinfo[i].dobj.objType = DO_OPERATOR;
3457 oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3458 oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3459 AssignDumpId(&oprinfo[i].dobj);
3460 oprinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_oprname));
3461 oprinfo[i].dobj.namespace =
3463 atooid(PQgetvalue(res, i, i_oprnamespace)),
3464 oprinfo[i].dobj.catId.oid);
3465 oprinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3466 oprinfo[i].oprkind = (PQgetvalue(res, i, i_oprkind))[0];
3467 oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
3469 /* Decide whether we want to dump it */
3470 selectDumpableObject(&(oprinfo[i].dobj));
3472 if (strlen(oprinfo[i].rolname) == 0)
3473 write_msg(NULL, "WARNING: owner of operator \"%s\" appears to be invalid\n",
3474 oprinfo[i].dobj.name);
3479 destroyPQExpBuffer(query);
3486 * read all collations in the system catalogs and return them in the
3487 * CollInfo* structure
3489 * numCollations is set to the number of collations read in
3492 getCollations(Archive *fout, int *numCollations)
3502 int i_collnamespace;
3505 /* Collations didn't exist pre-9.1 */
3506 if (fout->remoteVersion < 90100)
3512 query = createPQExpBuffer();
3515 * find all collations, including builtin collations; we filter out
3516 * system-defined collations at dump-out time.
3519 /* Make sure we are in proper schema */
3520 selectSourceSchema(fout, "pg_catalog");
3522 appendPQExpBuffer(query, "SELECT tableoid, oid, collname, "
3524 "(%s collowner) AS rolname "
3525 "FROM pg_collation",
3528 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3530 ntups = PQntuples(res);
3531 *numCollations = ntups;
3533 collinfo = (CollInfo *) pg_malloc(ntups * sizeof(CollInfo));
3535 i_tableoid = PQfnumber(res, "tableoid");
3536 i_oid = PQfnumber(res, "oid");
3537 i_collname = PQfnumber(res, "collname");
3538 i_collnamespace = PQfnumber(res, "collnamespace");
3539 i_rolname = PQfnumber(res, "rolname");
3541 for (i = 0; i < ntups; i++)
3543 collinfo[i].dobj.objType = DO_COLLATION;
3544 collinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3545 collinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3546 AssignDumpId(&collinfo[i].dobj);
3547 collinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_collname));
3548 collinfo[i].dobj.namespace =
3550 atooid(PQgetvalue(res, i, i_collnamespace)),
3551 collinfo[i].dobj.catId.oid);
3552 collinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3554 /* Decide whether we want to dump it */
3555 selectDumpableObject(&(collinfo[i].dobj));
3560 destroyPQExpBuffer(query);
3567 * read all conversions in the system catalogs and return them in the
3568 * ConvInfo* structure
3570 * numConversions is set to the number of conversions read in
3573 getConversions(Archive *fout, int *numConversions)
3578 PQExpBuffer query = createPQExpBuffer();
3586 /* Conversions didn't exist pre-7.3 */
3587 if (fout->remoteVersion < 70300)
3589 *numConversions = 0;
3594 * find all conversions, including builtin conversions; we filter out
3595 * system-defined conversions at dump-out time.
3598 /* Make sure we are in proper schema */
3599 selectSourceSchema(fout, "pg_catalog");
3601 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
3603 "(%s conowner) AS rolname "
3604 "FROM pg_conversion",
3607 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3609 ntups = PQntuples(res);
3610 *numConversions = ntups;
3612 convinfo = (ConvInfo *) pg_malloc(ntups * sizeof(ConvInfo));
3614 i_tableoid = PQfnumber(res, "tableoid");
3615 i_oid = PQfnumber(res, "oid");
3616 i_conname = PQfnumber(res, "conname");
3617 i_connamespace = PQfnumber(res, "connamespace");
3618 i_rolname = PQfnumber(res, "rolname");
3620 for (i = 0; i < ntups; i++)
3622 convinfo[i].dobj.objType = DO_CONVERSION;
3623 convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3624 convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3625 AssignDumpId(&convinfo[i].dobj);
3626 convinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
3627 convinfo[i].dobj.namespace =
3629 atooid(PQgetvalue(res, i, i_connamespace)),
3630 convinfo[i].dobj.catId.oid);
3631 convinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3633 /* Decide whether we want to dump it */
3634 selectDumpableObject(&(convinfo[i].dobj));
3639 destroyPQExpBuffer(query);
3646 * read all opclasses in the system catalogs and return them in the
3647 * OpclassInfo* structure
3649 * numOpclasses is set to the number of opclasses read in
3652 getOpclasses(Archive *fout, int *numOpclasses)
3657 PQExpBuffer query = createPQExpBuffer();
3658 OpclassInfo *opcinfo;
3666 * find all opclasses, including builtin opclasses; we filter out
3667 * system-defined opclasses at dump-out time.
3670 /* Make sure we are in proper schema */
3671 selectSourceSchema(fout, "pg_catalog");
3673 if (fout->remoteVersion >= 70300)
3675 appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
3677 "(%s opcowner) AS rolname "
3681 else if (fout->remoteVersion >= 70100)
3683 appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
3684 "0::oid AS opcnamespace, "
3685 "''::name AS rolname "
3690 appendPQExpBuffer(query, "SELECT "
3691 "(SELECT oid FROM pg_class WHERE relname = 'pg_opclass') AS tableoid, "
3693 "0::oid AS opcnamespace, "
3694 "''::name AS rolname "
3698 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3700 ntups = PQntuples(res);
3701 *numOpclasses = ntups;
3703 opcinfo = (OpclassInfo *) pg_malloc(ntups * sizeof(OpclassInfo));
3705 i_tableoid = PQfnumber(res, "tableoid");
3706 i_oid = PQfnumber(res, "oid");
3707 i_opcname = PQfnumber(res, "opcname");
3708 i_opcnamespace = PQfnumber(res, "opcnamespace");
3709 i_rolname = PQfnumber(res, "rolname");
3711 for (i = 0; i < ntups; i++)
3713 opcinfo[i].dobj.objType = DO_OPCLASS;
3714 opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3715 opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3716 AssignDumpId(&opcinfo[i].dobj);
3717 opcinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opcname));
3718 opcinfo[i].dobj.namespace =
3720 atooid(PQgetvalue(res, i, i_opcnamespace)),
3721 opcinfo[i].dobj.catId.oid);
3722 opcinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3724 /* Decide whether we want to dump it */
3725 selectDumpableObject(&(opcinfo[i].dobj));
3727 if (fout->remoteVersion >= 70300)
3729 if (strlen(opcinfo[i].rolname) == 0)
3730 write_msg(NULL, "WARNING: owner of operator class \"%s\" appears to be invalid\n",
3731 opcinfo[i].dobj.name);
3737 destroyPQExpBuffer(query);
3744 * read all opfamilies in the system catalogs and return them in the
3745 * OpfamilyInfo* structure
3747 * numOpfamilies is set to the number of opfamilies read in
3750 getOpfamilies(Archive *fout, int *numOpfamilies)
3756 OpfamilyInfo *opfinfo;
3763 /* Before 8.3, there is no separate concept of opfamilies */
3764 if (fout->remoteVersion < 80300)
3770 query = createPQExpBuffer();
3773 * find all opfamilies, including builtin opfamilies; we filter out
3774 * system-defined opfamilies at dump-out time.
3777 /* Make sure we are in proper schema */
3778 selectSourceSchema(fout, "pg_catalog");
3780 appendPQExpBuffer(query, "SELECT tableoid, oid, opfname, "
3782 "(%s opfowner) AS rolname "
3786 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3788 ntups = PQntuples(res);
3789 *numOpfamilies = ntups;
3791 opfinfo = (OpfamilyInfo *) pg_malloc(ntups * sizeof(OpfamilyInfo));
3793 i_tableoid = PQfnumber(res, "tableoid");
3794 i_oid = PQfnumber(res, "oid");
3795 i_opfname = PQfnumber(res, "opfname");
3796 i_opfnamespace = PQfnumber(res, "opfnamespace");
3797 i_rolname = PQfnumber(res, "rolname");
3799 for (i = 0; i < ntups; i++)
3801 opfinfo[i].dobj.objType = DO_OPFAMILY;
3802 opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3803 opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3804 AssignDumpId(&opfinfo[i].dobj);
3805 opfinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_opfname));
3806 opfinfo[i].dobj.namespace =
3808 atooid(PQgetvalue(res, i, i_opfnamespace)),
3809 opfinfo[i].dobj.catId.oid);
3810 opfinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3812 /* Decide whether we want to dump it */
3813 selectDumpableObject(&(opfinfo[i].dobj));
3815 if (fout->remoteVersion >= 70300)
3817 if (strlen(opfinfo[i].rolname) == 0)
3818 write_msg(NULL, "WARNING: owner of operator family \"%s\" appears to be invalid\n",
3819 opfinfo[i].dobj.name);
3825 destroyPQExpBuffer(query);
3832 * read all the user-defined aggregates in the system catalogs and
3833 * return them in the AggInfo* structure
3835 * numAggs is set to the number of aggregates read in
3838 getAggregates(Archive *fout, int *numAggs)
3843 PQExpBuffer query = createPQExpBuffer();
3855 /* Make sure we are in proper schema */
3856 selectSourceSchema(fout, "pg_catalog");
3859 * Find all user-defined aggregates. See comment in getFuncs() for the
3860 * rationale behind the filtering logic.
3863 if (fout->remoteVersion >= 80400)
3865 appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
3866 "pronamespace AS aggnamespace, "
3867 "pronargs, proargtypes, "
3868 "pg_catalog.pg_get_function_identity_arguments(oid) AS proiargs,"
3869 "(%s proowner) AS rolname, "
3872 "WHERE proisagg AND ("
3874 "(SELECT oid FROM pg_namespace "
3875 "WHERE nspname = 'pg_catalog')",
3877 if (binary_upgrade && fout->remoteVersion >= 90100)
3878 appendPQExpBuffer(query,
3879 " OR EXISTS(SELECT 1 FROM pg_depend WHERE "
3880 "classid = 'pg_proc'::regclass AND "
3881 "objid = p.oid AND "
3882 "refclassid = 'pg_extension'::regclass AND "
3884 appendPQExpBuffer(query, ")");
3886 else if (fout->remoteVersion >= 80200)
3888 appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
3889 "pronamespace AS aggnamespace, "
3890 "pronargs, proargtypes, "
3891 "NULL::text AS proiargs,"
3892 "(%s proowner) AS rolname, "
3895 "WHERE proisagg AND ("
3897 "(SELECT oid FROM pg_namespace "
3898 "WHERE nspname = 'pg_catalog'))",
3901 else if (fout->remoteVersion >= 70300)
3903 appendPQExpBuffer(query, "SELECT tableoid, oid, proname AS aggname, "
3904 "pronamespace AS aggnamespace, "
3905 "CASE WHEN proargtypes[0] = 'pg_catalog.\"any\"'::pg_catalog.regtype THEN 0 ELSE 1 END AS pronargs, "
3907 "NULL::text AS proiargs, "
3908 "(%s proowner) AS rolname, "
3912 "AND pronamespace != "
3913 "(SELECT oid FROM pg_namespace WHERE nspname = 'pg_catalog')",
3916 else if (fout->remoteVersion >= 70100)
3918 appendPQExpBuffer(query, "SELECT tableoid, oid, aggname, "
3919 "0::oid AS aggnamespace, "
3920 "CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
3921 "aggbasetype AS proargtypes, "
3922 "NULL::text AS proiargs, "
3923 "(%s aggowner) AS rolname, "
3925 "FROM pg_aggregate "
3926 "where oid > '%u'::oid",
3928 g_last_builtin_oid);
3932 appendPQExpBuffer(query, "SELECT "
3933 "(SELECT oid FROM pg_class WHERE relname = 'pg_aggregate') AS tableoid, "
3935 "0::oid AS aggnamespace, "
3936 "CASE WHEN aggbasetype = 0 THEN 0 ELSE 1 END AS pronargs, "
3937 "aggbasetype AS proargtypes, "
3938 "NULL::text AS proiargs, "
3939 "(%s aggowner) AS rolname, "
3941 "FROM pg_aggregate "
3942 "where oid > '%u'::oid",
3944 g_last_builtin_oid);
3947 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
3949 ntups = PQntuples(res);
3952 agginfo = (AggInfo *) pg_malloc(ntups * sizeof(AggInfo));
3954 i_tableoid = PQfnumber(res, "tableoid");
3955 i_oid = PQfnumber(res, "oid");
3956 i_aggname = PQfnumber(res, "aggname");
3957 i_aggnamespace = PQfnumber(res, "aggnamespace");
3958 i_pronargs = PQfnumber(res, "pronargs");
3959 i_proargtypes = PQfnumber(res, "proargtypes");
3960 i_rolname = PQfnumber(res, "rolname");
3961 i_aggacl = PQfnumber(res, "aggacl");
3962 i_proiargs = PQfnumber(res, "proiargs");
3964 for (i = 0; i < ntups; i++)
3966 agginfo[i].aggfn.dobj.objType = DO_AGG;
3967 agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3968 agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3969 AssignDumpId(&agginfo[i].aggfn.dobj);
3970 agginfo[i].aggfn.dobj.name = pg_strdup(PQgetvalue(res, i, i_aggname));
3971 agginfo[i].aggfn.dobj.namespace =
3973 atooid(PQgetvalue(res, i, i_aggnamespace)),
3974 agginfo[i].aggfn.dobj.catId.oid);
3975 agginfo[i].aggfn.rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
3976 if (strlen(agginfo[i].aggfn.rolname) == 0)
3977 write_msg(NULL, "WARNING: owner of aggregate function \"%s\" appears to be invalid\n",
3978 agginfo[i].aggfn.dobj.name);
3979 agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
3980 agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */
3981 agginfo[i].aggfn.proacl = pg_strdup(PQgetvalue(res, i, i_aggacl));
3982 agginfo[i].aggfn.proiargs = pg_strdup(PQgetvalue(res, i, i_proiargs));
3983 agginfo[i].aggfn.nargs = atoi(PQgetvalue(res, i, i_pronargs));
3984 if (agginfo[i].aggfn.nargs == 0)
3985 agginfo[i].aggfn.argtypes = NULL;
3988 agginfo[i].aggfn.argtypes = (Oid *) pg_malloc(agginfo[i].aggfn.nargs * sizeof(Oid));
3989 if (fout->remoteVersion >= 70300)
3990 parseOidArray(PQgetvalue(res, i, i_proargtypes),
3991 agginfo[i].aggfn.argtypes,
3992 agginfo[i].aggfn.nargs);
3994 /* it's just aggbasetype */
3995 agginfo[i].aggfn.argtypes[0] = atooid(PQgetvalue(res, i, i_proargtypes));
3998 /* Decide whether we want to dump it */
3999 selectDumpableObject(&(agginfo[i].aggfn.dobj));
4004 destroyPQExpBuffer(query);
4011 * read all the user-defined functions in the system catalogs and
4012 * return them in the FuncInfo* structure
4014 * numFuncs is set to the number of functions read in
4017 getFuncs(Archive *fout, int *numFuncs)
4022 PQExpBuffer query = createPQExpBuffer();
4036 /* Make sure we are in proper schema */
4037 selectSourceSchema(fout, "pg_catalog");
4040 * Find all user-defined functions. Normally we can exclude functions in
4041 * pg_catalog, which is worth doing since there are several thousand of
4042 * 'em. However, there are some extensions that create functions in
4043 * pg_catalog. In normal dumps we can still ignore those --- but in
4044 * binary-upgrade mode, we must dump the member objects of the extension,
4045 * so be sure to fetch any such functions.
4047 * Also, in 9.2 and up, exclude functions that are internally dependent on
4048 * something else, since presumably those will be created as a result of
4049 * creating the something else. This currently only acts to suppress
4050 * constructor functions for range types. Note that this is OK only
4051 * because the constructors don't have any dependencies the range type
4052 * doesn't have; otherwise we might not get creation ordering correct.
4055 if (fout->remoteVersion >= 80400)
4057 appendPQExpBuffer(query,
4058 "SELECT tableoid, oid, proname, prolang, "
4059 "pronargs, proargtypes, prorettype, proacl, "
4061 "pg_catalog.pg_get_function_identity_arguments(oid) AS proiargs,"
4062 "(%s proowner) AS rolname "
4064 "WHERE NOT proisagg AND ("
4066 "(SELECT oid FROM pg_namespace "
4067 "WHERE nspname = 'pg_catalog')",
4069 if (fout->remoteVersion >= 90200)
4070 appendPQExpBuffer(query,
4071 "\n AND NOT EXISTS (SELECT 1 FROM pg_depend "
4072 "WHERE classid = 'pg_proc'::regclass AND "
4073 "objid = p.oid AND deptype = 'i')");
4074 if (binary_upgrade && fout->remoteVersion >= 90100)
4075 appendPQExpBuffer(query,
4076 "\n OR EXISTS(SELECT 1 FROM pg_depend WHERE "
4077 "classid = 'pg_proc'::regclass AND "
4078 "objid = p.oid AND "
4079 "refclassid = 'pg_extension'::regclass AND "
4081 appendPQExpBuffer(query, ")");
4083 else if (fout->remoteVersion >= 70300)
4085 appendPQExpBuffer(query,
4086 "SELECT tableoid, oid, proname, prolang, "
4087 "pronargs, proargtypes, prorettype, proacl, "
4089 "NULL::text AS proiargs,"
4090 "(%s proowner) AS rolname "
4092 "WHERE NOT proisagg AND ("
4094 "(SELECT oid FROM pg_namespace "
4095 "WHERE nspname = 'pg_catalog'))",
4098 else if (fout->remoteVersion >= 70100)
4100 appendPQExpBuffer(query,
4101 "SELECT tableoid, oid, proname, prolang, "
4102 "pronargs, proargtypes, prorettype, "
4103 "'{=X}' AS proacl, "
4104 "0::oid AS pronamespace, "
4105 "NULL::text AS proiargs,"
4106 "(%s proowner) AS rolname "
4108 "WHERE pg_proc.oid > '%u'::oid",
4110 g_last_builtin_oid);
4114 appendPQExpBuffer(query,
4116 "(SELECT oid FROM pg_class "
4117 " WHERE relname = 'pg_proc') AS tableoid, "
4118 "oid, proname, prolang, "
4119 "pronargs, proargtypes, prorettype, "
4120 "'{=X}' AS proacl, "
4121 "0::oid AS pronamespace, "
4122 "NULL::text AS proiargs,"
4123 "(%s proowner) AS rolname "
4125 "where pg_proc.oid > '%u'::oid",
4127 g_last_builtin_oid);
4130 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4132 ntups = PQntuples(res);
4136 finfo = (FuncInfo *) pg_malloc0(ntups * sizeof(FuncInfo));
4138 i_tableoid = PQfnumber(res, "tableoid");
4139 i_oid = PQfnumber(res, "oid");
4140 i_proname = PQfnumber(res, "proname");
4141 i_pronamespace = PQfnumber(res, "pronamespace");
4142 i_rolname = PQfnumber(res, "rolname");
4143 i_prolang = PQfnumber(res, "prolang");
4144 i_pronargs = PQfnumber(res, "pronargs");
4145 i_proargtypes = PQfnumber(res, "proargtypes");
4146 i_prorettype = PQfnumber(res, "prorettype");
4147 i_proacl = PQfnumber(res, "proacl");
4148 i_proiargs = PQfnumber(res, "proiargs");
4150 for (i = 0; i < ntups; i++)
4152 finfo[i].dobj.objType = DO_FUNC;
4153 finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
4154 finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
4155 AssignDumpId(&finfo[i].dobj);
4156 finfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_proname));
4157 finfo[i].dobj.namespace =
4159 atooid(PQgetvalue(res, i, i_pronamespace)),
4160 finfo[i].dobj.catId.oid);
4161 finfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4162 finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
4163 finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
4164 finfo[i].proiargs = pg_strdup(PQgetvalue(res, i, i_proiargs));
4165 finfo[i].proacl = pg_strdup(PQgetvalue(res, i, i_proacl));
4166 finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
4167 if (finfo[i].nargs == 0)
4168 finfo[i].argtypes = NULL;
4171 finfo[i].argtypes = (Oid *) pg_malloc(finfo[i].nargs * sizeof(Oid));
4172 parseOidArray(PQgetvalue(res, i, i_proargtypes),
4173 finfo[i].argtypes, finfo[i].nargs);
4176 /* Decide whether we want to dump it */
4177 selectDumpableObject(&(finfo[i].dobj));
4179 if (strlen(finfo[i].rolname) == 0)
4181 "WARNING: owner of function \"%s\" appears to be invalid\n",
4182 finfo[i].dobj.name);
4187 destroyPQExpBuffer(query);
4194 * read all the user-defined tables (no indexes, no catalogs)
4195 * in the system catalogs return them in the TableInfo* structure
4197 * numTables is set to the number of tables read in
4200 getTables(Archive *fout, int *numTables)
4205 PQExpBuffer query = createPQExpBuffer();
4215 int i_relhastriggers;
4221 int i_toastfrozenxid;
4222 int i_relpersistence;
4223 int i_relispopulated;
4226 int i_reltablespace;
4229 int i_toastreloptions;
4233 /* Make sure we are in proper schema */
4234 selectSourceSchema(fout, "pg_catalog");
4237 * Find all the tables and table-like objects.
4239 * We include system catalogs, so that we can work if a user table is
4240 * defined to inherit from a system catalog (pretty weird, but...)
4242 * We ignore relations that are not ordinary tables, sequences, views,
4243 * materialized views, composite types, or foreign tables.
4245 * Composite-type table entries won't be dumped as such, but we have to
4246 * make a DumpableObject for them so that we can track dependencies of the
4247 * composite type (pg_depend entries for columns of the composite type
4248 * link to the pg_class entry not the pg_type entry).
4250 * Note: in this phase we should collect only a minimal amount of
4251 * information about each table, basically just enough to decide if it is
4252 * interesting. We must fetch all tables in this phase because otherwise
4253 * we cannot correctly identify inherited columns, owned sequences, etc.
4256 if (fout->remoteVersion >= 90300)
4259 * Left join to pick up dependency info linking sequences to their
4260 * owning column, if any (note this dependency is AUTO as of 8.2)
4262 appendPQExpBuffer(query,
4263 "SELECT c.tableoid, c.oid, c.relname, "
4264 "c.relacl, c.relkind, c.relnamespace, "
4265 "(%s c.relowner) AS rolname, "
4266 "c.relchecks, c.relhastriggers, "
4267 "c.relhasindex, c.relhasrules, c.relhasoids, "
4268 "c.relfrozenxid, tc.oid AS toid, "
4269 "tc.relfrozenxid AS tfrozenxid, "
4270 "c.relpersistence, c.relispopulated, "
4272 "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
4273 "d.refobjid AS owning_tab, "
4274 "d.refobjsubid AS owning_col, "
4275 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
4276 "array_to_string(array_remove(array_remove(c.reloptions,'check_option=local'),'check_option=cascaded'), ', ') AS reloptions, "
4277 "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
4278 "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption, "
4279 "array_to_string(array(SELECT 'toast.' || x FROM unnest(tc.reloptions) x), ', ') AS toast_reloptions "
4281 "LEFT JOIN pg_depend d ON "
4282 "(c.relkind = '%c' AND "
4283 "d.classid = c.tableoid AND d.objid = c.oid AND "
4284 "d.objsubid = 0 AND "
4285 "d.refclassid = c.tableoid AND d.deptype = 'a') "
4286 "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
4287 "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
4291 RELKIND_RELATION, RELKIND_SEQUENCE,
4292 RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
4293 RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
4295 else if (fout->remoteVersion >= 90100)
4298 * Left join to pick up dependency info linking sequences to their
4299 * owning column, if any (note this dependency is AUTO as of 8.2)
4301 appendPQExpBuffer(query,
4302 "SELECT c.tableoid, c.oid, c.relname, "
4303 "c.relacl, c.relkind, c.relnamespace, "
4304 "(%s c.relowner) AS rolname, "
4305 "c.relchecks, c.relhastriggers, "
4306 "c.relhasindex, c.relhasrules, c.relhasoids, "
4307 "c.relfrozenxid, tc.oid AS toid, "
4308 "tc.relfrozenxid AS tfrozenxid, "
4309 "c.relpersistence, 't' as relispopulated, "
4311 "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
4312 "d.refobjid AS owning_tab, "
4313 "d.refobjsubid AS owning_col, "
4314 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
4315 "array_to_string(c.reloptions, ', ') AS reloptions, "
4316 "array_to_string(array(SELECT 'toast.' || x FROM unnest(tc.reloptions) x), ', ') AS toast_reloptions "
4318 "LEFT JOIN pg_depend d ON "
4319 "(c.relkind = '%c' AND "
4320 "d.classid = c.tableoid AND d.objid = c.oid AND "
4321 "d.objsubid = 0 AND "
4322 "d.refclassid = c.tableoid AND d.deptype = 'a') "
4323 "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
4324 "WHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c') "
4328 RELKIND_RELATION, RELKIND_SEQUENCE,
4329 RELKIND_VIEW, RELKIND_COMPOSITE_TYPE,
4330 RELKIND_MATVIEW, RELKIND_FOREIGN_TABLE);
4332 else if (fout->remoteVersion >= 90000)
4335 * Left join to pick up dependency info linking sequences to their
4336 * owning column, if any (note this dependency is AUTO as of 8.2)
4338 appendPQExpBuffer(query,
4339 "SELECT c.tableoid, c.oid, c.relname, "
4340 "c.relacl, c.relkind, c.relnamespace, "
4341 "(%s c.relowner) AS rolname, "
4342 "c.relchecks, c.relhastriggers, "
4343 "c.relhasindex, c.relhasrules, c.relhasoids, "
4344 "c.relfrozenxid, tc.oid AS toid, "
4345 "tc.relfrozenxid AS tfrozenxid, "
4346 "'p' AS relpersistence, 't' as relispopulated, "
4348 "CASE WHEN c.reloftype <> 0 THEN c.reloftype::pg_catalog.regtype ELSE NULL END AS reloftype, "
4349 "d.refobjid AS owning_tab, "
4350 "d.refobjsubid AS owning_col, "
4351 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
4352 "array_to_string(c.reloptions, ', ') AS reloptions, "
4353 "array_to_string(array(SELECT 'toast.' || x FROM unnest(tc.reloptions) x), ', ') AS toast_reloptions "
4355 "LEFT JOIN pg_depend d ON "
4356 "(c.relkind = '%c' AND "
4357 "d.classid = c.tableoid AND d.objid = c.oid AND "
4358 "d.objsubid = 0 AND "
4359 "d.refclassid = c.tableoid AND d.deptype = 'a') "
4360 "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
4361 "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
4365 RELKIND_RELATION, RELKIND_SEQUENCE,
4366 RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
4368 else if (fout->remoteVersion >= 80400)
4371 * Left join to pick up dependency info linking sequences to their
4372 * owning column, if any (note this dependency is AUTO as of 8.2)
4374 appendPQExpBuffer(query,
4375 "SELECT c.tableoid, c.oid, c.relname, "
4376 "c.relacl, c.relkind, c.relnamespace, "
4377 "(%s c.relowner) AS rolname, "
4378 "c.relchecks, c.relhastriggers, "
4379 "c.relhasindex, c.relhasrules, c.relhasoids, "
4380 "c.relfrozenxid, tc.oid AS toid, "
4381 "tc.relfrozenxid AS tfrozenxid, "
4382 "'p' AS relpersistence, 't' as relispopulated, "
4384 "NULL AS reloftype, "
4385 "d.refobjid AS owning_tab, "
4386 "d.refobjsubid AS owning_col, "
4387 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
4388 "array_to_string(c.reloptions, ', ') AS reloptions, "
4389 "array_to_string(array(SELECT 'toast.' || x FROM unnest(tc.reloptions) x), ', ') AS toast_reloptions "
4391 "LEFT JOIN pg_depend d ON "
4392 "(c.relkind = '%c' AND "
4393 "d.classid = c.tableoid AND d.objid = c.oid AND "
4394 "d.objsubid = 0 AND "
4395 "d.refclassid = c.tableoid AND d.deptype = 'a') "
4396 "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
4397 "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
4401 RELKIND_RELATION, RELKIND_SEQUENCE,
4402 RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
4404 else if (fout->remoteVersion >= 80200)
4407 * Left join to pick up dependency info linking sequences to their
4408 * owning column, if any (note this dependency is AUTO as of 8.2)
4410 appendPQExpBuffer(query,
4411 "SELECT c.tableoid, c.oid, c.relname, "
4412 "c.relacl, c.relkind, c.relnamespace, "
4413 "(%s c.relowner) AS rolname, "
4414 "c.relchecks, (c.reltriggers <> 0) AS relhastriggers, "
4415 "c.relhasindex, c.relhasrules, c.relhasoids, "
4416 "c.relfrozenxid, tc.oid AS toid, "
4417 "tc.relfrozenxid AS tfrozenxid, "
4418 "'p' AS relpersistence, 't' as relispopulated, "
4420 "NULL AS reloftype, "
4421 "d.refobjid AS owning_tab, "
4422 "d.refobjsubid AS owning_col, "
4423 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
4424 "array_to_string(c.reloptions, ', ') AS reloptions, "
4425 "NULL AS toast_reloptions "
4427 "LEFT JOIN pg_depend d ON "
4428 "(c.relkind = '%c' AND "
4429 "d.classid = c.tableoid AND d.objid = c.oid AND "
4430 "d.objsubid = 0 AND "
4431 "d.refclassid = c.tableoid AND d.deptype = 'a') "
4432 "LEFT JOIN pg_class tc ON (c.reltoastrelid = tc.oid) "
4433 "WHERE c.relkind in ('%c', '%c', '%c', '%c') "
4437 RELKIND_RELATION, RELKIND_SEQUENCE,
4438 RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
4440 else if (fout->remoteVersion >= 80000)
4443 * Left join to pick up dependency info linking sequences to their
4444 * owning column, if any
4446 appendPQExpBuffer(query,
4447 "SELECT c.tableoid, c.oid, relname, "
4448 "relacl, relkind, relnamespace, "
4449 "(%s relowner) AS rolname, "
4450 "relchecks, (reltriggers <> 0) AS relhastriggers, "
4451 "relhasindex, relhasrules, relhasoids, "
4452 "0 AS relfrozenxid, "
4455 "'p' AS relpersistence, 't' as relispopulated, "
4457 "NULL AS reloftype, "
4458 "d.refobjid AS owning_tab, "
4459 "d.refobjsubid AS owning_col, "
4460 "(SELECT spcname FROM pg_tablespace t WHERE t.oid = c.reltablespace) AS reltablespace, "
4461 "NULL AS reloptions, "
4462 "NULL AS toast_reloptions "
4464 "LEFT JOIN pg_depend d ON "
4465 "(c.relkind = '%c' AND "
4466 "d.classid = c.tableoid AND d.objid = c.oid AND "
4467 "d.objsubid = 0 AND "
4468 "d.refclassid = c.tableoid AND d.deptype = 'i') "
4469 "WHERE relkind in ('%c', '%c', '%c', '%c') "
4473 RELKIND_RELATION, RELKIND_SEQUENCE,
4474 RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
4476 else if (fout->remoteVersion >= 70300)
4479 * Left join to pick up dependency info linking sequences to their
4480 * owning column, if any
4482 appendPQExpBuffer(query,
4483 "SELECT c.tableoid, c.oid, relname, "
4484 "relacl, relkind, relnamespace, "
4485 "(%s relowner) AS rolname, "
4486 "relchecks, (reltriggers <> 0) AS relhastriggers, "
4487 "relhasindex, relhasrules, relhasoids, "
4488 "0 AS relfrozenxid, "
4491 "'p' AS relpersistence, 't' as relispopulated, "
4493 "NULL AS reloftype, "
4494 "d.refobjid AS owning_tab, "
4495 "d.refobjsubid AS owning_col, "
4496 "NULL AS reltablespace, "
4497 "NULL AS reloptions, "
4498 "NULL AS toast_reloptions "
4500 "LEFT JOIN pg_depend d ON "
4501 "(c.relkind = '%c' AND "
4502 "d.classid = c.tableoid AND d.objid = c.oid AND "
4503 "d.objsubid = 0 AND "
4504 "d.refclassid = c.tableoid AND d.deptype = 'i') "
4505 "WHERE relkind IN ('%c', '%c', '%c', '%c') "
4509 RELKIND_RELATION, RELKIND_SEQUENCE,
4510 RELKIND_VIEW, RELKIND_COMPOSITE_TYPE);
4512 else if (fout->remoteVersion >= 70200)
4514 appendPQExpBuffer(query,
4515 "SELECT tableoid, oid, relname, relacl, relkind, "
4516 "0::oid AS relnamespace, "
4517 "(%s relowner) AS rolname, "
4518 "relchecks, (reltriggers <> 0) AS relhastriggers, "
4519 "relhasindex, relhasrules, relhasoids, "
4520 "0 AS relfrozenxid, "
4523 "'p' AS relpersistence, 't' as relispopulated, "
4525 "NULL AS reloftype, "
4526 "NULL::oid AS owning_tab, "
4527 "NULL::int4 AS owning_col, "
4528 "NULL AS reltablespace, "
4529 "NULL AS reloptions, "
4530 "NULL AS toast_reloptions "
4532 "WHERE relkind IN ('%c', '%c', '%c') "
4535 RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW);
4537 else if (fout->remoteVersion >= 70100)
4539 /* all tables have oids in 7.1 */
4540 appendPQExpBuffer(query,
4541 "SELECT tableoid, oid, relname, relacl, relkind, "
4542 "0::oid AS relnamespace, "
4543 "(%s relowner) AS rolname, "
4544 "relchecks, (reltriggers <> 0) AS relhastriggers, "
4545 "relhasindex, relhasrules, "
4546 "'t'::bool AS relhasoids, "
4547 "0 AS relfrozenxid, "
4550 "'p' AS relpersistence, 't' as relispopulated, "
4552 "NULL AS reloftype, "
4553 "NULL::oid AS owning_tab, "
4554 "NULL::int4 AS owning_col, "
4555 "NULL AS reltablespace, "
4556 "NULL AS reloptions, "
4557 "NULL AS toast_reloptions "
4559 "WHERE relkind IN ('%c', '%c', '%c') "
4562 RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW);
4567 * Before 7.1, view relkind was not set to 'v', so we must check if we
4568 * have a view by looking for a rule in pg_rewrite.
4570 appendPQExpBuffer(query,
4572 "(SELECT oid FROM pg_class WHERE relname = 'pg_class') AS tableoid, "
4573 "oid, relname, relacl, "
4574 "CASE WHEN relhasrules and relkind = 'r' "
4575 " and EXISTS(SELECT rulename FROM pg_rewrite r WHERE "
4576 " r.ev_class = c.oid AND r.ev_type = '1') "
4577 "THEN '%c'::\"char\" "
4578 "ELSE relkind END AS relkind,"
4579 "0::oid AS relnamespace, "
4580 "(%s relowner) AS rolname, "
4581 "relchecks, (reltriggers <> 0) AS relhastriggers, "
4582 "relhasindex, relhasrules, "
4583 "'t'::bool AS relhasoids, "
4584 "0 as relfrozenxid, "
4587 "'p' AS relpersistence, 't' as relispopulated, "
4589 "NULL AS reloftype, "
4590 "NULL::oid AS owning_tab, "
4591 "NULL::int4 AS owning_col, "
4592 "NULL AS reltablespace, "
4593 "NULL AS reloptions, "
4594 "NULL AS toast_reloptions "
4596 "WHERE relkind IN ('%c', '%c') "
4600 RELKIND_RELATION, RELKIND_SEQUENCE);
4603 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4605 ntups = PQntuples(res);
4610 * Extract data from result and lock dumpable tables. We do the locking
4611 * before anything else, to minimize the window wherein a table could
4612 * disappear under us.
4614 * Note that we have to save info about all tables here, even when dumping
4615 * only one, because we don't yet know which tables might be inheritance
4616 * ancestors of the target table.
4618 tblinfo = (TableInfo *) pg_malloc0(ntups * sizeof(TableInfo));
4620 i_reltableoid = PQfnumber(res, "tableoid");
4621 i_reloid = PQfnumber(res, "oid");
4622 i_relname = PQfnumber(res, "relname");
4623 i_relnamespace = PQfnumber(res, "relnamespace");
4624 i_relacl = PQfnumber(res, "relacl");
4625 i_relkind = PQfnumber(res, "relkind");
4626 i_rolname = PQfnumber(res, "rolname");
4627 i_relchecks = PQfnumber(res, "relchecks");
4628 i_relhastriggers = PQfnumber(res, "relhastriggers");
4629 i_relhasindex = PQfnumber(res, "relhasindex");
4630 i_relhasrules = PQfnumber(res, "relhasrules");
4631 i_relhasoids = PQfnumber(res, "relhasoids");
4632 i_relfrozenxid = PQfnumber(res, "relfrozenxid");
4633 i_toastoid = PQfnumber(res, "toid");
4634 i_toastfrozenxid = PQfnumber(res, "tfrozenxid");
4635 i_relpersistence = PQfnumber(res, "relpersistence");
4636 i_relispopulated = PQfnumber(res, "relispopulated");
4637 i_relpages = PQfnumber(res, "relpages");
4638 i_owning_tab = PQfnumber(res, "owning_tab");
4639 i_owning_col = PQfnumber(res, "owning_col");
4640 i_reltablespace = PQfnumber(res, "reltablespace");
4641 i_reloptions = PQfnumber(res, "reloptions");
4642 i_checkoption = PQfnumber(res, "checkoption");
4643 i_toastreloptions = PQfnumber(res, "toast_reloptions");
4644 i_reloftype = PQfnumber(res, "reloftype");
4646 if (lockWaitTimeout && fout->remoteVersion >= 70300)
4649 * Arrange to fail instead of waiting forever for a table lock.
4651 * NB: this coding assumes that the only queries issued within the
4652 * following loop are LOCK TABLEs; else the timeout may be undesirably
4653 * applied to other things too.
4655 resetPQExpBuffer(query);
4656 appendPQExpBuffer(query, "SET statement_timeout = ");
4657 appendStringLiteralConn(query, lockWaitTimeout, GetConnection(fout));
4658 ExecuteSqlStatement(fout, query->data);
4661 for (i = 0; i < ntups; i++)
4663 tblinfo[i].dobj.objType = DO_TABLE;
4664 tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
4665 tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
4666 AssignDumpId(&tblinfo[i].dobj);
4667 tblinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_relname));
4668 tblinfo[i].dobj.namespace =
4670 atooid(PQgetvalue(res, i, i_relnamespace)),
4671 tblinfo[i].dobj.catId.oid);
4672 tblinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
4673 tblinfo[i].relacl = pg_strdup(PQgetvalue(res, i, i_relacl));
4674 tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
4675 tblinfo[i].relpersistence = *(PQgetvalue(res, i, i_relpersistence));
4676 tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
4677 tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
4678 tblinfo[i].hastriggers = (strcmp(PQgetvalue(res, i, i_relhastriggers), "t") == 0);
4679 tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
4680 tblinfo[i].relispopulated = (strcmp(PQgetvalue(res, i, i_relispopulated), "t") == 0);
4681 tblinfo[i].relpages = atoi(PQgetvalue(res, i, i_relpages));
4682 tblinfo[i].frozenxid = atooid(PQgetvalue(res, i, i_relfrozenxid));
4683 tblinfo[i].toast_oid = atooid(PQgetvalue(res, i, i_toastoid));
4684 tblinfo[i].toast_frozenxid = atooid(PQgetvalue(res, i, i_toastfrozenxid));
4685 if (PQgetisnull(res, i, i_reloftype))
4686 tblinfo[i].reloftype = NULL;
4688 tblinfo[i].reloftype = pg_strdup(PQgetvalue(res, i, i_reloftype));
4689 tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
4690 if (PQgetisnull(res, i, i_owning_tab))
4692 tblinfo[i].owning_tab = InvalidOid;
4693 tblinfo[i].owning_col = 0;
4697 tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
4698 tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
4700 tblinfo[i].reltablespace = pg_strdup(PQgetvalue(res, i, i_reltablespace));
4701 tblinfo[i].reloptions = pg_strdup(PQgetvalue(res, i, i_reloptions));
4702 if (i_checkoption == -1 || PQgetisnull(res, i, i_checkoption))
4703 tblinfo[i].checkoption = NULL;
4705 tblinfo[i].checkoption = pg_strdup(PQgetvalue(res, i, i_checkoption));
4706 tblinfo[i].toast_reloptions = pg_strdup(PQgetvalue(res, i, i_toastreloptions));
4708 /* other fields were zeroed above */
4711 * Decide whether we want to dump this table.
4713 if (tblinfo[i].relkind == RELKIND_COMPOSITE_TYPE)
4714 tblinfo[i].dobj.dump = false;
4716 selectDumpableTable(&tblinfo[i]);
4717 tblinfo[i].interesting = tblinfo[i].dobj.dump;
4720 * Read-lock target tables to make sure they aren't DROPPED or altered
4721 * in schema before we get around to dumping them.
4723 * Note that we don't explicitly lock parents of the target tables; we
4724 * assume our lock on the child is enough to prevent schema
4725 * alterations to parent tables.
4727 * NOTE: it'd be kinda nice to lock other relations too, not only
4728 * plain tables, but the backend doesn't presently allow that.
4730 if (tblinfo[i].dobj.dump && tblinfo[i].relkind == RELKIND_RELATION)
4732 resetPQExpBuffer(query);
4733 appendPQExpBuffer(query,
4734 "LOCK TABLE %s IN ACCESS SHARE MODE",
4735 fmtQualifiedId(fout->remoteVersion,
4736 tblinfo[i].dobj.namespace->dobj.name,
4737 tblinfo[i].dobj.name));
4738 ExecuteSqlStatement(fout, query->data);
4741 /* Emit notice if join for owner failed */
4742 if (strlen(tblinfo[i].rolname) == 0)
4743 write_msg(NULL, "WARNING: owner of table \"%s\" appears to be invalid\n",
4744 tblinfo[i].dobj.name);
4747 if (lockWaitTimeout && fout->remoteVersion >= 70300)
4749 ExecuteSqlStatement(fout, "SET statement_timeout = 0");
4754 destroyPQExpBuffer(query);
4761 * identify owned sequences and mark them as dumpable if owning table is
4763 * We used to do this in getTables(), but it's better to do it after the
4764 * index used by findTableByOid() has been set up.
4767 getOwnedSeqs(Archive *fout, TableInfo tblinfo[], int numTables)
4772 * Force sequences that are "owned" by table columns to be dumped whenever
4773 * their owning table is being dumped.
4775 for (i = 0; i < numTables; i++)
4777 TableInfo *seqinfo = &tblinfo[i];
4778 TableInfo *owning_tab;
4780 if (!OidIsValid(seqinfo->owning_tab))
4781 continue; /* not an owned sequence */
4782 if (seqinfo->dobj.dump)
4783 continue; /* no need to search */
4784 owning_tab = findTableByOid(seqinfo->owning_tab);
4785 if (owning_tab && owning_tab->dobj.dump)
4787 seqinfo->interesting = true;
4788 seqinfo->dobj.dump = true;
4795 * read all the inheritance information
4796 * from the system catalogs return them in the InhInfo* structure
4798 * numInherits is set to the number of pairs read in
4801 getInherits(Archive *fout, int *numInherits)
4806 PQExpBuffer query = createPQExpBuffer();
4812 /* Make sure we are in proper schema */
4813 selectSourceSchema(fout, "pg_catalog");
4815 /* find all the inheritance information */
4817 appendPQExpBuffer(query, "SELECT inhrelid, inhparent FROM pg_inherits");
4819 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4821 ntups = PQntuples(res);
4823 *numInherits = ntups;
4825 inhinfo = (InhInfo *) pg_malloc(ntups * sizeof(InhInfo));
4827 i_inhrelid = PQfnumber(res, "inhrelid");
4828 i_inhparent = PQfnumber(res, "inhparent");
4830 for (i = 0; i < ntups; i++)
4832 inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
4833 inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
4838 destroyPQExpBuffer(query);
4845 * get information about every index on a dumpable table
4847 * Note: index data is not returned directly to the caller, but it
4848 * does get entered into the DumpableObject tables.
4851 getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
4855 PQExpBuffer query = createPQExpBuffer();
4858 ConstraintInfo *constrinfo;
4878 for (i = 0; i < numTables; i++)
4880 TableInfo *tbinfo = &tblinfo[i];
4882 /* Only plain tables and materialized views have indexes. */
4883 if (tbinfo->relkind != RELKIND_RELATION &&
4884 tbinfo->relkind != RELKIND_MATVIEW)
4886 if (!tbinfo->hasindex)
4889 /* Ignore indexes of tables not to be dumped */
4890 if (!tbinfo->dobj.dump)
4894 write_msg(NULL, "reading indexes for table \"%s\"\n",
4897 /* Make sure we are in proper schema so indexdef is right */
4898 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
4901 * The point of the messy-looking outer join is to find a constraint
4902 * that is related by an internal dependency link to the index. If we
4903 * find one, create a CONSTRAINT entry linked to the INDEX entry. We
4904 * assume an index won't have more than one internal dependency.
4906 * As of 9.0 we don't need to look at pg_depend but can check for a
4907 * match to pg_constraint.conindid. The check on conrelid is
4908 * redundant but useful because that column is indexed while conindid
4911 resetPQExpBuffer(query);
4912 if (fout->remoteVersion >= 90000)
4915 * the test on indisready is necessary in 9.2, and harmless in
4916 * earlier/later versions
4918 appendPQExpBuffer(query,
4919 "SELECT t.tableoid, t.oid, "
4920 "t.relname AS indexname, "
4921 "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
4922 "t.relnatts AS indnkeys, "
4923 "i.indkey, i.indisclustered, "
4925 "c.contype, c.conname, "
4926 "c.condeferrable, c.condeferred, "
4927 "c.tableoid AS contableoid, "
4929 "pg_catalog.pg_get_constraintdef(c.oid, false) AS condef, "
4930 "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
4931 "array_to_string(t.reloptions, ', ') AS options "
4932 "FROM pg_catalog.pg_index i "
4933 "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
4934 "LEFT JOIN pg_catalog.pg_constraint c "
4935 "ON (i.indrelid = c.conrelid AND "
4936 "i.indexrelid = c.conindid AND "
4937 "c.contype IN ('p','u','x')) "
4938 "WHERE i.indrelid = '%u'::pg_catalog.oid "
4939 "AND i.indisvalid AND i.indisready "
4940 "ORDER BY indexname",
4941 tbinfo->dobj.catId.oid);
4943 else if (fout->remoteVersion >= 80200)
4945 appendPQExpBuffer(query,
4946 "SELECT t.tableoid, t.oid, "
4947 "t.relname AS indexname, "
4948 "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
4949 "t.relnatts AS indnkeys, "
4950 "i.indkey, i.indisclustered, "
4952 "c.contype, c.conname, "
4953 "c.condeferrable, c.condeferred, "
4954 "c.tableoid AS contableoid, "
4957 "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
4958 "array_to_string(t.reloptions, ', ') AS options "
4959 "FROM pg_catalog.pg_index i "
4960 "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
4961 "LEFT JOIN pg_catalog.pg_depend d "
4962 "ON (d.classid = t.tableoid "
4963 "AND d.objid = t.oid "
4964 "AND d.deptype = 'i') "
4965 "LEFT JOIN pg_catalog.pg_constraint c "
4966 "ON (d.refclassid = c.tableoid "
4967 "AND d.refobjid = c.oid) "
4968 "WHERE i.indrelid = '%u'::pg_catalog.oid "
4970 "ORDER BY indexname",
4971 tbinfo->dobj.catId.oid);
4973 else if (fout->remoteVersion >= 80000)
4975 appendPQExpBuffer(query,
4976 "SELECT t.tableoid, t.oid, "
4977 "t.relname AS indexname, "
4978 "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
4979 "t.relnatts AS indnkeys, "
4980 "i.indkey, i.indisclustered, "
4982 "c.contype, c.conname, "
4983 "c.condeferrable, c.condeferred, "
4984 "c.tableoid AS contableoid, "
4987 "(SELECT spcname FROM pg_catalog.pg_tablespace s WHERE s.oid = t.reltablespace) AS tablespace, "
4989 "FROM pg_catalog.pg_index i "
4990 "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
4991 "LEFT JOIN pg_catalog.pg_depend d "
4992 "ON (d.classid = t.tableoid "
4993 "AND d.objid = t.oid "
4994 "AND d.deptype = 'i') "
4995 "LEFT JOIN pg_catalog.pg_constraint c "
4996 "ON (d.refclassid = c.tableoid "
4997 "AND d.refobjid = c.oid) "
4998 "WHERE i.indrelid = '%u'::pg_catalog.oid "
4999 "ORDER BY indexname",
5000 tbinfo->dobj.catId.oid);
5002 else if (fout->remoteVersion >= 70300)
5004 appendPQExpBuffer(query,
5005 "SELECT t.tableoid, t.oid, "
5006 "t.relname AS indexname, "
5007 "pg_catalog.pg_get_indexdef(i.indexrelid) AS indexdef, "
5008 "t.relnatts AS indnkeys, "
5009 "i.indkey, i.indisclustered, "
5011 "c.contype, c.conname, "
5012 "c.condeferrable, c.condeferred, "
5013 "c.tableoid AS contableoid, "
5016 "NULL AS tablespace, "
5018 "FROM pg_catalog.pg_index i "
5019 "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
5020 "LEFT JOIN pg_catalog.pg_depend d "
5021 "ON (d.classid = t.tableoid "
5022 "AND d.objid = t.oid "
5023 "AND d.deptype = 'i') "
5024 "LEFT JOIN pg_catalog.pg_constraint c "
5025 "ON (d.refclassid = c.tableoid "
5026 "AND d.refobjid = c.oid) "
5027 "WHERE i.indrelid = '%u'::pg_catalog.oid "
5028 "ORDER BY indexname",
5029 tbinfo->dobj.catId.oid);
5031 else if (fout->remoteVersion >= 70100)
5033 appendPQExpBuffer(query,
5034 "SELECT t.tableoid, t.oid, "
5035 "t.relname AS indexname, "
5036 "pg_get_indexdef(i.indexrelid) AS indexdef, "
5037 "t.relnatts AS indnkeys, "
5038 "i.indkey, false AS indisclustered, "
5040 "CASE WHEN i.indisprimary THEN 'p'::char "
5041 "ELSE '0'::char END AS contype, "
5042 "t.relname AS conname, "
5043 "false AS condeferrable, "
5044 "false AS condeferred, "
5045 "0::oid AS contableoid, "
5048 "NULL AS tablespace, "
5050 "FROM pg_index i, pg_class t "
5051 "WHERE t.oid = i.indexrelid "
5052 "AND i.indrelid = '%u'::oid "
5053 "ORDER BY indexname",
5054 tbinfo->dobj.catId.oid);
5058 appendPQExpBuffer(query,
5060 "(SELECT oid FROM pg_class WHERE relname = 'pg_class') AS tableoid, "
5062 "t.relname AS indexname, "
5063 "pg_get_indexdef(i.indexrelid) AS indexdef, "
5064 "t.relnatts AS indnkeys, "
5065 "i.indkey, false AS indisclustered, "
5067 "CASE WHEN i.indisprimary THEN 'p'::char "
5068 "ELSE '0'::char END AS contype, "
5069 "t.relname AS conname, "
5070 "false AS condeferrable, "
5071 "false AS condeferred, "
5072 "0::oid AS contableoid, "
5075 "NULL AS tablespace, "
5077 "FROM pg_index i, pg_class t "
5078 "WHERE t.oid = i.indexrelid "
5079 "AND i.indrelid = '%u'::oid "
5080 "ORDER BY indexname",
5081 tbinfo->dobj.catId.oid);
5084 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5086 ntups = PQntuples(res);
5088 i_tableoid = PQfnumber(res, "tableoid");
5089 i_oid = PQfnumber(res, "oid");
5090 i_indexname = PQfnumber(res, "indexname");
5091 i_indexdef = PQfnumber(res, "indexdef");
5092 i_indnkeys = PQfnumber(res, "indnkeys");
5093 i_indkey = PQfnumber(res, "indkey");
5094 i_indisclustered = PQfnumber(res, "indisclustered");
5095 i_relpages = PQfnumber(res, "relpages");
5096 i_contype = PQfnumber(res, "contype");
5097 i_conname = PQfnumber(res, "conname");
5098 i_condeferrable = PQfnumber(res, "condeferrable");
5099 i_condeferred = PQfnumber(res, "condeferred");
5100 i_contableoid = PQfnumber(res, "contableoid");
5101 i_conoid = PQfnumber(res, "conoid");
5102 i_condef = PQfnumber(res, "condef");
5103 i_tablespace = PQfnumber(res, "tablespace");
5104 i_options = PQfnumber(res, "options");
5106 indxinfo = (IndxInfo *) pg_malloc(ntups * sizeof(IndxInfo));
5107 constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
5109 for (j = 0; j < ntups; j++)
5113 indxinfo[j].dobj.objType = DO_INDEX;
5114 indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
5115 indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
5116 AssignDumpId(&indxinfo[j].dobj);
5117 indxinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_indexname));
5118 indxinfo[j].dobj.namespace = tbinfo->dobj.namespace;
5119 indxinfo[j].indextable = tbinfo;
5120 indxinfo[j].indexdef = pg_strdup(PQgetvalue(res, j, i_indexdef));
5121 indxinfo[j].indnkeys = atoi(PQgetvalue(res, j, i_indnkeys));
5122 indxinfo[j].tablespace = pg_strdup(PQgetvalue(res, j, i_tablespace));
5123 indxinfo[j].options = pg_strdup(PQgetvalue(res, j, i_options));
5126 * In pre-7.4 releases, indkeys may contain more entries than
5127 * indnkeys says (since indnkeys will be 1 for a functional
5128 * index). We don't actually care about this case since we don't
5129 * examine indkeys except for indexes associated with PRIMARY and
5130 * UNIQUE constraints, which are never functional indexes. But we
5131 * have to allocate enough space to keep parseOidArray from
5134 indxinfo[j].indkeys = (Oid *) pg_malloc(INDEX_MAX_KEYS * sizeof(Oid));
5135 parseOidArray(PQgetvalue(res, j, i_indkey),
5136 indxinfo[j].indkeys, INDEX_MAX_KEYS);
5137 indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
5138 indxinfo[j].relpages = atoi(PQgetvalue(res, j, i_relpages));
5139 contype = *(PQgetvalue(res, j, i_contype));
5141 if (contype == 'p' || contype == 'u' || contype == 'x')
5144 * If we found a constraint matching the index, create an
5147 * In a pre-7.3 database, we take this path iff the index was
5148 * marked indisprimary.
5150 constrinfo[j].dobj.objType = DO_CONSTRAINT;
5151 constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
5152 constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
5153 AssignDumpId(&constrinfo[j].dobj);
5154 constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
5155 constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
5156 constrinfo[j].contable = tbinfo;
5157 constrinfo[j].condomain = NULL;
5158 constrinfo[j].contype = contype;
5160 constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
5162 constrinfo[j].condef = NULL;
5163 constrinfo[j].confrelid = InvalidOid;
5164 constrinfo[j].conindex = indxinfo[j].dobj.dumpId;
5165 constrinfo[j].condeferrable = *(PQgetvalue(res, j, i_condeferrable)) == 't';
5166 constrinfo[j].condeferred = *(PQgetvalue(res, j, i_condeferred)) == 't';
5167 constrinfo[j].conislocal = true;
5168 constrinfo[j].separate = true;
5170 indxinfo[j].indexconstraint = constrinfo[j].dobj.dumpId;
5172 /* If pre-7.3 DB, better make sure table comes first */
5173 addObjectDependency(&constrinfo[j].dobj,
5174 tbinfo->dobj.dumpId);
5178 /* Plain secondary index */
5179 indxinfo[j].indexconstraint = 0;
5186 destroyPQExpBuffer(query);
5192 * Get info about constraints on dumpable tables.
5194 * Currently handles foreign keys only.
5195 * Unique and primary key constraints are handled with indexes,
5196 * while check constraints are processed in getTableAttrs().
5199 getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
5203 ConstraintInfo *constrinfo;
5213 /* pg_constraint was created in 7.3, so nothing to do if older */
5214 if (fout->remoteVersion < 70300)
5217 query = createPQExpBuffer();
5219 for (i = 0; i < numTables; i++)
5221 TableInfo *tbinfo = &tblinfo[i];
5223 if (!tbinfo->hastriggers || !tbinfo->dobj.dump)
5227 write_msg(NULL, "reading foreign key constraints for table \"%s\"\n",
5231 * select table schema to ensure constraint expr is qualified if
5234 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
5236 resetPQExpBuffer(query);
5237 appendPQExpBuffer(query,
5238 "SELECT tableoid, oid, conname, confrelid, "
5239 "pg_catalog.pg_get_constraintdef(oid) AS condef "
5240 "FROM pg_catalog.pg_constraint "
5241 "WHERE conrelid = '%u'::pg_catalog.oid "
5242 "AND contype = 'f'",
5243 tbinfo->dobj.catId.oid);
5244 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5246 ntups = PQntuples(res);
5248 i_contableoid = PQfnumber(res, "tableoid");
5249 i_conoid = PQfnumber(res, "oid");
5250 i_conname = PQfnumber(res, "conname");
5251 i_confrelid = PQfnumber(res, "confrelid");
5252 i_condef = PQfnumber(res, "condef");
5254 constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
5256 for (j = 0; j < ntups; j++)
5258 constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
5259 constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
5260 constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
5261 AssignDumpId(&constrinfo[j].dobj);
5262 constrinfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_conname));
5263 constrinfo[j].dobj.namespace = tbinfo->dobj.namespace;
5264 constrinfo[j].contable = tbinfo;
5265 constrinfo[j].condomain = NULL;
5266 constrinfo[j].contype = 'f';
5267 constrinfo[j].condef = pg_strdup(PQgetvalue(res, j, i_condef));
5268 constrinfo[j].confrelid = atooid(PQgetvalue(res, j, i_confrelid));
5269 constrinfo[j].conindex = 0;
5270 constrinfo[j].condeferrable = false;
5271 constrinfo[j].condeferred = false;
5272 constrinfo[j].conislocal = true;
5273 constrinfo[j].separate = true;
5279 destroyPQExpBuffer(query);
5283 * getDomainConstraints
5285 * Get info about constraints on a domain.
5288 getDomainConstraints(Archive *fout, TypeInfo *tyinfo)
5291 ConstraintInfo *constrinfo;
5300 /* pg_constraint was created in 7.3, so nothing to do if older */
5301 if (fout->remoteVersion < 70300)
5305 * select appropriate schema to ensure names in constraint are properly
5308 selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
5310 query = createPQExpBuffer();
5312 if (fout->remoteVersion >= 90100)
5313 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
5314 "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
5316 "FROM pg_catalog.pg_constraint "
5317 "WHERE contypid = '%u'::pg_catalog.oid "
5319 tyinfo->dobj.catId.oid);
5321 else if (fout->remoteVersion >= 70400)
5322 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
5323 "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
5324 "true as convalidated "
5325 "FROM pg_catalog.pg_constraint "
5326 "WHERE contypid = '%u'::pg_catalog.oid "
5328 tyinfo->dobj.catId.oid);
5330 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
5331 "'CHECK (' || consrc || ')' AS consrc, "
5332 "true as convalidated "
5333 "FROM pg_catalog.pg_constraint "
5334 "WHERE contypid = '%u'::pg_catalog.oid "
5336 tyinfo->dobj.catId.oid);
5338 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5340 ntups = PQntuples(res);
5342 i_tableoid = PQfnumber(res, "tableoid");
5343 i_oid = PQfnumber(res, "oid");
5344 i_conname = PQfnumber(res, "conname");
5345 i_consrc = PQfnumber(res, "consrc");
5347 constrinfo = (ConstraintInfo *) pg_malloc(ntups * sizeof(ConstraintInfo));
5349 tyinfo->nDomChecks = ntups;
5350 tyinfo->domChecks = constrinfo;
5352 for (i = 0; i < ntups; i++)
5354 bool validated = PQgetvalue(res, i, 4)[0] == 't';
5356 constrinfo[i].dobj.objType = DO_CONSTRAINT;
5357 constrinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5358 constrinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5359 AssignDumpId(&constrinfo[i].dobj);
5360 constrinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_conname));
5361 constrinfo[i].dobj.namespace = tyinfo->dobj.namespace;
5362 constrinfo[i].contable = NULL;
5363 constrinfo[i].condomain = tyinfo;
5364 constrinfo[i].contype = 'c';
5365 constrinfo[i].condef = pg_strdup(PQgetvalue(res, i, i_consrc));
5366 constrinfo[i].confrelid = InvalidOid;
5367 constrinfo[i].conindex = 0;
5368 constrinfo[i].condeferrable = false;
5369 constrinfo[i].condeferred = false;
5370 constrinfo[i].conislocal = true;
5372 constrinfo[i].separate = !validated;
5375 * Make the domain depend on the constraint, ensuring it won't be
5376 * output till any constraint dependencies are OK. If the constraint
5377 * has not been validated, it's going to be dumped after the domain
5378 * anyway, so this doesn't matter.
5381 addObjectDependency(&tyinfo->dobj,
5382 constrinfo[i].dobj.dumpId);
5387 destroyPQExpBuffer(query);
5392 * get basic information about every rule in the system
5394 * numRules is set to the number of rules read in
5397 getRules(Archive *fout, int *numRules)
5402 PQExpBuffer query = createPQExpBuffer();
5412 /* Make sure we are in proper schema */
5413 selectSourceSchema(fout, "pg_catalog");
5415 if (fout->remoteVersion >= 80300)
5417 appendPQExpBuffer(query, "SELECT "
5418 "tableoid, oid, rulename, "
5419 "ev_class AS ruletable, ev_type, is_instead, "
5424 else if (fout->remoteVersion >= 70100)
5426 appendPQExpBuffer(query, "SELECT "
5427 "tableoid, oid, rulename, "
5428 "ev_class AS ruletable, ev_type, is_instead, "
5429 "'O'::char AS ev_enabled "
5435 appendPQExpBuffer(query, "SELECT "
5436 "(SELECT oid FROM pg_class WHERE relname = 'pg_rewrite') AS tableoid, "
5438 "ev_class AS ruletable, ev_type, is_instead, "
5439 "'O'::char AS ev_enabled "
5444 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5446 ntups = PQntuples(res);
5450 ruleinfo = (RuleInfo *) pg_malloc(ntups * sizeof(RuleInfo));
5452 i_tableoid = PQfnumber(res, "tableoid");
5453 i_oid = PQfnumber(res, "oid");
5454 i_rulename = PQfnumber(res, "rulename");
5455 i_ruletable = PQfnumber(res, "ruletable");
5456 i_ev_type = PQfnumber(res, "ev_type");
5457 i_is_instead = PQfnumber(res, "is_instead");
5458 i_ev_enabled = PQfnumber(res, "ev_enabled");
5460 for (i = 0; i < ntups; i++)
5464 ruleinfo[i].dobj.objType = DO_RULE;
5465 ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5466 ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5467 AssignDumpId(&ruleinfo[i].dobj);
5468 ruleinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_rulename));
5469 ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
5470 ruleinfo[i].ruletable = findTableByOid(ruletableoid);
5471 if (ruleinfo[i].ruletable == NULL)
5472 exit_horribly(NULL, "failed sanity check, parent table OID %u of pg_rewrite entry OID %u not found\n",
5473 ruletableoid, ruleinfo[i].dobj.catId.oid);
5474 ruleinfo[i].dobj.namespace = ruleinfo[i].ruletable->dobj.namespace;
5475 ruleinfo[i].dobj.dump = ruleinfo[i].ruletable->dobj.dump;
5476 ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
5477 ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
5478 ruleinfo[i].ev_enabled = *(PQgetvalue(res, i, i_ev_enabled));
5479 if (ruleinfo[i].ruletable)
5482 * If the table is a view or materialized view, force its ON
5483 * SELECT rule to be sorted before the view itself --- this
5484 * ensures that any dependencies for the rule affect the table's
5485 * positioning. Other rules are forced to appear after their
5488 if ((ruleinfo[i].ruletable->relkind == RELKIND_VIEW ||
5489 ruleinfo[i].ruletable->relkind == RELKIND_MATVIEW) &&
5490 ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
5492 addObjectDependency(&ruleinfo[i].ruletable->dobj,
5493 ruleinfo[i].dobj.dumpId);
5494 /* We'll merge the rule into CREATE VIEW, if possible */
5495 ruleinfo[i].separate = false;
5499 addObjectDependency(&ruleinfo[i].dobj,
5500 ruleinfo[i].ruletable->dobj.dumpId);
5501 ruleinfo[i].separate = true;
5505 ruleinfo[i].separate = true;
5508 * If we're forced to break a dependency loop by dumping a view as a
5509 * table and separate _RETURN rule, we'll move the view's reloptions
5510 * to the rule. (This is necessary because tables and views have
5511 * different valid reloptions, so we can't apply the options until the
5512 * backend knows it's a view.) Otherwise the rule's reloptions stay
5515 ruleinfo[i].reloptions = NULL;
5520 destroyPQExpBuffer(query);
5527 * get information about every trigger on a dumpable table
5529 * Note: trigger data is not returned directly to the caller, but it
5530 * does get entered into the DumpableObject tables.
5533 getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
5537 PQExpBuffer query = createPQExpBuffer();
5539 TriggerInfo *tginfo;
5557 for (i = 0; i < numTables; i++)
5559 TableInfo *tbinfo = &tblinfo[i];
5561 if (!tbinfo->hastriggers || !tbinfo->dobj.dump)
5565 write_msg(NULL, "reading triggers for table \"%s\"\n",
5569 * select table schema to ensure regproc name is qualified if needed
5571 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
5573 resetPQExpBuffer(query);
5574 if (fout->remoteVersion >= 90000)
5577 * NB: think not to use pretty=true in pg_get_triggerdef. It
5578 * could result in non-forward-compatible dumps of WHEN clauses
5579 * due to under-parenthesization.
5581 appendPQExpBuffer(query,
5583 "tgfoid::pg_catalog.regproc AS tgfname, "
5584 "pg_catalog.pg_get_triggerdef(oid, false) AS tgdef, "
5585 "tgenabled, tableoid, oid "
5586 "FROM pg_catalog.pg_trigger t "
5587 "WHERE tgrelid = '%u'::pg_catalog.oid "
5588 "AND NOT tgisinternal",
5589 tbinfo->dobj.catId.oid);
5591 else if (fout->remoteVersion >= 80300)
5594 * We ignore triggers that are tied to a foreign-key constraint
5596 appendPQExpBuffer(query,
5598 "tgfoid::pg_catalog.regproc AS tgfname, "
5599 "tgtype, tgnargs, tgargs, tgenabled, "
5600 "tgisconstraint, tgconstrname, tgdeferrable, "
5601 "tgconstrrelid, tginitdeferred, tableoid, oid, "
5602 "tgconstrrelid::pg_catalog.regclass AS tgconstrrelname "
5603 "FROM pg_catalog.pg_trigger t "
5604 "WHERE tgrelid = '%u'::pg_catalog.oid "
5605 "AND tgconstraint = 0",
5606 tbinfo->dobj.catId.oid);
5608 else if (fout->remoteVersion >= 70300)
5611 * We ignore triggers that are tied to a foreign-key constraint,
5612 * but in these versions we have to grovel through pg_constraint
5615 appendPQExpBuffer(query,
5617 "tgfoid::pg_catalog.regproc AS tgfname, "
5618 "tgtype, tgnargs, tgargs, tgenabled, "
5619 "tgisconstraint, tgconstrname, tgdeferrable, "
5620 "tgconstrrelid, tginitdeferred, tableoid, oid, "
5621 "tgconstrrelid::pg_catalog.regclass AS tgconstrrelname "
5622 "FROM pg_catalog.pg_trigger t "
5623 "WHERE tgrelid = '%u'::pg_catalog.oid "
5624 "AND (NOT tgisconstraint "
5626 " (SELECT 1 FROM pg_catalog.pg_depend d "
5627 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
5628 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))",
5629 tbinfo->dobj.catId.oid);
5631 else if (fout->remoteVersion >= 70100)
5633 appendPQExpBuffer(query,
5634 "SELECT tgname, tgfoid::regproc AS tgfname, "
5635 "tgtype, tgnargs, tgargs, tgenabled, "
5636 "tgisconstraint, tgconstrname, tgdeferrable, "
5637 "tgconstrrelid, tginitdeferred, tableoid, oid, "
5638 "(SELECT relname FROM pg_class WHERE oid = tgconstrrelid) "
5639 " AS tgconstrrelname "
5641 "WHERE tgrelid = '%u'::oid",
5642 tbinfo->dobj.catId.oid);
5646 appendPQExpBuffer(query,
5647 "SELECT tgname, tgfoid::regproc AS tgfname, "
5648 "tgtype, tgnargs, tgargs, tgenabled, "
5649 "tgisconstraint, tgconstrname, tgdeferrable, "
5650 "tgconstrrelid, tginitdeferred, "
5651 "(SELECT oid FROM pg_class WHERE relname = 'pg_trigger') AS tableoid, "
5653 "(SELECT relname FROM pg_class WHERE oid = tgconstrrelid) "
5654 " AS tgconstrrelname "
5656 "WHERE tgrelid = '%u'::oid",
5657 tbinfo->dobj.catId.oid);
5659 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5661 ntups = PQntuples(res);
5663 i_tableoid = PQfnumber(res, "tableoid");
5664 i_oid = PQfnumber(res, "oid");
5665 i_tgname = PQfnumber(res, "tgname");
5666 i_tgfname = PQfnumber(res, "tgfname");
5667 i_tgtype = PQfnumber(res, "tgtype");
5668 i_tgnargs = PQfnumber(res, "tgnargs");
5669 i_tgargs = PQfnumber(res, "tgargs");
5670 i_tgisconstraint = PQfnumber(res, "tgisconstraint");
5671 i_tgconstrname = PQfnumber(res, "tgconstrname");
5672 i_tgconstrrelid = PQfnumber(res, "tgconstrrelid");
5673 i_tgconstrrelname = PQfnumber(res, "tgconstrrelname");
5674 i_tgenabled = PQfnumber(res, "tgenabled");
5675 i_tgdeferrable = PQfnumber(res, "tgdeferrable");
5676 i_tginitdeferred = PQfnumber(res, "tginitdeferred");
5677 i_tgdef = PQfnumber(res, "tgdef");
5679 tginfo = (TriggerInfo *) pg_malloc(ntups * sizeof(TriggerInfo));
5681 for (j = 0; j < ntups; j++)
5683 tginfo[j].dobj.objType = DO_TRIGGER;
5684 tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
5685 tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
5686 AssignDumpId(&tginfo[j].dobj);
5687 tginfo[j].dobj.name = pg_strdup(PQgetvalue(res, j, i_tgname));
5688 tginfo[j].dobj.namespace = tbinfo->dobj.namespace;
5689 tginfo[j].tgtable = tbinfo;
5690 tginfo[j].tgenabled = *(PQgetvalue(res, j, i_tgenabled));
5693 tginfo[j].tgdef = pg_strdup(PQgetvalue(res, j, i_tgdef));
5695 /* remaining fields are not valid if we have tgdef */
5696 tginfo[j].tgfname = NULL;
5697 tginfo[j].tgtype = 0;
5698 tginfo[j].tgnargs = 0;
5699 tginfo[j].tgargs = NULL;
5700 tginfo[j].tgisconstraint = false;
5701 tginfo[j].tgdeferrable = false;
5702 tginfo[j].tginitdeferred = false;
5703 tginfo[j].tgconstrname = NULL;
5704 tginfo[j].tgconstrrelid = InvalidOid;
5705 tginfo[j].tgconstrrelname = NULL;
5709 tginfo[j].tgdef = NULL;
5711 tginfo[j].tgfname = pg_strdup(PQgetvalue(res, j, i_tgfname));
5712 tginfo[j].tgtype = atoi(PQgetvalue(res, j, i_tgtype));
5713 tginfo[j].tgnargs = atoi(PQgetvalue(res, j, i_tgnargs));
5714 tginfo[j].tgargs = pg_strdup(PQgetvalue(res, j, i_tgargs));
5715 tginfo[j].tgisconstraint = *(PQgetvalue(res, j, i_tgisconstraint)) == 't';
5716 tginfo[j].tgdeferrable = *(PQgetvalue(res, j, i_tgdeferrable)) == 't';
5717 tginfo[j].tginitdeferred = *(PQgetvalue(res, j, i_tginitdeferred)) == 't';
5719 if (tginfo[j].tgisconstraint)
5721 tginfo[j].tgconstrname = pg_strdup(PQgetvalue(res, j, i_tgconstrname));
5722 tginfo[j].tgconstrrelid = atooid(PQgetvalue(res, j, i_tgconstrrelid));
5723 if (OidIsValid(tginfo[j].tgconstrrelid))
5725 if (PQgetisnull(res, j, i_tgconstrrelname))
5726 exit_horribly(NULL, "query produced null referenced table name for foreign key trigger \"%s\" on table \"%s\" (OID of table: %u)\n",
5727 tginfo[j].dobj.name,
5729 tginfo[j].tgconstrrelid);
5730 tginfo[j].tgconstrrelname = pg_strdup(PQgetvalue(res, j, i_tgconstrrelname));
5733 tginfo[j].tgconstrrelname = NULL;
5737 tginfo[j].tgconstrname = NULL;
5738 tginfo[j].tgconstrrelid = InvalidOid;
5739 tginfo[j].tgconstrrelname = NULL;
5747 destroyPQExpBuffer(query);
5752 * get information about event triggers
5755 getEventTriggers(Archive *fout, int *numEventTriggers)
5760 EventTriggerInfo *evtinfo;
5771 /* Before 9.3, there are no event triggers */
5772 if (fout->remoteVersion < 90300)
5774 *numEventTriggers = 0;
5778 query = createPQExpBuffer();
5780 /* Make sure we are in proper schema */
5781 selectSourceSchema(fout, "pg_catalog");
5783 appendPQExpBuffer(query,
5784 "SELECT e.tableoid, e.oid, evtname, evtenabled, "
5785 "evtevent, (%s evtowner) AS evtowner, "
5786 "array_to_string(array("
5787 "select quote_literal(x) "
5788 " from unnest(evttags) as t(x)), ', ') as evttags, "
5789 "e.evtfoid::regproc as evtfname "
5790 "FROM pg_event_trigger e "
5794 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5796 ntups = PQntuples(res);
5798 *numEventTriggers = ntups;
5800 evtinfo = (EventTriggerInfo *) pg_malloc(ntups * sizeof(EventTriggerInfo));
5802 i_tableoid = PQfnumber(res, "tableoid");
5803 i_oid = PQfnumber(res, "oid");
5804 i_evtname = PQfnumber(res, "evtname");
5805 i_evtevent = PQfnumber(res, "evtevent");
5806 i_evtowner = PQfnumber(res, "evtowner");
5807 i_evttags = PQfnumber(res, "evttags");
5808 i_evtfname = PQfnumber(res, "evtfname");
5809 i_evtenabled = PQfnumber(res, "evtenabled");
5811 for (i = 0; i < ntups; i++)
5813 evtinfo[i].dobj.objType = DO_EVENT_TRIGGER;
5814 evtinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5815 evtinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5816 AssignDumpId(&evtinfo[i].dobj);
5817 evtinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_evtname));
5818 evtinfo[i].evtname = pg_strdup(PQgetvalue(res, i, i_evtname));
5819 evtinfo[i].evtevent = pg_strdup(PQgetvalue(res, i, i_evtevent));
5820 evtinfo[i].evtowner = pg_strdup(PQgetvalue(res, i, i_evtowner));
5821 evtinfo[i].evttags = pg_strdup(PQgetvalue(res, i, i_evttags));
5822 evtinfo[i].evtfname = pg_strdup(PQgetvalue(res, i, i_evtfname));
5823 evtinfo[i].evtenabled = *(PQgetvalue(res, i, i_evtenabled));
5828 destroyPQExpBuffer(query);
5835 * get basic information about every procedural language in the system
5837 * numProcLangs is set to the number of langs read in
5839 * NB: this must run after getFuncs() because we assume we can do
5843 getProcLangs(Archive *fout, int *numProcLangs)
5848 PQExpBuffer query = createPQExpBuffer();
5849 ProcLangInfo *planginfo;
5854 int i_lanplcallfoid;
5860 /* Make sure we are in proper schema */
5861 selectSourceSchema(fout, "pg_catalog");
5863 if (fout->remoteVersion >= 90000)
5865 /* pg_language has a laninline column */
5866 appendPQExpBuffer(query, "SELECT tableoid, oid, "
5867 "lanname, lanpltrusted, lanplcallfoid, "
5868 "laninline, lanvalidator, lanacl, "
5869 "(%s lanowner) AS lanowner "
5875 else if (fout->remoteVersion >= 80300)
5877 /* pg_language has a lanowner column */
5878 appendPQExpBuffer(query, "SELECT tableoid, oid, "
5879 "lanname, lanpltrusted, lanplcallfoid, "
5880 "lanvalidator, lanacl, "
5881 "(%s lanowner) AS lanowner "
5887 else if (fout->remoteVersion >= 80100)
5889 /* Languages are owned by the bootstrap superuser, OID 10 */
5890 appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
5891 "(%s '10') AS lanowner "
5897 else if (fout->remoteVersion >= 70400)
5899 /* Languages are owned by the bootstrap superuser, sysid 1 */
5900 appendPQExpBuffer(query, "SELECT tableoid, oid, *, "
5901 "(%s '1') AS lanowner "
5907 else if (fout->remoteVersion >= 70100)
5909 /* No clear notion of an owner at all before 7.4 ... */
5910 appendPQExpBuffer(query, "SELECT tableoid, oid, * FROM pg_language "
5916 appendPQExpBuffer(query, "SELECT "
5917 "(SELECT oid FROM pg_class WHERE relname = 'pg_language') AS tableoid, "
5918 "oid, * FROM pg_language "
5923 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
5925 ntups = PQntuples(res);
5927 *numProcLangs = ntups;
5929 planginfo = (ProcLangInfo *) pg_malloc(ntups * sizeof(ProcLangInfo));
5931 i_tableoid = PQfnumber(res, "tableoid");
5932 i_oid = PQfnumber(res, "oid");
5933 i_lanname = PQfnumber(res, "lanname");
5934 i_lanpltrusted = PQfnumber(res, "lanpltrusted");
5935 i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
5936 /* these may fail and return -1: */
5937 i_laninline = PQfnumber(res, "laninline");
5938 i_lanvalidator = PQfnumber(res, "lanvalidator");
5939 i_lanacl = PQfnumber(res, "lanacl");
5940 i_lanowner = PQfnumber(res, "lanowner");
5942 for (i = 0; i < ntups; i++)
5944 planginfo[i].dobj.objType = DO_PROCLANG;
5945 planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5946 planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5947 AssignDumpId(&planginfo[i].dobj);
5949 planginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_lanname));
5950 planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
5951 planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
5952 if (i_laninline >= 0)
5953 planginfo[i].laninline = atooid(PQgetvalue(res, i, i_laninline));
5955 planginfo[i].laninline = InvalidOid;
5956 if (i_lanvalidator >= 0)
5957 planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
5959 planginfo[i].lanvalidator = InvalidOid;
5961 planginfo[i].lanacl = pg_strdup(PQgetvalue(res, i, i_lanacl));
5963 planginfo[i].lanacl = pg_strdup("{=U}");
5964 if (i_lanowner >= 0)
5965 planginfo[i].lanowner = pg_strdup(PQgetvalue(res, i, i_lanowner));
5967 planginfo[i].lanowner = pg_strdup("");
5969 if (fout->remoteVersion < 70300)
5972 * We need to make a dependency to ensure the function will be
5973 * dumped first. (In 7.3 and later the regular dependency
5974 * mechanism will handle this for us.)
5976 FuncInfo *funcInfo = findFuncByOid(planginfo[i].lanplcallfoid);
5979 addObjectDependency(&planginfo[i].dobj,
5980 funcInfo->dobj.dumpId);
5986 destroyPQExpBuffer(query);
5993 * get basic information about every cast in the system
5995 * numCasts is set to the number of casts read in
5998 getCasts(Archive *fout, int *numCasts)
6003 PQExpBuffer query = createPQExpBuffer();
6013 /* Make sure we are in proper schema */
6014 selectSourceSchema(fout, "pg_catalog");
6016 if (fout->remoteVersion >= 80400)
6018 appendPQExpBuffer(query, "SELECT tableoid, oid, "
6019 "castsource, casttarget, castfunc, castcontext, "
6021 "FROM pg_cast ORDER BY 3,4");
6023 else if (fout->remoteVersion >= 70300)
6025 appendPQExpBuffer(query, "SELECT tableoid, oid, "
6026 "castsource, casttarget, castfunc, castcontext, "
6027 "CASE WHEN castfunc = 0 THEN 'b' ELSE 'f' END AS castmethod "
6028 "FROM pg_cast ORDER BY 3,4");
6032 appendPQExpBuffer(query, "SELECT 0 AS tableoid, p.oid, "
6033 "t1.oid AS castsource, t2.oid AS casttarget, "
6034 "p.oid AS castfunc, 'e' AS castcontext, "
6035 "'f' AS castmethod "
6036 "FROM pg_type t1, pg_type t2, pg_proc p "
6037 "WHERE p.pronargs = 1 AND "
6038 "p.proargtypes[0] = t1.oid AND "
6039 "p.prorettype = t2.oid AND p.proname = t2.typname "
6043 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6045 ntups = PQntuples(res);
6049 castinfo = (CastInfo *) pg_malloc(ntups * sizeof(CastInfo));
6051 i_tableoid = PQfnumber(res, "tableoid");
6052 i_oid = PQfnumber(res, "oid");
6053 i_castsource = PQfnumber(res, "castsource");
6054 i_casttarget = PQfnumber(res, "casttarget");
6055 i_castfunc = PQfnumber(res, "castfunc");
6056 i_castcontext = PQfnumber(res, "castcontext");
6057 i_castmethod = PQfnumber(res, "castmethod");
6059 for (i = 0; i < ntups; i++)
6061 PQExpBufferData namebuf;
6062 TypeInfo *sTypeInfo;
6063 TypeInfo *tTypeInfo;
6065 castinfo[i].dobj.objType = DO_CAST;
6066 castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6067 castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6068 AssignDumpId(&castinfo[i].dobj);
6069 castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
6070 castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
6071 castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
6072 castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
6073 castinfo[i].castmethod = *(PQgetvalue(res, i, i_castmethod));
6076 * Try to name cast as concatenation of typnames. This is only used
6077 * for purposes of sorting. If we fail to find either type, the name
6078 * will be an empty string.
6080 initPQExpBuffer(&namebuf);
6081 sTypeInfo = findTypeByOid(castinfo[i].castsource);
6082 tTypeInfo = findTypeByOid(castinfo[i].casttarget);
6083 if (sTypeInfo && tTypeInfo)
6084 appendPQExpBuffer(&namebuf, "%s %s",
6085 sTypeInfo->dobj.name, tTypeInfo->dobj.name);
6086 castinfo[i].dobj.name = namebuf.data;
6088 if (OidIsValid(castinfo[i].castfunc))
6091 * We need to make a dependency to ensure the function will be
6092 * dumped first. (In 7.3 and later the regular dependency
6093 * mechanism will handle this for us.)
6097 funcInfo = findFuncByOid(castinfo[i].castfunc);
6099 addObjectDependency(&castinfo[i].dobj,
6100 funcInfo->dobj.dumpId);
6106 destroyPQExpBuffer(query);
6113 * for each interesting table, read info about its attributes
6114 * (names, types, default values, CHECK constraints, etc)
6116 * This is implemented in a very inefficient way right now, looping
6117 * through the tblinfo and doing a join per table to find the attrs and their
6118 * types. However, because we want type names and so forth to be named
6119 * relative to the schema of each table, we couldn't do it in just one
6120 * query. (Maybe one query per schema?)
6125 getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
6129 PQExpBuffer q = createPQExpBuffer();
6134 int i_attstattarget;
6145 int i_attfdwoptions;
6150 for (i = 0; i < numTables; i++)
6152 TableInfo *tbinfo = &tblinfo[i];
6154 /* Don't bother to collect info for sequences */
6155 if (tbinfo->relkind == RELKIND_SEQUENCE)
6158 /* Don't bother with uninteresting tables, either */
6159 if (!tbinfo->interesting)
6163 * Make sure we are in proper schema for this table; this allows
6164 * correct retrieval of formatted type names and default exprs
6166 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
6168 /* find all the user attributes and their types */
6171 * we must read the attribute names in attribute number order! because
6172 * we will use the attnum to index into the attnames array later. We
6173 * actually ask to order by "attrelid, attnum" because (at least up to
6174 * 7.3) the planner is not smart enough to realize it needn't re-sort
6175 * the output of an indexscan on pg_attribute_relid_attnum_index.
6178 write_msg(NULL, "finding the columns and types of table \"%s\"\n",
6181 resetPQExpBuffer(q);
6183 if (fout->remoteVersion >= 90200)
6186 * attfdwoptions is new in 9.2.
6188 appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
6189 "a.attstattarget, a.attstorage, t.typstorage, "
6190 "a.attnotnull, a.atthasdef, a.attisdropped, "
6191 "a.attlen, a.attalign, a.attislocal, "
6192 "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
6193 "array_to_string(a.attoptions, ', ') AS attoptions, "
6194 "CASE WHEN a.attcollation <> t.typcollation "
6195 "THEN a.attcollation ELSE 0 END AS attcollation, "
6196 "pg_catalog.array_to_string(ARRAY("
6197 "SELECT pg_catalog.quote_ident(option_name) || "
6198 "' ' || pg_catalog.quote_literal(option_value) "
6199 "FROM pg_catalog.pg_options_to_table(attfdwoptions) "
6200 "ORDER BY option_name"
6201 "), E',\n ') AS attfdwoptions "
6202 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
6203 "ON a.atttypid = t.oid "
6204 "WHERE a.attrelid = '%u'::pg_catalog.oid "
6205 "AND a.attnum > 0::pg_catalog.int2 "
6206 "ORDER BY a.attrelid, a.attnum",
6207 tbinfo->dobj.catId.oid);
6209 else if (fout->remoteVersion >= 90100)
6212 * attcollation is new in 9.1. Since we only want to dump COLLATE
6213 * clauses for attributes whose collation is different from their
6214 * type's default, we use a CASE here to suppress uninteresting
6215 * attcollations cheaply.
6217 appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
6218 "a.attstattarget, a.attstorage, t.typstorage, "
6219 "a.attnotnull, a.atthasdef, a.attisdropped, "
6220 "a.attlen, a.attalign, a.attislocal, "
6221 "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
6222 "array_to_string(a.attoptions, ', ') AS attoptions, "
6223 "CASE WHEN a.attcollation <> t.typcollation "
6224 "THEN a.attcollation ELSE 0 END AS attcollation, "
6225 "NULL AS attfdwoptions "
6226 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
6227 "ON a.atttypid = t.oid "
6228 "WHERE a.attrelid = '%u'::pg_catalog.oid "
6229 "AND a.attnum > 0::pg_catalog.int2 "
6230 "ORDER BY a.attrelid, a.attnum",
6231 tbinfo->dobj.catId.oid);
6233 else if (fout->remoteVersion >= 90000)
6235 /* attoptions is new in 9.0 */
6236 appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
6237 "a.attstattarget, a.attstorage, t.typstorage, "
6238 "a.attnotnull, a.atthasdef, a.attisdropped, "
6239 "a.attlen, a.attalign, a.attislocal, "
6240 "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
6241 "array_to_string(a.attoptions, ', ') AS attoptions, "
6242 "0 AS attcollation, "
6243 "NULL AS attfdwoptions "
6244 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
6245 "ON a.atttypid = t.oid "
6246 "WHERE a.attrelid = '%u'::pg_catalog.oid "
6247 "AND a.attnum > 0::pg_catalog.int2 "
6248 "ORDER BY a.attrelid, a.attnum",
6249 tbinfo->dobj.catId.oid);
6251 else if (fout->remoteVersion >= 70300)
6253 /* need left join here to not fail on dropped columns ... */
6254 appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
6255 "a.attstattarget, a.attstorage, t.typstorage, "
6256 "a.attnotnull, a.atthasdef, a.attisdropped, "
6257 "a.attlen, a.attalign, a.attislocal, "
6258 "pg_catalog.format_type(t.oid,a.atttypmod) AS atttypname, "
6259 "'' AS attoptions, 0 AS attcollation, "
6260 "NULL AS attfdwoptions "
6261 "FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_type t "
6262 "ON a.atttypid = t.oid "
6263 "WHERE a.attrelid = '%u'::pg_catalog.oid "
6264 "AND a.attnum > 0::pg_catalog.int2 "
6265 "ORDER BY a.attrelid, a.attnum",
6266 tbinfo->dobj.catId.oid);
6268 else if (fout->remoteVersion >= 70100)
6271 * attstattarget doesn't exist in 7.1. It does exist in 7.2, but
6272 * we don't dump it because we can't tell whether it's been
6273 * explicitly set or was just a default.
6275 * attislocal doesn't exist before 7.3, either; in older databases
6276 * we assume it's TRUE, else we'd fail to dump non-inherited atts.
6278 appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, "
6279 "-1 AS attstattarget, a.attstorage, "
6280 "t.typstorage, a.attnotnull, a.atthasdef, "
6281 "false AS attisdropped, a.attlen, "
6282 "a.attalign, true AS attislocal, "
6283 "format_type(t.oid,a.atttypmod) AS atttypname, "
6284 "'' AS attoptions, 0 AS attcollation, "
6285 "NULL AS attfdwoptions "
6286 "FROM pg_attribute a LEFT JOIN pg_type t "
6287 "ON a.atttypid = t.oid "
6288 "WHERE a.attrelid = '%u'::oid "
6289 "AND a.attnum > 0::int2 "
6290 "ORDER BY a.attrelid, a.attnum",
6291 tbinfo->dobj.catId.oid);
6295 /* format_type not available before 7.1 */
6296 appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, "
6297 "-1 AS attstattarget, "
6298 "attstorage, attstorage AS typstorage, "
6299 "attnotnull, atthasdef, false AS attisdropped, "
6300 "attlen, attalign, "
6301 "true AS attislocal, "
6302 "(SELECT typname FROM pg_type WHERE oid = atttypid) AS atttypname, "
6303 "'' AS attoptions, 0 AS attcollation, "
6304 "NULL AS attfdwoptions "
6305 "FROM pg_attribute a "
6306 "WHERE attrelid = '%u'::oid "
6307 "AND attnum > 0::int2 "
6308 "ORDER BY attrelid, attnum",
6309 tbinfo->dobj.catId.oid);
6312 res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
6314 ntups = PQntuples(res);
6316 i_attnum = PQfnumber(res, "attnum");
6317 i_attname = PQfnumber(res, "attname");
6318 i_atttypname = PQfnumber(res, "atttypname");
6319 i_atttypmod = PQfnumber(res, "atttypmod");
6320 i_attstattarget = PQfnumber(res, "attstattarget");
6321 i_attstorage = PQfnumber(res, "attstorage");
6322 i_typstorage = PQfnumber(res, "typstorage");
6323 i_attnotnull = PQfnumber(res, "attnotnull");
6324 i_atthasdef = PQfnumber(res, "atthasdef");
6325 i_attisdropped = PQfnumber(res, "attisdropped");
6326 i_attlen = PQfnumber(res, "attlen");
6327 i_attalign = PQfnumber(res, "attalign");
6328 i_attislocal = PQfnumber(res, "attislocal");
6329 i_attoptions = PQfnumber(res, "attoptions");
6330 i_attcollation = PQfnumber(res, "attcollation");
6331 i_attfdwoptions = PQfnumber(res, "attfdwoptions");
6333 tbinfo->numatts = ntups;
6334 tbinfo->attnames = (char **) pg_malloc(ntups * sizeof(char *));
6335 tbinfo->atttypnames = (char **) pg_malloc(ntups * sizeof(char *));
6336 tbinfo->atttypmod = (int *) pg_malloc(ntups * sizeof(int));
6337 tbinfo->attstattarget = (int *) pg_malloc(ntups * sizeof(int));
6338 tbinfo->attstorage = (char *) pg_malloc(ntups * sizeof(char));
6339 tbinfo->typstorage = (char *) pg_malloc(ntups * sizeof(char));
6340 tbinfo->attisdropped = (bool *) pg_malloc(ntups * sizeof(bool));
6341 tbinfo->attlen = (int *) pg_malloc(ntups * sizeof(int));
6342 tbinfo->attalign = (char *) pg_malloc(ntups * sizeof(char));
6343 tbinfo->attislocal = (bool *) pg_malloc(ntups * sizeof(bool));
6344 tbinfo->attoptions = (char **) pg_malloc(ntups * sizeof(char *));
6345 tbinfo->attcollation = (Oid *) pg_malloc(ntups * sizeof(Oid));
6346 tbinfo->attfdwoptions = (char **) pg_malloc(ntups * sizeof(char *));
6347 tbinfo->notnull = (bool *) pg_malloc(ntups * sizeof(bool));
6348 tbinfo->inhNotNull = (bool *) pg_malloc(ntups * sizeof(bool));
6349 tbinfo->attrdefs = (AttrDefInfo **) pg_malloc(ntups * sizeof(AttrDefInfo *));
6350 hasdefaults = false;
6352 for (j = 0; j < ntups; j++)
6354 if (j + 1 != atoi(PQgetvalue(res, j, i_attnum)))
6356 "invalid column numbering in table \"%s\"\n",
6358 tbinfo->attnames[j] = pg_strdup(PQgetvalue(res, j, i_attname));
6359 tbinfo->atttypnames[j] = pg_strdup(PQgetvalue(res, j, i_atttypname));
6360 tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
6361 tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
6362 tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
6363 tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
6364 tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
6365 tbinfo->attlen[j] = atoi(PQgetvalue(res, j, i_attlen));
6366 tbinfo->attalign[j] = *(PQgetvalue(res, j, i_attalign));
6367 tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
6368 tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
6369 tbinfo->attoptions[j] = pg_strdup(PQgetvalue(res, j, i_attoptions));
6370 tbinfo->attcollation[j] = atooid(PQgetvalue(res, j, i_attcollation));
6371 tbinfo->attfdwoptions[j] = pg_strdup(PQgetvalue(res, j, i_attfdwoptions));
6372 tbinfo->attrdefs[j] = NULL; /* fix below */
6373 if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
6375 /* these flags will be set in flagInhAttrs() */
6376 tbinfo->inhNotNull[j] = false;
6382 * Get info about column defaults
6386 AttrDefInfo *attrdefs;
6390 write_msg(NULL, "finding default expressions of table \"%s\"\n",
6393 resetPQExpBuffer(q);
6394 if (fout->remoteVersion >= 70300)
6396 appendPQExpBuffer(q, "SELECT tableoid, oid, adnum, "
6397 "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc "
6398 "FROM pg_catalog.pg_attrdef "
6399 "WHERE adrelid = '%u'::pg_catalog.oid",
6400 tbinfo->dobj.catId.oid);
6402 else if (fout->remoteVersion >= 70200)
6404 /* 7.2 did not have OIDs in pg_attrdef */
6405 appendPQExpBuffer(q, "SELECT tableoid, 0 AS oid, adnum, "
6406 "pg_get_expr(adbin, adrelid) AS adsrc "
6408 "WHERE adrelid = '%u'::oid",
6409 tbinfo->dobj.catId.oid);
6411 else if (fout->remoteVersion >= 70100)
6413 /* no pg_get_expr, so must rely on adsrc */
6414 appendPQExpBuffer(q, "SELECT tableoid, oid, adnum, adsrc "
6416 "WHERE adrelid = '%u'::oid",
6417 tbinfo->dobj.catId.oid);
6421 /* no pg_get_expr, no tableoid either */
6422 appendPQExpBuffer(q, "SELECT "
6423 "(SELECT oid FROM pg_class WHERE relname = 'pg_attrdef') AS tableoid, "
6424 "oid, adnum, adsrc "
6426 "WHERE adrelid = '%u'::oid",
6427 tbinfo->dobj.catId.oid);
6429 res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
6431 numDefaults = PQntuples(res);
6432 attrdefs = (AttrDefInfo *) pg_malloc(numDefaults * sizeof(AttrDefInfo));
6434 for (j = 0; j < numDefaults; j++)
6438 adnum = atoi(PQgetvalue(res, j, 2));
6440 if (adnum <= 0 || adnum > ntups)
6442 "invalid adnum value %d for table \"%s\"\n",
6443 adnum, tbinfo->dobj.name);
6446 * dropped columns shouldn't have defaults, but just in case,
6449 if (tbinfo->attisdropped[adnum - 1])
6452 attrdefs[j].dobj.objType = DO_ATTRDEF;
6453 attrdefs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
6454 attrdefs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
6455 AssignDumpId(&attrdefs[j].dobj);
6456 attrdefs[j].adtable = tbinfo;
6457 attrdefs[j].adnum = adnum;
6458 attrdefs[j].adef_expr = pg_strdup(PQgetvalue(res, j, 3));
6460 attrdefs[j].dobj.name = pg_strdup(tbinfo->dobj.name);
6461 attrdefs[j].dobj.namespace = tbinfo->dobj.namespace;
6463 attrdefs[j].dobj.dump = tbinfo->dobj.dump;
6466 * Defaults on a VIEW must always be dumped as separate ALTER
6467 * TABLE commands. Defaults on regular tables are dumped as
6468 * part of the CREATE TABLE if possible, which it won't be if
6469 * the column is not going to be emitted explicitly.
6471 if (tbinfo->relkind == RELKIND_VIEW)
6473 attrdefs[j].separate = true;
6474 /* needed in case pre-7.3 DB: */
6475 addObjectDependency(&attrdefs[j].dobj,
6476 tbinfo->dobj.dumpId);
6478 else if (!shouldPrintColumn(tbinfo, adnum - 1))
6480 /* column will be suppressed, print default separately */
6481 attrdefs[j].separate = true;
6482 /* needed in case pre-7.3 DB: */
6483 addObjectDependency(&attrdefs[j].dobj,
6484 tbinfo->dobj.dumpId);
6488 attrdefs[j].separate = false;
6491 * Mark the default as needing to appear before the table,
6492 * so that any dependencies it has must be emitted before
6493 * the CREATE TABLE. If this is not possible, we'll
6494 * change to "separate" mode while sorting dependencies.
6496 addObjectDependency(&tbinfo->dobj,
6497 attrdefs[j].dobj.dumpId);
6500 tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
6506 * Get info about table CHECK constraints
6508 if (tbinfo->ncheck > 0)
6510 ConstraintInfo *constrs;
6514 write_msg(NULL, "finding check constraints for table \"%s\"\n",
6517 resetPQExpBuffer(q);
6518 if (fout->remoteVersion >= 90200)
6521 * convalidated is new in 9.2 (actually, it is there in 9.1,
6522 * but it wasn't ever false for check constraints until 9.2).
6524 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
6525 "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
6526 "conislocal, convalidated "
6527 "FROM pg_catalog.pg_constraint "
6528 "WHERE conrelid = '%u'::pg_catalog.oid "
6529 " AND contype = 'c' "
6531 tbinfo->dobj.catId.oid);
6533 else if (fout->remoteVersion >= 80400)
6535 /* conislocal is new in 8.4 */
6536 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
6537 "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
6538 "conislocal, true AS convalidated "
6539 "FROM pg_catalog.pg_constraint "
6540 "WHERE conrelid = '%u'::pg_catalog.oid "
6541 " AND contype = 'c' "
6543 tbinfo->dobj.catId.oid);
6545 else if (fout->remoteVersion >= 70400)
6547 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
6548 "pg_catalog.pg_get_constraintdef(oid) AS consrc, "
6549 "true AS conislocal, true AS convalidated "
6550 "FROM pg_catalog.pg_constraint "
6551 "WHERE conrelid = '%u'::pg_catalog.oid "
6552 " AND contype = 'c' "
6554 tbinfo->dobj.catId.oid);
6556 else if (fout->remoteVersion >= 70300)
6558 /* no pg_get_constraintdef, must use consrc */
6559 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
6560 "'CHECK (' || consrc || ')' AS consrc, "
6561 "true AS conislocal, true AS convalidated "
6562 "FROM pg_catalog.pg_constraint "
6563 "WHERE conrelid = '%u'::pg_catalog.oid "
6564 " AND contype = 'c' "
6566 tbinfo->dobj.catId.oid);
6568 else if (fout->remoteVersion >= 70200)
6570 /* 7.2 did not have OIDs in pg_relcheck */
6571 appendPQExpBuffer(q, "SELECT tableoid, 0 AS oid, "
6572 "rcname AS conname, "
6573 "'CHECK (' || rcsrc || ')' AS consrc, "
6574 "true AS conislocal, true AS convalidated "
6576 "WHERE rcrelid = '%u'::oid "
6578 tbinfo->dobj.catId.oid);
6580 else if (fout->remoteVersion >= 70100)
6582 appendPQExpBuffer(q, "SELECT tableoid, oid, "
6583 "rcname AS conname, "
6584 "'CHECK (' || rcsrc || ')' AS consrc, "
6585 "true AS conislocal, true AS convalidated "
6587 "WHERE rcrelid = '%u'::oid "
6589 tbinfo->dobj.catId.oid);
6593 /* no tableoid in 7.0 */
6594 appendPQExpBuffer(q, "SELECT "
6595 "(SELECT oid FROM pg_class WHERE relname = 'pg_relcheck') AS tableoid, "
6596 "oid, rcname AS conname, "
6597 "'CHECK (' || rcsrc || ')' AS consrc, "
6598 "true AS conislocal, true AS convalidated "
6600 "WHERE rcrelid = '%u'::oid "
6602 tbinfo->dobj.catId.oid);
6604 res = ExecuteSqlQuery(fout, q->data, PGRES_TUPLES_OK);
6606 numConstrs = PQntuples(res);
6607 if (numConstrs != tbinfo->ncheck)
6609 write_msg(NULL, ngettext("expected %d check constraint on table \"%s\" but found %d\n",
6610 "expected %d check constraints on table \"%s\" but found %d\n",
6612 tbinfo->ncheck, tbinfo->dobj.name, numConstrs);
6613 write_msg(NULL, "(The system catalogs might be corrupted.)\n");
6617 constrs = (ConstraintInfo *) pg_malloc(numConstrs * sizeof(ConstraintInfo));
6618 tbinfo->checkexprs = constrs;
6620 for (j = 0; j < numConstrs; j++)
6622 bool validated = PQgetvalue(res, j, 5)[0] == 't';
6624 constrs[j].dobj.objType = DO_CONSTRAINT;
6625 constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
6626 constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
6627 AssignDumpId(&constrs[j].dobj);
6628 constrs[j].dobj.name = pg_strdup(PQgetvalue(res, j, 2));
6629 constrs[j].dobj.namespace = tbinfo->dobj.namespace;
6630 constrs[j].contable = tbinfo;
6631 constrs[j].condomain = NULL;
6632 constrs[j].contype = 'c';
6633 constrs[j].condef = pg_strdup(PQgetvalue(res, j, 3));
6634 constrs[j].confrelid = InvalidOid;
6635 constrs[j].conindex = 0;
6636 constrs[j].condeferrable = false;
6637 constrs[j].condeferred = false;
6638 constrs[j].conislocal = (PQgetvalue(res, j, 4)[0] == 't');
6641 * An unvalidated constraint needs to be dumped separately, so
6642 * that potentially-violating existing data is loaded before
6645 constrs[j].separate = !validated;
6647 constrs[j].dobj.dump = tbinfo->dobj.dump;
6650 * Mark the constraint as needing to appear before the table
6651 * --- this is so that any other dependencies of the
6652 * constraint will be emitted before we try to create the
6653 * table. If the constraint is to be dumped separately, it
6654 * will be dumped after data is loaded anyway, so don't do it.
6655 * (There's an automatic dependency in the opposite direction
6656 * anyway, so don't need to add one manually here.)
6658 if (!constrs[j].separate)
6659 addObjectDependency(&tbinfo->dobj,
6660 constrs[j].dobj.dumpId);
6663 * If the constraint is inherited, this will be detected later
6664 * (in pre-8.4 databases). We also detect later if the
6665 * constraint must be split out from the table definition.
6672 destroyPQExpBuffer(q);
6676 * Test whether a column should be printed as part of table's CREATE TABLE.
6677 * Column number is zero-based.
6679 * Normally this is always true, but it's false for dropped columns, as well
6680 * as those that were inherited without any local definition. (If we print
6681 * such a column it will mistakenly get pg_attribute.attislocal set to true.)
6682 * However, in binary_upgrade mode, we must print all such columns anyway and
6683 * fix the attislocal/attisdropped state later, so as to keep control of the
6684 * physical column order.
6686 * This function exists because there are scattered nonobvious places that
6687 * must be kept in sync with this decision.
6690 shouldPrintColumn(TableInfo *tbinfo, int colno)
6694 return (tbinfo->attislocal[colno] && !tbinfo->attisdropped[colno]);
6700 * read all text search parsers in the system catalogs and return them
6701 * in the TSParserInfo* structure
6703 * numTSParsers is set to the number of parsers read in
6706 getTSParsers(Archive *fout, int *numTSParsers)
6712 TSParserInfo *prsinfo;
6723 /* Before 8.3, there is no built-in text search support */
6724 if (fout->remoteVersion < 80300)
6730 query = createPQExpBuffer();
6733 * find all text search objects, including builtin ones; we filter out
6734 * system-defined objects at dump-out time.
6737 /* Make sure we are in proper schema */
6738 selectSourceSchema(fout, "pg_catalog");
6740 appendPQExpBuffer(query, "SELECT tableoid, oid, prsname, prsnamespace, "
6741 "prsstart::oid, prstoken::oid, "
6742 "prsend::oid, prsheadline::oid, prslextype::oid "
6743 "FROM pg_ts_parser");
6745 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6747 ntups = PQntuples(res);
6748 *numTSParsers = ntups;
6750 prsinfo = (TSParserInfo *) pg_malloc(ntups * sizeof(TSParserInfo));
6752 i_tableoid = PQfnumber(res, "tableoid");
6753 i_oid = PQfnumber(res, "oid");
6754 i_prsname = PQfnumber(res, "prsname");
6755 i_prsnamespace = PQfnumber(res, "prsnamespace");
6756 i_prsstart = PQfnumber(res, "prsstart");
6757 i_prstoken = PQfnumber(res, "prstoken");
6758 i_prsend = PQfnumber(res, "prsend");
6759 i_prsheadline = PQfnumber(res, "prsheadline");
6760 i_prslextype = PQfnumber(res, "prslextype");
6762 for (i = 0; i < ntups; i++)
6764 prsinfo[i].dobj.objType = DO_TSPARSER;
6765 prsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6766 prsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6767 AssignDumpId(&prsinfo[i].dobj);
6768 prsinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_prsname));
6769 prsinfo[i].dobj.namespace =
6771 atooid(PQgetvalue(res, i, i_prsnamespace)),
6772 prsinfo[i].dobj.catId.oid);
6773 prsinfo[i].prsstart = atooid(PQgetvalue(res, i, i_prsstart));
6774 prsinfo[i].prstoken = atooid(PQgetvalue(res, i, i_prstoken));
6775 prsinfo[i].prsend = atooid(PQgetvalue(res, i, i_prsend));
6776 prsinfo[i].prsheadline = atooid(PQgetvalue(res, i, i_prsheadline));
6777 prsinfo[i].prslextype = atooid(PQgetvalue(res, i, i_prslextype));
6779 /* Decide whether we want to dump it */
6780 selectDumpableObject(&(prsinfo[i].dobj));
6785 destroyPQExpBuffer(query);
6791 * getTSDictionaries:
6792 * read all text search dictionaries in the system catalogs and return them
6793 * in the TSDictInfo* structure
6795 * numTSDicts is set to the number of dictionaries read in
6798 getTSDictionaries(Archive *fout, int *numTSDicts)
6804 TSDictInfo *dictinfo;
6808 int i_dictnamespace;
6811 int i_dictinitoption;
6813 /* Before 8.3, there is no built-in text search support */
6814 if (fout->remoteVersion < 80300)
6820 query = createPQExpBuffer();
6822 /* Make sure we are in proper schema */
6823 selectSourceSchema(fout, "pg_catalog");
6825 appendPQExpBuffer(query, "SELECT tableoid, oid, dictname, "
6826 "dictnamespace, (%s dictowner) AS rolname, "
6827 "dicttemplate, dictinitoption "
6831 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6833 ntups = PQntuples(res);
6834 *numTSDicts = ntups;
6836 dictinfo = (TSDictInfo *) pg_malloc(ntups * sizeof(TSDictInfo));
6838 i_tableoid = PQfnumber(res, "tableoid");
6839 i_oid = PQfnumber(res, "oid");
6840 i_dictname = PQfnumber(res, "dictname");
6841 i_dictnamespace = PQfnumber(res, "dictnamespace");
6842 i_rolname = PQfnumber(res, "rolname");
6843 i_dictinitoption = PQfnumber(res, "dictinitoption");
6844 i_dicttemplate = PQfnumber(res, "dicttemplate");
6846 for (i = 0; i < ntups; i++)
6848 dictinfo[i].dobj.objType = DO_TSDICT;
6849 dictinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6850 dictinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6851 AssignDumpId(&dictinfo[i].dobj);
6852 dictinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_dictname));
6853 dictinfo[i].dobj.namespace =
6855 atooid(PQgetvalue(res, i, i_dictnamespace)),
6856 dictinfo[i].dobj.catId.oid);
6857 dictinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
6858 dictinfo[i].dicttemplate = atooid(PQgetvalue(res, i, i_dicttemplate));
6859 if (PQgetisnull(res, i, i_dictinitoption))
6860 dictinfo[i].dictinitoption = NULL;
6862 dictinfo[i].dictinitoption = pg_strdup(PQgetvalue(res, i, i_dictinitoption));
6864 /* Decide whether we want to dump it */
6865 selectDumpableObject(&(dictinfo[i].dobj));
6870 destroyPQExpBuffer(query);
6877 * read all text search templates in the system catalogs and return them
6878 * in the TSTemplateInfo* structure
6880 * numTSTemplates is set to the number of templates read in
6883 getTSTemplates(Archive *fout, int *numTSTemplates)
6889 TSTemplateInfo *tmplinfo;
6893 int i_tmplnamespace;
6897 /* Before 8.3, there is no built-in text search support */
6898 if (fout->remoteVersion < 80300)
6900 *numTSTemplates = 0;
6904 query = createPQExpBuffer();
6906 /* Make sure we are in proper schema */
6907 selectSourceSchema(fout, "pg_catalog");
6909 appendPQExpBuffer(query, "SELECT tableoid, oid, tmplname, "
6910 "tmplnamespace, tmplinit::oid, tmpllexize::oid "
6911 "FROM pg_ts_template");
6913 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6915 ntups = PQntuples(res);
6916 *numTSTemplates = ntups;
6918 tmplinfo = (TSTemplateInfo *) pg_malloc(ntups * sizeof(TSTemplateInfo));
6920 i_tableoid = PQfnumber(res, "tableoid");
6921 i_oid = PQfnumber(res, "oid");
6922 i_tmplname = PQfnumber(res, "tmplname");
6923 i_tmplnamespace = PQfnumber(res, "tmplnamespace");
6924 i_tmplinit = PQfnumber(res, "tmplinit");
6925 i_tmpllexize = PQfnumber(res, "tmpllexize");
6927 for (i = 0; i < ntups; i++)
6929 tmplinfo[i].dobj.objType = DO_TSTEMPLATE;
6930 tmplinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
6931 tmplinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
6932 AssignDumpId(&tmplinfo[i].dobj);
6933 tmplinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_tmplname));
6934 tmplinfo[i].dobj.namespace =
6936 atooid(PQgetvalue(res, i, i_tmplnamespace)),
6937 tmplinfo[i].dobj.catId.oid);
6938 tmplinfo[i].tmplinit = atooid(PQgetvalue(res, i, i_tmplinit));
6939 tmplinfo[i].tmpllexize = atooid(PQgetvalue(res, i, i_tmpllexize));
6941 /* Decide whether we want to dump it */
6942 selectDumpableObject(&(tmplinfo[i].dobj));
6947 destroyPQExpBuffer(query);
6953 * getTSConfigurations:
6954 * read all text search configurations in the system catalogs and return
6955 * them in the TSConfigInfo* structure
6957 * numTSConfigs is set to the number of configurations read in
6960 getTSConfigurations(Archive *fout, int *numTSConfigs)
6966 TSConfigInfo *cfginfo;
6974 /* Before 8.3, there is no built-in text search support */
6975 if (fout->remoteVersion < 80300)
6981 query = createPQExpBuffer();
6983 /* Make sure we are in proper schema */
6984 selectSourceSchema(fout, "pg_catalog");
6986 appendPQExpBuffer(query, "SELECT tableoid, oid, cfgname, "
6987 "cfgnamespace, (%s cfgowner) AS rolname, cfgparser "
6988 "FROM pg_ts_config",
6991 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
6993 ntups = PQntuples(res);
6994 *numTSConfigs = ntups;
6996 cfginfo = (TSConfigInfo *) pg_malloc(ntups * sizeof(TSConfigInfo));
6998 i_tableoid = PQfnumber(res, "tableoid");
6999 i_oid = PQfnumber(res, "oid");
7000 i_cfgname = PQfnumber(res, "cfgname");
7001 i_cfgnamespace = PQfnumber(res, "cfgnamespace");
7002 i_rolname = PQfnumber(res, "rolname");
7003 i_cfgparser = PQfnumber(res, "cfgparser");
7005 for (i = 0; i < ntups; i++)
7007 cfginfo[i].dobj.objType = DO_TSCONFIG;
7008 cfginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7009 cfginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7010 AssignDumpId(&cfginfo[i].dobj);
7011 cfginfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_cfgname));
7012 cfginfo[i].dobj.namespace =
7014 atooid(PQgetvalue(res, i, i_cfgnamespace)),
7015 cfginfo[i].dobj.catId.oid);
7016 cfginfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
7017 cfginfo[i].cfgparser = atooid(PQgetvalue(res, i, i_cfgparser));
7019 /* Decide whether we want to dump it */
7020 selectDumpableObject(&(cfginfo[i].dobj));
7025 destroyPQExpBuffer(query);
7031 * getForeignDataWrappers:
7032 * read all foreign-data wrappers in the system catalogs and return
7033 * them in the FdwInfo* structure
7035 * numForeignDataWrappers is set to the number of fdws read in
7038 getForeignDataWrappers(Archive *fout, int *numForeignDataWrappers)
7043 PQExpBuffer query = createPQExpBuffer();
7054 /* Before 8.4, there are no foreign-data wrappers */
7055 if (fout->remoteVersion < 80400)
7057 *numForeignDataWrappers = 0;
7061 /* Make sure we are in proper schema */
7062 selectSourceSchema(fout, "pg_catalog");
7064 if (fout->remoteVersion >= 90100)
7066 appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
7067 "(%s fdwowner) AS rolname, "
7068 "fdwhandler::pg_catalog.regproc, "
7069 "fdwvalidator::pg_catalog.regproc, fdwacl, "
7070 "array_to_string(ARRAY("
7071 "SELECT quote_ident(option_name) || ' ' || "
7072 "quote_literal(option_value) "
7073 "FROM pg_options_to_table(fdwoptions) "
7074 "ORDER BY option_name"
7075 "), E',\n ') AS fdwoptions "
7076 "FROM pg_foreign_data_wrapper",
7081 appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
7082 "(%s fdwowner) AS rolname, "
7083 "'-' AS fdwhandler, "
7084 "fdwvalidator::pg_catalog.regproc, fdwacl, "
7085 "array_to_string(ARRAY("
7086 "SELECT quote_ident(option_name) || ' ' || "
7087 "quote_literal(option_value) "
7088 "FROM pg_options_to_table(fdwoptions) "
7089 "ORDER BY option_name"
7090 "), E',\n ') AS fdwoptions "
7091 "FROM pg_foreign_data_wrapper",
7095 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7097 ntups = PQntuples(res);
7098 *numForeignDataWrappers = ntups;
7100 fdwinfo = (FdwInfo *) pg_malloc(ntups * sizeof(FdwInfo));
7102 i_tableoid = PQfnumber(res, "tableoid");
7103 i_oid = PQfnumber(res, "oid");
7104 i_fdwname = PQfnumber(res, "fdwname");
7105 i_rolname = PQfnumber(res, "rolname");
7106 i_fdwhandler = PQfnumber(res, "fdwhandler");
7107 i_fdwvalidator = PQfnumber(res, "fdwvalidator");
7108 i_fdwacl = PQfnumber(res, "fdwacl");
7109 i_fdwoptions = PQfnumber(res, "fdwoptions");
7111 for (i = 0; i < ntups; i++)
7113 fdwinfo[i].dobj.objType = DO_FDW;
7114 fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7115 fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7116 AssignDumpId(&fdwinfo[i].dobj);
7117 fdwinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_fdwname));
7118 fdwinfo[i].dobj.namespace = NULL;
7119 fdwinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
7120 fdwinfo[i].fdwhandler = pg_strdup(PQgetvalue(res, i, i_fdwhandler));
7121 fdwinfo[i].fdwvalidator = pg_strdup(PQgetvalue(res, i, i_fdwvalidator));
7122 fdwinfo[i].fdwoptions = pg_strdup(PQgetvalue(res, i, i_fdwoptions));
7123 fdwinfo[i].fdwacl = pg_strdup(PQgetvalue(res, i, i_fdwacl));
7125 /* Decide whether we want to dump it */
7126 selectDumpableObject(&(fdwinfo[i].dobj));
7131 destroyPQExpBuffer(query);
7137 * getForeignServers:
7138 * read all foreign servers in the system catalogs and return
7139 * them in the ForeignServerInfo * structure
7141 * numForeignServers is set to the number of servers read in
7144 getForeignServers(Archive *fout, int *numForeignServers)
7149 PQExpBuffer query = createPQExpBuffer();
7150 ForeignServerInfo *srvinfo;
7161 /* Before 8.4, there are no foreign servers */
7162 if (fout->remoteVersion < 80400)
7164 *numForeignServers = 0;
7168 /* Make sure we are in proper schema */
7169 selectSourceSchema(fout, "pg_catalog");
7171 appendPQExpBuffer(query, "SELECT tableoid, oid, srvname, "
7172 "(%s srvowner) AS rolname, "
7173 "srvfdw, srvtype, srvversion, srvacl,"
7174 "array_to_string(ARRAY("
7175 "SELECT quote_ident(option_name) || ' ' || "
7176 "quote_literal(option_value) "
7177 "FROM pg_options_to_table(srvoptions) "
7178 "ORDER BY option_name"
7179 "), E',\n ') AS srvoptions "
7180 "FROM pg_foreign_server",
7183 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7185 ntups = PQntuples(res);
7186 *numForeignServers = ntups;
7188 srvinfo = (ForeignServerInfo *) pg_malloc(ntups * sizeof(ForeignServerInfo));
7190 i_tableoid = PQfnumber(res, "tableoid");
7191 i_oid = PQfnumber(res, "oid");
7192 i_srvname = PQfnumber(res, "srvname");
7193 i_rolname = PQfnumber(res, "rolname");
7194 i_srvfdw = PQfnumber(res, "srvfdw");
7195 i_srvtype = PQfnumber(res, "srvtype");
7196 i_srvversion = PQfnumber(res, "srvversion");
7197 i_srvacl = PQfnumber(res, "srvacl");
7198 i_srvoptions = PQfnumber(res, "srvoptions");
7200 for (i = 0; i < ntups; i++)
7202 srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
7203 srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7204 srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7205 AssignDumpId(&srvinfo[i].dobj);
7206 srvinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_srvname));
7207 srvinfo[i].dobj.namespace = NULL;
7208 srvinfo[i].rolname = pg_strdup(PQgetvalue(res, i, i_rolname));
7209 srvinfo[i].srvfdw = atooid(PQgetvalue(res, i, i_srvfdw));
7210 srvinfo[i].srvtype = pg_strdup(PQgetvalue(res, i, i_srvtype));
7211 srvinfo[i].srvversion = pg_strdup(PQgetvalue(res, i, i_srvversion));
7212 srvinfo[i].srvoptions = pg_strdup(PQgetvalue(res, i, i_srvoptions));
7213 srvinfo[i].srvacl = pg_strdup(PQgetvalue(res, i, i_srvacl));
7215 /* Decide whether we want to dump it */
7216 selectDumpableObject(&(srvinfo[i].dobj));
7221 destroyPQExpBuffer(query);
7228 * read all default ACL information in the system catalogs and return
7229 * them in the DefaultACLInfo structure
7231 * numDefaultACLs is set to the number of ACLs read in
7234 getDefaultACLs(Archive *fout, int *numDefaultACLs)
7236 DefaultACLInfo *daclinfo;
7242 int i_defaclnamespace;
7243 int i_defaclobjtype;
7248 if (fout->remoteVersion < 90000)
7250 *numDefaultACLs = 0;
7254 query = createPQExpBuffer();
7256 /* Make sure we are in proper schema */
7257 selectSourceSchema(fout, "pg_catalog");
7259 appendPQExpBuffer(query, "SELECT oid, tableoid, "
7260 "(%s defaclrole) AS defaclrole, "
7264 "FROM pg_default_acl",
7267 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7269 ntups = PQntuples(res);
7270 *numDefaultACLs = ntups;
7272 daclinfo = (DefaultACLInfo *) pg_malloc(ntups * sizeof(DefaultACLInfo));
7274 i_oid = PQfnumber(res, "oid");
7275 i_tableoid = PQfnumber(res, "tableoid");
7276 i_defaclrole = PQfnumber(res, "defaclrole");
7277 i_defaclnamespace = PQfnumber(res, "defaclnamespace");
7278 i_defaclobjtype = PQfnumber(res, "defaclobjtype");
7279 i_defaclacl = PQfnumber(res, "defaclacl");
7281 for (i = 0; i < ntups; i++)
7283 Oid nspid = atooid(PQgetvalue(res, i, i_defaclnamespace));
7285 daclinfo[i].dobj.objType = DO_DEFAULT_ACL;
7286 daclinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
7287 daclinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
7288 AssignDumpId(&daclinfo[i].dobj);
7289 /* cheesy ... is it worth coming up with a better object name? */
7290 daclinfo[i].dobj.name = pg_strdup(PQgetvalue(res, i, i_defaclobjtype));
7292 if (nspid != InvalidOid)
7293 daclinfo[i].dobj.namespace = findNamespace(fout, nspid,
7294 daclinfo[i].dobj.catId.oid);
7296 daclinfo[i].dobj.namespace = NULL;
7298 daclinfo[i].defaclrole = pg_strdup(PQgetvalue(res, i, i_defaclrole));
7299 daclinfo[i].defaclobjtype = *(PQgetvalue(res, i, i_defaclobjtype));
7300 daclinfo[i].defaclacl = pg_strdup(PQgetvalue(res, i, i_defaclacl));
7302 /* Decide whether we want to dump it */
7303 selectDumpableDefaultACL(&(daclinfo[i]));
7308 destroyPQExpBuffer(query);
7316 * This routine is used to dump any comments associated with the
7317 * object handed to this routine. The routine takes a constant character
7318 * string for the target part of the comment-creation command, plus
7319 * the namespace and owner of the object (for labeling the ArchiveEntry),
7320 * plus catalog ID and subid which are the lookup key for pg_description,
7321 * plus the dump ID for the object (for setting a dependency).
7322 * If a matching pg_description entry is found, it is dumped.
7324 * Note: although this routine takes a dumpId for dependency purposes,
7325 * that purpose is just to mark the dependency in the emitted dump file
7326 * for possible future use by pg_restore. We do NOT use it for determining
7327 * ordering of the comment in the dump file, because this routine is called
7328 * after dependency sorting occurs. This routine should be called just after
7329 * calling ArchiveEntry() for the specified object.
7332 dumpComment(Archive *fout, const char *target,
7333 const char *namespace, const char *owner,
7334 CatalogId catalogId, int subid, DumpId dumpId)
7336 CommentItem *comments;
7339 /* Comments are schema not data ... except blob comments are data */
7340 if (strncmp(target, "LARGE OBJECT ", 13) != 0)
7351 /* Search for comments associated with catalogId, using table */
7352 ncomments = findComments(fout, catalogId.tableoid, catalogId.oid,
7355 /* Is there one matching the subid? */
7356 while (ncomments > 0)
7358 if (comments->objsubid == subid)
7364 /* If a comment exists, build COMMENT ON statement */
7367 PQExpBuffer query = createPQExpBuffer();
7369 appendPQExpBuffer(query, "COMMENT ON %s IS ", target);
7370 appendStringLiteralAH(query, comments->descr, fout);
7371 appendPQExpBuffer(query, ";\n");
7374 * We mark comments as SECTION_NONE because they really belong in the
7375 * same section as their parent, whether that is pre-data or
7378 ArchiveEntry(fout, nilCatalogId, createDumpId(),
7379 target, namespace, NULL, owner,
7380 false, "COMMENT", SECTION_NONE,
7381 query->data, "", NULL,
7385 destroyPQExpBuffer(query);
7390 * dumpTableComment --
7392 * As above, but dump comments for both the specified table (or view)
7396 dumpTableComment(Archive *fout, TableInfo *tbinfo,
7397 const char *reltypename)
7399 CommentItem *comments;
7404 /* Comments are SCHEMA not data */
7408 /* Search for comments associated with relation, using table */
7409 ncomments = findComments(fout,
7410 tbinfo->dobj.catId.tableoid,
7411 tbinfo->dobj.catId.oid,
7414 /* If comments exist, build COMMENT ON statements */
7418 query = createPQExpBuffer();
7419 target = createPQExpBuffer();
7421 while (ncomments > 0)
7423 const char *descr = comments->descr;
7424 int objsubid = comments->objsubid;
7428 resetPQExpBuffer(target);
7429 appendPQExpBuffer(target, "%s %s", reltypename,
7430 fmtId(tbinfo->dobj.name));
7432 resetPQExpBuffer(query);
7433 appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
7434 appendStringLiteralAH(query, descr, fout);
7435 appendPQExpBuffer(query, ";\n");
7437 ArchiveEntry(fout, nilCatalogId, createDumpId(),
7439 tbinfo->dobj.namespace->dobj.name,
7440 NULL, tbinfo->rolname,
7441 false, "COMMENT", SECTION_NONE,
7442 query->data, "", NULL,
7443 &(tbinfo->dobj.dumpId), 1,
7446 else if (objsubid > 0 && objsubid <= tbinfo->numatts)
7448 resetPQExpBuffer(target);
7449 appendPQExpBuffer(target, "COLUMN %s.",
7450 fmtId(tbinfo->dobj.name));
7451 appendPQExpBuffer(target, "%s",
7452 fmtId(tbinfo->attnames[objsubid - 1]));
7454 resetPQExpBuffer(query);
7455 appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
7456 appendStringLiteralAH(query, descr, fout);
7457 appendPQExpBuffer(query, ";\n");
7459 ArchiveEntry(fout, nilCatalogId, createDumpId(),
7461 tbinfo->dobj.namespace->dobj.name,
7462 NULL, tbinfo->rolname,
7463 false, "COMMENT", SECTION_NONE,
7464 query->data, "", NULL,
7465 &(tbinfo->dobj.dumpId), 1,
7473 destroyPQExpBuffer(query);
7474 destroyPQExpBuffer(target);
7480 * Find the comment(s), if any, associated with the given object. All the
7481 * objsubid values associated with the given classoid/objoid are found with
7485 findComments(Archive *fout, Oid classoid, Oid objoid,
7486 CommentItem **items)
7488 /* static storage for table of comments */
7489 static CommentItem *comments = NULL;
7490 static int ncomments = -1;
7492 CommentItem *middle = NULL;
7497 /* Get comments if we didn't already */
7499 ncomments = collectComments(fout, &comments);
7502 * Pre-7.2, pg_description does not contain classoid, so collectComments
7503 * just stores a zero. If there's a collision on object OID, well, you
7504 * get duplicate comments.
7506 if (fout->remoteVersion < 70200)
7510 * Do binary search to find some item matching the object.
7513 high = &comments[ncomments - 1];
7516 middle = low + (high - low) / 2;
7518 if (classoid < middle->classoid)
7520 else if (classoid > middle->classoid)
7522 else if (objoid < middle->objoid)
7524 else if (objoid > middle->objoid)
7527 break; /* found a match */
7530 if (low > high) /* no matches */
7537 * Now determine how many items match the object. The search loop
7538 * invariant still holds: only items between low and high inclusive could
7542 while (middle > low)
7544 if (classoid != middle[-1].classoid ||
7545 objoid != middle[-1].objoid)
7554 while (middle <= high)
7556 if (classoid != middle->classoid ||
7557 objoid != middle->objoid)
7567 * collectComments --
7569 * Construct a table of all comments available for database objects.
7570 * We used to do per-object queries for the comments, but it's much faster
7571 * to pull them all over at once, and on most databases the memory cost
7574 * The table is sorted by classoid/objid/objsubid for speed in lookup.
7577 collectComments(Archive *fout, CommentItem **items)
7587 CommentItem *comments;
7590 * Note we do NOT change source schema here; preserve the caller's
7594 query = createPQExpBuffer();
7596 if (fout->remoteVersion >= 70300)
7598 appendPQExpBuffer(query, "SELECT description, classoid, objoid, objsubid "
7599 "FROM pg_catalog.pg_description "
7600 "ORDER BY classoid, objoid, objsubid");
7602 else if (fout->remoteVersion >= 70200)
7604 appendPQExpBuffer(query, "SELECT description, classoid, objoid, objsubid "
7605 "FROM pg_description "
7606 "ORDER BY classoid, objoid, objsubid");
7610 /* Note: this will fail to find attribute comments in pre-7.2... */
7611 appendPQExpBuffer(query, "SELECT description, 0 AS classoid, objoid, 0 AS objsubid "
7612 "FROM pg_description "
7616 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
7618 /* Construct lookup table containing OIDs in numeric form */
7620 i_description = PQfnumber(res, "description");
7621 i_classoid = PQfnumber(res, "classoid");
7622 i_objoid = PQfnumber(res, "objoid");
7623 i_objsubid = PQfnumber(res, "objsubid");
7625 ntups = PQntuples(res);
7627 comments = (CommentItem *) pg_malloc(ntups * sizeof(CommentItem));
7629 for (i = 0; i < ntups; i++)
7631 comments[i].descr = PQgetvalue(res, i, i_description);
7632 comments[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
7633 comments[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
7634 comments[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
7637 /* Do NOT free the PGresult since we are keeping pointers into it */
7638 destroyPQExpBuffer(query);
7645 * dumpDumpableObject
7647 * This routine and its subsidiaries are responsible for creating
7648 * ArchiveEntries (TOC objects) for each object to be dumped.
7651 dumpDumpableObject(Archive *fout, DumpableObject *dobj)
7653 switch (dobj->objType)
7656 dumpNamespace(fout, (NamespaceInfo *) dobj);
7659 dumpExtension(fout, (ExtensionInfo *) dobj);
7662 dumpType(fout, (TypeInfo *) dobj);
7665 dumpShellType(fout, (ShellTypeInfo *) dobj);
7668 dumpFunc(fout, (FuncInfo *) dobj);
7671 dumpAgg(fout, (AggInfo *) dobj);
7674 dumpOpr(fout, (OprInfo *) dobj);
7677 dumpOpclass(fout, (OpclassInfo *) dobj);
7680 dumpOpfamily(fout, (OpfamilyInfo *) dobj);
7683 dumpCollation(fout, (CollInfo *) dobj);
7686 dumpConversion(fout, (ConvInfo *) dobj);
7689 dumpTable(fout, (TableInfo *) dobj);
7692 dumpAttrDef(fout, (AttrDefInfo *) dobj);
7695 dumpIndex(fout, (IndxInfo *) dobj);
7697 case DO_REFRESH_MATVIEW:
7698 refreshMatViewData(fout, (TableDataInfo *) dobj);
7701 dumpRule(fout, (RuleInfo *) dobj);
7704 dumpTrigger(fout, (TriggerInfo *) dobj);
7706 case DO_EVENT_TRIGGER:
7707 dumpEventTrigger(fout, (EventTriggerInfo *) dobj);
7710 dumpConstraint(fout, (ConstraintInfo *) dobj);
7712 case DO_FK_CONSTRAINT:
7713 dumpConstraint(fout, (ConstraintInfo *) dobj);
7716 dumpProcLang(fout, (ProcLangInfo *) dobj);
7719 dumpCast(fout, (CastInfo *) dobj);
7722 if (((TableDataInfo *) dobj)->tdtable->relkind == RELKIND_SEQUENCE)
7723 dumpSequenceData(fout, (TableDataInfo *) dobj);
7725 dumpTableData(fout, (TableDataInfo *) dobj);
7728 /* table rowtypes and array types are never dumped separately */
7731 dumpTSParser(fout, (TSParserInfo *) dobj);
7734 dumpTSDictionary(fout, (TSDictInfo *) dobj);
7737 dumpTSTemplate(fout, (TSTemplateInfo *) dobj);
7740 dumpTSConfig(fout, (TSConfigInfo *) dobj);
7743 dumpForeignDataWrapper(fout, (FdwInfo *) dobj);
7745 case DO_FOREIGN_SERVER:
7746 dumpForeignServer(fout, (ForeignServerInfo *) dobj);
7748 case DO_DEFAULT_ACL:
7749 dumpDefaultACL(fout, (DefaultACLInfo *) dobj);
7752 dumpBlob(fout, (BlobInfo *) dobj);
7755 ArchiveEntry(fout, dobj->catId, dobj->dumpId,
7756 dobj->name, NULL, NULL, "",
7757 false, "BLOBS", SECTION_DATA,
7762 case DO_PRE_DATA_BOUNDARY:
7763 case DO_POST_DATA_BOUNDARY:
7764 /* never dumped, nothing to do */
7771 * writes out to fout the queries to recreate a user-defined namespace
7774 dumpNamespace(Archive *fout, NamespaceInfo *nspinfo)
7781 /* Skip if not to be dumped */
7782 if (!nspinfo->dobj.dump || dataOnly)
7785 /* don't dump dummy namespace from pre-7.3 source */
7786 if (strlen(nspinfo->dobj.name) == 0)
7789 q = createPQExpBuffer();
7790 delq = createPQExpBuffer();
7791 labelq = createPQExpBuffer();
7793 qnspname = pg_strdup(fmtId(nspinfo->dobj.name));
7795 appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
7797 appendPQExpBuffer(q, "CREATE SCHEMA %s;\n", qnspname);
7799 appendPQExpBuffer(labelq, "SCHEMA %s", qnspname);
7802 binary_upgrade_extension_member(q, &nspinfo->dobj, labelq->data);
7804 ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
7808 false, "SCHEMA", SECTION_PRE_DATA,
7809 q->data, delq->data, NULL,
7813 /* Dump Schema Comments and Security Labels */
7814 dumpComment(fout, labelq->data,
7815 NULL, nspinfo->rolname,
7816 nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
7817 dumpSecLabel(fout, labelq->data,
7818 NULL, nspinfo->rolname,
7819 nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
7821 dumpACL(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId, "SCHEMA",
7822 qnspname, NULL, nspinfo->dobj.name, NULL,
7823 nspinfo->rolname, nspinfo->nspacl);
7827 destroyPQExpBuffer(q);
7828 destroyPQExpBuffer(delq);
7829 destroyPQExpBuffer(labelq);
7834 * writes out to fout the queries to recreate an extension
7837 dumpExtension(Archive *fout, ExtensionInfo *extinfo)
7844 /* Skip if not to be dumped */
7845 if (!extinfo->dobj.dump || dataOnly)
7848 q = createPQExpBuffer();
7849 delq = createPQExpBuffer();
7850 labelq = createPQExpBuffer();
7852 qextname = pg_strdup(fmtId(extinfo->dobj.name));
7854 appendPQExpBuffer(delq, "DROP EXTENSION %s;\n", qextname);
7856 if (!binary_upgrade)
7859 * In a regular dump, we use IF NOT EXISTS so that there isn't a
7860 * problem if the extension already exists in the target database;
7861 * this is essential for installed-by-default extensions such as
7864 * In binary-upgrade mode, that doesn't work well, so instead we skip
7865 * built-in extensions based on their OIDs; see
7866 * selectDumpableExtension.
7868 appendPQExpBuffer(q, "CREATE EXTENSION IF NOT EXISTS %s WITH SCHEMA %s;\n",
7869 qextname, fmtId(extinfo->namespace));
7876 appendPQExpBuffer(q, "-- For binary upgrade, create an empty extension and insert objects into it\n");
7879 * We unconditionally create the extension, so we must drop it if it
7880 * exists. This could happen if the user deleted 'plpgsql' and then
7881 * readded it, causing its oid to be greater than FirstNormalObjectId.
7882 * The FirstNormalObjectId test was kept to avoid repeatedly dropping
7883 * and recreating extensions like 'plpgsql'.
7885 appendPQExpBuffer(q, "DROP EXTENSION IF EXISTS %s;\n", qextname);
7887 appendPQExpBuffer(q,
7888 "SELECT binary_upgrade.create_empty_extension(");
7889 appendStringLiteralAH(q, extinfo->dobj.name, fout);
7890 appendPQExpBuffer(q, ", ");
7891 appendStringLiteralAH(q, extinfo->namespace, fout);
7892 appendPQExpBuffer(q, ", ");
7893 appendPQExpBuffer(q, "%s, ", extinfo->relocatable ? "true" : "false");
7894 appendStringLiteralAH(q, extinfo->extversion, fout);
7895 appendPQExpBuffer(q, ", ");
7898 * Note that we're pushing extconfig (an OID array) back into
7899 * pg_extension exactly as-is. This is OK because pg_class OIDs are
7900 * preserved in binary upgrade.
7902 if (strlen(extinfo->extconfig) > 2)
7903 appendStringLiteralAH(q, extinfo->extconfig, fout);
7905 appendPQExpBuffer(q, "NULL");
7906 appendPQExpBuffer(q, ", ");
7907 if (strlen(extinfo->extcondition) > 2)
7908 appendStringLiteralAH(q, extinfo->extcondition, fout);
7910 appendPQExpBuffer(q, "NULL");
7911 appendPQExpBuffer(q, ", ");
7912 appendPQExpBuffer(q, "ARRAY[");
7914 for (i = 0; i < extinfo->dobj.nDeps; i++)
7916 DumpableObject *extobj;
7918 extobj = findObjectByDumpId(extinfo->dobj.dependencies[i]);
7919 if (extobj && extobj->objType == DO_EXTENSION)
7922 appendPQExpBuffer(q, ",");
7923 appendStringLiteralAH(q, extobj->name, fout);
7926 appendPQExpBuffer(q, "]::pg_catalog.text[]");
7927 appendPQExpBuffer(q, ");\n");
7930 appendPQExpBuffer(labelq, "EXTENSION %s", qextname);
7932 ArchiveEntry(fout, extinfo->dobj.catId, extinfo->dobj.dumpId,
7936 false, "EXTENSION", SECTION_PRE_DATA,
7937 q->data, delq->data, NULL,
7941 /* Dump Extension Comments and Security Labels */
7942 dumpComment(fout, labelq->data,
7944 extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
7945 dumpSecLabel(fout, labelq->data,
7947 extinfo->dobj.catId, 0, extinfo->dobj.dumpId);
7951 destroyPQExpBuffer(q);
7952 destroyPQExpBuffer(delq);
7953 destroyPQExpBuffer(labelq);
7958 * writes out to fout the queries to recreate a user-defined type
7961 dumpType(Archive *fout, TypeInfo *tyinfo)
7963 /* Skip if not to be dumped */
7964 if (!tyinfo->dobj.dump || dataOnly)
7967 /* Dump out in proper style */
7968 if (tyinfo->typtype == TYPTYPE_BASE)
7969 dumpBaseType(fout, tyinfo);
7970 else if (tyinfo->typtype == TYPTYPE_DOMAIN)
7971 dumpDomain(fout, tyinfo);
7972 else if (tyinfo->typtype == TYPTYPE_COMPOSITE)
7973 dumpCompositeType(fout, tyinfo);
7974 else if (tyinfo->typtype == TYPTYPE_ENUM)
7975 dumpEnumType(fout, tyinfo);
7976 else if (tyinfo->typtype == TYPTYPE_RANGE)
7977 dumpRangeType(fout, tyinfo);
7979 write_msg(NULL, "WARNING: typtype of data type \"%s\" appears to be invalid\n",
7985 * writes out to fout the queries to recreate a user-defined enum type
7988 dumpEnumType(Archive *fout, TypeInfo *tyinfo)
7990 PQExpBuffer q = createPQExpBuffer();
7991 PQExpBuffer delq = createPQExpBuffer();
7992 PQExpBuffer labelq = createPQExpBuffer();
7993 PQExpBuffer query = createPQExpBuffer();
8001 /* Set proper schema search path */
8002 selectSourceSchema(fout, "pg_catalog");
8004 if (fout->remoteVersion >= 90100)
8005 appendPQExpBuffer(query, "SELECT oid, enumlabel "
8006 "FROM pg_catalog.pg_enum "
8007 "WHERE enumtypid = '%u'"
8008 "ORDER BY enumsortorder",
8009 tyinfo->dobj.catId.oid);
8011 appendPQExpBuffer(query, "SELECT oid, enumlabel "
8012 "FROM pg_catalog.pg_enum "
8013 "WHERE enumtypid = '%u'"
8015 tyinfo->dobj.catId.oid);
8017 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8019 num = PQntuples(res);
8021 qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
8024 * DROP must be fully qualified in case same name appears in pg_catalog.
8025 * CASCADE shouldn't be required here as for normal types since the I/O
8026 * functions are generic and do not get dropped.
8028 appendPQExpBuffer(delq, "DROP TYPE %s.",
8029 fmtId(tyinfo->dobj.namespace->dobj.name));
8030 appendPQExpBuffer(delq, "%s;\n",
8034 binary_upgrade_set_type_oids_by_type_oid(fout, q,
8035 tyinfo->dobj.catId.oid);
8037 appendPQExpBuffer(q, "CREATE TYPE %s AS ENUM (",
8040 if (!binary_upgrade)
8042 /* Labels with server-assigned oids */
8043 for (i = 0; i < num; i++)
8045 label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
8047 appendPQExpBuffer(q, ",");
8048 appendPQExpBuffer(q, "\n ");
8049 appendStringLiteralAH(q, label, fout);
8053 appendPQExpBuffer(q, "\n);\n");
8057 /* Labels with dump-assigned (preserved) oids */
8058 for (i = 0; i < num; i++)
8060 enum_oid = atooid(PQgetvalue(res, i, PQfnumber(res, "oid")));
8061 label = PQgetvalue(res, i, PQfnumber(res, "enumlabel"));
8064 appendPQExpBuffer(q, "\n-- For binary upgrade, must preserve pg_enum oids\n");
8065 appendPQExpBuffer(q,
8066 "SELECT binary_upgrade.set_next_pg_enum_oid('%u'::pg_catalog.oid);\n",
8068 appendPQExpBuffer(q, "ALTER TYPE %s.",
8069 fmtId(tyinfo->dobj.namespace->dobj.name));
8070 appendPQExpBuffer(q, "%s ADD VALUE ",
8072 appendStringLiteralAH(q, label, fout);
8073 appendPQExpBuffer(q, ";\n\n");
8077 appendPQExpBuffer(labelq, "TYPE %s", qtypname);
8080 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
8082 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
8084 tyinfo->dobj.namespace->dobj.name,
8086 tyinfo->rolname, false,
8087 "TYPE", SECTION_PRE_DATA,
8088 q->data, delq->data, NULL,
8092 /* Dump Type Comments and Security Labels */
8093 dumpComment(fout, labelq->data,
8094 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8095 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8096 dumpSecLabel(fout, labelq->data,
8097 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8098 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8100 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
8101 qtypname, NULL, tyinfo->dobj.name,
8102 tyinfo->dobj.namespace->dobj.name,
8103 tyinfo->rolname, tyinfo->typacl);
8106 destroyPQExpBuffer(q);
8107 destroyPQExpBuffer(delq);
8108 destroyPQExpBuffer(labelq);
8109 destroyPQExpBuffer(query);
8114 * writes out to fout the queries to recreate a user-defined range type
8117 dumpRangeType(Archive *fout, TypeInfo *tyinfo)
8119 PQExpBuffer q = createPQExpBuffer();
8120 PQExpBuffer delq = createPQExpBuffer();
8121 PQExpBuffer labelq = createPQExpBuffer();
8122 PQExpBuffer query = createPQExpBuffer();
8129 * select appropriate schema to ensure names in CREATE are properly
8132 selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
8134 appendPQExpBuffer(query,
8135 "SELECT pg_catalog.format_type(rngsubtype, NULL) AS rngsubtype, "
8136 "opc.opcname AS opcname, "
8137 "(SELECT nspname FROM pg_catalog.pg_namespace nsp "
8138 " WHERE nsp.oid = opc.opcnamespace) AS opcnsp, "
8140 "CASE WHEN rngcollation = st.typcollation THEN 0 "
8141 " ELSE rngcollation END AS collation, "
8142 "rngcanonical, rngsubdiff "
8143 "FROM pg_catalog.pg_range r, pg_catalog.pg_type st, "
8144 " pg_catalog.pg_opclass opc "
8145 "WHERE st.oid = rngsubtype AND opc.oid = rngsubopc AND "
8147 tyinfo->dobj.catId.oid);
8149 res = ExecuteSqlQueryForSingleRow(fout, query->data);
8151 qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
8154 * DROP must be fully qualified in case same name appears in pg_catalog.
8155 * CASCADE shouldn't be required here as for normal types since the I/O
8156 * functions are generic and do not get dropped.
8158 appendPQExpBuffer(delq, "DROP TYPE %s.",
8159 fmtId(tyinfo->dobj.namespace->dobj.name));
8160 appendPQExpBuffer(delq, "%s;\n",
8164 binary_upgrade_set_type_oids_by_type_oid(fout,
8165 q, tyinfo->dobj.catId.oid);
8167 appendPQExpBuffer(q, "CREATE TYPE %s AS RANGE (",
8170 appendPQExpBuffer(q, "\n subtype = %s",
8171 PQgetvalue(res, 0, PQfnumber(res, "rngsubtype")));
8173 /* print subtype_opclass only if not default for subtype */
8174 if (PQgetvalue(res, 0, PQfnumber(res, "opcdefault"))[0] != 't')
8176 char *opcname = PQgetvalue(res, 0, PQfnumber(res, "opcname"));
8177 char *nspname = PQgetvalue(res, 0, PQfnumber(res, "opcnsp"));
8179 /* always schema-qualify, don't try to be smart */
8180 appendPQExpBuffer(q, ",\n subtype_opclass = %s.",
8182 appendPQExpBuffer(q, "%s", fmtId(opcname));
8185 collationOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "collation")));
8186 if (OidIsValid(collationOid))
8188 CollInfo *coll = findCollationByOid(collationOid);
8192 /* always schema-qualify, don't try to be smart */
8193 appendPQExpBuffer(q, ",\n collation = %s.",
8194 fmtId(coll->dobj.namespace->dobj.name));
8195 appendPQExpBuffer(q, "%s",
8196 fmtId(coll->dobj.name));
8200 procname = PQgetvalue(res, 0, PQfnumber(res, "rngcanonical"));
8201 if (strcmp(procname, "-") != 0)
8202 appendPQExpBuffer(q, ",\n canonical = %s", procname);
8204 procname = PQgetvalue(res, 0, PQfnumber(res, "rngsubdiff"));
8205 if (strcmp(procname, "-") != 0)
8206 appendPQExpBuffer(q, ",\n subtype_diff = %s", procname);
8208 appendPQExpBuffer(q, "\n);\n");
8210 appendPQExpBuffer(labelq, "TYPE %s", qtypname);
8213 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
8215 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
8217 tyinfo->dobj.namespace->dobj.name,
8219 tyinfo->rolname, false,
8220 "TYPE", SECTION_PRE_DATA,
8221 q->data, delq->data, NULL,
8225 /* Dump Type Comments and Security Labels */
8226 dumpComment(fout, labelq->data,
8227 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8228 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8229 dumpSecLabel(fout, labelq->data,
8230 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8231 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8233 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
8234 qtypname, NULL, tyinfo->dobj.name,
8235 tyinfo->dobj.namespace->dobj.name,
8236 tyinfo->rolname, tyinfo->typacl);
8239 destroyPQExpBuffer(q);
8240 destroyPQExpBuffer(delq);
8241 destroyPQExpBuffer(labelq);
8242 destroyPQExpBuffer(query);
8247 * writes out to fout the queries to recreate a user-defined base type
8250 dumpBaseType(Archive *fout, TypeInfo *tyinfo)
8252 PQExpBuffer q = createPQExpBuffer();
8253 PQExpBuffer delq = createPQExpBuffer();
8254 PQExpBuffer labelq = createPQExpBuffer();
8255 PQExpBuffer query = createPQExpBuffer();
8272 char *typispreferred;
8277 char *typcollatable;
8279 bool typdefault_is_literal = false;
8281 /* Set proper schema search path so regproc references list correctly */
8282 selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
8284 /* Fetch type-specific details */
8285 if (fout->remoteVersion >= 90100)
8287 appendPQExpBuffer(query, "SELECT typlen, "
8288 "typinput, typoutput, typreceive, typsend, "
8289 "typmodin, typmodout, typanalyze, "
8290 "typreceive::pg_catalog.oid AS typreceiveoid, "
8291 "typsend::pg_catalog.oid AS typsendoid, "
8292 "typmodin::pg_catalog.oid AS typmodinoid, "
8293 "typmodout::pg_catalog.oid AS typmodoutoid, "
8294 "typanalyze::pg_catalog.oid AS typanalyzeoid, "
8295 "typcategory, typispreferred, "
8296 "typdelim, typbyval, typalign, typstorage, "
8297 "(typcollation <> 0) AS typcollatable, "
8298 "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
8299 "FROM pg_catalog.pg_type "
8300 "WHERE oid = '%u'::pg_catalog.oid",
8301 tyinfo->dobj.catId.oid);
8303 else if (fout->remoteVersion >= 80400)
8305 appendPQExpBuffer(query, "SELECT typlen, "
8306 "typinput, typoutput, typreceive, typsend, "
8307 "typmodin, typmodout, typanalyze, "
8308 "typreceive::pg_catalog.oid AS typreceiveoid, "
8309 "typsend::pg_catalog.oid AS typsendoid, "
8310 "typmodin::pg_catalog.oid AS typmodinoid, "
8311 "typmodout::pg_catalog.oid AS typmodoutoid, "
8312 "typanalyze::pg_catalog.oid AS typanalyzeoid, "
8313 "typcategory, typispreferred, "
8314 "typdelim, typbyval, typalign, typstorage, "
8315 "false AS typcollatable, "
8316 "pg_catalog.pg_get_expr(typdefaultbin, 0) AS typdefaultbin, typdefault "
8317 "FROM pg_catalog.pg_type "
8318 "WHERE oid = '%u'::pg_catalog.oid",
8319 tyinfo->dobj.catId.oid);
8321 else if (fout->remoteVersion >= 80300)
8323 /* Before 8.4, pg_get_expr does not allow 0 for its second arg */
8324 appendPQExpBuffer(query, "SELECT typlen, "
8325 "typinput, typoutput, typreceive, typsend, "
8326 "typmodin, typmodout, typanalyze, "
8327 "typreceive::pg_catalog.oid AS typreceiveoid, "
8328 "typsend::pg_catalog.oid AS typsendoid, "
8329 "typmodin::pg_catalog.oid AS typmodinoid, "
8330 "typmodout::pg_catalog.oid AS typmodoutoid, "
8331 "typanalyze::pg_catalog.oid AS typanalyzeoid, "
8332 "'U' AS typcategory, false AS typispreferred, "
8333 "typdelim, typbyval, typalign, typstorage, "
8334 "false AS typcollatable, "
8335 "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
8336 "FROM pg_catalog.pg_type "
8337 "WHERE oid = '%u'::pg_catalog.oid",
8338 tyinfo->dobj.catId.oid);
8340 else if (fout->remoteVersion >= 80000)
8342 appendPQExpBuffer(query, "SELECT typlen, "
8343 "typinput, typoutput, typreceive, typsend, "
8344 "'-' AS typmodin, '-' AS typmodout, "
8346 "typreceive::pg_catalog.oid AS typreceiveoid, "
8347 "typsend::pg_catalog.oid AS typsendoid, "
8348 "0 AS typmodinoid, 0 AS typmodoutoid, "
8349 "typanalyze::pg_catalog.oid AS typanalyzeoid, "
8350 "'U' AS typcategory, false AS typispreferred, "
8351 "typdelim, typbyval, typalign, typstorage, "
8352 "false AS typcollatable, "
8353 "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
8354 "FROM pg_catalog.pg_type "
8355 "WHERE oid = '%u'::pg_catalog.oid",
8356 tyinfo->dobj.catId.oid);
8358 else if (fout->remoteVersion >= 70400)
8360 appendPQExpBuffer(query, "SELECT typlen, "
8361 "typinput, typoutput, typreceive, typsend, "
8362 "'-' AS typmodin, '-' AS typmodout, "
8363 "'-' AS typanalyze, "
8364 "typreceive::pg_catalog.oid AS typreceiveoid, "
8365 "typsend::pg_catalog.oid AS typsendoid, "
8366 "0 AS typmodinoid, 0 AS typmodoutoid, "
8367 "0 AS typanalyzeoid, "
8368 "'U' AS typcategory, false AS typispreferred, "
8369 "typdelim, typbyval, typalign, typstorage, "
8370 "false AS typcollatable, "
8371 "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
8372 "FROM pg_catalog.pg_type "
8373 "WHERE oid = '%u'::pg_catalog.oid",
8374 tyinfo->dobj.catId.oid);
8376 else if (fout->remoteVersion >= 70300)
8378 appendPQExpBuffer(query, "SELECT typlen, "
8379 "typinput, typoutput, "
8380 "'-' AS typreceive, '-' AS typsend, "
8381 "'-' AS typmodin, '-' AS typmodout, "
8382 "'-' AS typanalyze, "
8383 "0 AS typreceiveoid, 0 AS typsendoid, "
8384 "0 AS typmodinoid, 0 AS typmodoutoid, "
8385 "0 AS typanalyzeoid, "
8386 "'U' AS typcategory, false AS typispreferred, "
8387 "typdelim, typbyval, typalign, typstorage, "
8388 "false AS typcollatable, "
8389 "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, typdefault "
8390 "FROM pg_catalog.pg_type "
8391 "WHERE oid = '%u'::pg_catalog.oid",
8392 tyinfo->dobj.catId.oid);
8394 else if (fout->remoteVersion >= 70200)
8397 * Note: although pre-7.3 catalogs contain typreceive and typsend,
8398 * ignore them because they are not right.
8400 appendPQExpBuffer(query, "SELECT typlen, "
8401 "typinput, typoutput, "
8402 "'-' AS typreceive, '-' AS typsend, "
8403 "'-' AS typmodin, '-' AS typmodout, "
8404 "'-' AS typanalyze, "
8405 "0 AS typreceiveoid, 0 AS typsendoid, "
8406 "0 AS typmodinoid, 0 AS typmodoutoid, "
8407 "0 AS typanalyzeoid, "
8408 "'U' AS typcategory, false AS typispreferred, "
8409 "typdelim, typbyval, typalign, typstorage, "
8410 "false AS typcollatable, "
8411 "NULL AS typdefaultbin, typdefault "
8413 "WHERE oid = '%u'::oid",
8414 tyinfo->dobj.catId.oid);
8416 else if (fout->remoteVersion >= 70100)
8419 * Ignore pre-7.2 typdefault; the field exists but has an unusable
8422 appendPQExpBuffer(query, "SELECT typlen, "
8423 "typinput, typoutput, "
8424 "'-' AS typreceive, '-' AS typsend, "
8425 "'-' AS typmodin, '-' AS typmodout, "
8426 "'-' AS typanalyze, "
8427 "0 AS typreceiveoid, 0 AS typsendoid, "
8428 "0 AS typmodinoid, 0 AS typmodoutoid, "
8429 "0 AS typanalyzeoid, "
8430 "'U' AS typcategory, false AS typispreferred, "
8431 "typdelim, typbyval, typalign, typstorage, "
8432 "false AS typcollatable, "
8433 "NULL AS typdefaultbin, NULL AS typdefault "
8435 "WHERE oid = '%u'::oid",
8436 tyinfo->dobj.catId.oid);
8440 appendPQExpBuffer(query, "SELECT typlen, "
8441 "typinput, typoutput, "
8442 "'-' AS typreceive, '-' AS typsend, "
8443 "'-' AS typmodin, '-' AS typmodout, "
8444 "'-' AS typanalyze, "
8445 "0 AS typreceiveoid, 0 AS typsendoid, "
8446 "0 AS typmodinoid, 0 AS typmodoutoid, "
8447 "0 AS typanalyzeoid, "
8448 "'U' AS typcategory, false AS typispreferred, "
8449 "typdelim, typbyval, typalign, "
8450 "'p'::char AS typstorage, "
8451 "false AS typcollatable, "
8452 "NULL AS typdefaultbin, NULL AS typdefault "
8454 "WHERE oid = '%u'::oid",
8455 tyinfo->dobj.catId.oid);
8458 res = ExecuteSqlQueryForSingleRow(fout, query->data);
8460 typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
8461 typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
8462 typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
8463 typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
8464 typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
8465 typmodin = PQgetvalue(res, 0, PQfnumber(res, "typmodin"));
8466 typmodout = PQgetvalue(res, 0, PQfnumber(res, "typmodout"));
8467 typanalyze = PQgetvalue(res, 0, PQfnumber(res, "typanalyze"));
8468 typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
8469 typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
8470 typmodinoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodinoid")));
8471 typmodoutoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typmodoutoid")));
8472 typanalyzeoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typanalyzeoid")));
8473 typcategory = PQgetvalue(res, 0, PQfnumber(res, "typcategory"));
8474 typispreferred = PQgetvalue(res, 0, PQfnumber(res, "typispreferred"));
8475 typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
8476 typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
8477 typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
8478 typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
8479 typcollatable = PQgetvalue(res, 0, PQfnumber(res, "typcollatable"));
8480 if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
8481 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
8482 else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
8484 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
8485 typdefault_is_literal = true; /* it needs quotes */
8490 qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
8493 * DROP must be fully qualified in case same name appears in pg_catalog.
8494 * The reason we include CASCADE is that the circular dependency between
8495 * the type and its I/O functions makes it impossible to drop the type any
8498 appendPQExpBuffer(delq, "DROP TYPE %s.",
8499 fmtId(tyinfo->dobj.namespace->dobj.name));
8500 appendPQExpBuffer(delq, "%s CASCADE;\n",
8503 /* We might already have a shell type, but setting pg_type_oid is harmless */
8505 binary_upgrade_set_type_oids_by_type_oid(fout, q,
8506 tyinfo->dobj.catId.oid);
8508 appendPQExpBuffer(q,
8509 "CREATE TYPE %s (\n"
8510 " INTERNALLENGTH = %s",
8512 (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
8514 if (fout->remoteVersion >= 70300)
8516 /* regproc result is correctly quoted as of 7.3 */
8517 appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
8518 appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
8519 if (OidIsValid(typreceiveoid))
8520 appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
8521 if (OidIsValid(typsendoid))
8522 appendPQExpBuffer(q, ",\n SEND = %s", typsend);
8523 if (OidIsValid(typmodinoid))
8524 appendPQExpBuffer(q, ",\n TYPMOD_IN = %s", typmodin);
8525 if (OidIsValid(typmodoutoid))
8526 appendPQExpBuffer(q, ",\n TYPMOD_OUT = %s", typmodout);
8527 if (OidIsValid(typanalyzeoid))
8528 appendPQExpBuffer(q, ",\n ANALYZE = %s", typanalyze);
8532 /* regproc delivers an unquoted name before 7.3 */
8533 /* cannot combine these because fmtId uses static result area */
8534 appendPQExpBuffer(q, ",\n INPUT = %s", fmtId(typinput));
8535 appendPQExpBuffer(q, ",\n OUTPUT = %s", fmtId(typoutput));
8536 /* receive/send/typmodin/typmodout/analyze need not be printed */
8539 if (strcmp(typcollatable, "t") == 0)
8540 appendPQExpBuffer(q, ",\n COLLATABLE = true");
8542 if (typdefault != NULL)
8544 appendPQExpBuffer(q, ",\n DEFAULT = ");
8545 if (typdefault_is_literal)
8546 appendStringLiteralAH(q, typdefault, fout);
8548 appendPQExpBufferStr(q, typdefault);
8551 if (OidIsValid(tyinfo->typelem))
8555 /* reselect schema in case changed by function dump */
8556 selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
8557 elemType = getFormattedTypeName(fout, tyinfo->typelem, zeroAsOpaque);
8558 appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
8562 if (strcmp(typcategory, "U") != 0)
8564 appendPQExpBuffer(q, ",\n CATEGORY = ");
8565 appendStringLiteralAH(q, typcategory, fout);
8568 if (strcmp(typispreferred, "t") == 0)
8569 appendPQExpBuffer(q, ",\n PREFERRED = true");
8571 if (typdelim && strcmp(typdelim, ",") != 0)
8573 appendPQExpBuffer(q, ",\n DELIMITER = ");
8574 appendStringLiteralAH(q, typdelim, fout);
8577 if (strcmp(typalign, "c") == 0)
8578 appendPQExpBuffer(q, ",\n ALIGNMENT = char");
8579 else if (strcmp(typalign, "s") == 0)
8580 appendPQExpBuffer(q, ",\n ALIGNMENT = int2");
8581 else if (strcmp(typalign, "i") == 0)
8582 appendPQExpBuffer(q, ",\n ALIGNMENT = int4");
8583 else if (strcmp(typalign, "d") == 0)
8584 appendPQExpBuffer(q, ",\n ALIGNMENT = double");
8586 if (strcmp(typstorage, "p") == 0)
8587 appendPQExpBuffer(q, ",\n STORAGE = plain");
8588 else if (strcmp(typstorage, "e") == 0)
8589 appendPQExpBuffer(q, ",\n STORAGE = external");
8590 else if (strcmp(typstorage, "x") == 0)
8591 appendPQExpBuffer(q, ",\n STORAGE = extended");
8592 else if (strcmp(typstorage, "m") == 0)
8593 appendPQExpBuffer(q, ",\n STORAGE = main");
8595 if (strcmp(typbyval, "t") == 0)
8596 appendPQExpBuffer(q, ",\n PASSEDBYVALUE");
8598 appendPQExpBuffer(q, "\n);\n");
8600 appendPQExpBuffer(labelq, "TYPE %s", qtypname);
8603 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
8605 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
8607 tyinfo->dobj.namespace->dobj.name,
8609 tyinfo->rolname, false,
8610 "TYPE", SECTION_PRE_DATA,
8611 q->data, delq->data, NULL,
8615 /* Dump Type Comments and Security Labels */
8616 dumpComment(fout, labelq->data,
8617 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8618 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8619 dumpSecLabel(fout, labelq->data,
8620 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8621 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8623 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
8624 qtypname, NULL, tyinfo->dobj.name,
8625 tyinfo->dobj.namespace->dobj.name,
8626 tyinfo->rolname, tyinfo->typacl);
8629 destroyPQExpBuffer(q);
8630 destroyPQExpBuffer(delq);
8631 destroyPQExpBuffer(labelq);
8632 destroyPQExpBuffer(query);
8637 * writes out to fout the queries to recreate a user-defined domain
8640 dumpDomain(Archive *fout, TypeInfo *tyinfo)
8642 PQExpBuffer q = createPQExpBuffer();
8643 PQExpBuffer delq = createPQExpBuffer();
8644 PQExpBuffer labelq = createPQExpBuffer();
8645 PQExpBuffer query = createPQExpBuffer();
8653 bool typdefault_is_literal = false;
8655 /* Set proper schema search path so type references list correctly */
8656 selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
8658 /* Fetch domain specific details */
8659 if (fout->remoteVersion >= 90100)
8661 /* typcollation is new in 9.1 */
8662 appendPQExpBuffer(query, "SELECT t.typnotnull, "
8663 "pg_catalog.format_type(t.typbasetype, t.typtypmod) AS typdefn, "
8664 "pg_catalog.pg_get_expr(t.typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
8666 "CASE WHEN t.typcollation <> u.typcollation "
8667 "THEN t.typcollation ELSE 0 END AS typcollation "
8668 "FROM pg_catalog.pg_type t "
8669 "LEFT JOIN pg_catalog.pg_type u ON (t.typbasetype = u.oid) "
8670 "WHERE t.oid = '%u'::pg_catalog.oid",
8671 tyinfo->dobj.catId.oid);
8675 /* We assume here that remoteVersion must be at least 70300 */
8676 appendPQExpBuffer(query, "SELECT typnotnull, "
8677 "pg_catalog.format_type(typbasetype, typtypmod) AS typdefn, "
8678 "pg_catalog.pg_get_expr(typdefaultbin, 'pg_catalog.pg_type'::pg_catalog.regclass) AS typdefaultbin, "
8679 "typdefault, 0 AS typcollation "
8680 "FROM pg_catalog.pg_type "
8681 "WHERE oid = '%u'::pg_catalog.oid",
8682 tyinfo->dobj.catId.oid);
8685 res = ExecuteSqlQueryForSingleRow(fout, query->data);
8687 typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
8688 typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
8689 if (!PQgetisnull(res, 0, PQfnumber(res, "typdefaultbin")))
8690 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefaultbin"));
8691 else if (!PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
8693 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
8694 typdefault_is_literal = true; /* it needs quotes */
8698 typcollation = atooid(PQgetvalue(res, 0, PQfnumber(res, "typcollation")));
8701 binary_upgrade_set_type_oids_by_type_oid(fout, q,
8702 tyinfo->dobj.catId.oid);
8704 qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
8706 appendPQExpBuffer(q,
8707 "CREATE DOMAIN %s AS %s",
8711 /* Print collation only if different from base type's collation */
8712 if (OidIsValid(typcollation))
8716 coll = findCollationByOid(typcollation);
8719 /* always schema-qualify, don't try to be smart */
8720 appendPQExpBuffer(q, " COLLATE %s.",
8721 fmtId(coll->dobj.namespace->dobj.name));
8722 appendPQExpBuffer(q, "%s",
8723 fmtId(coll->dobj.name));
8727 if (typnotnull[0] == 't')
8728 appendPQExpBuffer(q, " NOT NULL");
8730 if (typdefault != NULL)
8732 appendPQExpBuffer(q, " DEFAULT ");
8733 if (typdefault_is_literal)
8734 appendStringLiteralAH(q, typdefault, fout);
8736 appendPQExpBufferStr(q, typdefault);
8742 * Add any CHECK constraints for the domain
8744 for (i = 0; i < tyinfo->nDomChecks; i++)
8746 ConstraintInfo *domcheck = &(tyinfo->domChecks[i]);
8748 if (!domcheck->separate)
8749 appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
8750 fmtId(domcheck->dobj.name), domcheck->condef);
8753 appendPQExpBuffer(q, ";\n");
8756 * DROP must be fully qualified in case same name appears in pg_catalog
8758 appendPQExpBuffer(delq, "DROP DOMAIN %s.",
8759 fmtId(tyinfo->dobj.namespace->dobj.name));
8760 appendPQExpBuffer(delq, "%s;\n",
8763 appendPQExpBuffer(labelq, "DOMAIN %s", qtypname);
8766 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
8768 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
8770 tyinfo->dobj.namespace->dobj.name,
8772 tyinfo->rolname, false,
8773 "DOMAIN", SECTION_PRE_DATA,
8774 q->data, delq->data, NULL,
8778 /* Dump Domain Comments and Security Labels */
8779 dumpComment(fout, labelq->data,
8780 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8781 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8782 dumpSecLabel(fout, labelq->data,
8783 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8784 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8786 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
8787 qtypname, NULL, tyinfo->dobj.name,
8788 tyinfo->dobj.namespace->dobj.name,
8789 tyinfo->rolname, tyinfo->typacl);
8791 destroyPQExpBuffer(q);
8792 destroyPQExpBuffer(delq);
8793 destroyPQExpBuffer(labelq);
8794 destroyPQExpBuffer(query);
8799 * writes out to fout the queries to recreate a user-defined stand-alone
8803 dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
8805 PQExpBuffer q = createPQExpBuffer();
8806 PQExpBuffer dropped = createPQExpBuffer();
8807 PQExpBuffer delq = createPQExpBuffer();
8808 PQExpBuffer labelq = createPQExpBuffer();
8809 PQExpBuffer query = createPQExpBuffer();
8823 /* Set proper schema search path so type references list correctly */
8824 selectSourceSchema(fout, tyinfo->dobj.namespace->dobj.name);
8826 /* Fetch type specific details */
8827 if (fout->remoteVersion >= 90100)
8830 * attcollation is new in 9.1. Since we only want to dump COLLATE
8831 * clauses for attributes whose collation is different from their
8832 * type's default, we use a CASE here to suppress uninteresting
8833 * attcollations cheaply. atttypid will be 0 for dropped columns;
8834 * collation does not matter for those.
8836 appendPQExpBuffer(query, "SELECT a.attname, "
8837 "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
8838 "a.attlen, a.attalign, a.attisdropped, "
8839 "CASE WHEN a.attcollation <> at.typcollation "
8840 "THEN a.attcollation ELSE 0 END AS attcollation, "
8842 "FROM pg_catalog.pg_type ct "
8843 "JOIN pg_catalog.pg_attribute a ON a.attrelid = ct.typrelid "
8844 "LEFT JOIN pg_catalog.pg_type at ON at.oid = a.atttypid "
8845 "WHERE ct.oid = '%u'::pg_catalog.oid "
8846 "ORDER BY a.attnum ",
8847 tyinfo->dobj.catId.oid);
8852 * We assume here that remoteVersion must be at least 70300. Since
8853 * ALTER TYPE could not drop columns until 9.1, attisdropped should
8856 appendPQExpBuffer(query, "SELECT a.attname, "
8857 "pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
8858 "a.attlen, a.attalign, a.attisdropped, "
8859 "0 AS attcollation, "
8861 "FROM pg_catalog.pg_type ct, pg_catalog.pg_attribute a "
8862 "WHERE ct.oid = '%u'::pg_catalog.oid "
8863 "AND a.attrelid = ct.typrelid "
8864 "ORDER BY a.attnum ",
8865 tyinfo->dobj.catId.oid);
8868 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
8870 ntups = PQntuples(res);
8872 i_attname = PQfnumber(res, "attname");
8873 i_atttypdefn = PQfnumber(res, "atttypdefn");
8874 i_attlen = PQfnumber(res, "attlen");
8875 i_attalign = PQfnumber(res, "attalign");
8876 i_attisdropped = PQfnumber(res, "attisdropped");
8877 i_attcollation = PQfnumber(res, "attcollation");
8878 i_typrelid = PQfnumber(res, "typrelid");
8882 Oid typrelid = atooid(PQgetvalue(res, 0, i_typrelid));
8884 binary_upgrade_set_type_oids_by_type_oid(fout, q,
8885 tyinfo->dobj.catId.oid);
8886 binary_upgrade_set_pg_class_oids(fout, q, typrelid, false);
8889 qtypname = pg_strdup(fmtId(tyinfo->dobj.name));
8891 appendPQExpBuffer(q, "CREATE TYPE %s AS (",
8895 for (i = 0; i < ntups; i++)
8904 attname = PQgetvalue(res, i, i_attname);
8905 atttypdefn = PQgetvalue(res, i, i_atttypdefn);
8906 attlen = PQgetvalue(res, i, i_attlen);
8907 attalign = PQgetvalue(res, i, i_attalign);
8908 attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
8909 attcollation = atooid(PQgetvalue(res, i, i_attcollation));
8911 if (attisdropped && !binary_upgrade)
8914 /* Format properly if not first attr */
8915 if (actual_atts++ > 0)
8916 appendPQExpBuffer(q, ",");
8917 appendPQExpBuffer(q, "\n\t");
8921 appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
8923 /* Add collation if not default for the column type */
8924 if (OidIsValid(attcollation))
8928 coll = findCollationByOid(attcollation);
8931 /* always schema-qualify, don't try to be smart */
8932 appendPQExpBuffer(q, " COLLATE %s.",
8933 fmtId(coll->dobj.namespace->dobj.name));
8934 appendPQExpBuffer(q, "%s",
8935 fmtId(coll->dobj.name));
8942 * This is a dropped attribute and we're in binary_upgrade mode.
8943 * Insert a placeholder for it in the CREATE TYPE command, and set
8944 * length and alignment with direct UPDATE to the catalogs
8945 * afterwards. See similar code in dumpTableSchema().
8947 appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
8949 /* stash separately for insertion after the CREATE TYPE */
8950 appendPQExpBuffer(dropped,
8951 "\n-- For binary upgrade, recreate dropped column.\n");
8952 appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
8954 "attalign = '%s', attbyval = false\n"
8955 "WHERE attname = ", attlen, attalign);
8956 appendStringLiteralAH(dropped, attname, fout);
8957 appendPQExpBuffer(dropped, "\n AND attrelid = ");
8958 appendStringLiteralAH(dropped, qtypname, fout);
8959 appendPQExpBuffer(dropped, "::pg_catalog.regclass;\n");
8961 appendPQExpBuffer(dropped, "ALTER TYPE %s ",
8963 appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
8967 appendPQExpBuffer(q, "\n);\n");
8968 appendPQExpBufferStr(q, dropped->data);
8971 * DROP must be fully qualified in case same name appears in pg_catalog
8973 appendPQExpBuffer(delq, "DROP TYPE %s.",
8974 fmtId(tyinfo->dobj.namespace->dobj.name));
8975 appendPQExpBuffer(delq, "%s;\n",
8978 appendPQExpBuffer(labelq, "TYPE %s", qtypname);
8981 binary_upgrade_extension_member(q, &tyinfo->dobj, labelq->data);
8983 ArchiveEntry(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId,
8985 tyinfo->dobj.namespace->dobj.name,
8987 tyinfo->rolname, false,
8988 "TYPE", SECTION_PRE_DATA,
8989 q->data, delq->data, NULL,
8994 /* Dump Type Comments and Security Labels */
8995 dumpComment(fout, labelq->data,
8996 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
8997 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
8998 dumpSecLabel(fout, labelq->data,
8999 tyinfo->dobj.namespace->dobj.name, tyinfo->rolname,
9000 tyinfo->dobj.catId, 0, tyinfo->dobj.dumpId);
9002 dumpACL(fout, tyinfo->dobj.catId, tyinfo->dobj.dumpId, "TYPE",
9003 qtypname, NULL, tyinfo->dobj.name,
9004 tyinfo->dobj.namespace->dobj.name,
9005 tyinfo->rolname, tyinfo->typacl);
9008 destroyPQExpBuffer(q);
9009 destroyPQExpBuffer(dropped);
9010 destroyPQExpBuffer(delq);
9011 destroyPQExpBuffer(labelq);
9012 destroyPQExpBuffer(query);
9014 /* Dump any per-column comments */
9015 dumpCompositeTypeColComments(fout, tyinfo);
9019 * dumpCompositeTypeColComments
9020 * writes out to fout the queries to recreate comments on the columns of
9021 * a user-defined stand-alone composite type
9024 dumpCompositeTypeColComments(Archive *fout, TypeInfo *tyinfo)
9026 CommentItem *comments;
9037 query = createPQExpBuffer();
9039 /* We assume here that remoteVersion must be at least 70300 */
9040 appendPQExpBuffer(query,
9041 "SELECT c.tableoid, a.attname, a.attnum "
9042 "FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a "
9043 "WHERE c.oid = '%u' AND c.oid = a.attrelid "
9044 " AND NOT a.attisdropped "
9045 "ORDER BY a.attnum ",
9048 /* Fetch column attnames */
9049 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
9051 ntups = PQntuples(res);
9055 destroyPQExpBuffer(query);
9059 pgClassOid = atooid(PQgetvalue(res, 0, PQfnumber(res, "tableoid")));
9061 /* Search for comments associated with type's pg_class OID */
9062 ncomments = findComments(fout,
9067 /* If no comments exist, we're done */
9071 destroyPQExpBuffer(query);
9075 /* Build COMMENT ON statements */
9076 target = createPQExpBuffer();
9078 i_attnum = PQfnumber(res, "attnum");
9079 i_attname = PQfnumber(res, "attname");
9080 while (ncomments > 0)
9082 const char *attname;
9085 for (i = 0; i < ntups; i++)
9087 if (atoi(PQgetvalue(res, i, i_attnum)) == comments->objsubid)
9089 attname = PQgetvalue(res, i, i_attname);
9093 if (attname) /* just in case we don't find it */
9095 const char *descr = comments->descr;
9097 resetPQExpBuffer(target);
9098 appendPQExpBuffer(target, "COLUMN %s.",
9099 fmtId(tyinfo->dobj.name));
9100 appendPQExpBuffer(target, "%s",
9103 resetPQExpBuffer(query);
9104 appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
9105 appendStringLiteralAH(query, descr, fout);
9106 appendPQExpBuffer(query, ";\n");
9108 ArchiveEntry(fout, nilCatalogId, createDumpId(),
9110 tyinfo->dobj.namespace->dobj.name,
9111 NULL, tyinfo->rolname,
9112 false, "COMMENT", SECTION_NONE,
9113 query->data, "", NULL,
9114 &(tyinfo->dobj.dumpId), 1,
9123 destroyPQExpBuffer(query);
9124 destroyPQExpBuffer(target);
9129 * writes out to fout the queries to create a shell type
9131 * We dump a shell definition in advance of the I/O functions for the type.
9134 dumpShellType(Archive *fout, ShellTypeInfo *stinfo)
9138 /* Skip if not to be dumped */
9139 if (!stinfo->dobj.dump || dataOnly)
9142 q = createPQExpBuffer();
9145 * Note the lack of a DROP command for the shell type; any required DROP
9146 * is driven off the base type entry, instead. This interacts with
9147 * _printTocEntry()'s use of the presence of a DROP command to decide
9148 * whether an entry needs an ALTER OWNER command. We don't want to alter
9149 * the shell type's owner immediately on creation; that should happen only
9150 * after it's filled in, otherwise the backend complains.
9154 binary_upgrade_set_type_oids_by_type_oid(fout, q,
9155 stinfo->baseType->dobj.catId.oid);
9157 appendPQExpBuffer(q, "CREATE TYPE %s;\n",
9158 fmtId(stinfo->dobj.name));
9160 ArchiveEntry(fout, stinfo->dobj.catId, stinfo->dobj.dumpId,
9162 stinfo->dobj.namespace->dobj.name,
9164 stinfo->baseType->rolname, false,
9165 "SHELL TYPE", SECTION_PRE_DATA,
9170 destroyPQExpBuffer(q);
9174 * Determine whether we want to dump definitions for procedural languages.
9175 * Since the languages themselves don't have schemas, we can't rely on
9176 * the normal schema-based selection mechanism. We choose to dump them
9177 * whenever neither --schema nor --table was given. (Before 8.1, we used
9178 * the dump flag of the PL's call handler function, but in 8.1 this will
9179 * probably always be false since call handlers are created in pg_catalog.)
9181 * For some backwards compatibility with the older behavior, we forcibly
9182 * dump a PL if its handler function (and validator if any) are in a
9183 * dumpable namespace. That case is not checked here.
9185 * Also, if the PL belongs to an extension, we do not use this heuristic.
9186 * That case isn't checked here either.
9189 shouldDumpProcLangs(void)
9191 if (!include_everything)
9193 /* And they're schema not data */
9201 * writes out to fout the queries to recreate a user-defined
9202 * procedural language
9205 dumpProcLang(Archive *fout, ProcLangInfo *plang)
9214 FuncInfo *inlineInfo = NULL;
9215 FuncInfo *validatorInfo = NULL;
9217 /* Skip if not to be dumped */
9218 if (!plang->dobj.dump || dataOnly)
9222 * Try to find the support function(s). It is not an error if we don't
9223 * find them --- if the functions are in the pg_catalog schema, as is
9224 * standard in 8.1 and up, then we won't have loaded them. (In this case
9225 * we will emit a parameterless CREATE LANGUAGE command, which will
9226 * require PL template knowledge in the backend to reload.)
9229 funcInfo = findFuncByOid(plang->lanplcallfoid);
9230 if (funcInfo != NULL && !funcInfo->dobj.dump)
9231 funcInfo = NULL; /* treat not-dumped same as not-found */
9233 if (OidIsValid(plang->laninline))
9235 inlineInfo = findFuncByOid(plang->laninline);
9236 if (inlineInfo != NULL && !inlineInfo->dobj.dump)
9240 if (OidIsValid(plang->lanvalidator))
9242 validatorInfo = findFuncByOid(plang->lanvalidator);
9243 if (validatorInfo != NULL && !validatorInfo->dobj.dump)
9244 validatorInfo = NULL;
9248 * If the functions are dumpable then emit a traditional CREATE LANGUAGE
9249 * with parameters. Otherwise, dump only if shouldDumpProcLangs() says to
9252 * However, for a language that belongs to an extension, we must not use
9253 * the shouldDumpProcLangs heuristic, but just dump the language iff we're
9254 * told to (via dobj.dump). Generally the support functions will belong
9255 * to the same extension and so have the same dump flags ... if they
9256 * don't, this might not work terribly nicely.
9258 useParams = (funcInfo != NULL &&
9259 (inlineInfo != NULL || !OidIsValid(plang->laninline)) &&
9260 (validatorInfo != NULL || !OidIsValid(plang->lanvalidator)));
9262 if (!plang->dobj.ext_member)
9264 if (!useParams && !shouldDumpProcLangs())
9268 defqry = createPQExpBuffer();
9269 delqry = createPQExpBuffer();
9270 labelq = createPQExpBuffer();
9272 qlanname = pg_strdup(fmtId(plang->dobj.name));
9275 * If dumping a HANDLER clause, treat the language as being in the handler
9276 * function's schema; this avoids cluttering the HANDLER clause. Otherwise
9277 * it doesn't really have a schema.
9280 lanschema = funcInfo->dobj.namespace->dobj.name;
9284 appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
9289 appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
9290 plang->lanpltrusted ? "TRUSTED " : "",
9292 appendPQExpBuffer(defqry, " HANDLER %s",
9293 fmtId(funcInfo->dobj.name));
9294 if (OidIsValid(plang->laninline))
9296 appendPQExpBuffer(defqry, " INLINE ");
9297 /* Cope with possibility that inline is in different schema */
9298 if (inlineInfo->dobj.namespace != funcInfo->dobj.namespace)
9299 appendPQExpBuffer(defqry, "%s.",
9300 fmtId(inlineInfo->dobj.namespace->dobj.name));
9301 appendPQExpBuffer(defqry, "%s",
9302 fmtId(inlineInfo->dobj.name));
9304 if (OidIsValid(plang->lanvalidator))
9306 appendPQExpBuffer(defqry, " VALIDATOR ");
9307 /* Cope with possibility that validator is in different schema */
9308 if (validatorInfo->dobj.namespace != funcInfo->dobj.namespace)
9309 appendPQExpBuffer(defqry, "%s.",
9310 fmtId(validatorInfo->dobj.namespace->dobj.name));
9311 appendPQExpBuffer(defqry, "%s",
9312 fmtId(validatorInfo->dobj.name));
9318 * If not dumping parameters, then use CREATE OR REPLACE so that the
9319 * command will not fail if the language is preinstalled in the target
9320 * database. We restrict the use of REPLACE to this case so as to
9321 * eliminate the risk of replacing a language with incompatible
9322 * parameter settings: this command will only succeed at all if there
9323 * is a pg_pltemplate entry, and if there is one, the existing entry
9324 * must match it too.
9326 appendPQExpBuffer(defqry, "CREATE OR REPLACE PROCEDURAL LANGUAGE %s",
9329 appendPQExpBuffer(defqry, ";\n");
9331 appendPQExpBuffer(labelq, "LANGUAGE %s", qlanname);
9334 binary_upgrade_extension_member(defqry, &plang->dobj, labelq->data);
9336 ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
9338 lanschema, NULL, plang->lanowner,
9339 false, "PROCEDURAL LANGUAGE", SECTION_PRE_DATA,
9340 defqry->data, delqry->data, NULL,
9344 /* Dump Proc Lang Comments and Security Labels */
9345 dumpComment(fout, labelq->data,
9347 plang->dobj.catId, 0, plang->dobj.dumpId);
9348 dumpSecLabel(fout, labelq->data,
9350 plang->dobj.catId, 0, plang->dobj.dumpId);
9352 if (plang->lanpltrusted)
9353 dumpACL(fout, plang->dobj.catId, plang->dobj.dumpId, "LANGUAGE",
9354 qlanname, NULL, plang->dobj.name,
9356 plang->lanowner, plang->lanacl);
9360 destroyPQExpBuffer(defqry);
9361 destroyPQExpBuffer(delqry);
9362 destroyPQExpBuffer(labelq);
9366 * format_function_arguments: generate function name and argument list
9368 * This is used when we can rely on pg_get_function_arguments to format
9369 * the argument list. Note, however, that pg_get_function_arguments
9370 * does not special-case zero-argument aggregates.
9373 format_function_arguments(FuncInfo *finfo, char *funcargs, bool is_agg)
9377 initPQExpBuffer(&fn);
9378 appendPQExpBuffer(&fn, "%s", fmtId(finfo->dobj.name));
9379 if (is_agg && finfo->nargs == 0)
9380 appendPQExpBuffer(&fn, "(*)");
9382 appendPQExpBuffer(&fn, "(%s)", funcargs);
9387 * format_function_arguments_old: generate function name and argument list
9389 * The argument type names are qualified if needed. The function name
9390 * is never qualified.
9392 * This is used only with pre-8.4 servers, so we aren't expecting to see
9393 * VARIADIC or TABLE arguments, nor are there any defaults for arguments.
9395 * Any or all of allargtypes, argmodes, argnames may be NULL.
9398 format_function_arguments_old(Archive *fout,
9399 FuncInfo *finfo, int nallargs,
9407 initPQExpBuffer(&fn);
9408 appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
9409 for (j = 0; j < nallargs; j++)
9413 const char *argmode;
9414 const char *argname;
9416 typid = allargtypes ? atooid(allargtypes[j]) : finfo->argtypes[j];
9417 typname = getFormattedTypeName(fout, typid, zeroAsOpaque);
9421 switch (argmodes[j][0])
9426 case PROARGMODE_OUT:
9429 case PROARGMODE_INOUT:
9433 write_msg(NULL, "WARNING: bogus value in proargmodes array\n");
9441 argname = argnames ? argnames[j] : (char *) NULL;
9442 if (argname && argname[0] == '\0')
9445 appendPQExpBuffer(&fn, "%s%s%s%s%s",
9446 (j > 0) ? ", " : "",
9448 argname ? fmtId(argname) : "",
9453 appendPQExpBuffer(&fn, ")");
9458 * format_function_signature: generate function name and argument list
9460 * This is like format_function_arguments_old except that only a minimal
9461 * list of input argument types is generated; this is sufficient to
9462 * reference the function, but not to define it.
9464 * If honor_quotes is false then the function name is never quoted.
9465 * This is appropriate for use in TOC tags, but not in SQL commands.
9468 format_function_signature(Archive *fout, FuncInfo *finfo, bool honor_quotes)
9473 initPQExpBuffer(&fn);
9475 appendPQExpBuffer(&fn, "%s(", fmtId(finfo->dobj.name));
9477 appendPQExpBuffer(&fn, "%s(", finfo->dobj.name);
9478 for (j = 0; j < finfo->nargs; j++)
9482 typname = getFormattedTypeName(fout, finfo->argtypes[j],
9485 appendPQExpBuffer(&fn, "%s%s",
9486 (j > 0) ? ", " : "",
9490 appendPQExpBuffer(&fn, ")");
9497 * dump out one function
9500 dumpFunc(Archive *fout, FuncInfo *finfo)
9508 char *funcsig; /* identity signature */
9509 char *funcfullsig; /* full signature */
9517 char *proallargtypes;
9531 char **allargtypes = NULL;
9532 char **argmodes = NULL;
9533 char **argnames = NULL;
9534 char **configitems = NULL;
9535 int nconfigitems = 0;
9538 /* Skip if not to be dumped */
9539 if (!finfo->dobj.dump || dataOnly)
9542 query = createPQExpBuffer();
9543 q = createPQExpBuffer();
9544 delqry = createPQExpBuffer();
9545 labelq = createPQExpBuffer();
9546 asPart = createPQExpBuffer();
9548 /* Set proper schema search path so type references list correctly */
9549 selectSourceSchema(fout, finfo->dobj.namespace->dobj.name);
9551 /* Fetch function-specific details */
9552 if (fout->remoteVersion >= 90200)
9555 * proleakproof was added at v9.2
9557 appendPQExpBuffer(query,
9558 "SELECT proretset, prosrc, probin, "
9559 "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
9560 "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
9561 "pg_catalog.pg_get_function_result(oid) AS funcresult, "
9562 "proiswindow, provolatile, proisstrict, prosecdef, "
9563 "proleakproof, proconfig, procost, prorows, "
9564 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
9565 "FROM pg_catalog.pg_proc "
9566 "WHERE oid = '%u'::pg_catalog.oid",
9567 finfo->dobj.catId.oid);
9569 else if (fout->remoteVersion >= 80400)
9572 * In 8.4 and up we rely on pg_get_function_arguments and
9573 * pg_get_function_result instead of examining proallargtypes etc.
9575 appendPQExpBuffer(query,
9576 "SELECT proretset, prosrc, probin, "
9577 "pg_catalog.pg_get_function_arguments(oid) AS funcargs, "
9578 "pg_catalog.pg_get_function_identity_arguments(oid) AS funciargs, "
9579 "pg_catalog.pg_get_function_result(oid) AS funcresult, "
9580 "proiswindow, provolatile, proisstrict, prosecdef, "
9581 "false AS proleakproof, "
9582 " proconfig, procost, prorows, "
9583 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
9584 "FROM pg_catalog.pg_proc "
9585 "WHERE oid = '%u'::pg_catalog.oid",
9586 finfo->dobj.catId.oid);
9588 else if (fout->remoteVersion >= 80300)
9590 appendPQExpBuffer(query,
9591 "SELECT proretset, prosrc, probin, "
9592 "proallargtypes, proargmodes, proargnames, "
9593 "false AS proiswindow, "
9594 "provolatile, proisstrict, prosecdef, "
9595 "false AS proleakproof, "
9596 "proconfig, procost, prorows, "
9597 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
9598 "FROM pg_catalog.pg_proc "
9599 "WHERE oid = '%u'::pg_catalog.oid",
9600 finfo->dobj.catId.oid);
9602 else if (fout->remoteVersion >= 80100)
9604 appendPQExpBuffer(query,
9605 "SELECT proretset, prosrc, probin, "
9606 "proallargtypes, proargmodes, proargnames, "
9607 "false AS proiswindow, "
9608 "provolatile, proisstrict, prosecdef, "
9609 "false AS proleakproof, "
9610 "null AS proconfig, 0 AS procost, 0 AS prorows, "
9611 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
9612 "FROM pg_catalog.pg_proc "
9613 "WHERE oid = '%u'::pg_catalog.oid",
9614 finfo->dobj.catId.oid);
9616 else if (fout->remoteVersion >= 80000)
9618 appendPQExpBuffer(query,
9619 "SELECT proretset, prosrc, probin, "
9620 "null AS proallargtypes, "
9621 "null AS proargmodes, "
9623 "false AS proiswindow, "
9624 "provolatile, proisstrict, prosecdef, "
9625 "false AS proleakproof, "
9626 "null AS proconfig, 0 AS procost, 0 AS prorows, "
9627 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
9628 "FROM pg_catalog.pg_proc "
9629 "WHERE oid = '%u'::pg_catalog.oid",
9630 finfo->dobj.catId.oid);
9632 else if (fout->remoteVersion >= 70300)
9634 appendPQExpBuffer(query,
9635 "SELECT proretset, prosrc, probin, "
9636 "null AS proallargtypes, "
9637 "null AS proargmodes, "
9638 "null AS proargnames, "
9639 "false AS proiswindow, "
9640 "provolatile, proisstrict, prosecdef, "
9641 "false AS proleakproof, "
9642 "null AS proconfig, 0 AS procost, 0 AS prorows, "
9643 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) AS lanname "
9644 "FROM pg_catalog.pg_proc "
9645 "WHERE oid = '%u'::pg_catalog.oid",
9646 finfo->dobj.catId.oid);
9648 else if (fout->remoteVersion >= 70100)
9650 appendPQExpBuffer(query,
9651 "SELECT proretset, prosrc, probin, "
9652 "null AS proallargtypes, "
9653 "null AS proargmodes, "
9654 "null AS proargnames, "
9655 "false AS proiswindow, "
9656 "case when proiscachable then 'i' else 'v' end AS provolatile, "
9658 "false AS prosecdef, "
9659 "false AS proleakproof, "
9660 "null AS proconfig, 0 AS procost, 0 AS prorows, "
9661 "(SELECT lanname FROM pg_language WHERE oid = prolang) AS lanname "
9663 "WHERE oid = '%u'::oid",
9664 finfo->dobj.catId.oid);
9668 appendPQExpBuffer(query,
9669 "SELECT proretset, prosrc, probin, "
9670 "null AS proallargtypes, "
9671 "null AS proargmodes, "
9672 "null AS proargnames, "
9673 "false AS proiswindow, "
9674 "CASE WHEN proiscachable THEN 'i' ELSE 'v' END AS provolatile, "
9675 "false AS proisstrict, "
9676 "false AS prosecdef, "
9677 "false AS proleakproof, "
9678 "NULL AS proconfig, 0 AS procost, 0 AS prorows, "
9679 "(SELECT lanname FROM pg_language WHERE oid = prolang) AS lanname "
9681 "WHERE oid = '%u'::oid",
9682 finfo->dobj.catId.oid);
9685 res = ExecuteSqlQueryForSingleRow(fout, query->data);
9687 proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
9688 prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
9689 probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
9690 if (fout->remoteVersion >= 80400)
9692 funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
9693 funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
9694 funcresult = PQgetvalue(res, 0, PQfnumber(res, "funcresult"));
9695 proallargtypes = proargmodes = proargnames = NULL;
9699 proallargtypes = PQgetvalue(res, 0, PQfnumber(res, "proallargtypes"));
9700 proargmodes = PQgetvalue(res, 0, PQfnumber(res, "proargmodes"));
9701 proargnames = PQgetvalue(res, 0, PQfnumber(res, "proargnames"));
9702 funcargs = funciargs = funcresult = NULL;
9704 proiswindow = PQgetvalue(res, 0, PQfnumber(res, "proiswindow"));
9705 provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
9706 proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
9707 prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
9708 proleakproof = PQgetvalue(res, 0, PQfnumber(res, "proleakproof"));
9709 proconfig = PQgetvalue(res, 0, PQfnumber(res, "proconfig"));
9710 procost = PQgetvalue(res, 0, PQfnumber(res, "procost"));
9711 prorows = PQgetvalue(res, 0, PQfnumber(res, "prorows"));
9712 lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
9715 * See backend/commands/functioncmds.c for details of how the 'AS' clause
9716 * is used. In 8.4 and up, an unused probin is NULL (here ""); previous
9717 * versions would set it to "-". There are no known cases in which prosrc
9718 * is unused, so the tests below for "-" are probably useless.
9720 if (probin[0] != '\0' && strcmp(probin, "-") != 0)
9722 appendPQExpBuffer(asPart, "AS ");
9723 appendStringLiteralAH(asPart, probin, fout);
9724 if (strcmp(prosrc, "-") != 0)
9726 appendPQExpBuffer(asPart, ", ");
9729 * where we have bin, use dollar quoting if allowed and src
9730 * contains quote or backslash; else use regular quoting.
9732 if (disable_dollar_quoting ||
9733 (strchr(prosrc, '\'') == NULL && strchr(prosrc, '\\') == NULL))
9734 appendStringLiteralAH(asPart, prosrc, fout);
9736 appendStringLiteralDQ(asPart, prosrc, NULL);
9741 if (strcmp(prosrc, "-") != 0)
9743 appendPQExpBuffer(asPart, "AS ");
9744 /* with no bin, dollar quote src unconditionally if allowed */
9745 if (disable_dollar_quoting)
9746 appendStringLiteralAH(asPart, prosrc, fout);
9748 appendStringLiteralDQ(asPart, prosrc, NULL);
9752 nallargs = finfo->nargs; /* unless we learn different from allargs */
9754 if (proallargtypes && *proallargtypes)
9758 if (!parsePGArray(proallargtypes, &allargtypes, &nitems) ||
9759 nitems < finfo->nargs)
9761 write_msg(NULL, "WARNING: could not parse proallargtypes array\n");
9770 if (proargmodes && *proargmodes)
9774 if (!parsePGArray(proargmodes, &argmodes, &nitems) ||
9777 write_msg(NULL, "WARNING: could not parse proargmodes array\n");
9784 if (proargnames && *proargnames)
9788 if (!parsePGArray(proargnames, &argnames, &nitems) ||
9791 write_msg(NULL, "WARNING: could not parse proargnames array\n");
9798 if (proconfig && *proconfig)
9800 if (!parsePGArray(proconfig, &configitems, &nconfigitems))
9802 write_msg(NULL, "WARNING: could not parse proconfig array\n");
9812 /* 8.4 or later; we rely on server-side code for most of the work */
9813 funcfullsig = format_function_arguments(finfo, funcargs, false);
9814 funcsig = format_function_arguments(finfo, funciargs, false);
9818 /* pre-8.4, do it ourselves */
9819 funcsig = format_function_arguments_old(fout,
9820 finfo, nallargs, allargtypes,
9821 argmodes, argnames);
9822 funcfullsig = funcsig;
9825 funcsig_tag = format_function_signature(fout, finfo, false);
9828 * DROP must be fully qualified in case same name appears in pg_catalog
9830 appendPQExpBuffer(delqry, "DROP FUNCTION %s.%s;\n",
9831 fmtId(finfo->dobj.namespace->dobj.name),
9834 appendPQExpBuffer(q, "CREATE FUNCTION %s ", funcfullsig);
9836 appendPQExpBuffer(q, "RETURNS %s", funcresult);
9839 rettypename = getFormattedTypeName(fout, finfo->prorettype,
9841 appendPQExpBuffer(q, "RETURNS %s%s",
9842 (proretset[0] == 't') ? "SETOF " : "",
9847 appendPQExpBuffer(q, "\n LANGUAGE %s", fmtId(lanname));
9849 if (proiswindow[0] == 't')
9850 appendPQExpBuffer(q, " WINDOW");
9852 if (provolatile[0] != PROVOLATILE_VOLATILE)
9854 if (provolatile[0] == PROVOLATILE_IMMUTABLE)
9855 appendPQExpBuffer(q, " IMMUTABLE");
9856 else if (provolatile[0] == PROVOLATILE_STABLE)
9857 appendPQExpBuffer(q, " STABLE");
9858 else if (provolatile[0] != PROVOLATILE_VOLATILE)
9859 exit_horribly(NULL, "unrecognized provolatile value for function \"%s\"\n",
9863 if (proisstrict[0] == 't')
9864 appendPQExpBuffer(q, " STRICT");
9866 if (prosecdef[0] == 't')
9867 appendPQExpBuffer(q, " SECURITY DEFINER");
9869 if (proleakproof[0] == 't')
9870 appendPQExpBuffer(q, " LEAKPROOF");
9873 * COST and ROWS are emitted only if present and not default, so as not to
9874 * break backwards-compatibility of the dump without need. Keep this code
9875 * in sync with the defaults in functioncmds.c.
9877 if (strcmp(procost, "0") != 0)
9879 if (strcmp(lanname, "internal") == 0 || strcmp(lanname, "c") == 0)
9881 /* default cost is 1 */
9882 if (strcmp(procost, "1") != 0)
9883 appendPQExpBuffer(q, " COST %s", procost);
9887 /* default cost is 100 */
9888 if (strcmp(procost, "100") != 0)
9889 appendPQExpBuffer(q, " COST %s", procost);
9892 if (proretset[0] == 't' &&
9893 strcmp(prorows, "0") != 0 && strcmp(prorows, "1000") != 0)
9894 appendPQExpBuffer(q, " ROWS %s", prorows);
9896 for (i = 0; i < nconfigitems; i++)
9898 /* we feel free to scribble on configitems[] here */
9899 char *configitem = configitems[i];
9902 pos = strchr(configitem, '=');
9906 appendPQExpBuffer(q, "\n SET %s TO ", fmtId(configitem));
9909 * Some GUC variable names are 'LIST' type and hence must not be
9912 if (pg_strcasecmp(configitem, "DateStyle") == 0
9913 || pg_strcasecmp(configitem, "search_path") == 0)
9914 appendPQExpBuffer(q, "%s", pos);
9916 appendStringLiteralAH(q, pos, fout);
9919 appendPQExpBuffer(q, "\n %s;\n", asPart->data);
9921 appendPQExpBuffer(labelq, "FUNCTION %s", funcsig);
9924 binary_upgrade_extension_member(q, &finfo->dobj, labelq->data);
9926 ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
9928 finfo->dobj.namespace->dobj.name,
9930 finfo->rolname, false,
9931 "FUNCTION", SECTION_PRE_DATA,
9932 q->data, delqry->data, NULL,
9936 /* Dump Function Comments and Security Labels */
9937 dumpComment(fout, labelq->data,
9938 finfo->dobj.namespace->dobj.name, finfo->rolname,
9939 finfo->dobj.catId, 0, finfo->dobj.dumpId);
9940 dumpSecLabel(fout, labelq->data,
9941 finfo->dobj.namespace->dobj.name, finfo->rolname,
9942 finfo->dobj.catId, 0, finfo->dobj.dumpId);
9944 dumpACL(fout, finfo->dobj.catId, finfo->dobj.dumpId, "FUNCTION",
9945 funcsig, NULL, funcsig_tag,
9946 finfo->dobj.namespace->dobj.name,
9947 finfo->rolname, finfo->proacl);
9951 destroyPQExpBuffer(query);
9952 destroyPQExpBuffer(q);
9953 destroyPQExpBuffer(delqry);
9954 destroyPQExpBuffer(labelq);
9955 destroyPQExpBuffer(asPart);
9970 * Dump a user-defined cast
9973 dumpCast(Archive *fout, CastInfo *cast)
9978 FuncInfo *funcInfo = NULL;
9980 /* Skip if not to be dumped */
9981 if (!cast->dobj.dump || dataOnly)
9984 /* Cannot dump if we don't have the cast function's info */
9985 if (OidIsValid(cast->castfunc))
9987 funcInfo = findFuncByOid(cast->castfunc);
9988 if (funcInfo == NULL)
9993 * As per discussion we dump casts if one or more of the underlying
9994 * objects (the conversion function and the two data types) are not
9995 * builtin AND if all of the non-builtin objects are included in the dump.
9996 * Builtin meaning, the namespace name does not start with "pg_".
9998 * However, for a cast that belongs to an extension, we must not use this
9999 * heuristic, but just dump the cast iff we're told to (via dobj.dump).
10001 if (!cast->dobj.ext_member)
10003 TypeInfo *sourceInfo = findTypeByOid(cast->castsource);
10004 TypeInfo *targetInfo = findTypeByOid(cast->casttarget);
10006 if (sourceInfo == NULL || targetInfo == NULL)
10010 * Skip this cast if all objects are from pg_
10012 if ((funcInfo == NULL ||
10013 strncmp(funcInfo->dobj.namespace->dobj.name, "pg_", 3) == 0) &&
10014 strncmp(sourceInfo->dobj.namespace->dobj.name, "pg_", 3) == 0 &&
10015 strncmp(targetInfo->dobj.namespace->dobj.name, "pg_", 3) == 0)
10019 * Skip cast if function isn't from pg_ and is not to be dumped.
10022 strncmp(funcInfo->dobj.namespace->dobj.name, "pg_", 3) != 0 &&
10023 !funcInfo->dobj.dump)
10027 * Same for the source type
10029 if (strncmp(sourceInfo->dobj.namespace->dobj.name, "pg_", 3) != 0 &&
10030 !sourceInfo->dobj.dump)
10034 * and the target type.
10036 if (strncmp(targetInfo->dobj.namespace->dobj.name, "pg_", 3) != 0 &&
10037 !targetInfo->dobj.dump)
10041 /* Make sure we are in proper schema (needed for getFormattedTypeName) */
10042 selectSourceSchema(fout, "pg_catalog");
10044 defqry = createPQExpBuffer();
10045 delqry = createPQExpBuffer();
10046 labelq = createPQExpBuffer();
10048 appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
10049 getFormattedTypeName(fout, cast->castsource, zeroAsNone),
10050 getFormattedTypeName(fout, cast->casttarget, zeroAsNone));
10052 appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
10053 getFormattedTypeName(fout, cast->castsource, zeroAsNone),
10054 getFormattedTypeName(fout, cast->casttarget, zeroAsNone));
10056 switch (cast->castmethod)
10058 case COERCION_METHOD_BINARY:
10059 appendPQExpBuffer(defqry, "WITHOUT FUNCTION");
10061 case COERCION_METHOD_INOUT:
10062 appendPQExpBuffer(defqry, "WITH INOUT");
10064 case COERCION_METHOD_FUNCTION:
10067 char *fsig = format_function_signature(fout, funcInfo, true);
10070 * Always qualify the function name, in case it is not in
10071 * pg_catalog schema (format_function_signature won't qualify
10074 appendPQExpBuffer(defqry, "WITH FUNCTION %s.%s",
10075 fmtId(funcInfo->dobj.namespace->dobj.name), fsig);
10079 write_msg(NULL, "WARNING: bogus value in pg_cast.castfunc or pg_cast.castmethod field\n");
10082 write_msg(NULL, "WARNING: bogus value in pg_cast.castmethod field\n");
10085 if (cast->castcontext == 'a')
10086 appendPQExpBuffer(defqry, " AS ASSIGNMENT");
10087 else if (cast->castcontext == 'i')
10088 appendPQExpBuffer(defqry, " AS IMPLICIT");
10089 appendPQExpBuffer(defqry, ";\n");
10091 appendPQExpBuffer(labelq, "CAST (%s AS %s)",
10092 getFormattedTypeName(fout, cast->castsource, zeroAsNone),
10093 getFormattedTypeName(fout, cast->casttarget, zeroAsNone));
10095 if (binary_upgrade)
10096 binary_upgrade_extension_member(defqry, &cast->dobj, labelq->data);
10098 ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
10100 "pg_catalog", NULL, "",
10101 false, "CAST", SECTION_PRE_DATA,
10102 defqry->data, delqry->data, NULL,
10106 /* Dump Cast Comments */
10107 dumpComment(fout, labelq->data,
10109 cast->dobj.catId, 0, cast->dobj.dumpId);
10111 destroyPQExpBuffer(defqry);
10112 destroyPQExpBuffer(delqry);
10113 destroyPQExpBuffer(labelq);
10118 * write out a single operator definition
10121 dumpOpr(Archive *fout, OprInfo *oprinfo)
10126 PQExpBuffer labelq;
10128 PQExpBuffer details;
10152 /* Skip if not to be dumped */
10153 if (!oprinfo->dobj.dump || dataOnly)
10157 * some operators are invalid because they were the result of user
10158 * defining operators before commutators exist
10160 if (!OidIsValid(oprinfo->oprcode))
10163 query = createPQExpBuffer();
10164 q = createPQExpBuffer();
10165 delq = createPQExpBuffer();
10166 labelq = createPQExpBuffer();
10167 oprid = createPQExpBuffer();
10168 details = createPQExpBuffer();
10170 /* Make sure we are in proper schema so regoperator works correctly */
10171 selectSourceSchema(fout, oprinfo->dobj.namespace->dobj.name);
10173 if (fout->remoteVersion >= 80300)
10175 appendPQExpBuffer(query, "SELECT oprkind, "
10176 "oprcode::pg_catalog.regprocedure, "
10177 "oprleft::pg_catalog.regtype, "
10178 "oprright::pg_catalog.regtype, "
10179 "oprcom::pg_catalog.regoperator, "
10180 "oprnegate::pg_catalog.regoperator, "
10181 "oprrest::pg_catalog.regprocedure, "
10182 "oprjoin::pg_catalog.regprocedure, "
10183 "oprcanmerge, oprcanhash "
10184 "FROM pg_catalog.pg_operator "
10185 "WHERE oid = '%u'::pg_catalog.oid",
10186 oprinfo->dobj.catId.oid);
10188 else if (fout->remoteVersion >= 70300)
10190 appendPQExpBuffer(query, "SELECT oprkind, "
10191 "oprcode::pg_catalog.regprocedure, "
10192 "oprleft::pg_catalog.regtype, "
10193 "oprright::pg_catalog.regtype, "
10194 "oprcom::pg_catalog.regoperator, "
10195 "oprnegate::pg_catalog.regoperator, "
10196 "oprrest::pg_catalog.regprocedure, "
10197 "oprjoin::pg_catalog.regprocedure, "
10198 "(oprlsortop != 0) AS oprcanmerge, "
10200 "FROM pg_catalog.pg_operator "
10201 "WHERE oid = '%u'::pg_catalog.oid",
10202 oprinfo->dobj.catId.oid);
10204 else if (fout->remoteVersion >= 70100)
10206 appendPQExpBuffer(query, "SELECT oprkind, oprcode, "
10207 "CASE WHEN oprleft = 0 THEN '-' "
10208 "ELSE format_type(oprleft, NULL) END AS oprleft, "
10209 "CASE WHEN oprright = 0 THEN '-' "
10210 "ELSE format_type(oprright, NULL) END AS oprright, "
10211 "oprcom, oprnegate, oprrest, oprjoin, "
10212 "(oprlsortop != 0) AS oprcanmerge, "
10214 "FROM pg_operator "
10215 "WHERE oid = '%u'::oid",
10216 oprinfo->dobj.catId.oid);
10220 appendPQExpBuffer(query, "SELECT oprkind, oprcode, "
10221 "CASE WHEN oprleft = 0 THEN '-'::name "
10222 "ELSE (SELECT typname FROM pg_type WHERE oid = oprleft) END AS oprleft, "
10223 "CASE WHEN oprright = 0 THEN '-'::name "
10224 "ELSE (SELECT typname FROM pg_type WHERE oid = oprright) END AS oprright, "
10225 "oprcom, oprnegate, oprrest, oprjoin, "
10226 "(oprlsortop != 0) AS oprcanmerge, "
10228 "FROM pg_operator "
10229 "WHERE oid = '%u'::oid",
10230 oprinfo->dobj.catId.oid);
10233 res = ExecuteSqlQueryForSingleRow(fout, query->data);
10235 i_oprkind = PQfnumber(res, "oprkind");
10236 i_oprcode = PQfnumber(res, "oprcode");
10237 i_oprleft = PQfnumber(res, "oprleft");
10238 i_oprright = PQfnumber(res, "oprright");
10239 i_oprcom = PQfnumber(res, "oprcom");
10240 i_oprnegate = PQfnumber(res, "oprnegate");
10241 i_oprrest = PQfnumber(res, "oprrest");
10242 i_oprjoin = PQfnumber(res, "oprjoin");
10243 i_oprcanmerge = PQfnumber(res, "oprcanmerge");
10244 i_oprcanhash = PQfnumber(res, "oprcanhash");
10246 oprkind = PQgetvalue(res, 0, i_oprkind);
10247 oprcode = PQgetvalue(res, 0, i_oprcode);
10248 oprleft = PQgetvalue(res, 0, i_oprleft);
10249 oprright = PQgetvalue(res, 0, i_oprright);
10250 oprcom = PQgetvalue(res, 0, i_oprcom);
10251 oprnegate = PQgetvalue(res, 0, i_oprnegate);
10252 oprrest = PQgetvalue(res, 0, i_oprrest);
10253 oprjoin = PQgetvalue(res, 0, i_oprjoin);
10254 oprcanmerge = PQgetvalue(res, 0, i_oprcanmerge);
10255 oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
10257 appendPQExpBuffer(details, " PROCEDURE = %s",
10258 convertRegProcReference(fout, oprcode));
10260 appendPQExpBuffer(oprid, "%s (",
10261 oprinfo->dobj.name);
10264 * right unary means there's a left arg and left unary means there's a
10267 if (strcmp(oprkind, "r") == 0 ||
10268 strcmp(oprkind, "b") == 0)
10270 if (fout->remoteVersion >= 70100)
10273 name = fmtId(oprleft);
10274 appendPQExpBuffer(details, ",\n LEFTARG = %s", name);
10275 appendPQExpBuffer(oprid, "%s", name);
10278 appendPQExpBuffer(oprid, "NONE");
10280 if (strcmp(oprkind, "l") == 0 ||
10281 strcmp(oprkind, "b") == 0)
10283 if (fout->remoteVersion >= 70100)
10286 name = fmtId(oprright);
10287 appendPQExpBuffer(details, ",\n RIGHTARG = %s", name);
10288 appendPQExpBuffer(oprid, ", %s)", name);
10291 appendPQExpBuffer(oprid, ", NONE)");
10293 name = convertOperatorReference(fout, oprcom);
10295 appendPQExpBuffer(details, ",\n COMMUTATOR = %s", name);
10297 name = convertOperatorReference(fout, oprnegate);
10299 appendPQExpBuffer(details, ",\n NEGATOR = %s", name);
10301 if (strcmp(oprcanmerge, "t") == 0)
10302 appendPQExpBuffer(details, ",\n MERGES");
10304 if (strcmp(oprcanhash, "t") == 0)
10305 appendPQExpBuffer(details, ",\n HASHES");
10307 name = convertRegProcReference(fout, oprrest);
10309 appendPQExpBuffer(details, ",\n RESTRICT = %s", name);
10311 name = convertRegProcReference(fout, oprjoin);
10313 appendPQExpBuffer(details, ",\n JOIN = %s", name);
10316 * DROP must be fully qualified in case same name appears in pg_catalog
10318 appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
10319 fmtId(oprinfo->dobj.namespace->dobj.name),
10322 appendPQExpBuffer(q, "CREATE OPERATOR %s (\n%s\n);\n",
10323 oprinfo->dobj.name, details->data);
10325 appendPQExpBuffer(labelq, "OPERATOR %s", oprid->data);
10327 if (binary_upgrade)
10328 binary_upgrade_extension_member(q, &oprinfo->dobj, labelq->data);
10330 ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
10331 oprinfo->dobj.name,
10332 oprinfo->dobj.namespace->dobj.name,
10335 false, "OPERATOR", SECTION_PRE_DATA,
10336 q->data, delq->data, NULL,
10340 /* Dump Operator Comments */
10341 dumpComment(fout, labelq->data,
10342 oprinfo->dobj.namespace->dobj.name, oprinfo->rolname,
10343 oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
10347 destroyPQExpBuffer(query);
10348 destroyPQExpBuffer(q);
10349 destroyPQExpBuffer(delq);
10350 destroyPQExpBuffer(labelq);
10351 destroyPQExpBuffer(oprid);
10352 destroyPQExpBuffer(details);
10356 * Convert a function reference obtained from pg_operator
10358 * Returns what to print, or NULL if function references is InvalidOid
10360 * In 7.3 the input is a REGPROCEDURE display; we have to strip the
10361 * argument-types part. In prior versions, the input is a REGPROC display.
10363 static const char *
10364 convertRegProcReference(Archive *fout, const char *proc)
10366 /* In all cases "-" means a null reference */
10367 if (strcmp(proc, "-") == 0)
10370 if (fout->remoteVersion >= 70300)
10376 name = pg_strdup(proc);
10377 /* find non-double-quoted left paren */
10379 for (paren = name; *paren; paren++)
10381 if (*paren == '(' && !inquote)
10387 inquote = !inquote;
10392 /* REGPROC before 7.3 does not quote its result */
10393 return fmtId(proc);
10397 * Convert an operator cross-reference obtained from pg_operator
10399 * Returns what to print, or NULL to print nothing
10401 * In 7.3 and up the input is a REGOPERATOR display; we have to strip the
10402 * argument-types part, and add OPERATOR() decoration if the name is
10403 * schema-qualified. In older versions, the input is just a numeric OID,
10404 * which we search our operator list for.
10406 static const char *
10407 convertOperatorReference(Archive *fout, const char *opr)
10411 /* In all cases "0" means a null reference */
10412 if (strcmp(opr, "0") == 0)
10415 if (fout->remoteVersion >= 70300)
10423 name = pg_strdup(opr);
10424 /* find non-double-quoted left paren, and check for non-quoted dot */
10427 for (ptr = name; *ptr; ptr++)
10430 inquote = !inquote;
10431 else if (*ptr == '.' && !inquote)
10433 else if (*ptr == '(' && !inquote)
10439 /* If not schema-qualified, don't need to add OPERATOR() */
10442 pg_asprintf(&oname, "OPERATOR(%s)", name);
10447 oprInfo = findOprByOid(atooid(opr));
10448 if (oprInfo == NULL)
10450 write_msg(NULL, "WARNING: could not find operator with OID %s\n",
10454 return oprInfo->dobj.name;
10458 * Convert a function OID obtained from pg_ts_parser or pg_ts_template
10460 * It is sufficient to use REGPROC rather than REGPROCEDURE, since the
10461 * argument lists of these functions are predetermined. Note that the
10462 * caller should ensure we are in the proper schema, because the results
10463 * are search path dependent!
10465 static const char *
10466 convertTSFunction(Archive *fout, Oid funcOid)
10472 snprintf(query, sizeof(query),
10473 "SELECT '%u'::pg_catalog.regproc", funcOid);
10474 res = ExecuteSqlQueryForSingleRow(fout, query);
10476 result = pg_strdup(PQgetvalue(res, 0, 0));
10486 * write out a single operator class definition
10489 dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
10494 PQExpBuffer labelq;
10501 int i_opcfamilyname;
10502 int i_opcfamilynsp;
10504 int i_amopstrategy;
10505 int i_amopreqcheck;
10508 int i_sortfamilynsp;
10511 int i_amproclefttype;
10512 int i_amprocrighttype;
10517 char *opcfamilyname;
10518 char *opcfamilynsp;
10520 char *amopstrategy;
10521 char *amopreqcheck;
10524 char *sortfamilynsp;
10527 char *amproclefttype;
10528 char *amprocrighttype;
10532 /* Skip if not to be dumped */
10533 if (!opcinfo->dobj.dump || dataOnly)
10537 * XXX currently we do not implement dumping of operator classes from
10538 * pre-7.3 databases. This could be done but it seems not worth the
10541 if (fout->remoteVersion < 70300)
10544 query = createPQExpBuffer();
10545 q = createPQExpBuffer();
10546 delq = createPQExpBuffer();
10547 labelq = createPQExpBuffer();
10549 /* Make sure we are in proper schema so regoperator works correctly */
10550 selectSourceSchema(fout, opcinfo->dobj.namespace->dobj.name);
10552 /* Get additional fields from the pg_opclass row */
10553 if (fout->remoteVersion >= 80300)
10555 appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
10556 "opckeytype::pg_catalog.regtype, "
10557 "opcdefault, opcfamily, "
10558 "opfname AS opcfamilyname, "
10559 "nspname AS opcfamilynsp, "
10560 "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
10561 "FROM pg_catalog.pg_opclass c "
10562 "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
10563 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
10564 "WHERE c.oid = '%u'::pg_catalog.oid",
10565 opcinfo->dobj.catId.oid);
10569 appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
10570 "opckeytype::pg_catalog.regtype, "
10571 "opcdefault, NULL AS opcfamily, "
10572 "NULL AS opcfamilyname, "
10573 "NULL AS opcfamilynsp, "
10574 "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcamid) AS amname "
10575 "FROM pg_catalog.pg_opclass "
10576 "WHERE oid = '%u'::pg_catalog.oid",
10577 opcinfo->dobj.catId.oid);
10580 res = ExecuteSqlQueryForSingleRow(fout, query->data);
10582 i_opcintype = PQfnumber(res, "opcintype");
10583 i_opckeytype = PQfnumber(res, "opckeytype");
10584 i_opcdefault = PQfnumber(res, "opcdefault");
10585 i_opcfamily = PQfnumber(res, "opcfamily");
10586 i_opcfamilyname = PQfnumber(res, "opcfamilyname");
10587 i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
10588 i_amname = PQfnumber(res, "amname");
10590 opcintype = PQgetvalue(res, 0, i_opcintype);
10591 opckeytype = PQgetvalue(res, 0, i_opckeytype);
10592 opcdefault = PQgetvalue(res, 0, i_opcdefault);
10593 /* opcfamily will still be needed after we PQclear res */
10594 opcfamily = pg_strdup(PQgetvalue(res, 0, i_opcfamily));
10595 opcfamilyname = PQgetvalue(res, 0, i_opcfamilyname);
10596 opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
10597 /* amname will still be needed after we PQclear res */
10598 amname = pg_strdup(PQgetvalue(res, 0, i_amname));
10601 * DROP must be fully qualified in case same name appears in pg_catalog
10603 appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
10604 fmtId(opcinfo->dobj.namespace->dobj.name));
10605 appendPQExpBuffer(delq, ".%s",
10606 fmtId(opcinfo->dobj.name));
10607 appendPQExpBuffer(delq, " USING %s;\n",
10610 /* Build the fixed portion of the CREATE command */
10611 appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ",
10612 fmtId(opcinfo->dobj.name));
10613 if (strcmp(opcdefault, "t") == 0)
10614 appendPQExpBuffer(q, "DEFAULT ");
10615 appendPQExpBuffer(q, "FOR TYPE %s USING %s",
10618 if (strlen(opcfamilyname) > 0 &&
10619 (strcmp(opcfamilyname, opcinfo->dobj.name) != 0 ||
10620 strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0))
10622 appendPQExpBuffer(q, " FAMILY ");
10623 if (strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0)
10624 appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
10625 appendPQExpBuffer(q, "%s", fmtId(opcfamilyname));
10627 appendPQExpBuffer(q, " AS\n ");
10631 if (strcmp(opckeytype, "-") != 0)
10633 appendPQExpBuffer(q, "STORAGE %s",
10641 * Now fetch and print the OPERATOR entries (pg_amop rows).
10643 * Print only those opfamily members that are tied to the opclass by
10644 * pg_depend entries.
10646 * XXX RECHECK is gone as of 8.4, but we'll still print it if dumping an
10647 * older server's opclass in which it is used. This is to avoid
10648 * hard-to-detect breakage if a newer pg_dump is used to dump from an
10649 * older server and then reload into that old version. This can go away
10650 * once 8.3 is so old as to not be of interest to anyone.
10652 resetPQExpBuffer(query);
10654 if (fout->remoteVersion >= 90100)
10656 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
10657 "amopopr::pg_catalog.regoperator, "
10658 "opfname AS sortfamily, "
10659 "nspname AS sortfamilynsp "
10660 "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
10661 "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
10662 "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
10663 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
10664 "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
10665 "AND refobjid = '%u'::pg_catalog.oid "
10666 "AND amopfamily = '%s'::pg_catalog.oid "
10667 "ORDER BY amopstrategy",
10668 opcinfo->dobj.catId.oid,
10671 else if (fout->remoteVersion >= 80400)
10673 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
10674 "amopopr::pg_catalog.regoperator, "
10675 "NULL AS sortfamily, "
10676 "NULL AS sortfamilynsp "
10677 "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
10678 "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
10679 "AND refobjid = '%u'::pg_catalog.oid "
10680 "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
10681 "AND objid = ao.oid "
10682 "ORDER BY amopstrategy",
10683 opcinfo->dobj.catId.oid);
10685 else if (fout->remoteVersion >= 80300)
10687 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
10688 "amopopr::pg_catalog.regoperator, "
10689 "NULL AS sortfamily, "
10690 "NULL AS sortfamilynsp "
10691 "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
10692 "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
10693 "AND refobjid = '%u'::pg_catalog.oid "
10694 "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
10695 "AND objid = ao.oid "
10696 "ORDER BY amopstrategy",
10697 opcinfo->dobj.catId.oid);
10702 * Here, we print all entries since there are no opfamilies and hence
10703 * no loose operators to worry about.
10705 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
10706 "amopopr::pg_catalog.regoperator, "
10707 "NULL AS sortfamily, "
10708 "NULL AS sortfamilynsp "
10709 "FROM pg_catalog.pg_amop "
10710 "WHERE amopclaid = '%u'::pg_catalog.oid "
10711 "ORDER BY amopstrategy",
10712 opcinfo->dobj.catId.oid);
10715 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10717 ntups = PQntuples(res);
10719 i_amopstrategy = PQfnumber(res, "amopstrategy");
10720 i_amopreqcheck = PQfnumber(res, "amopreqcheck");
10721 i_amopopr = PQfnumber(res, "amopopr");
10722 i_sortfamily = PQfnumber(res, "sortfamily");
10723 i_sortfamilynsp = PQfnumber(res, "sortfamilynsp");
10725 for (i = 0; i < ntups; i++)
10727 amopstrategy = PQgetvalue(res, i, i_amopstrategy);
10728 amopreqcheck = PQgetvalue(res, i, i_amopreqcheck);
10729 amopopr = PQgetvalue(res, i, i_amopopr);
10730 sortfamily = PQgetvalue(res, i, i_sortfamily);
10731 sortfamilynsp = PQgetvalue(res, i, i_sortfamilynsp);
10734 appendPQExpBuffer(q, " ,\n ");
10736 appendPQExpBuffer(q, "OPERATOR %s %s",
10737 amopstrategy, amopopr);
10739 if (strlen(sortfamily) > 0)
10741 appendPQExpBuffer(q, " FOR ORDER BY ");
10742 if (strcmp(sortfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0)
10743 appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
10744 appendPQExpBuffer(q, "%s", fmtId(sortfamily));
10747 if (strcmp(amopreqcheck, "t") == 0)
10748 appendPQExpBuffer(q, " RECHECK");
10756 * Now fetch and print the FUNCTION entries (pg_amproc rows).
10758 * Print only those opfamily members that are tied to the opclass by
10759 * pg_depend entries.
10761 * We print the amproclefttype/amprocrighttype even though in most cases
10762 * the backend could deduce the right values, because of the corner case
10763 * of a btree sort support function for a cross-type comparison. That's
10764 * only allowed in 9.2 and later, but for simplicity print them in all
10765 * versions that have the columns.
10767 resetPQExpBuffer(query);
10769 if (fout->remoteVersion >= 80300)
10771 appendPQExpBuffer(query, "SELECT amprocnum, "
10772 "amproc::pg_catalog.regprocedure, "
10773 "amproclefttype::pg_catalog.regtype, "
10774 "amprocrighttype::pg_catalog.regtype "
10775 "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
10776 "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
10777 "AND refobjid = '%u'::pg_catalog.oid "
10778 "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
10779 "AND objid = ap.oid "
10780 "ORDER BY amprocnum",
10781 opcinfo->dobj.catId.oid);
10785 appendPQExpBuffer(query, "SELECT amprocnum, "
10786 "amproc::pg_catalog.regprocedure, "
10787 "'' AS amproclefttype, "
10788 "'' AS amprocrighttype "
10789 "FROM pg_catalog.pg_amproc "
10790 "WHERE amopclaid = '%u'::pg_catalog.oid "
10791 "ORDER BY amprocnum",
10792 opcinfo->dobj.catId.oid);
10795 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10797 ntups = PQntuples(res);
10799 i_amprocnum = PQfnumber(res, "amprocnum");
10800 i_amproc = PQfnumber(res, "amproc");
10801 i_amproclefttype = PQfnumber(res, "amproclefttype");
10802 i_amprocrighttype = PQfnumber(res, "amprocrighttype");
10804 for (i = 0; i < ntups; i++)
10806 amprocnum = PQgetvalue(res, i, i_amprocnum);
10807 amproc = PQgetvalue(res, i, i_amproc);
10808 amproclefttype = PQgetvalue(res, i, i_amproclefttype);
10809 amprocrighttype = PQgetvalue(res, i, i_amprocrighttype);
10812 appendPQExpBuffer(q, " ,\n ");
10814 appendPQExpBuffer(q, "FUNCTION %s", amprocnum);
10816 if (*amproclefttype && *amprocrighttype)
10817 appendPQExpBuffer(q, " (%s, %s)", amproclefttype, amprocrighttype);
10819 appendPQExpBuffer(q, " %s", amproc);
10826 appendPQExpBuffer(q, ";\n");
10828 appendPQExpBuffer(labelq, "OPERATOR CLASS %s",
10829 fmtId(opcinfo->dobj.name));
10830 appendPQExpBuffer(labelq, " USING %s",
10833 if (binary_upgrade)
10834 binary_upgrade_extension_member(q, &opcinfo->dobj, labelq->data);
10836 ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
10837 opcinfo->dobj.name,
10838 opcinfo->dobj.namespace->dobj.name,
10841 false, "OPERATOR CLASS", SECTION_PRE_DATA,
10842 q->data, delq->data, NULL,
10846 /* Dump Operator Class Comments */
10847 dumpComment(fout, labelq->data,
10848 NULL, opcinfo->rolname,
10849 opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
10852 destroyPQExpBuffer(query);
10853 destroyPQExpBuffer(q);
10854 destroyPQExpBuffer(delq);
10855 destroyPQExpBuffer(labelq);
10860 * write out a single operator family definition
10862 * Note: this also dumps any "loose" operator members that aren't bound to a
10863 * specific opclass within the opfamily.
10866 dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
10871 PQExpBuffer labelq;
10874 PGresult *res_procs;
10877 int i_amopstrategy;
10878 int i_amopreqcheck;
10881 int i_sortfamilynsp;
10884 int i_amproclefttype;
10885 int i_amprocrighttype;
10887 char *amopstrategy;
10888 char *amopreqcheck;
10891 char *sortfamilynsp;
10894 char *amproclefttype;
10895 char *amprocrighttype;
10899 /* Skip if not to be dumped */
10900 if (!opfinfo->dobj.dump || dataOnly)
10904 * We want to dump the opfamily only if (1) it contains "loose" operators
10905 * or functions, or (2) it contains an opclass with a different name or
10906 * owner. Otherwise it's sufficient to let it be created during creation
10907 * of the contained opclass, and not dumping it improves portability of
10908 * the dump. Since we have to fetch the loose operators/funcs anyway, do
10912 query = createPQExpBuffer();
10913 q = createPQExpBuffer();
10914 delq = createPQExpBuffer();
10915 labelq = createPQExpBuffer();
10917 /* Make sure we are in proper schema so regoperator works correctly */
10918 selectSourceSchema(fout, opfinfo->dobj.namespace->dobj.name);
10921 * Fetch only those opfamily members that are tied directly to the
10922 * opfamily by pg_depend entries.
10924 * XXX RECHECK is gone as of 8.4, but we'll still print it if dumping an
10925 * older server's opclass in which it is used. This is to avoid
10926 * hard-to-detect breakage if a newer pg_dump is used to dump from an
10927 * older server and then reload into that old version. This can go away
10928 * once 8.3 is so old as to not be of interest to anyone.
10930 if (fout->remoteVersion >= 90100)
10932 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
10933 "amopopr::pg_catalog.regoperator, "
10934 "opfname AS sortfamily, "
10935 "nspname AS sortfamilynsp "
10936 "FROM pg_catalog.pg_amop ao JOIN pg_catalog.pg_depend ON "
10937 "(classid = 'pg_catalog.pg_amop'::pg_catalog.regclass AND objid = ao.oid) "
10938 "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = amopsortfamily "
10939 "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
10940 "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
10941 "AND refobjid = '%u'::pg_catalog.oid "
10942 "AND amopfamily = '%u'::pg_catalog.oid "
10943 "ORDER BY amopstrategy",
10944 opfinfo->dobj.catId.oid,
10945 opfinfo->dobj.catId.oid);
10947 else if (fout->remoteVersion >= 80400)
10949 appendPQExpBuffer(query, "SELECT amopstrategy, false AS amopreqcheck, "
10950 "amopopr::pg_catalog.regoperator, "
10951 "NULL AS sortfamily, "
10952 "NULL AS sortfamilynsp "
10953 "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
10954 "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
10955 "AND refobjid = '%u'::pg_catalog.oid "
10956 "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
10957 "AND objid = ao.oid "
10958 "ORDER BY amopstrategy",
10959 opfinfo->dobj.catId.oid);
10963 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
10964 "amopopr::pg_catalog.regoperator, "
10965 "NULL AS sortfamily, "
10966 "NULL AS sortfamilynsp "
10967 "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
10968 "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
10969 "AND refobjid = '%u'::pg_catalog.oid "
10970 "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
10971 "AND objid = ao.oid "
10972 "ORDER BY amopstrategy",
10973 opfinfo->dobj.catId.oid);
10976 res_ops = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10978 resetPQExpBuffer(query);
10980 appendPQExpBuffer(query, "SELECT amprocnum, "
10981 "amproc::pg_catalog.regprocedure, "
10982 "amproclefttype::pg_catalog.regtype, "
10983 "amprocrighttype::pg_catalog.regtype "
10984 "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
10985 "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
10986 "AND refobjid = '%u'::pg_catalog.oid "
10987 "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
10988 "AND objid = ap.oid "
10989 "ORDER BY amprocnum",
10990 opfinfo->dobj.catId.oid);
10992 res_procs = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
10994 if (PQntuples(res_ops) == 0 && PQntuples(res_procs) == 0)
10996 /* No loose members, so check contained opclasses */
10997 resetPQExpBuffer(query);
10999 appendPQExpBuffer(query, "SELECT 1 "
11000 "FROM pg_catalog.pg_opclass c, pg_catalog.pg_opfamily f, pg_catalog.pg_depend "
11001 "WHERE f.oid = '%u'::pg_catalog.oid "
11002 "AND refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
11003 "AND refobjid = f.oid "
11004 "AND classid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
11005 "AND objid = c.oid "
11006 "AND (opcname != opfname OR opcnamespace != opfnamespace OR opcowner != opfowner) "
11008 opfinfo->dobj.catId.oid);
11010 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11012 if (PQntuples(res) == 0)
11014 /* no need to dump it, so bail out */
11017 PQclear(res_procs);
11018 destroyPQExpBuffer(query);
11019 destroyPQExpBuffer(q);
11020 destroyPQExpBuffer(delq);
11021 destroyPQExpBuffer(labelq);
11028 /* Get additional fields from the pg_opfamily row */
11029 resetPQExpBuffer(query);
11031 appendPQExpBuffer(query, "SELECT "
11032 "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
11033 "FROM pg_catalog.pg_opfamily "
11034 "WHERE oid = '%u'::pg_catalog.oid",
11035 opfinfo->dobj.catId.oid);
11037 res = ExecuteSqlQueryForSingleRow(fout, query->data);
11039 i_amname = PQfnumber(res, "amname");
11041 /* amname will still be needed after we PQclear res */
11042 amname = pg_strdup(PQgetvalue(res, 0, i_amname));
11045 * DROP must be fully qualified in case same name appears in pg_catalog
11047 appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
11048 fmtId(opfinfo->dobj.namespace->dobj.name));
11049 appendPQExpBuffer(delq, ".%s",
11050 fmtId(opfinfo->dobj.name));
11051 appendPQExpBuffer(delq, " USING %s;\n",
11054 /* Build the fixed portion of the CREATE command */
11055 appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
11056 fmtId(opfinfo->dobj.name));
11057 appendPQExpBuffer(q, " USING %s;\n",
11062 /* Do we need an ALTER to add loose members? */
11063 if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
11065 appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
11066 fmtId(opfinfo->dobj.name));
11067 appendPQExpBuffer(q, " USING %s ADD\n ",
11073 * Now fetch and print the OPERATOR entries (pg_amop rows).
11075 ntups = PQntuples(res_ops);
11077 i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
11078 i_amopreqcheck = PQfnumber(res_ops, "amopreqcheck");
11079 i_amopopr = PQfnumber(res_ops, "amopopr");
11080 i_sortfamily = PQfnumber(res_ops, "sortfamily");
11081 i_sortfamilynsp = PQfnumber(res_ops, "sortfamilynsp");
11083 for (i = 0; i < ntups; i++)
11085 amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
11086 amopreqcheck = PQgetvalue(res_ops, i, i_amopreqcheck);
11087 amopopr = PQgetvalue(res_ops, i, i_amopopr);
11088 sortfamily = PQgetvalue(res_ops, i, i_sortfamily);
11089 sortfamilynsp = PQgetvalue(res_ops, i, i_sortfamilynsp);
11092 appendPQExpBuffer(q, " ,\n ");
11094 appendPQExpBuffer(q, "OPERATOR %s %s",
11095 amopstrategy, amopopr);
11097 if (strlen(sortfamily) > 0)
11099 appendPQExpBuffer(q, " FOR ORDER BY ");
11100 if (strcmp(sortfamilynsp, opfinfo->dobj.namespace->dobj.name) != 0)
11101 appendPQExpBuffer(q, "%s.", fmtId(sortfamilynsp));
11102 appendPQExpBuffer(q, "%s", fmtId(sortfamily));
11105 if (strcmp(amopreqcheck, "t") == 0)
11106 appendPQExpBuffer(q, " RECHECK");
11112 * Now fetch and print the FUNCTION entries (pg_amproc rows).
11114 ntups = PQntuples(res_procs);
11116 i_amprocnum = PQfnumber(res_procs, "amprocnum");
11117 i_amproc = PQfnumber(res_procs, "amproc");
11118 i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
11119 i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
11121 for (i = 0; i < ntups; i++)
11123 amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
11124 amproc = PQgetvalue(res_procs, i, i_amproc);
11125 amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
11126 amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
11129 appendPQExpBuffer(q, " ,\n ");
11131 appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
11132 amprocnum, amproclefttype, amprocrighttype,
11138 appendPQExpBuffer(q, ";\n");
11141 appendPQExpBuffer(labelq, "OPERATOR FAMILY %s",
11142 fmtId(opfinfo->dobj.name));
11143 appendPQExpBuffer(labelq, " USING %s",
11146 if (binary_upgrade)
11147 binary_upgrade_extension_member(q, &opfinfo->dobj, labelq->data);
11149 ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
11150 opfinfo->dobj.name,
11151 opfinfo->dobj.namespace->dobj.name,
11154 false, "OPERATOR FAMILY", SECTION_PRE_DATA,
11155 q->data, delq->data, NULL,
11159 /* Dump Operator Family Comments */
11160 dumpComment(fout, labelq->data,
11161 NULL, opfinfo->rolname,
11162 opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
11166 PQclear(res_procs);
11167 destroyPQExpBuffer(query);
11168 destroyPQExpBuffer(q);
11169 destroyPQExpBuffer(delq);
11170 destroyPQExpBuffer(labelq);
11175 * write out a single collation definition
11178 dumpCollation(Archive *fout, CollInfo *collinfo)
11183 PQExpBuffer labelq;
11187 const char *collcollate;
11188 const char *collctype;
11190 /* Skip if not to be dumped */
11191 if (!collinfo->dobj.dump || dataOnly)
11194 query = createPQExpBuffer();
11195 q = createPQExpBuffer();
11196 delq = createPQExpBuffer();
11197 labelq = createPQExpBuffer();
11199 /* Make sure we are in proper schema */
11200 selectSourceSchema(fout, collinfo->dobj.namespace->dobj.name);
11202 /* Get conversion-specific details */
11203 appendPQExpBuffer(query, "SELECT "
11206 "FROM pg_catalog.pg_collation c "
11207 "WHERE c.oid = '%u'::pg_catalog.oid",
11208 collinfo->dobj.catId.oid);
11210 res = ExecuteSqlQueryForSingleRow(fout, query->data);
11212 i_collcollate = PQfnumber(res, "collcollate");
11213 i_collctype = PQfnumber(res, "collctype");
11215 collcollate = PQgetvalue(res, 0, i_collcollate);
11216 collctype = PQgetvalue(res, 0, i_collctype);
11219 * DROP must be fully qualified in case same name appears in pg_catalog
11221 appendPQExpBuffer(delq, "DROP COLLATION %s",
11222 fmtId(collinfo->dobj.namespace->dobj.name));
11223 appendPQExpBuffer(delq, ".%s;\n",
11224 fmtId(collinfo->dobj.name));
11226 appendPQExpBuffer(q, "CREATE COLLATION %s (lc_collate = ",
11227 fmtId(collinfo->dobj.name));
11228 appendStringLiteralAH(q, collcollate, fout);
11229 appendPQExpBuffer(q, ", lc_ctype = ");
11230 appendStringLiteralAH(q, collctype, fout);
11231 appendPQExpBuffer(q, ");\n");
11233 appendPQExpBuffer(labelq, "COLLATION %s", fmtId(collinfo->dobj.name));
11235 if (binary_upgrade)
11236 binary_upgrade_extension_member(q, &collinfo->dobj, labelq->data);
11238 ArchiveEntry(fout, collinfo->dobj.catId, collinfo->dobj.dumpId,
11239 collinfo->dobj.name,
11240 collinfo->dobj.namespace->dobj.name,
11243 false, "COLLATION", SECTION_PRE_DATA,
11244 q->data, delq->data, NULL,
11248 /* Dump Collation Comments */
11249 dumpComment(fout, labelq->data,
11250 collinfo->dobj.namespace->dobj.name, collinfo->rolname,
11251 collinfo->dobj.catId, 0, collinfo->dobj.dumpId);
11255 destroyPQExpBuffer(query);
11256 destroyPQExpBuffer(q);
11257 destroyPQExpBuffer(delq);
11258 destroyPQExpBuffer(labelq);
11263 * write out a single conversion definition
11266 dumpConversion(Archive *fout, ConvInfo *convinfo)
11271 PQExpBuffer labelq;
11273 int i_conforencoding;
11274 int i_contoencoding;
11277 const char *conforencoding;
11278 const char *contoencoding;
11279 const char *conproc;
11282 /* Skip if not to be dumped */
11283 if (!convinfo->dobj.dump || dataOnly)
11286 query = createPQExpBuffer();
11287 q = createPQExpBuffer();
11288 delq = createPQExpBuffer();
11289 labelq = createPQExpBuffer();
11291 /* Make sure we are in proper schema */
11292 selectSourceSchema(fout, convinfo->dobj.namespace->dobj.name);
11294 /* Get conversion-specific details */
11295 appendPQExpBuffer(query, "SELECT "
11296 "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
11297 "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
11298 "conproc, condefault "
11299 "FROM pg_catalog.pg_conversion c "
11300 "WHERE c.oid = '%u'::pg_catalog.oid",
11301 convinfo->dobj.catId.oid);
11303 res = ExecuteSqlQueryForSingleRow(fout, query->data);
11305 i_conforencoding = PQfnumber(res, "conforencoding");
11306 i_contoencoding = PQfnumber(res, "contoencoding");
11307 i_conproc = PQfnumber(res, "conproc");
11308 i_condefault = PQfnumber(res, "condefault");
11310 conforencoding = PQgetvalue(res, 0, i_conforencoding);
11311 contoencoding = PQgetvalue(res, 0, i_contoencoding);
11312 conproc = PQgetvalue(res, 0, i_conproc);
11313 condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
11316 * DROP must be fully qualified in case same name appears in pg_catalog
11318 appendPQExpBuffer(delq, "DROP CONVERSION %s",
11319 fmtId(convinfo->dobj.namespace->dobj.name));
11320 appendPQExpBuffer(delq, ".%s;\n",
11321 fmtId(convinfo->dobj.name));
11323 appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
11324 (condefault) ? "DEFAULT " : "",
11325 fmtId(convinfo->dobj.name));
11326 appendStringLiteralAH(q, conforencoding, fout);
11327 appendPQExpBuffer(q, " TO ");
11328 appendStringLiteralAH(q, contoencoding, fout);
11329 /* regproc is automatically quoted in 7.3 and above */
11330 appendPQExpBuffer(q, " FROM %s;\n", conproc);
11332 appendPQExpBuffer(labelq, "CONVERSION %s", fmtId(convinfo->dobj.name));
11334 if (binary_upgrade)
11335 binary_upgrade_extension_member(q, &convinfo->dobj, labelq->data);
11337 ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
11338 convinfo->dobj.name,
11339 convinfo->dobj.namespace->dobj.name,
11342 false, "CONVERSION", SECTION_PRE_DATA,
11343 q->data, delq->data, NULL,
11347 /* Dump Conversion Comments */
11348 dumpComment(fout, labelq->data,
11349 convinfo->dobj.namespace->dobj.name, convinfo->rolname,
11350 convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
11354 destroyPQExpBuffer(query);
11355 destroyPQExpBuffer(q);
11356 destroyPQExpBuffer(delq);
11357 destroyPQExpBuffer(labelq);
11361 * format_aggregate_signature: generate aggregate name and argument list
11363 * The argument type names are qualified if needed. The aggregate name
11364 * is never qualified.
11367 format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
11369 PQExpBufferData buf;
11372 initPQExpBuffer(&buf);
11374 appendPQExpBuffer(&buf, "%s",
11375 fmtId(agginfo->aggfn.dobj.name));
11377 appendPQExpBuffer(&buf, "%s", agginfo->aggfn.dobj.name);
11379 if (agginfo->aggfn.nargs == 0)
11380 appendPQExpBuffer(&buf, "(*)");
11383 appendPQExpBuffer(&buf, "(");
11384 for (j = 0; j < agginfo->aggfn.nargs; j++)
11388 typname = getFormattedTypeName(fout, agginfo->aggfn.argtypes[j],
11391 appendPQExpBuffer(&buf, "%s%s",
11392 (j > 0) ? ", " : "",
11396 appendPQExpBuffer(&buf, ")");
11403 * write out a single aggregate definition
11406 dumpAgg(Archive *fout, AggInfo *agginfo)
11411 PQExpBuffer labelq;
11412 PQExpBuffer details;
11413 char *aggsig; /* identity signature */
11414 char *aggfullsig; /* full signature */
11420 int i_aggtranstype;
11423 const char *aggtransfn;
11424 const char *aggfinalfn;
11425 const char *aggsortop;
11426 const char *aggtranstype;
11427 const char *agginitval;
11430 /* Skip if not to be dumped */
11431 if (!agginfo->aggfn.dobj.dump || dataOnly)
11434 query = createPQExpBuffer();
11435 q = createPQExpBuffer();
11436 delq = createPQExpBuffer();
11437 labelq = createPQExpBuffer();
11438 details = createPQExpBuffer();
11440 /* Make sure we are in proper schema */
11441 selectSourceSchema(fout, agginfo->aggfn.dobj.namespace->dobj.name);
11443 /* Get aggregate-specific details */
11444 if (fout->remoteVersion >= 80400)
11446 appendPQExpBuffer(query, "SELECT aggtransfn, "
11447 "aggfinalfn, aggtranstype::pg_catalog.regtype, "
11448 "aggsortop::pg_catalog.regoperator, "
11450 "'t'::boolean AS convertok, "
11451 "pg_catalog.pg_get_function_arguments(p.oid) AS funcargs, "
11452 "pg_catalog.pg_get_function_identity_arguments(p.oid) AS funciargs "
11453 "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
11454 "WHERE a.aggfnoid = p.oid "
11455 "AND p.oid = '%u'::pg_catalog.oid",
11456 agginfo->aggfn.dobj.catId.oid);
11458 else if (fout->remoteVersion >= 80100)
11460 appendPQExpBuffer(query, "SELECT aggtransfn, "
11461 "aggfinalfn, aggtranstype::pg_catalog.regtype, "
11462 "aggsortop::pg_catalog.regoperator, "
11464 "'t'::boolean AS convertok "
11465 "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
11466 "WHERE a.aggfnoid = p.oid "
11467 "AND p.oid = '%u'::pg_catalog.oid",
11468 agginfo->aggfn.dobj.catId.oid);
11470 else if (fout->remoteVersion >= 70300)
11472 appendPQExpBuffer(query, "SELECT aggtransfn, "
11473 "aggfinalfn, aggtranstype::pg_catalog.regtype, "
11476 "'t'::boolean AS convertok "
11477 "FROM pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
11478 "WHERE a.aggfnoid = p.oid "
11479 "AND p.oid = '%u'::pg_catalog.oid",
11480 agginfo->aggfn.dobj.catId.oid);
11482 else if (fout->remoteVersion >= 70100)
11484 appendPQExpBuffer(query, "SELECT aggtransfn, aggfinalfn, "
11485 "format_type(aggtranstype, NULL) AS aggtranstype, "
11488 "'t'::boolean AS convertok "
11489 "FROM pg_aggregate "
11490 "WHERE oid = '%u'::oid",
11491 agginfo->aggfn.dobj.catId.oid);
11495 appendPQExpBuffer(query, "SELECT aggtransfn1 AS aggtransfn, "
11497 "(SELECT typname FROM pg_type WHERE oid = aggtranstype1) AS aggtranstype, "
11499 "agginitval1 AS agginitval, "
11500 "(aggtransfn2 = 0 and aggtranstype2 = 0 and agginitval2 is null) AS convertok "
11501 "FROM pg_aggregate "
11502 "WHERE oid = '%u'::oid",
11503 agginfo->aggfn.dobj.catId.oid);
11506 res = ExecuteSqlQueryForSingleRow(fout, query->data);
11508 i_aggtransfn = PQfnumber(res, "aggtransfn");
11509 i_aggfinalfn = PQfnumber(res, "aggfinalfn");
11510 i_aggsortop = PQfnumber(res, "aggsortop");
11511 i_aggtranstype = PQfnumber(res, "aggtranstype");
11512 i_agginitval = PQfnumber(res, "agginitval");
11513 i_convertok = PQfnumber(res, "convertok");
11515 aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
11516 aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
11517 aggsortop = PQgetvalue(res, 0, i_aggsortop);
11518 aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
11519 agginitval = PQgetvalue(res, 0, i_agginitval);
11520 convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
11522 if (fout->remoteVersion >= 80400)
11524 /* 8.4 or later; we rely on server-side code for most of the work */
11528 funcargs = PQgetvalue(res, 0, PQfnumber(res, "funcargs"));
11529 funciargs = PQgetvalue(res, 0, PQfnumber(res, "funciargs"));
11530 aggfullsig = format_function_arguments(&agginfo->aggfn, funcargs, true);
11531 aggsig = format_function_arguments(&agginfo->aggfn, funciargs, true);
11535 /* pre-8.4, do it ourselves */
11536 aggsig = format_aggregate_signature(agginfo, fout, true);
11537 aggfullsig = aggsig;
11540 aggsig_tag = format_aggregate_signature(agginfo, fout, false);
11544 write_msg(NULL, "WARNING: aggregate function %s could not be dumped correctly for this database version; ignored\n",
11549 if (fout->remoteVersion >= 70300)
11551 /* If using 7.3's regproc or regtype, data is already quoted */
11552 appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
11556 else if (fout->remoteVersion >= 70100)
11558 /* format_type quotes, regproc does not */
11559 appendPQExpBuffer(details, " SFUNC = %s,\n STYPE = %s",
11565 /* need quotes all around */
11566 appendPQExpBuffer(details, " SFUNC = %s,\n",
11567 fmtId(aggtransfn));
11568 appendPQExpBuffer(details, " STYPE = %s",
11569 fmtId(aggtranstype));
11572 if (!PQgetisnull(res, 0, i_agginitval))
11574 appendPQExpBuffer(details, ",\n INITCOND = ");
11575 appendStringLiteralAH(details, agginitval, fout);
11578 if (strcmp(aggfinalfn, "-") != 0)
11580 appendPQExpBuffer(details, ",\n FINALFUNC = %s",
11584 aggsortop = convertOperatorReference(fout, aggsortop);
11587 appendPQExpBuffer(details, ",\n SORTOP = %s",
11592 * DROP must be fully qualified in case same name appears in pg_catalog
11594 appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
11595 fmtId(agginfo->aggfn.dobj.namespace->dobj.name),
11598 appendPQExpBuffer(q, "CREATE AGGREGATE %s (\n%s\n);\n",
11599 aggfullsig, details->data);
11601 appendPQExpBuffer(labelq, "AGGREGATE %s", aggsig);
11603 if (binary_upgrade)
11604 binary_upgrade_extension_member(q, &agginfo->aggfn.dobj, labelq->data);
11606 ArchiveEntry(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
11608 agginfo->aggfn.dobj.namespace->dobj.name,
11610 agginfo->aggfn.rolname,
11611 false, "AGGREGATE", SECTION_PRE_DATA,
11612 q->data, delq->data, NULL,
11616 /* Dump Aggregate Comments */
11617 dumpComment(fout, labelq->data,
11618 agginfo->aggfn.dobj.namespace->dobj.name, agginfo->aggfn.rolname,
11619 agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
11620 dumpSecLabel(fout, labelq->data,
11621 agginfo->aggfn.dobj.namespace->dobj.name, agginfo->aggfn.rolname,
11622 agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
11625 * Since there is no GRANT ON AGGREGATE syntax, we have to make the ACL
11626 * command look like a function's GRANT; in particular this affects the
11627 * syntax for zero-argument aggregates.
11632 aggsig = format_function_signature(fout, &agginfo->aggfn, true);
11633 aggsig_tag = format_function_signature(fout, &agginfo->aggfn, false);
11635 dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
11637 aggsig, NULL, aggsig_tag,
11638 agginfo->aggfn.dobj.namespace->dobj.name,
11639 agginfo->aggfn.rolname, agginfo->aggfn.proacl);
11646 destroyPQExpBuffer(query);
11647 destroyPQExpBuffer(q);
11648 destroyPQExpBuffer(delq);
11649 destroyPQExpBuffer(labelq);
11650 destroyPQExpBuffer(details);
11655 * write out a single text search parser
11658 dumpTSParser(Archive *fout, TSParserInfo *prsinfo)
11662 PQExpBuffer labelq;
11664 /* Skip if not to be dumped */
11665 if (!prsinfo->dobj.dump || dataOnly)
11668 q = createPQExpBuffer();
11669 delq = createPQExpBuffer();
11670 labelq = createPQExpBuffer();
11672 /* Make sure we are in proper schema */
11673 selectSourceSchema(fout, prsinfo->dobj.namespace->dobj.name);
11675 appendPQExpBuffer(q, "CREATE TEXT SEARCH PARSER %s (\n",
11676 fmtId(prsinfo->dobj.name));
11678 appendPQExpBuffer(q, " START = %s,\n",
11679 convertTSFunction(fout, prsinfo->prsstart));
11680 appendPQExpBuffer(q, " GETTOKEN = %s,\n",
11681 convertTSFunction(fout, prsinfo->prstoken));
11682 appendPQExpBuffer(q, " END = %s,\n",
11683 convertTSFunction(fout, prsinfo->prsend));
11684 if (prsinfo->prsheadline != InvalidOid)
11685 appendPQExpBuffer(q, " HEADLINE = %s,\n",
11686 convertTSFunction(fout, prsinfo->prsheadline));
11687 appendPQExpBuffer(q, " LEXTYPES = %s );\n",
11688 convertTSFunction(fout, prsinfo->prslextype));
11691 * DROP must be fully qualified in case same name appears in pg_catalog
11693 appendPQExpBuffer(delq, "DROP TEXT SEARCH PARSER %s",
11694 fmtId(prsinfo->dobj.namespace->dobj.name));
11695 appendPQExpBuffer(delq, ".%s;\n",
11696 fmtId(prsinfo->dobj.name));
11698 appendPQExpBuffer(labelq, "TEXT SEARCH PARSER %s",
11699 fmtId(prsinfo->dobj.name));
11701 if (binary_upgrade)
11702 binary_upgrade_extension_member(q, &prsinfo->dobj, labelq->data);
11704 ArchiveEntry(fout, prsinfo->dobj.catId, prsinfo->dobj.dumpId,
11705 prsinfo->dobj.name,
11706 prsinfo->dobj.namespace->dobj.name,
11709 false, "TEXT SEARCH PARSER", SECTION_PRE_DATA,
11710 q->data, delq->data, NULL,
11714 /* Dump Parser Comments */
11715 dumpComment(fout, labelq->data,
11717 prsinfo->dobj.catId, 0, prsinfo->dobj.dumpId);
11719 destroyPQExpBuffer(q);
11720 destroyPQExpBuffer(delq);
11721 destroyPQExpBuffer(labelq);
11726 * write out a single text search dictionary
11729 dumpTSDictionary(Archive *fout, TSDictInfo *dictinfo)
11733 PQExpBuffer labelq;
11739 /* Skip if not to be dumped */
11740 if (!dictinfo->dobj.dump || dataOnly)
11743 q = createPQExpBuffer();
11744 delq = createPQExpBuffer();
11745 labelq = createPQExpBuffer();
11746 query = createPQExpBuffer();
11748 /* Fetch name and namespace of the dictionary's template */
11749 selectSourceSchema(fout, "pg_catalog");
11750 appendPQExpBuffer(query, "SELECT nspname, tmplname "
11751 "FROM pg_ts_template p, pg_namespace n "
11752 "WHERE p.oid = '%u' AND n.oid = tmplnamespace",
11753 dictinfo->dicttemplate);
11754 res = ExecuteSqlQueryForSingleRow(fout, query->data);
11755 nspname = PQgetvalue(res, 0, 0);
11756 tmplname = PQgetvalue(res, 0, 1);
11758 /* Make sure we are in proper schema */
11759 selectSourceSchema(fout, dictinfo->dobj.namespace->dobj.name);
11761 appendPQExpBuffer(q, "CREATE TEXT SEARCH DICTIONARY %s (\n",
11762 fmtId(dictinfo->dobj.name));
11764 appendPQExpBuffer(q, " TEMPLATE = ");
11765 if (strcmp(nspname, dictinfo->dobj.namespace->dobj.name) != 0)
11766 appendPQExpBuffer(q, "%s.", fmtId(nspname));
11767 appendPQExpBuffer(q, "%s", fmtId(tmplname));
11771 /* the dictinitoption can be dumped straight into the command */
11772 if (dictinfo->dictinitoption)
11773 appendPQExpBuffer(q, ",\n %s", dictinfo->dictinitoption);
11775 appendPQExpBuffer(q, " );\n");
11778 * DROP must be fully qualified in case same name appears in pg_catalog
11780 appendPQExpBuffer(delq, "DROP TEXT SEARCH DICTIONARY %s",
11781 fmtId(dictinfo->dobj.namespace->dobj.name));
11782 appendPQExpBuffer(delq, ".%s;\n",
11783 fmtId(dictinfo->dobj.name));
11785 appendPQExpBuffer(labelq, "TEXT SEARCH DICTIONARY %s",
11786 fmtId(dictinfo->dobj.name));
11788 if (binary_upgrade)
11789 binary_upgrade_extension_member(q, &dictinfo->dobj, labelq->data);
11791 ArchiveEntry(fout, dictinfo->dobj.catId, dictinfo->dobj.dumpId,
11792 dictinfo->dobj.name,
11793 dictinfo->dobj.namespace->dobj.name,
11796 false, "TEXT SEARCH DICTIONARY", SECTION_PRE_DATA,
11797 q->data, delq->data, NULL,
11801 /* Dump Dictionary Comments */
11802 dumpComment(fout, labelq->data,
11803 NULL, dictinfo->rolname,
11804 dictinfo->dobj.catId, 0, dictinfo->dobj.dumpId);
11806 destroyPQExpBuffer(q);
11807 destroyPQExpBuffer(delq);
11808 destroyPQExpBuffer(labelq);
11809 destroyPQExpBuffer(query);
11814 * write out a single text search template
11817 dumpTSTemplate(Archive *fout, TSTemplateInfo *tmplinfo)
11821 PQExpBuffer labelq;
11823 /* Skip if not to be dumped */
11824 if (!tmplinfo->dobj.dump || dataOnly)
11827 q = createPQExpBuffer();
11828 delq = createPQExpBuffer();
11829 labelq = createPQExpBuffer();
11831 /* Make sure we are in proper schema */
11832 selectSourceSchema(fout, tmplinfo->dobj.namespace->dobj.name);
11834 appendPQExpBuffer(q, "CREATE TEXT SEARCH TEMPLATE %s (\n",
11835 fmtId(tmplinfo->dobj.name));
11837 if (tmplinfo->tmplinit != InvalidOid)
11838 appendPQExpBuffer(q, " INIT = %s,\n",
11839 convertTSFunction(fout, tmplinfo->tmplinit));
11840 appendPQExpBuffer(q, " LEXIZE = %s );\n",
11841 convertTSFunction(fout, tmplinfo->tmpllexize));
11844 * DROP must be fully qualified in case same name appears in pg_catalog
11846 appendPQExpBuffer(delq, "DROP TEXT SEARCH TEMPLATE %s",
11847 fmtId(tmplinfo->dobj.namespace->dobj.name));
11848 appendPQExpBuffer(delq, ".%s;\n",
11849 fmtId(tmplinfo->dobj.name));
11851 appendPQExpBuffer(labelq, "TEXT SEARCH TEMPLATE %s",
11852 fmtId(tmplinfo->dobj.name));
11854 if (binary_upgrade)
11855 binary_upgrade_extension_member(q, &tmplinfo->dobj, labelq->data);
11857 ArchiveEntry(fout, tmplinfo->dobj.catId, tmplinfo->dobj.dumpId,
11858 tmplinfo->dobj.name,
11859 tmplinfo->dobj.namespace->dobj.name,
11862 false, "TEXT SEARCH TEMPLATE", SECTION_PRE_DATA,
11863 q->data, delq->data, NULL,
11867 /* Dump Template Comments */
11868 dumpComment(fout, labelq->data,
11870 tmplinfo->dobj.catId, 0, tmplinfo->dobj.dumpId);
11872 destroyPQExpBuffer(q);
11873 destroyPQExpBuffer(delq);
11874 destroyPQExpBuffer(labelq);
11879 * write out a single text search configuration
11882 dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo)
11886 PQExpBuffer labelq;
11896 /* Skip if not to be dumped */
11897 if (!cfginfo->dobj.dump || dataOnly)
11900 q = createPQExpBuffer();
11901 delq = createPQExpBuffer();
11902 labelq = createPQExpBuffer();
11903 query = createPQExpBuffer();
11905 /* Fetch name and namespace of the config's parser */
11906 selectSourceSchema(fout, "pg_catalog");
11907 appendPQExpBuffer(query, "SELECT nspname, prsname "
11908 "FROM pg_ts_parser p, pg_namespace n "
11909 "WHERE p.oid = '%u' AND n.oid = prsnamespace",
11910 cfginfo->cfgparser);
11911 res = ExecuteSqlQueryForSingleRow(fout, query->data);
11912 nspname = PQgetvalue(res, 0, 0);
11913 prsname = PQgetvalue(res, 0, 1);
11915 /* Make sure we are in proper schema */
11916 selectSourceSchema(fout, cfginfo->dobj.namespace->dobj.name);
11918 appendPQExpBuffer(q, "CREATE TEXT SEARCH CONFIGURATION %s (\n",
11919 fmtId(cfginfo->dobj.name));
11921 appendPQExpBuffer(q, " PARSER = ");
11922 if (strcmp(nspname, cfginfo->dobj.namespace->dobj.name) != 0)
11923 appendPQExpBuffer(q, "%s.", fmtId(nspname));
11924 appendPQExpBuffer(q, "%s );\n", fmtId(prsname));
11928 resetPQExpBuffer(query);
11929 appendPQExpBuffer(query,
11931 " ( SELECT alias FROM pg_catalog.ts_token_type('%u'::pg_catalog.oid) AS t \n"
11932 " WHERE t.tokid = m.maptokentype ) AS tokenname, \n"
11933 " m.mapdict::pg_catalog.regdictionary AS dictname \n"
11934 "FROM pg_catalog.pg_ts_config_map AS m \n"
11935 "WHERE m.mapcfg = '%u' \n"
11936 "ORDER BY m.mapcfg, m.maptokentype, m.mapseqno",
11937 cfginfo->cfgparser, cfginfo->dobj.catId.oid);
11939 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
11940 ntups = PQntuples(res);
11942 i_tokenname = PQfnumber(res, "tokenname");
11943 i_dictname = PQfnumber(res, "dictname");
11945 for (i = 0; i < ntups; i++)
11947 char *tokenname = PQgetvalue(res, i, i_tokenname);
11948 char *dictname = PQgetvalue(res, i, i_dictname);
11951 strcmp(tokenname, PQgetvalue(res, i - 1, i_tokenname)) != 0)
11953 /* starting a new token type, so start a new command */
11955 appendPQExpBuffer(q, ";\n");
11956 appendPQExpBuffer(q, "\nALTER TEXT SEARCH CONFIGURATION %s\n",
11957 fmtId(cfginfo->dobj.name));
11958 /* tokenname needs quoting, dictname does NOT */
11959 appendPQExpBuffer(q, " ADD MAPPING FOR %s WITH %s",
11960 fmtId(tokenname), dictname);
11963 appendPQExpBuffer(q, ", %s", dictname);
11967 appendPQExpBuffer(q, ";\n");
11972 * DROP must be fully qualified in case same name appears in pg_catalog
11974 appendPQExpBuffer(delq, "DROP TEXT SEARCH CONFIGURATION %s",
11975 fmtId(cfginfo->dobj.namespace->dobj.name));
11976 appendPQExpBuffer(delq, ".%s;\n",
11977 fmtId(cfginfo->dobj.name));
11979 appendPQExpBuffer(labelq, "TEXT SEARCH CONFIGURATION %s",
11980 fmtId(cfginfo->dobj.name));
11982 if (binary_upgrade)
11983 binary_upgrade_extension_member(q, &cfginfo->dobj, labelq->data);
11985 ArchiveEntry(fout, cfginfo->dobj.catId, cfginfo->dobj.dumpId,
11986 cfginfo->dobj.name,
11987 cfginfo->dobj.namespace->dobj.name,
11990 false, "TEXT SEARCH CONFIGURATION", SECTION_PRE_DATA,
11991 q->data, delq->data, NULL,
11995 /* Dump Configuration Comments */
11996 dumpComment(fout, labelq->data,
11997 NULL, cfginfo->rolname,
11998 cfginfo->dobj.catId, 0, cfginfo->dobj.dumpId);
12000 destroyPQExpBuffer(q);
12001 destroyPQExpBuffer(delq);
12002 destroyPQExpBuffer(labelq);
12003 destroyPQExpBuffer(query);
12007 * dumpForeignDataWrapper
12008 * write out a single foreign-data wrapper definition
12011 dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo)
12015 PQExpBuffer labelq;
12018 /* Skip if not to be dumped */
12019 if (!fdwinfo->dobj.dump || dataOnly)
12023 * FDWs that belong to an extension are dumped based on their "dump"
12024 * field. Otherwise omit them if we are only dumping some specific object.
12026 if (!fdwinfo->dobj.ext_member)
12027 if (!include_everything)
12030 q = createPQExpBuffer();
12031 delq = createPQExpBuffer();
12032 labelq = createPQExpBuffer();
12034 qfdwname = pg_strdup(fmtId(fdwinfo->dobj.name));
12036 appendPQExpBuffer(q, "CREATE FOREIGN DATA WRAPPER %s",
12039 if (strcmp(fdwinfo->fdwhandler, "-") != 0)
12040 appendPQExpBuffer(q, " HANDLER %s", fdwinfo->fdwhandler);
12042 if (strcmp(fdwinfo->fdwvalidator, "-") != 0)
12043 appendPQExpBuffer(q, " VALIDATOR %s", fdwinfo->fdwvalidator);
12045 if (strlen(fdwinfo->fdwoptions) > 0)
12046 appendPQExpBuffer(q, " OPTIONS (\n %s\n)", fdwinfo->fdwoptions);
12048 appendPQExpBuffer(q, ";\n");
12050 appendPQExpBuffer(delq, "DROP FOREIGN DATA WRAPPER %s;\n",
12053 appendPQExpBuffer(labelq, "FOREIGN DATA WRAPPER %s",
12056 if (binary_upgrade)
12057 binary_upgrade_extension_member(q, &fdwinfo->dobj, labelq->data);
12059 ArchiveEntry(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
12060 fdwinfo->dobj.name,
12064 false, "FOREIGN DATA WRAPPER", SECTION_PRE_DATA,
12065 q->data, delq->data, NULL,
12069 /* Handle the ACL */
12070 dumpACL(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
12071 "FOREIGN DATA WRAPPER",
12072 qfdwname, NULL, fdwinfo->dobj.name,
12073 NULL, fdwinfo->rolname,
12076 /* Dump Foreign Data Wrapper Comments */
12077 dumpComment(fout, labelq->data,
12078 NULL, fdwinfo->rolname,
12079 fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
12083 destroyPQExpBuffer(q);
12084 destroyPQExpBuffer(delq);
12085 destroyPQExpBuffer(labelq);
12089 * dumpForeignServer
12090 * write out a foreign server definition
12093 dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo)
12097 PQExpBuffer labelq;
12103 /* Skip if not to be dumped */
12104 if (!srvinfo->dobj.dump || dataOnly || !include_everything)
12107 q = createPQExpBuffer();
12108 delq = createPQExpBuffer();
12109 labelq = createPQExpBuffer();
12110 query = createPQExpBuffer();
12112 qsrvname = pg_strdup(fmtId(srvinfo->dobj.name));
12114 /* look up the foreign-data wrapper */
12115 selectSourceSchema(fout, "pg_catalog");
12116 appendPQExpBuffer(query, "SELECT fdwname "
12117 "FROM pg_foreign_data_wrapper w "
12118 "WHERE w.oid = '%u'",
12120 res = ExecuteSqlQueryForSingleRow(fout, query->data);
12121 fdwname = PQgetvalue(res, 0, 0);
12123 appendPQExpBuffer(q, "CREATE SERVER %s", qsrvname);
12124 if (srvinfo->srvtype && strlen(srvinfo->srvtype) > 0)
12126 appendPQExpBuffer(q, " TYPE ");
12127 appendStringLiteralAH(q, srvinfo->srvtype, fout);
12129 if (srvinfo->srvversion && strlen(srvinfo->srvversion) > 0)
12131 appendPQExpBuffer(q, " VERSION ");
12132 appendStringLiteralAH(q, srvinfo->srvversion, fout);
12135 appendPQExpBuffer(q, " FOREIGN DATA WRAPPER ");
12136 appendPQExpBuffer(q, "%s", fmtId(fdwname));
12138 if (srvinfo->srvoptions && strlen(srvinfo->srvoptions) > 0)
12139 appendPQExpBuffer(q, " OPTIONS (\n %s\n)", srvinfo->srvoptions);
12141 appendPQExpBuffer(q, ";\n");
12143 appendPQExpBuffer(delq, "DROP SERVER %s;\n",
12146 appendPQExpBuffer(labelq, "SERVER %s", qsrvname);
12148 if (binary_upgrade)
12149 binary_upgrade_extension_member(q, &srvinfo->dobj, labelq->data);
12151 ArchiveEntry(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
12152 srvinfo->dobj.name,
12156 false, "SERVER", SECTION_PRE_DATA,
12157 q->data, delq->data, NULL,
12161 /* Handle the ACL */
12162 dumpACL(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
12164 qsrvname, NULL, srvinfo->dobj.name,
12165 NULL, srvinfo->rolname,
12168 /* Dump user mappings */
12169 dumpUserMappings(fout,
12170 srvinfo->dobj.name, NULL,
12172 srvinfo->dobj.catId, srvinfo->dobj.dumpId);
12174 /* Dump Foreign Server Comments */
12175 dumpComment(fout, labelq->data,
12176 NULL, srvinfo->rolname,
12177 srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
12181 destroyPQExpBuffer(q);
12182 destroyPQExpBuffer(delq);
12183 destroyPQExpBuffer(labelq);
12189 * This routine is used to dump any user mappings associated with the
12190 * server handed to this routine. Should be called after ArchiveEntry()
12194 dumpUserMappings(Archive *fout,
12195 const char *servername, const char *namespace,
12197 CatalogId catalogId, DumpId dumpId)
12209 q = createPQExpBuffer();
12210 tag = createPQExpBuffer();
12211 delq = createPQExpBuffer();
12212 query = createPQExpBuffer();
12215 * We read from the publicly accessible view pg_user_mappings, so as not
12216 * to fail if run by a non-superuser. Note that the view will show
12217 * umoptions as null if the user hasn't got privileges for the associated
12218 * server; this means that pg_dump will dump such a mapping, but with no
12219 * OPTIONS clause. A possible alternative is to skip such mappings
12220 * altogether, but it's not clear that that's an improvement.
12222 selectSourceSchema(fout, "pg_catalog");
12224 appendPQExpBuffer(query,
12226 "array_to_string(ARRAY("
12227 "SELECT quote_ident(option_name) || ' ' || "
12228 "quote_literal(option_value) "
12229 "FROM pg_options_to_table(umoptions) "
12230 "ORDER BY option_name"
12231 "), E',\n ') AS umoptions "
12232 "FROM pg_user_mappings "
12233 "WHERE srvid = '%u' "
12234 "ORDER BY usename",
12237 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12239 ntups = PQntuples(res);
12240 i_usename = PQfnumber(res, "usename");
12241 i_umoptions = PQfnumber(res, "umoptions");
12243 for (i = 0; i < ntups; i++)
12248 usename = PQgetvalue(res, i, i_usename);
12249 umoptions = PQgetvalue(res, i, i_umoptions);
12251 resetPQExpBuffer(q);
12252 appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
12253 appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
12255 if (umoptions && strlen(umoptions) > 0)
12256 appendPQExpBuffer(q, " OPTIONS (\n %s\n)", umoptions);
12258 appendPQExpBuffer(q, ";\n");
12260 resetPQExpBuffer(delq);
12261 appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
12262 appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
12264 resetPQExpBuffer(tag);
12265 appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
12266 usename, servername);
12268 ArchiveEntry(fout, nilCatalogId, createDumpId(),
12273 "USER MAPPING", SECTION_PRE_DATA,
12274 q->data, delq->data, NULL,
12281 destroyPQExpBuffer(query);
12282 destroyPQExpBuffer(delq);
12283 destroyPQExpBuffer(q);
12287 * Write out default privileges information
12290 dumpDefaultACL(Archive *fout, DefaultACLInfo *daclinfo)
12296 /* Skip if not to be dumped */
12297 if (!daclinfo->dobj.dump || dataOnly || aclsSkip)
12300 q = createPQExpBuffer();
12301 tag = createPQExpBuffer();
12303 switch (daclinfo->defaclobjtype)
12305 case DEFACLOBJ_RELATION:
12308 case DEFACLOBJ_SEQUENCE:
12309 type = "SEQUENCES";
12311 case DEFACLOBJ_FUNCTION:
12312 type = "FUNCTIONS";
12314 case DEFACLOBJ_TYPE:
12318 /* shouldn't get here */
12319 exit_horribly(NULL,
12320 "unrecognized object type in default privileges: %d\n",
12321 (int) daclinfo->defaclobjtype);
12322 type = ""; /* keep compiler quiet */
12325 appendPQExpBuffer(tag, "DEFAULT PRIVILEGES FOR %s", type);
12327 /* build the actual command(s) for this tuple */
12328 if (!buildDefaultACLCommands(type,
12329 daclinfo->dobj.namespace != NULL ?
12330 daclinfo->dobj.namespace->dobj.name : NULL,
12331 daclinfo->defaclacl,
12332 daclinfo->defaclrole,
12333 fout->remoteVersion,
12335 exit_horribly(NULL, "could not parse default ACL list (%s)\n",
12336 daclinfo->defaclacl);
12338 ArchiveEntry(fout, daclinfo->dobj.catId, daclinfo->dobj.dumpId,
12340 daclinfo->dobj.namespace ? daclinfo->dobj.namespace->dobj.name : NULL,
12342 daclinfo->defaclrole,
12343 false, "DEFAULT ACL", SECTION_POST_DATA,
12348 destroyPQExpBuffer(tag);
12349 destroyPQExpBuffer(q);
12353 * Write out grant/revoke information
12355 * 'objCatId' is the catalog ID of the underlying object.
12356 * 'objDumpId' is the dump ID of the underlying object.
12357 * 'type' must be one of
12358 * TABLE, SEQUENCE, FUNCTION, LANGUAGE, SCHEMA, DATABASE, TABLESPACE,
12359 * FOREIGN DATA WRAPPER, SERVER, or LARGE OBJECT.
12360 * 'name' is the formatted name of the object. Must be quoted etc. already.
12361 * 'subname' is the formatted name of the sub-object, if any. Must be quoted.
12362 * 'tag' is the tag for the archive entry (typ. unquoted name of object).
12363 * 'nspname' is the namespace the object is in (NULL if none).
12364 * 'owner' is the owner, NULL if there is no owner (for languages).
12365 * 'acls' is the string read out of the fooacl system catalog field;
12366 * it will be parsed here.
12370 dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
12371 const char *type, const char *name, const char *subname,
12372 const char *tag, const char *nspname, const char *owner,
12377 /* Do nothing if ACL dump is not enabled */
12381 /* --data-only skips ACLs *except* BLOB ACLs */
12382 if (dataOnly && strcmp(type, "LARGE OBJECT") != 0)
12385 sql = createPQExpBuffer();
12387 if (!buildACLCommands(name, subname, type, acls, owner,
12388 "", fout->remoteVersion, sql))
12389 exit_horribly(NULL,
12390 "could not parse ACL list (%s) for object \"%s\" (%s)\n",
12394 ArchiveEntry(fout, nilCatalogId, createDumpId(),
12397 owner ? owner : "",
12398 false, "ACL", SECTION_NONE,
12399 sql->data, "", NULL,
12403 destroyPQExpBuffer(sql);
12409 * This routine is used to dump any security labels associated with the
12410 * object handed to this routine. The routine takes a constant character
12411 * string for the target part of the security-label command, plus
12412 * the namespace and owner of the object (for labeling the ArchiveEntry),
12413 * plus catalog ID and subid which are the lookup key for pg_seclabel,
12414 * plus the dump ID for the object (for setting a dependency).
12415 * If a matching pg_seclabel entry is found, it is dumped.
12417 * Note: although this routine takes a dumpId for dependency purposes,
12418 * that purpose is just to mark the dependency in the emitted dump file
12419 * for possible future use by pg_restore. We do NOT use it for determining
12420 * ordering of the label in the dump file, because this routine is called
12421 * after dependency sorting occurs. This routine should be called just after
12422 * calling ArchiveEntry() for the specified object.
12425 dumpSecLabel(Archive *fout, const char *target,
12426 const char *namespace, const char *owner,
12427 CatalogId catalogId, int subid, DumpId dumpId)
12429 SecLabelItem *labels;
12434 /* do nothing, if --no-security-labels is supplied */
12435 if (no_security_labels)
12438 /* Comments are schema not data ... except blob comments are data */
12439 if (strncmp(target, "LARGE OBJECT ", 13) != 0)
12450 /* Search for security labels associated with catalogId, using table */
12451 nlabels = findSecLabels(fout, catalogId.tableoid, catalogId.oid, &labels);
12453 query = createPQExpBuffer();
12455 for (i = 0; i < nlabels; i++)
12458 * Ignore label entries for which the subid doesn't match.
12460 if (labels[i].objsubid != subid)
12463 appendPQExpBuffer(query,
12464 "SECURITY LABEL FOR %s ON %s IS ",
12465 fmtId(labels[i].provider), target);
12466 appendStringLiteralAH(query, labels[i].label, fout);
12467 appendPQExpBuffer(query, ";\n");
12470 if (query->len > 0)
12472 ArchiveEntry(fout, nilCatalogId, createDumpId(),
12473 target, namespace, NULL, owner,
12474 false, "SECURITY LABEL", SECTION_NONE,
12475 query->data, "", NULL,
12479 destroyPQExpBuffer(query);
12483 * dumpTableSecLabel
12485 * As above, but dump security label for both the specified table (or view)
12489 dumpTableSecLabel(Archive *fout, TableInfo *tbinfo, const char *reltypename)
12491 SecLabelItem *labels;
12495 PQExpBuffer target;
12497 /* do nothing, if --no-security-labels is supplied */
12498 if (no_security_labels)
12501 /* SecLabel are SCHEMA not data */
12505 /* Search for comments associated with relation, using table */
12506 nlabels = findSecLabels(fout,
12507 tbinfo->dobj.catId.tableoid,
12508 tbinfo->dobj.catId.oid,
12511 /* If security labels exist, build SECURITY LABEL statements */
12515 query = createPQExpBuffer();
12516 target = createPQExpBuffer();
12518 for (i = 0; i < nlabels; i++)
12520 const char *colname;
12521 const char *provider = labels[i].provider;
12522 const char *label = labels[i].label;
12523 int objsubid = labels[i].objsubid;
12525 resetPQExpBuffer(target);
12528 appendPQExpBuffer(target, "%s %s", reltypename,
12529 fmtId(tbinfo->dobj.name));
12533 colname = getAttrName(objsubid, tbinfo);
12534 /* first fmtId result must be consumed before calling it again */
12535 appendPQExpBuffer(target, "COLUMN %s", fmtId(tbinfo->dobj.name));
12536 appendPQExpBuffer(target, ".%s", fmtId(colname));
12538 appendPQExpBuffer(query, "SECURITY LABEL FOR %s ON %s IS ",
12539 fmtId(provider), target->data);
12540 appendStringLiteralAH(query, label, fout);
12541 appendPQExpBuffer(query, ";\n");
12543 if (query->len > 0)
12545 resetPQExpBuffer(target);
12546 appendPQExpBuffer(target, "%s %s", reltypename,
12547 fmtId(tbinfo->dobj.name));
12548 ArchiveEntry(fout, nilCatalogId, createDumpId(),
12550 tbinfo->dobj.namespace->dobj.name,
12551 NULL, tbinfo->rolname,
12552 false, "SECURITY LABEL", SECTION_NONE,
12553 query->data, "", NULL,
12554 &(tbinfo->dobj.dumpId), 1,
12557 destroyPQExpBuffer(query);
12558 destroyPQExpBuffer(target);
12564 * Find the security label(s), if any, associated with the given object.
12565 * All the objsubid values associated with the given classoid/objoid are
12566 * found with one search.
12569 findSecLabels(Archive *fout, Oid classoid, Oid objoid, SecLabelItem **items)
12571 /* static storage for table of security labels */
12572 static SecLabelItem *labels = NULL;
12573 static int nlabels = -1;
12575 SecLabelItem *middle = NULL;
12577 SecLabelItem *high;
12580 /* Get security labels if we didn't already */
12582 nlabels = collectSecLabels(fout, &labels);
12584 if (nlabels <= 0) /* no labels, so no match is possible */
12591 * Do binary search to find some item matching the object.
12594 high = &labels[nlabels - 1];
12595 while (low <= high)
12597 middle = low + (high - low) / 2;
12599 if (classoid < middle->classoid)
12601 else if (classoid > middle->classoid)
12603 else if (objoid < middle->objoid)
12605 else if (objoid > middle->objoid)
12608 break; /* found a match */
12611 if (low > high) /* no matches */
12618 * Now determine how many items match the object. The search loop
12619 * invariant still holds: only items between low and high inclusive could
12623 while (middle > low)
12625 if (classoid != middle[-1].classoid ||
12626 objoid != middle[-1].objoid)
12635 while (middle <= high)
12637 if (classoid != middle->classoid ||
12638 objoid != middle->objoid)
12650 * Construct a table of all security labels available for database objects.
12651 * It's much faster to pull them all at once.
12653 * The table is sorted by classoid/objid/objsubid for speed in lookup.
12656 collectSecLabels(Archive *fout, SecLabelItem **items)
12667 SecLabelItem *labels;
12669 query = createPQExpBuffer();
12671 appendPQExpBuffer(query,
12672 "SELECT label, provider, classoid, objoid, objsubid "
12673 "FROM pg_catalog.pg_seclabel "
12674 "ORDER BY classoid, objoid, objsubid");
12676 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12678 /* Construct lookup table containing OIDs in numeric form */
12679 i_label = PQfnumber(res, "label");
12680 i_provider = PQfnumber(res, "provider");
12681 i_classoid = PQfnumber(res, "classoid");
12682 i_objoid = PQfnumber(res, "objoid");
12683 i_objsubid = PQfnumber(res, "objsubid");
12685 ntups = PQntuples(res);
12687 labels = (SecLabelItem *) pg_malloc(ntups * sizeof(SecLabelItem));
12689 for (i = 0; i < ntups; i++)
12691 labels[i].label = PQgetvalue(res, i, i_label);
12692 labels[i].provider = PQgetvalue(res, i, i_provider);
12693 labels[i].classoid = atooid(PQgetvalue(res, i, i_classoid));
12694 labels[i].objoid = atooid(PQgetvalue(res, i, i_objoid));
12695 labels[i].objsubid = atoi(PQgetvalue(res, i, i_objsubid));
12698 /* Do NOT free the PGresult since we are keeping pointers into it */
12699 destroyPQExpBuffer(query);
12707 * write out to fout the declarations (not data) of a user-defined table
12710 dumpTable(Archive *fout, TableInfo *tbinfo)
12712 if (tbinfo->dobj.dump && !dataOnly)
12716 if (tbinfo->relkind == RELKIND_SEQUENCE)
12717 dumpSequence(fout, tbinfo);
12719 dumpTableSchema(fout, tbinfo);
12721 /* Handle the ACL here */
12722 namecopy = pg_strdup(fmtId(tbinfo->dobj.name));
12723 dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
12724 (tbinfo->relkind == RELKIND_SEQUENCE) ? "SEQUENCE" :
12726 namecopy, NULL, tbinfo->dobj.name,
12727 tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
12731 * Handle column ACLs, if any. Note: we pull these with a separate
12732 * query rather than trying to fetch them during getTableAttrs, so
12733 * that we won't miss ACLs on system columns.
12735 if (fout->remoteVersion >= 80400)
12737 PQExpBuffer query = createPQExpBuffer();
12741 appendPQExpBuffer(query,
12742 "SELECT attname, attacl FROM pg_catalog.pg_attribute "
12743 "WHERE attrelid = '%u' AND NOT attisdropped AND attacl IS NOT NULL "
12745 tbinfo->dobj.catId.oid);
12746 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12748 for (i = 0; i < PQntuples(res); i++)
12750 char *attname = PQgetvalue(res, i, 0);
12751 char *attacl = PQgetvalue(res, i, 1);
12755 attnamecopy = pg_strdup(fmtId(attname));
12756 pg_asprintf(&acltag, "%s.%s", tbinfo->dobj.name, attname);
12757 /* Column's GRANT type is always TABLE */
12758 dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, "TABLE",
12759 namecopy, attnamecopy, acltag,
12760 tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
12766 destroyPQExpBuffer(query);
12774 * Create the AS clause for a view or materialized view. The semicolon is
12775 * stripped because a materialized view must add a WITH NO DATA clause.
12777 * This returns a new buffer which must be freed by the caller.
12780 createViewAsClause(Archive *fout, TableInfo *tbinfo)
12782 PQExpBuffer query = createPQExpBuffer();
12783 PQExpBuffer result = createPQExpBuffer();
12787 /* Fetch the view definition */
12788 if (fout->remoteVersion >= 70300)
12790 /* Beginning in 7.3, viewname is not unique; rely on OID */
12791 appendPQExpBuffer(query,
12792 "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef",
12793 tbinfo->dobj.catId.oid);
12797 appendPQExpBuffer(query, "SELECT definition AS viewdef "
12798 "FROM pg_views WHERE viewname = ");
12799 appendStringLiteralAH(query, tbinfo->dobj.name, fout);
12802 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
12804 if (PQntuples(res) != 1)
12806 if (PQntuples(res) < 1)
12807 exit_horribly(NULL, "query to obtain definition of view \"%s\" returned no data\n",
12808 tbinfo->dobj.name);
12810 exit_horribly(NULL, "query to obtain definition of view \"%s\" returned more than one definition\n",
12811 tbinfo->dobj.name);
12814 len = PQgetlength(res, 0, 0);
12817 exit_horribly(NULL, "definition of view \"%s\" appears to be empty (length zero)\n",
12818 tbinfo->dobj.name);
12820 /* Strip off the trailing semicolon so that other things may follow. */
12821 Assert(PQgetvalue(res, 0, 0)[len - 1] == ';');
12822 appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1);
12825 destroyPQExpBuffer(query);
12832 * write the declaration (not data) of one user-defined table or view
12835 dumpTableSchema(Archive *fout, TableInfo *tbinfo)
12837 PQExpBuffer q = createPQExpBuffer();
12838 PQExpBuffer delq = createPQExpBuffer();
12839 PQExpBuffer labelq = createPQExpBuffer();
12841 TableInfo **parents;
12842 int actual_atts; /* number of attrs in this CREATE statement */
12843 const char *reltypename;
12850 /* Make sure we are in proper schema */
12851 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
12853 if (binary_upgrade)
12854 binary_upgrade_set_type_oids_by_rel_oid(fout, q,
12855 tbinfo->dobj.catId.oid);
12857 /* Is it a table or a view? */
12858 if (tbinfo->relkind == RELKIND_VIEW)
12860 PQExpBuffer result;
12862 reltypename = "VIEW";
12865 * DROP must be fully qualified in case same name appears in
12868 appendPQExpBuffer(delq, "DROP VIEW %s.",
12869 fmtId(tbinfo->dobj.namespace->dobj.name));
12870 appendPQExpBuffer(delq, "%s;\n",
12871 fmtId(tbinfo->dobj.name));
12873 if (binary_upgrade)
12874 binary_upgrade_set_pg_class_oids(fout, q,
12875 tbinfo->dobj.catId.oid, false);
12877 appendPQExpBuffer(q, "CREATE VIEW %s", fmtId(tbinfo->dobj.name));
12878 if (tbinfo->reloptions && strlen(tbinfo->reloptions) > 0)
12879 appendPQExpBuffer(q, " WITH (%s)", tbinfo->reloptions);
12880 result = createViewAsClause(fout, tbinfo);
12881 appendPQExpBuffer(q, " AS\n%s", result->data);
12882 destroyPQExpBuffer(result);
12884 if (tbinfo->checkoption != NULL)
12885 appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption);
12886 appendPQExpBuffer(q, ";\n");
12888 appendPQExpBuffer(labelq, "VIEW %s",
12889 fmtId(tbinfo->dobj.name));
12893 switch (tbinfo->relkind)
12895 case (RELKIND_FOREIGN_TABLE):
12897 PQExpBuffer query = createPQExpBuffer();
12902 reltypename = "FOREIGN TABLE";
12904 /* retrieve name of foreign server and generic options */
12905 appendPQExpBuffer(query,
12906 "SELECT fs.srvname, "
12907 "pg_catalog.array_to_string(ARRAY("
12908 "SELECT pg_catalog.quote_ident(option_name) || "
12909 "' ' || pg_catalog.quote_literal(option_value) "
12910 "FROM pg_catalog.pg_options_to_table(ftoptions) "
12911 "ORDER BY option_name"
12912 "), E',\n ') AS ftoptions "
12913 "FROM pg_catalog.pg_foreign_table ft "
12914 "JOIN pg_catalog.pg_foreign_server fs "
12915 "ON (fs.oid = ft.ftserver) "
12916 "WHERE ft.ftrelid = '%u'",
12917 tbinfo->dobj.catId.oid);
12918 res = ExecuteSqlQueryForSingleRow(fout, query->data);
12919 i_srvname = PQfnumber(res, "srvname");
12920 i_ftoptions = PQfnumber(res, "ftoptions");
12921 srvname = pg_strdup(PQgetvalue(res, 0, i_srvname));
12922 ftoptions = pg_strdup(PQgetvalue(res, 0, i_ftoptions));
12924 destroyPQExpBuffer(query);
12927 case (RELKIND_MATVIEW):
12928 reltypename = "MATERIALIZED VIEW";
12933 reltypename = "TABLE";
12938 numParents = tbinfo->numParents;
12939 parents = tbinfo->parents;
12942 * DROP must be fully qualified in case same name appears in
12945 appendPQExpBuffer(delq, "DROP %s %s.", reltypename,
12946 fmtId(tbinfo->dobj.namespace->dobj.name));
12947 appendPQExpBuffer(delq, "%s;\n",
12948 fmtId(tbinfo->dobj.name));
12950 appendPQExpBuffer(labelq, "%s %s", reltypename,
12951 fmtId(tbinfo->dobj.name));
12953 if (binary_upgrade)
12954 binary_upgrade_set_pg_class_oids(fout, q,
12955 tbinfo->dobj.catId.oid, false);
12957 appendPQExpBuffer(q, "CREATE %s%s %s",
12958 tbinfo->relpersistence == RELPERSISTENCE_UNLOGGED ?
12961 fmtId(tbinfo->dobj.name));
12964 * Attach to type, if reloftype; except in case of a binary upgrade,
12965 * we dump the table normally and attach it to the type afterward.
12967 if (tbinfo->reloftype && !binary_upgrade)
12968 appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
12970 if (tbinfo->relkind != RELKIND_MATVIEW)
12972 /* Dump the attributes */
12974 for (j = 0; j < tbinfo->numatts; j++)
12977 * Normally, dump if it's locally defined in this table, and
12978 * not dropped. But for binary upgrade, we'll dump all the
12979 * columns, and then fix up the dropped and nonlocal cases
12982 if (shouldPrintColumn(tbinfo, j))
12985 * Default value --- suppress if to be printed separately.
12987 bool has_default = (tbinfo->attrdefs[j] != NULL &&
12988 !tbinfo->attrdefs[j]->separate);
12991 * Not Null constraint --- suppress if inherited, except
12992 * in binary-upgrade case where that won't work.
12994 bool has_notnull = (tbinfo->notnull[j] &&
12995 (!tbinfo->inhNotNull[j] ||
12998 /* Skip column if fully defined by reloftype */
12999 if (tbinfo->reloftype &&
13000 !has_default && !has_notnull && !binary_upgrade)
13003 /* Format properly if not first attr */
13004 if (actual_atts == 0)
13005 appendPQExpBuffer(q, " (");
13007 appendPQExpBuffer(q, ",");
13008 appendPQExpBuffer(q, "\n ");
13011 /* Attribute name */
13012 appendPQExpBuffer(q, "%s",
13013 fmtId(tbinfo->attnames[j]));
13015 if (tbinfo->attisdropped[j])
13018 * ALTER TABLE DROP COLUMN clears
13019 * pg_attribute.atttypid, so we will not have gotten a
13020 * valid type name; insert INTEGER as a stopgap. We'll
13021 * clean things up later.
13023 appendPQExpBuffer(q, " INTEGER /* dummy */");
13024 /* Skip all the rest, too */
13028 /* Attribute type */
13029 if (tbinfo->reloftype && !binary_upgrade)
13031 appendPQExpBuffer(q, " WITH OPTIONS");
13033 else if (fout->remoteVersion >= 70100)
13035 appendPQExpBuffer(q, " %s",
13036 tbinfo->atttypnames[j]);
13040 /* If no format_type, fake it */
13041 appendPQExpBuffer(q, " %s",
13042 myFormatType(tbinfo->atttypnames[j],
13043 tbinfo->atttypmod[j]));
13046 /* Add collation if not default for the type */
13047 if (OidIsValid(tbinfo->attcollation[j]))
13051 coll = findCollationByOid(tbinfo->attcollation[j]);
13054 /* always schema-qualify, don't try to be smart */
13055 appendPQExpBuffer(q, " COLLATE %s.",
13056 fmtId(coll->dobj.namespace->dobj.name));
13057 appendPQExpBuffer(q, "%s",
13058 fmtId(coll->dobj.name));
13063 appendPQExpBuffer(q, " DEFAULT %s",
13064 tbinfo->attrdefs[j]->adef_expr);
13067 appendPQExpBuffer(q, " NOT NULL");
13072 * Add non-inherited CHECK constraints, if any.
13074 for (j = 0; j < tbinfo->ncheck; j++)
13076 ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
13078 if (constr->separate || !constr->conislocal)
13081 if (actual_atts == 0)
13082 appendPQExpBuffer(q, " (\n ");
13084 appendPQExpBuffer(q, ",\n ");
13086 appendPQExpBuffer(q, "CONSTRAINT %s ",
13087 fmtId(constr->dobj.name));
13088 appendPQExpBuffer(q, "%s", constr->condef);
13094 appendPQExpBuffer(q, "\n)");
13095 else if (!(tbinfo->reloftype && !binary_upgrade))
13098 * We must have a parenthesized attribute list, even though
13099 * empty, when not using the OF TYPE syntax.
13101 appendPQExpBuffer(q, " (\n)");
13104 if (numParents > 0 && !binary_upgrade)
13106 appendPQExpBuffer(q, "\nINHERITS (");
13107 for (k = 0; k < numParents; k++)
13109 TableInfo *parentRel = parents[k];
13112 appendPQExpBuffer(q, ", ");
13113 if (parentRel->dobj.namespace != tbinfo->dobj.namespace)
13114 appendPQExpBuffer(q, "%s.",
13115 fmtId(parentRel->dobj.namespace->dobj.name));
13116 appendPQExpBuffer(q, "%s",
13117 fmtId(parentRel->dobj.name));
13119 appendPQExpBuffer(q, ")");
13122 if (tbinfo->relkind == RELKIND_FOREIGN_TABLE)
13123 appendPQExpBuffer(q, "\nSERVER %s", fmtId(srvname));
13126 if ((tbinfo->reloptions && strlen(tbinfo->reloptions) > 0) ||
13127 (tbinfo->toast_reloptions && strlen(tbinfo->toast_reloptions) > 0))
13129 bool addcomma = false;
13131 appendPQExpBuffer(q, "\nWITH (");
13132 if (tbinfo->reloptions && strlen(tbinfo->reloptions) > 0)
13135 appendPQExpBuffer(q, "%s", tbinfo->reloptions);
13137 if (tbinfo->toast_reloptions && strlen(tbinfo->toast_reloptions) > 0)
13139 appendPQExpBuffer(q, "%s%s", addcomma ? ", " : "",
13140 tbinfo->toast_reloptions);
13142 appendPQExpBuffer(q, ")");
13145 /* Dump generic options if any */
13146 if (ftoptions && ftoptions[0])
13147 appendPQExpBuffer(q, "\nOPTIONS (\n %s\n)", ftoptions);
13150 * For materialized views, create the AS clause just like a view. At
13151 * this point, we always mark the view as not populated.
13153 if (tbinfo->relkind == RELKIND_MATVIEW)
13155 PQExpBuffer result;
13157 result = createViewAsClause(fout, tbinfo);
13158 appendPQExpBuffer(q, " AS\n%s\n WITH NO DATA;\n",
13160 destroyPQExpBuffer(result);
13163 appendPQExpBuffer(q, ";\n");
13166 * To create binary-compatible heap files, we have to ensure the same
13167 * physical column order, including dropped columns, as in the
13168 * original. Therefore, we create dropped columns above and drop them
13169 * here, also updating their attlen/attalign values so that the
13170 * dropped column can be skipped properly. (We do not bother with
13171 * restoring the original attbyval setting.) Also, inheritance
13172 * relationships are set up by doing ALTER INHERIT rather than using
13173 * an INHERITS clause --- the latter would possibly mess up the column
13174 * order. That also means we have to take care about setting
13175 * attislocal correctly, plus fix up any inherited CHECK constraints.
13176 * Analogously, we set up typed tables using ALTER TABLE / OF here.
13178 if (binary_upgrade && (tbinfo->relkind == RELKIND_RELATION ||
13179 tbinfo->relkind == RELKIND_FOREIGN_TABLE) )
13181 for (j = 0; j < tbinfo->numatts; j++)
13183 if (tbinfo->attisdropped[j])
13185 appendPQExpBuffer(q, "\n-- For binary upgrade, recreate dropped column.\n");
13186 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_attribute\n"
13187 "SET attlen = %d, "
13188 "attalign = '%c', attbyval = false\n"
13189 "WHERE attname = ",
13191 tbinfo->attalign[j]);
13192 appendStringLiteralAH(q, tbinfo->attnames[j], fout);
13193 appendPQExpBuffer(q, "\n AND attrelid = ");
13194 appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
13195 appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
13197 if (tbinfo->relkind == RELKIND_RELATION)
13198 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
13199 fmtId(tbinfo->dobj.name));
13201 appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
13202 fmtId(tbinfo->dobj.name));
13204 appendPQExpBuffer(q, "DROP COLUMN %s;\n",
13205 fmtId(tbinfo->attnames[j]));
13207 else if (!tbinfo->attislocal[j])
13209 Assert(tbinfo->relkind != RELKIND_FOREIGN_TABLE);
13210 appendPQExpBuffer(q, "\n-- For binary upgrade, recreate inherited column.\n");
13211 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_attribute\n"
13212 "SET attislocal = false\n"
13213 "WHERE attname = ");
13214 appendStringLiteralAH(q, tbinfo->attnames[j], fout);
13215 appendPQExpBuffer(q, "\n AND attrelid = ");
13216 appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
13217 appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
13221 for (k = 0; k < tbinfo->ncheck; k++)
13223 ConstraintInfo *constr = &(tbinfo->checkexprs[k]);
13225 if (constr->separate || constr->conislocal)
13228 appendPQExpBuffer(q, "\n-- For binary upgrade, set up inherited constraint.\n");
13229 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
13230 fmtId(tbinfo->dobj.name));
13231 appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
13232 fmtId(constr->dobj.name));
13233 appendPQExpBuffer(q, "%s;\n", constr->condef);
13234 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_constraint\n"
13235 "SET conislocal = false\n"
13236 "WHERE contype = 'c' AND conname = ");
13237 appendStringLiteralAH(q, constr->dobj.name, fout);
13238 appendPQExpBuffer(q, "\n AND conrelid = ");
13239 appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
13240 appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
13243 if (numParents > 0)
13245 appendPQExpBuffer(q, "\n-- For binary upgrade, set up inheritance this way.\n");
13246 for (k = 0; k < numParents; k++)
13248 TableInfo *parentRel = parents[k];
13250 appendPQExpBuffer(q, "ALTER TABLE ONLY %s INHERIT ",
13251 fmtId(tbinfo->dobj.name));
13252 if (parentRel->dobj.namespace != tbinfo->dobj.namespace)
13253 appendPQExpBuffer(q, "%s.",
13254 fmtId(parentRel->dobj.namespace->dobj.name));
13255 appendPQExpBuffer(q, "%s;\n",
13256 fmtId(parentRel->dobj.name));
13260 if (tbinfo->reloftype)
13262 appendPQExpBuffer(q, "\n-- For binary upgrade, set up typed tables this way.\n");
13263 appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
13264 fmtId(tbinfo->dobj.name),
13265 tbinfo->reloftype);
13268 appendPQExpBuffer(q, "\n-- For binary upgrade, set heap's relfrozenxid\n");
13269 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
13270 "SET relfrozenxid = '%u'\n"
13272 tbinfo->frozenxid);
13273 appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
13274 appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
13276 if (tbinfo->toast_oid)
13278 /* We preserve the toast oids, so we can use it during restore */
13279 appendPQExpBuffer(q, "\n-- For binary upgrade, set toast's relfrozenxid\n");
13280 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
13281 "SET relfrozenxid = '%u'\n"
13282 "WHERE oid = '%u';\n",
13283 tbinfo->toast_frozenxid, tbinfo->toast_oid);
13288 * In binary_upgrade mode, restore matviews' populated status by
13289 * poking pg_class directly. This is pretty ugly, but we can't use
13290 * REFRESH MATERIALIZED VIEW since it's possible that some underlying
13291 * matview is not populated even though this matview is.
13293 if (binary_upgrade && tbinfo->relkind == RELKIND_MATVIEW &&
13294 tbinfo->relispopulated)
13296 appendPQExpBuffer(q, "\n-- For binary upgrade, mark materialized view as populated\n");
13297 appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
13298 "SET relispopulated = 't'\n"
13300 appendStringLiteralAH(q, fmtId(tbinfo->dobj.name), fout);
13301 appendPQExpBuffer(q, "::pg_catalog.regclass;\n");
13305 * Dump additional per-column properties that we can't handle in the
13306 * main CREATE TABLE command.
13308 for (j = 0; j < tbinfo->numatts; j++)
13310 /* None of this applies to dropped columns */
13311 if (tbinfo->attisdropped[j])
13315 * If we didn't dump the column definition explicitly above, and
13316 * it is NOT NULL and did not inherit that property from a parent,
13317 * we have to mark it separately.
13319 if (!shouldPrintColumn(tbinfo, j) &&
13320 tbinfo->notnull[j] && !tbinfo->inhNotNull[j])
13322 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
13323 fmtId(tbinfo->dobj.name));
13324 appendPQExpBuffer(q, "ALTER COLUMN %s SET NOT NULL;\n",
13325 fmtId(tbinfo->attnames[j]));
13329 * Dump per-column statistics information. We only issue an ALTER
13330 * TABLE statement if the attstattarget entry for this column is
13331 * non-negative (i.e. it's not the default value)
13333 if (tbinfo->attstattarget[j] >= 0)
13335 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
13336 fmtId(tbinfo->dobj.name));
13337 appendPQExpBuffer(q, "ALTER COLUMN %s ",
13338 fmtId(tbinfo->attnames[j]));
13339 appendPQExpBuffer(q, "SET STATISTICS %d;\n",
13340 tbinfo->attstattarget[j]);
13344 * Dump per-column storage information. The statement is only
13345 * dumped if the storage has been changed from the type's default.
13347 if (tbinfo->attstorage[j] != tbinfo->typstorage[j])
13349 switch (tbinfo->attstorage[j])
13355 storage = "EXTERNAL";
13361 storage = "EXTENDED";
13368 * Only dump the statement if it's a storage type we recognize
13370 if (storage != NULL)
13372 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
13373 fmtId(tbinfo->dobj.name));
13374 appendPQExpBuffer(q, "ALTER COLUMN %s ",
13375 fmtId(tbinfo->attnames[j]));
13376 appendPQExpBuffer(q, "SET STORAGE %s;\n",
13382 * Dump per-column attributes.
13384 if (tbinfo->attoptions[j] && tbinfo->attoptions[j][0] != '\0')
13386 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
13387 fmtId(tbinfo->dobj.name));
13388 appendPQExpBuffer(q, "ALTER COLUMN %s ",
13389 fmtId(tbinfo->attnames[j]));
13390 appendPQExpBuffer(q, "SET (%s);\n",
13391 tbinfo->attoptions[j]);
13395 * Dump per-column fdw options.
13397 if (tbinfo->relkind == RELKIND_FOREIGN_TABLE &&
13398 tbinfo->attfdwoptions[j] &&
13399 tbinfo->attfdwoptions[j][0] != '\0')
13401 appendPQExpBuffer(q, "ALTER FOREIGN TABLE %s ",
13402 fmtId(tbinfo->dobj.name));
13403 appendPQExpBuffer(q, "ALTER COLUMN %s ",
13404 fmtId(tbinfo->attnames[j]));
13405 appendPQExpBuffer(q, "OPTIONS (\n %s\n);\n",
13406 tbinfo->attfdwoptions[j]);
13411 if (binary_upgrade)
13412 binary_upgrade_extension_member(q, &tbinfo->dobj, labelq->data);
13414 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
13416 tbinfo->dobj.namespace->dobj.name,
13417 (tbinfo->relkind == RELKIND_VIEW) ? NULL : tbinfo->reltablespace,
13419 (strcmp(reltypename, "TABLE") == 0) ? tbinfo->hasoids : false,
13420 reltypename, SECTION_PRE_DATA,
13421 q->data, delq->data, NULL,
13426 /* Dump Table Comments */
13427 dumpTableComment(fout, tbinfo, reltypename);
13429 /* Dump Table Security Labels */
13430 dumpTableSecLabel(fout, tbinfo, reltypename);
13432 /* Dump comments on inlined table constraints */
13433 for (j = 0; j < tbinfo->ncheck; j++)
13435 ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
13437 if (constr->separate || !constr->conislocal)
13440 dumpTableConstraintComment(fout, constr);
13443 destroyPQExpBuffer(q);
13444 destroyPQExpBuffer(delq);
13445 destroyPQExpBuffer(labelq);
13449 * dumpAttrDef --- dump an attribute's default-value declaration
13452 dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
13454 TableInfo *tbinfo = adinfo->adtable;
13455 int adnum = adinfo->adnum;
13459 /* Skip if table definition not to be dumped */
13460 if (!tbinfo->dobj.dump || dataOnly)
13463 /* Skip if not "separate"; it was dumped in the table's definition */
13464 if (!adinfo->separate)
13467 q = createPQExpBuffer();
13468 delq = createPQExpBuffer();
13470 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
13471 fmtId(tbinfo->dobj.name));
13472 appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
13473 fmtId(tbinfo->attnames[adnum - 1]),
13474 adinfo->adef_expr);
13477 * DROP must be fully qualified in case same name appears in pg_catalog
13479 appendPQExpBuffer(delq, "ALTER TABLE %s.",
13480 fmtId(tbinfo->dobj.namespace->dobj.name));
13481 appendPQExpBuffer(delq, "%s ",
13482 fmtId(tbinfo->dobj.name));
13483 appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
13484 fmtId(tbinfo->attnames[adnum - 1]));
13486 ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
13487 tbinfo->attnames[adnum - 1],
13488 tbinfo->dobj.namespace->dobj.name,
13491 false, "DEFAULT", SECTION_PRE_DATA,
13492 q->data, delq->data, NULL,
13496 destroyPQExpBuffer(q);
13497 destroyPQExpBuffer(delq);
13501 * getAttrName: extract the correct name for an attribute
13503 * The array tblInfo->attnames[] only provides names of user attributes;
13504 * if a system attribute number is supplied, we have to fake it.
13505 * We also do a little bit of bounds checking for safety's sake.
13507 static const char *
13508 getAttrName(int attrnum, TableInfo *tblInfo)
13510 if (attrnum > 0 && attrnum <= tblInfo->numatts)
13511 return tblInfo->attnames[attrnum - 1];
13514 case SelfItemPointerAttributeNumber:
13516 case ObjectIdAttributeNumber:
13518 case MinTransactionIdAttributeNumber:
13520 case MinCommandIdAttributeNumber:
13522 case MaxTransactionIdAttributeNumber:
13524 case MaxCommandIdAttributeNumber:
13526 case TableOidAttributeNumber:
13529 exit_horribly(NULL, "invalid column number %d for table \"%s\"\n",
13530 attrnum, tblInfo->dobj.name);
13531 return NULL; /* keep compiler quiet */
13536 * write out to fout a user-defined index
13539 dumpIndex(Archive *fout, IndxInfo *indxinfo)
13541 TableInfo *tbinfo = indxinfo->indextable;
13542 bool is_constraint = (indxinfo->indexconstraint != 0);
13545 PQExpBuffer labelq;
13550 q = createPQExpBuffer();
13551 delq = createPQExpBuffer();
13552 labelq = createPQExpBuffer();
13554 appendPQExpBuffer(labelq, "INDEX %s",
13555 fmtId(indxinfo->dobj.name));
13558 * If there's an associated constraint, don't dump the index per se, but
13559 * do dump any comment for it. (This is safe because dependency ordering
13560 * will have ensured the constraint is emitted first.) Note that the
13561 * emitted comment has to be shown as depending on the constraint, not
13562 * the index, in such cases.
13564 if (!is_constraint)
13566 if (binary_upgrade)
13567 binary_upgrade_set_pg_class_oids(fout, q,
13568 indxinfo->dobj.catId.oid, true);
13570 /* Plain secondary index */
13571 appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
13573 /* If the index is clustered, we need to record that. */
13574 if (indxinfo->indisclustered)
13576 appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
13577 fmtId(tbinfo->dobj.name));
13578 appendPQExpBuffer(q, " ON %s;\n",
13579 fmtId(indxinfo->dobj.name));
13583 * DROP must be fully qualified in case same name appears in
13586 appendPQExpBuffer(delq, "DROP INDEX %s.",
13587 fmtId(tbinfo->dobj.namespace->dobj.name));
13588 appendPQExpBuffer(delq, "%s;\n",
13589 fmtId(indxinfo->dobj.name));
13591 ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
13592 indxinfo->dobj.name,
13593 tbinfo->dobj.namespace->dobj.name,
13594 indxinfo->tablespace,
13595 tbinfo->rolname, false,
13596 "INDEX", SECTION_POST_DATA,
13597 q->data, delq->data, NULL,
13602 /* Dump Index Comments */
13603 dumpComment(fout, labelq->data,
13604 tbinfo->dobj.namespace->dobj.name,
13606 indxinfo->dobj.catId, 0,
13607 is_constraint ? indxinfo->indexconstraint :
13608 indxinfo->dobj.dumpId);
13610 destroyPQExpBuffer(q);
13611 destroyPQExpBuffer(delq);
13612 destroyPQExpBuffer(labelq);
13617 * write out to fout a user-defined constraint
13620 dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
13622 TableInfo *tbinfo = coninfo->contable;
13626 /* Skip if not to be dumped */
13627 if (!coninfo->dobj.dump || dataOnly)
13630 q = createPQExpBuffer();
13631 delq = createPQExpBuffer();
13633 if (coninfo->contype == 'p' ||
13634 coninfo->contype == 'u' ||
13635 coninfo->contype == 'x')
13637 /* Index-related constraint */
13638 IndxInfo *indxinfo;
13641 indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
13643 if (indxinfo == NULL)
13644 exit_horribly(NULL, "missing index for constraint \"%s\"\n",
13645 coninfo->dobj.name);
13647 if (binary_upgrade)
13648 binary_upgrade_set_pg_class_oids(fout, q,
13649 indxinfo->dobj.catId.oid, true);
13651 appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
13652 fmtId(tbinfo->dobj.name));
13653 appendPQExpBuffer(q, " ADD CONSTRAINT %s ",
13654 fmtId(coninfo->dobj.name));
13656 if (coninfo->condef)
13658 /* pg_get_constraintdef should have provided everything */
13659 appendPQExpBuffer(q, "%s;\n", coninfo->condef);
13663 appendPQExpBuffer(q, "%s (",
13664 coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
13665 for (k = 0; k < indxinfo->indnkeys; k++)
13667 int indkey = (int) indxinfo->indkeys[k];
13668 const char *attname;
13670 if (indkey == InvalidAttrNumber)
13672 attname = getAttrName(indkey, tbinfo);
13674 appendPQExpBuffer(q, "%s%s",
13675 (k == 0) ? "" : ", ",
13679 appendPQExpBuffer(q, ")");
13681 if (indxinfo->options && strlen(indxinfo->options) > 0)
13682 appendPQExpBuffer(q, " WITH (%s)", indxinfo->options);
13684 if (coninfo->condeferrable)
13686 appendPQExpBuffer(q, " DEFERRABLE");
13687 if (coninfo->condeferred)
13688 appendPQExpBuffer(q, " INITIALLY DEFERRED");
13691 appendPQExpBuffer(q, ";\n");
13694 /* If the index is clustered, we need to record that. */
13695 if (indxinfo->indisclustered)
13697 appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
13698 fmtId(tbinfo->dobj.name));
13699 appendPQExpBuffer(q, " ON %s;\n",
13700 fmtId(indxinfo->dobj.name));
13704 * DROP must be fully qualified in case same name appears in
13707 appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
13708 fmtId(tbinfo->dobj.namespace->dobj.name));
13709 appendPQExpBuffer(delq, "%s ",
13710 fmtId(tbinfo->dobj.name));
13711 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
13712 fmtId(coninfo->dobj.name));
13714 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
13715 coninfo->dobj.name,
13716 tbinfo->dobj.namespace->dobj.name,
13717 indxinfo->tablespace,
13718 tbinfo->rolname, false,
13719 "CONSTRAINT", SECTION_POST_DATA,
13720 q->data, delq->data, NULL,
13724 else if (coninfo->contype == 'f')
13727 * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that the
13728 * current table data is not processed
13730 appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
13731 fmtId(tbinfo->dobj.name));
13732 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
13733 fmtId(coninfo->dobj.name),
13737 * DROP must be fully qualified in case same name appears in
13740 appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
13741 fmtId(tbinfo->dobj.namespace->dobj.name));
13742 appendPQExpBuffer(delq, "%s ",
13743 fmtId(tbinfo->dobj.name));
13744 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
13745 fmtId(coninfo->dobj.name));
13747 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
13748 coninfo->dobj.name,
13749 tbinfo->dobj.namespace->dobj.name,
13751 tbinfo->rolname, false,
13752 "FK CONSTRAINT", SECTION_POST_DATA,
13753 q->data, delq->data, NULL,
13757 else if (coninfo->contype == 'c' && tbinfo)
13759 /* CHECK constraint on a table */
13761 /* Ignore if not to be dumped separately */
13762 if (coninfo->separate)
13764 /* not ONLY since we want it to propagate to children */
13765 appendPQExpBuffer(q, "ALTER TABLE %s\n",
13766 fmtId(tbinfo->dobj.name));
13767 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
13768 fmtId(coninfo->dobj.name),
13772 * DROP must be fully qualified in case same name appears in
13775 appendPQExpBuffer(delq, "ALTER TABLE %s.",
13776 fmtId(tbinfo->dobj.namespace->dobj.name));
13777 appendPQExpBuffer(delq, "%s ",
13778 fmtId(tbinfo->dobj.name));
13779 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
13780 fmtId(coninfo->dobj.name));
13782 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
13783 coninfo->dobj.name,
13784 tbinfo->dobj.namespace->dobj.name,
13786 tbinfo->rolname, false,
13787 "CHECK CONSTRAINT", SECTION_POST_DATA,
13788 q->data, delq->data, NULL,
13793 else if (coninfo->contype == 'c' && tbinfo == NULL)
13795 /* CHECK constraint on a domain */
13796 TypeInfo *tyinfo = coninfo->condomain;
13798 /* Ignore if not to be dumped separately */
13799 if (coninfo->separate)
13801 appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
13802 fmtId(tyinfo->dobj.name));
13803 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
13804 fmtId(coninfo->dobj.name),
13808 * DROP must be fully qualified in case same name appears in
13811 appendPQExpBuffer(delq, "ALTER DOMAIN %s.",
13812 fmtId(tyinfo->dobj.namespace->dobj.name));
13813 appendPQExpBuffer(delq, "%s ",
13814 fmtId(tyinfo->dobj.name));
13815 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
13816 fmtId(coninfo->dobj.name));
13818 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
13819 coninfo->dobj.name,
13820 tyinfo->dobj.namespace->dobj.name,
13822 tyinfo->rolname, false,
13823 "CHECK CONSTRAINT", SECTION_POST_DATA,
13824 q->data, delq->data, NULL,
13831 exit_horribly(NULL, "unrecognized constraint type: %c\n",
13835 /* Dump Constraint Comments --- only works for table constraints */
13836 if (tbinfo && coninfo->separate)
13837 dumpTableConstraintComment(fout, coninfo);
13839 destroyPQExpBuffer(q);
13840 destroyPQExpBuffer(delq);
13844 * dumpTableConstraintComment --- dump a constraint's comment if any
13846 * This is split out because we need the function in two different places
13847 * depending on whether the constraint is dumped as part of CREATE TABLE
13848 * or as a separate ALTER command.
13851 dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo)
13853 TableInfo *tbinfo = coninfo->contable;
13854 PQExpBuffer labelq = createPQExpBuffer();
13856 appendPQExpBuffer(labelq, "CONSTRAINT %s ",
13857 fmtId(coninfo->dobj.name));
13858 appendPQExpBuffer(labelq, "ON %s",
13859 fmtId(tbinfo->dobj.name));
13860 dumpComment(fout, labelq->data,
13861 tbinfo->dobj.namespace->dobj.name,
13863 coninfo->dobj.catId, 0,
13864 coninfo->separate ? coninfo->dobj.dumpId : tbinfo->dobj.dumpId);
13866 destroyPQExpBuffer(labelq);
13870 * findLastBuiltInOid -
13871 * find the last built in oid
13873 * For 7.1 and 7.2, we do this by retrieving datlastsysoid from the
13874 * pg_database entry for the current database
13877 findLastBuiltinOid_V71(Archive *fout, const char *dbname)
13881 PQExpBuffer query = createPQExpBuffer();
13883 resetPQExpBuffer(query);
13884 appendPQExpBuffer(query, "SELECT datlastsysoid from pg_database where datname = ");
13885 appendStringLiteralAH(query, dbname, fout);
13887 res = ExecuteSqlQueryForSingleRow(fout, query->data);
13888 last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
13890 destroyPQExpBuffer(query);
13895 * findLastBuiltInOid -
13896 * find the last built in oid
13898 * For 7.0, we do this by assuming that the last thing that initdb does is to
13899 * create the pg_indexes view. This sucks in general, but seeing that 7.0.x
13900 * initdb won't be changing anymore, it'll do.
13903 findLastBuiltinOid_V70(Archive *fout)
13908 res = ExecuteSqlQueryForSingleRow(fout,
13909 "SELECT oid FROM pg_class WHERE relname = 'pg_indexes'");
13910 last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
13917 * write the declaration (not data) of one user-defined sequence
13920 dumpSequence(Archive *fout, TableInfo *tbinfo)
13931 PQExpBuffer query = createPQExpBuffer();
13932 PQExpBuffer delqry = createPQExpBuffer();
13933 PQExpBuffer labelq = createPQExpBuffer();
13935 /* Make sure we are in proper schema */
13936 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
13938 snprintf(bufm, sizeof(bufm), INT64_FORMAT, SEQ_MINVALUE);
13939 snprintf(bufx, sizeof(bufx), INT64_FORMAT, SEQ_MAXVALUE);
13941 if (fout->remoteVersion >= 80400)
13943 appendPQExpBuffer(query,
13944 "SELECT sequence_name, "
13945 "start_value, increment_by, "
13946 "CASE WHEN increment_by > 0 AND max_value = %s THEN NULL "
13947 " WHEN increment_by < 0 AND max_value = -1 THEN NULL "
13949 "END AS max_value, "
13950 "CASE WHEN increment_by > 0 AND min_value = 1 THEN NULL "
13951 " WHEN increment_by < 0 AND min_value = %s THEN NULL "
13953 "END AS min_value, "
13954 "cache_value, is_cycled FROM %s",
13956 fmtId(tbinfo->dobj.name));
13960 appendPQExpBuffer(query,
13961 "SELECT sequence_name, "
13962 "0 AS start_value, increment_by, "
13963 "CASE WHEN increment_by > 0 AND max_value = %s THEN NULL "
13964 " WHEN increment_by < 0 AND max_value = -1 THEN NULL "
13966 "END AS max_value, "
13967 "CASE WHEN increment_by > 0 AND min_value = 1 THEN NULL "
13968 " WHEN increment_by < 0 AND min_value = %s THEN NULL "
13970 "END AS min_value, "
13971 "cache_value, is_cycled FROM %s",
13973 fmtId(tbinfo->dobj.name));
13976 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
13978 if (PQntuples(res) != 1)
13980 write_msg(NULL, ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)\n",
13981 "query to get data of sequence \"%s\" returned %d rows (expected 1)\n",
13983 tbinfo->dobj.name, PQntuples(res));
13987 /* Disable this check: it fails if sequence has been renamed */
13989 if (strcmp(PQgetvalue(res, 0, 0), tbinfo->dobj.name) != 0)
13991 write_msg(NULL, "query to get data of sequence \"%s\" returned name \"%s\"\n",
13992 tbinfo->dobj.name, PQgetvalue(res, 0, 0));
13997 startv = PQgetvalue(res, 0, 1);
13998 incby = PQgetvalue(res, 0, 2);
13999 if (!PQgetisnull(res, 0, 3))
14000 maxv = PQgetvalue(res, 0, 3);
14001 if (!PQgetisnull(res, 0, 4))
14002 minv = PQgetvalue(res, 0, 4);
14003 cache = PQgetvalue(res, 0, 5);
14004 cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
14007 * DROP must be fully qualified in case same name appears in pg_catalog
14009 appendPQExpBuffer(delqry, "DROP SEQUENCE %s.",
14010 fmtId(tbinfo->dobj.namespace->dobj.name));
14011 appendPQExpBuffer(delqry, "%s;\n",
14012 fmtId(tbinfo->dobj.name));
14014 resetPQExpBuffer(query);
14016 if (binary_upgrade)
14018 binary_upgrade_set_pg_class_oids(fout, query,
14019 tbinfo->dobj.catId.oid, false);
14020 binary_upgrade_set_type_oids_by_rel_oid(fout, query,
14021 tbinfo->dobj.catId.oid);
14024 appendPQExpBuffer(query,
14025 "CREATE SEQUENCE %s\n",
14026 fmtId(tbinfo->dobj.name));
14028 if (fout->remoteVersion >= 80400)
14029 appendPQExpBuffer(query, " START WITH %s\n", startv);
14031 appendPQExpBuffer(query, " INCREMENT BY %s\n", incby);
14034 appendPQExpBuffer(query, " MINVALUE %s\n", minv);
14036 appendPQExpBuffer(query, " NO MINVALUE\n");
14039 appendPQExpBuffer(query, " MAXVALUE %s\n", maxv);
14041 appendPQExpBuffer(query, " NO MAXVALUE\n");
14043 appendPQExpBuffer(query,
14045 cache, (cycled ? "\n CYCLE" : ""));
14047 appendPQExpBuffer(query, ";\n");
14049 appendPQExpBuffer(labelq, "SEQUENCE %s", fmtId(tbinfo->dobj.name));
14051 /* binary_upgrade: no need to clear TOAST table oid */
14053 if (binary_upgrade)
14054 binary_upgrade_extension_member(query, &tbinfo->dobj,
14057 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
14059 tbinfo->dobj.namespace->dobj.name,
14062 false, "SEQUENCE", SECTION_PRE_DATA,
14063 query->data, delqry->data, NULL,
14068 * If the sequence is owned by a table column, emit the ALTER for it as a
14069 * separate TOC entry immediately following the sequence's own entry. It's
14070 * OK to do this rather than using full sorting logic, because the
14071 * dependency that tells us it's owned will have forced the table to be
14072 * created first. We can't just include the ALTER in the TOC entry
14073 * because it will fail if we haven't reassigned the sequence owner to
14074 * match the table's owner.
14076 * We need not schema-qualify the table reference because both sequence
14077 * and table must be in the same schema.
14079 if (OidIsValid(tbinfo->owning_tab))
14081 TableInfo *owning_tab = findTableByOid(tbinfo->owning_tab);
14083 if (owning_tab && owning_tab->dobj.dump)
14085 resetPQExpBuffer(query);
14086 appendPQExpBuffer(query, "ALTER SEQUENCE %s",
14087 fmtId(tbinfo->dobj.name));
14088 appendPQExpBuffer(query, " OWNED BY %s",
14089 fmtId(owning_tab->dobj.name));
14090 appendPQExpBuffer(query, ".%s;\n",
14091 fmtId(owning_tab->attnames[tbinfo->owning_col - 1]));
14093 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14095 tbinfo->dobj.namespace->dobj.name,
14098 false, "SEQUENCE OWNED BY", SECTION_PRE_DATA,
14099 query->data, "", NULL,
14100 &(tbinfo->dobj.dumpId), 1,
14105 /* Dump Sequence Comments and Security Labels */
14106 dumpComment(fout, labelq->data,
14107 tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
14108 tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
14109 dumpSecLabel(fout, labelq->data,
14110 tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
14111 tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
14115 destroyPQExpBuffer(query);
14116 destroyPQExpBuffer(delqry);
14117 destroyPQExpBuffer(labelq);
14122 * write the data of one user-defined sequence
14125 dumpSequenceData(Archive *fout, TableDataInfo *tdinfo)
14127 TableInfo *tbinfo = tdinfo->tdtable;
14131 PQExpBuffer query = createPQExpBuffer();
14133 /* Make sure we are in proper schema */
14134 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
14136 appendPQExpBuffer(query,
14137 "SELECT last_value, is_called FROM %s",
14138 fmtId(tbinfo->dobj.name));
14140 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14142 if (PQntuples(res) != 1)
14144 write_msg(NULL, ngettext("query to get data of sequence \"%s\" returned %d row (expected 1)\n",
14145 "query to get data of sequence \"%s\" returned %d rows (expected 1)\n",
14147 tbinfo->dobj.name, PQntuples(res));
14151 last = PQgetvalue(res, 0, 0);
14152 called = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
14154 resetPQExpBuffer(query);
14155 appendPQExpBuffer(query, "SELECT pg_catalog.setval(");
14156 appendStringLiteralAH(query, fmtId(tbinfo->dobj.name), fout);
14157 appendPQExpBuffer(query, ", %s, %s);\n",
14158 last, (called ? "true" : "false"));
14160 ArchiveEntry(fout, nilCatalogId, createDumpId(),
14162 tbinfo->dobj.namespace->dobj.name,
14165 false, "SEQUENCE SET", SECTION_DATA,
14166 query->data, "", NULL,
14167 &(tbinfo->dobj.dumpId), 1,
14172 destroyPQExpBuffer(query);
14176 dumpTrigger(Archive *fout, TriggerInfo *tginfo)
14178 TableInfo *tbinfo = tginfo->tgtable;
14180 PQExpBuffer delqry;
14181 PQExpBuffer labelq;
14190 query = createPQExpBuffer();
14191 delqry = createPQExpBuffer();
14192 labelq = createPQExpBuffer();
14195 * DROP must be fully qualified in case same name appears in pg_catalog
14197 appendPQExpBuffer(delqry, "DROP TRIGGER %s ",
14198 fmtId(tginfo->dobj.name));
14199 appendPQExpBuffer(delqry, "ON %s.",
14200 fmtId(tbinfo->dobj.namespace->dobj.name));
14201 appendPQExpBuffer(delqry, "%s;\n",
14202 fmtId(tbinfo->dobj.name));
14206 appendPQExpBuffer(query, "%s;\n", tginfo->tgdef);
14210 if (tginfo->tgisconstraint)
14212 appendPQExpBuffer(query, "CREATE CONSTRAINT TRIGGER ");
14213 appendPQExpBufferStr(query, fmtId(tginfo->tgconstrname));
14217 appendPQExpBuffer(query, "CREATE TRIGGER ");
14218 appendPQExpBufferStr(query, fmtId(tginfo->dobj.name));
14220 appendPQExpBuffer(query, "\n ");
14223 if (TRIGGER_FOR_BEFORE(tginfo->tgtype))
14224 appendPQExpBuffer(query, "BEFORE");
14225 else if (TRIGGER_FOR_AFTER(tginfo->tgtype))
14226 appendPQExpBuffer(query, "AFTER");
14227 else if (TRIGGER_FOR_INSTEAD(tginfo->tgtype))
14228 appendPQExpBuffer(query, "INSTEAD OF");
14231 write_msg(NULL, "unexpected tgtype value: %d\n", tginfo->tgtype);
14236 if (TRIGGER_FOR_INSERT(tginfo->tgtype))
14238 appendPQExpBuffer(query, " INSERT");
14241 if (TRIGGER_FOR_DELETE(tginfo->tgtype))
14244 appendPQExpBuffer(query, " OR DELETE");
14246 appendPQExpBuffer(query, " DELETE");
14249 if (TRIGGER_FOR_UPDATE(tginfo->tgtype))
14252 appendPQExpBuffer(query, " OR UPDATE");
14254 appendPQExpBuffer(query, " UPDATE");
14257 if (TRIGGER_FOR_TRUNCATE(tginfo->tgtype))
14260 appendPQExpBuffer(query, " OR TRUNCATE");
14262 appendPQExpBuffer(query, " TRUNCATE");
14265 appendPQExpBuffer(query, " ON %s\n",
14266 fmtId(tbinfo->dobj.name));
14268 if (tginfo->tgisconstraint)
14270 if (OidIsValid(tginfo->tgconstrrelid))
14272 /* If we are using regclass, name is already quoted */
14273 if (fout->remoteVersion >= 70300)
14274 appendPQExpBuffer(query, " FROM %s\n ",
14275 tginfo->tgconstrrelname);
14277 appendPQExpBuffer(query, " FROM %s\n ",
14278 fmtId(tginfo->tgconstrrelname));
14280 if (!tginfo->tgdeferrable)
14281 appendPQExpBuffer(query, "NOT ");
14282 appendPQExpBuffer(query, "DEFERRABLE INITIALLY ");
14283 if (tginfo->tginitdeferred)
14284 appendPQExpBuffer(query, "DEFERRED\n");
14286 appendPQExpBuffer(query, "IMMEDIATE\n");
14289 if (TRIGGER_FOR_ROW(tginfo->tgtype))
14290 appendPQExpBuffer(query, " FOR EACH ROW\n ");
14292 appendPQExpBuffer(query, " FOR EACH STATEMENT\n ");
14294 /* In 7.3, result of regproc is already quoted */
14295 if (fout->remoteVersion >= 70300)
14296 appendPQExpBuffer(query, "EXECUTE PROCEDURE %s(",
14299 appendPQExpBuffer(query, "EXECUTE PROCEDURE %s(",
14300 fmtId(tginfo->tgfname));
14302 tgargs = (char *) PQunescapeBytea((unsigned char *) tginfo->tgargs,
14305 for (findx = 0; findx < tginfo->tgnargs; findx++)
14307 /* find the embedded null that terminates this trigger argument */
14308 size_t tlen = strlen(p);
14310 if (p + tlen >= tgargs + lentgargs)
14312 /* hm, not found before end of bytea value... */
14313 write_msg(NULL, "invalid argument string (%s) for trigger \"%s\" on table \"%s\"\n",
14316 tbinfo->dobj.name);
14321 appendPQExpBuffer(query, ", ");
14322 appendStringLiteralAH(query, p, fout);
14326 appendPQExpBuffer(query, ");\n");
14329 if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
14331 appendPQExpBuffer(query, "\nALTER TABLE %s ",
14332 fmtId(tbinfo->dobj.name));
14333 switch (tginfo->tgenabled)
14337 appendPQExpBuffer(query, "DISABLE");
14340 appendPQExpBuffer(query, "ENABLE ALWAYS");
14343 appendPQExpBuffer(query, "ENABLE REPLICA");
14346 appendPQExpBuffer(query, "ENABLE");
14349 appendPQExpBuffer(query, " TRIGGER %s;\n",
14350 fmtId(tginfo->dobj.name));
14353 appendPQExpBuffer(labelq, "TRIGGER %s ",
14354 fmtId(tginfo->dobj.name));
14355 appendPQExpBuffer(labelq, "ON %s",
14356 fmtId(tbinfo->dobj.name));
14358 ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
14360 tbinfo->dobj.namespace->dobj.name,
14362 tbinfo->rolname, false,
14363 "TRIGGER", SECTION_POST_DATA,
14364 query->data, delqry->data, NULL,
14368 dumpComment(fout, labelq->data,
14369 tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
14370 tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
14372 destroyPQExpBuffer(query);
14373 destroyPQExpBuffer(delqry);
14374 destroyPQExpBuffer(labelq);
14378 dumpEventTrigger(Archive *fout, EventTriggerInfo *evtinfo)
14381 PQExpBuffer labelq;
14383 query = createPQExpBuffer();
14384 labelq = createPQExpBuffer();
14386 appendPQExpBuffer(query, "CREATE EVENT TRIGGER ");
14387 appendPQExpBufferStr(query, fmtId(evtinfo->dobj.name));
14388 appendPQExpBuffer(query, " ON ");
14389 appendPQExpBufferStr(query, fmtId(evtinfo->evtevent));
14390 appendPQExpBufferStr(query, " ");
14392 if (strcmp("", evtinfo->evttags) != 0)
14394 appendPQExpBufferStr(query, "\n WHEN TAG IN (");
14395 appendPQExpBufferStr(query, evtinfo->evttags);
14396 appendPQExpBufferStr(query, ") ");
14399 appendPQExpBuffer(query, "\n EXECUTE PROCEDURE ");
14400 appendPQExpBufferStr(query, evtinfo->evtfname);
14401 appendPQExpBuffer(query, "();\n");
14403 if (evtinfo->evtenabled != 'O')
14405 appendPQExpBuffer(query, "\nALTER EVENT TRIGGER %s ",
14406 fmtId(evtinfo->dobj.name));
14407 switch (evtinfo->evtenabled)
14410 appendPQExpBuffer(query, "DISABLE");
14413 appendPQExpBuffer(query, "ENABLE ALWAYS");
14416 appendPQExpBuffer(query, "ENABLE REPLICA");
14419 appendPQExpBuffer(query, "ENABLE");
14422 appendPQExpBuffer(query, ";\n");
14424 appendPQExpBuffer(labelq, "EVENT TRIGGER %s ",
14425 fmtId(evtinfo->dobj.name));
14427 ArchiveEntry(fout, evtinfo->dobj.catId, evtinfo->dobj.dumpId,
14428 evtinfo->dobj.name, NULL, NULL, evtinfo->evtowner, false,
14429 "EVENT TRIGGER", SECTION_POST_DATA,
14430 query->data, "", NULL, NULL, 0, NULL, NULL);
14432 dumpComment(fout, labelq->data,
14434 evtinfo->dobj.catId, 0, evtinfo->dobj.dumpId);
14436 destroyPQExpBuffer(query);
14437 destroyPQExpBuffer(labelq);
14445 dumpRule(Archive *fout, RuleInfo *rinfo)
14447 TableInfo *tbinfo = rinfo->ruletable;
14450 PQExpBuffer delcmd;
14451 PQExpBuffer labelq;
14454 /* Skip if not to be dumped */
14455 if (!rinfo->dobj.dump || dataOnly)
14459 * If it is an ON SELECT rule that is created implicitly by CREATE VIEW,
14460 * we do not want to dump it as a separate object.
14462 if (!rinfo->separate)
14466 * Make sure we are in proper schema.
14468 selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name);
14470 query = createPQExpBuffer();
14471 cmd = createPQExpBuffer();
14472 delcmd = createPQExpBuffer();
14473 labelq = createPQExpBuffer();
14475 if (fout->remoteVersion >= 70300)
14477 appendPQExpBuffer(query,
14478 "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid) AS definition",
14479 rinfo->dobj.catId.oid);
14483 /* Rule name was unique before 7.3 ... */
14484 appendPQExpBuffer(query,
14485 "SELECT pg_get_ruledef('%s') AS definition",
14489 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14491 if (PQntuples(res) != 1)
14493 write_msg(NULL, "query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned\n",
14494 rinfo->dobj.name, tbinfo->dobj.name);
14498 printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
14501 * Add the command to alter the rules replication firing semantics if it
14502 * differs from the default.
14504 if (rinfo->ev_enabled != 'O')
14506 appendPQExpBuffer(cmd, "ALTER TABLE %s ", fmtId(tbinfo->dobj.name));
14507 switch (rinfo->ev_enabled)
14510 appendPQExpBuffer(cmd, "ENABLE ALWAYS RULE %s;\n",
14511 fmtId(rinfo->dobj.name));
14514 appendPQExpBuffer(cmd, "ENABLE REPLICA RULE %s;\n",
14515 fmtId(rinfo->dobj.name));
14518 appendPQExpBuffer(cmd, "DISABLE RULE %s;\n",
14519 fmtId(rinfo->dobj.name));
14525 * Apply view's reloptions when its ON SELECT rule is separate.
14527 if (rinfo->reloptions && strlen(rinfo->reloptions) > 0)
14529 appendPQExpBuffer(cmd, "ALTER VIEW %s SET (%s);\n",
14530 fmtId(tbinfo->dobj.name),
14531 rinfo->reloptions);
14535 * DROP must be fully qualified in case same name appears in pg_catalog
14537 appendPQExpBuffer(delcmd, "DROP RULE %s ",
14538 fmtId(rinfo->dobj.name));
14539 appendPQExpBuffer(delcmd, "ON %s.",
14540 fmtId(tbinfo->dobj.namespace->dobj.name));
14541 appendPQExpBuffer(delcmd, "%s;\n",
14542 fmtId(tbinfo->dobj.name));
14544 appendPQExpBuffer(labelq, "RULE %s",
14545 fmtId(rinfo->dobj.name));
14546 appendPQExpBuffer(labelq, " ON %s",
14547 fmtId(tbinfo->dobj.name));
14549 ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
14551 tbinfo->dobj.namespace->dobj.name,
14553 tbinfo->rolname, false,
14554 "RULE", SECTION_POST_DATA,
14555 cmd->data, delcmd->data, NULL,
14559 /* Dump rule comments */
14560 dumpComment(fout, labelq->data,
14561 tbinfo->dobj.namespace->dobj.name,
14563 rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
14567 destroyPQExpBuffer(query);
14568 destroyPQExpBuffer(cmd);
14569 destroyPQExpBuffer(delcmd);
14570 destroyPQExpBuffer(labelq);
14574 * getExtensionMembership --- obtain extension membership data
14577 getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
14588 DumpableObject *dobj,
14591 /* Nothing to do if no extensions */
14592 if (numExtensions == 0)
14595 /* Make sure we are in proper schema */
14596 selectSourceSchema(fout, "pg_catalog");
14598 query = createPQExpBuffer();
14600 /* refclassid constraint is redundant but may speed the search */
14601 appendPQExpBuffer(query, "SELECT "
14602 "classid, objid, refclassid, refobjid "
14604 "WHERE refclassid = 'pg_extension'::regclass "
14605 "AND deptype = 'e' "
14608 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14610 ntups = PQntuples(res);
14612 i_classid = PQfnumber(res, "classid");
14613 i_objid = PQfnumber(res, "objid");
14614 i_refclassid = PQfnumber(res, "refclassid");
14615 i_refobjid = PQfnumber(res, "refobjid");
14618 * Since we ordered the SELECT by referenced ID, we can expect that
14619 * multiple entries for the same extension will appear together; this
14620 * saves on searches.
14624 for (i = 0; i < ntups; i++)
14627 CatalogId refobjId;
14629 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
14630 objId.oid = atooid(PQgetvalue(res, i, i_objid));
14631 refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
14632 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
14634 if (refdobj == NULL ||
14635 refdobj->catId.tableoid != refobjId.tableoid ||
14636 refdobj->catId.oid != refobjId.oid)
14637 refdobj = findObjectByCatalogId(refobjId);
14640 * Failure to find objects mentioned in pg_depend is not unexpected,
14641 * since for example we don't collect info about TOAST tables.
14643 if (refdobj == NULL)
14646 fprintf(stderr, "no referenced object %u %u\n",
14647 refobjId.tableoid, refobjId.oid);
14652 dobj = findObjectByCatalogId(objId);
14657 fprintf(stderr, "no referencing object %u %u\n",
14658 objId.tableoid, objId.oid);
14663 /* Record dependency so that getDependencies needn't repeat this */
14664 addObjectDependency(dobj, refdobj->dumpId);
14666 dobj->ext_member = true;
14669 * Normally, mark the member object as not to be dumped. But in
14670 * binary upgrades, we still dump the members individually, since the
14671 * idea is to exactly reproduce the database contents rather than
14672 * replace the extension contents with something different.
14674 if (!binary_upgrade)
14675 dobj->dump = false;
14677 dobj->dump = refdobj->dump;
14683 * Now identify extension configuration tables and create TableDataInfo
14684 * objects for them, ensuring their data will be dumped even though the
14685 * tables themselves won't be.
14687 * Note that we create TableDataInfo objects even in schemaOnly mode, ie,
14688 * user data in a configuration table is treated like schema data. This
14689 * seems appropriate since system data in a config table would get
14690 * reloaded by CREATE EXTENSION.
14692 for (i = 0; i < numExtensions; i++)
14694 ExtensionInfo *curext = &(extinfo[i]);
14695 char *extconfig = curext->extconfig;
14696 char *extcondition = curext->extcondition;
14697 char **extconfigarray = NULL;
14698 char **extconditionarray = NULL;
14700 int nconditionitems;
14702 if (parsePGArray(extconfig, &extconfigarray, &nconfigitems) &&
14703 parsePGArray(extcondition, &extconditionarray, &nconditionitems) &&
14704 nconfigitems == nconditionitems)
14708 for (j = 0; j < nconfigitems; j++)
14710 TableInfo *configtbl;
14711 Oid configtbloid = atooid(extconfigarray[j]);
14712 bool dumpobj = curext->dobj.dump;
14714 configtbl = findTableByOid(configtbloid);
14715 if (configtbl == NULL)
14719 * Tables of not-to-be-dumped extensions shouldn't be dumped
14720 * unless the table or its schema is explicitly included
14722 if (!curext->dobj.dump)
14724 /* check table explicitly requested */
14725 if (table_include_oids.head != NULL &&
14726 simple_oid_list_member(&table_include_oids,
14730 /* check table's schema explicitly requested */
14731 if (configtbl->dobj.namespace->dobj.dump)
14735 /* check table excluded by an exclusion switch */
14736 if (table_exclude_oids.head != NULL &&
14737 simple_oid_list_member(&table_exclude_oids,
14741 /* check schema excluded by an exclusion switch */
14742 if (simple_oid_list_member(&schema_exclude_oids,
14743 configtbl->dobj.namespace->dobj.catId.oid))
14749 * Note: config tables are dumped without OIDs regardless
14750 * of the --oids setting. This is because row filtering
14751 * conditions aren't compatible with dumping OIDs.
14753 makeTableDataInfo(configtbl, false);
14754 if (configtbl->dataObj != NULL)
14756 if (strlen(extconditionarray[j]) > 0)
14757 configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
14762 if (extconfigarray)
14763 free(extconfigarray);
14764 if (extconditionarray)
14765 free(extconditionarray);
14768 destroyPQExpBuffer(query);
14772 * getDependencies --- obtain available dependency data
14775 getDependencies(Archive *fout)
14786 DumpableObject *dobj,
14789 /* No dependency info available before 7.3 */
14790 if (fout->remoteVersion < 70300)
14794 write_msg(NULL, "reading dependency data\n");
14796 /* Make sure we are in proper schema */
14797 selectSourceSchema(fout, "pg_catalog");
14799 query = createPQExpBuffer();
14802 * PIN dependencies aren't interesting, and EXTENSION dependencies were
14803 * already processed by getExtensionMembership.
14805 appendPQExpBuffer(query, "SELECT "
14806 "classid, objid, refclassid, refobjid, deptype "
14808 "WHERE deptype != 'p' AND deptype != 'e' "
14811 res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
14813 ntups = PQntuples(res);
14815 i_classid = PQfnumber(res, "classid");
14816 i_objid = PQfnumber(res, "objid");
14817 i_refclassid = PQfnumber(res, "refclassid");
14818 i_refobjid = PQfnumber(res, "refobjid");
14819 i_deptype = PQfnumber(res, "deptype");
14822 * Since we ordered the SELECT by referencing ID, we can expect that
14823 * multiple entries for the same object will appear together; this saves
14828 for (i = 0; i < ntups; i++)
14831 CatalogId refobjId;
14834 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
14835 objId.oid = atooid(PQgetvalue(res, i, i_objid));
14836 refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
14837 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
14838 deptype = *(PQgetvalue(res, i, i_deptype));
14840 if (dobj == NULL ||
14841 dobj->catId.tableoid != objId.tableoid ||
14842 dobj->catId.oid != objId.oid)
14843 dobj = findObjectByCatalogId(objId);
14846 * Failure to find objects mentioned in pg_depend is not unexpected,
14847 * since for example we don't collect info about TOAST tables.
14852 fprintf(stderr, "no referencing object %u %u\n",
14853 objId.tableoid, objId.oid);
14858 refdobj = findObjectByCatalogId(refobjId);
14860 if (refdobj == NULL)
14863 fprintf(stderr, "no referenced object %u %u\n",
14864 refobjId.tableoid, refobjId.oid);
14870 * Ordinarily, table rowtypes have implicit dependencies on their
14871 * tables. However, for a composite type the implicit dependency goes
14872 * the other way in pg_depend; which is the right thing for DROP but
14873 * it doesn't produce the dependency ordering we need. So in that one
14874 * case, we reverse the direction of the dependency.
14876 if (deptype == 'i' &&
14877 dobj->objType == DO_TABLE &&
14878 refdobj->objType == DO_TYPE)
14879 addObjectDependency(refdobj, dobj->dumpId);
14882 addObjectDependency(dobj, refdobj->dumpId);
14887 destroyPQExpBuffer(query);
14892 * createBoundaryObjects - create dummy DumpableObjects to represent
14893 * dump section boundaries.
14895 static DumpableObject *
14896 createBoundaryObjects(void)
14898 DumpableObject *dobjs;
14900 dobjs = (DumpableObject *) pg_malloc(2 * sizeof(DumpableObject));
14902 dobjs[0].objType = DO_PRE_DATA_BOUNDARY;
14903 dobjs[0].catId = nilCatalogId;
14904 AssignDumpId(dobjs + 0);
14905 dobjs[0].name = pg_strdup("PRE-DATA BOUNDARY");
14907 dobjs[1].objType = DO_POST_DATA_BOUNDARY;
14908 dobjs[1].catId = nilCatalogId;
14909 AssignDumpId(dobjs + 1);
14910 dobjs[1].name = pg_strdup("POST-DATA BOUNDARY");
14916 * addBoundaryDependencies - add dependencies as needed to enforce the dump
14917 * section boundaries.
14920 addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
14921 DumpableObject *boundaryObjs)
14923 DumpableObject *preDataBound = boundaryObjs + 0;
14924 DumpableObject *postDataBound = boundaryObjs + 1;
14927 for (i = 0; i < numObjs; i++)
14929 DumpableObject *dobj = dobjs[i];
14932 * The classification of object types here must match the SECTION_xxx
14933 * values assigned during subsequent ArchiveEntry calls!
14935 switch (dobj->objType)
14940 case DO_SHELL_TYPE:
14947 case DO_CONVERSION:
14952 case DO_DUMMY_TYPE:
14955 case DO_TSTEMPLATE:
14958 case DO_FOREIGN_SERVER:
14960 /* Pre-data objects: must come before the pre-data boundary */
14961 addObjectDependency(preDataBound, dobj->dumpId);
14963 case DO_TABLE_DATA:
14965 /* Data objects: must come between the boundaries */
14966 addObjectDependency(dobj, preDataBound->dumpId);
14967 addObjectDependency(postDataBound, dobj->dumpId);
14970 case DO_REFRESH_MATVIEW:
14972 case DO_EVENT_TRIGGER:
14973 case DO_DEFAULT_ACL:
14974 /* Post-data objects: must come after the post-data boundary */
14975 addObjectDependency(dobj, postDataBound->dumpId);
14978 /* Rules are post-data, but only if dumped separately */
14979 if (((RuleInfo *) dobj)->separate)
14980 addObjectDependency(dobj, postDataBound->dumpId);
14982 case DO_CONSTRAINT:
14983 case DO_FK_CONSTRAINT:
14984 /* Constraints are post-data, but only if dumped separately */
14985 if (((ConstraintInfo *) dobj)->separate)
14986 addObjectDependency(dobj, postDataBound->dumpId);
14988 case DO_PRE_DATA_BOUNDARY:
14989 /* nothing to do */
14991 case DO_POST_DATA_BOUNDARY:
14992 /* must come after the pre-data boundary */
14993 addObjectDependency(dobj, preDataBound->dumpId);
15001 * BuildArchiveDependencies - create dependency data for archive TOC entries
15003 * The raw dependency data obtained by getDependencies() is not terribly
15004 * useful in an archive dump, because in many cases there are dependency
15005 * chains linking through objects that don't appear explicitly in the dump.
15006 * For example, a view will depend on its _RETURN rule while the _RETURN rule
15007 * will depend on other objects --- but the rule will not appear as a separate
15008 * object in the dump. We need to adjust the view's dependencies to include
15009 * whatever the rule depends on that is included in the dump.
15011 * Just to make things more complicated, there are also "special" dependencies
15012 * such as the dependency of a TABLE DATA item on its TABLE, which we must
15013 * not rearrange because pg_restore knows that TABLE DATA only depends on
15014 * its table. In these cases we must leave the dependencies strictly as-is
15015 * even if they refer to not-to-be-dumped objects.
15017 * To handle this, the convention is that "special" dependencies are created
15018 * during ArchiveEntry calls, and an archive TOC item that has any such
15019 * entries will not be touched here. Otherwise, we recursively search the
15020 * DumpableObject data structures to build the correct dependencies for each
15021 * archive TOC item.
15024 BuildArchiveDependencies(Archive *fout)
15026 ArchiveHandle *AH = (ArchiveHandle *) fout;
15029 /* Scan all TOC entries in the archive */
15030 for (te = AH->toc->next; te != AH->toc; te = te->next)
15032 DumpableObject *dobj;
15033 DumpId *dependencies;
15037 /* No need to process entries that will not be dumped */
15040 /* Ignore entries that already have "special" dependencies */
15043 /* Otherwise, look up the item's original DumpableObject, if any */
15044 dobj = findObjectByDumpId(te->dumpId);
15047 /* No work if it has no dependencies */
15048 if (dobj->nDeps <= 0)
15050 /* Set up work array */
15052 dependencies = (DumpId *) pg_malloc(allocDeps * sizeof(DumpId));
15054 /* Recursively find all dumpable dependencies */
15055 findDumpableDependencies(AH, dobj,
15056 &dependencies, &nDeps, &allocDeps);
15057 /* And save 'em ... */
15060 dependencies = (DumpId *) pg_realloc(dependencies,
15061 nDeps * sizeof(DumpId));
15062 te->dependencies = dependencies;
15066 free(dependencies);
15070 /* Recursive search subroutine for BuildArchiveDependencies */
15072 findDumpableDependencies(ArchiveHandle *AH, DumpableObject *dobj,
15073 DumpId **dependencies, int *nDeps, int *allocDeps)
15078 * Ignore section boundary objects: if we search through them, we'll
15079 * report lots of bogus dependencies.
15081 if (dobj->objType == DO_PRE_DATA_BOUNDARY ||
15082 dobj->objType == DO_POST_DATA_BOUNDARY)
15085 for (i = 0; i < dobj->nDeps; i++)
15087 DumpId depid = dobj->dependencies[i];
15089 if (TocIDRequired(AH, depid) != 0)
15091 /* Object will be dumped, so just reference it as a dependency */
15092 if (*nDeps >= *allocDeps)
15095 *dependencies = (DumpId *) pg_realloc(*dependencies,
15096 *allocDeps * sizeof(DumpId));
15098 (*dependencies)[*nDeps] = depid;
15104 * Object will not be dumped, so recursively consider its deps. We
15105 * rely on the assumption that sortDumpableObjects already broke
15106 * any dependency loops, else we might recurse infinitely.
15108 DumpableObject *otherdobj = findObjectByDumpId(depid);
15111 findDumpableDependencies(AH, otherdobj,
15112 dependencies, nDeps, allocDeps);
15119 * selectSourceSchema - make the specified schema the active search path
15120 * in the source database.
15122 * NB: pg_catalog is explicitly searched after the specified schema;
15123 * so user names are only qualified if they are cross-schema references,
15124 * and system names are only qualified if they conflict with a user name
15125 * in the current schema.
15127 * Whenever the selected schema is not pg_catalog, be careful to qualify
15128 * references to system catalogs and types in our emitted commands!
15130 * This function is called only from selectSourceSchemaOnAH and
15131 * selectSourceSchema.
15134 selectSourceSchema(Archive *fout, const char *schemaName)
15138 /* This is checked by the callers already */
15139 Assert(schemaName != NULL && *schemaName != '\0');
15141 /* Not relevant if fetching from pre-7.3 DB */
15142 if (fout->remoteVersion < 70300)
15145 query = createPQExpBuffer();
15146 appendPQExpBuffer(query, "SET search_path = %s",
15147 fmtId(schemaName));
15148 if (strcmp(schemaName, "pg_catalog") != 0)
15149 appendPQExpBuffer(query, ", pg_catalog");
15151 ExecuteSqlStatement(fout, query->data);
15153 destroyPQExpBuffer(query);
15157 * getFormattedTypeName - retrieve a nicely-formatted type name for the
15160 * NB: in 7.3 and up the result may depend on the currently-selected
15161 * schema; this is why we don't try to cache the names.
15164 getFormattedTypeName(Archive *fout, Oid oid, OidOptions opts)
15172 if ((opts & zeroAsOpaque) != 0)
15173 return pg_strdup(g_opaque_type);
15174 else if ((opts & zeroAsAny) != 0)
15175 return pg_strdup("'any'");
15176 else if ((opts & zeroAsStar) != 0)
15177 return pg_strdup("*");
15178 else if ((opts & zeroAsNone) != 0)
15179 return pg_strdup("NONE");
15182 query = createPQExpBuffer();
15183 if (fout->remoteVersion >= 70300)
15185 appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
15188 else if (fout->remoteVersion >= 70100)
15190 appendPQExpBuffer(query, "SELECT format_type('%u'::oid, NULL)",
15195 appendPQExpBuffer(query, "SELECT typname "
15197 "WHERE oid = '%u'::oid",
15201 res = ExecuteSqlQueryForSingleRow(fout, query->data);
15203 if (fout->remoteVersion >= 70100)
15205 /* already quoted */
15206 result = pg_strdup(PQgetvalue(res, 0, 0));
15210 /* may need to quote it */
15211 result = pg_strdup(fmtId(PQgetvalue(res, 0, 0)));
15215 destroyPQExpBuffer(query);
15221 * myFormatType --- local implementation of format_type for use with 7.0.
15224 myFormatType(const char *typname, int32 typmod)
15227 bool isarray = false;
15228 PQExpBuffer buf = createPQExpBuffer();
15230 /* Handle array types */
15231 if (typname[0] == '_')
15237 /* Show lengths on bpchar and varchar */
15238 if (strcmp(typname, "bpchar") == 0)
15240 int len = (typmod - VARHDRSZ);
15242 appendPQExpBuffer(buf, "character");
15244 appendPQExpBuffer(buf, "(%d)",
15245 typmod - VARHDRSZ);
15247 else if (strcmp(typname, "varchar") == 0)
15249 appendPQExpBuffer(buf, "character varying");
15251 appendPQExpBuffer(buf, "(%d)",
15252 typmod - VARHDRSZ);
15254 else if (strcmp(typname, "numeric") == 0)
15256 appendPQExpBuffer(buf, "numeric");
15263 tmp_typmod = typmod - VARHDRSZ;
15264 precision = (tmp_typmod >> 16) & 0xffff;
15265 scale = tmp_typmod & 0xffff;
15266 appendPQExpBuffer(buf, "(%d,%d)",
15272 * char is an internal single-byte data type; Let's make sure we force it
15273 * through with quotes. - thomas 1998-12-13
15275 else if (strcmp(typname, "char") == 0)
15276 appendPQExpBuffer(buf, "\"char\"");
15278 appendPQExpBuffer(buf, "%s", fmtId(typname));
15280 /* Append array qualifier for array types */
15282 appendPQExpBuffer(buf, "[]");
15284 result = pg_strdup(buf->data);
15285 destroyPQExpBuffer(buf);
15291 * Return a column list clause for the given relation.
15293 * Special case: if there are no undropped columns in the relation, return
15294 * "", not an invalid "()" column list.
15296 static const char *
15297 fmtCopyColumnList(const TableInfo *ti, PQExpBuffer buffer)
15299 int numatts = ti->numatts;
15300 char **attnames = ti->attnames;
15301 bool *attisdropped = ti->attisdropped;
15305 appendPQExpBuffer(buffer, "(");
15307 for (i = 0; i < numatts; i++)
15309 if (attisdropped[i])
15312 appendPQExpBuffer(buffer, ", ");
15313 appendPQExpBuffer(buffer, "%s", fmtId(attnames[i]));
15318 return ""; /* no undropped columns */
15320 appendPQExpBuffer(buffer, ")");
15321 return buffer->data;
15325 * Execute an SQL query and verify that we got exactly one row back.
15328 ExecuteSqlQueryForSingleRow(Archive *fout, char *query)
15333 res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
15335 /* Expecting a single result only */
15336 ntups = PQntuples(res);
15338 exit_horribly(NULL,
15339 ngettext("query returned %d row instead of one: %s\n",
15340 "query returned %d rows instead of one: %s\n",