28#include "catalog/pg_authid_d.h"
43#define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
52#define SH_PREFIX rolename
53#define SH_ELEMENT_TYPE RoleNameEntry
54#define SH_KEY_TYPE char *
55#define SH_KEY rolename
56#define SH_HASH_KEY(tb, key) hash_string(key)
57#define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
59#define SH_GET_HASH(tb, a) (a)->hashval
60#define SH_SCOPE static inline
61#define SH_RAW_ALLOCATOR pg_malloc0
66static void help(
void);
81 const char *objtype,
const char *objname,
128#define PG_AUTHID "pg_authid"
129#define PG_ROLES "pg_roles "
215 char *use_role =
NULL;
237 if (
strcmp(argv[1],
"--help") == 0 ||
strcmp(argv[1],
"-?") == 0)
242 if (
strcmp(argv[1],
"--version") == 0 ||
strcmp(argv[1],
"-V") == 0)
258 pg_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
261 pg_fatal(
"program \"%s\" was found by \"%s\" but was not the same version as %s",
415 pg_log_error(
"too many command-line arguments (first is \"%s\")",
424 pg_log_error(
"option %s cannot be used together with %s, %s, or %s",
425 "--exclude-database",
426 "-g/--globals-only",
"-r/--roles-only",
"-t/--tablespaces-only");
434 pg_log_error(
"options %s and %s cannot be used together",
435 "-g/--globals-only",
"-r/--roles-only");
442 pg_log_error(
"options %s and %s cannot be used together",
443 "-g/--globals-only",
"-t/--tablespaces-only");
449 pg_fatal(
"option %s requires option %s",
450 "--if-exists",
"-c/--clean");
454 pg_log_error(
"options %s and %s cannot be used together",
455 "-r/--roles-only",
"-t/--tablespaces-only");
471 "-F/--format",
"-f/--file");
478 pg_fatal(
"option %s can only be used with %s=plain",
479 "--restrict-key",
"--format");
483 pg_fatal(
"options %s and %s cannot be used together in non-text dump",
484 "--clean",
"-g/--globals-only");
561 pg_fatal(
"could not open output file \"%s\": %m",
573 pg_fatal(
"could not generate restrict key");
585 prompt_password,
false,
594 prompt_password,
false,
598 prompt_password,
true,
603 pg_log_error(
"could not connect to databases \"postgres\" or \"template1\"\n"
604 "Please specify an alternative database.");
622 pg_fatal(
"invalid client encoding \"%s\" specified",
706 .createStmt = qry->
data));
721 .createStmt = qry->
data));
732 .createStmt = qry->
data));
737 fprintf(
OPF,
"--\n-- PostgreSQL database cluster dump\n--\n\n");
760 fprintf(
OPF,
"SET default_transaction_read_only = off;\n\n");
765 fprintf(
OPF,
"SET standard_conforming_strings = on;\n");
835 fprintf(
OPF,
"--\n-- PostgreSQL database cluster dump complete\n--\n\n");
865 printf(
_(
"%s exports a PostgreSQL database cluster as an SQL script or to other formats.\n\n"),
progname);
869 printf(
_(
"\nGeneral options:\n"));
870 printf(
_(
" -f, --file=FILENAME output file name\n"));
871 printf(
_(
" -F, --format=c|d|t|p output file format (custom, directory, tar,\n"
872 " plain text (default))\n"));
873 printf(
_(
" -v, --verbose verbose mode\n"));
874 printf(
_(
" -V, --version output version information, then exit\n"));
875 printf(
_(
" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
876 printf(
_(
" -?, --help show this help, then exit\n"));
877 printf(
_(
"\nOptions controlling the output content:\n"));
878 printf(
_(
" -a, --data-only dump only the data, not the schema or statistics\n"));
879 printf(
_(
" -c, --clean clean (drop) databases before recreating\n"));
880 printf(
_(
" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
881 printf(
_(
" -g, --globals-only dump only global objects, no databases\n"));
882 printf(
_(
" -O, --no-owner skip restoration of object ownership\n"));
883 printf(
_(
" -r, --roles-only dump only roles, no databases or tablespaces\n"));
884 printf(
_(
" -s, --schema-only dump only the schema, no data or statistics\n"));
885 printf(
_(
" -S, --superuser=NAME superuser user name to use in the dump\n"));
886 printf(
_(
" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
887 printf(
_(
" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
888 printf(
_(
" --binary-upgrade for use by upgrade utilities only\n"));
889 printf(
_(
" --column-inserts dump data as INSERT commands with column names\n"));
890 printf(
_(
" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
891 printf(
_(
" --disable-triggers disable triggers during data-only restore\n"));
892 printf(
_(
" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
893 printf(
_(
" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
894 printf(
_(
" --filter=FILENAME exclude databases based on expressions in FILENAME\n"));
895 printf(
_(
" --if-exists use IF EXISTS when dropping objects\n"));
896 printf(
_(
" --inserts dump data as INSERT commands, rather than COPY\n"));
897 printf(
_(
" --load-via-partition-root load partitions via the root table\n"));
898 printf(
_(
" --no-comments do not dump comment commands\n"));
899 printf(
_(
" --no-data do not dump data\n"));
900 printf(
_(
" --no-policies do not dump row security policies\n"));
901 printf(
_(
" --no-publications do not dump publications\n"));
902 printf(
_(
" --no-role-passwords do not dump passwords for roles\n"));
903 printf(
_(
" --no-schema do not dump schema\n"));
904 printf(
_(
" --no-security-labels do not dump security label assignments\n"));
905 printf(
_(
" --no-statistics do not dump statistics\n"));
906 printf(
_(
" --no-subscriptions do not dump subscriptions\n"));
907 printf(
_(
" --no-sync do not wait for changes to be written safely to disk\n"));
908 printf(
_(
" --no-table-access-method do not dump table access methods\n"));
909 printf(
_(
" --no-tablespaces do not dump tablespace assignments\n"));
910 printf(
_(
" --no-toast-compression do not dump TOAST compression methods\n"));
911 printf(
_(
" --no-unlogged-table-data do not dump unlogged table data\n"));
912 printf(
_(
" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
913 printf(
_(
" --quote-all-identifiers quote all identifiers, even if not key words\n"));
914 printf(
_(
" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
915 printf(
_(
" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
916 printf(
_(
" --sequence-data include sequence data in dump\n"));
917 printf(
_(
" --statistics dump the statistics\n"));
918 printf(
_(
" --statistics-only dump only the statistics, not schema or data\n"));
919 printf(
_(
" --use-set-session-authorization\n"
920 " use SET SESSION AUTHORIZATION commands instead of\n"
921 " ALTER OWNER commands to set ownership\n"));
923 printf(
_(
"\nConnection options:\n"));
924 printf(
_(
" -d, --dbname=CONNSTR connect using connection string\n"));
925 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
926 printf(
_(
" -l, --database=DBNAME alternative default database\n"));
927 printf(
_(
" -p, --port=PORT database server port number\n"));
928 printf(
_(
" -U, --username=NAME connect as specified database user\n"));
929 printf(
_(
" -w, --no-password never prompt for password\n"));
930 printf(
_(
" -W, --password force password prompt (should happen automatically)\n"));
931 printf(
_(
" --role=ROLENAME do SET ROLE before dump\n"));
933 printf(
_(
"\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
955 "WHERE rolname !~ '^pg_' "
972 const char *rolename;
995 .createStmt =
delQry->data));
1040 "SELECT oid, rolname, rolsuper, rolinherit, "
1041 "rolcreaterole, rolcreatedb, "
1042 "rolcanlogin, rolconnlimit, rolpassword, "
1043 "rolvaliduntil, rolreplication, rolbypassrls, "
1044 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
1045 "rolname = current_user AS is_current_user "
1047 "WHERE rolname !~ '^pg_' "
1051 "SELECT oid, rolname, rolsuper, rolinherit, "
1052 "rolcreaterole, rolcreatedb, "
1053 "rolcanlogin, rolconnlimit, rolpassword, "
1054 "rolvaliduntil, rolreplication, rolbypassrls, "
1055 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
1056 "rolname = current_user AS is_current_user "
1061 "SELECT oid, rolname, rolsuper, rolinherit, "
1062 "rolcreaterole, rolcreatedb, "
1063 "rolcanlogin, rolconnlimit, rolpassword, "
1064 "rolvaliduntil, rolreplication, "
1065 "false as rolbypassrls, "
1066 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
1067 "rolname = current_user AS is_current_user "
1093 const char *rolename;
1099 if (
strncmp(rolename,
"pg_", 3) == 0)
1114 "SELECT pg_catalog.binary_upgrade_set_next_pg_authid_oid('%u'::pg_catalog.oid);\n\n",
1213 .createStmt =
buf->data));
1240 fprintf(
OPF,
"\n--\n-- User Configurations\n--\n");
1304 "um.rolname AS member, "
1305 "ug.rolname AS grantor, "
1306 "a.roleid AS roleid, "
1307 "a.member AS memberid, "
1308 "a.grantor AS grantorid, "
1313 "LEFT JOIN %s ur on ur.oid = a.roleid "
1314 "LEFT JOIN %s um on um.oid = a.member "
1315 "LEFT JOIN %s ug on ug.oid = a.grantor "
1316 "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
1330 fprintf(
OPF,
"--\n-- Role memberships\n--\n\n");
1347 while (
start < total)
1360 pg_log_warning(
"found orphaned pg_auth_members entry for role %s",
1366 for (end =
start; end < total; ++end)
1393 pg_log_error(
"could not find a legal dump ordering for memberships in role \"%s\"",
1418 pg_log_warning(
"found orphaned pg_auth_members entry for role %s",
1427 pg_log_warning(
"found orphaned pg_auth_members entry for role %s",
1474 if (
optbuf->data[0] !=
'\0')
1483 if (
optbuf->data[0] !=
'\0')
1487 if (
optbuf->data[0] !=
'\0')
1541 "FROM pg_catalog.pg_parameter_acl "
1545 fprintf(
OPF,
"--\n-- Role privileges on configuration parameters\n--\n\n");
1563 pg_log_error(
"could not parse ACL list (%s) for parameter \"%s\"",
1578 .createStmt =
buf->data));
1605 "FROM pg_catalog.pg_tablespace "
1606 "WHERE spcname !~ '^pg_' "
1610 fprintf(
OPF,
"--\n-- Drop tablespaces\n--\n\n");
1634 .createStmt =
delQry->data));
1662 "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1663 "pg_catalog.pg_tablespace_location(oid), "
1664 "spcacl, acldefault('t', spcowner) AS acldefault, "
1665 "array_to_string(spcoptions, ', '),"
1666 "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1667 "FROM pg_catalog.pg_tablespace "
1668 "WHERE spcname !~ '^pg_' "
1726 pg_log_error(
"could not parse ACL list (%s) for tablespace \"%s\"",
1764 .createStmt =
buf->data));
1813 "FROM pg_database d "
1814 "WHERE datallowconn AND datconnlimit != -2 "
1815 "ORDER BY datname");
1818 fprintf(
OPF,
"--\n-- Drop databases (except postgres and template1)\n--\n\n");
1852 .createStmt =
delQry->data));
1876 "WHERE setdatabase = 0 AND setrole = "
1877 "(SELECT oid FROM %s WHERE rolname = ",
1909 .createStmt =
buf->data));
1945 "SELECT datname FROM pg_catalog.pg_database n\n");
1952 pg_log_error(
"improper qualified name (too many dotted names): %s",
1995 "SELECT datname, oid "
1996 "FROM pg_database d "
1997 "WHERE datallowconn AND datconnlimit != -2 "
1998 "ORDER BY (datname <> 'template1'), datname");
2012 "#################################################################",
2015 "# This file maps oids to database names",
2017 "# pg_restore will restore all the databases listed here, unless",
2018 "# otherwise excluded. You can also inhibit restoration of a",
2019 "# database by removing the line or commenting out the line with"
2021 "#################################################################",
2119 pg_fatal(
"could not re-open the output file \"%s\": %m",
2211 const char *objtype,
const char *objname,
2270 "SELECT datname AS objname, 'database' AS objtype "
2271 "FROM pg_catalog.pg_database "
2272 "WHERE datallowconn AND datconnlimit != -2 "
2274 "SELECT rolname AS objname, 'role' AS objtype "
2275 "FROM pg_catalog.pg_roles "
2277 "SELECT spcname AS objname, 'tablespace' AS objtype "
2278 "FROM pg_catalog.pg_tablespace");
2288 if (
strcmp(objtype,
"database") == 0 &&
2295 for (
char *p = objname; *p; p++)
2299 else if (*p ==
'\r')
2312 pg_fatal(
"database, role, or tablespace names contain a newline or carriage return character, which is not supported in non-plain-text dumps:\n%s",
2418 pg_fatal(
"unrecognized output format \"%s\"; please specify \"c\", \"d\", \"p\", or \"t\"",
Acl * acldefault(ObjectType objtype, Oid ownerId)
Datum now(PG_FUNCTION_ARGS)
void on_exit_close_archive(Archive *AHX)
#define Assert(condition)
#define PG_TEXTDOMAIN(domain)
int find_my_exec(const char *argv0, char *retpath)
void set_pglocale_pgservice(const char *argv0, const char *app)
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
PGresult * executeQuery(PGconn *conn, const char *query)
PGconn * ConnectDatabase(const char *dbname, const char *connection_string, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error, const char *progname, const char **connstr, int *server_version, char *password, char *override_dbname)
#define fprintf(file, fmt, msg)
char * generate_restrict_key(void)
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *baseacls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
bool valid_restrict_key(const char *restrict_key)
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
char * sanitize_line(const char *str, bool want_hyphen)
void create_or_open_dir(const char *dirname)
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
#define PGDUMP_STRFTIME_FMT
void fsync_fname(const char *fname, bool isdir)
int PQserverVersion(const PGconn *conn)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
int PQsetClientEncoding(PGconn *conn, const char *encoding)
int PQfnumber(const PGresult *res, const char *field_name)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
#define pg_malloc0_array(type, count)
@ DATA_DIR_SYNC_METHOD_FSYNC
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
void filter_free(FilterStateData *fstate)
const char * filter_object_type_name(FilterObjectType fot)
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
@ FILTER_OBJECT_TYPE_SCHEMA
@ FILTER_OBJECT_TYPE_INDEX
@ FILTER_OBJECT_TYPE_TRIGGER
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
@ FILTER_OBJECT_TYPE_DATABASE
@ FILTER_OBJECT_TYPE_FUNCTION
@ FILTER_OBJECT_TYPE_TABLE_DATA
@ FILTER_OBJECT_TYPE_NONE
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
@ FILTER_OBJECT_TYPE_EXTENSION
@ FILTER_OBJECT_TYPE_TABLE
@ FILTER_COMMAND_TYPE_INCLUDE
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
void pg_logging_increase_verbosity(void)
void pg_logging_init(const char *argv0)
void pg_logging_set_level(enum pg_log_level new_level)
#define pg_log_error(...)
#define pg_log_error_hint(...)
#define pg_log_error_detail(...)
void ProcessArchiveRestoreOptions(Archive *AHX)
RestoreOptions * NewRestoreOptions(void)
#define appendStringLiteralAH(buf, str, AH)
enum _archiveFormat ArchiveFormat
void CloseArchive(Archive *AHX)
Archive * CreateArchive(const char *FileSpec, const ArchiveFormat fmt, const pg_compress_specification compression_spec, bool dosync, ArchiveMode mode, SetupWorkerPtrType setupDumpWorker, DataDirSyncMethod sync_method)
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
void InitDumpOptions(DumpOptions *opts)
TocEntry * ArchiveEntry(Archive *AHX, CatalogId catalogId, DumpId dumpId, ArchiveOpts *opts)
#define ARCHIVE_OPTS(...)
void exit_nicely(int code)
static void dumpTimestamp(const char *msg)
static const CatalogId nilCatalogId
static int on_conflict_do_nothing
static ArchiveFormat archDumpFormat
static void dropTablespaces(PGconn *conn)
static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns, SimpleStringList *names)
static int no_role_passwords
static PQExpBuffer pgdumpopts
static int statistics_only
static int no_table_access_method
static int no_unlogged_table_data
static void check_for_invalid_global_names(PGconn *conn, SimpleStringList *database_exclude_names)
static int binary_upgrade
static int disable_triggers
static void dumpUserConfig(PGconn *conn, const char *username)
static const char * connstr
static SimpleStringList database_exclude_patterns
static void dumpTablespaces(PGconn *conn)
static void dumpRoleMembership(PGconn *conn)
static int createDumpId(void)
static char pg_dump_bin[MAXPGPATH]
static int runPgDump(const char *dbname, const char *create_opts, char *dbfile)
static SimpleStringList database_exclude_names
static int no_publications
static char * restrict_key
static int no_security_labels
static void dumpDatabases(PGconn *conn)
static ArchiveFormat parseDumpFormat(const char *format)
static int disable_dollar_quoting
static void buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
static int no_tablespaces
static int no_toast_compression
static void executeCommand(PGconn *conn, const char *query)
static void dumpRoleGUCPrivs(PGconn *conn)
static void dumpRoles(PGconn *conn)
static int use_setsessauth
static int load_via_partition_root
static int column_inserts
static void read_dumpall_filters(const char *filename, SimpleStringList *pattern)
static void dropRoles(PGconn *conn)
static void dropDBs(PGconn *conn)
static int server_version
static char role_catalog[10]
static int no_subscriptions
static int with_statistics
#define PGDUMP_VERSIONSTR
PGDLLIMPORT char * optarg
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define pg_encoding_to_char
static const char * pghost
static const char * pgport
#define pg_log_warning(...)
#define is_absolute_path(filename)
int pg_strcasecmp(const char *s1, const char *s2)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
PQExpBuffer createPQExpBuffer(void)
void initPQExpBuffer(PQExpBuffer str)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
char * psprintf(const char *fmt,...)
bool quote_all_identifiers
bool simple_string_list_member(SimpleStringList *list, const char *val)
void simple_string_list_append(SimpleStringList *list, const char *val)
const char * fmtId(const char *rawid)
void setFmtEncoding(int encoding)
void appendShellString(PQExpBuffer buf, const char *str)
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
bool processSQLNamePattern(PGconn *conn, PQExpBuffer buf, const char *pattern, bool have_where, bool force_escape, const char *schemavar, const char *namevar, const char *altnamevar, const char *visibilityrule, PQExpBuffer dbnamebuf, int *dotcnt)
void appendConnStrVal(PQExpBuffer buf, const char *str)
SimpleStringListCell * head