📄 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-2003, 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 * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.355.2.5 2004/05/26 18:27:23 momjian 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_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"#define _(x) gettext((x))typedef struct _dumpContext{ TableInfo *tblinfo; int tblidx; bool oids;} DumpContext;static void help(const char *progname);static NamespaceInfo *findNamespace(const char *nsoid, const char *objoid);static void dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout, const bool oids);static void dumpComment(Archive *fout, const char *target, const char *namespace, const char *owner, const char *oid, const char *classname, int subid, const char *((*deps)[]));static void dumpOneBaseType(Archive *fout, TypeInfo *tinfo, FuncInfo *g_finfo, int numFuncs, TypeInfo *g_tinfo, int numTypes);static void dumpOneDomain(Archive *fout, TypeInfo *tinfo);static void dumpOneCompositeType(Archive *fout, TypeInfo *tinfo);static void dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo);static void dumpOneSequence(Archive *fout, TableInfo *tbinfo, const bool schemaOnly, const bool dataOnly);static void dumpTableACL(Archive *fout, TableInfo *tbinfo);static void dumpFuncACL(Archive *fout, FuncInfo *finfo);static void dumpAggACL(Archive *fout, AggInfo *finfo);static void dumpACL(Archive *fout, const char *type, const char *name, const char *tag, const char *nspname, const char *owner, const char *acl, const char *objoid);static void dumpConstraints(Archive *fout, TableInfo *tblinfo, int numTables);static void dumpTriggers(Archive *fout, TableInfo *tblinfo, int numTables);static void dumpRules(Archive *fout, TableInfo *tblinfo, int numTables);static char *format_function_signature(FuncInfo *finfo, bool honor_quotes);static void dumpOneFunc(Archive *fout, FuncInfo *finfo);static void dumpOneOpr(Archive *fout, OprInfo *oprinfo, OprInfo *g_oprinfo, int numOperators);static const char *convertRegProcReference(const char *proc);static const char *convertOperatorReference(const char *opr, OprInfo *g_oprinfo, int numOperators);static void dumpOneOpclass(Archive *fout, OpclassInfo *opcinfo);static void dumpOneAgg(Archive *fout, AggInfo *agginfo);static Oid findLastBuiltinOid_V71(const char *);static Oid findLastBuiltinOid_V70(void);static void setMaxOid(Archive *fout);static void selectSourceSchema(const char *schemaName);static char *getFormattedTypeName(const char *oid, OidOptions opts);static char *myFormatType(const char *typname, int32 typmod);static const char *fmtQualifiedId(const char *schema, const char *id);static int dumpBlobs(Archive *AH, char *, void *);static int dumpDatabase(Archive *AH);static void dumpEncoding(Archive *AH);static const char *getAttrName(int attrnum, TableInfo *tblInfo);static const char *fmtCopyColumnList(const TableInfo *ti);extern char *optarg;extern int optind, opterr;/* 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 dumpData; /* dump data using proper insert strings */bool attrNames; /* put attr names into insert strings */bool schemaOnly;bool dataOnly;bool aclsSkip;/* 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];/* these are to avoid passing around info for findNamespace() */static NamespaceInfo *g_namespaces;static int g_numNamespaces;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; bool oids = false; PGresult *res; TableInfo *tblinfo; int numTables; bool force_password = false; int compressLevel = -1; bool ignore_version = false; int plainText = 0; int outputClean = 0; int outputCreate = 0; int outputBlobs = 0; 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'}, {"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' */ {"use-set-session-authorization", no_argument, &use_setsessauth, 1}, {"disable-triggers", no_argument, &disable_triggers, 1}, {NULL, 0, NULL, 0} }; int optindex;#ifdef ENABLE_NLS setlocale(LC_ALL, ""); bindtextdomain("pg_dump", LOCALEDIR); textdomain("pg_dump");#endif g_verbose = false; strcpy(g_comment_start, "-- "); g_comment_end[0] = '\0'; strcpy(g_opaque_type, "opaque"); dataOnly = schemaOnly = dumpData = attrNames = false; progname = get_progname(argv[0]); /* Set default options based on progname */ if (strcmp(progname, "pg_backup") == 0) { format = "c"; outputBlobs = true; } 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, "abcCdDf: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 */ outputBlobs = true; 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 */ dumpData = true; break; case 'D': /* dump data as proper insert strings with * attr names */ dumpData = true; attrNames = true; 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; 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, "use-set-session-authorization") == 0) /* no-op, still allowed for compatibility */ ; else if (strcmp(optarg, "disable-triggers") == 0) disable_triggers = 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 (outputBlobs && selectTableName != NULL) { write_msg(NULL, "large-object output not supported for a single table\n"); write_msg(NULL, "use a full dump instead\n"); exit(1); } if (outputBlobs && selectSchemaName != NULL) { write_msg(NULL, "large-object output not supported for a single schema\n"); write_msg(NULL, "use a full dump instead\n"); exit(1); } if (dumpData == 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); } if (outputBlobs == true && (format[0] == 'p' || format[0] == 'P')) { write_msg(NULL, "large-object output is not supported for plain-text dump files\n"); write_msg(NULL, "(Use a different output format.)\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); break; case 'p': case 'P': plainText = 1; g_fout = CreateArchive(filename, archNull, 0); break; case 't': case 'T':
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -