21#include "catalog/pg_authid_d.h"
34#define PGDUMP_VERSIONSTR "pg_dump (PostgreSQL) " PG_VERSION "\n"
43#define SH_PREFIX rolename
44#define SH_ELEMENT_TYPE RoleNameEntry
45#define SH_KEY_TYPE char *
46#define SH_KEY rolename
47#define SH_HASH_KEY(tb, key) hash_string(key)
48#define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
50#define SH_GET_HASH(tb, a) (a)->hashval
51#define SH_SCOPE static inline
52#define SH_RAW_ALLOCATOR pg_malloc0
57static void help(
void);
71 const char *catalog_name,
Oid objectId,
72 const char *objtype,
const char *objname,
76 const char *
pgport,
const char *pguser,
77 trivalue prompt_password,
bool fail_on_error);
115#define PG_AUTHID "pg_authid"
116#define PG_ROLES "pg_roles "
124#define exit_nicely(code) exit(code)
129 static struct option long_options[] = {
189 char *use_role = NULL;
190 const char *dumpencoding = NULL;
192 bool data_only =
false;
193 bool globals_only =
false;
194 bool roles_only =
false;
195 bool tablespaces_only =
false;
198 const char *std_strings;
210 if (strcmp(argv[1],
"--help") == 0 || strcmp(argv[1],
"-?") == 0)
215 if (strcmp(argv[1],
"--version") == 0 || strcmp(argv[1],
"-V") == 0)
217 puts(
"pg_dumpall (PostgreSQL) " PG_VERSION);
231 pg_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
234 pg_fatal(
"program \"%s\" was found by \"%s\" but was not the same version as %s",
240 while ((
c =
getopt_long(argc, argv,
"acd:E:f:gh:l:Op:rsS:tU:vwWx", long_options, &optindex)) != -1)
303 tablespaces_only =
true;
378 pg_log_error(
"too many command-line arguments (first is \"%s\")",
385 (globals_only || roles_only || tablespaces_only))
387 pg_log_error(
"option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only, or -t/--tablespaces-only");
393 if (globals_only && roles_only)
395 pg_log_error(
"options -g/--globals-only and -r/--roles-only cannot be used together");
400 if (globals_only && tablespaces_only)
402 pg_log_error(
"options -g/--globals-only and -t/--tablespaces-only cannot be used together");
408 pg_fatal(
"option --if-exists requires option -c/--clean");
410 if (roles_only && tablespaces_only)
412 pg_log_error(
"options -r/--roles-only and -t/--tablespaces-only cannot be used together");
471 prompt_password,
false);
474 pg_fatal(
"could not connect to database \"%s\"", pgdb);
479 prompt_password,
false);
482 prompt_password,
true);
486 pg_log_error(
"could not connect to databases \"postgres\" or \"template1\"\n"
487 "Please specify an alternative database.");
506 pg_fatal(
"could not open output file \"%s\": %m",
518 pg_fatal(
"invalid client encoding \"%s\" specified",
545 fprintf(
OPF,
"--\n-- PostgreSQL database cluster dump\n--\n\n");
557 fprintf(
OPF,
"SET default_transaction_read_only = off;\n\n");
562 fprintf(
OPF,
"SET standard_conforming_strings = %s;\n", std_strings);
563 if (strcmp(std_strings,
"off") == 0)
564 fprintf(
OPF,
"SET escape_string_warning = off;\n");
577 if (!globals_only && !roles_only && !tablespaces_only)
583 if (!tablespaces_only)
591 if (!tablespaces_only)
609 if (!globals_only && !roles_only && !tablespaces_only)
616 fprintf(
OPF,
"--\n-- PostgreSQL database cluster dump complete\n--\n\n");
634 printf(
_(
"%s extracts a PostgreSQL database cluster into an SQL script file.\n\n"),
progname);
638 printf(
_(
"\nGeneral options:\n"));
639 printf(
_(
" -f, --file=FILENAME output file name\n"));
640 printf(
_(
" -v, --verbose verbose mode\n"));
641 printf(
_(
" -V, --version output version information, then exit\n"));
642 printf(
_(
" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
643 printf(
_(
" -?, --help show this help, then exit\n"));
644 printf(
_(
"\nOptions controlling the output content:\n"));
645 printf(
_(
" -a, --data-only dump only the data, not the schema\n"));
646 printf(
_(
" -c, --clean clean (drop) databases before recreating\n"));
647 printf(
_(
" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
648 printf(
_(
" -g, --globals-only dump only global objects, no databases\n"));
649 printf(
_(
" -O, --no-owner skip restoration of object ownership\n"));
650 printf(
_(
" -r, --roles-only dump only roles, no databases or tablespaces\n"));
651 printf(
_(
" -s, --schema-only dump only the schema, no data\n"));
652 printf(
_(
" -S, --superuser=NAME superuser user name to use in the dump\n"));
653 printf(
_(
" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
654 printf(
_(
" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
655 printf(
_(
" --binary-upgrade for use by upgrade utilities only\n"));
656 printf(
_(
" --column-inserts dump data as INSERT commands with column names\n"));
657 printf(
_(
" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
658 printf(
_(
" --disable-triggers disable triggers during data-only restore\n"));
659 printf(
_(
" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
660 printf(
_(
" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
661 printf(
_(
" --filter=FILENAME exclude databases based on expressions in FILENAME\n"));
662 printf(
_(
" --if-exists use IF EXISTS when dropping objects\n"));
663 printf(
_(
" --inserts dump data as INSERT commands, rather than COPY\n"));
664 printf(
_(
" --load-via-partition-root load partitions via the root table\n"));
665 printf(
_(
" --no-comments do not dump comment commands\n"));
666 printf(
_(
" --no-publications do not dump publications\n"));
667 printf(
_(
" --no-role-passwords do not dump passwords for roles\n"));
668 printf(
_(
" --no-security-labels do not dump security label assignments\n"));
669 printf(
_(
" --no-subscriptions do not dump subscriptions\n"));
670 printf(
_(
" --no-sync do not wait for changes to be written safely to disk\n"));
671 printf(
_(
" --no-table-access-method do not dump table access methods\n"));
672 printf(
_(
" --no-tablespaces do not dump tablespace assignments\n"));
673 printf(
_(
" --no-toast-compression do not dump TOAST compression methods\n"));
674 printf(
_(
" --no-unlogged-table-data do not dump unlogged table data\n"));
675 printf(
_(
" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
676 printf(
_(
" --quote-all-identifiers quote all identifiers, even if not key words\n"));
677 printf(
_(
" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
678 printf(
_(
" --use-set-session-authorization\n"
679 " use SET SESSION AUTHORIZATION commands instead of\n"
680 " ALTER OWNER commands to set ownership\n"));
682 printf(
_(
"\nConnection options:\n"));
683 printf(
_(
" -d, --dbname=CONNSTR connect using connection string\n"));
684 printf(
_(
" -h, --host=HOSTNAME database server host or socket directory\n"));
685 printf(
_(
" -l, --database=DBNAME alternative default database\n"));
686 printf(
_(
" -p, --port=PORT database server port number\n"));
687 printf(
_(
" -U, --username=NAME connect as specified database user\n"));
688 printf(
_(
" -w, --no-password never prompt for password\n"));
689 printf(
_(
" -W, --password force password prompt (should happen automatically)\n"));
690 printf(
_(
" --role=ROLENAME do SET ROLE before dump\n"));
692 printf(
_(
"\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
694 printf(
_(
"Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
695 printf(
_(
"%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
714 "WHERE rolname !~ '^pg_' "
731 const char *rolename;
776 "SELECT oid, rolname, rolsuper, rolinherit, "
777 "rolcreaterole, rolcreatedb, "
778 "rolcanlogin, rolconnlimit, rolpassword, "
779 "rolvaliduntil, rolreplication, rolbypassrls, "
780 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
781 "rolname = current_user AS is_current_user "
783 "WHERE rolname !~ '^pg_' "
787 "SELECT oid, rolname, rolsuper, rolinherit, "
788 "rolcreaterole, rolcreatedb, "
789 "rolcanlogin, rolconnlimit, rolpassword, "
790 "rolvaliduntil, rolreplication, rolbypassrls, "
791 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
792 "rolname = current_user AS is_current_user "
797 "SELECT oid, rolname, rolsuper, rolinherit, "
798 "rolcreaterole, rolcreatedb, "
799 "rolcanlogin, rolconnlimit, rolpassword, "
800 "rolvaliduntil, rolreplication, "
801 "false as rolbypassrls, "
802 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
803 "rolname = current_user AS is_current_user "
829 const char *rolename;
835 if (strncmp(rolename,
"pg_", 3) == 0)
848 "SELECT pg_catalog.binary_upgrade_set_next_pg_authid_oid('%u'::pg_catalog.oid);\n\n",
938 fprintf(
OPF,
"\n--\n-- User Configurations\n--\n");
967 bool dump_grant_options;
968 int i_inherit_option;
987 "um.rolname AS member, "
988 "ug.oid AS grantorid, "
989 "ug.rolname AS grantor, "
991 if (dump_grant_options)
994 "LEFT JOIN %s ur on ur.oid = a.roleid "
995 "LEFT JOIN %s um on um.oid = a.member "
996 "LEFT JOIN %s ug on ug.oid = a.grantor "
997 "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
1004 fprintf(
OPF,
"--\n-- Role memberships\n--\n\n");
1021 while (
start < total)
1027 int prev_remaining = 0;
1031 for (end =
start; end < total; ++end)
1036 if (strcmp(role, otherrole) != 0)
1059 pg_log_error(
"could not find a legal dump ordering for memberships in role \"%s\"",
1084 if (dump_grant_options)
1093 if (dump_grantors &&
1094 atooid(grantorid) != BOOTSTRAP_SUPERUSERID &&
1095 rolename_lookup(ht, grantor) == NULL)
1107 rolename_insert(ht, member, &found);
1115 if (dump_grant_options)
1119 if (optbuf->
data[0] !=
'\0')
1128 if (optbuf->
data[0] !=
'\0')
1132 if (optbuf->
data[0] !=
'\0')
1140 rolename_destroy(ht);
1169 "pg_catalog.pg_get_userbyid(" CppAsString2(BOOTSTRAP_SUPERUSERID)
") AS parowner, "
1171 "pg_catalog.acldefault('p', " CppAsString2(BOOTSTRAP_SUPERUSERID)
") AS acldefault "
1172 "FROM pg_catalog.pg_parameter_acl "
1176 fprintf(
OPF,
"--\n-- Role privileges on configuration parameters\n--\n\n");
1194 pg_log_error(
"could not parse ACL list (%s) for parameter \"%s\"",
1225 "FROM pg_catalog.pg_tablespace "
1226 "WHERE spcname !~ '^pg_' "
1230 fprintf(
OPF,
"--\n-- Drop tablespaces\n--\n\n");
1260 "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1261 "pg_catalog.pg_tablespace_location(oid), "
1262 "spcacl, acldefault('t', spcowner) AS acldefault, "
1263 "array_to_string(spcoptions, ', '),"
1264 "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1265 "FROM pg_catalog.pg_tablespace "
1266 "WHERE spcname !~ '^pg_' "
1291 appendPQExpBuffer(
buf,
"SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
1310 if (spcoptions && spcoptions[0] !=
'\0')
1312 fspcname, spcoptions);
1321 pg_log_error(
"could not parse ACL list (%s) for tablespace \"%s\"",
1327 if (!
no_comments && spccomment && spccomment[0] !=
'\0')
1336 "TABLESPACE", spcname,
1365 "FROM pg_database d "
1366 "WHERE datallowconn AND datconnlimit != -2 "
1367 "ORDER BY datname");
1370 fprintf(
OPF,
"--\n-- Drop databases (except postgres and template1)\n--\n\n");
1381 if (strcmp(
dbname,
"template1") != 0 &&
1382 strcmp(
dbname,
"template0") != 0 &&
1383 strcmp(
dbname,
"postgres") != 0)
1407 "WHERE setdatabase = 0 AND setrole = "
1408 "(SELECT oid FROM %s WHERE rolname = ",
1444 if (patterns->
head == NULL)
1460 "SELECT datname FROM pg_catalog.pg_database n\n");
1462 false, NULL,
"datname", NULL, NULL, NULL,
1467 pg_log_error(
"improper qualified name (too many dotted names): %s",
1508 "FROM pg_database d "
1509 "WHERE datallowconn AND datconnlimit != -2 "
1510 "ORDER BY (datname <> 'template1'), datname");
1518 const char *create_opts;
1522 if (strcmp(
dbname,
"template0") == 0)
1544 if (strcmp(
dbname,
"template1") == 0 || strcmp(
dbname,
"postgres") == 0)
1547 create_opts =
"--clean --create";
1556 create_opts =
"--create";
1569 pg_fatal(
"could not re-open the output file \"%s\": %m",
1617 ret = system(cmd.
data);
1638 const char *objtype,
const char *objname,
1664 const char *
pghost,
const char *
pgport,
const char *pguser,
1665 trivalue prompt_password,
bool fail_on_error)
1669 const char *remoteversion_str;
1672 const char **
values = NULL;
1687 char *err_msg = NULL;
1703 if (conn_opts == NULL)
1706 for (conn_opt = conn_opts; conn_opt->
keyword != NULL; conn_opt++)
1708 if (conn_opt->
val != NULL && conn_opt->
val[0] !=
'\0' &&
1709 strcmp(conn_opt->
keyword,
"dbname") != 0)
1716 for (conn_opt = conn_opts; conn_opt->
keyword != NULL; conn_opt++)
1718 if (conn_opt->
val != NULL && conn_opt->
val[0] !=
'\0' &&
1719 strcmp(conn_opt->
keyword,
"dbname") != 0)
1763 keywords[
i] =
"fallback_application_name";
1776 prompt_password !=
TRI_NO)
1813 if (!remoteversion_str)
1814 pg_fatal(
"could not get server version");
1817 pg_fatal(
"could not parse server version \"%s\"",
1820 my_version = PG_VERSION_NUM;
1830 pg_log_error(
"aborting because of server version mismatch");
1832 remoteversion_str,
progname, PG_VERSION);
1857 bool firstkeyword =
true;
1862 if (strcmp(
keywords[
i],
"dbname") == 0 ||
1864 strcmp(
keywords[
i],
"fallback_application_name") == 0)
1869 firstkeyword =
false;
1933 time_t
now = time(NULL);
Acl * acldefault(ObjectType objtype, Oid ownerId)
Datum now(PG_FUNCTION_ARGS)
static Datum values[MAXATTR]
#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)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
#define fprintf(file, fmt, msg)
static void PGresult * res
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)
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)
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)
void PQconninfoFree(PQconninfoOption *connOptions)
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
int PQconnectionNeedsPassword(const PGconn *conn)
ConnStatusType PQstatus(const PGconn *conn)
int PQclientEncoding(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
int PQsetClientEncoding(PGconn *conn, const char *encoding)
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
ExecStatusType PQresultStatus(const PGresult *res)
int PQntuples(const PGresult *res)
int PQfnumber(const PGresult *res, const char *field_name)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
PGresult * PQexec(PGconn *conn, const char *query)
char * pg_strdup(const char *in)
void * pg_malloc0(size_t size)
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
static const JsonPathKeyword keywords[]
static void const char fflush(stdout)
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(...)
static void dumpTimestamp(const char *msg)
static int on_conflict_do_nothing
int main(int argc, char *argv[])
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 no_table_access_method
static int no_unlogged_table_data
static int binary_upgrade
static int disable_triggers
static void dumpUserConfig(PGconn *conn, const char *username)
static SimpleStringList database_exclude_patterns
static void dumpTablespaces(PGconn *conn)
static void dumpRoleMembership(PGconn *conn)
static 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)
#define exit_nicely(code)
static char pg_dump_bin[MAXPGPATH]
static SimpleStringList database_exclude_names
static char * constructConnStr(const char **keywords, const char **values)
static int no_publications
static int no_security_labels
static void dumpDatabases(PGconn *conn)
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 int runPgDump(const char *dbname, const char *create_opts)
static void dumpRoleGUCPrivs(PGconn *conn)
static void dumpRoles(PGconn *conn)
static PGresult * executeQuery(PGconn *conn, const char *query)
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 const char * progname
static int no_subscriptions
#define PGDUMP_VERSIONSTR
PGDLLIMPORT char * optarg
#define pg_encoding_to_char
static const char * pghost
static const char * pgport
#define pg_log_warning(...)
#define is_absolute_path(filename)
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)
bool quote_all_identifiers
bool simple_string_list_member(SimpleStringList *list, const char *val)
void simple_string_list_append(SimpleStringList *list, const char *val)
char * simple_prompt(const char *prompt, bool echo)
const char * fmtId(const char *rawid)
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)
struct SimpleStringListCell * next
SimpleStringListCell * head