1 /*-------------------------------------------------------------------------
4 * pg_dump is a utility for dumping out a postgres database
7 * Portions Copyright (c) 1996-2003, 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
15 * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.362 2004/01/07 00:44:21 tgl Exp $
17 *-------------------------------------------------------------------------
21 * Although this is not a backend module, we must include postgres.h anyway
22 * so that we can include a bunch of backend include files. pg_dump has
23 * never pretended to be very independent of the backend anyhow ...
40 #include "getopt_long.h"
46 #include "access/attnum.h"
47 #include "access/htup.h"
48 #include "catalog/pg_class.h"
49 #include "catalog/pg_proc.h"
50 #include "catalog/pg_trigger.h"
51 #include "catalog/pg_type.h"
53 #include "commands/sequence.h"
56 #include "libpq/libpq-fs.h"
59 #include "pg_backup.h"
60 #include "pg_backup_archiver.h"
61 #include "dumputils.h"
63 #define _(x) gettext((x))
71 bool g_verbose; /* User wants verbose narration of our
73 Archive *g_fout; /* the script file */
74 PGconn *g_conn; /* the database connection */
76 /* various user-settable parameters */
77 bool dumpInserts; /* dump data using proper insert strings */
78 bool attrNames; /* put attr names into insert strings */
83 /* obsolete as of 7.3: */
84 static Oid g_last_builtin_oid; /* value of the last builtin oid */
86 static char *selectTableName = NULL; /* name of a single table to dump */
87 static char *selectSchemaName = NULL; /* name of a single schema to dump */
89 char g_opaque_type[10]; /* name for the opaque type */
91 /* placeholders for the delimiters for comments */
92 char g_comment_start[10];
93 char g_comment_end[10];
95 static const CatalogId nilCatalogId = { 0, 0 };
97 /* these are to avoid passing around info for findNamespace() */
98 static NamespaceInfo *g_namespaces;
99 static int g_numNamespaces;
102 static void help(const char *progname);
103 static NamespaceInfo *findNamespace(Oid nsoid, Oid objoid);
104 static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);
105 static void dumpComment(Archive *fout, const char *target,
106 const char *namespace, const char *owner,
107 CatalogId catalogId, int subid, DumpId dumpId);
108 static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);
109 static void dumpNamespace(Archive *fout, NamespaceInfo *nspinfo);
110 static void dumpType(Archive *fout, TypeInfo *tinfo);
111 static void dumpBaseType(Archive *fout, TypeInfo *tinfo);
112 static void dumpDomain(Archive *fout, TypeInfo *tinfo);
113 static void dumpCompositeType(Archive *fout, TypeInfo *tinfo);
114 static void dumpProcLang(Archive *fout, ProcLangInfo *plang);
115 static void dumpFunc(Archive *fout, FuncInfo *finfo);
116 static void dumpCast(Archive *fout, CastInfo *cast);
117 static void dumpOpr(Archive *fout, OprInfo *oprinfo);
118 static void dumpOpclass(Archive *fout, OpclassInfo *opcinfo);
119 static void dumpConversion(Archive *fout, ConvInfo *convinfo);
120 static void dumpRule(Archive *fout, RuleInfo *rinfo);
121 static void dumpAgg(Archive *fout, AggInfo *agginfo);
122 static void dumpTrigger(Archive *fout, TriggerInfo *tginfo);
123 static void dumpTable(Archive *fout, TableInfo *tbinfo);
124 static void dumpTableSchema(Archive *fout, TableInfo *tbinfo);
125 static void dumpAttrDef(Archive *fout, AttrDefInfo *adinfo);
126 static void dumpSequence(Archive *fout, TableInfo *tbinfo);
127 static void dumpIndex(Archive *fout, IndxInfo *indxinfo);
128 static void dumpConstraint(Archive *fout, ConstraintInfo *coninfo);
130 static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
131 const char *type, const char *name,
132 const char *tag, const char *nspname, const char *owner,
135 static void getDependencies(void);
136 static void getDomainConstraints(TypeInfo *tinfo);
137 static void getTableData(TableInfo *tblinfo, int numTables, bool oids);
138 static char *format_function_signature(FuncInfo *finfo, char **argnames,
140 static const char *convertRegProcReference(const char *proc);
141 static const char *convertOperatorReference(const char *opr);
142 static Oid findLastBuiltinOid_V71(const char *);
143 static Oid findLastBuiltinOid_V70(void);
144 static void setMaxOid(Archive *fout);
145 static void selectSourceSchema(const char *schemaName);
146 static char *getFormattedTypeName(Oid oid, OidOptions opts);
147 static char *myFormatType(const char *typname, int32 typmod);
148 static const char *fmtQualifiedId(const char *schema, const char *id);
149 static int dumpBlobs(Archive *AH, void *arg);
150 static void dumpDatabase(Archive *AH);
151 static const char *getAttrName(int attrnum, TableInfo *tblInfo);
152 static const char *fmtCopyColumnList(const TableInfo *ti);
153 static void do_sql_command(PGconn *conn, const char *query);
154 static void check_sql_result(PGresult *res, PGconn *conn, const char *query,
155 ExecStatusType expected);
159 main(int argc, char **argv)
162 const char *filename = NULL;
163 const char *format = "p";
164 const char *dbname = NULL;
165 const char *pghost = NULL;
166 const char *pgport = NULL;
167 const char *username = NULL;
171 DumpableObject **dobjs;
174 bool force_password = false;
175 int compressLevel = -1;
176 bool ignore_version = false;
179 int outputCreate = 0;
181 int outputNoOwner = 0;
182 static int use_setsessauth = 0;
183 static int disable_triggers = 0;
184 char *outputSuperuser = NULL;
186 RestoreOptions *ropt;
188 static struct option long_options[] = {
189 {"data-only", no_argument, NULL, 'a'},
190 {"blobs", no_argument, NULL, 'b'},
191 {"clean", no_argument, NULL, 'c'},
192 {"create", no_argument, NULL, 'C'},
193 {"file", required_argument, NULL, 'f'},
194 {"format", required_argument, NULL, 'F'},
195 {"inserts", no_argument, NULL, 'd'},
196 {"attribute-inserts", no_argument, NULL, 'D'},
197 {"column-inserts", no_argument, NULL, 'D'},
198 {"host", required_argument, NULL, 'h'},
199 {"ignore-version", no_argument, NULL, 'i'},
200 {"no-reconnect", no_argument, NULL, 'R'},
201 {"oids", no_argument, NULL, 'o'},
202 {"no-owner", no_argument, NULL, 'O'},
203 {"port", required_argument, NULL, 'p'},
204 {"schema", required_argument, NULL, 'n'},
205 {"schema-only", no_argument, NULL, 's'},
206 {"superuser", required_argument, NULL, 'S'},
207 {"table", required_argument, NULL, 't'},
208 {"password", no_argument, NULL, 'W'},
209 {"username", required_argument, NULL, 'U'},
210 {"verbose", no_argument, NULL, 'v'},
211 {"no-privileges", no_argument, NULL, 'x'},
212 {"no-acl", no_argument, NULL, 'x'},
213 {"compress", required_argument, NULL, 'Z'},
214 {"help", no_argument, NULL, '?'},
215 {"version", no_argument, NULL, 'V'},
218 * the following options don't have an equivalent short option
219 * letter, but are available as '-X long-name'
221 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
222 {"disable-triggers", no_argument, &disable_triggers, 1},
229 setlocale(LC_ALL, "");
230 bindtextdomain("pg_dump", LOCALEDIR);
231 textdomain("pg_dump");
236 strcpy(g_comment_start, "-- ");
237 g_comment_end[0] = '\0';
238 strcpy(g_opaque_type, "opaque");
240 dataOnly = schemaOnly = dumpInserts = attrNames = false;
242 progname = get_progname(argv[0]);
244 /* Set default options based on progname */
245 if (strcmp(progname, "pg_backup") == 0)
253 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
258 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
260 puts("pg_dump (PostgreSQL) " PG_VERSION);
265 while ((c = getopt_long(argc, argv, "abcCdDf:F:h:in:oOp:RsS:t:uU:vWxX:Z:",
266 long_options, &optindex)) != -1)
270 case 'a': /* Dump data only */
274 case 'b': /* Dump blobs */
278 case 'c': /* clean (i.e., drop) schema prior to
283 case 'C': /* Create DB */
287 case 'd': /* dump data as proper insert strings */
291 case 'D': /* dump data as proper insert strings with
305 case 'h': /* server host */
309 case 'i': /* ignore database version mismatch */
310 ignore_version = true;
313 case 'n': /* Dump data for this schema only */
314 selectSchemaName = strdup(optarg);
317 case 'o': /* Dump oids */
321 case 'O': /* Don't reconnect to match owner */
325 case 'p': /* server port */
330 /* no-op, still accepted for backwards compatibility */
333 case 's': /* dump schema only */
337 case 'S': /* Username for superuser in plain text
339 outputSuperuser = strdup(optarg);
342 case 't': /* Dump data for this table only */
343 selectTableName = strdup(optarg);
347 force_password = true;
348 username = simple_prompt("User name: ", 100, true);
355 case 'v': /* verbose */
360 force_password = true;
363 case 'x': /* skip ACL dump */
368 * Option letters were getting scarce, so I invented this
369 * new scheme: '-X feature' turns on some feature. Compare
370 * to the -f option in GCC. You should also add an
371 * equivalent GNU-style option --feature. Features that
372 * require arguments should use '-X feature=foo'.
375 if (strcmp(optarg, "use-set-session-authorization") == 0)
376 /* no-op, still allowed for compatibility */ ;
377 else if (strcmp(optarg, "disable-triggers") == 0)
378 disable_triggers = 1;
382 _("%s: invalid -X option -- %s\n"),
384 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
389 case 'Z': /* Compression Level */
390 compressLevel = atoi(optarg);
392 /* This covers the long options equivalent to -X xxx. */
398 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
403 if (optind < (argc - 1))
405 fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
406 progname, argv[optind + 1]);
407 fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
412 /* Get database name from command line */
414 dbname = argv[optind];
416 if (dataOnly && schemaOnly)
418 write_msg(NULL, "options \"schema only\" (-s) and \"data only\" (-a) cannot be used together\n");
422 if (dataOnly && outputClean)
424 write_msg(NULL, "options \"clean\" (-c) and \"data only\" (-a) cannot be used together\n");
428 if (outputBlobs && selectTableName != NULL)
430 write_msg(NULL, "large-object output not supported for a single table\n");
431 write_msg(NULL, "use a full dump instead\n");
435 if (outputBlobs && selectSchemaName != NULL)
437 write_msg(NULL, "large-object output not supported for a single schema\n");
438 write_msg(NULL, "use a full dump instead\n");
442 if (dumpInserts == true && oids == true)
444 write_msg(NULL, "INSERT (-d, -D) and OID (-o) options cannot be used together\n");
445 write_msg(NULL, "(The INSERT command cannot set OIDs.)\n");
449 if (outputBlobs == true && (format[0] == 'p' || format[0] == 'P'))
451 write_msg(NULL, "large-object output is not supported for plain-text dump files\n");
452 write_msg(NULL, "(Use a different output format.)\n");
456 /* open the output file */
461 g_fout = CreateArchive(filename, archCustom, compressLevel);
466 g_fout = CreateArchive(filename, archFiles, compressLevel);
472 g_fout = CreateArchive(filename, archNull, 0);
477 g_fout = CreateArchive(filename, archTar, compressLevel);
481 write_msg(NULL, "invalid output format \"%s\" specified\n", format);
487 write_msg(NULL, "could not open output file \"%s\" for writing\n", filename);
491 /* Let the archiver know how noisy to be */
492 g_fout->verbose = g_verbose;
494 g_fout->minRemoteVersion = 70000; /* we can handle back to 7.0 */
495 g_fout->maxRemoteVersion = parse_version(PG_VERSION);
496 if (g_fout->maxRemoteVersion < 0)
498 write_msg(NULL, "could not parse version string \"%s\"\n", PG_VERSION);
503 * Open the database using the Archiver, so it knows about it. Errors
506 g_conn = ConnectDatabase(g_fout, dbname, pghost, pgport,
507 username, force_password, ignore_version);
510 * Start serializable transaction to dump consistent data.
512 do_sql_command(g_conn, "BEGIN");
514 do_sql_command(g_conn, "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
516 /* Set the datestyle to ISO to ensure the dump's portability */
517 do_sql_command(g_conn, "SET DATESTYLE = ISO");
520 * If supported, set extra_float_digits so that we can dump float data
521 * exactly (given correctly implemented float I/O code, anyway)
523 if (g_fout->remoteVersion >= 70400)
524 do_sql_command(g_conn, "SET extra_float_digits TO 2");
526 /* Find the last built-in OID, if needed */
527 if (g_fout->remoteVersion < 70300)
529 if (g_fout->remoteVersion >= 70100)
530 g_last_builtin_oid = findLastBuiltinOid_V71(PQdb(g_conn));
532 g_last_builtin_oid = findLastBuiltinOid_V70();
534 write_msg(NULL, "last built-in OID is %u\n", g_last_builtin_oid);
538 * Now scan the database and create DumpableObject structs for all the
539 * objects we intend to dump.
541 tblinfo = getSchemaData(&numTables, schemaOnly, dataOnly);
544 getTableData(tblinfo, numTables, oids);
547 * Collect dependency data to assist in ordering the objects.
552 * Sort the objects into a safe dump order (no forward references).
554 getDumpableObjects(&dobjs, &numObjs);
556 sortDumpableObjectsByType(dobjs, numObjs);
557 sortDumpableObjects(dobjs, numObjs);
560 * Create archive TOC entries for all the objects to be dumped,
564 /* The database item is always first. */
566 dumpDatabase(g_fout);
568 /* Max OID is second. */
572 /* Now the rearrangeable objects. */
573 for (i = 0; i < numObjs; i++)
575 dumpDumpableObject(g_fout, dobjs[i]);
578 /* BLOBs are always last. */
580 ArchiveEntry(g_fout, nilCatalogId, createDumpId(),
582 "BLOBS", "", "", NULL,
587 * And finally we can do the actual output.
591 ropt = NewRestoreOptions();
592 ropt->filename = (char *) filename;
593 ropt->dropSchema = outputClean;
594 ropt->aclsSkip = aclsSkip;
595 ropt->superuser = outputSuperuser;
596 ropt->create = outputCreate;
597 ropt->noOwner = outputNoOwner;
598 ropt->disable_triggers = disable_triggers;
600 if (compressLevel == -1)
601 ropt->compression = 0;
603 ropt->compression = compressLevel;
605 ropt->suppressDumpWarnings = true; /* We've already shown
608 RestoreArchive(g_fout, ropt);
611 CloseArchive(g_fout);
620 help(const char *progname)
622 printf(_("%s dumps a database as a text file or to other formats.\n\n"), progname);
623 printf(_("Usage:\n"));
624 printf(_(" %s [OPTION]... [DBNAME]\n"), progname);
626 printf(_("\nGeneral options:\n"));
627 printf(_(" -f, --file=FILENAME output file name\n"));
628 printf(_(" -F, --format=c|t|p output file format (custom, tar, plain text)\n"));
629 printf(_(" -i, --ignore-version proceed even when server version mismatches\n"
630 " pg_dump version\n"));
631 printf(_(" -v, --verbose verbose mode\n"));
632 printf(_(" -Z, --compress=0-9 compression level for compressed formats\n"));
633 printf(_(" --help show this help, then exit\n"));
634 printf(_(" --version output version information, then exit\n"));
636 printf(_("\nOptions controlling the output content:\n"));
637 printf(_(" -a, --data-only dump only the data, not the schema\n"));
638 printf(_(" -b, --blobs include large objects in dump\n"));
639 printf(_(" -c, --clean clean (drop) schema prior to create\n"));
640 printf(_(" -C, --create include commands to create database in dump\n"));
641 printf(_(" -d, --inserts dump data as INSERT, rather than COPY, commands\n"));
642 printf(_(" -D, --column-inserts dump data as INSERT commands with column names\n"));
643 printf(_(" -n, --schema=SCHEMA dump the named schema only\n"));
644 printf(_(" -o, --oids include OIDs in dump\n"));
645 printf(_(" -O, --no-owner do not output commands to set object ownership\n"
646 " in plain text format\n"));
647 printf(_(" -s, --schema-only dump only the schema, no data\n"));
648 printf(_(" -S, --superuser=NAME specify the superuser user name to use in\n"
649 " plain text format\n"));
650 printf(_(" -t, --table=TABLE dump the named table only\n"));
651 printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
652 printf(_(" -X disable-triggers, --disable-triggers\n"
653 " disable triggers during data-only restore\n"));
655 printf(_("\nConnection options:\n"));
656 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
657 printf(_(" -p, --port=PORT database server port number\n"));
658 printf(_(" -U, --username=NAME connect as specified database user\n"));
659 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
661 printf(_("\nIf no database name is supplied, then the PGDATABASE environment\n"
662 "variable value is used.\n\n"));
663 printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
671 write_msg(NULL, "*** aborted because of error\n");
676 * selectDumpableNamespace: policy-setting subroutine
677 * Mark a namespace as to be dumped or not
680 selectDumpableNamespace(NamespaceInfo *nsinfo)
683 * If a specific table is being dumped, do not dump any complete
684 * namespaces. If a specific namespace is being dumped, dump just
685 * that namespace. Otherwise, dump all non-system namespaces.
687 if (selectTableName != NULL)
688 nsinfo->dump = false;
689 else if (selectSchemaName != NULL)
691 if (strcmp(nsinfo->nspname, selectSchemaName) == 0)
694 nsinfo->dump = false;
696 else if (strncmp(nsinfo->nspname, "pg_", 3) == 0 ||
697 strcmp(nsinfo->nspname, "information_schema") == 0)
698 nsinfo->dump = false;
704 * selectDumpableTable: policy-setting subroutine
705 * Mark a table as to be dumped or not
708 selectDumpableTable(TableInfo *tbinfo)
711 * Always dump if dumping parent namespace; else, if a particular
712 * tablename has been specified, dump matching table name; else, do
715 tbinfo->dump = false;
716 if (tbinfo->relnamespace->dump)
718 else if (selectTableName != NULL &&
719 strcmp(tbinfo->relname, selectTableName) == 0)
721 /* If both -s and -t specified, must match both to dump */
722 if (selectSchemaName == NULL)
724 else if (strcmp(tbinfo->relnamespace->nspname, selectSchemaName) == 0)
730 * Dump a table's contents for loading using the COPY command
731 * - this routine is called by the Archiver when it wants the table
735 #define COPYBUFSIZ 8192
738 dumpTableData_copy(Archive *fout, void *dcontext)
740 TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
741 TableInfo *tbinfo = tdinfo->tdtable;
742 const char *classname = tbinfo->relname;
743 const bool hasoids = tbinfo->hasoids;
744 const bool oids = tdinfo->oids;
745 PQExpBuffer q = createPQExpBuffer();
749 char copybuf[COPYBUFSIZ];
750 const char *column_list;
753 write_msg(NULL, "dumping contents of table %s\n", classname);
756 * Make sure we are in proper schema. We will qualify the table name
757 * below anyway (in case its name conflicts with a pg_catalog table);
758 * but this ensures reproducible results in case the table contains
759 * regproc, regclass, etc columns.
761 selectSourceSchema(tbinfo->relnamespace->nspname);
764 * If possible, specify the column list explicitly so that we have no
765 * possibility of retrieving data in the wrong column order. (The
766 * default column ordering of COPY will not be what we want in certain
767 * corner cases involving ADD COLUMN and inheritance.)
769 if (g_fout->remoteVersion >= 70300)
770 column_list = fmtCopyColumnList(tbinfo);
772 column_list = ""; /* can't select columns in COPY */
776 appendPQExpBuffer(q, "COPY %s %s WITH OIDS TO stdout;",
777 fmtQualifiedId(tbinfo->relnamespace->nspname,
783 appendPQExpBuffer(q, "COPY %s %s TO stdout;",
784 fmtQualifiedId(tbinfo->relnamespace->nspname,
788 res = PQexec(g_conn, q->data);
789 check_sql_result(res, g_conn, q->data, PGRES_COPY_OUT);
795 ret = PQgetline(g_conn, copybuf, COPYBUFSIZ);
797 if (copybuf[0] == '\\' &&
801 copydone = true; /* don't print this... */
805 archputs(copybuf, fout);
812 archputs("\n", fout);
822 * There was considerable discussion in late July, 2000 regarding
823 * slowing down pg_dump when backing up large tables. Users with
824 * both slow & fast (muti-processor) machines experienced
825 * performance degradation when doing a backup.
827 * Initial attempts based on sleeping for a number of ms for each ms
828 * of work were deemed too complex, then a simple 'sleep in each
829 * loop' implementation was suggested. The latter failed because
830 * the loop was too tight. Finally, the following was implemented:
832 * If throttle is non-zero, then See how long since the last sleep.
833 * Work out how long to sleep (based on ratio). If sleep is more
834 * than 100ms, then sleep reset timer EndIf EndIf
836 * where the throttle value was the number of ms to sleep per ms of
837 * work. The calculation was done in each loop.
839 * Most of the hard work is done in the backend, and this solution
840 * still did not work particularly well: on slow machines, the
841 * ratio was 50:1, and on medium paced machines, 1:1, and on fast
842 * multi-processor machines, it had little or no effect, for
843 * reasons that were unclear.
845 * Further discussion ensued, and the proposal was dropped.
847 * For those people who want this feature, it can be implemented
848 * using gettimeofday in each loop, calculating the time since
849 * last sleep, multiplying that by the sleep ratio, then if the
850 * result is more than a preset 'minimum sleep time' (say 100ms),
851 * call the 'select' function to sleep for a subsecond period ie.
853 * select(0, NULL, NULL, NULL, &tvi);
855 * This will return after the interval specified in the structure
856 * tvi. Finally, call gettimeofday again to save the 'last sleep
860 archprintf(fout, "\\.\n\n\n");
862 ret = PQendcopy(g_conn);
865 write_msg(NULL, "SQL command to dump the contents of table \"%s\" failed: PQendcopy() failed.\n", classname);
866 write_msg(NULL, "Error message from server: %s", PQerrorMessage(g_conn));
867 write_msg(NULL, "The command was: %s\n", q->data);
872 destroyPQExpBuffer(q);
877 dumpTableData_insert(Archive *fout, void *dcontext)
879 TableDataInfo *tdinfo = (TableDataInfo *) dcontext;
880 TableInfo *tbinfo = tdinfo->tdtable;
881 const char *classname = tbinfo->relname;
882 PQExpBuffer q = createPQExpBuffer();
889 * Make sure we are in proper schema. We will qualify the table name
890 * below anyway (in case its name conflicts with a pg_catalog table);
891 * but this ensures reproducible results in case the table contains
892 * regproc, regclass, etc columns.
894 selectSourceSchema(tbinfo->relnamespace->nspname);
896 if (fout->remoteVersion >= 70100)
898 appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
899 "SELECT * FROM ONLY %s",
900 fmtQualifiedId(tbinfo->relnamespace->nspname,
905 appendPQExpBuffer(q, "DECLARE _pg_dump_cursor CURSOR FOR "
907 fmtQualifiedId(tbinfo->relnamespace->nspname,
911 res = PQexec(g_conn, q->data);
912 check_sql_result(res, g_conn, q->data, PGRES_COMMAND_OK);
918 res = PQexec(g_conn, "FETCH 100 FROM _pg_dump_cursor");
919 check_sql_result(res, g_conn, "FETCH 100 FROM _pg_dump_cursor",
921 nfields = PQnfields(res);
922 for (tuple = 0; tuple < PQntuples(res); tuple++)
924 archprintf(fout, "INSERT INTO %s ", fmtId(classname));
927 /* corner case for zero-column table */
928 archprintf(fout, "DEFAULT VALUES;\n");
931 if (attrNames == true)
934 appendPQExpBuffer(q, "(");
935 for (field = 0; field < nfields; field++)
938 appendPQExpBuffer(q, ", ");
939 appendPQExpBuffer(q, fmtId(PQfname(res, field)));
941 appendPQExpBuffer(q, ") ");
942 archprintf(fout, "%s", q->data);
944 archprintf(fout, "VALUES (");
945 for (field = 0; field < nfields; field++)
948 archprintf(fout, ", ");
949 if (PQgetisnull(res, tuple, field))
951 archprintf(fout, "NULL");
955 /* XXX This code is partially duplicated in ruleutils.c */
956 switch (PQftype(res, field))
967 * These types are printed without quotes
968 * unless they contain values that aren't
969 * accepted by the scanner unquoted (e.g.,
970 * 'NaN'). Note that strtod() and friends
971 * might accept NaN, so we can't use that to
974 * In reality we only need to defend against
975 * infinity and NaN, so we need not get too
976 * crazy about pattern matching here.
978 const char *s = PQgetvalue(res, tuple, field);
980 if (strspn(s, "0123456789 +-eE.") == strlen(s))
981 archprintf(fout, "%s", s);
983 archprintf(fout, "'%s'", s);
989 archprintf(fout, "B'%s'",
990 PQgetvalue(res, tuple, field));
994 if (strcmp(PQgetvalue(res, tuple, field), "t") == 0)
995 archprintf(fout, "true");
997 archprintf(fout, "false");
1001 /* All other types are printed as string literals. */
1002 resetPQExpBuffer(q);
1003 appendStringLiteral(q, PQgetvalue(res, tuple, field), false);
1004 archprintf(fout, "%s", q->data);
1008 archprintf(fout, ");\n");
1010 } while (PQntuples(res) > 0);
1014 archprintf(fout, "\n\n");
1016 do_sql_command(g_conn, "CLOSE _pg_dump_cursor");
1018 destroyPQExpBuffer(q);
1025 * dump the contents of a single table
1027 * Actually, this just makes an ArchiveEntry for the table contents.
1030 dumpTableData(Archive *fout, TableDataInfo *tdinfo)
1032 TableInfo *tbinfo = tdinfo->tdtable;
1033 PQExpBuffer copyBuf = createPQExpBuffer();
1034 DataDumperPtr dumpFn;
1039 /* Dump/restore using COPY */
1040 dumpFn = dumpTableData_copy;
1041 /* must use 2 steps here 'cause fmtId is nonreentrant */
1042 appendPQExpBuffer(copyBuf, "COPY %s ",
1043 fmtId(tbinfo->relname));
1044 appendPQExpBuffer(copyBuf, "%s %sFROM stdin;\n",
1045 fmtCopyColumnList(tbinfo),
1046 (tdinfo->oids && tbinfo->hasoids) ? "WITH OIDS " : "");
1047 copyStmt = copyBuf->data;
1051 /* Restore using INSERT */
1052 dumpFn = dumpTableData_insert;
1056 ArchiveEntry(fout, tdinfo->dobj.catId, tdinfo->dobj.dumpId,
1058 tbinfo->relnamespace->nspname,
1060 "TABLE DATA", "", "", copyStmt,
1061 tdinfo->dobj.dependencies, tdinfo->dobj.nDeps,
1064 destroyPQExpBuffer(copyBuf);
1069 * set up dumpable objects representing the contents of tables
1072 getTableData(TableInfo *tblinfo, int numTables, bool oids)
1076 for (i = 0; i < numTables; i++)
1078 /* Skip VIEWs (no data to dump) */
1079 if (tblinfo[i].relkind == RELKIND_VIEW)
1081 /* Skip SEQUENCEs (handled elsewhere) */
1082 if (tblinfo[i].relkind == RELKIND_SEQUENCE)
1085 if (tblinfo[i].dump)
1087 TableDataInfo *tdinfo;
1089 tdinfo = (TableDataInfo *) malloc(sizeof(TableDataInfo));
1091 tdinfo->dobj.objType = DO_TABLE_DATA;
1093 * Note: use tableoid 0 so that this object won't be mistaken
1094 * for something that pg_depend entries apply to.
1096 tdinfo->dobj.catId.tableoid = 0;
1097 tdinfo->dobj.catId.oid = tblinfo[i].dobj.catId.oid;
1098 AssignDumpId(&tdinfo->dobj);
1099 tdinfo->tdtable = &(tblinfo[i]);
1100 tdinfo->oids = oids;
1101 addObjectDependency(&tdinfo->dobj, tblinfo[i].dobj.dumpId);
1109 * dump the database definition
1112 dumpDatabase(Archive *AH)
1114 PQExpBuffer dbQry = createPQExpBuffer();
1115 PQExpBuffer delQry = createPQExpBuffer();
1116 PQExpBuffer creaQry = createPQExpBuffer();
1126 const char *datname,
1131 datname = PQdb(g_conn);
1134 write_msg(NULL, "saving database definition\n");
1136 /* Make sure we are in proper schema */
1137 selectSourceSchema("pg_catalog");
1139 /* Get the database owner and parameters from pg_database */
1140 if (g_fout->remoteVersion >= 70100)
1142 appendPQExpBuffer(dbQry, "SELECT tableoid, oid, "
1143 "(SELECT usename FROM pg_user WHERE usesysid = datdba) as dba, "
1144 "pg_encoding_to_char(encoding) as encoding, "
1147 "WHERE datname = ");
1148 appendStringLiteral(dbQry, datname, true);
1152 appendPQExpBuffer(dbQry, "SELECT "
1153 "(SELECT oid FROM pg_class WHERE relname = 'pg_database') AS tableoid, "
1155 "(SELECT usename FROM pg_user WHERE usesysid = datdba) as dba, "
1156 "pg_encoding_to_char(encoding) as encoding, "
1159 "WHERE datname = ");
1160 appendStringLiteral(dbQry, datname, true);
1163 res = PQexec(g_conn, dbQry->data);
1164 check_sql_result(res, g_conn, dbQry->data, PGRES_TUPLES_OK);
1166 ntups = PQntuples(res);
1170 write_msg(NULL, "missing pg_database entry for database \"%s\"\n",
1177 write_msg(NULL, "query returned more than one (%d) pg_database entry for database \"%s\"\n",
1182 i_tableoid = PQfnumber(res, "tableoid");
1183 i_oid = PQfnumber(res, "oid");
1184 i_dba = PQfnumber(res, "dba");
1185 i_encoding = PQfnumber(res, "encoding");
1186 i_datpath = PQfnumber(res, "datpath");
1188 dbCatId.tableoid = atooid(PQgetvalue(res, 0, i_tableoid));
1189 dbCatId.oid = atooid(PQgetvalue(res, 0, i_oid));
1190 dba = PQgetvalue(res, 0, i_dba);
1191 encoding = PQgetvalue(res, 0, i_encoding);
1192 datpath = PQgetvalue(res, 0, i_datpath);
1194 appendPQExpBuffer(creaQry, "CREATE DATABASE %s WITH TEMPLATE = template0",
1196 if (strlen(datpath) > 0)
1198 appendPQExpBuffer(creaQry, " LOCATION = ");
1199 appendStringLiteral(creaQry, datpath, true);
1201 if (strlen(encoding) > 0)
1203 appendPQExpBuffer(creaQry, " ENCODING = ");
1204 appendStringLiteral(creaQry, encoding, true);
1206 appendPQExpBuffer(creaQry, ";\n");
1208 appendPQExpBuffer(delQry, "DROP DATABASE %s;\n",
1211 dbDumpId = createDumpId();
1214 dbCatId, /* catalog ID */
1215 dbDumpId, /* dump ID */
1217 NULL, /* Namespace */
1219 "DATABASE", /* Desc */
1220 creaQry->data, /* Create */
1221 delQry->data, /* Del */
1226 NULL); /* Dumper Arg */
1228 /* Dump DB comment if any */
1229 resetPQExpBuffer(dbQry);
1230 appendPQExpBuffer(dbQry, "DATABASE %s", fmtId(datname));
1231 dumpComment(AH, dbQry->data, NULL, "",
1232 dbCatId, 0, dbDumpId);
1236 destroyPQExpBuffer(dbQry);
1237 destroyPQExpBuffer(delQry);
1238 destroyPQExpBuffer(creaQry);
1248 #define loBufSize 16384
1249 #define loFetchSize 1000
1252 dumpBlobs(Archive *AH, void *arg)
1254 PQExpBuffer oidQry = createPQExpBuffer();
1255 PQExpBuffer oidFetchQry = createPQExpBuffer();
1259 char buf[loBufSize];
1264 write_msg(NULL, "saving large objects\n");
1266 /* Make sure we are in proper schema */
1267 selectSourceSchema("pg_catalog");
1269 /* Cursor to get all BLOB tables */
1270 if (AH->remoteVersion >= 70100)
1271 appendPQExpBuffer(oidQry, "Declare blobOid Cursor for SELECT DISTINCT loid FROM pg_largeobject");
1273 appendPQExpBuffer(oidQry, "Declare blobOid Cursor for SELECT oid from pg_class where relkind = 'l'");
1275 res = PQexec(g_conn, oidQry->data);
1276 check_sql_result(res, g_conn, oidQry->data, PGRES_COMMAND_OK);
1278 /* Fetch for cursor */
1279 appendPQExpBuffer(oidFetchQry, "Fetch %d in blobOid", loFetchSize);
1286 res = PQexec(g_conn, oidFetchQry->data);
1287 check_sql_result(res, g_conn, oidFetchQry->data, PGRES_TUPLES_OK);
1289 /* Process the tuples, if any */
1290 for (i = 0; i < PQntuples(res); i++)
1292 blobOid = atooid(PQgetvalue(res, i, 0));
1294 loFd = lo_open(g_conn, blobOid, INV_READ);
1297 write_msg(NULL, "dumpBlobs(): could not open large object: %s",
1298 PQerrorMessage(g_conn));
1302 StartBlob(AH, blobOid);
1304 /* Now read it in chunks, sending data to archive */
1307 cnt = lo_read(g_conn, loFd, buf, loBufSize);
1310 write_msg(NULL, "dumpBlobs(): error reading large object: %s",
1311 PQerrorMessage(g_conn));
1315 WriteData(AH, buf, cnt);
1319 lo_close(g_conn, loFd);
1321 EndBlob(AH, blobOid);
1324 } while (PQntuples(res) > 0);
1326 destroyPQExpBuffer(oidQry);
1327 destroyPQExpBuffer(oidFetchQry);
1334 * read all namespaces in the system catalogs and return them in the
1335 * NamespaceInfo* structure
1337 * numNamespaces is set to the number of namespaces read in
1340 getNamespaces(int *numNamespaces)
1346 NamespaceInfo *nsinfo;
1354 * Before 7.3, there are no real namespaces; create two dummy entries,
1355 * one for user stuff and one for system stuff.
1357 if (g_fout->remoteVersion < 70300)
1359 nsinfo = (NamespaceInfo *) malloc(2 * sizeof(NamespaceInfo));
1361 nsinfo[0].dobj.objType = DO_NAMESPACE;
1362 nsinfo[0].dobj.catId.tableoid = 0;
1363 nsinfo[0].dobj.catId.oid = 0;
1364 AssignDumpId(&nsinfo[0].dobj);
1365 nsinfo[0].nspname = strdup("");
1366 nsinfo[0].usename = strdup("");
1367 nsinfo[0].nspacl = strdup("");
1369 selectDumpableNamespace(&nsinfo[0]);
1371 nsinfo[1].dobj.objType = DO_NAMESPACE;
1372 nsinfo[1].dobj.catId.tableoid = 0;
1373 nsinfo[1].dobj.catId.oid = 1;
1374 AssignDumpId(&nsinfo[1].dobj);
1375 nsinfo[1].nspname = strdup("pg_catalog");
1376 nsinfo[1].usename = strdup("");
1377 nsinfo[1].nspacl = strdup("");
1379 selectDumpableNamespace(&nsinfo[1]);
1381 g_namespaces = nsinfo;
1382 g_numNamespaces = *numNamespaces = 2;
1387 query = createPQExpBuffer();
1389 /* Make sure we are in proper schema */
1390 selectSourceSchema("pg_catalog");
1393 * we fetch all namespaces including system ones, so that every object
1394 * we read in can be linked to a containing namespace.
1396 appendPQExpBuffer(query, "SELECT tableoid, oid, nspname, "
1397 "(select usename from pg_user where nspowner = usesysid) as usename, "
1399 "FROM pg_namespace");
1401 res = PQexec(g_conn, query->data);
1402 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
1404 ntups = PQntuples(res);
1406 nsinfo = (NamespaceInfo *) malloc(ntups * sizeof(NamespaceInfo));
1408 i_tableoid = PQfnumber(res, "tableoid");
1409 i_oid = PQfnumber(res, "oid");
1410 i_nspname = PQfnumber(res, "nspname");
1411 i_usename = PQfnumber(res, "usename");
1412 i_nspacl = PQfnumber(res, "nspacl");
1414 for (i = 0; i < ntups; i++)
1416 nsinfo[i].dobj.objType = DO_NAMESPACE;
1417 nsinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
1418 nsinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
1419 AssignDumpId(&nsinfo[i].dobj);
1420 nsinfo[i].nspname = strdup(PQgetvalue(res, i, i_nspname));
1421 nsinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
1422 nsinfo[i].nspacl = strdup(PQgetvalue(res, i, i_nspacl));
1424 /* Decide whether to dump this namespace */
1425 selectDumpableNamespace(&nsinfo[i]);
1427 if (strlen(nsinfo[i].usename) == 0)
1428 write_msg(NULL, "WARNING: owner of schema \"%s\" appears to be invalid\n",
1433 * If the user attempted to dump a specific namespace, check to ensure
1434 * that the specified namespace actually exists.
1436 if (selectSchemaName)
1438 for (i = 0; i < ntups; i++)
1439 if (strcmp(nsinfo[i].nspname, selectSchemaName) == 0)
1442 /* Didn't find a match */
1445 write_msg(NULL, "specified schema \"%s\" does not exist\n",
1452 destroyPQExpBuffer(query);
1454 g_namespaces = nsinfo;
1455 g_numNamespaces = *numNamespaces = ntups;
1462 * given a namespace OID and an object OID, look up the info read by
1465 * NB: for pre-7.3 source database, we use object OID to guess whether it's
1466 * a system object or not. In 7.3 and later there is no guessing.
1468 static NamespaceInfo *
1469 findNamespace(Oid nsoid, Oid objoid)
1473 if (g_fout->remoteVersion >= 70300)
1475 for (i = 0; i < g_numNamespaces; i++)
1477 NamespaceInfo *nsinfo = &g_namespaces[i];
1479 if (nsoid == nsinfo->dobj.catId.oid)
1482 write_msg(NULL, "schema with OID %u does not exist\n", nsoid);
1487 /* This code depends on the layout set up by getNamespaces. */
1488 if (objoid > g_last_builtin_oid)
1489 i = 0; /* user object */
1491 i = 1; /* system object */
1492 return &g_namespaces[i];
1495 return NULL; /* keep compiler quiet */
1500 * read all types in the system catalogs and return them in the
1501 * TypeInfo* structure
1503 * numTypes is set to the number of types read in
1505 * NB: this must run after getFuncs() because we assume we can do
1509 getTypes(int *numTypes)
1514 PQExpBuffer query = createPQExpBuffer();
1530 * we include even the built-in types because those may be used as
1531 * array elements by user-defined types
1533 * we filter out the built-in types when we dump out the types
1535 * same approach for undefined (shell) types
1538 /* Make sure we are in proper schema */
1539 selectSourceSchema("pg_catalog");
1541 if (g_fout->remoteVersion >= 70300)
1543 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
1545 "(select usename from pg_user where typowner = usesysid) as usename, "
1546 "typinput::oid as typinput, "
1547 "typoutput::oid as typoutput, typelem, typrelid, "
1548 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
1549 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END as typrelkind, "
1550 "typtype, typisdefined "
1553 else if (g_fout->remoteVersion >= 70100)
1555 appendPQExpBuffer(query, "SELECT tableoid, oid, typname, "
1556 "0::oid as typnamespace, "
1557 "(select usename from pg_user where typowner = usesysid) as usename, "
1558 "typinput::oid as typinput, "
1559 "typoutput::oid as typoutput, typelem, typrelid, "
1560 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
1561 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END as typrelkind, "
1562 "typtype, typisdefined "
1567 appendPQExpBuffer(query, "SELECT "
1568 "(SELECT oid FROM pg_class WHERE relname = 'pg_type') AS tableoid, "
1570 "0::oid as typnamespace, "
1571 "(select usename from pg_user where typowner = usesysid) as usename, "
1572 "typinput::oid as typinput, "
1573 "typoutput::oid as typoutput, typelem, typrelid, "
1574 "CASE WHEN typrelid = 0 THEN ' '::\"char\" "
1575 "ELSE (SELECT relkind FROM pg_class WHERE oid = typrelid) END as typrelkind, "
1576 "typtype, typisdefined "
1580 res = PQexec(g_conn, query->data);
1581 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
1583 ntups = PQntuples(res);
1585 tinfo = (TypeInfo *) malloc(ntups * sizeof(TypeInfo));
1587 i_tableoid = PQfnumber(res, "tableoid");
1588 i_oid = PQfnumber(res, "oid");
1589 i_typname = PQfnumber(res, "typname");
1590 i_typnamespace = PQfnumber(res, "typnamespace");
1591 i_usename = PQfnumber(res, "usename");
1592 i_typinput = PQfnumber(res, "typinput");
1593 i_typoutput = PQfnumber(res, "typoutput");
1594 i_typelem = PQfnumber(res, "typelem");
1595 i_typrelid = PQfnumber(res, "typrelid");
1596 i_typrelkind = PQfnumber(res, "typrelkind");
1597 i_typtype = PQfnumber(res, "typtype");
1598 i_typisdefined = PQfnumber(res, "typisdefined");
1600 for (i = 0; i < ntups; i++)
1605 tinfo[i].dobj.objType = DO_TYPE;
1606 tinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
1607 tinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
1608 AssignDumpId(&tinfo[i].dobj);
1609 tinfo[i].typname = strdup(PQgetvalue(res, i, i_typname));
1610 tinfo[i].typnamespace = findNamespace(atooid(PQgetvalue(res, i, i_typnamespace)),
1611 tinfo[i].dobj.catId.oid);
1612 tinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
1613 tinfo[i].typinput = atooid(PQgetvalue(res, i, i_typinput));
1614 typoutput = atooid(PQgetvalue(res, i, i_typoutput));
1615 tinfo[i].typelem = atooid(PQgetvalue(res, i, i_typelem));
1616 tinfo[i].typrelid = atooid(PQgetvalue(res, i, i_typrelid));
1617 tinfo[i].typrelkind = *PQgetvalue(res, i, i_typrelkind);
1618 tinfo[i].typtype = *PQgetvalue(res, i, i_typtype);
1621 * check for user-defined array types, omit system generated ones
1623 if (OidIsValid(tinfo[i].typelem) &&
1624 tinfo[i].typname[0] != '_')
1625 tinfo[i].isArray = true;
1627 tinfo[i].isArray = false;
1629 if (strcmp(PQgetvalue(res, i, i_typisdefined), "t") == 0)
1630 tinfo[i].isDefined = true;
1632 tinfo[i].isDefined = false;
1635 * If it's a domain, fetch info about its constraints, if any
1637 tinfo[i].nDomChecks = 0;
1638 tinfo[i].domChecks = NULL;
1639 if (tinfo[i].typtype == 'd')
1640 getDomainConstraints(&(tinfo[i]));
1643 * Make sure there are dependencies from the type to its input and
1644 * output functions. (We don't worry about typsend/typreceive since
1645 * those are only valid in 7.4 and later, wherein the standard
1646 * dependency mechanism will pick them up.)
1648 funcInfo = findFuncByOid(tinfo[i].typinput);
1650 addObjectDependency(&tinfo[i].dobj,
1651 funcInfo->dobj.dumpId);
1652 funcInfo = findFuncByOid(typoutput);
1654 addObjectDependency(&tinfo[i].dobj,
1655 funcInfo->dobj.dumpId);
1657 if (strlen(tinfo[i].usename) == 0 && tinfo[i].isDefined)
1658 write_msg(NULL, "WARNING: owner of data type \"%s\" appears to be invalid\n",
1666 destroyPQExpBuffer(query);
1673 * read all operators in the system catalogs and return them in the
1674 * OprInfo* structure
1676 * numOprs is set to the number of operators read in
1679 getOperators(int *numOprs)
1684 PQExpBuffer query = createPQExpBuffer();
1694 * find all operators, including builtin operators; we filter out
1695 * system-defined operators at dump-out time.
1698 /* Make sure we are in proper schema */
1699 selectSourceSchema("pg_catalog");
1701 if (g_fout->remoteVersion >= 70300)
1703 appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
1705 "(select usename from pg_user where oprowner = usesysid) as usename, "
1706 "oprcode::oid as oprcode "
1707 "FROM pg_operator");
1709 else if (g_fout->remoteVersion >= 70100)
1711 appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, "
1712 "0::oid as oprnamespace, "
1713 "(select usename from pg_user where oprowner = usesysid) as usename, "
1714 "oprcode::oid as oprcode "
1715 "FROM pg_operator");
1719 appendPQExpBuffer(query, "SELECT "
1720 "(SELECT oid FROM pg_class WHERE relname = 'pg_operator') AS tableoid, "
1722 "0::oid as oprnamespace, "
1723 "(select usename from pg_user where oprowner = usesysid) as usename, "
1724 "oprcode::oid as oprcode "
1725 "FROM pg_operator");
1728 res = PQexec(g_conn, query->data);
1729 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
1731 ntups = PQntuples(res);
1734 oprinfo = (OprInfo *) malloc(ntups * sizeof(OprInfo));
1736 i_tableoid = PQfnumber(res, "tableoid");
1737 i_oid = PQfnumber(res, "oid");
1738 i_oprname = PQfnumber(res, "oprname");
1739 i_oprnamespace = PQfnumber(res, "oprnamespace");
1740 i_usename = PQfnumber(res, "usename");
1741 i_oprcode = PQfnumber(res, "oprcode");
1743 for (i = 0; i < ntups; i++)
1745 oprinfo[i].dobj.objType = DO_OPERATOR;
1746 oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
1747 oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
1748 AssignDumpId(&oprinfo[i].dobj);
1749 oprinfo[i].oprname = strdup(PQgetvalue(res, i, i_oprname));
1750 oprinfo[i].oprnamespace = findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)),
1751 oprinfo[i].dobj.catId.oid);
1752 oprinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
1753 oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode));
1755 if (strlen(oprinfo[i].usename) == 0)
1756 write_msg(NULL, "WARNING: owner of operator \"%s\" appears to be invalid\n",
1757 oprinfo[i].oprname);
1762 destroyPQExpBuffer(query);
1769 * read all conversions in the system catalogs and return them in the
1770 * ConvInfo* structure
1772 * numConversions is set to the number of conversions read in
1775 getConversions(int *numConversions)
1780 PQExpBuffer query = createPQExpBuffer();
1788 /* Conversions didn't exist pre-7.3 */
1789 if (g_fout->remoteVersion < 70300) {
1790 *numConversions = 0;
1795 * find all conversions, including builtin conversions; we filter out
1796 * system-defined conversions at dump-out time.
1799 /* Make sure we are in proper schema */
1800 selectSourceSchema("pg_catalog");
1802 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
1804 "(select usename from pg_user where conowner = usesysid) as usename "
1805 "FROM pg_conversion");
1807 res = PQexec(g_conn, query->data);
1808 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
1810 ntups = PQntuples(res);
1811 *numConversions = ntups;
1813 convinfo = (ConvInfo *) malloc(ntups * sizeof(ConvInfo));
1815 i_tableoid = PQfnumber(res, "tableoid");
1816 i_oid = PQfnumber(res, "oid");
1817 i_conname = PQfnumber(res, "conname");
1818 i_connamespace = PQfnumber(res, "connamespace");
1819 i_usename = PQfnumber(res, "usename");
1821 for (i = 0; i < ntups; i++)
1823 convinfo[i].dobj.objType = DO_CONVERSION;
1824 convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
1825 convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
1826 AssignDumpId(&convinfo[i].dobj);
1827 convinfo[i].conname = strdup(PQgetvalue(res, i, i_conname));
1828 convinfo[i].connamespace = findNamespace(atooid(PQgetvalue(res, i, i_connamespace)),
1829 convinfo[i].dobj.catId.oid);
1830 convinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
1835 destroyPQExpBuffer(query);
1842 * read all opclasses in the system catalogs and return them in the
1843 * OpclassInfo* structure
1845 * numOpclasses is set to the number of opclasses read in
1848 getOpclasses(int *numOpclasses)
1853 PQExpBuffer query = createPQExpBuffer();
1854 OpclassInfo *opcinfo;
1862 * find all opclasses, including builtin opclasses; we filter out
1863 * system-defined opclasses at dump-out time.
1866 /* Make sure we are in proper schema */
1867 selectSourceSchema("pg_catalog");
1869 if (g_fout->remoteVersion >= 70300)
1871 appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
1873 "(select usename from pg_user where opcowner = usesysid) as usename "
1876 else if (g_fout->remoteVersion >= 70100)
1878 appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, "
1879 "0::oid as opcnamespace, "
1880 "''::name as usename "
1885 appendPQExpBuffer(query, "SELECT "
1886 "(SELECT oid FROM pg_class WHERE relname = 'pg_opclass') AS tableoid, "
1888 "0::oid as opcnamespace, "
1889 "''::name as usename "
1893 res = PQexec(g_conn, query->data);
1894 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
1896 ntups = PQntuples(res);
1897 *numOpclasses = ntups;
1899 opcinfo = (OpclassInfo *) malloc(ntups * sizeof(OpclassInfo));
1901 i_tableoid = PQfnumber(res, "tableoid");
1902 i_oid = PQfnumber(res, "oid");
1903 i_opcname = PQfnumber(res, "opcname");
1904 i_opcnamespace = PQfnumber(res, "opcnamespace");
1905 i_usename = PQfnumber(res, "usename");
1907 for (i = 0; i < ntups; i++)
1909 opcinfo[i].dobj.objType = DO_OPCLASS;
1910 opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
1911 opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
1912 AssignDumpId(&opcinfo[i].dobj);
1913 opcinfo[i].opcname = strdup(PQgetvalue(res, i, i_opcname));
1914 opcinfo[i].opcnamespace = findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace)),
1915 opcinfo[i].dobj.catId.oid);
1916 opcinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
1918 if (g_fout->remoteVersion >= 70300)
1920 if (strlen(opcinfo[i].usename) == 0)
1921 write_msg(NULL, "WARNING: owner of operator class \"%s\" appears to be invalid\n",
1922 opcinfo[i].opcname);
1928 destroyPQExpBuffer(query);
1935 * read all the user-defined aggregates in the system catalogs and
1936 * return them in the AggInfo* structure
1938 * numAggs is set to the number of aggregates read in
1941 getAggregates(int *numAggs)
1946 PQExpBuffer query = createPQExpBuffer();
1956 /* Make sure we are in proper schema */
1957 selectSourceSchema("pg_catalog");
1959 /* find all user-defined aggregates */
1961 if (g_fout->remoteVersion >= 70300)
1963 appendPQExpBuffer(query, "SELECT tableoid, oid, proname as aggname, "
1964 "pronamespace as aggnamespace, "
1965 "proargtypes[0] as aggbasetype, "
1966 "(select usename from pg_user where proowner = usesysid) as usename, "
1970 "AND pronamespace != "
1971 "(select oid from pg_namespace where nspname = 'pg_catalog')");
1973 else if (g_fout->remoteVersion >= 70100)
1975 appendPQExpBuffer(query, "SELECT tableoid, oid, aggname, "
1976 "0::oid as aggnamespace, "
1978 "(select usename from pg_user where aggowner = usesysid) as usename, "
1980 "FROM pg_aggregate "
1981 "where oid > '%u'::oid",
1982 g_last_builtin_oid);
1986 appendPQExpBuffer(query, "SELECT "
1987 "(SELECT oid FROM pg_class WHERE relname = 'pg_aggregate') AS tableoid, "
1989 "0::oid as aggnamespace, "
1991 "(select usename from pg_user where aggowner = usesysid) as usename, "
1993 "FROM pg_aggregate "
1994 "where oid > '%u'::oid",
1995 g_last_builtin_oid);
1998 res = PQexec(g_conn, query->data);
1999 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2001 ntups = PQntuples(res);
2004 agginfo = (AggInfo *) malloc(ntups * sizeof(AggInfo));
2006 i_tableoid = PQfnumber(res, "tableoid");
2007 i_oid = PQfnumber(res, "oid");
2008 i_aggname = PQfnumber(res, "aggname");
2009 i_aggnamespace = PQfnumber(res, "aggnamespace");
2010 i_aggbasetype = PQfnumber(res, "aggbasetype");
2011 i_usename = PQfnumber(res, "usename");
2012 i_aggacl = PQfnumber(res, "aggacl");
2014 for (i = 0; i < ntups; i++)
2016 agginfo[i].aggfn.dobj.objType = DO_AGG;
2017 agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
2018 agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
2019 AssignDumpId(&agginfo[i].aggfn.dobj);
2020 agginfo[i].aggfn.proname = strdup(PQgetvalue(res, i, i_aggname));
2021 agginfo[i].aggfn.pronamespace = findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace)),
2022 agginfo[i].aggfn.dobj.catId.oid);
2023 agginfo[i].aggfn.usename = strdup(PQgetvalue(res, i, i_usename));
2024 if (strlen(agginfo[i].aggfn.usename) == 0)
2025 write_msg(NULL, "WARNING: owner of aggregate function \"%s\" appears to be invalid\n",
2026 agginfo[i].aggfn.proname);
2027 agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */
2028 agginfo[i].aggfn.nargs = 1;
2029 agginfo[i].aggfn.argtypes = (Oid *) malloc(sizeof(Oid));
2030 agginfo[i].aggfn.argtypes[0] = atooid(PQgetvalue(res, i, i_aggbasetype));
2031 agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */
2032 agginfo[i].aggfn.proacl = strdup(PQgetvalue(res, i, i_aggacl));
2033 agginfo[i].anybasetype = false; /* computed when it's dumped */
2034 agginfo[i].fmtbasetype = NULL; /* computed when it's dumped */
2039 destroyPQExpBuffer(query);
2046 * read all the user-defined functions in the system catalogs and
2047 * return them in the FuncInfo* structure
2049 * numFuncs is set to the number of functions read in
2052 getFuncs(int *numFuncs)
2057 PQExpBuffer query = createPQExpBuffer();
2070 /* Make sure we are in proper schema */
2071 selectSourceSchema("pg_catalog");
2073 /* find all user-defined funcs */
2075 if (g_fout->remoteVersion >= 70300)
2077 appendPQExpBuffer(query,
2078 "SELECT tableoid, oid, proname, prolang, "
2079 "pronargs, proargtypes, prorettype, proacl, "
2081 "(select usename from pg_user where proowner = usesysid) as usename "
2083 "WHERE NOT proisagg "
2084 "AND pronamespace != "
2085 "(select oid from pg_namespace where nspname = 'pg_catalog')");
2087 else if (g_fout->remoteVersion >= 70100)
2089 appendPQExpBuffer(query,
2090 "SELECT tableoid, oid, proname, prolang, "
2091 "pronargs, proargtypes, prorettype, "
2092 "'{=X}' as proacl, "
2093 "0::oid as pronamespace, "
2094 "(select usename from pg_user where proowner = usesysid) as usename "
2096 "where pg_proc.oid > '%u'::oid",
2097 g_last_builtin_oid);
2101 appendPQExpBuffer(query,
2103 "(SELECT oid FROM pg_class WHERE relname = 'pg_proc') AS tableoid, "
2104 "oid, proname, prolang, "
2105 "pronargs, proargtypes, prorettype, "
2106 "'{=X}' as proacl, "
2107 "0::oid as pronamespace, "
2108 "(select usename from pg_user where proowner = usesysid) as usename "
2110 "where pg_proc.oid > '%u'::oid",
2111 g_last_builtin_oid);
2114 res = PQexec(g_conn, query->data);
2115 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2117 ntups = PQntuples(res);
2121 finfo = (FuncInfo *) calloc(ntups, sizeof(FuncInfo));
2123 i_tableoid = PQfnumber(res, "tableoid");
2124 i_oid = PQfnumber(res, "oid");
2125 i_proname = PQfnumber(res, "proname");
2126 i_pronamespace = PQfnumber(res, "pronamespace");
2127 i_usename = PQfnumber(res, "usename");
2128 i_prolang = PQfnumber(res, "prolang");
2129 i_pronargs = PQfnumber(res, "pronargs");
2130 i_proargtypes = PQfnumber(res, "proargtypes");
2131 i_prorettype = PQfnumber(res, "prorettype");
2132 i_proacl = PQfnumber(res, "proacl");
2134 for (i = 0; i < ntups; i++)
2136 finfo[i].dobj.objType = DO_FUNC;
2137 finfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
2138 finfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
2139 AssignDumpId(&finfo[i].dobj);
2140 finfo[i].proname = strdup(PQgetvalue(res, i, i_proname));
2141 finfo[i].pronamespace = findNamespace(atooid(PQgetvalue(res, i, i_pronamespace)),
2142 finfo[i].dobj.catId.oid);
2143 finfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
2144 finfo[i].lang = atooid(PQgetvalue(res, i, i_prolang));
2145 finfo[i].prorettype = atooid(PQgetvalue(res, i, i_prorettype));
2146 finfo[i].proacl = strdup(PQgetvalue(res, i, i_proacl));
2147 finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs));
2148 if (finfo[i].nargs == 0)
2149 finfo[i].argtypes = NULL;
2152 finfo[i].argtypes = (Oid *) malloc(finfo[i].nargs * sizeof(Oid));
2153 parseOidArray(PQgetvalue(res, i, i_proargtypes),
2154 finfo[i].argtypes, finfo[i].nargs);
2157 if (strlen(finfo[i].usename) == 0)
2158 write_msg(NULL, "WARNING: owner of function \"%s\" appears to be invalid\n",
2164 destroyPQExpBuffer(query);
2171 * read all the user-defined tables (no indexes, no catalogs)
2172 * in the system catalogs return them in the TableInfo* structure
2174 * numTables is set to the number of tables read in
2177 getTables(int *numTables)
2182 PQExpBuffer query = createPQExpBuffer();
2183 PQExpBuffer delqry = createPQExpBuffer();
2184 PQExpBuffer lockquery = createPQExpBuffer();
2201 /* Make sure we are in proper schema */
2202 selectSourceSchema("pg_catalog");
2205 * Find all the tables (including views and sequences).
2207 * We include system catalogs, so that we can work if a user table is
2208 * defined to inherit from a system catalog (pretty weird, but...)
2210 * We ignore tables that are not type 'r' (ordinary relation) or 'S'
2211 * (sequence) or 'v' (view).
2213 * Note: in this phase we should collect only a minimal amount of
2214 * information about each table, basically just enough to decide if it
2215 * is interesting. We must fetch all tables in this phase because
2216 * otherwise we cannot correctly identify inherited columns, serial
2220 if (g_fout->remoteVersion >= 70300)
2223 * Left join to pick up dependency info linking sequences to their
2224 * serial column, if any
2226 appendPQExpBuffer(query,
2227 "SELECT c.tableoid, c.oid, relname, "
2228 "relacl, relkind, relnamespace, "
2229 "(select usename from pg_user where relowner = usesysid) as usename, "
2230 "relchecks, reltriggers, "
2231 "relhasindex, relhasrules, relhasoids, "
2232 "d.refobjid as owning_tab, "
2233 "d.refobjsubid as owning_col "
2235 "left join pg_depend d on "
2236 "(c.relkind = '%c' and "
2237 "d.classid = c.tableoid and d.objid = c.oid and "
2238 "d.objsubid = 0 and "
2239 "d.refclassid = c.tableoid and d.deptype = 'i') "
2240 "where relkind in ('%c', '%c', '%c') "
2243 RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW);
2245 else if (g_fout->remoteVersion >= 70200)
2247 appendPQExpBuffer(query,
2248 "SELECT tableoid, oid, relname, relacl, relkind, "
2249 "0::oid as relnamespace, "
2250 "(select usename from pg_user where relowner = usesysid) as usename, "
2251 "relchecks, reltriggers, "
2252 "relhasindex, relhasrules, relhasoids, "
2253 "NULL::oid as owning_tab, "
2254 "NULL::int4 as owning_col "
2256 "where relkind in ('%c', '%c', '%c') "
2258 RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW);
2260 else if (g_fout->remoteVersion >= 70100)
2262 /* all tables have oids in 7.1 */
2263 appendPQExpBuffer(query,
2264 "SELECT tableoid, oid, relname, relacl, relkind, "
2265 "0::oid as relnamespace, "
2266 "(select usename from pg_user where relowner = usesysid) as usename, "
2267 "relchecks, reltriggers, "
2268 "relhasindex, relhasrules, "
2269 "'t'::bool as relhasoids, "
2270 "NULL::oid as owning_tab, "
2271 "NULL::int4 as owning_col "
2273 "where relkind in ('%c', '%c', '%c') "
2275 RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW);
2280 * Before 7.1, view relkind was not set to 'v', so we must check
2281 * if we have a view by looking for a rule in pg_rewrite.
2283 appendPQExpBuffer(query,
2285 "(SELECT oid FROM pg_class WHERE relname = 'pg_class') AS tableoid, "
2286 "oid, relname, relacl, "
2287 "CASE WHEN relhasrules and relkind = 'r' "
2288 " and EXISTS(SELECT rulename FROM pg_rewrite r WHERE "
2289 " r.ev_class = c.oid AND r.ev_type = '1') "
2290 "THEN '%c'::\"char\" "
2291 "ELSE relkind END AS relkind,"
2292 "0::oid as relnamespace, "
2293 "(select usename from pg_user where relowner = usesysid) as usename, "
2294 "relchecks, reltriggers, "
2295 "relhasindex, relhasrules, "
2296 "'t'::bool as relhasoids, "
2297 "NULL::oid as owning_tab, "
2298 "NULL::int4 as owning_col "
2300 "where relkind in ('%c', '%c') "
2303 RELKIND_RELATION, RELKIND_SEQUENCE);
2306 res = PQexec(g_conn, query->data);
2307 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2309 ntups = PQntuples(res);
2314 * Extract data from result and lock dumpable tables. We do the
2315 * locking before anything else, to minimize the window wherein a
2316 * table could disappear under us.
2318 * Note that we have to save info about all tables here, even when
2319 * dumping only one, because we don't yet know which tables might be
2320 * inheritance ancestors of the target table.
2322 tblinfo = (TableInfo *) calloc(ntups, sizeof(TableInfo));
2324 i_reltableoid = PQfnumber(res, "tableoid");
2325 i_reloid = PQfnumber(res, "oid");
2326 i_relname = PQfnumber(res, "relname");
2327 i_relnamespace = PQfnumber(res, "relnamespace");
2328 i_relacl = PQfnumber(res, "relacl");
2329 i_relkind = PQfnumber(res, "relkind");
2330 i_usename = PQfnumber(res, "usename");
2331 i_relchecks = PQfnumber(res, "relchecks");
2332 i_reltriggers = PQfnumber(res, "reltriggers");
2333 i_relhasindex = PQfnumber(res, "relhasindex");
2334 i_relhasrules = PQfnumber(res, "relhasrules");
2335 i_relhasoids = PQfnumber(res, "relhasoids");
2336 i_owning_tab = PQfnumber(res, "owning_tab");
2337 i_owning_col = PQfnumber(res, "owning_col");
2339 for (i = 0; i < ntups; i++)
2341 tblinfo[i].dobj.objType = DO_TABLE;
2342 tblinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_reltableoid));
2343 tblinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_reloid));
2344 AssignDumpId(&tblinfo[i].dobj);
2345 tblinfo[i].relname = strdup(PQgetvalue(res, i, i_relname));
2346 tblinfo[i].relnamespace = findNamespace(atooid(PQgetvalue(res, i, i_relnamespace)),
2347 tblinfo[i].dobj.catId.oid);
2348 tblinfo[i].usename = strdup(PQgetvalue(res, i, i_usename));
2349 tblinfo[i].relacl = strdup(PQgetvalue(res, i, i_relacl));
2350 tblinfo[i].relkind = *(PQgetvalue(res, i, i_relkind));
2351 tblinfo[i].hasindex = (strcmp(PQgetvalue(res, i, i_relhasindex), "t") == 0);
2352 tblinfo[i].hasrules = (strcmp(PQgetvalue(res, i, i_relhasrules), "t") == 0);
2353 tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0);
2354 tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
2355 tblinfo[i].ntrig = atoi(PQgetvalue(res, i, i_reltriggers));
2356 if (PQgetisnull(res, i, i_owning_tab))
2358 tblinfo[i].owning_tab = InvalidOid;
2359 tblinfo[i].owning_col = 0;
2363 tblinfo[i].owning_tab = atooid(PQgetvalue(res, i, i_owning_tab));
2364 tblinfo[i].owning_col = atoi(PQgetvalue(res, i, i_owning_col));
2367 /* other fields were zeroed above */
2370 * Decide whether we want to dump this table. Sequences owned by
2371 * serial columns are never dumpable on their own; we will
2372 * transpose their owning table's dump flag to them below.
2374 if (OidIsValid(tblinfo[i].owning_tab))
2375 tblinfo[i].dump = false;
2377 selectDumpableTable(&tblinfo[i]);
2378 tblinfo[i].interesting = tblinfo[i].dump;
2381 * Read-lock target tables to make sure they aren't DROPPED or
2382 * altered in schema before we get around to dumping them.
2384 * Note that we don't explicitly lock parents of the target tables;
2385 * we assume our lock on the child is enough to prevent schema
2386 * alterations to parent tables.
2388 * NOTE: it'd be kinda nice to lock views and sequences too, not only
2389 * plain tables, but the backend doesn't presently allow that.
2391 if (tblinfo[i].dump && tblinfo[i].relkind == RELKIND_RELATION)
2393 resetPQExpBuffer(lockquery);
2394 appendPQExpBuffer(lockquery,
2395 "LOCK TABLE %s IN ACCESS SHARE MODE",
2396 fmtQualifiedId(tblinfo[i].relnamespace->nspname,
2397 tblinfo[i].relname));
2398 do_sql_command(g_conn, lockquery->data);
2401 /* Emit notice if join for owner failed */
2402 if (strlen(tblinfo[i].usename) == 0)
2403 write_msg(NULL, "WARNING: owner of table \"%s\" appears to be invalid\n",
2404 tblinfo[i].relname);
2408 * If the user is attempting to dump a specific table, check to ensure
2409 * that the specified table actually exists. (This is a bit simplistic
2410 * since we don't fully check the combination of -n and -t switches.)
2412 if (selectTableName)
2414 for (i = 0; i < ntups; i++)
2415 if (strcmp(tblinfo[i].relname, selectTableName) == 0)
2418 /* Didn't find a match */
2421 write_msg(NULL, "specified table \"%s\" does not exist\n",
2428 destroyPQExpBuffer(query);
2429 destroyPQExpBuffer(delqry);
2430 destroyPQExpBuffer(lockquery);
2437 * read all the inheritance information
2438 * from the system catalogs return them in the InhInfo* structure
2440 * numInherits is set to the number of pairs read in
2443 getInherits(int *numInherits)
2448 PQExpBuffer query = createPQExpBuffer();
2454 /* Make sure we are in proper schema */
2455 selectSourceSchema("pg_catalog");
2457 /* find all the inheritance information */
2459 appendPQExpBuffer(query, "SELECT inhrelid, inhparent from pg_inherits");
2461 res = PQexec(g_conn, query->data);
2462 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2464 ntups = PQntuples(res);
2466 *numInherits = ntups;
2468 inhinfo = (InhInfo *) malloc(ntups * sizeof(InhInfo));
2470 i_inhrelid = PQfnumber(res, "inhrelid");
2471 i_inhparent = PQfnumber(res, "inhparent");
2473 for (i = 0; i < ntups; i++)
2475 inhinfo[i].inhrelid = atooid(PQgetvalue(res, i, i_inhrelid));
2476 inhinfo[i].inhparent = atooid(PQgetvalue(res, i, i_inhparent));
2481 destroyPQExpBuffer(query);
2488 * get information about every index on a dumpable table
2490 * Note: index data is not returned directly to the caller, but it
2491 * does get entered into the DumpableObject tables.
2494 getIndexes(TableInfo tblinfo[], int numTables)
2498 PQExpBuffer query = createPQExpBuffer();
2501 ConstraintInfo *constrinfo;
2515 for (i = 0; i < numTables; i++)
2517 TableInfo *tbinfo = &tblinfo[i];
2519 /* Only plain tables have indexes */
2520 if (tbinfo->relkind != RELKIND_RELATION || !tbinfo->hasindex)
2527 write_msg(NULL, "reading indexes for table \"%s\"\n",
2530 /* Make sure we are in proper schema so indexdef is right */
2531 selectSourceSchema(tbinfo->relnamespace->nspname);
2534 * The point of the messy-looking outer join is to find a
2535 * constraint that is related by an internal dependency link to
2536 * the index. If we find one, create a CONSTRAINT entry linked
2537 * to the INDEX entry. We assume an index won't have more than
2538 * one internal dependency.
2540 resetPQExpBuffer(query);
2541 if (g_fout->remoteVersion >= 70300)
2543 appendPQExpBuffer(query,
2544 "SELECT t.tableoid, t.oid, "
2545 "t.relname as indexname, "
2546 "pg_catalog.pg_get_indexdef(i.indexrelid) as indexdef, "
2547 "t.relnatts as indnkeys, "
2548 "i.indkey, i.indisclustered, "
2549 "c.contype, c.conname, "
2550 "c.tableoid as contableoid, "
2552 "FROM pg_catalog.pg_index i "
2553 "JOIN pg_catalog.pg_class t ON (t.oid = i.indexrelid) "
2554 "LEFT JOIN pg_catalog.pg_depend d "
2555 "ON (d.classid = t.tableoid "
2556 "AND d.objid = t.oid "
2557 "AND d.deptype = 'i') "
2558 "LEFT JOIN pg_catalog.pg_constraint c "
2559 "ON (d.refclassid = c.tableoid "
2560 "AND d.refobjid = c.oid) "
2561 "WHERE i.indrelid = '%u'::pg_catalog.oid "
2562 "ORDER BY indexname",
2563 tbinfo->dobj.catId.oid);
2565 else if (g_fout->remoteVersion >= 70100)
2567 appendPQExpBuffer(query,
2568 "SELECT t.tableoid, t.oid, "
2569 "t.relname as indexname, "
2570 "pg_get_indexdef(i.indexrelid) as indexdef, "
2571 "t.relnatts as indnkeys, "
2572 "i.indkey, false as indisclustered, "
2573 "CASE WHEN i.indisprimary THEN 'p'::char "
2574 "ELSE '0'::char END as contype, "
2575 "t.relname as conname, "
2576 "0::oid as contableoid, "
2578 "FROM pg_index i, pg_class t "
2579 "WHERE t.oid = i.indexrelid "
2580 "AND i.indrelid = '%u'::oid "
2581 "ORDER BY indexname",
2582 tbinfo->dobj.catId.oid);
2586 appendPQExpBuffer(query,
2588 "(SELECT oid FROM pg_class WHERE relname = 'pg_class') AS tableoid, "
2590 "t.relname as indexname, "
2591 "pg_get_indexdef(i.indexrelid) as indexdef, "
2592 "t.relnatts as indnkeys, "
2593 "i.indkey, false as indisclustered, "
2594 "CASE WHEN i.indisprimary THEN 'p'::char "
2595 "ELSE '0'::char END as contype, "
2596 "t.relname as conname, "
2597 "0::oid as contableoid, "
2599 "FROM pg_index i, pg_class t "
2600 "WHERE t.oid = i.indexrelid "
2601 "AND i.indrelid = '%u'::oid "
2602 "ORDER BY indexname",
2603 tbinfo->dobj.catId.oid);
2606 res = PQexec(g_conn, query->data);
2607 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2609 ntups = PQntuples(res);
2611 i_tableoid = PQfnumber(res, "tableoid");
2612 i_oid = PQfnumber(res, "oid");
2613 i_indexname = PQfnumber(res, "indexname");
2614 i_indexdef = PQfnumber(res, "indexdef");
2615 i_indnkeys = PQfnumber(res, "indnkeys");
2616 i_indkey = PQfnumber(res, "indkey");
2617 i_indisclustered = PQfnumber(res, "indisclustered");
2618 i_contype = PQfnumber(res, "contype");
2619 i_conname = PQfnumber(res, "conname");
2620 i_contableoid = PQfnumber(res, "contableoid");
2621 i_conoid = PQfnumber(res, "conoid");
2623 indxinfo = (IndxInfo *) malloc(ntups * sizeof(IndxInfo));
2624 constrinfo = (ConstraintInfo *) malloc(ntups * sizeof(ConstraintInfo));
2626 for (j = 0; j < ntups; j++)
2630 indxinfo[j].dobj.objType = DO_INDEX;
2631 indxinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
2632 indxinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
2633 AssignDumpId(&indxinfo[j].dobj);
2634 indxinfo[j].indexname = strdup(PQgetvalue(res, j, i_indexname));
2635 indxinfo[j].indextable = tbinfo;
2636 indxinfo[j].indexdef = strdup(PQgetvalue(res, j, i_indexdef));
2637 indxinfo[j].indnkeys = atoi(PQgetvalue(res, j, i_indnkeys));
2639 * In pre-7.4 releases, indkeys may contain more entries than
2640 * indnkeys says (since indnkeys will be 1 for a functional
2641 * index). We don't actually care about this case since we don't
2642 * examine indkeys except for indexes associated with PRIMARY
2643 * and UNIQUE constraints, which are never functional indexes.
2644 * But we have to allocate enough space to keep parseOidArray
2647 indxinfo[j].indkeys = (Oid *) malloc(INDEX_MAX_KEYS * sizeof(Oid));
2648 parseOidArray(PQgetvalue(res, j, i_indkey),
2649 indxinfo[j].indkeys, INDEX_MAX_KEYS);
2650 indxinfo[j].indisclustered = (PQgetvalue(res, j, i_indisclustered)[0] == 't');
2651 contype = *(PQgetvalue(res, j, i_contype));
2653 if (contype == 'p' || contype == 'u')
2656 * If we found a constraint matching the index, create an
2659 * In a pre-7.3 database, we take this path iff the index was
2660 * marked indisprimary.
2662 constrinfo[j].dobj.objType = DO_CONSTRAINT;
2663 constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
2664 constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
2665 AssignDumpId(&constrinfo[j].dobj);
2667 constrinfo[j].conname = strdup(PQgetvalue(res, j, i_conname));
2668 constrinfo[j].contable = tbinfo;
2669 constrinfo[j].condomain = NULL;
2670 constrinfo[j].contype = contype;
2671 constrinfo[j].condef = NULL;
2672 constrinfo[j].conindex = indxinfo[j].dobj.dumpId;
2673 constrinfo[j].coninherited = false;
2674 constrinfo[j].separate = true;
2676 indxinfo[j].indexconstraint = constrinfo[j].dobj.dumpId;
2678 /* If pre-7.3 DB, better make sure table comes first */
2679 addObjectDependency(&constrinfo[j].dobj,
2680 tbinfo->dobj.dumpId);
2684 /* Plain secondary index */
2685 indxinfo[j].indexconstraint = 0;
2692 destroyPQExpBuffer(query);
2698 * Get info about constraints on dumpable tables.
2700 * Currently handles foreign keys only.
2701 * Unique and primary key constraints are handled with indexes,
2702 * while check constraints are processed in getTableAttrs().
2705 getConstraints(TableInfo tblinfo[], int numTables)
2709 ConstraintInfo *constrinfo;
2718 /* pg_constraint was created in 7.3, so nothing to do if older */
2719 if (g_fout->remoteVersion < 70300)
2722 query = createPQExpBuffer();
2724 for (i = 0; i < numTables; i++)
2726 TableInfo *tbinfo = &tblinfo[i];
2728 if (tbinfo->ntrig == 0 || !tbinfo->dump)
2732 write_msg(NULL, "reading foreign key constraints for table \"%s\"\n",
2736 * select table schema to ensure constraint expr is qualified if
2739 selectSourceSchema(tbinfo->relnamespace->nspname);
2741 resetPQExpBuffer(query);
2742 appendPQExpBuffer(query,
2743 "SELECT tableoid, oid, conname, "
2744 "pg_catalog.pg_get_constraintdef(oid) as condef "
2745 "FROM pg_catalog.pg_constraint "
2746 "WHERE conrelid = '%u'::pg_catalog.oid "
2747 "AND contype = 'f'",
2748 tbinfo->dobj.catId.oid);
2749 res = PQexec(g_conn, query->data);
2750 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2752 ntups = PQntuples(res);
2754 i_contableoid = PQfnumber(res, "tableoid");
2755 i_conoid = PQfnumber(res, "oid");
2756 i_conname = PQfnumber(res, "conname");
2757 i_condef = PQfnumber(res, "condef");
2759 constrinfo = (ConstraintInfo *) malloc(ntups * sizeof(ConstraintInfo));
2761 for (j = 0; j < ntups; j++)
2763 constrinfo[j].dobj.objType = DO_FK_CONSTRAINT;
2764 constrinfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_contableoid));
2765 constrinfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_conoid));
2766 AssignDumpId(&constrinfo[j].dobj);
2767 constrinfo[j].conname = strdup(PQgetvalue(res, j, i_conname));
2768 constrinfo[j].contable = tbinfo;
2769 constrinfo[j].condomain = NULL;
2770 constrinfo[j].contype = 'f';
2771 constrinfo[j].condef = strdup(PQgetvalue(res, j, i_condef));
2772 constrinfo[j].conindex = 0;
2773 constrinfo[j].coninherited = false;
2774 constrinfo[j].separate = true;
2780 destroyPQExpBuffer(query);
2784 * getDomainConstraints
2786 * Get info about constraints on a domain.
2789 getDomainConstraints(TypeInfo *tinfo)
2792 ConstraintInfo *constrinfo;
2801 /* pg_constraint was created in 7.3, so nothing to do if older */
2802 if (g_fout->remoteVersion < 70300)
2806 * select appropriate schema to ensure names in constraint are properly
2809 selectSourceSchema(tinfo->typnamespace->nspname);
2811 query = createPQExpBuffer();
2813 if (g_fout->remoteVersion >= 70400)
2814 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
2815 "pg_catalog.pg_get_constraintdef(oid) AS consrc "
2816 "FROM pg_catalog.pg_constraint "
2817 "WHERE contypid = '%u'::pg_catalog.oid "
2819 tinfo->dobj.catId.oid);
2821 appendPQExpBuffer(query, "SELECT tableoid, oid, conname, "
2822 "'CHECK (' || consrc || ')' AS consrc "
2823 "FROM pg_catalog.pg_constraint "
2824 "WHERE contypid = '%u'::pg_catalog.oid "
2826 tinfo->dobj.catId.oid);
2828 res = PQexec(g_conn, query->data);
2829 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2831 ntups = PQntuples(res);
2833 i_tableoid = PQfnumber(res, "tableoid");
2834 i_oid = PQfnumber(res, "oid");
2835 i_conname = PQfnumber(res, "conname");
2836 i_consrc = PQfnumber(res, "consrc");
2838 constrinfo = (ConstraintInfo *) malloc(ntups * sizeof(ConstraintInfo));
2840 tinfo->nDomChecks = ntups;
2841 tinfo->domChecks = constrinfo;
2843 for (i = 0; i < ntups; i++)
2845 constrinfo[i].dobj.objType = DO_CONSTRAINT;
2846 constrinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
2847 constrinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
2848 AssignDumpId(&constrinfo[i].dobj);
2849 constrinfo[i].conname = strdup(PQgetvalue(res, i, i_conname));
2850 constrinfo[i].contable = NULL;
2851 constrinfo[i].condomain = tinfo;
2852 constrinfo[i].contype = 'c';
2853 constrinfo[i].condef = strdup(PQgetvalue(res, i, i_consrc));
2854 constrinfo[i].conindex = 0;
2855 constrinfo[i].coninherited = false;
2856 constrinfo[i].separate = false;
2858 * Make the domain depend on the constraint, ensuring it won't
2859 * be output till any constraint dependencies are OK.
2861 addObjectDependency(&tinfo->dobj,
2862 constrinfo[i].dobj.dumpId);
2867 destroyPQExpBuffer(query);
2872 * get basic information about every rule in the system
2874 * numRules is set to the number of rules read in
2877 getRules(int *numRules)
2882 PQExpBuffer query = createPQExpBuffer();
2891 /* Make sure we are in proper schema */
2892 selectSourceSchema("pg_catalog");
2894 if (g_fout->remoteVersion >= 70100)
2896 appendPQExpBuffer(query, "SELECT "
2897 "tableoid, oid, rulename, "
2898 "ev_class as ruletable, ev_type, is_instead "
2904 appendPQExpBuffer(query, "SELECT "
2905 "(SELECT oid FROM pg_class WHERE relname = 'pg_rewrite') AS tableoid, "
2907 "ev_class as ruletable, ev_type, is_instead "
2912 res = PQexec(g_conn, query->data);
2913 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
2915 ntups = PQntuples(res);
2919 ruleinfo = (RuleInfo *) malloc(ntups * sizeof(RuleInfo));
2921 i_tableoid = PQfnumber(res, "tableoid");
2922 i_oid = PQfnumber(res, "oid");
2923 i_rulename = PQfnumber(res, "rulename");
2924 i_ruletable = PQfnumber(res, "ruletable");
2925 i_ev_type = PQfnumber(res, "ev_type");
2926 i_is_instead = PQfnumber(res, "is_instead");
2928 for (i = 0; i < ntups; i++)
2932 ruleinfo[i].dobj.objType = DO_RULE;
2933 ruleinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
2934 ruleinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
2935 AssignDumpId(&ruleinfo[i].dobj);
2936 ruleinfo[i].rulename = strdup(PQgetvalue(res, i, i_rulename));
2937 ruletableoid = atooid(PQgetvalue(res, i, i_ruletable));
2938 ruleinfo[i].ruletable = findTableByOid(ruletableoid);
2939 ruleinfo[i].ev_type = *(PQgetvalue(res, i, i_ev_type));
2940 ruleinfo[i].is_instead = *(PQgetvalue(res, i, i_is_instead)) == 't';
2941 if (ruleinfo[i].ruletable)
2944 * If the table is a view, force its ON SELECT rule to be sorted
2945 * before the view itself --- this ensures that any dependencies
2946 * for the rule affect the table's positioning. Other rules
2947 * are forced to appear after their table.
2949 if (ruleinfo[i].ruletable->relkind == RELKIND_VIEW &&
2950 ruleinfo[i].ev_type == '1' && ruleinfo[i].is_instead)
2951 addObjectDependency(&ruleinfo[i].ruletable->dobj,
2952 ruleinfo[i].dobj.dumpId);
2954 addObjectDependency(&ruleinfo[i].dobj,
2955 ruleinfo[i].ruletable->dobj.dumpId);
2961 destroyPQExpBuffer(query);
2968 * get information about every trigger on a dumpable table
2970 * Note: trigger data is not returned directly to the caller, but it
2971 * does get entered into the DumpableObject tables.
2974 getTriggers(TableInfo tblinfo[], int numTables)
2978 PQExpBuffer query = createPQExpBuffer();
2980 TriggerInfo *tginfo;
2996 for (i = 0; i < numTables; i++)
2998 TableInfo *tbinfo = &tblinfo[i];
3000 if (tbinfo->ntrig == 0 || !tbinfo->dump)
3004 write_msg(NULL, "reading triggers for table \"%s\"\n",
3008 * select table schema to ensure regproc name is qualified if
3011 selectSourceSchema(tbinfo->relnamespace->nspname);
3013 resetPQExpBuffer(query);
3014 if (g_fout->remoteVersion >= 70300)
3017 * We ignore triggers that are tied to a foreign-key
3020 appendPQExpBuffer(query,
3022 "tgfoid::pg_catalog.regproc as tgfname, "
3023 "tgtype, tgnargs, tgargs, "
3024 "tgisconstraint, tgconstrname, tgdeferrable, "
3025 "tgconstrrelid, tginitdeferred, tableoid, oid, "
3026 "tgconstrrelid::pg_catalog.regclass as tgconstrrelname "
3027 "from pg_catalog.pg_trigger t "
3028 "where tgrelid = '%u'::pg_catalog.oid "
3029 "and (not tgisconstraint "
3031 " (SELECT 1 FROM pg_catalog.pg_depend d "
3032 " JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
3033 " WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))",
3034 tbinfo->dobj.catId.oid);
3036 else if (g_fout->remoteVersion >= 70100)
3038 appendPQExpBuffer(query,
3039 "SELECT tgname, tgfoid::regproc as tgfname, "
3040 "tgtype, tgnargs, tgargs, "
3041 "tgisconstraint, tgconstrname, tgdeferrable, "
3042 "tgconstrrelid, tginitdeferred, tableoid, oid, "
3043 "(select relname from pg_class where oid = tgconstrrelid) "
3044 " as tgconstrrelname "
3046 "where tgrelid = '%u'::oid",
3047 tbinfo->dobj.catId.oid);
3051 appendPQExpBuffer(query,
3052 "SELECT tgname, tgfoid::regproc as tgfname, "
3053 "tgtype, tgnargs, tgargs, "
3054 "tgisconstraint, tgconstrname, tgdeferrable, "
3055 "tgconstrrelid, tginitdeferred, "
3056 "(SELECT oid FROM pg_class WHERE relname = 'pg_trigger') AS tableoid, "
3059 "(select relname from pg_class where oid = tgconstrrelid) "
3060 " as tgconstrrelname "
3062 "where tgrelid = '%u'::oid",
3063 tbinfo->dobj.catId.oid);
3065 res = PQexec(g_conn, query->data);
3066 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
3068 ntups = PQntuples(res);
3071 * We may have less triggers than recorded due to having ignored
3072 * foreign-key triggers
3074 if (ntups > tbinfo->ntrig)
3076 write_msg(NULL, "expected %d triggers on table \"%s\" but found %d\n",
3077 tbinfo->ntrig, tbinfo->relname, ntups);
3080 i_tableoid = PQfnumber(res, "tableoid");
3081 i_oid = PQfnumber(res, "oid");
3082 i_tgname = PQfnumber(res, "tgname");
3083 i_tgfname = PQfnumber(res, "tgfname");
3084 i_tgtype = PQfnumber(res, "tgtype");
3085 i_tgnargs = PQfnumber(res, "tgnargs");
3086 i_tgargs = PQfnumber(res, "tgargs");
3087 i_tgisconstraint = PQfnumber(res, "tgisconstraint");
3088 i_tgconstrname = PQfnumber(res, "tgconstrname");
3089 i_tgconstrrelid = PQfnumber(res, "tgconstrrelid");
3090 i_tgconstrrelname = PQfnumber(res, "tgconstrrelname");
3091 i_tgdeferrable = PQfnumber(res, "tgdeferrable");
3092 i_tginitdeferred = PQfnumber(res, "tginitdeferred");
3094 tginfo = (TriggerInfo *) malloc(ntups * sizeof(TriggerInfo));
3096 for (j = 0; j < ntups; j++)
3098 tginfo[j].dobj.objType = DO_TRIGGER;
3099 tginfo[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, i_tableoid));
3100 tginfo[j].dobj.catId.oid = atooid(PQgetvalue(res, j, i_oid));
3101 AssignDumpId(&tginfo[j].dobj);
3102 tginfo[j].tgtable = tbinfo;
3103 tginfo[j].tgname = strdup(PQgetvalue(res, j, i_tgname));
3104 tginfo[j].tgfname = strdup(PQgetvalue(res, j, i_tgfname));
3105 tginfo[j].tgtype = atoi(PQgetvalue(res, j, i_tgtype));
3106 tginfo[j].tgnargs = atoi(PQgetvalue(res, j, i_tgnargs));
3107 tginfo[j].tgargs = strdup(PQgetvalue(res, j, i_tgargs));
3108 tginfo[j].tgisconstraint = *(PQgetvalue(res, j, i_tgisconstraint)) == 't';
3109 tginfo[j].tgdeferrable = *(PQgetvalue(res, j, i_tgdeferrable)) == 't';
3110 tginfo[j].tginitdeferred = *(PQgetvalue(res, j, i_tginitdeferred)) == 't';
3112 if (tginfo[j].tgisconstraint)
3114 tginfo[j].tgconstrname = strdup(PQgetvalue(res, j, i_tgconstrname));
3115 tginfo[j].tgconstrrelid = atooid(PQgetvalue(res, j, i_tgconstrrelid));
3116 if (OidIsValid(tginfo[j].tgconstrrelid))
3118 if (PQgetisnull(res, j, i_tgconstrrelname))
3120 write_msg(NULL, "query produced null referenced table name for foreign key trigger \"%s\" on table \"%s\" (OID of table: %u)\n",
3121 tginfo[j].tgname, tbinfo->relname,
3122 tginfo[j].tgconstrrelid);
3125 tginfo[j].tgconstrrelname = strdup(PQgetvalue(res, j, i_tgconstrrelname));
3128 tginfo[j].tgconstrrelname = NULL;
3132 tginfo[j].tgconstrname = NULL;
3133 tginfo[j].tgconstrrelid = InvalidOid;
3134 tginfo[j].tgconstrrelname = NULL;
3141 destroyPQExpBuffer(query);
3146 * get basic information about every procedural language in the system
3148 * numProcLangs is set to the number of langs read in
3150 * NB: this must run after getFuncs() because we assume we can do
3154 getProcLangs(int *numProcLangs)
3159 PQExpBuffer query = createPQExpBuffer();
3160 ProcLangInfo *planginfo;
3165 int i_lanplcallfoid;
3166 int i_lanvalidator = -1;
3169 /* Make sure we are in proper schema */
3170 selectSourceSchema("pg_catalog");
3172 if (g_fout->remoteVersion >= 70100)
3174 appendPQExpBuffer(query, "SELECT tableoid, oid, * FROM pg_language "
3180 appendPQExpBuffer(query, "SELECT "
3181 "(SELECT oid FROM pg_class WHERE relname = 'pg_language') AS tableoid, "
3182 "oid, * FROM pg_language "
3187 res = PQexec(g_conn, query->data);
3188 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
3190 ntups = PQntuples(res);
3192 *numProcLangs = ntups;
3194 planginfo = (ProcLangInfo *) malloc(ntups * sizeof(ProcLangInfo));
3196 i_tableoid = PQfnumber(res, "tableoid");
3197 i_oid = PQfnumber(res, "oid");
3198 i_lanname = PQfnumber(res, "lanname");
3199 i_lanpltrusted = PQfnumber(res, "lanpltrusted");
3200 i_lanplcallfoid = PQfnumber(res, "lanplcallfoid");
3201 if (g_fout->remoteVersion >= 70300)
3203 i_lanvalidator = PQfnumber(res, "lanvalidator");
3204 i_lanacl = PQfnumber(res, "lanacl");
3207 for (i = 0; i < ntups; i++)
3209 planginfo[i].dobj.objType = DO_PROCLANG;
3210 planginfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3211 planginfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3212 AssignDumpId(&planginfo[i].dobj);
3214 planginfo[i].lanname = strdup(PQgetvalue(res, i, i_lanname));
3215 planginfo[i].lanpltrusted = *(PQgetvalue(res, i, i_lanpltrusted)) == 't';
3216 planginfo[i].lanplcallfoid = atooid(PQgetvalue(res, i, i_lanplcallfoid));
3217 if (g_fout->remoteVersion >= 70300)
3219 planginfo[i].lanvalidator = atooid(PQgetvalue(res, i, i_lanvalidator));
3220 planginfo[i].lanacl = strdup(PQgetvalue(res, i, i_lanacl));
3226 planginfo[i].lanvalidator = InvalidOid;
3227 planginfo[i].lanacl = strdup("{=U}");
3229 * We need to make a dependency to ensure the function will
3230 * be dumped first. (In 7.3 and later the regular dependency
3231 * mechanism will handle this for us.)
3233 funcInfo = findFuncByOid(planginfo[i].lanplcallfoid);
3235 addObjectDependency(&planginfo[i].dobj,
3236 funcInfo->dobj.dumpId);
3242 destroyPQExpBuffer(query);
3249 * get basic information about every cast in the system
3251 * numCasts is set to the number of casts read in
3254 getCasts(int *numCasts)
3259 PQExpBuffer query = createPQExpBuffer();
3268 /* Make sure we are in proper schema */
3269 selectSourceSchema("pg_catalog");
3271 if (g_fout->remoteVersion >= 70300)
3273 appendPQExpBuffer(query, "SELECT tableoid, oid, "
3274 "castsource, casttarget, castfunc, castcontext "
3275 "FROM pg_cast ORDER BY 3,4");
3279 appendPQExpBuffer(query, "SELECT 0 as tableoid, p.oid, "
3280 "t1.oid as castsource, t2.oid as casttarget, "
3281 "p.oid as castfunc, 'e' as castcontext "
3282 "FROM pg_type t1, pg_type t2, pg_proc p "
3283 "WHERE p.pronargs = 1 AND "
3284 "p.proargtypes[0] = t1.oid AND "
3285 "p.prorettype = t2.oid AND p.proname = t2.typname "
3289 res = PQexec(g_conn, query->data);
3290 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
3292 ntups = PQntuples(res);
3296 castinfo = (CastInfo *) malloc(ntups * sizeof(CastInfo));
3298 i_tableoid = PQfnumber(res, "tableoid");
3299 i_oid = PQfnumber(res, "oid");
3300 i_castsource = PQfnumber(res, "castsource");
3301 i_casttarget = PQfnumber(res, "casttarget");
3302 i_castfunc = PQfnumber(res, "castfunc");
3303 i_castcontext = PQfnumber(res, "castcontext");
3305 for (i = 0; i < ntups; i++)
3307 castinfo[i].dobj.objType = DO_CAST;
3308 castinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
3309 castinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
3310 AssignDumpId(&castinfo[i].dobj);
3311 castinfo[i].castsource = atooid(PQgetvalue(res, i, i_castsource));
3312 castinfo[i].casttarget = atooid(PQgetvalue(res, i, i_casttarget));
3313 castinfo[i].castfunc = atooid(PQgetvalue(res, i, i_castfunc));
3314 castinfo[i].castcontext = *(PQgetvalue(res, i, i_castcontext));
3316 if (OidIsValid(castinfo[i].castfunc))
3319 * We need to make a dependency to ensure the function will
3320 * be dumped first. (In 7.3 and later the regular dependency
3321 * mechanism will handle this for us.)
3325 funcInfo = findFuncByOid(castinfo[i].castfunc);
3327 addObjectDependency(&castinfo[i].dobj,
3328 funcInfo->dobj.dumpId);
3334 destroyPQExpBuffer(query);
3341 * for each interesting table, read info about its attributes
3342 * (names, types, default values, CHECK constraints, etc)
3344 * This is implemented in a very inefficient way right now, looping
3345 * through the tblinfo and doing a join per table to find the attrs and their
3346 * types. However, because we want type names and so forth to be named
3347 * relative to the schema of each table, we couldn't do it in just one
3348 * query. (Maybe one query per schema?)
3353 getTableAttrs(TableInfo *tblinfo, int numTables)
3358 PQExpBuffer q = createPQExpBuffer();
3363 int i_attstattarget;
3374 for (i = 0; i < numTables; i++)
3376 TableInfo *tbinfo = &tblinfo[i];
3378 /* Don't bother to collect info for sequences */
3379 if (tbinfo->relkind == RELKIND_SEQUENCE)
3382 /* Don't bother with uninteresting tables, either */
3383 if (!tbinfo->interesting)
3387 * Make sure we are in proper schema for this table; this allows
3388 * correct retrieval of formatted type names and default exprs
3390 selectSourceSchema(tbinfo->relnamespace->nspname);
3392 /* find all the user attributes and their types */
3395 * we must read the attribute names in attribute number order!
3396 * because we will use the attnum to index into the attnames array
3397 * later. We actually ask to order by "attrelid, attnum" because
3398 * (at least up to 7.3) the planner is not smart enough to realize
3399 * it needn't re-sort the output of an indexscan on
3400 * pg_attribute_relid_attnum_index.
3403 write_msg(NULL, "finding the columns and types of table \"%s\"\n",
3406 resetPQExpBuffer(q);
3408 if (g_fout->remoteVersion >= 70300)
3410 /* need left join here to not fail on dropped columns ... */
3411 appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, a.attstattarget, a.attstorage, t.typstorage, "
3412 "a.attnotnull, a.atthasdef, a.attisdropped, a.attislocal, "
3413 "pg_catalog.format_type(t.oid,a.atttypmod) as atttypname "
3414 "from pg_catalog.pg_attribute a left join pg_catalog.pg_type t "
3415 "on a.atttypid = t.oid "
3416 "where a.attrelid = '%u'::pg_catalog.oid "
3417 "and a.attnum > 0::pg_catalog.int2 "
3418 "order by a.attrelid, a.attnum",
3419 tbinfo->dobj.catId.oid);
3421 else if (g_fout->remoteVersion >= 70100)
3424 * attstattarget doesn't exist in 7.1. It does exist in 7.2,
3425 * but we don't dump it because we can't tell whether it's
3426 * been explicitly set or was just a default.
3428 appendPQExpBuffer(q, "SELECT a.attnum, a.attname, a.atttypmod, -1 as attstattarget, a.attstorage, t.typstorage, "
3429 "a.attnotnull, a.atthasdef, false as attisdropped, false as attislocal, "
3430 "format_type(t.oid,a.atttypmod) as atttypname "
3431 "from pg_attribute a left join pg_type t "
3432 "on a.atttypid = t.oid "
3433 "where a.attrelid = '%u'::oid "
3434 "and a.attnum > 0::int2 "
3435 "order by a.attrelid, a.attnum",
3436 tbinfo->dobj.catId.oid);
3440 /* format_type not available before 7.1 */
3441 appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, -1 as attstattarget, attstorage, attstorage as typstorage, "
3442 "attnotnull, atthasdef, false as attisdropped, false as attislocal, "
3443 "(select typname from pg_type where oid = atttypid) as atttypname "
3444 "from pg_attribute a "
3445 "where attrelid = '%u'::oid "
3446 "and attnum > 0::int2 "
3447 "order by attrelid, attnum",
3448 tbinfo->dobj.catId.oid);
3451 res = PQexec(g_conn, q->data);
3452 check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
3454 ntups = PQntuples(res);
3456 i_attnum = PQfnumber(res, "attnum");
3457 i_attname = PQfnumber(res, "attname");
3458 i_atttypname = PQfnumber(res, "atttypname");
3459 i_atttypmod = PQfnumber(res, "atttypmod");
3460 i_attstattarget = PQfnumber(res, "attstattarget");
3461 i_attstorage = PQfnumber(res, "attstorage");
3462 i_typstorage = PQfnumber(res, "typstorage");
3463 i_attnotnull = PQfnumber(res, "attnotnull");
3464 i_atthasdef = PQfnumber(res, "atthasdef");
3465 i_attisdropped = PQfnumber(res, "attisdropped");
3466 i_attislocal = PQfnumber(res, "attislocal");
3468 tbinfo->numatts = ntups;
3469 tbinfo->attnames = (char **) malloc(ntups * sizeof(char *));
3470 tbinfo->atttypnames = (char **) malloc(ntups * sizeof(char *));
3471 tbinfo->atttypmod = (int *) malloc(ntups * sizeof(int));
3472 tbinfo->attstattarget = (int *) malloc(ntups * sizeof(int));
3473 tbinfo->attstorage = (char *) malloc(ntups * sizeof(char));
3474 tbinfo->typstorage = (char *) malloc(ntups * sizeof(char));
3475 tbinfo->attisdropped = (bool *) malloc(ntups * sizeof(bool));
3476 tbinfo->attislocal = (bool *) malloc(ntups * sizeof(bool));
3477 tbinfo->attisserial = (bool *) malloc(ntups * sizeof(bool));
3478 tbinfo->notnull = (bool *) malloc(ntups * sizeof(bool));
3479 tbinfo->attrdefs = (AttrDefInfo **) malloc(ntups * sizeof(AttrDefInfo *));
3480 tbinfo->inhAttrs = (bool *) malloc(ntups * sizeof(bool));
3481 tbinfo->inhAttrDef = (bool *) malloc(ntups * sizeof(bool));
3482 tbinfo->inhNotNull = (bool *) malloc(ntups * sizeof(bool));
3483 hasdefaults = false;
3485 for (j = 0; j < ntups; j++)
3487 if (j + 1 != atoi(PQgetvalue(res, j, i_attnum)))
3489 write_msg(NULL, "invalid column numbering in table \"%s\"\n",
3493 tbinfo->attnames[j] = strdup(PQgetvalue(res, j, i_attname));
3494 tbinfo->atttypnames[j] = strdup(PQgetvalue(res, j, i_atttypname));
3495 tbinfo->atttypmod[j] = atoi(PQgetvalue(res, j, i_atttypmod));
3496 tbinfo->attstattarget[j] = atoi(PQgetvalue(res, j, i_attstattarget));
3497 tbinfo->attstorage[j] = *(PQgetvalue(res, j, i_attstorage));
3498 tbinfo->typstorage[j] = *(PQgetvalue(res, j, i_typstorage));
3499 tbinfo->attisdropped[j] = (PQgetvalue(res, j, i_attisdropped)[0] == 't');
3500 tbinfo->attislocal[j] = (PQgetvalue(res, j, i_attislocal)[0] == 't');
3501 tbinfo->attisserial[j] = false; /* fix below */
3502 tbinfo->notnull[j] = (PQgetvalue(res, j, i_attnotnull)[0] == 't');
3503 tbinfo->attrdefs[j] = NULL; /* fix below */
3504 if (PQgetvalue(res, j, i_atthasdef)[0] == 't')
3506 /* these flags will be set in flagInhAttrs() */
3507 tbinfo->inhAttrs[j] = false;
3508 tbinfo->inhAttrDef[j] = false;
3509 tbinfo->inhNotNull[j] = false;
3515 * Get info about column defaults
3519 AttrDefInfo *attrdefs;
3523 write_msg(NULL, "finding default expressions of table \"%s\"\n",
3526 resetPQExpBuffer(q);
3527 if (g_fout->remoteVersion >= 70300)
3529 appendPQExpBuffer(q, "SELECT tableoid, oid, adnum, "
3530 "pg_catalog.pg_get_expr(adbin, adrelid) AS adsrc "
3531 "FROM pg_catalog.pg_attrdef "
3532 "WHERE adrelid = '%u'::pg_catalog.oid",
3533 tbinfo->dobj.catId.oid);
3535 else if (g_fout->remoteVersion >= 70200)
3537 /* 7.2 did not have OIDs in pg_attrdef */
3538 appendPQExpBuffer(q, "SELECT tableoid, 0 as oid, adnum, "
3539 "pg_get_expr(adbin, adrelid) AS adsrc "
3541 "WHERE adrelid = '%u'::oid",
3542 tbinfo->dobj.catId.oid);
3544 else if (g_fout->remoteVersion >= 70100)
3546 /* no pg_get_expr, so must rely on adsrc */
3547 appendPQExpBuffer(q, "SELECT tableoid, oid, adnum, adsrc "
3549 "WHERE adrelid = '%u'::oid",
3550 tbinfo->dobj.catId.oid);
3554 /* no pg_get_expr, no tableoid either */
3555 appendPQExpBuffer(q, "SELECT "
3556 "(SELECT oid FROM pg_class WHERE relname = 'pg_attrdef') AS tableoid, "
3557 "oid, adnum, adsrc "
3559 "WHERE adrelid = '%u'::oid",
3560 tbinfo->dobj.catId.oid);
3562 res = PQexec(g_conn, q->data);
3563 check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
3565 numDefaults = PQntuples(res);
3566 attrdefs = (AttrDefInfo *) malloc(numDefaults * sizeof(AttrDefInfo));
3568 for (j = 0; j < numDefaults; j++)
3572 attrdefs[j].dobj.objType = DO_ATTRDEF;
3573 attrdefs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
3574 attrdefs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
3575 AssignDumpId(&attrdefs[j].dobj);
3576 attrdefs[j].adtable = tbinfo;
3577 attrdefs[j].adnum = adnum = atoi(PQgetvalue(res, j, 2));
3578 attrdefs[j].adef_expr = strdup(PQgetvalue(res, j, 3));
3581 * Defaults on a VIEW must always be dumped as separate
3582 * ALTER TABLE commands. Defaults on regular tables are
3583 * dumped as part of the CREATE TABLE if possible. To check
3584 * if it's safe, we mark the default as needing to appear
3585 * before the CREATE.
3587 if (tbinfo->relkind == RELKIND_VIEW)
3589 attrdefs[j].separate = true;
3590 /* needed in case pre-7.3 DB: */
3591 addObjectDependency(&attrdefs[j].dobj,
3592 tbinfo->dobj.dumpId);
3596 attrdefs[j].separate = false;
3597 addObjectDependency(&tbinfo->dobj,
3598 attrdefs[j].dobj.dumpId);
3601 if (adnum <= 0 || adnum > ntups)
3603 write_msg(NULL, "invalid adnum value %d for table \"%s\"\n",
3604 adnum, tbinfo->relname);
3607 tbinfo->attrdefs[adnum - 1] = &attrdefs[j];
3613 * Get info about table CHECK constraints
3615 if (tbinfo->ncheck > 0)
3617 ConstraintInfo *constrs;
3621 write_msg(NULL, "finding check constraints for table \"%s\"\n",
3624 resetPQExpBuffer(q);
3625 if (g_fout->remoteVersion >= 70400)
3627 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
3628 "pg_catalog.pg_get_constraintdef(oid) AS consrc "
3629 "FROM pg_catalog.pg_constraint "
3630 "WHERE conrelid = '%u'::pg_catalog.oid "
3631 " AND contype = 'c' "
3633 tbinfo->dobj.catId.oid);
3635 else if (g_fout->remoteVersion >= 70300)
3637 /* no pg_get_constraintdef, must use consrc */
3638 appendPQExpBuffer(q, "SELECT tableoid, oid, conname, "
3639 "'CHECK (' || consrc || ')' AS consrc "
3640 "FROM pg_catalog.pg_constraint "
3641 "WHERE conrelid = '%u'::pg_catalog.oid "
3642 " AND contype = 'c' "
3644 tbinfo->dobj.catId.oid);
3646 else if (g_fout->remoteVersion >= 70200)
3648 /* 7.2 did not have OIDs in pg_relcheck */
3649 appendPQExpBuffer(q, "SELECT tableoid, 0 as oid, "
3650 "rcname AS conname, "
3651 "'CHECK (' || rcsrc || ')' AS consrc "
3653 "WHERE rcrelid = '%u'::oid "
3655 tbinfo->dobj.catId.oid);
3657 else if (g_fout->remoteVersion >= 70100)
3659 appendPQExpBuffer(q, "SELECT tableoid, oid, "
3660 "rcname AS conname, "
3661 "'CHECK (' || rcsrc || ')' AS consrc "
3663 "WHERE rcrelid = '%u'::oid "
3665 tbinfo->dobj.catId.oid);
3669 /* no tableoid in 7.0 */
3670 appendPQExpBuffer(q, "SELECT "
3671 "(SELECT oid FROM pg_class WHERE relname = 'pg_relcheck') AS tableoid, "
3672 "oid, rcname AS conname, "
3673 "'CHECK (' || rcsrc || ')' AS consrc "
3675 "WHERE rcrelid = '%u'::oid "
3677 tbinfo->dobj.catId.oid);
3679 res = PQexec(g_conn, q->data);
3680 check_sql_result(res, g_conn, q->data, PGRES_TUPLES_OK);
3682 numConstrs = PQntuples(res);
3683 if (numConstrs != tbinfo->ncheck)
3685 write_msg(NULL, "expected %d check constraints on table \"%s\" but found %d\n",
3686 tbinfo->ncheck, tbinfo->relname, numConstrs);
3687 write_msg(NULL, "(The system catalogs might be corrupted.)\n");
3691 constrs = (ConstraintInfo *) malloc(numConstrs * sizeof(ConstraintInfo));
3692 tbinfo->checkexprs = constrs;
3694 for (j = 0; j < numConstrs; j++)
3696 constrs[j].dobj.objType = DO_CONSTRAINT;
3697 constrs[j].dobj.catId.tableoid = atooid(PQgetvalue(res, j, 0));
3698 constrs[j].dobj.catId.oid = atooid(PQgetvalue(res, j, 1));
3699 AssignDumpId(&constrs[j].dobj);
3700 constrs[j].contable = tbinfo;
3701 constrs[j].condomain = NULL;
3702 constrs[j].contype = 'c';
3703 constrs[j].conname = strdup(PQgetvalue(res, j, 2));
3704 constrs[j].condef = strdup(PQgetvalue(res, j, 3));
3705 constrs[j].conindex = 0;
3706 constrs[j].coninherited = false;
3707 constrs[j].separate = false;
3708 addObjectDependency(&tbinfo->dobj,
3709 constrs[j].dobj.dumpId);
3711 * If the constraint is inherited, this will be detected
3712 * later. We also detect later if the constraint must be
3713 * split out from the table definition.
3720 * Check to see if any columns are serial columns. Our first
3721 * quick filter is that it must be integer or bigint with a
3722 * default. If so, we scan to see if we found a sequence linked
3723 * to this column. If we did, mark the column and sequence
3726 for (j = 0; j < ntups; j++)
3729 * Note assumption that format_type will show these types as
3730 * exactly "integer" and "bigint" regardless of schema path.
3731 * This is correct in 7.3 but needs to be watched.
3733 if (strcmp(tbinfo->atttypnames[j], "integer") != 0 &&
3734 strcmp(tbinfo->atttypnames[j], "bigint") != 0)
3736 if (tbinfo->attrdefs[j] == NULL)
3738 for (k = 0; k < numTables; k++)
3740 TableInfo *seqinfo = &tblinfo[k];
3742 if (OidIsValid(seqinfo->owning_tab) &&
3743 seqinfo->owning_tab == tbinfo->dobj.catId.oid &&
3744 seqinfo->owning_col == j + 1)
3747 * Found a match. Copy the table's interesting and
3748 * dumpable flags to the sequence.
3750 tbinfo->attisserial[j] = true;
3751 seqinfo->interesting = tbinfo->interesting;
3752 seqinfo->dump = tbinfo->dump;
3759 destroyPQExpBuffer(q);
3766 * This routine is used to dump any comments associated with the
3767 * object handed to this routine. The routine takes a constant character
3768 * string for the target part of the comment-creation command, plus
3769 * the namespace and owner of the object (for labeling the ArchiveEntry),
3770 * plus catalog ID and subid which are the lookup key for pg_description,
3771 * plus the dump ID for the object (for setting a dependency).
3772 * If a matching pg_description entry is found, it is dumped.
3775 dumpComment(Archive *fout, const char *target,
3776 const char *namespace, const char *owner,
3777 CatalogId catalogId, int subid, DumpId dumpId)
3783 /* Comments are SCHEMA not data */
3788 * Note we do NOT change source schema here; preserve the caller's
3792 /* Build query to find comment */
3794 query = createPQExpBuffer();
3796 if (fout->remoteVersion >= 70300)
3798 appendPQExpBuffer(query,
3799 "SELECT description FROM pg_catalog.pg_description "
3800 "WHERE classoid = '%u'::pg_catalog.oid and "
3801 "objoid = '%u'::pg_catalog.oid and objsubid = %d",
3802 catalogId.tableoid, catalogId.oid, subid);
3804 else if (fout->remoteVersion >= 70200)
3806 appendPQExpBuffer(query,
3807 "SELECT description FROM pg_description "
3808 "WHERE classoid = '%u'::oid and "
3809 "objoid = '%u'::oid and objsubid = %d",
3810 catalogId.tableoid, catalogId.oid, subid);
3814 /* Note: this will fail to find attribute comments in pre-7.2... */
3815 appendPQExpBuffer(query, "SELECT description FROM pg_description WHERE objoid = '%u'::oid", catalogId.oid);
3820 res = PQexec(g_conn, query->data);
3821 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
3823 /* If a comment exists, build COMMENT ON statement */
3825 if (PQntuples(res) == 1)
3827 i_description = PQfnumber(res, "description");
3828 resetPQExpBuffer(query);
3829 appendPQExpBuffer(query, "COMMENT ON %s IS ", target);
3830 appendStringLiteral(query, PQgetvalue(res, 0, i_description), false);
3831 appendPQExpBuffer(query, ";\n");
3833 ArchiveEntry(fout, nilCatalogId, createDumpId(),
3834 target, namespace, owner,
3835 "COMMENT", query->data, "", NULL,
3841 destroyPQExpBuffer(query);
3845 * dumpTableComment --
3847 * As above, but dump comments for both the specified table (or view)
3848 * and its columns. For speed, we want to do this with only one query.
3851 dumpTableComment(Archive *fout, TableInfo *tbinfo,
3852 const char *reltypename)
3862 /* Comments are SCHEMA not data */
3867 * Note we do NOT change source schema here; preserve the caller's
3871 /* Build query to find comments */
3873 query = createPQExpBuffer();
3874 target = createPQExpBuffer();
3876 if (fout->remoteVersion >= 70300)
3878 appendPQExpBuffer(query, "SELECT description, objsubid FROM pg_catalog.pg_description "
3879 "WHERE classoid = '%u'::pg_catalog.oid and "
3880 "objoid = '%u'::pg_catalog.oid "
3881 "ORDER BY objoid, classoid, objsubid",
3882 tbinfo->dobj.catId.tableoid, tbinfo->dobj.catId.oid);
3884 else if (fout->remoteVersion >= 70200)
3886 appendPQExpBuffer(query, "SELECT description, objsubid FROM pg_description "
3887 "WHERE classoid = '%u'::oid and "
3888 "objoid = '%u'::oid "
3889 "ORDER BY objoid, classoid, objsubid",
3890 tbinfo->dobj.catId.tableoid, tbinfo->dobj.catId.oid);
3894 /* Note: this will fail to find attribute comments in pre-7.2... */
3895 appendPQExpBuffer(query, "SELECT description, 0 as objsubid FROM pg_description WHERE objoid = '%u'::oid",
3896 tbinfo->dobj.catId.oid);
3901 res = PQexec(g_conn, query->data);
3902 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
3904 i_description = PQfnumber(res, "description");
3905 i_objsubid = PQfnumber(res, "objsubid");
3907 /* If comments exist, build COMMENT ON statements */
3909 ntups = PQntuples(res);
3910 for (i = 0; i < ntups; i++)
3912 const char *descr = PQgetvalue(res, i, i_description);
3913 int objsubid = atoi(PQgetvalue(res, i, i_objsubid));
3917 resetPQExpBuffer(target);
3918 appendPQExpBuffer(target, "%s %s", reltypename,
3919 fmtId(tbinfo->relname));
3921 resetPQExpBuffer(query);
3922 appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
3923 appendStringLiteral(query, descr, false);
3924 appendPQExpBuffer(query, ";\n");
3926 ArchiveEntry(fout, nilCatalogId, createDumpId(),
3928 tbinfo->relnamespace->nspname, tbinfo->usename,
3929 "COMMENT", query->data, "", NULL,
3930 &(tbinfo->dobj.dumpId), 1,
3933 else if (objsubid > 0 && objsubid <= tbinfo->numatts)
3935 resetPQExpBuffer(target);
3936 appendPQExpBuffer(target, "COLUMN %s.",
3937 fmtId(tbinfo->relname));
3938 appendPQExpBuffer(target, "%s",
3939 fmtId(tbinfo->attnames[objsubid - 1]));
3941 resetPQExpBuffer(query);
3942 appendPQExpBuffer(query, "COMMENT ON %s IS ", target->data);
3943 appendStringLiteral(query, descr, false);
3944 appendPQExpBuffer(query, ";\n");
3946 ArchiveEntry(fout, nilCatalogId, createDumpId(),
3948 tbinfo->relnamespace->nspname, tbinfo->usename,
3949 "COMMENT", query->data, "", NULL,
3950 &(tbinfo->dobj.dumpId), 1,
3956 destroyPQExpBuffer(query);
3957 destroyPQExpBuffer(target);
3961 * dumpDumpableObject
3963 * This routine and its subsidiaries are responsible for creating
3964 * ArchiveEntries (TOC objects) for each object to be dumped.
3967 dumpDumpableObject(Archive *fout, DumpableObject *dobj)
3969 switch (dobj->objType)
3972 dumpNamespace(fout, (NamespaceInfo *) dobj);
3975 dumpType(fout, (TypeInfo *) dobj);
3978 dumpFunc(fout, (FuncInfo *) dobj);
3981 dumpAgg(fout, (AggInfo *) dobj);
3984 dumpOpr(fout, (OprInfo *) dobj);
3987 dumpOpclass(fout, (OpclassInfo *) dobj);
3990 dumpConversion(fout, (ConvInfo *) dobj);
3993 dumpTable(fout, (TableInfo *) dobj);
3996 dumpAttrDef(fout, (AttrDefInfo *) dobj);
3999 dumpIndex(fout, (IndxInfo *) dobj);
4002 dumpRule(fout, (RuleInfo *) dobj);
4005 dumpTrigger(fout, (TriggerInfo *) dobj);
4008 dumpConstraint(fout, (ConstraintInfo *) dobj);
4010 case DO_FK_CONSTRAINT:
4011 dumpConstraint(fout, (ConstraintInfo *) dobj);
4014 dumpProcLang(fout, (ProcLangInfo *) dobj);
4017 dumpCast(fout, (CastInfo *) dobj);
4020 dumpTableData(fout, (TableDataInfo *) dobj);
4027 * writes out to fout the queries to recreate a user-defined namespace
4030 dumpNamespace(Archive *fout, NamespaceInfo *nspinfo)
4036 /* skip if not to be dumped */
4037 if (!nspinfo->dump || dataOnly)
4040 /* don't dump dummy namespace from pre-7.3 source */
4041 if (strlen(nspinfo->nspname) == 0)
4044 q = createPQExpBuffer();
4045 delq = createPQExpBuffer();
4047 qnspname = strdup(fmtId(nspinfo->nspname));
4050 * If it's the PUBLIC namespace, suppress the CREATE SCHEMA record
4051 * for it, since we expect PUBLIC to exist already in the
4052 * destination database. But do emit ACL in case it's not standard,
4055 * Note that ownership is shown in the AUTHORIZATION clause,
4056 * while the archive entry is listed with empty owner (causing
4057 * it to be emitted with SET SESSION AUTHORIZATION DEFAULT).
4058 * This seems the best way of dealing with schemas owned by
4059 * users without CREATE SCHEMA privilege. Further hacking has
4060 * to be applied for --no-owner mode, though!
4062 if (strcmp(nspinfo->nspname, "public") != 0)
4064 appendPQExpBuffer(delq, "DROP SCHEMA %s;\n", qnspname);
4066 appendPQExpBuffer(q, "CREATE SCHEMA %s AUTHORIZATION %s;\n",
4067 qnspname, fmtId(nspinfo->usename));
4069 ArchiveEntry(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId,
4072 "SCHEMA", q->data, delq->data, NULL,
4073 nspinfo->dobj.dependencies, nspinfo->dobj.nDeps,
4077 /* Dump Schema Comments */
4078 resetPQExpBuffer(q);
4079 appendPQExpBuffer(q, "SCHEMA %s", qnspname);
4080 dumpComment(fout, q->data,
4081 NULL, nspinfo->usename,
4082 nspinfo->dobj.catId, 0, nspinfo->dobj.dumpId);
4084 dumpACL(fout, nspinfo->dobj.catId, nspinfo->dobj.dumpId, "SCHEMA",
4085 qnspname, nspinfo->nspname, NULL,
4086 nspinfo->usename, nspinfo->nspacl);
4090 destroyPQExpBuffer(q);
4091 destroyPQExpBuffer(delq);
4096 * writes out to fout the queries to recreate a user-defined type
4099 dumpType(Archive *fout, TypeInfo *tinfo)
4101 /* Dump only types in dumpable namespaces */
4102 if (!tinfo->typnamespace->dump || dataOnly)
4105 /* skip complex types, except for standalone composite types */
4106 if (OidIsValid(tinfo->typrelid) && tinfo->typrelkind != 'c')
4109 /* skip undefined placeholder types */
4110 if (!tinfo->isDefined)
4113 /* skip all array types that start w/ underscore */
4114 if ((tinfo->typname[0] == '_') &&
4115 OidIsValid(tinfo->typelem))
4118 /* Dump out in proper style */
4119 if (tinfo->typtype == 'b')
4120 dumpBaseType(fout, tinfo);
4121 else if (tinfo->typtype == 'd')
4122 dumpDomain(fout, tinfo);
4123 else if (tinfo->typtype == 'c')
4124 dumpCompositeType(fout, tinfo);
4129 * writes out to fout the queries to recreate a user-defined base type
4132 dumpBaseType(Archive *fout, TypeInfo *tinfo)
4134 PQExpBuffer q = createPQExpBuffer();
4135 PQExpBuffer delq = createPQExpBuffer();
4136 PQExpBuffer query = createPQExpBuffer();
4154 /* Set proper schema search path so regproc references list correctly */
4155 selectSourceSchema(tinfo->typnamespace->nspname);
4157 /* Fetch type-specific details */
4158 if (fout->remoteVersion >= 70400)
4160 appendPQExpBuffer(query, "SELECT typlen, "
4161 "typinput, typoutput, typreceive, typsend, "
4162 "typinput::pg_catalog.oid as typinputoid, "
4163 "typoutput::pg_catalog.oid as typoutputoid, "
4164 "typreceive::pg_catalog.oid as typreceiveoid, "
4165 "typsend::pg_catalog.oid as typsendoid, "
4166 "typdelim, typdefault, typbyval, typalign, "
4168 "FROM pg_catalog.pg_type "
4169 "WHERE oid = '%u'::pg_catalog.oid",
4170 tinfo->dobj.catId.oid);
4172 else if (fout->remoteVersion >= 70300)
4174 appendPQExpBuffer(query, "SELECT typlen, "
4175 "typinput, typoutput, "
4176 "'-' as typreceive, '-' as typsend, "
4177 "typinput::pg_catalog.oid as typinputoid, "
4178 "typoutput::pg_catalog.oid as typoutputoid, "
4179 "0 as typreceiveoid, 0 as typsendoid, "
4180 "typdelim, typdefault, typbyval, typalign, "
4182 "FROM pg_catalog.pg_type "
4183 "WHERE oid = '%u'::pg_catalog.oid",
4184 tinfo->dobj.catId.oid);
4186 else if (fout->remoteVersion >= 70100)
4189 * Note: although pre-7.3 catalogs contain typreceive and typsend,
4190 * ignore them because they are not right.
4192 appendPQExpBuffer(query, "SELECT typlen, "
4193 "typinput, typoutput, "
4194 "'-' as typreceive, '-' as typsend, "
4195 "typinput::oid as typinputoid, "
4196 "typoutput::oid as typoutputoid, "
4197 "0 as typreceiveoid, 0 as typsendoid, "
4198 "typdelim, typdefault, typbyval, typalign, "
4201 "WHERE oid = '%u'::oid",
4202 tinfo->dobj.catId.oid);
4206 appendPQExpBuffer(query, "SELECT typlen, "
4207 "typinput, typoutput, "
4208 "'-' as typreceive, '-' as typsend, "
4209 "typinput::oid as typinputoid, "
4210 "typoutput::oid as typoutputoid, "
4211 "0 as typreceiveoid, 0 as typsendoid, "
4212 "typdelim, typdefault, typbyval, typalign, "
4213 "'p'::char as typstorage "
4215 "WHERE oid = '%u'::oid",
4216 tinfo->dobj.catId.oid);
4219 res = PQexec(g_conn, query->data);
4220 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
4222 /* Expecting a single result only */
4223 ntups = PQntuples(res);
4226 write_msg(NULL, "Got %d rows instead of one from: %s",
4227 ntups, query->data);
4231 typlen = PQgetvalue(res, 0, PQfnumber(res, "typlen"));
4232 typinput = PQgetvalue(res, 0, PQfnumber(res, "typinput"));
4233 typoutput = PQgetvalue(res, 0, PQfnumber(res, "typoutput"));
4234 typreceive = PQgetvalue(res, 0, PQfnumber(res, "typreceive"));
4235 typsend = PQgetvalue(res, 0, PQfnumber(res, "typsend"));
4236 typinputoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typinputoid")));
4237 typoutputoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typoutputoid")));
4238 typreceiveoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typreceiveoid")));
4239 typsendoid = atooid(PQgetvalue(res, 0, PQfnumber(res, "typsendoid")));
4240 typdelim = PQgetvalue(res, 0, PQfnumber(res, "typdelim"));
4241 if (PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
4244 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
4245 typbyval = PQgetvalue(res, 0, PQfnumber(res, "typbyval"));
4246 typalign = PQgetvalue(res, 0, PQfnumber(res, "typalign"));
4247 typstorage = PQgetvalue(res, 0, PQfnumber(res, "typstorage"));
4250 * DROP must be fully qualified in case same name appears in
4253 appendPQExpBuffer(delq, "DROP TYPE %s.",
4254 fmtId(tinfo->typnamespace->nspname));
4255 appendPQExpBuffer(delq, "%s CASCADE;\n",
4256 fmtId(tinfo->typname));
4258 appendPQExpBuffer(q,
4259 "CREATE TYPE %s (\n"
4260 " INTERNALLENGTH = %s",
4261 fmtId(tinfo->typname),
4262 (strcmp(typlen, "-1") == 0) ? "variable" : typlen);
4264 if (fout->remoteVersion >= 70300)
4266 /* regproc result is correctly quoted in 7.3 */
4267 appendPQExpBuffer(q, ",\n INPUT = %s", typinput);
4268 appendPQExpBuffer(q, ",\n OUTPUT = %s", typoutput);
4269 if (OidIsValid(typreceiveoid))
4270 appendPQExpBuffer(q, ",\n RECEIVE = %s", typreceive);
4271 if (OidIsValid(typsendoid))
4272 appendPQExpBuffer(q, ",\n SEND = %s", typsend);
4276 /* regproc delivers an unquoted name before 7.3 */
4277 /* cannot combine these because fmtId uses static result area */
4278 appendPQExpBuffer(q, ",\n INPUT = %s", fmtId(typinput));
4279 appendPQExpBuffer(q, ",\n OUTPUT = %s", fmtId(typoutput));
4280 /* no chance that receive/send need be printed */
4283 if (typdefault != NULL)
4285 appendPQExpBuffer(q, ",\n DEFAULT = ");
4286 appendStringLiteral(q, typdefault, true);
4293 /* reselect schema in case changed by function dump */
4294 selectSourceSchema(tinfo->typnamespace->nspname);
4295 elemType = getFormattedTypeName(tinfo->typelem, zeroAsOpaque);
4296 appendPQExpBuffer(q, ",\n ELEMENT = %s", elemType);
4300 if (typdelim && strcmp(typdelim, ",") != 0)
4302 appendPQExpBuffer(q, ",\n DELIMITER = ");
4303 appendStringLiteral(q, typdelim, true);
4306 if (strcmp(typalign, "c") == 0)
4307 appendPQExpBuffer(q, ",\n ALIGNMENT = char");
4308 else if (strcmp(typalign, "s") == 0)
4309 appendPQExpBuffer(q, ",\n ALIGNMENT = int2");
4310 else if (strcmp(typalign, "i") == 0)
4311 appendPQExpBuffer(q, ",\n ALIGNMENT = int4");
4312 else if (strcmp(typalign, "d") == 0)
4313 appendPQExpBuffer(q, ",\n ALIGNMENT = double");
4315 if (strcmp(typstorage, "p") == 0)
4316 appendPQExpBuffer(q, ",\n STORAGE = plain");
4317 else if (strcmp(typstorage, "e") == 0)
4318 appendPQExpBuffer(q, ",\n STORAGE = external");
4319 else if (strcmp(typstorage, "x") == 0)
4320 appendPQExpBuffer(q, ",\n STORAGE = extended");
4321 else if (strcmp(typstorage, "m") == 0)
4322 appendPQExpBuffer(q, ",\n STORAGE = main");
4324 if (strcmp(typbyval, "t") == 0)
4325 appendPQExpBuffer(q, ",\n PASSEDBYVALUE");
4327 appendPQExpBuffer(q, "\n);\n");
4329 ArchiveEntry(fout, tinfo->dobj.catId, tinfo->dobj.dumpId,
4331 tinfo->typnamespace->nspname,
4333 "TYPE", q->data, delq->data, NULL,
4334 tinfo->dobj.dependencies, tinfo->dobj.nDeps,
4337 /* Dump Type Comments */
4338 resetPQExpBuffer(q);
4340 appendPQExpBuffer(q, "TYPE %s", fmtId(tinfo->typname));
4341 dumpComment(fout, q->data,
4342 tinfo->typnamespace->nspname, tinfo->usename,
4343 tinfo->dobj.catId, 0, tinfo->dobj.dumpId);
4346 destroyPQExpBuffer(q);
4347 destroyPQExpBuffer(delq);
4348 destroyPQExpBuffer(query);
4353 * writes out to fout the queries to recreate a user-defined domain
4356 dumpDomain(Archive *fout, TypeInfo *tinfo)
4358 PQExpBuffer q = createPQExpBuffer();
4359 PQExpBuffer delq = createPQExpBuffer();
4360 PQExpBuffer query = createPQExpBuffer();
4368 /* Set proper schema search path so type references list correctly */
4369 selectSourceSchema(tinfo->typnamespace->nspname);
4371 /* Fetch domain specific details */
4372 /* We assume here that remoteVersion must be at least 70300 */
4373 appendPQExpBuffer(query, "SELECT typnotnull, "
4374 "pg_catalog.format_type(typbasetype, typtypmod) as typdefn, "
4376 "FROM pg_catalog.pg_type "
4377 "WHERE oid = '%u'::pg_catalog.oid",
4378 tinfo->dobj.catId.oid);
4380 res = PQexec(g_conn, query->data);
4381 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
4383 /* Expecting a single result only */
4384 ntups = PQntuples(res);
4387 write_msg(NULL, "Got %d rows instead of one from: %s",
4388 ntups, query->data);
4392 typnotnull = PQgetvalue(res, 0, PQfnumber(res, "typnotnull"));
4393 typdefn = PQgetvalue(res, 0, PQfnumber(res, "typdefn"));
4394 if (PQgetisnull(res, 0, PQfnumber(res, "typdefault")))
4397 typdefault = PQgetvalue(res, 0, PQfnumber(res, "typdefault"));
4399 appendPQExpBuffer(q,
4400 "CREATE DOMAIN %s AS %s",
4401 fmtId(tinfo->typname),
4404 if (typnotnull[0] == 't')
4405 appendPQExpBuffer(q, " NOT NULL");
4408 appendPQExpBuffer(q, " DEFAULT %s", typdefault);
4413 * Add any CHECK constraints for the domain
4415 for (i = 0; i < tinfo->nDomChecks; i++)
4417 ConstraintInfo *domcheck = &(tinfo->domChecks[i]);
4419 if (!domcheck->separate)
4420 appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
4421 fmtId(domcheck->conname), domcheck->condef);
4424 appendPQExpBuffer(q, ";\n");
4427 * DROP must be fully qualified in case same name appears in
4430 appendPQExpBuffer(delq, "DROP DOMAIN %s.",
4431 fmtId(tinfo->typnamespace->nspname));
4432 appendPQExpBuffer(delq, "%s;\n",
4433 fmtId(tinfo->typname));
4435 ArchiveEntry(fout, tinfo->dobj.catId, tinfo->dobj.dumpId,
4437 tinfo->typnamespace->nspname,
4439 "DOMAIN", q->data, delq->data, NULL,
4440 tinfo->dobj.dependencies, tinfo->dobj.nDeps,
4443 /* Dump Domain Comments */
4444 resetPQExpBuffer(q);
4446 appendPQExpBuffer(q, "DOMAIN %s", fmtId(tinfo->typname));
4447 dumpComment(fout, q->data,
4448 tinfo->typnamespace->nspname, tinfo->usename,
4449 tinfo->dobj.catId, 0, tinfo->dobj.dumpId);
4451 destroyPQExpBuffer(q);
4452 destroyPQExpBuffer(delq);
4453 destroyPQExpBuffer(query);
4458 * writes out to fout the queries to recreate a user-defined stand-alone
4462 dumpCompositeType(Archive *fout, TypeInfo *tinfo)
4464 PQExpBuffer q = createPQExpBuffer();
4465 PQExpBuffer delq = createPQExpBuffer();
4466 PQExpBuffer query = createPQExpBuffer();
4473 /* Set proper schema search path so type references list correctly */
4474 selectSourceSchema(tinfo->typnamespace->nspname);
4476 /* Fetch type specific details */
4477 /* We assume here that remoteVersion must be at least 70300 */
4479 appendPQExpBuffer(query, "SELECT a.attname, "
4480 "pg_catalog.format_type(a.atttypid, a.atttypmod) as atttypdefn "
4481 "FROM pg_catalog.pg_type t, pg_catalog.pg_attribute a "
4482 "WHERE t.oid = '%u'::pg_catalog.oid "
4483 "AND a.attrelid = t.typrelid "
4484 "AND NOT a.attisdropped "
4485 "ORDER BY a.attnum ",
4486 tinfo->dobj.catId.oid);
4488 res = PQexec(g_conn, query->data);
4489 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
4491 /* Expecting at least a single result */
4492 ntups = PQntuples(res);
4495 write_msg(NULL, "query yielded no rows: %s\n", query->data);
4499 i_attname = PQfnumber(res, "attname");
4500 i_atttypdefn = PQfnumber(res, "atttypdefn");
4502 appendPQExpBuffer(q, "CREATE TYPE %s AS (",
4503 fmtId(tinfo->typname));
4505 for (i = 0; i < ntups; i++)
4510 attname = PQgetvalue(res, i, i_attname);
4511 atttypdefn = PQgetvalue(res, i, i_atttypdefn);
4513 appendPQExpBuffer(q, "\n\t%s %s", fmtId(attname), atttypdefn);
4515 appendPQExpBuffer(q, ",");
4517 appendPQExpBuffer(q, "\n);\n");
4520 * DROP must be fully qualified in case same name appears in
4523 appendPQExpBuffer(delq, "DROP TYPE %s.",
4524 fmtId(tinfo->typnamespace->nspname));
4525 appendPQExpBuffer(delq, "%s;\n",
4526 fmtId(tinfo->typname));
4528 ArchiveEntry(fout, tinfo->dobj.catId, tinfo->dobj.dumpId,
4530 tinfo->typnamespace->nspname,
4532 "TYPE", q->data, delq->data, NULL,
4533 tinfo->dobj.dependencies, tinfo->dobj.nDeps,
4537 /* Dump Type Comments */
4538 resetPQExpBuffer(q);
4540 appendPQExpBuffer(q, "TYPE %s", fmtId(tinfo->typname));
4541 dumpComment(fout, q->data,
4542 tinfo->typnamespace->nspname, tinfo->usename,
4543 tinfo->dobj.catId, 0, tinfo->dobj.dumpId);
4546 destroyPQExpBuffer(q);
4547 destroyPQExpBuffer(delq);
4548 destroyPQExpBuffer(query);
4553 * writes out to fout the queries to recreate a user-defined
4554 * procedural language
4557 dumpProcLang(Archive *fout, ProcLangInfo *plang)
4563 FuncInfo *validatorInfo = NULL;
4569 * Current theory is to dump PLs iff their underlying functions
4570 * will be dumped (are in a dumpable namespace, or have a
4571 * non-system OID in pre-7.3 databases). Actually, we treat the
4572 * PL itself as being in the underlying function's namespace,
4573 * though it isn't really. This avoids searchpath problems for
4574 * the HANDLER clause.
4576 * If the underlying function is in the pg_catalog namespace,
4577 * we won't have loaded it into finfo[] at all; therefore,
4578 * treat failure to find it in finfo[] as indicating we shouldn't
4579 * dump it, not as an error condition. Ditto for the validator.
4582 funcInfo = findFuncByOid(plang->lanplcallfoid);
4583 if (funcInfo == NULL)
4586 if (!funcInfo->pronamespace->dump)
4589 if (OidIsValid(plang->lanvalidator))
4591 validatorInfo = findFuncByOid(plang->lanvalidator);
4592 if (validatorInfo == NULL)
4596 defqry = createPQExpBuffer();
4597 delqry = createPQExpBuffer();
4599 qlanname = strdup(fmtId(plang->lanname));
4601 appendPQExpBuffer(delqry, "DROP PROCEDURAL LANGUAGE %s;\n",
4604 appendPQExpBuffer(defqry, "CREATE %sPROCEDURAL LANGUAGE %s",
4605 plang->lanpltrusted ? "TRUSTED " : "",
4607 appendPQExpBuffer(defqry, " HANDLER %s",
4608 fmtId(funcInfo->proname));
4609 if (OidIsValid(plang->lanvalidator))
4611 appendPQExpBuffer(defqry, " VALIDATOR ");
4612 /* Cope with possibility that validator is in different schema */
4613 if (validatorInfo->pronamespace != funcInfo->pronamespace)
4614 appendPQExpBuffer(defqry, "%s.",
4615 fmtId(validatorInfo->pronamespace->nspname));
4616 appendPQExpBuffer(defqry, "%s",
4617 fmtId(validatorInfo->proname));
4619 appendPQExpBuffer(defqry, ";\n");
4621 ArchiveEntry(fout, plang->dobj.catId, plang->dobj.dumpId,
4623 funcInfo->pronamespace->nspname, "",
4624 "PROCEDURAL LANGUAGE",
4625 defqry->data, delqry->data, NULL,
4626 plang->dobj.dependencies, plang->dobj.nDeps,
4629 /* Dump Proc Lang Comments */
4630 resetPQExpBuffer(defqry);
4632 appendPQExpBuffer(defqry, "LANGUAGE %s", qlanname);
4633 dumpComment(fout, defqry->data,
4635 plang->dobj.catId, 0, plang->dobj.dumpId);
4637 if (plang->lanpltrusted)
4638 dumpACL(fout, plang->dobj.catId, plang->dobj.dumpId, "LANGUAGE",
4639 qlanname, plang->lanname,
4640 funcInfo->pronamespace->nspname,
4641 NULL, plang->lanacl);
4645 destroyPQExpBuffer(defqry);
4646 destroyPQExpBuffer(delqry);
4650 * format_function_signature: generate function name and argument list
4652 * The argument type names are qualified if needed. The function name
4653 * is never qualified.
4655 * argnames may be NULL if no names are available.
4658 format_function_signature(FuncInfo *finfo, char **argnames,
4664 initPQExpBuffer(&fn);
4666 appendPQExpBuffer(&fn, "%s(", fmtId(finfo->proname));
4668 appendPQExpBuffer(&fn, "%s(", finfo->proname);
4669 for (j = 0; j < finfo->nargs; j++)
4674 typname = getFormattedTypeName(finfo->argtypes[j], zeroAsOpaque);
4676 argname = argnames ? argnames[j] : (char *) NULL;
4677 if (argname && argname[0] == '\0')
4680 appendPQExpBuffer(&fn, "%s%s%s%s",
4681 (j > 0) ? ", " : "",
4682 argname ? fmtId(argname) : "",
4687 appendPQExpBuffer(&fn, ")");
4694 * dump out one function
4697 dumpFunc(Archive *fout, FuncInfo *finfo)
4716 char **argnamearray = NULL;
4718 /* Dump only funcs in dumpable namespaces */
4719 if (!finfo->pronamespace->dump || dataOnly)
4722 query = createPQExpBuffer();
4723 q = createPQExpBuffer();
4724 delqry = createPQExpBuffer();
4725 asPart = createPQExpBuffer();
4727 /* Set proper schema search path so type references list correctly */
4728 selectSourceSchema(finfo->pronamespace->nspname);
4730 /* Fetch function-specific details */
4731 if (g_fout->remoteVersion >= 70500)
4733 appendPQExpBuffer(query,
4734 "SELECT proretset, prosrc, probin, "
4736 "provolatile, proisstrict, prosecdef, "
4737 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) as lanname "
4738 "FROM pg_catalog.pg_proc "
4739 "WHERE oid = '%u'::pg_catalog.oid",
4740 finfo->dobj.catId.oid);
4742 else if (g_fout->remoteVersion >= 70300)
4744 appendPQExpBuffer(query,
4745 "SELECT proretset, prosrc, probin, "
4746 "null::text as proargnames, "
4747 "provolatile, proisstrict, prosecdef, "
4748 "(SELECT lanname FROM pg_catalog.pg_language WHERE oid = prolang) as lanname "
4749 "FROM pg_catalog.pg_proc "
4750 "WHERE oid = '%u'::pg_catalog.oid",
4751 finfo->dobj.catId.oid);
4753 else if (g_fout->remoteVersion >= 70100)
4755 appendPQExpBuffer(query,
4756 "SELECT proretset, prosrc, probin, "
4757 "null::text as proargnames, "
4758 "case when proiscachable then 'i' else 'v' end as provolatile, "
4760 "'f'::boolean as prosecdef, "
4761 "(SELECT lanname FROM pg_language WHERE oid = prolang) as lanname "
4763 "WHERE oid = '%u'::oid",
4764 finfo->dobj.catId.oid);
4768 appendPQExpBuffer(query,
4769 "SELECT proretset, prosrc, probin, "
4770 "null::text as proargnames, "
4771 "case when proiscachable then 'i' else 'v' end as provolatile, "
4772 "'f'::boolean as proisstrict, "
4773 "'f'::boolean as prosecdef, "
4774 "(SELECT lanname FROM pg_language WHERE oid = prolang) as lanname "
4776 "WHERE oid = '%u'::oid",
4777 finfo->dobj.catId.oid);
4780 res = PQexec(g_conn, query->data);
4781 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
4783 /* Expecting a single result only */
4784 ntups = PQntuples(res);
4787 write_msg(NULL, "Got %d rows instead of one from: %s",
4788 ntups, query->data);
4792 proretset = PQgetvalue(res, 0, PQfnumber(res, "proretset"));
4793 prosrc = PQgetvalue(res, 0, PQfnumber(res, "prosrc"));
4794 probin = PQgetvalue(res, 0, PQfnumber(res, "probin"));
4795 proargnames = PQgetvalue(res, 0, PQfnumber(res, "proargnames"));
4796 provolatile = PQgetvalue(res, 0, PQfnumber(res, "provolatile"));
4797 proisstrict = PQgetvalue(res, 0, PQfnumber(res, "proisstrict"));
4798 prosecdef = PQgetvalue(res, 0, PQfnumber(res, "prosecdef"));
4799 lanname = PQgetvalue(res, 0, PQfnumber(res, "lanname"));
4802 * See backend/commands/define.c for details of how the 'AS' clause is
4805 if (strcmp(probin, "-") != 0)
4807 appendPQExpBuffer(asPart, "AS ");
4808 appendStringLiteral(asPart, probin, true);
4809 if (strcmp(prosrc, "-") != 0)
4811 appendPQExpBuffer(asPart, ", ");
4812 appendStringLiteral(asPart, prosrc, false);
4817 if (strcmp(prosrc, "-") != 0)
4819 appendPQExpBuffer(asPart, "AS ");
4820 appendStringLiteral(asPart, prosrc, false);
4824 if (proargnames && *proargnames)
4828 if (!parsePGArray(proargnames, &argnamearray, &nitems) ||
4829 nitems != finfo->nargs)
4831 write_msg(NULL, "WARNING: could not parse proargnames array\n");
4834 argnamearray = NULL;
4838 funcsig = format_function_signature(finfo, argnamearray, true);
4839 funcsig_tag = format_function_signature(finfo, NULL, false);
4842 * DROP must be fully qualified in case same name appears in
4845 appendPQExpBuffer(delqry, "DROP FUNCTION %s.%s;\n",
4846 fmtId(finfo->pronamespace->nspname),
4849 rettypename = getFormattedTypeName(finfo->prorettype, zeroAsOpaque);
4851 appendPQExpBuffer(q, "CREATE FUNCTION %s ", funcsig);
4852 appendPQExpBuffer(q, "RETURNS %s%s\n %s\n LANGUAGE %s",
4853 (proretset[0] == 't') ? "SETOF " : "",
4860 if (provolatile[0] != PROVOLATILE_VOLATILE)
4862 if (provolatile[0] == PROVOLATILE_IMMUTABLE)
4863 appendPQExpBuffer(q, " IMMUTABLE");
4864 else if (provolatile[0] == PROVOLATILE_STABLE)
4865 appendPQExpBuffer(q, " STABLE");
4866 else if (provolatile[0] != PROVOLATILE_VOLATILE)
4868 write_msg(NULL, "unrecognized provolatile value for function \"%s\"\n",
4874 if (proisstrict[0] == 't')
4875 appendPQExpBuffer(q, " STRICT");
4877 if (prosecdef[0] == 't')
4878 appendPQExpBuffer(q, " SECURITY DEFINER");
4880 appendPQExpBuffer(q, ";\n");
4882 ArchiveEntry(fout, finfo->dobj.catId, finfo->dobj.dumpId,
4884 finfo->pronamespace->nspname,
4886 "FUNCTION", q->data, delqry->data, NULL,
4887 finfo->dobj.dependencies, finfo->dobj.nDeps,
4890 /* Dump Function Comments */
4891 resetPQExpBuffer(q);
4892 appendPQExpBuffer(q, "FUNCTION %s", funcsig);
4893 dumpComment(fout, q->data,
4894 finfo->pronamespace->nspname, finfo->usename,
4895 finfo->dobj.catId, 0, finfo->dobj.dumpId);
4897 dumpACL(fout, finfo->dobj.catId, finfo->dobj.dumpId, "FUNCTION",
4898 funcsig, funcsig_tag,
4899 finfo->pronamespace->nspname,
4900 finfo->usename, finfo->proacl);
4904 destroyPQExpBuffer(query);
4905 destroyPQExpBuffer(q);
4906 destroyPQExpBuffer(delqry);
4907 destroyPQExpBuffer(asPart);
4916 * Dump a user-defined cast
4919 dumpCast(Archive *fout, CastInfo *cast)
4923 PQExpBuffer castsig;
4924 FuncInfo *funcInfo = NULL;
4925 TypeInfo *sourceInfo;
4926 TypeInfo *targetInfo;
4931 if (OidIsValid(cast->castfunc))
4933 funcInfo = findFuncByOid(cast->castfunc);
4934 if (funcInfo == NULL)
4939 * As per discussion we dump casts if one or more of the underlying
4940 * objects (the conversion function and the two data types) are not
4941 * builtin AND if all of the non-builtin objects namespaces are
4942 * included in the dump. Builtin meaning, the namespace name does
4943 * not start with "pg_".
4945 sourceInfo = findTypeByOid(cast->castsource);
4946 targetInfo = findTypeByOid(cast->casttarget);
4948 if (sourceInfo == NULL || targetInfo == NULL)
4952 * Skip this cast if all objects are from pg_
4954 if ((funcInfo == NULL || strncmp(funcInfo->pronamespace->nspname, "pg_", 3) == 0) &&
4955 strncmp(sourceInfo->typnamespace->nspname, "pg_", 3) == 0 &&
4956 strncmp(targetInfo->typnamespace->nspname, "pg_", 3) == 0)
4960 * Skip cast if function isn't from pg_ and that namespace is
4964 strncmp(funcInfo->pronamespace->nspname, "pg_", 3) != 0 &&
4965 !funcInfo->pronamespace->dump)
4969 * Same for the Source type
4971 if (strncmp(sourceInfo->typnamespace->nspname, "pg_", 3) != 0 &&
4972 !sourceInfo->typnamespace->dump)
4976 * and the target type.
4978 if (strncmp(targetInfo->typnamespace->nspname, "pg_", 3) != 0 &&
4979 !targetInfo->typnamespace->dump)
4982 /* Make sure we are in proper schema (needed for getFormattedTypeName) */
4983 selectSourceSchema("pg_catalog");
4985 defqry = createPQExpBuffer();
4986 delqry = createPQExpBuffer();
4987 castsig = createPQExpBuffer();
4989 appendPQExpBuffer(delqry, "DROP CAST (%s AS %s);\n",
4990 getFormattedTypeName(cast->castsource, zeroAsNone),
4991 getFormattedTypeName(cast->casttarget, zeroAsNone));
4993 appendPQExpBuffer(defqry, "CREATE CAST (%s AS %s) ",
4994 getFormattedTypeName(cast->castsource, zeroAsNone),
4995 getFormattedTypeName(cast->casttarget, zeroAsNone));
4997 if (!OidIsValid(cast->castfunc))
4998 appendPQExpBuffer(defqry, "WITHOUT FUNCTION");
5000 appendPQExpBuffer(defqry, "WITH FUNCTION %s",
5001 format_function_signature(funcInfo, NULL, true));
5003 if (cast->castcontext == 'a')
5004 appendPQExpBuffer(defqry, " AS ASSIGNMENT");
5005 else if (cast->castcontext == 'i')
5006 appendPQExpBuffer(defqry, " AS IMPLICIT");
5007 appendPQExpBuffer(defqry, ";\n");
5009 appendPQExpBuffer(castsig, "CAST (%s AS %s)",
5010 getFormattedTypeName(cast->castsource, zeroAsNone),
5011 getFormattedTypeName(cast->casttarget, zeroAsNone));
5013 ArchiveEntry(fout, cast->dobj.catId, cast->dobj.dumpId,
5015 sourceInfo->typnamespace->nspname, "",
5016 "CAST", defqry->data, delqry->data, NULL,
5017 cast->dobj.dependencies, cast->dobj.nDeps,
5020 /* Dump Cast Comments */
5021 resetPQExpBuffer(defqry);
5022 appendPQExpBuffer(defqry, "CAST (%s AS %s)",
5023 getFormattedTypeName(cast->castsource, zeroAsNone),
5024 getFormattedTypeName(cast->casttarget, zeroAsNone));
5025 dumpComment(fout, defqry->data,
5027 cast->dobj.catId, 0, cast->dobj.dumpId);
5029 destroyPQExpBuffer(defqry);
5030 destroyPQExpBuffer(delqry);
5031 destroyPQExpBuffer(castsig);
5036 * write out a single operator definition
5039 dumpOpr(Archive *fout, OprInfo *oprinfo)
5045 PQExpBuffer details;
5076 /* Dump only operators in dumpable namespaces */
5077 if (!oprinfo->oprnamespace->dump || dataOnly)
5081 * some operators are invalid because they were the result of user
5082 * defining operators before commutators exist
5084 if (!OidIsValid(oprinfo->oprcode))
5087 query = createPQExpBuffer();
5088 q = createPQExpBuffer();
5089 delq = createPQExpBuffer();
5090 oprid = createPQExpBuffer();
5091 details = createPQExpBuffer();
5093 /* Make sure we are in proper schema so regoperator works correctly */
5094 selectSourceSchema(oprinfo->oprnamespace->nspname);
5096 if (g_fout->remoteVersion >= 70300)
5098 appendPQExpBuffer(query, "SELECT oprkind, "
5099 "oprcode::pg_catalog.regprocedure, "
5100 "oprleft::pg_catalog.regtype, "
5101 "oprright::pg_catalog.regtype, "
5102 "oprcom::pg_catalog.regoperator, "
5103 "oprnegate::pg_catalog.regoperator, "
5104 "oprrest::pg_catalog.regprocedure, "
5105 "oprjoin::pg_catalog.regprocedure, "
5107 "oprlsortop::pg_catalog.regoperator, "
5108 "oprrsortop::pg_catalog.regoperator, "
5109 "oprltcmpop::pg_catalog.regoperator, "
5110 "oprgtcmpop::pg_catalog.regoperator "
5111 "from pg_catalog.pg_operator "
5112 "where oid = '%u'::pg_catalog.oid",
5113 oprinfo->dobj.catId.oid);
5115 else if (g_fout->remoteVersion >= 70100)
5117 appendPQExpBuffer(query, "SELECT oprkind, oprcode, "
5118 "CASE WHEN oprleft = 0 THEN '-' "
5119 "ELSE format_type(oprleft, NULL) END as oprleft, "
5120 "CASE WHEN oprright = 0 THEN '-' "
5121 "ELSE format_type(oprright, NULL) END as oprright, "
5122 "oprcom, oprnegate, oprrest, oprjoin, "
5123 "oprcanhash, oprlsortop, oprrsortop, "
5124 "0 as oprltcmpop, 0 as oprgtcmpop "
5126 "where oid = '%u'::oid",
5127 oprinfo->dobj.catId.oid);
5131 appendPQExpBuffer(query, "SELECT oprkind, oprcode, "
5132 "CASE WHEN oprleft = 0 THEN '-'::name "
5133 "ELSE (select typname from pg_type where oid = oprleft) END as oprleft, "
5134 "CASE WHEN oprright = 0 THEN '-'::name "
5135 "ELSE (select typname from pg_type where oid = oprright) END as oprright, "
5136 "oprcom, oprnegate, oprrest, oprjoin, "
5137 "oprcanhash, oprlsortop, oprrsortop, "
5138 "0 as oprltcmpop, 0 as oprgtcmpop "
5140 "where oid = '%u'::oid",
5141 oprinfo->dobj.catId.oid);
5144 res = PQexec(g_conn, query->data);
5145 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
5147 /* Expecting a single result only */
5148 ntups = PQntuples(res);
5151 write_msg(NULL, "Got %d rows instead of one from: %s",
5152 ntups, query->data);
5156 i_oprkind = PQfnumber(res, "oprkind");
5157 i_oprcode = PQfnumber(res, "oprcode");
5158 i_oprleft = PQfnumber(res, "oprleft");
5159 i_oprright = PQfnumber(res, "oprright");
5160 i_oprcom = PQfnumber(res, "oprcom");
5161 i_oprnegate = PQfnumber(res, "oprnegate");
5162 i_oprrest = PQfnumber(res, "oprrest");
5163 i_oprjoin = PQfnumber(res, "oprjoin");
5164 i_oprcanhash = PQfnumber(res, "oprcanhash");
5165 i_oprlsortop = PQfnumber(res, "oprlsortop");
5166 i_oprrsortop = PQfnumber(res, "oprrsortop");
5167 i_oprltcmpop = PQfnumber(res, "oprltcmpop");
5168 i_oprgtcmpop = PQfnumber(res, "oprgtcmpop");
5170 oprkind = PQgetvalue(res, 0, i_oprkind);
5171 oprcode = PQgetvalue(res, 0, i_oprcode);
5172 oprleft = PQgetvalue(res, 0, i_oprleft);
5173 oprright = PQgetvalue(res, 0, i_oprright);
5174 oprcom = PQgetvalue(res, 0, i_oprcom);
5175 oprnegate = PQgetvalue(res, 0, i_oprnegate);
5176 oprrest = PQgetvalue(res, 0, i_oprrest);
5177 oprjoin = PQgetvalue(res, 0, i_oprjoin);
5178 oprcanhash = PQgetvalue(res, 0, i_oprcanhash);
5179 oprlsortop = PQgetvalue(res, 0, i_oprlsortop);
5180 oprrsortop = PQgetvalue(res, 0, i_oprrsortop);
5181 oprltcmpop = PQgetvalue(res, 0, i_oprltcmpop);
5182 oprgtcmpop = PQgetvalue(res, 0, i_oprgtcmpop);
5184 appendPQExpBuffer(details, " PROCEDURE = %s",
5185 convertRegProcReference(oprcode));
5187 appendPQExpBuffer(oprid, "%s (",
5191 * right unary means there's a left arg and left unary means there's a
5194 if (strcmp(oprkind, "r") == 0 ||
5195 strcmp(oprkind, "b") == 0)
5197 if (g_fout->remoteVersion >= 70100)
5200 name = fmtId(oprleft);
5201 appendPQExpBuffer(details, ",\n LEFTARG = %s", name);
5202 appendPQExpBuffer(oprid, "%s", name);
5205 appendPQExpBuffer(oprid, "NONE");
5207 if (strcmp(oprkind, "l") == 0 ||
5208 strcmp(oprkind, "b") == 0)
5210 if (g_fout->remoteVersion >= 70100)
5213 name = fmtId(oprright);
5214 appendPQExpBuffer(details, ",\n RIGHTARG = %s", name);
5215 appendPQExpBuffer(oprid, ", %s)", name);
5218 appendPQExpBuffer(oprid, ", NONE)");
5220 name = convertOperatorReference(oprcom);
5222 appendPQExpBuffer(details, ",\n COMMUTATOR = %s", name);
5224 name = convertOperatorReference(oprnegate);
5226 appendPQExpBuffer(details, ",\n NEGATOR = %s", name);
5228 if (strcmp(oprcanhash, "t") == 0)
5229 appendPQExpBuffer(details, ",\n HASHES");
5231 name = convertRegProcReference(oprrest);
5233 appendPQExpBuffer(details, ",\n RESTRICT = %s", name);
5235 name = convertRegProcReference(oprjoin);
5237 appendPQExpBuffer(details, ",\n JOIN = %s", name);
5239 name = convertOperatorReference(oprlsortop);
5241 appendPQExpBuffer(details, ",\n SORT1 = %s", name);
5243 name = convertOperatorReference(oprrsortop);
5245 appendPQExpBuffer(details, ",\n SORT2 = %s", name);
5247 name = convertOperatorReference(oprltcmpop);
5249 appendPQExpBuffer(details, ",\n LTCMP = %s", name);
5251 name = convertOperatorReference(oprgtcmpop);
5253 appendPQExpBuffer(details, ",\n GTCMP = %s", name);
5256 * DROP must be fully qualified in case same name appears in
5259 appendPQExpBuffer(delq, "DROP OPERATOR %s.%s;\n",
5260 fmtId(oprinfo->oprnamespace->nspname),
5263 appendPQExpBuffer(q, "CREATE OPERATOR %s (\n%s\n);\n",
5264 oprinfo->oprname, details->data);
5266 ArchiveEntry(fout, oprinfo->dobj.catId, oprinfo->dobj.dumpId,
5268 oprinfo->oprnamespace->nspname, oprinfo->usename,
5269 "OPERATOR", q->data, delq->data, NULL,
5270 oprinfo->dobj.dependencies, oprinfo->dobj.nDeps,
5273 /* Dump Operator Comments */
5274 resetPQExpBuffer(q);
5275 appendPQExpBuffer(q, "OPERATOR %s", oprid->data);
5276 dumpComment(fout, q->data,
5277 oprinfo->oprnamespace->nspname, oprinfo->usename,
5278 oprinfo->dobj.catId, 0, oprinfo->dobj.dumpId);
5282 destroyPQExpBuffer(query);
5283 destroyPQExpBuffer(q);
5284 destroyPQExpBuffer(delq);
5285 destroyPQExpBuffer(oprid);
5286 destroyPQExpBuffer(details);
5290 * Convert a function reference obtained from pg_operator
5292 * Returns what to print, or NULL if function references is InvalidOid
5294 * In 7.3 the input is a REGPROCEDURE display; we have to strip the
5295 * argument-types part. In prior versions, the input is a REGPROC display.
5298 convertRegProcReference(const char *proc)
5300 /* In all cases "-" means a null reference */
5301 if (strcmp(proc, "-") == 0)
5304 if (g_fout->remoteVersion >= 70300)
5310 name = strdup(proc);
5311 /* find non-double-quoted left paren */
5313 for (paren = name; *paren; paren++)
5315 if (*paren == '(' && !inquote)
5326 /* REGPROC before 7.3 does not quote its result */
5331 * Convert an operator cross-reference obtained from pg_operator
5333 * Returns what to print, or NULL to print nothing
5335 * In 7.3 the input is a REGOPERATOR display; we have to strip the
5336 * argument-types part. In prior versions, the input is just a
5337 * numeric OID, which we search our operator list for.
5340 convertOperatorReference(const char *opr)
5344 /* In all cases "0" means a null reference */
5345 if (strcmp(opr, "0") == 0)
5348 if (g_fout->remoteVersion >= 70300)
5355 /* find non-double-quoted left paren */
5357 for (paren = name; *paren; paren++)
5359 if (*paren == '(' && !inquote)
5370 oprInfo = findOprByOid(atooid(opr));
5371 if (oprInfo == NULL)
5373 write_msg(NULL, "WARNING: could not find operator with OID %s\n",
5377 return oprInfo->oprname;
5382 * write out a single operator class definition
5385 dumpOpclass(Archive *fout, OpclassInfo *opcinfo)
5413 /* Dump only opclasses in dumpable namespaces */
5414 if (!opcinfo->opcnamespace->dump || dataOnly)
5418 * XXX currently we do not implement dumping of operator classes from
5419 * pre-7.3 databases. This could be done but it seems not worth the
5422 if (g_fout->remoteVersion < 70300)
5425 query = createPQExpBuffer();
5426 q = createPQExpBuffer();
5427 delq = createPQExpBuffer();
5429 /* Make sure we are in proper schema so regoperator works correctly */
5430 selectSourceSchema(opcinfo->opcnamespace->nspname);
5432 /* Get additional fields from the pg_opclass row */
5433 appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
5434 "opckeytype::pg_catalog.regtype, "
5436 "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcamid) AS amname "
5437 "FROM pg_catalog.pg_opclass "
5438 "WHERE oid = '%u'::pg_catalog.oid",
5439 opcinfo->dobj.catId.oid);
5441 res = PQexec(g_conn, query->data);
5442 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
5444 /* Expecting a single result only */
5445 ntups = PQntuples(res);
5448 write_msg(NULL, "Got %d rows instead of one from: %s",
5449 ntups, query->data);
5453 i_opcintype = PQfnumber(res, "opcintype");
5454 i_opckeytype = PQfnumber(res, "opckeytype");
5455 i_opcdefault = PQfnumber(res, "opcdefault");
5456 i_amname = PQfnumber(res, "amname");
5458 opcintype = PQgetvalue(res, 0, i_opcintype);
5459 opckeytype = PQgetvalue(res, 0, i_opckeytype);
5460 opcdefault = PQgetvalue(res, 0, i_opcdefault);
5461 /* amname will still be needed after we PQclear res */
5462 amname = strdup(PQgetvalue(res, 0, i_amname));
5465 * DROP must be fully qualified in case same name appears in
5468 appendPQExpBuffer(delq, "DROP OPERATOR CLASS %s",
5469 fmtId(opcinfo->opcnamespace->nspname));
5470 appendPQExpBuffer(delq, ".%s",
5471 fmtId(opcinfo->opcname));
5472 appendPQExpBuffer(delq, " USING %s;\n",
5475 /* Build the fixed portion of the CREATE command */
5476 appendPQExpBuffer(q, "CREATE OPERATOR CLASS %s\n ",
5477 fmtId(opcinfo->opcname));
5478 if (strcmp(opcdefault, "t") == 0)
5479 appendPQExpBuffer(q, "DEFAULT ");
5480 appendPQExpBuffer(q, "FOR TYPE %s USING %s AS\n ",
5486 if (strcmp(opckeytype, "-") != 0)
5488 appendPQExpBuffer(q, "STORAGE %s",
5496 * Now fetch and print the OPERATOR entries (pg_amop rows).
5498 resetPQExpBuffer(query);
5500 appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
5501 "amopopr::pg_catalog.regoperator "
5502 "FROM pg_catalog.pg_amop "
5503 "WHERE amopclaid = '%u'::pg_catalog.oid "
5504 "ORDER BY amopstrategy",
5505 opcinfo->dobj.catId.oid);
5507 res = PQexec(g_conn, query->data);
5508 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
5510 ntups = PQntuples(res);
5512 i_amopstrategy = PQfnumber(res, "amopstrategy");
5513 i_amopreqcheck = PQfnumber(res, "amopreqcheck");
5514 i_amopopr = PQfnumber(res, "amopopr");
5516 for (i = 0; i < ntups; i++)
5518 amopstrategy = PQgetvalue(res, i, i_amopstrategy);
5519 amopreqcheck = PQgetvalue(res, i, i_amopreqcheck);
5520 amopopr = PQgetvalue(res, i, i_amopopr);
5523 appendPQExpBuffer(q, " ,\n ");
5525 appendPQExpBuffer(q, "OPERATOR %s %s",
5526 amopstrategy, amopopr);
5527 if (strcmp(amopreqcheck, "t") == 0)
5528 appendPQExpBuffer(q, " RECHECK");
5536 * Now fetch and print the FUNCTION entries (pg_amproc rows).
5538 resetPQExpBuffer(query);
5540 appendPQExpBuffer(query, "SELECT amprocnum, "
5541 "amproc::pg_catalog.regprocedure "
5542 "FROM pg_catalog.pg_amproc "
5543 "WHERE amopclaid = '%u'::pg_catalog.oid "
5544 "ORDER BY amprocnum",
5545 opcinfo->dobj.catId.oid);
5547 res = PQexec(g_conn, query->data);
5548 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
5550 ntups = PQntuples(res);
5552 i_amprocnum = PQfnumber(res, "amprocnum");
5553 i_amproc = PQfnumber(res, "amproc");
5555 for (i = 0; i < ntups; i++)
5557 amprocnum = PQgetvalue(res, i, i_amprocnum);
5558 amproc = PQgetvalue(res, i, i_amproc);
5561 appendPQExpBuffer(q, " ,\n ");
5563 appendPQExpBuffer(q, "FUNCTION %s %s",
5571 appendPQExpBuffer(q, ";\n");
5573 ArchiveEntry(fout, opcinfo->dobj.catId, opcinfo->dobj.dumpId,
5575 opcinfo->opcnamespace->nspname, opcinfo->usename,
5576 "OPERATOR CLASS", q->data, delq->data, NULL,
5577 opcinfo->dobj.dependencies, opcinfo->dobj.nDeps,
5580 /* Dump Operator Class Comments */
5581 resetPQExpBuffer(q);
5582 appendPQExpBuffer(q, "OPERATOR CLASS %s",
5583 fmtId(opcinfo->opcname));
5584 appendPQExpBuffer(q, " USING %s",
5586 dumpComment(fout, q->data,
5587 NULL, opcinfo->usename,
5588 opcinfo->dobj.catId, 0, opcinfo->dobj.dumpId);
5591 destroyPQExpBuffer(query);
5592 destroyPQExpBuffer(q);
5593 destroyPQExpBuffer(delq);
5598 * write out a single conversion definition
5601 dumpConversion(Archive *fout, ConvInfo *convinfo)
5606 PQExpBuffer details;
5610 int i_conforencoding;
5611 int i_contoencoding;
5614 const char *conname;
5615 const char *conforencoding;
5616 const char *contoencoding;
5617 const char *conproc;
5620 /* Dump only conversions in dumpable namespaces */
5621 if (!convinfo->connamespace->dump || dataOnly)
5624 query = createPQExpBuffer();
5625 q = createPQExpBuffer();
5626 delq = createPQExpBuffer();
5627 details = createPQExpBuffer();
5629 /* Make sure we are in proper schema */
5630 selectSourceSchema(convinfo->connamespace->nspname);
5632 /* Get conversion-specific details */
5633 appendPQExpBuffer(query, "SELECT conname, "
5634 "pg_catalog.pg_encoding_to_char(conforencoding) AS conforencoding, "
5635 "pg_catalog.pg_encoding_to_char(contoencoding) AS contoencoding, "
5636 "conproc, condefault "
5637 "FROM pg_catalog.pg_conversion c "
5638 "WHERE c.oid = '%u'::pg_catalog.oid",
5639 convinfo->dobj.catId.oid);
5641 res = PQexec(g_conn, query->data);
5642 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
5644 /* Expecting a single result only */
5645 ntups = PQntuples(res);
5648 write_msg(NULL, "Got %d rows instead of one from: %s",
5649 ntups, query->data);
5653 i_conname = PQfnumber(res, "conname");
5654 i_conforencoding = PQfnumber(res, "conforencoding");
5655 i_contoencoding = PQfnumber(res, "contoencoding");
5656 i_conproc = PQfnumber(res, "conproc");
5657 i_condefault = PQfnumber(res, "condefault");
5659 conname = PQgetvalue(res, 0, i_conname);
5660 conforencoding = PQgetvalue(res, 0, i_conforencoding);
5661 contoencoding = PQgetvalue(res, 0, i_contoencoding);
5662 conproc = PQgetvalue(res, 0, i_conproc);
5663 condefault = (PQgetvalue(res, 0, i_condefault)[0] == 't');
5666 * DROP must be fully qualified in case same name appears in
5669 appendPQExpBuffer(delq, "DROP CONVERSION %s",
5670 fmtId(convinfo->connamespace->nspname));
5671 appendPQExpBuffer(delq, ".%s;\n",
5672 fmtId(convinfo->conname));
5674 appendPQExpBuffer(q, "CREATE %sCONVERSION %s FOR ",
5675 (condefault) ? "DEFAULT " : "",
5676 fmtId(convinfo->conname));
5677 appendStringLiteral(q, conforencoding, true);
5678 appendPQExpBuffer(q, " TO ");
5679 appendStringLiteral(q, contoencoding, true);
5680 /* regproc is automatically quoted in 7.3 and above */
5681 appendPQExpBuffer(q, " FROM %s;\n", conproc);
5683 ArchiveEntry(fout, convinfo->dobj.catId, convinfo->dobj.dumpId,
5685 convinfo->connamespace->nspname, convinfo->usename,
5686 "CONVERSION", q->data, delq->data, NULL,
5687 convinfo->dobj.dependencies, convinfo->dobj.nDeps,
5690 /* Dump Conversion Comments */
5691 resetPQExpBuffer(q);
5692 appendPQExpBuffer(q, "CONVERSION %s", fmtId(convinfo->conname));
5693 dumpComment(fout, q->data,
5694 convinfo->connamespace->nspname, convinfo->usename,
5695 convinfo->dobj.catId, 0, convinfo->dobj.dumpId);
5699 destroyPQExpBuffer(query);
5700 destroyPQExpBuffer(q);
5701 destroyPQExpBuffer(delq);
5702 destroyPQExpBuffer(details);
5706 * format_aggregate_signature: generate aggregate name and argument list
5708 * The argument type names are qualified if needed. The aggregate name
5709 * is never qualified.
5712 format_aggregate_signature(AggInfo *agginfo, Archive *fout, bool honor_quotes)
5714 PQExpBufferData buf;
5716 initPQExpBuffer(&buf);
5718 appendPQExpBuffer(&buf, "%s",
5719 fmtId(agginfo->aggfn.proname));
5721 appendPQExpBuffer(&buf, "%s", agginfo->aggfn.proname);
5723 /* If using regtype or format_type, fmtbasetype is already quoted */
5724 if (fout->remoteVersion >= 70100)
5726 if (agginfo->anybasetype)
5727 appendPQExpBuffer(&buf, "(*)");
5729 appendPQExpBuffer(&buf, "(%s)", agginfo->fmtbasetype);
5733 if (agginfo->anybasetype)
5734 appendPQExpBuffer(&buf, "(*)");
5736 appendPQExpBuffer(&buf, "(%s)",
5737 fmtId(agginfo->fmtbasetype));
5745 * write out a single aggregate definition
5748 dumpAgg(Archive *fout, AggInfo *agginfo)
5753 PQExpBuffer details;
5765 const char *aggtransfn;
5766 const char *aggfinalfn;
5767 const char *aggtranstype;
5768 const char *agginitval;
5771 /* Dump only aggs in dumpable namespaces */
5772 if (!agginfo->aggfn.pronamespace->dump || dataOnly)
5775 query = createPQExpBuffer();
5776 q = createPQExpBuffer();
5777 delq = createPQExpBuffer();
5778 details = createPQExpBuffer();
5780 /* Make sure we are in proper schema */
5781 selectSourceSchema(agginfo->aggfn.pronamespace->nspname);
5783 /* Get aggregate-specific details */
5784 if (g_fout->remoteVersion >= 70300)
5786 appendPQExpBuffer(query, "SELECT aggtransfn, "
5787 "aggfinalfn, aggtranstype::pg_catalog.regtype, "
5789 "proargtypes[0] = 'pg_catalog.\"any\"'::pg_catalog.regtype as anybasetype, "
5790 "proargtypes[0]::pg_catalog.regtype as fmtbasetype, "
5791 "'t'::boolean as convertok "
5792 "from pg_catalog.pg_aggregate a, pg_catalog.pg_proc p "
5793 "where a.aggfnoid = p.oid "
5794 "and p.oid = '%u'::pg_catalog.oid",
5795 agginfo->aggfn.dobj.catId.oid);
5797 else if (g_fout->remoteVersion >= 70100)
5799 appendPQExpBuffer(query, "SELECT aggtransfn, aggfinalfn, "
5800 "format_type(aggtranstype, NULL) as aggtranstype, "
5802 "aggbasetype = 0 as anybasetype, "
5803 "CASE WHEN aggbasetype = 0 THEN '-' "
5804 "ELSE format_type(aggbasetype, NULL) END as fmtbasetype, "
5805 "'t'::boolean as convertok "
5806 "from pg_aggregate "
5807 "where oid = '%u'::oid",
5808 agginfo->aggfn.dobj.catId.oid);
5812 appendPQExpBuffer(query, "SELECT aggtransfn1 as aggtransfn, "
5814 "(select typname from pg_type where oid = aggtranstype1) as aggtranstype, "
5815 "agginitval1 as agginitval, "
5816 "aggbasetype = 0 as anybasetype, "
5817 "(select typname from pg_type where oid = aggbasetype) as fmtbasetype, "
5818 "(aggtransfn2 = 0 and aggtranstype2 = 0 and agginitval2 is null) as convertok "
5819 "from pg_aggregate "
5820 "where oid = '%u'::oid",
5821 agginfo->aggfn.dobj.catId.oid);
5824 res = PQexec(g_conn, query->data);
5825 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
5827 /* Expecting a single result only */
5828 ntups = PQntuples(res);
5831 write_msg(NULL, "Got %d rows instead of one from: %s",
5832 ntups, query->data);
5836 i_aggtransfn = PQfnumber(res, "aggtransfn");
5837 i_aggfinalfn = PQfnumber(res, "aggfinalfn");
5838 i_aggtranstype = PQfnumber(res, "aggtranstype");
5839 i_agginitval = PQfnumber(res, "agginitval");
5840 i_anybasetype = PQfnumber(res, "anybasetype");
5841 i_fmtbasetype = PQfnumber(res, "fmtbasetype");
5842 i_convertok = PQfnumber(res, "convertok");
5844 aggtransfn = PQgetvalue(res, 0, i_aggtransfn);
5845 aggfinalfn = PQgetvalue(res, 0, i_aggfinalfn);
5846 aggtranstype = PQgetvalue(res, 0, i_aggtranstype);
5847 agginitval = PQgetvalue(res, 0, i_agginitval);
5848 /* we save anybasetype for format_aggregate_signature */
5849 agginfo->anybasetype = (PQgetvalue(res, 0, i_anybasetype)[0] == 't');
5850 /* we save fmtbasetype for format_aggregate_signature */
5851 agginfo->fmtbasetype = strdup(PQgetvalue(res, 0, i_fmtbasetype));
5852 convertok = (PQgetvalue(res, 0, i_convertok)[0] == 't');
5854 aggsig = format_aggregate_signature(agginfo, fout, true);
5855 aggsig_tag = format_aggregate_signature(agginfo, fout, false);
5859 write_msg(NULL, "WARNING: aggregate function %s could not be dumped correctly for this database version; ignored\n",
5864 if (g_fout->remoteVersion >= 70300)
5866 /* If using 7.3's regproc or regtype, data is already quoted */
5867 appendPQExpBuffer(details, " BASETYPE = %s,\n SFUNC = %s,\n STYPE = %s",
5868 agginfo->anybasetype ? "'any'" :
5869 agginfo->fmtbasetype,
5873 else if (g_fout->remoteVersion >= 70100)
5875 /* format_type quotes, regproc does not */
5876 appendPQExpBuffer(details, " BASETYPE = %s,\n SFUNC = %s,\n STYPE = %s",
5877 agginfo->anybasetype ? "'any'" :
5878 agginfo->fmtbasetype,
5884 /* need quotes all around */
5885 appendPQExpBuffer(details, " BASETYPE = %s,\n",
5886 agginfo->anybasetype ? "'any'" :
5887 fmtId(agginfo->fmtbasetype));
5888 appendPQExpBuffer(details, " SFUNC = %s,\n",
5890 appendPQExpBuffer(details, " STYPE = %s",
5891 fmtId(aggtranstype));
5894 if (!PQgetisnull(res, 0, i_agginitval))
5896 appendPQExpBuffer(details, ",\n INITCOND = ");
5897 appendStringLiteral(details, agginitval, true);
5900 if (strcmp(aggfinalfn, "-") != 0)
5902 appendPQExpBuffer(details, ",\n FINALFUNC = %s",
5907 * DROP must be fully qualified in case same name appears in
5910 appendPQExpBuffer(delq, "DROP AGGREGATE %s.%s;\n",
5911 fmtId(agginfo->aggfn.pronamespace->nspname),
5914 appendPQExpBuffer(q, "CREATE AGGREGATE %s (\n%s\n);\n",
5915 fmtId(agginfo->aggfn.proname),
5918 ArchiveEntry(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
5920 agginfo->aggfn.pronamespace->nspname, agginfo->aggfn.usename,
5921 "AGGREGATE", q->data, delq->data, NULL,
5922 agginfo->aggfn.dobj.dependencies, agginfo->aggfn.dobj.nDeps,
5925 /* Dump Aggregate Comments */
5926 resetPQExpBuffer(q);
5927 appendPQExpBuffer(q, "AGGREGATE %s", aggsig);
5928 dumpComment(fout, q->data,
5929 agginfo->aggfn.pronamespace->nspname, agginfo->aggfn.usename,
5930 agginfo->aggfn.dobj.catId, 0, agginfo->aggfn.dobj.dumpId);
5933 * Since there is no GRANT ON AGGREGATE syntax, we have to make the
5934 * ACL command look like a function's GRANT; in particular this affects
5935 * the syntax for aggregates on ANY.
5940 aggsig = format_function_signature(&agginfo->aggfn, NULL, true);
5941 aggsig_tag = format_function_signature(&agginfo->aggfn, NULL, false);
5943 dumpACL(fout, agginfo->aggfn.dobj.catId, agginfo->aggfn.dobj.dumpId,
5946 agginfo->aggfn.pronamespace->nspname,
5947 agginfo->aggfn.usename, agginfo->aggfn.proacl);
5954 destroyPQExpBuffer(query);
5955 destroyPQExpBuffer(q);
5956 destroyPQExpBuffer(delq);
5957 destroyPQExpBuffer(details);
5962 * Write out grant/revoke information
5964 * 'objCatId' is the catalog ID of the underlying object.
5965 * 'objDumpId' is the dump ID of the underlying object.
5966 * 'type' must be TABLE, FUNCTION, LANGUAGE, or SCHEMA.
5967 * 'name' is the formatted name of the object. Must be quoted etc. already.
5968 * 'tag' is the tag for the archive entry (typ. unquoted name of object).
5969 * 'nspname' is the namespace the object is in (NULL if none).
5970 * 'owner' is the owner, NULL if there is no owner (for languages).
5971 * 'acls' is the string read out of the fooacl system catalog field;
5972 * it will be parsed here.
5976 dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId,
5977 const char *type, const char *name,
5978 const char *tag, const char *nspname, const char *owner,
5983 /* Do nothing if ACL dump is not enabled */
5984 if (dataOnly || aclsSkip)
5987 sql = createPQExpBuffer();
5989 if (!buildACLCommands(name, type, acls, owner, fout->remoteVersion, sql))
5991 write_msg(NULL, "could not parse ACL list (%s) for object \"%s\" (%s)\n",
5997 ArchiveEntry(fout, nilCatalogId, createDumpId(),
6000 "ACL", sql->data, "", NULL,
6004 destroyPQExpBuffer(sql);
6009 * write out to fout the declarations (not data) of a user-defined table
6012 dumpTable(Archive *fout, TableInfo *tbinfo)
6018 if (tbinfo->relkind == RELKIND_SEQUENCE)
6019 dumpSequence(fout, tbinfo);
6021 dumpTableSchema(fout, tbinfo);
6023 /* Handle the ACL here */
6024 namecopy = strdup(fmtId(tbinfo->relname));
6025 dumpACL(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId, "TABLE",
6026 namecopy, tbinfo->relname,
6027 tbinfo->relnamespace->nspname, tbinfo->usename,
6035 * write the declaration (not data) of one user-defined table or view
6038 dumpTableSchema(Archive *fout, TableInfo *tbinfo)
6040 PQExpBuffer query = createPQExpBuffer();
6041 PQExpBuffer q = createPQExpBuffer();
6042 PQExpBuffer delq = createPQExpBuffer();
6045 TableInfo **parents;
6046 int actual_atts; /* number of attrs in this CREATE statment */
6052 /* Make sure we are in proper schema */
6053 selectSourceSchema(tbinfo->relnamespace->nspname);
6055 /* Is it a table or a view? */
6056 if (tbinfo->relkind == RELKIND_VIEW)
6060 reltypename = "VIEW";
6062 /* Fetch the view definition */
6063 if (g_fout->remoteVersion >= 70300)
6065 /* Beginning in 7.3, viewname is not unique; rely on OID */
6066 appendPQExpBuffer(query,
6067 "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) as viewdef",
6068 tbinfo->dobj.catId.oid);
6072 appendPQExpBuffer(query, "SELECT definition as viewdef "
6073 " from pg_views where viewname = ");
6074 appendStringLiteral(query, tbinfo->relname, true);
6075 appendPQExpBuffer(query, ";");
6078 res = PQexec(g_conn, query->data);
6079 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
6081 if (PQntuples(res) != 1)
6083 if (PQntuples(res) < 1)
6084 write_msg(NULL, "query to obtain definition of view \"%s\" returned no data\n",
6087 write_msg(NULL, "query to obtain definition of view \"%s\" returned more than one definition\n",
6092 viewdef = PQgetvalue(res, 0, 0);
6094 if (strlen(viewdef) == 0)
6096 write_msg(NULL, "definition of view \"%s\" appears to be empty (length zero)\n",
6102 * DROP must be fully qualified in case same name appears in
6105 appendPQExpBuffer(delq, "DROP VIEW %s.",
6106 fmtId(tbinfo->relnamespace->nspname));
6107 appendPQExpBuffer(delq, "%s;\n",
6108 fmtId(tbinfo->relname));
6110 appendPQExpBuffer(q, "CREATE VIEW %s AS\n %s\n",
6111 fmtId(tbinfo->relname), viewdef);
6117 reltypename = "TABLE";
6118 numParents = tbinfo->numParents;
6119 parents = tbinfo->parents;
6122 * DROP must be fully qualified in case same name appears in
6125 appendPQExpBuffer(delq, "DROP TABLE %s.",
6126 fmtId(tbinfo->relnamespace->nspname));
6127 appendPQExpBuffer(delq, "%s;\n",
6128 fmtId(tbinfo->relname));
6130 appendPQExpBuffer(q, "CREATE TABLE %s (",
6131 fmtId(tbinfo->relname));
6133 for (j = 0; j < tbinfo->numatts; j++)
6135 /* Is this one of the table's own attrs, and not dropped ? */
6136 if (!tbinfo->inhAttrs[j] && !tbinfo->attisdropped[j])
6138 /* Format properly if not first attr */
6139 if (actual_atts > 0)
6140 appendPQExpBuffer(q, ",");
6141 appendPQExpBuffer(q, "\n ");
6143 /* Attribute name */
6144 appendPQExpBuffer(q, "%s ",
6145 fmtId(tbinfo->attnames[j]));
6147 /* Attribute type */
6148 if (g_fout->remoteVersion >= 70100)
6150 char *typname = tbinfo->atttypnames[j];
6152 if (tbinfo->attisserial[j])
6154 if (strcmp(typname, "integer") == 0)
6156 else if (strcmp(typname, "bigint") == 0)
6157 typname = "bigserial";
6159 appendPQExpBuffer(q, "%s", typname);
6163 /* If no format_type, fake it */
6164 appendPQExpBuffer(q, "%s",
6165 myFormatType(tbinfo->atttypnames[j],
6166 tbinfo->atttypmod[j]));
6170 * Default value --- suppress if inherited, serial,
6171 * or to be printed separately.
6173 if (tbinfo->attrdefs[j] != NULL &&
6174 !tbinfo->inhAttrDef[j] &&
6175 !tbinfo->attisserial[j] &&
6176 !tbinfo->attrdefs[j]->separate)
6177 appendPQExpBuffer(q, " DEFAULT %s",
6178 tbinfo->attrdefs[j]->adef_expr);
6181 * Not Null constraint --- suppress if inherited
6183 * Note: we could suppress this for serial columns since
6184 * SERIAL implies NOT NULL. We choose not to for forward
6185 * compatibility, since there has been some talk of making
6186 * SERIAL not imply NOT NULL, in which case the explicit
6187 * specification would be needed.
6189 if (tbinfo->notnull[j] && !tbinfo->inhNotNull[j])
6190 appendPQExpBuffer(q, " NOT NULL");
6197 * Add non-inherited CHECK constraints, if any.
6199 for (j = 0; j < tbinfo->ncheck; j++)
6201 ConstraintInfo *constr = &(tbinfo->checkexprs[j]);
6203 if (constr->coninherited || constr->separate)
6206 if (actual_atts > 0)
6207 appendPQExpBuffer(q, ",\n ");
6209 appendPQExpBuffer(q, "CONSTRAINT %s ",
6210 fmtId(constr->conname));
6211 appendPQExpBuffer(q, "%s", constr->condef);
6216 appendPQExpBuffer(q, "\n)");
6220 appendPQExpBuffer(q, "\nINHERITS (");
6221 for (k = 0; k < numParents; k++)
6223 TableInfo *parentRel = parents[k];
6226 appendPQExpBuffer(q, ", ");
6227 if (parentRel->relnamespace != tbinfo->relnamespace)
6228 appendPQExpBuffer(q, "%s.",
6229 fmtId(parentRel->relnamespace->nspname));
6230 appendPQExpBuffer(q, "%s",
6231 fmtId(parentRel->relname));
6233 appendPQExpBuffer(q, ")");
6236 appendPQExpBuffer(q, tbinfo->hasoids ? " WITH OIDS" : " WITHOUT OIDS");
6238 appendPQExpBuffer(q, ";\n");
6240 /* Loop dumping statistics and storage statements */
6241 for (j = 0; j < tbinfo->numatts; j++)
6244 * Dump per-column statistics information. We only issue an
6245 * ALTER TABLE statement if the attstattarget entry for this
6246 * column is non-negative (i.e. it's not the default value)
6248 if (tbinfo->attstattarget[j] >= 0 &&
6249 !tbinfo->attisdropped[j])
6251 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
6252 fmtId(tbinfo->relname));
6253 appendPQExpBuffer(q, "ALTER COLUMN %s ",
6254 fmtId(tbinfo->attnames[j]));
6255 appendPQExpBuffer(q, "SET STATISTICS %d;\n",
6256 tbinfo->attstattarget[j]);
6260 * Dump per-column storage information. The statement is only
6261 * dumped if the storage has been changed from the type's
6264 if (!tbinfo->attisdropped[j] && tbinfo->attstorage[j] != tbinfo->typstorage[j])
6266 switch (tbinfo->attstorage[j])
6272 storage = "EXTERNAL";
6278 storage = "EXTENDED";
6285 * Only dump the statement if it's a storage type we
6288 if (storage != NULL)
6290 appendPQExpBuffer(q, "ALTER TABLE ONLY %s ",
6291 fmtId(tbinfo->relname));
6292 appendPQExpBuffer(q, "ALTER COLUMN %s ",
6293 fmtId(tbinfo->attnames[j]));
6294 appendPQExpBuffer(q, "SET STORAGE %s;\n",
6301 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
6303 tbinfo->relnamespace->nspname, tbinfo->usename,
6304 reltypename, q->data, delq->data, NULL,
6305 tbinfo->dobj.dependencies, tbinfo->dobj.nDeps,
6308 /* Dump Table Comments */
6309 dumpTableComment(fout, tbinfo, reltypename);
6311 destroyPQExpBuffer(query);
6312 destroyPQExpBuffer(q);
6313 destroyPQExpBuffer(delq);
6317 * dumpAttrDef --- dump an attribute's default-value declaration
6320 dumpAttrDef(Archive *fout, AttrDefInfo *adinfo)
6322 TableInfo *tbinfo = adinfo->adtable;
6323 int adnum = adinfo->adnum;
6327 /* Only print it if "separate" mode is selected */
6328 if (!tbinfo->dump || !adinfo->separate || dataOnly)
6331 /* Don't print inherited or serial defaults, either */
6332 if (tbinfo->inhAttrDef[adnum-1] || tbinfo->attisserial[adnum-1])
6335 q = createPQExpBuffer();
6336 delq = createPQExpBuffer();
6338 appendPQExpBuffer(q, "ALTER TABLE %s ",
6339 fmtId(tbinfo->relname));
6340 appendPQExpBuffer(q, "ALTER COLUMN %s SET DEFAULT %s;\n",
6341 fmtId(tbinfo->attnames[adnum - 1]),
6345 * DROP must be fully qualified in case same name appears
6348 appendPQExpBuffer(delq, "ALTER TABLE %s.",
6349 fmtId(tbinfo->relnamespace->nspname));
6350 appendPQExpBuffer(delq, "%s ",
6351 fmtId(tbinfo->relname));
6352 appendPQExpBuffer(delq, "ALTER COLUMN %s DROP DEFAULT;\n",
6353 fmtId(tbinfo->attnames[adnum - 1]));
6355 ArchiveEntry(fout, adinfo->dobj.catId, adinfo->dobj.dumpId,
6356 tbinfo->attnames[adnum - 1],
6357 tbinfo->relnamespace->nspname, tbinfo->usename,
6358 "DEFAULT", q->data, delq->data, NULL,
6359 adinfo->dobj.dependencies, adinfo->dobj.nDeps,
6362 destroyPQExpBuffer(q);
6363 destroyPQExpBuffer(delq);
6367 * getAttrName: extract the correct name for an attribute
6369 * The array tblInfo->attnames[] only provides names of user attributes;
6370 * if a system attribute number is supplied, we have to fake it.
6371 * We also do a little bit of bounds checking for safety's sake.
6374 getAttrName(int attrnum, TableInfo *tblInfo)
6376 if (attrnum > 0 && attrnum <= tblInfo->numatts)
6377 return tblInfo->attnames[attrnum - 1];
6380 case SelfItemPointerAttributeNumber:
6382 case ObjectIdAttributeNumber:
6384 case MinTransactionIdAttributeNumber:
6386 case MinCommandIdAttributeNumber:
6388 case MaxTransactionIdAttributeNumber:
6390 case MaxCommandIdAttributeNumber:
6392 case TableOidAttributeNumber:
6395 write_msg(NULL, "invalid column number %d for table \"%s\"\n",
6396 attrnum, tblInfo->relname);
6398 return NULL; /* keep compiler quiet */
6403 * write out to fout a user-defined index
6406 dumpIndex(Archive *fout, IndxInfo *indxinfo)
6408 TableInfo *tbinfo = indxinfo->indextable;
6415 q = createPQExpBuffer();
6416 delq = createPQExpBuffer();
6419 * If there's an associated constraint, don't dump the index per se,
6420 * but do dump any comment for it.
6422 if (indxinfo->indexconstraint == 0)
6424 /* Plain secondary index */
6425 appendPQExpBuffer(q, "%s;\n", indxinfo->indexdef);
6427 /* If the index is clustered, we need to record that. */
6428 if (indxinfo->indisclustered)
6430 appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
6431 fmtId(tbinfo->relname));
6432 appendPQExpBuffer(q, " ON %s;\n",
6433 fmtId(indxinfo->indexname));
6437 * DROP must be fully qualified in case same name appears
6440 appendPQExpBuffer(delq, "DROP INDEX %s.",
6441 fmtId(tbinfo->relnamespace->nspname));
6442 appendPQExpBuffer(delq, "%s;\n",
6443 fmtId(indxinfo->indexname));
6445 ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
6446 indxinfo->indexname,
6447 tbinfo->relnamespace->nspname,
6449 "INDEX", q->data, delq->data, NULL,
6450 indxinfo->dobj.dependencies, indxinfo->dobj.nDeps,
6454 /* Dump Index Comments */
6455 resetPQExpBuffer(q);
6456 appendPQExpBuffer(q, "INDEX %s",
6457 fmtId(indxinfo->indexname));
6458 dumpComment(fout, q->data,
6459 tbinfo->relnamespace->nspname,
6461 indxinfo->dobj.catId, 0, indxinfo->dobj.dumpId);
6463 destroyPQExpBuffer(q);
6464 destroyPQExpBuffer(delq);
6469 * write out to fout a user-defined constraint
6472 dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
6474 TableInfo *tbinfo = coninfo->contable;
6480 if (tbinfo && !tbinfo->dump)
6483 q = createPQExpBuffer();
6484 delq = createPQExpBuffer();
6486 if (coninfo->contype == 'p' || coninfo->contype == 'u')
6488 /* Index-related constraint */
6492 indxinfo = (IndxInfo *) findObjectByDumpId(coninfo->conindex);
6494 if (indxinfo == NULL)
6496 write_msg(NULL, "missing index for constraint %s\n",
6501 appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
6502 fmtId(tbinfo->relname));
6503 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s (",
6504 fmtId(coninfo->conname),
6505 coninfo->contype == 'p' ? "PRIMARY KEY" : "UNIQUE");
6507 for (k = 0; k < indxinfo->indnkeys; k++)
6509 int indkey = (int) indxinfo->indkeys[k];
6510 const char *attname;
6512 if (indkey == InvalidAttrNumber)
6514 attname = getAttrName(indkey, tbinfo);
6516 appendPQExpBuffer(q, "%s%s",
6517 (k == 0) ? "" : ", ",
6521 appendPQExpBuffer(q, ");\n");
6523 /* If the index is clustered, we need to record that. */
6524 if (indxinfo->indisclustered)
6526 appendPQExpBuffer(q, "\nALTER TABLE %s CLUSTER",
6527 fmtId(tbinfo->relname));
6528 appendPQExpBuffer(q, " ON %s;\n",
6529 fmtId(indxinfo->indexname));
6533 * DROP must be fully qualified in case same name appears
6536 appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
6537 fmtId(tbinfo->relnamespace->nspname));
6538 appendPQExpBuffer(delq, "%s ",
6539 fmtId(tbinfo->relname));
6540 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
6541 fmtId(coninfo->conname));
6543 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
6545 tbinfo->relnamespace->nspname,
6547 "CONSTRAINT", q->data, delq->data, NULL,
6548 coninfo->dobj.dependencies, coninfo->dobj.nDeps,
6551 else if (coninfo->contype == 'f')
6554 * XXX Potentially wrap in a 'SET CONSTRAINTS OFF' block so that
6555 * the current table data is not processed
6557 appendPQExpBuffer(q, "ALTER TABLE ONLY %s\n",
6558 fmtId(tbinfo->relname));
6559 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
6560 fmtId(coninfo->conname),
6564 * DROP must be fully qualified in case same name appears in
6567 appendPQExpBuffer(delq, "ALTER TABLE ONLY %s.",
6568 fmtId(tbinfo->relnamespace->nspname));
6569 appendPQExpBuffer(delq, "%s ",
6570 fmtId(tbinfo->relname));
6571 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
6572 fmtId(coninfo->conname));
6574 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
6576 tbinfo->relnamespace->nspname,
6578 "FK CONSTRAINT", q->data, delq->data, NULL,
6579 coninfo->dobj.dependencies, coninfo->dobj.nDeps,
6582 else if (coninfo->contype == 'c' && tbinfo)
6584 /* CHECK constraint on a table */
6586 /* Ignore if not to be dumped separately */
6587 if (coninfo->separate)
6589 /* not ONLY since we want it to propagate to children */
6590 appendPQExpBuffer(q, "ALTER TABLE %s\n",
6591 fmtId(tbinfo->relname));
6592 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
6593 fmtId(coninfo->conname),
6597 * DROP must be fully qualified in case same name appears in
6600 appendPQExpBuffer(delq, "ALTER TABLE %s.",
6601 fmtId(tbinfo->relnamespace->nspname));
6602 appendPQExpBuffer(delq, "%s ",
6603 fmtId(tbinfo->relname));
6604 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
6605 fmtId(coninfo->conname));
6607 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
6609 tbinfo->relnamespace->nspname,
6611 "CHECK CONSTRAINT", q->data, delq->data, NULL,
6612 coninfo->dobj.dependencies, coninfo->dobj.nDeps,
6616 else if (coninfo->contype == 'c' && tbinfo == NULL)
6618 /* CHECK constraint on a domain */
6619 TypeInfo *tinfo = coninfo->condomain;
6621 /* Ignore if not to be dumped separately, or if not dumping domain */
6622 if (coninfo->separate && tinfo->typnamespace->dump)
6624 appendPQExpBuffer(q, "ALTER DOMAIN %s\n",
6625 fmtId(tinfo->typname));
6626 appendPQExpBuffer(q, " ADD CONSTRAINT %s %s;\n",
6627 fmtId(coninfo->conname),
6631 * DROP must be fully qualified in case same name appears in
6634 appendPQExpBuffer(delq, "ALTER DOMAIN %s.",
6635 fmtId(tinfo->typnamespace->nspname));
6636 appendPQExpBuffer(delq, "%s ",
6637 fmtId(tinfo->typname));
6638 appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
6639 fmtId(coninfo->conname));
6641 ArchiveEntry(fout, coninfo->dobj.catId, coninfo->dobj.dumpId,
6643 tinfo->typnamespace->nspname,
6645 "CHECK CONSTRAINT", q->data, delq->data, NULL,
6646 coninfo->dobj.dependencies, coninfo->dobj.nDeps,
6652 write_msg(NULL, "unexpected constraint type\n");
6656 /* Dump Constraint Comments --- only works for table constraints */
6659 resetPQExpBuffer(q);
6660 appendPQExpBuffer(q, "CONSTRAINT %s ",
6661 fmtId(coninfo->conname));
6662 appendPQExpBuffer(q, "ON %s",
6663 fmtId(tbinfo->relname));
6664 dumpComment(fout, q->data,
6665 tbinfo->relnamespace->nspname,
6667 coninfo->dobj.catId, 0, coninfo->dobj.dumpId);
6670 destroyPQExpBuffer(q);
6671 destroyPQExpBuffer(delq);
6676 * find the maximum oid and generate a COPY statement to set it
6680 setMaxOid(Archive *fout)
6686 do_sql_command(g_conn,
6687 "CREATE TEMPORARY TABLE pgdump_oid (dummy integer)");
6688 res = PQexec(g_conn, "INSERT INTO pgdump_oid VALUES (0)");
6689 check_sql_result(res, g_conn, "INSERT INTO pgdump_oid VALUES (0)",
6691 max_oid = PQoidValue(res);
6694 write_msg(NULL, "inserted invalid OID\n");
6698 do_sql_command(g_conn, "DROP TABLE pgdump_oid;");
6700 write_msg(NULL, "maximum system OID is %u\n", max_oid);
6701 snprintf(sql, sizeof(sql),
6702 "CREATE TEMPORARY TABLE pgdump_oid (dummy integer);\n"
6703 "COPY pgdump_oid WITH OIDS FROM stdin;\n"
6706 "DROP TABLE pgdump_oid;\n",
6709 ArchiveEntry(fout, nilCatalogId, createDumpId(),
6710 "Max OID", NULL, "",
6711 "<Init>", sql, "", NULL,
6717 * findLastBuiltInOid -
6718 * find the last built in oid
6720 * For 7.1 and 7.2, we do this by retrieving datlastsysoid from the
6721 * pg_database entry for the current database
6724 findLastBuiltinOid_V71(const char *dbname)
6729 PQExpBuffer query = createPQExpBuffer();
6731 resetPQExpBuffer(query);
6732 appendPQExpBuffer(query, "SELECT datlastsysoid from pg_database where datname = ");
6733 appendStringLiteral(query, dbname, true);
6735 res = PQexec(g_conn, query->data);
6736 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
6738 ntups = PQntuples(res);
6741 write_msg(NULL, "missing pg_database entry for this database\n");
6746 write_msg(NULL, "found more than one pg_database entry for this database\n");
6749 last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "datlastsysoid")));
6751 destroyPQExpBuffer(query);
6756 * findLastBuiltInOid -
6757 * find the last built in oid
6759 * For 7.0, we do this by assuming that the last thing that initdb does is to
6760 * create the pg_indexes view. This sucks in general, but seeing that 7.0.x
6761 * initdb won't be changing anymore, it'll do.
6764 findLastBuiltinOid_V70(void)
6770 res = PQexec(g_conn,
6771 "SELECT oid FROM pg_class WHERE relname = 'pg_indexes'");
6772 check_sql_result(res, g_conn,
6773 "SELECT oid FROM pg_class WHERE relname = 'pg_indexes'",
6775 ntups = PQntuples(res);
6778 write_msg(NULL, "could not find entry for pg_indexes in pg_class\n");
6783 write_msg(NULL, "found more than one entry for pg_indexes in pg_class\n");
6786 last_oid = atooid(PQgetvalue(res, 0, PQfnumber(res, "oid")));
6792 dumpSequence(Archive *fout, TableInfo *tbinfo)
6804 PQExpBuffer query = createPQExpBuffer();
6805 PQExpBuffer delqry = createPQExpBuffer();
6807 /* Make sure we are in proper schema */
6808 selectSourceSchema(tbinfo->relnamespace->nspname);
6810 snprintf(bufm, sizeof(bufm), INT64_FORMAT, SEQ_MINVALUE);
6811 snprintf(bufx, sizeof(bufx), INT64_FORMAT, SEQ_MAXVALUE);
6813 appendPQExpBuffer(query,
6814 "SELECT sequence_name, last_value, increment_by, "
6815 "CASE WHEN increment_by > 0 AND max_value = %s THEN NULL "
6816 " WHEN increment_by < 0 AND max_value = -1 THEN NULL "
6818 "END AS max_value, "
6819 "CASE WHEN increment_by > 0 AND min_value = 1 THEN NULL "
6820 " WHEN increment_by < 0 AND min_value = %s THEN NULL "
6822 "END AS min_value, "
6823 "cache_value, is_cycled, is_called from %s",
6825 fmtId(tbinfo->relname));
6827 res = PQexec(g_conn, query->data);
6828 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
6830 if (PQntuples(res) != 1)
6832 write_msg(NULL, "query to get data of sequence \"%s\" returned %d rows (expected 1)\n",
6833 tbinfo->relname, PQntuples(res));
6837 /* Disable this check: it fails if sequence has been renamed */
6839 if (strcmp(PQgetvalue(res, 0, 0), tbinfo->relname) != 0)
6841 write_msg(NULL, "query to get data of sequence \"%s\" returned name \"%s\"\n",
6842 tbinfo->relname, PQgetvalue(res, 0, 0));
6847 last = PQgetvalue(res, 0, 1);
6848 incby = PQgetvalue(res, 0, 2);
6849 if (!PQgetisnull(res, 0, 3))
6850 maxv = PQgetvalue(res, 0, 3);
6851 if (!PQgetisnull(res, 0, 4))
6852 minv = PQgetvalue(res, 0, 4);
6853 cache = PQgetvalue(res, 0, 5);
6854 cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
6855 called = (strcmp(PQgetvalue(res, 0, 7), "t") == 0);
6858 * The logic we use for restoring sequences is as follows:
6860 * Add a basic CREATE SEQUENCE statement (use last_val for start if
6861 * called is false, else use min_val for start_val). Skip this if the
6862 * sequence came from a SERIAL column.
6864 * Add a 'SETVAL(seq, last_val, iscalled)' at restore-time iff we load
6865 * data. We do this for serial sequences too.
6868 if (!dataOnly && !OidIsValid(tbinfo->owning_tab))
6870 resetPQExpBuffer(delqry);
6873 * DROP must be fully qualified in case same name appears in
6876 appendPQExpBuffer(delqry, "DROP SEQUENCE %s.",
6877 fmtId(tbinfo->relnamespace->nspname));
6878 appendPQExpBuffer(delqry, "%s;\n",
6879 fmtId(tbinfo->relname));
6881 resetPQExpBuffer(query);
6882 appendPQExpBuffer(query,
6883 "CREATE SEQUENCE %s\n",
6884 fmtId(tbinfo->relname));
6887 appendPQExpBuffer(query, " START WITH %s\n", last);
6889 appendPQExpBuffer(query, " INCREMENT BY %s\n", incby);
6892 appendPQExpBuffer(query, " MAXVALUE %s\n", maxv);
6894 appendPQExpBuffer(query, " NO MAXVALUE\n");
6897 appendPQExpBuffer(query, " MINVALUE %s\n", minv);
6899 appendPQExpBuffer(query, " NO MINVALUE\n");
6901 appendPQExpBuffer(query,
6903 cache, (cycled ? "\n CYCLE" : ""));
6905 ArchiveEntry(fout, tbinfo->dobj.catId, tbinfo->dobj.dumpId,
6907 tbinfo->relnamespace->nspname, tbinfo->usename,
6908 "SEQUENCE", query->data, delqry->data, NULL,
6909 tbinfo->dobj.dependencies, tbinfo->dobj.nDeps,
6915 resetPQExpBuffer(query);
6916 appendPQExpBuffer(query, "SELECT pg_catalog.setval(");
6917 appendStringLiteral(query, fmtId(tbinfo->relname), true);
6918 appendPQExpBuffer(query, ", %s, %s);\n",
6919 last, (called ? "true" : "false"));
6921 ArchiveEntry(fout, nilCatalogId, createDumpId(),
6923 tbinfo->relnamespace->nspname, tbinfo->usename,
6924 "SEQUENCE SET", query->data, "", NULL,
6925 &(tbinfo->dobj.dumpId), 1,
6931 /* Dump Sequence Comments */
6932 resetPQExpBuffer(query);
6933 appendPQExpBuffer(query, "SEQUENCE %s", fmtId(tbinfo->relname));
6934 dumpComment(fout, query->data,
6935 tbinfo->relnamespace->nspname, tbinfo->usename,
6936 tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
6941 destroyPQExpBuffer(query);
6942 destroyPQExpBuffer(delqry);
6946 dumpTrigger(Archive *fout, TriggerInfo *tginfo)
6948 TableInfo *tbinfo = tginfo->tgtable;
6957 query = createPQExpBuffer();
6958 delqry = createPQExpBuffer();
6961 * DROP must be fully qualified in case same name appears in
6964 appendPQExpBuffer(delqry, "DROP TRIGGER %s ",
6965 fmtId(tginfo->tgname));
6966 appendPQExpBuffer(delqry, "ON %s.",
6967 fmtId(tbinfo->relnamespace->nspname));
6968 appendPQExpBuffer(delqry, "%s;\n",
6969 fmtId(tbinfo->relname));
6971 if (tginfo->tgisconstraint)
6973 appendPQExpBuffer(query, "CREATE CONSTRAINT TRIGGER ");
6974 appendPQExpBuffer(query, fmtId(tginfo->tgconstrname));
6978 appendPQExpBuffer(query, "CREATE TRIGGER ");
6979 appendPQExpBuffer(query, fmtId(tginfo->tgname));
6981 appendPQExpBuffer(query, "\n ");
6985 if (TRIGGER_FOR_BEFORE(tginfo->tgtype))
6986 appendPQExpBuffer(query, "BEFORE");
6988 appendPQExpBuffer(query, "AFTER");
6989 if (TRIGGER_FOR_INSERT(tginfo->tgtype))
6991 appendPQExpBuffer(query, " INSERT");
6994 if (TRIGGER_FOR_DELETE(tginfo->tgtype))
6997 appendPQExpBuffer(query, " OR DELETE");
6999 appendPQExpBuffer(query, " DELETE");
7002 if (TRIGGER_FOR_UPDATE(tginfo->tgtype))
7005 appendPQExpBuffer(query, " OR UPDATE");
7007 appendPQExpBuffer(query, " UPDATE");
7009 appendPQExpBuffer(query, " ON %s\n",
7010 fmtId(tbinfo->relname));
7012 if (tginfo->tgisconstraint)
7014 if (OidIsValid(tginfo->tgconstrrelid))
7016 /* If we are using regclass, name is already quoted */
7017 if (g_fout->remoteVersion >= 70300)
7018 appendPQExpBuffer(query, " FROM %s\n ",
7019 tginfo->tgconstrrelname);
7021 appendPQExpBuffer(query, " FROM %s\n ",
7022 fmtId(tginfo->tgconstrrelname));
7024 if (!tginfo->tgdeferrable)
7025 appendPQExpBuffer(query, "NOT ");
7026 appendPQExpBuffer(query, "DEFERRABLE INITIALLY ");
7027 if (tginfo->tginitdeferred)
7028 appendPQExpBuffer(query, "DEFERRED\n");
7030 appendPQExpBuffer(query, "IMMEDIATE\n");
7033 if (TRIGGER_FOR_ROW(tginfo->tgtype))
7034 appendPQExpBuffer(query, " FOR EACH ROW\n ");
7036 appendPQExpBuffer(query, " FOR EACH STATEMENT\n ");
7038 /* In 7.3, result of regproc is already quoted */
7039 if (g_fout->remoteVersion >= 70300)
7040 appendPQExpBuffer(query, "EXECUTE PROCEDURE %s(",
7043 appendPQExpBuffer(query, "EXECUTE PROCEDURE %s(",
7044 fmtId(tginfo->tgfname));
7047 for (findx = 0; findx < tginfo->tgnargs; findx++)
7053 p = strchr(p, '\\');
7056 write_msg(NULL, "invalid argument string (%s) for trigger \"%s\" on table \"%s\"\n",
7068 if (p[0] == '0' && p[1] == '0' && p[2] == '0')
7072 appendPQExpBufferChar(query, '\'');
7076 appendPQExpBufferChar(query, '\\');
7077 appendPQExpBufferChar(query, *s++);
7079 appendPQExpBufferChar(query, '\'');
7080 appendPQExpBuffer(query,
7081 (findx < tginfo->tgnargs - 1) ? ", " : "");
7084 appendPQExpBuffer(query, ");\n");
7086 ArchiveEntry(fout, tginfo->dobj.catId, tginfo->dobj.dumpId,
7088 tbinfo->relnamespace->nspname,
7090 "TRIGGER", query->data, delqry->data, NULL,
7091 tginfo->dobj.dependencies, tginfo->dobj.nDeps,
7094 resetPQExpBuffer(query);
7095 appendPQExpBuffer(query, "TRIGGER %s ",
7096 fmtId(tginfo->tgname));
7097 appendPQExpBuffer(query, "ON %s",
7098 fmtId(tbinfo->relname));
7100 dumpComment(fout, query->data,
7101 tbinfo->relnamespace->nspname, tbinfo->usename,
7102 tginfo->dobj.catId, 0, tginfo->dobj.dumpId);
7104 destroyPQExpBuffer(query);
7105 destroyPQExpBuffer(delqry);
7113 dumpRule(Archive *fout, RuleInfo *rinfo)
7115 TableInfo *tbinfo = rinfo->ruletable;
7122 * Ignore rules for not-to-be-dumped tables
7124 if (tbinfo == NULL || !tbinfo->dump || dataOnly)
7128 * If it is an ON SELECT rule, we do not need to dump it because
7129 * it will be handled via CREATE VIEW for the table.
7131 if (rinfo->ev_type == '1' && rinfo->is_instead)
7135 * Make sure we are in proper schema.
7137 selectSourceSchema(tbinfo->relnamespace->nspname);
7139 query = createPQExpBuffer();
7140 cmd = createPQExpBuffer();
7141 delcmd = createPQExpBuffer();
7143 if (g_fout->remoteVersion >= 70300)
7145 appendPQExpBuffer(query,
7146 "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid) AS definition",
7147 rinfo->dobj.catId.oid);
7151 /* Rule name was unique before 7.3 ... */
7152 appendPQExpBuffer(query,
7153 "SELECT pg_get_ruledef('%s') AS definition",
7157 res = PQexec(g_conn, query->data);
7158 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
7160 if (PQntuples(res) != 1)
7162 write_msg(NULL, "query to get rule \"%s\" for table \"%s\" failed: wrong number of rows returned",
7163 rinfo->rulename, tbinfo->relname);
7167 printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0));
7170 * DROP must be fully qualified in case same name appears in
7173 appendPQExpBuffer(delcmd, "DROP RULE %s ",
7174 fmtId(rinfo->rulename));
7175 appendPQExpBuffer(delcmd, "ON %s.",
7176 fmtId(tbinfo->relnamespace->nspname));
7177 appendPQExpBuffer(delcmd, "%s;\n",
7178 fmtId(tbinfo->relname));
7180 ArchiveEntry(fout, rinfo->dobj.catId, rinfo->dobj.dumpId,
7182 tbinfo->relnamespace->nspname,
7184 "RULE", cmd->data, delcmd->data, NULL,
7185 rinfo->dobj.dependencies, rinfo->dobj.nDeps,
7188 /* Dump rule comments */
7189 resetPQExpBuffer(query);
7190 appendPQExpBuffer(query, "RULE %s",
7191 fmtId(rinfo->rulename));
7192 appendPQExpBuffer(query, " ON %s",
7193 fmtId(tbinfo->relname));
7194 dumpComment(fout, query->data,
7195 tbinfo->relnamespace->nspname,
7197 rinfo->dobj.catId, 0, rinfo->dobj.dumpId);
7201 destroyPQExpBuffer(query);
7202 destroyPQExpBuffer(cmd);
7203 destroyPQExpBuffer(delcmd);
7207 * getDependencies --- obtain available dependency data
7210 getDependencies(void)
7221 DumpableObject *dobj,
7224 /* No dependency info available before 7.3 */
7225 if (g_fout->remoteVersion < 70300)
7229 write_msg(NULL, "fetching dependency data\n");
7231 /* Make sure we are in proper schema */
7232 selectSourceSchema("pg_catalog");
7234 query = createPQExpBuffer();
7236 appendPQExpBuffer(query, "SELECT "
7237 "classid, objid, refclassid, refobjid, deptype "
7239 "WHERE deptype != 'p' "
7242 res = PQexec(g_conn, query->data);
7243 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
7245 ntups = PQntuples(res);
7247 i_classid = PQfnumber(res, "classid");
7248 i_objid = PQfnumber(res, "objid");
7249 i_refclassid = PQfnumber(res, "refclassid");
7250 i_refobjid = PQfnumber(res, "refobjid");
7251 i_deptype = PQfnumber(res, "deptype");
7254 * Since we ordered the SELECT by referencing ID, we can expect that
7255 * multiple entries for the same object will appear together; this
7256 * saves on searches.
7260 for (i = 0; i < ntups; i++)
7266 objId.tableoid = atooid(PQgetvalue(res, i, i_classid));
7267 objId.oid = atooid(PQgetvalue(res, i, i_objid));
7268 refobjId.tableoid = atooid(PQgetvalue(res, i, i_refclassid));
7269 refobjId.oid = atooid(PQgetvalue(res, i, i_refobjid));
7270 deptype = *(PQgetvalue(res, i, i_deptype));
7273 dobj->catId.tableoid != objId.tableoid ||
7274 dobj->catId.oid != objId.oid)
7275 dobj = findObjectByCatalogId(objId);
7278 * Failure to find objects mentioned in pg_depend is not unexpected,
7279 * since for example we don't collect info about TOAST tables.
7284 fprintf(stderr, "no referencing object %u %u\n",
7285 objId.tableoid, objId.oid);
7290 refdobj = findObjectByCatalogId(refobjId);
7292 if (refdobj == NULL)
7295 fprintf(stderr, "no referenced object %u %u\n",
7296 refobjId.tableoid, refobjId.oid);
7301 addObjectDependency(dobj, refdobj->dumpId);
7306 destroyPQExpBuffer(query);
7311 * selectSourceSchema - make the specified schema the active search path
7312 * in the source database.
7314 * NB: pg_catalog is explicitly searched after the specified schema;
7315 * so user names are only qualified if they are cross-schema references,
7316 * and system names are only qualified if they conflict with a user name
7317 * in the current schema.
7319 * Whenever the selected schema is not pg_catalog, be careful to qualify
7320 * references to system catalogs and types in our emitted commands!
7323 selectSourceSchema(const char *schemaName)
7325 static char *curSchemaName = NULL;
7328 /* Not relevant if fetching from pre-7.3 DB */
7329 if (g_fout->remoteVersion < 70300)
7331 /* Ignore null schema names */
7332 if (schemaName == NULL || *schemaName == '\0')
7334 /* Optimize away repeated selection of same schema */
7335 if (curSchemaName && strcmp(curSchemaName, schemaName) == 0)
7338 query = createPQExpBuffer();
7339 appendPQExpBuffer(query, "SET search_path = %s",
7341 if (strcmp(schemaName, "pg_catalog") != 0)
7342 appendPQExpBuffer(query, ", pg_catalog");
7344 do_sql_command(g_conn, query->data);
7346 destroyPQExpBuffer(query);
7348 free(curSchemaName);
7349 curSchemaName = strdup(schemaName);
7353 * getFormattedTypeName - retrieve a nicely-formatted type name for the
7356 * NB: in 7.3 and up the result may depend on the currently-selected
7357 * schema; this is why we don't try to cache the names.
7360 getFormattedTypeName(Oid oid, OidOptions opts)
7369 if ((opts & zeroAsOpaque) != 0)
7370 return strdup(g_opaque_type);
7371 else if ((opts & zeroAsAny) != 0)
7372 return strdup("'any'");
7373 else if ((opts & zeroAsStar) != 0)
7375 else if ((opts & zeroAsNone) != 0)
7376 return strdup("NONE");
7379 query = createPQExpBuffer();
7380 if (g_fout->remoteVersion >= 70300)
7382 appendPQExpBuffer(query, "SELECT pg_catalog.format_type('%u'::pg_catalog.oid, NULL)",
7385 else if (g_fout->remoteVersion >= 70100)
7387 appendPQExpBuffer(query, "SELECT format_type('%u'::oid, NULL)",
7392 appendPQExpBuffer(query, "SELECT typname "
7394 "WHERE oid = '%u'::oid",
7398 res = PQexec(g_conn, query->data);
7399 check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
7401 /* Expecting a single result only */
7402 ntups = PQntuples(res);
7405 write_msg(NULL, "query yielded %d rows instead of one: %s\n",
7406 ntups, query->data);
7410 if (g_fout->remoteVersion >= 70100)
7412 /* already quoted */
7413 result = strdup(PQgetvalue(res, 0, 0));
7417 /* may need to quote it */
7418 result = strdup(fmtId(PQgetvalue(res, 0, 0)));
7422 destroyPQExpBuffer(query);
7428 * myFormatType --- local implementation of format_type for use with 7.0.
7431 myFormatType(const char *typname, int32 typmod)
7434 PQExpBuffer buf = createPQExpBuffer();
7436 /* Show lengths on bpchar and varchar */
7437 if (!strcmp(typname, "bpchar"))
7439 int len = (typmod - VARHDRSZ);
7441 appendPQExpBuffer(buf, "character");
7443 appendPQExpBuffer(buf, "(%d)",
7446 else if (!strcmp(typname, "varchar"))
7448 appendPQExpBuffer(buf, "character varying");
7450 appendPQExpBuffer(buf, "(%d)",
7453 else if (!strcmp(typname, "numeric"))
7455 appendPQExpBuffer(buf, "numeric");
7462 tmp_typmod = typmod - VARHDRSZ;
7463 precision = (tmp_typmod >> 16) & 0xffff;
7464 scale = tmp_typmod & 0xffff;
7465 appendPQExpBuffer(buf, "(%d,%d)",
7471 * char is an internal single-byte data type; Let's make sure we force
7472 * it through with quotes. - thomas 1998-12-13
7474 else if (strcmp(typname, "char") == 0)
7475 appendPQExpBuffer(buf, "\"char\"");
7477 appendPQExpBuffer(buf, "%s", fmtId(typname));
7479 result = strdup(buf->data);
7480 destroyPQExpBuffer(buf);
7486 * fmtQualifiedId - convert a qualified name to the proper format for
7487 * the source database.
7489 * Like fmtId, use the result before calling again.
7492 fmtQualifiedId(const char *schema, const char *id)
7494 static PQExpBuffer id_return = NULL;
7496 if (id_return) /* first time through? */
7497 resetPQExpBuffer(id_return);
7499 id_return = createPQExpBuffer();
7501 /* Suppress schema name if fetching from pre-7.3 DB */
7502 if (g_fout->remoteVersion >= 70300 && schema && *schema)
7504 appendPQExpBuffer(id_return, "%s.",
7507 appendPQExpBuffer(id_return, "%s",
7510 return id_return->data;
7514 * Return a column list clause for the given relation.
7516 * Special case: if there are no undropped columns in the relation, return
7517 * "", not an invalid "()" column list.
7520 fmtCopyColumnList(const TableInfo *ti)
7522 static PQExpBuffer q = NULL;
7523 int numatts = ti->numatts;
7524 char **attnames = ti->attnames;
7525 bool *attisdropped = ti->attisdropped;
7529 if (q) /* first time through? */
7530 resetPQExpBuffer(q);
7532 q = createPQExpBuffer();
7534 appendPQExpBuffer(q, "(");
7536 for (i = 0; i < numatts; i++)
7538 if (attisdropped[i])
7541 appendPQExpBuffer(q, ", ");
7542 appendPQExpBuffer(q, "%s", fmtId(attnames[i]));
7547 return ""; /* no undropped columns */
7549 appendPQExpBuffer(q, ")");
7554 * Convenience subroutine to execute a SQL command and check for
7555 * COMMAND_OK status.
7558 do_sql_command(PGconn *conn, const char *query)
7562 res = PQexec(conn, query);
7563 check_sql_result(res, conn, query, PGRES_COMMAND_OK);
7568 * Convenience subroutine to verify a SQL command succeeded,
7569 * and exit with a useful error message if not.
7572 check_sql_result(PGresult *res, PGconn *conn, const char *query,
7573 ExecStatusType expected)
7577 if (res && PQresultStatus(res) == expected)
7580 write_msg(NULL, "SQL command failed\n");
7582 err = PQresultErrorMessage(res);
7584 err = PQerrorMessage(conn);
7585 write_msg(NULL, "Error message from server: %s", err);
7586 write_msg(NULL, "The command was: %s\n", query);