📄 pg_dump.c
字号:
/*------------------------------------------------------------------------- * * pg_dump.c * pg_dump is a utility for dumping out a postgres database * into a script file. * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * pg_dump will read the system catalogs in a database and dump out a * script that reproduces the schema in terms of SQL that is understood * by PostgreSQL * * IDENTIFICATION * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.422.2.3 2006/02/21 18:01:41 tgl Exp $ * *------------------------------------------------------------------------- *//* * Although this is not a backend module, we must include postgres.h anyway * so that we can include a bunch of backend include files. pg_dump has * never pretended to be very independent of the backend anyhow ... */#include "postgres.h"#include <unistd.h>#include <ctype.h>#ifdef ENABLE_NLS#include <locale.h>#endif#ifdef HAVE_TERMIOS_H#include <termios.h>#endif#ifndef HAVE_STRDUP#include "strdup.h"#endif#include "getopt_long.h"#ifndef HAVE_INT_OPTRESETint optreset;#endif#include "access/attnum.h"#include "access/htup.h"#include "catalog/pg_class.h"#include "catalog/pg_proc.h"#include "catalog/pg_trigger.h"#include "catalog/pg_type.h"#include "commands/sequence.h"#include "libpq-fe.h"#include "libpq/libpq-fs.h"#include "pg_dump.h"#include "pg_backup.h"#include "pg_backup_archiver.h"#include "dumputils.h"extern char *optarg;extern int optind, opterr;typedef struct{ const char *descr; /* comment for an object */ Oid classoid; /* object class (catalog OID) */ Oid objoid; /* object OID */ int objsubid; /* subobject (table column #) */} CommentItem;/* global decls */bool g_verbose; /* User wants verbose narration of our * activities. */Archive *g_fout; /* the script file */PGconn *g_conn; /* the database connection *//* various user-settable parameters */bool dumpInserts; /* dump data using proper insert strings */bool attrNames; /* put attr names into insert strings */bool schemaOnly;bool dataOnly;bool aclsSkip;/* subquery used to convert user ID (eg, datdba) to user name */static const char *username_subquery;/* obsolete as of 7.3: */static Oid g_last_builtin_oid; /* value of the last builtin oid */static char *selectTableName = NULL; /* name of a single table to dump */static char *selectSchemaName = NULL; /* name of a single schema to dump */char g_opaque_type[10]; /* name for the opaque type *//* placeholders for the delimiters for comments */char g_comment_start[10];char g_comment_end[10];static const CatalogId nilCatalogId = {0, 0};/* these are to avoid passing around info for findNamespace() */static NamespaceInfo *g_namespaces;static int g_numNamespaces;/* flag to turn on/off dollar quoting */static int disable_dollar_quoting = 0;static void help(const char *progname);static NamespaceInfo *findNamespace(Oid nsoid, Oid objoid);static void dumpTableData(Archive *fout, TableDataInfo *tdinfo);static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, CatalogId catalogId, int subid, DumpId dumpId);static int findComments(Archive *fout, Oid classoid, Oid objoid, CommentItem **items);static int collectComments(Archive *fout, CommentItem **items);static void dumpDumpableObject(Archive *fout, DumpableObject *dobj);static void dumpNamespace(Archive *fout, NamespaceInfo *nspinfo);static void dumpType(Archive *fout, TypeInfo *tinfo);static void dumpBaseType(Archive *fout, TypeInfo *tinfo);static void dumpDomain(Archive *fout, TypeInfo *tinfo);static void dumpCompositeType(Archive *fout, TypeInfo *tinfo);static void dumpProcLang(Archive *fout, ProcLangInfo *plang);static void dumpFunc(Archive *fout, FuncInfo *finfo);static void dumpCast(Archive *fout, CastInfo *cast);static void dumpOpr(Archive *fout, OprInfo *oprinfo);static void dumpOpclass(Archive *fout, OpclassInfo *opcinfo);static void dumpConversion(Archive *fout, ConvInfo *convinfo);static void dumpRule(Archive *fout, RuleInfo *rinfo);static void dumpAgg(Archive *fout, AggInfo *agginfo);static void dumpTrigger(Archive *fout, TriggerInfo *tginfo);static void dumpTable(Archive *fout, TableInfo *tbinfo);static void dumpTableSchema(Archive *fout, TableInfo *tbinfo);static void dumpAttrDef(Archive *fout, AttrDefInfo *adinfo);static void dumpSequence(Archive *fout, TableInfo *tbinfo);static void dumpIndex(Archive *fout, IndxInfo *indxinfo);static void dumpConstraint(Archive *fout, ConstraintInfo *coninfo);static void dumpTableConstraintComment(Archive *fout, ConstraintInfo *coninfo);static void dumpACL(Archive *fout, CatalogId objCatId, DumpId objDumpId, const char *type, const char *name, const char *tag, const char *nspname, const char *owner, const char *acls);static void getDependencies(void);static void getDomainConstraints(TypeInfo *tinfo);static void getTableData(TableInfo *tblinfo, int numTables, bool oids);static char *format_function_arguments(FuncInfo *finfo, int nallargs, char **allargtypes, char **argmodes, char **argnames);static char *format_function_signature(FuncInfo *finfo, bool honor_quotes);static const char *convertRegProcReference(const char *proc);static const char *convertOperatorReference(const char *opr);static Oid findLastBuiltinOid_V71(const char *);static Oid findLastBuiltinOid_V70(void);static void selectSourceSchema(const char *schemaName);static char *getFormattedTypeName(Oid oid, OidOptions opts);static char *myFormatType(const char *typname, int32 typmod);static const char *fmtQualifiedId(const char *schema, const char *id);static bool hasBlobs(Archive *AH);static int dumpBlobs(Archive *AH, void *arg);static int dumpBlobComments(Archive *AH, void *arg);static void dumpDatabase(Archive *AH);static void dumpEncoding(Archive *AH);static const char *getAttrName(int attrnum, TableInfo *tblInfo);static const char *fmtCopyColumnList(const TableInfo *ti);static void do_sql_command(PGconn *conn, const char *query);static void check_sql_result(PGresult *res, PGconn *conn, const char *query, ExecStatusType expected);intmain(int argc, char **argv){ int c; const char *filename = NULL; const char *format = "p"; const char *dbname = NULL; const char *pghost = NULL; const char *pgport = NULL; const char *username = NULL; const char *dumpencoding = NULL; bool oids = false; TableInfo *tblinfo; int numTables; DumpableObject **dobjs; int numObjs; int i; bool force_password = false; int compressLevel = -1; bool ignore_version = false; int plainText = 0; int outputClean = 0; int outputCreate = 0; bool outputBlobs = true; int outputNoOwner = 0; static int use_setsessauth = 0; static int disable_triggers = 0; char *outputSuperuser = NULL; RestoreOptions *ropt; static struct option long_options[] = { {"data-only", no_argument, NULL, 'a'}, {"blobs", no_argument, NULL, 'b'}, {"clean", no_argument, NULL, 'c'}, {"create", no_argument, NULL, 'C'}, {"file", required_argument, NULL, 'f'}, {"format", required_argument, NULL, 'F'}, {"inserts", no_argument, NULL, 'd'}, {"attribute-inserts", no_argument, NULL, 'D'}, {"column-inserts", no_argument, NULL, 'D'}, {"host", required_argument, NULL, 'h'}, {"ignore-version", no_argument, NULL, 'i'}, {"no-reconnect", no_argument, NULL, 'R'}, {"oids", no_argument, NULL, 'o'}, {"no-owner", no_argument, NULL, 'O'}, {"port", required_argument, NULL, 'p'}, {"schema", required_argument, NULL, 'n'}, {"schema-only", no_argument, NULL, 's'}, {"superuser", required_argument, NULL, 'S'}, {"table", required_argument, NULL, 't'}, {"password", no_argument, NULL, 'W'}, {"username", required_argument, NULL, 'U'}, {"verbose", no_argument, NULL, 'v'}, {"no-privileges", no_argument, NULL, 'x'}, {"no-acl", no_argument, NULL, 'x'}, {"compress", required_argument, NULL, 'Z'}, {"encoding", required_argument, NULL, 'E'}, {"help", no_argument, NULL, '?'}, {"version", no_argument, NULL, 'V'}, /* * the following options don't have an equivalent short option letter, * but are available as '-X long-name' */ {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1}, {"disable-triggers", no_argument, &disable_triggers, 1}, {"use-set-session-authorization", no_argument, &use_setsessauth, 1}, {NULL, 0, NULL, 0} }; int optindex; set_pglocale_pgservice(argv[0], "pg_dump"); g_verbose = false; strcpy(g_comment_start, "-- "); g_comment_end[0] = '\0'; strcpy(g_opaque_type, "opaque"); dataOnly = schemaOnly = dumpInserts = attrNames = false; progname = get_progname(argv[0]); /* Set default options based on progname */ if (strcmp(progname, "pg_backup") == 0) format = "c"; if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { help(progname); exit(0); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { puts("pg_dump (PostgreSQL) " PG_VERSION); exit(0); } } while ((c = getopt_long(argc, argv, "abcCdDE:f:F:h:in:oOp:RsS:t:uU:vWxX:Z:", long_options, &optindex)) != -1) { switch (c) { case 'a': /* Dump data only */ dataOnly = true; break; case 'b': /* Dump blobs */ /* this is now default, so just ignore the switch */ break; case 'c': /* clean (i.e., drop) schema prior to create */ outputClean = 1; break; case 'C': /* Create DB */ outputCreate = 1; break; case 'd': /* dump data as proper insert strings */ dumpInserts = true; break; case 'D': /* dump data as proper insert strings with * attr names */ dumpInserts = true; attrNames = true; break; case 'E': /* Dump encoding */ dumpencoding = optarg; break; case 'f': filename = optarg; break; case 'F': format = optarg; break; case 'h': /* server host */ pghost = optarg; break; case 'i': /* ignore database version mismatch */ ignore_version = true; break; case 'n': /* Dump data for this schema only */ selectSchemaName = strdup(optarg); break; case 'o': /* Dump oids */ oids = true; break; case 'O': /* Don't reconnect to match owner */ outputNoOwner = 1; break; case 'p': /* server port */ pgport = optarg; break; case 'R': /* no-op, still accepted for backwards compatibility */ break; case 's': /* dump schema only */ schemaOnly = true; outputBlobs = false; break; case 'S': /* Username for superuser in plain text output */ outputSuperuser = strdup(optarg); break; case 't': /* Dump data for this table only */ selectTableName = strdup(optarg); break; case 'u': force_password = true; username = simple_prompt("User name: ", 100, true); break; case 'U': username = optarg; break; case 'v': /* verbose */ g_verbose = true; break; case 'W': force_password = true; break; case 'x': /* skip ACL dump */ aclsSkip = true; break; /* * Option letters were getting scarce, so I invented this new * scheme: '-X feature' turns on some feature. Compare to the * -f option in GCC. You should also add an equivalent * GNU-style option --feature. Features that require * arguments should use '-X feature=foo'. */ case 'X': if (strcmp(optarg, "disable-dollar-quoting") == 0) disable_dollar_quoting = 1; else if (strcmp(optarg, "disable-triggers") == 0) disable_triggers = 1; else if (strcmp(optarg, "use-set-session-authorization") == 0) use_setsessauth = 1; else { fprintf(stderr, _("%s: invalid -X option -- %s\n"), progname, optarg); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } break; case 'Z': /* Compression Level */ compressLevel = atoi(optarg); break; /* This covers the long options equivalent to -X xxx. */ case 0: break; default: fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } } if (optind < (argc - 1)) { fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind + 1]); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); exit(1); } /* Get database name from command line */ if (optind < argc) dbname = argv[optind]; if (dataOnly && schemaOnly) { write_msg(NULL, "options \"schema only\" (-s) and \"data only\" (-a) cannot be used together\n"); exit(1); } if (dataOnly && outputClean) { write_msg(NULL, "options \"clean\" (-c) and \"data only\" (-a) cannot be used together\n"); exit(1); } if (selectTableName != NULL || selectSchemaName != NULL) outputBlobs = false; if (dumpInserts == true && oids == true) { write_msg(NULL, "INSERT (-d, -D) and OID (-o) options cannot be used together\n"); write_msg(NULL, "(The INSERT command cannot set OIDs.)\n"); exit(1); } /* open the output file */ switch (format[0]) { case 'c': case 'C': g_fout = CreateArchive(filename, archCustom, compressLevel); break; case 'f': case 'F': g_fout = CreateArchive(filename, archFiles, compressLevel);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -