PostgreSQL Source Code git master
Data Structures | Macros | Functions | Variables
pg_dumpall.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include <unistd.h>
#include "catalog/pg_authid_d.h"
#include "common/connect.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "common/hashfn_unstable.h"
#include "common/logging.h"
#include "common/string.h"
#include "connectdb.h"
#include "dumputils.h"
#include "fe_utils/string_utils.h"
#include "filter.h"
#include "getopt_long.h"
#include "lib/simplehash.h"
Include dependency graph for pg_dumpall.c:

Go to the source code of this file.

Data Structures

struct   RoleNameEntry
 

Macros

#define  PGDUMP_VERSIONSTR   "pg_dump (PostgreSQL) " PG_VERSION "\n"
 
#define  SH_PREFIX   rolename
 
 
#define  SH_KEY_TYPE   char *
 
#define  SH_KEY   rolename
 
#define  SH_HASH_KEY(tb, key)   hash_string(key)
 
#define  SH_EQUAL(tb, a, b)   (strcmp(a, b) == 0)
 
#define  SH_STORE_HASH
 
#define  SH_GET_HASH(tb, a)   (a)->hashval
 
#define  SH_SCOPE   static inline
 
#define  SH_RAW_ALLOCATOR   pg_malloc0
 
#define  SH_DECLARE
 
#define  SH_DEFINE
 
#define  PG_AUTHID   "pg_authid"
 
#define  PG_ROLES   "pg_roles "
 

Functions

static void  help (void)
 
static void  dropRoles (PGconn *conn)
 
static void  dumpRoles (PGconn *conn)
 
static void  dumpRoleMembership (PGconn *conn)
 
static void  dumpRoleGUCPrivs (PGconn *conn)
 
static void  dropTablespaces (PGconn *conn)
 
static void  dumpTablespaces (PGconn *conn)
 
static void  dropDBs (PGconn *conn)
 
static void  dumpUserConfig (PGconn *conn, const char *username)
 
static void  dumpDatabases (PGconn *conn)
 
static void  dumpTimestamp (const char *msg)
 
static int  runPgDump (const char *dbname, const char *create_opts)
 
static void  buildShSecLabels (PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
 
static void  executeCommand (PGconn *conn, const char *query)
 
 
static void  read_dumpall_filters (const char *filename, SimpleStringList *pattern)
 
int  main (int argc, char *argv[])
 

Variables

static char  pg_dump_bin [MAXPGPATH]
 
 
static const char *  connstr = ""
 
static bool  output_clean = false
 
static bool  skip_acls = false
 
static bool  verbose = false
 
static bool  dosync = true
 
static int  binary_upgrade = 0
 
static int  column_inserts = 0
 
static int  disable_dollar_quoting = 0
 
static int  disable_triggers = 0
 
static int  if_exists = 0
 
static int  inserts = 0
 
static int  no_table_access_method = 0
 
static int  no_tablespaces = 0
 
static int  use_setsessauth = 0
 
static int  no_comments = 0
 
static int  no_policies = 0
 
static int  no_publications = 0
 
static int  no_security_labels = 0
 
static int  no_data = 0
 
static int  no_schema = 0
 
static int  no_statistics = 0
 
static int  no_subscriptions = 0
 
static int  no_toast_compression = 0
 
static int  no_unlogged_table_data = 0
 
static int  no_role_passwords = 0
 
static int  with_statistics = 0
 
static int  server_version
 
static int  load_via_partition_root = 0
 
static int  on_conflict_do_nothing = 0
 
static int  statistics_only = 0
 
static int  sequence_data = 0
 
static char  role_catalog [10]
 
static FILE *  OPF
 
static char *  filename = NULL
 
 
static SimpleStringList  database_exclude_names = {NULL, NULL}
 
static char *  restrict_key
 

Macro Definition Documentation

PG_AUTHID

#define PG_AUTHID   "pg_authid"

Definition at line 116 of file pg_dumpall.c.

PG_ROLES

#define PG_ROLES   "pg_roles "

Definition at line 117 of file pg_dumpall.c.

PGDUMP_VERSIONSTR

#define PGDUMP_VERSIONSTR   "pg_dump (PostgreSQL) " PG_VERSION "\n"

Definition at line 35 of file pg_dumpall.c.

SH_DECLARE

#define SH_DECLARE

Definition at line 54 of file pg_dumpall.c.

SH_DEFINE

#define SH_DEFINE

Definition at line 55 of file pg_dumpall.c.

SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   RoleNameEntry

Definition at line 45 of file pg_dumpall.c.

SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)    (strcmp(a, b) == 0)

Definition at line 49 of file pg_dumpall.c.

SH_GET_HASH

#define SH_GET_HASH (   tb,
  a 
)    (a)->hashval

Definition at line 51 of file pg_dumpall.c.

SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    hash_string(key)

Definition at line 48 of file pg_dumpall.c.

SH_KEY

#define SH_KEY   rolename

Definition at line 47 of file pg_dumpall.c.

SH_KEY_TYPE

#define SH_KEY_TYPE   char *

Definition at line 46 of file pg_dumpall.c.

SH_PREFIX

#define SH_PREFIX   rolename

Definition at line 44 of file pg_dumpall.c.

SH_RAW_ALLOCATOR

#define SH_RAW_ALLOCATOR   pg_malloc0

Definition at line 53 of file pg_dumpall.c.

SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 52 of file pg_dumpall.c.

SH_STORE_HASH

#define SH_STORE_HASH

Definition at line 50 of file pg_dumpall.c.

Function Documentation

buildShSecLabels()

static void buildShSecLabels ( PGconnconn,
const char *  catalog_name,
Oid  objectId,
const char *  objtype,
const char *  objname,
PQExpBuffer  buffer 
)
static

Definition at line 1761 of file pg_dumpall.c.

1764{
1766 PGresult *res;
1767
1768 buildShSecLabelQuery(catalog_name, objectId, sql);
1769 res = executeQuery(conn, sql->data);
1770 emitShSecLabels(conn, res, buffer, objtype, objname);
1771
1772 PQclear(res);
1773 destroyPQExpBuffer(sql);
1774}
PGresult * executeQuery(PGconn *conn, const char *query)
Definition: connectdb.c:278
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
Definition: dumputils.c:678
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
Definition: dumputils.c:696
#define PQclear
Definition: libpq-be-fe.h:245
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114
PGconn * conn
Definition: streamutil.c:52
char * data
Definition: pqexpbuffer.h:46

References buildShSecLabelQuery(), conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), emitShSecLabels(), executeQuery(), and PQclear.

Referenced by dumpRoles(), and dumpTablespaces().

dropDBs()

static void dropDBs ( PGconnconn )
static

Definition at line 1469 of file pg_dumpall.c.

1470{
1471 PGresult *res;
1472 int i;
1473
1474 /*
1475 * Skip databases marked not datallowconn, since we'd be unable to connect
1476 * to them anyway. This must agree with dumpDatabases().
1477 */
1478 res = executeQuery(conn,
1479 "SELECT datname "
1480 "FROM pg_database d "
1481 "WHERE datallowconn AND datconnlimit != -2 "
1482 "ORDER BY datname");
1483
1484 if (PQntuples(res) > 0)
1485 fprintf(OPF, "--\n-- Drop databases (except postgres and template1)\n--\n\n");
1486
1487 for (i = 0; i < PQntuples(res); i++)
1488 {
1489 char *dbname = PQgetvalue(res, i, 0);
1490
1491 /*
1492 * Skip "postgres" and "template1"; dumpDatabases() will deal with
1493 * them specially. Also, be sure to skip "template0", even if for
1494 * some reason it's not marked !datallowconn.
1495 */
1496 if (strcmp(dbname, "template1") != 0 &&
1497 strcmp(dbname, "template0") != 0 &&
1498 strcmp(dbname, "postgres") != 0)
1499 {
1500 fprintf(OPF, "DROP DATABASE %s%s;\n",
1501 if_exists ? "IF EXISTS " : "",
1502 fmtId(dbname));
1503 }
1504 }
1505
1506 PQclear(res);
1507
1508 fprintf(OPF, "\n\n");
1509}
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
i
int i
Definition: isn.c:77
#define PQgetvalue
Definition: libpq-be-fe.h:253
#define PQntuples
Definition: libpq-be-fe.h:251
static int if_exists
Definition: pg_dumpall.c:92
static FILE * OPF
Definition: pg_dumpall.c:119
char * dbname
Definition: streamutil.c:49
const char * fmtId(const char *rawid)
Definition: string_utils.c:248

References conn, dbname, executeQuery(), fmtId(), fprintf, i, if_exists, OPF, PQclear, PQgetvalue, and PQntuples.

Referenced by main().

dropRoles()

static void dropRoles ( PGconnconn )
static

Definition at line 770 of file pg_dumpall.c.

771{
773 PGresult *res;
774 int i_rolname;
775 int i;
776
777 if (server_version >= 90600)
779 "SELECT rolname "
780 "FROM %s "
781 "WHERE rolname !~ '^pg_' "
782 "ORDER BY 1", role_catalog);
783 else
785 "SELECT rolname "
786 "FROM %s "
787 "ORDER BY 1", role_catalog);
788
789 res = executeQuery(conn, buf->data);
790
791 i_rolname = PQfnumber(res, "rolname");
792
793 if (PQntuples(res) > 0)
794 fprintf(OPF, "--\n-- Drop roles\n--\n\n");
795
796 for (i = 0; i < PQntuples(res); i++)
797 {
798 const char *rolename;
799
800 rolename = PQgetvalue(res, i, i_rolname);
801
802 fprintf(OPF, "DROP ROLE %s%s;\n",
803 if_exists ? "IF EXISTS " : "",
804 fmtId(rolename));
805 }
806
807 PQclear(res);
809
810 fprintf(OPF, "\n\n");
811}
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3600
static int server_version
Definition: pg_dumpall.c:109
static char role_catalog[10]
Definition: pg_dumpall.c:115
static char * buf
Definition: pg_test_fsync.c:72
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235

References buf, conn, createPQExpBuffer(), destroyPQExpBuffer(), executeQuery(), fmtId(), fprintf, i, if_exists, OPF, PQclear, PQfnumber(), PQgetvalue, PQntuples, printfPQExpBuffer(), role_catalog, and server_version.

Referenced by main().

dropTablespaces()

static void dropTablespaces ( PGconnconn )
static

Definition at line 1330 of file pg_dumpall.c.

1331{
1332 PGresult *res;
1333 int i;
1334
1335 /*
1336 * Get all tablespaces except built-in ones (which we assume are named
1337 * pg_xxx)
1338 */
1339 res = executeQuery(conn, "SELECT spcname "
1340 "FROM pg_catalog.pg_tablespace "
1341 "WHERE spcname !~ '^pg_' "
1342 "ORDER BY 1");
1343
1344 if (PQntuples(res) > 0)
1345 fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1346
1347 for (i = 0; i < PQntuples(res); i++)
1348 {
1349 char *spcname = PQgetvalue(res, i, 0);
1350
1351 fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1352 if_exists ? "IF EXISTS " : "",
1353 fmtId(spcname));
1354 }
1355
1356 PQclear(res);
1357
1358 fprintf(OPF, "\n\n");
1359}

References conn, executeQuery(), fmtId(), fprintf, i, if_exists, OPF, PQclear, PQgetvalue, and PQntuples.

Referenced by main().

dumpDatabases()

static void dumpDatabases ( PGconnconn )
static

Definition at line 1611 of file pg_dumpall.c.

1612{
1613 PGresult *res;
1614 int i;
1615
1616 /*
1617 * Skip databases marked not datallowconn, since we'd be unable to connect
1618 * to them anyway. This must agree with dropDBs().
1619 *
1620 * We arrange for template1 to be processed first, then we process other
1621 * DBs in alphabetical order. If we just did them all alphabetically, we
1622 * might find ourselves trying to drop the "postgres" database while still
1623 * connected to it. This makes trying to run the restore script while
1624 * connected to "template1" a bad idea, but there's no fixed order that
1625 * doesn't have some failure mode with --clean.
1626 */
1627 res = executeQuery(conn,
1628 "SELECT datname "
1629 "FROM pg_database d "
1630 "WHERE datallowconn AND datconnlimit != -2 "
1631 "ORDER BY (datname <> 'template1'), datname");
1632
1633 if (PQntuples(res) > 0)
1634 fprintf(OPF, "--\n-- Databases\n--\n\n");
1635
1636 for (i = 0; i < PQntuples(res); i++)
1637 {
1638 char *dbname = PQgetvalue(res, i, 0);
1639 char *sanitized;
1640 const char *create_opts;
1641 int ret;
1642
1643 /* Skip template0, even if it's not marked !datallowconn. */
1644 if (strcmp(dbname, "template0") == 0)
1645 continue;
1646
1647 /* Skip any explicitly excluded database */
1649 {
1650 pg_log_info("excluding database \"%s\"", dbname);
1651 continue;
1652 }
1653
1654 pg_log_info("dumping database \"%s\"", dbname);
1655
1656 sanitized = sanitize_line(dbname, true);
1657 fprintf(OPF, "--\n-- Database \"%s\" dump\n--\n\n", sanitized);
1658 free(sanitized);
1659
1660 /*
1661 * We assume that "template1" and "postgres" already exist in the
1662 * target installation. dropDBs() won't have removed them, for fear
1663 * of removing the DB the restore script is initially connected to. If
1664 * --clean was specified, tell pg_dump to drop and recreate them;
1665 * otherwise we'll merely restore their contents. Other databases
1666 * should simply be created.
1667 */
1668 if (strcmp(dbname, "template1") == 0 || strcmp(dbname, "postgres") == 0)
1669 {
1670 if (output_clean)
1671 create_opts = "--clean --create";
1672 else
1673 {
1674 create_opts = "";
1675 /* Since pg_dump won't emit a \connect command, we must */
1676 fprintf(OPF, "\\connect %s\n\n", dbname);
1677 }
1678 }
1679 else
1680 create_opts = "--create";
1681
1682 if (filename)
1683 fclose(OPF);
1684
1685 ret = runPgDump(dbname, create_opts);
1686 if (ret != 0)
1687 pg_fatal("pg_dump failed on database \"%s\", exiting", dbname);
1688
1689 if (filename)
1690 {
1691 OPF = fopen(filename, PG_BINARY_A);
1692 if (!OPF)
1693 pg_fatal("could not re-open the output file \"%s\": %m",
1694 filename);
1695 }
1696 }
1697
1698 PQclear(res);
1699}
#define PG_BINARY_A
Definition: c.h:1273
char * sanitize_line(const char *str, bool want_hyphen)
Definition: dumputils.c:52
#define free(a)
Definition: header.h:65
#define pg_log_info(...)
Definition: logging.h:124
#define pg_fatal(...)
static SimpleStringList database_exclude_names
Definition: pg_dumpall.c:123
static int runPgDump(const char *dbname, const char *create_opts)
Definition: pg_dumpall.c:1707
static bool output_clean
Definition: pg_dumpall.c:83
static char * filename
Definition: pg_dumpall.c:120
bool simple_string_list_member(SimpleStringList *list, const char *val)
Definition: simple_list.c:87

References conn, database_exclude_names, dbname, executeQuery(), filename, fprintf, free, i, OPF, output_clean, PG_BINARY_A, pg_fatal, pg_log_info, PQclear, PQgetvalue, PQntuples, runPgDump(), sanitize_line(), and simple_string_list_member().

Referenced by main().

dumpRoleGUCPrivs()

static void dumpRoleGUCPrivs ( PGconnconn )
static

Definition at line 1275 of file pg_dumpall.c.

1276{
1277 PGresult *res;
1278 int i;
1279
1280 /*
1281 * Get all parameters that have non-default acls defined.
1282 */
1283 res = executeQuery(conn, "SELECT parname, "
1284 "pg_catalog.pg_get_userbyid(" CppAsString2(BOOTSTRAP_SUPERUSERID) ") AS parowner, "
1285 "paracl, "
1286 "pg_catalog.acldefault('p', " CppAsString2(BOOTSTRAP_SUPERUSERID) ") AS acldefault "
1287 "FROM pg_catalog.pg_parameter_acl "
1288 "ORDER BY 1");
1289
1290 if (PQntuples(res) > 0)
1291 fprintf(OPF, "--\n-- Role privileges on configuration parameters\n--\n\n");
1292
1293 for (i = 0; i < PQntuples(res); i++)
1294 {
1296 char *parname = PQgetvalue(res, i, 0);
1297 char *parowner = PQgetvalue(res, i, 1);
1298 char *paracl = PQgetvalue(res, i, 2);
1299 char *acldefault = PQgetvalue(res, i, 3);
1300 char *fparname;
1301
1302 /* needed for buildACLCommands() */
1303 fparname = pg_strdup(fmtId(parname));
1304
1305 if (!buildACLCommands(fparname, NULL, NULL, "PARAMETER",
1306 paracl, acldefault,
1307 parowner, "", server_version, buf))
1308 {
1309 pg_log_error("could not parse ACL list (%s) for parameter \"%s\"",
1310 paracl, parname);
1311 PQfinish(conn);
1312 exit_nicely(1);
1313 }
1314
1315 fprintf(OPF, "%s", buf->data);
1316
1317 free(fparname);
1319 }
1320
1321 PQclear(res);
1322 fprintf(OPF, "\n\n");
1323}
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:803
#define CppAsString2(x)
Definition: c.h:418
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)
Definition: dumputils.c:104
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5305
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define pg_log_error(...)
Definition: logging.h:106
void exit_nicely(int code)

References acldefault(), buf, buildACLCommands(), conn, CppAsString2, createPQExpBuffer(), destroyPQExpBuffer(), executeQuery(), exit_nicely(), fmtId(), fprintf, free, i, OPF, pg_log_error, pg_strdup(), PQclear, PQfinish(), PQgetvalue, PQntuples, and server_version.

Referenced by main().

dumpRoleMembership()

static void dumpRoleMembership ( PGconnconn )
static

Definition at line 1025 of file pg_dumpall.c.

1026{
1028 PQExpBuffer optbuf = createPQExpBuffer();
1029 PGresult *res;
1030 int start = 0,
1031 end,
1032 total;
1033 bool dump_grantors;
1034 bool dump_grant_options;
1035 int i_role;
1036 int i_member;
1037 int i_grantor;
1038 int i_roleid;
1039 int i_memberid;
1040 int i_grantorid;
1041 int i_admin_option;
1042 int i_inherit_option;
1043 int i_set_option;
1044
1045 /*
1046 * Previous versions of PostgreSQL didn't used to track the grantor very
1047 * carefully in the backend, and the grantor could be any user even if
1048 * they didn't have ADMIN OPTION on the role, or a user that no longer
1049 * existed. To avoid dump and restore failures, don't dump the grantor
1050 * when talking to an old server version.
1051 *
1052 * Also, in older versions the roleid and/or member could be role OIDs
1053 * that no longer exist. If we find such cases, print a warning and skip
1054 * the entry.
1055 */
1056 dump_grantors = (PQserverVersion(conn) >= 160000);
1057
1058 /*
1059 * Previous versions of PostgreSQL also did not have grant-level options.
1060 */
1061 dump_grant_options = (server_version >= 160000);
1062
1063 /* Generate and execute query. */
1064 printfPQExpBuffer(buf, "SELECT ur.rolname AS role, "
1065 "um.rolname AS member, "
1066 "ug.rolname AS grantor, "
1067 "a.roleid AS roleid, "
1068 "a.member AS memberid, "
1069 "a.grantor AS grantorid, "
1070 "a.admin_option");
1071 if (dump_grant_options)
1072 appendPQExpBufferStr(buf, ", a.inherit_option, a.set_option");
1073 appendPQExpBuffer(buf, " FROM pg_auth_members a "
1074 "LEFT JOIN %s ur on ur.oid = a.roleid "
1075 "LEFT JOIN %s um on um.oid = a.member "
1076 "LEFT JOIN %s ug on ug.oid = a.grantor "
1077 "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
1078 "ORDER BY 1,2,3", role_catalog, role_catalog, role_catalog);
1079 res = executeQuery(conn, buf->data);
1080 i_role = PQfnumber(res, "role");
1081 i_member = PQfnumber(res, "member");
1082 i_grantor = PQfnumber(res, "grantor");
1083 i_roleid = PQfnumber(res, "roleid");
1084 i_memberid = PQfnumber(res, "memberid");
1085 i_grantorid = PQfnumber(res, "grantorid");
1086 i_admin_option = PQfnumber(res, "admin_option");
1087 i_inherit_option = PQfnumber(res, "inherit_option");
1088 i_set_option = PQfnumber(res, "set_option");
1089
1090 if (PQntuples(res) > 0)
1091 fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1092
1093 /*
1094 * We can't dump these GRANT commands in arbitrary order, because a role
1095 * that is named as a grantor must already have ADMIN OPTION on the role
1096 * for which it is granting permissions, except for the bootstrap
1097 * superuser, who can always be named as the grantor.
1098 *
1099 * We handle this by considering these grants role by role. For each role,
1100 * we initially consider the only allowable grantor to be the bootstrap
1101 * superuser. Every time we grant ADMIN OPTION on the role to some user,
1102 * that user also becomes an allowable grantor. We make repeated passes
1103 * over the grants for the role, each time dumping those whose grantors
1104 * are allowable and which we haven't done yet. Eventually this should let
1105 * us dump all the grants.
1106 */
1107 total = PQntuples(res);
1108 while (start < total)
1109 {
1110 char *role = PQgetvalue(res, start, i_role);
1111 int i;
1112 bool *done;
1113 int remaining;
1114 int prev_remaining = 0;
1115 rolename_hash *ht;
1116
1117 /* If we hit a null roleid, we're done (nulls sort to the end). */
1118 if (PQgetisnull(res, start, i_role))
1119 {
1120 /* translator: %s represents a numeric role OID */
1121 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1122 PQgetvalue(res, start, i_roleid));
1123 break;
1124 }
1125
1126 /* All memberships for a single role should be adjacent. */
1127 for (end = start; end < total; ++end)
1128 {
1129 char *otherrole;
1130
1131 otherrole = PQgetvalue(res, end, i_role);
1132 if (strcmp(role, otherrole) != 0)
1133 break;
1134 }
1135
1136 remaining = end - start;
1137 done = pg_malloc0(remaining * sizeof(bool));
1138 ht = rolename_create(remaining, NULL);
1139
1140 /*
1141 * Make repeated passes over the grants for this role until all have
1142 * been dumped.
1143 */
1144 while (remaining > 0)
1145 {
1146 /*
1147 * We should make progress on every iteration, because a notional
1148 * graph whose vertices are grants and whose edges point from
1149 * grantors to members should be connected and acyclic. If we fail
1150 * to make progress, either we or the server have messed up.
1151 */
1152 if (remaining == prev_remaining)
1153 {
1154 pg_log_error("could not find a legal dump ordering for memberships in role \"%s\"",
1155 role);
1156 PQfinish(conn);
1157 exit_nicely(1);
1158 }
1159 prev_remaining = remaining;
1160
1161 /* Make one pass over the grants for this role. */
1162 for (i = start; i < end; ++i)
1163 {
1164 char *member;
1165 char *admin_option;
1166 char *grantorid;
1167 char *grantor;
1168 char *set_option = "true";
1169 bool found;
1170
1171 /* If we already did this grant, don't do it again. */
1172 if (done[i - start])
1173 continue;
1174
1175 /* Complain about, then ignore, entries with orphaned OIDs. */
1176 if (PQgetisnull(res, i, i_member))
1177 {
1178 /* translator: %s represents a numeric role OID */
1179 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1180 PQgetvalue(res, i, i_memberid));
1181 done[i - start] = true;
1182 --remaining;
1183 continue;
1184 }
1185 if (PQgetisnull(res, i, i_grantor))
1186 {
1187 /* translator: %s represents a numeric role OID */
1188 pg_log_warning("found orphaned pg_auth_members entry for role %s",
1189 PQgetvalue(res, i, i_grantorid));
1190 done[i - start] = true;
1191 --remaining;
1192 continue;
1193 }
1194
1195 member = PQgetvalue(res, i, i_member);
1196 grantor = PQgetvalue(res, i, i_grantor);
1197 grantorid = PQgetvalue(res, i, i_grantorid);
1198 admin_option = PQgetvalue(res, i, i_admin_option);
1199 if (dump_grant_options)
1200 set_option = PQgetvalue(res, i, i_set_option);
1201
1202 /*
1203 * If we're not dumping grantors or if the grantor is the
1204 * bootstrap superuser, it's fine to dump this now. Otherwise,
1205 * it's got to be someone who has already been granted ADMIN
1206 * OPTION.
1207 */
1208 if (dump_grantors &&
1209 atooid(grantorid) != BOOTSTRAP_SUPERUSERID &&
1210 rolename_lookup(ht, grantor) == NULL)
1211 continue;
1212
1213 /* Remember that we did this so that we don't do it again. */
1214 done[i - start] = true;
1215 --remaining;
1216
1217 /*
1218 * If ADMIN OPTION is being granted, remember that grants
1219 * listing this member as the grantor can now be dumped.
1220 */
1221 if (*admin_option == 't')
1222 rolename_insert(ht, member, &found);
1223
1224 /* Generate the actual GRANT statement. */
1225 resetPQExpBuffer(optbuf);
1226 fprintf(OPF, "GRANT %s", fmtId(role));
1227 fprintf(OPF, " TO %s", fmtId(member));
1228 if (*admin_option == 't')
1229 appendPQExpBufferStr(optbuf, "ADMIN OPTION");
1230 if (dump_grant_options)
1231 {
1232 char *inherit_option;
1233
1234 if (optbuf->data[0] != '0円')
1235 appendPQExpBufferStr(optbuf, ", ");
1236 inherit_option = PQgetvalue(res, i, i_inherit_option);
1237 appendPQExpBuffer(optbuf, "INHERIT %s",
1238 *inherit_option == 't' ?
1239 "TRUE" : "FALSE");
1240 }
1241 if (*set_option != 't')
1242 {
1243 if (optbuf->data[0] != '0円')
1244 appendPQExpBufferStr(optbuf, ", ");
1245 appendPQExpBufferStr(optbuf, "SET FALSE");
1246 }
1247 if (optbuf->data[0] != '0円')
1248 fprintf(OPF, " WITH %s", optbuf->data);
1249 if (dump_grantors)
1250 fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
1251 fprintf(OPF, ";\n");
1252 }
1253 }
1254
1255 rolename_destroy(ht);
1256 pg_free(done);
1257 start = end;
1258 }
1259
1260 PQclear(res);
1262
1263 fprintf(OPF, "\n\n");
1264}
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7669
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void pg_free(void *ptr)
Definition: fe_memutils.c:105
return str start
int remaining
Definition: informix.c:692
#define PQgetisnull
Definition: libpq-be-fe.h:255
bool set_option
bool inherit_option
bool admin_option
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define atooid(x)
Definition: postgres_ext.h:43
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367

References admin_option, appendPQExpBuffer(), appendPQExpBufferStr(), atooid, buf, conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), exit_nicely(), fmtId(), fprintf, i, inherit_option, OPF, pg_free(), pg_log_error, pg_log_warning, pg_malloc0(), PQclear, PQfinish(), PQfnumber(), PQgetisnull, PQgetvalue, PQntuples, PQserverVersion(), printfPQExpBuffer(), remaining, resetPQExpBuffer(), role_catalog, server_version, set_option, and start.

Referenced by main().

dumpRoles()

static void dumpRoles ( PGconnconn )
static

Definition at line 817 of file pg_dumpall.c.

818{
820 PGresult *res;
821 int i_oid,
822 i_rolname,
823 i_rolsuper,
824 i_rolinherit,
825 i_rolcreaterole,
826 i_rolcreatedb,
827 i_rolcanlogin,
828 i_rolconnlimit,
829 i_rolpassword,
830 i_rolvaliduntil,
831 i_rolreplication,
832 i_rolbypassrls,
833 i_rolcomment,
834 i_is_current_user;
835 int i;
836
837 /*
838 * Notes: rolconfig is dumped later, and pg_authid must be used for
839 * extracting rolcomment regardless of role_catalog.
840 */
841 if (server_version >= 90600)
843 "SELECT oid, rolname, rolsuper, rolinherit, "
844 "rolcreaterole, rolcreatedb, "
845 "rolcanlogin, rolconnlimit, rolpassword, "
846 "rolvaliduntil, rolreplication, rolbypassrls, "
847 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
848 "rolname = current_user AS is_current_user "
849 "FROM %s "
850 "WHERE rolname !~ '^pg_' "
851 "ORDER BY 2", role_catalog);
852 else if (server_version >= 90500)
854 "SELECT oid, rolname, rolsuper, rolinherit, "
855 "rolcreaterole, rolcreatedb, "
856 "rolcanlogin, rolconnlimit, rolpassword, "
857 "rolvaliduntil, rolreplication, rolbypassrls, "
858 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
859 "rolname = current_user AS is_current_user "
860 "FROM %s "
861 "ORDER BY 2", role_catalog);
862 else
864 "SELECT oid, rolname, rolsuper, rolinherit, "
865 "rolcreaterole, rolcreatedb, "
866 "rolcanlogin, rolconnlimit, rolpassword, "
867 "rolvaliduntil, rolreplication, "
868 "false as rolbypassrls, "
869 "pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, "
870 "rolname = current_user AS is_current_user "
871 "FROM %s "
872 "ORDER BY 2", role_catalog);
873
874 res = executeQuery(conn, buf->data);
875
876 i_oid = PQfnumber(res, "oid");
877 i_rolname = PQfnumber(res, "rolname");
878 i_rolsuper = PQfnumber(res, "rolsuper");
879 i_rolinherit = PQfnumber(res, "rolinherit");
880 i_rolcreaterole = PQfnumber(res, "rolcreaterole");
881 i_rolcreatedb = PQfnumber(res, "rolcreatedb");
882 i_rolcanlogin = PQfnumber(res, "rolcanlogin");
883 i_rolconnlimit = PQfnumber(res, "rolconnlimit");
884 i_rolpassword = PQfnumber(res, "rolpassword");
885 i_rolvaliduntil = PQfnumber(res, "rolvaliduntil");
886 i_rolreplication = PQfnumber(res, "rolreplication");
887 i_rolbypassrls = PQfnumber(res, "rolbypassrls");
888 i_rolcomment = PQfnumber(res, "rolcomment");
889 i_is_current_user = PQfnumber(res, "is_current_user");
890
891 if (PQntuples(res) > 0)
892 fprintf(OPF, "--\n-- Roles\n--\n\n");
893
894 for (i = 0; i < PQntuples(res); i++)
895 {
896 const char *rolename;
897 Oid auth_oid;
898
899 auth_oid = atooid(PQgetvalue(res, i, i_oid));
900 rolename = PQgetvalue(res, i, i_rolname);
901
902 if (strncmp(rolename, "pg_", 3) == 0)
903 {
904 pg_log_warning("role name starting with \"pg_\" skipped (%s)",
905 rolename);
906 continue;
907 }
908
910
911 if (binary_upgrade)
912 {
913 appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_authid.oid\n");
915 "SELECT pg_catalog.binary_upgrade_set_next_pg_authid_oid('%u'::pg_catalog.oid);\n\n",
916 auth_oid);
917 }
918
919 /*
920 * We dump CREATE ROLE followed by ALTER ROLE to ensure that the role
921 * will acquire the right properties even if it already exists (ie, it
922 * won't hurt for the CREATE to fail). This is particularly important
923 * for the role we are connected as, since even with --clean we will
924 * have failed to drop it. binary_upgrade cannot generate any errors,
925 * so we assume the current role is already created.
926 */
927 if (!binary_upgrade ||
928 strcmp(PQgetvalue(res, i, i_is_current_user), "f") == 0)
929 appendPQExpBuffer(buf, "CREATE ROLE %s;\n", fmtId(rolename));
930 appendPQExpBuffer(buf, "ALTER ROLE %s WITH", fmtId(rolename));
931
932 if (strcmp(PQgetvalue(res, i, i_rolsuper), "t") == 0)
933 appendPQExpBufferStr(buf, " SUPERUSER");
934 else
935 appendPQExpBufferStr(buf, " NOSUPERUSER");
936
937 if (strcmp(PQgetvalue(res, i, i_rolinherit), "t") == 0)
938 appendPQExpBufferStr(buf, " INHERIT");
939 else
940 appendPQExpBufferStr(buf, " NOINHERIT");
941
942 if (strcmp(PQgetvalue(res, i, i_rolcreaterole), "t") == 0)
943 appendPQExpBufferStr(buf, " CREATEROLE");
944 else
945 appendPQExpBufferStr(buf, " NOCREATEROLE");
946
947 if (strcmp(PQgetvalue(res, i, i_rolcreatedb), "t") == 0)
948 appendPQExpBufferStr(buf, " CREATEDB");
949 else
950 appendPQExpBufferStr(buf, " NOCREATEDB");
951
952 if (strcmp(PQgetvalue(res, i, i_rolcanlogin), "t") == 0)
953 appendPQExpBufferStr(buf, " LOGIN");
954 else
955 appendPQExpBufferStr(buf, " NOLOGIN");
956
957 if (strcmp(PQgetvalue(res, i, i_rolreplication), "t") == 0)
958 appendPQExpBufferStr(buf, " REPLICATION");
959 else
960 appendPQExpBufferStr(buf, " NOREPLICATION");
961
962 if (strcmp(PQgetvalue(res, i, i_rolbypassrls), "t") == 0)
963 appendPQExpBufferStr(buf, " BYPASSRLS");
964 else
965 appendPQExpBufferStr(buf, " NOBYPASSRLS");
966
967 if (strcmp(PQgetvalue(res, i, i_rolconnlimit), "-1") != 0)
968 appendPQExpBuffer(buf, " CONNECTION LIMIT %s",
969 PQgetvalue(res, i, i_rolconnlimit));
970
971
972 if (!PQgetisnull(res, i, i_rolpassword) && !no_role_passwords)
973 {
974 appendPQExpBufferStr(buf, " PASSWORD ");
975 appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolpassword), conn);
976 }
977
978 if (!PQgetisnull(res, i, i_rolvaliduntil))
979 appendPQExpBuffer(buf, " VALID UNTIL '%s'",
980 PQgetvalue(res, i, i_rolvaliduntil));
981
983
984 if (!no_comments && !PQgetisnull(res, i, i_rolcomment))
985 {
986 appendPQExpBuffer(buf, "COMMENT ON ROLE %s IS ", fmtId(rolename));
987 appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolcomment), conn);
989 }
990
992 buildShSecLabels(conn, "pg_authid", auth_oid,
993 "ROLE", rolename,
994 buf);
995
996 fprintf(OPF, "%s", buf->data);
997 }
998
999 /*
1000 * Dump configuration settings for roles after all roles have been dumped.
1001 * We do it this way because config settings for roles could mention the
1002 * names of other roles.
1003 */
1004 if (PQntuples(res) > 0)
1005 fprintf(OPF, "\n--\n-- User Configurations\n--\n");
1006
1007 for (i = 0; i < PQntuples(res); i++)
1008 dumpUserConfig(conn, PQgetvalue(res, i, i_rolname));
1009
1010 PQclear(res);
1011
1012 fprintf(OPF, "\n\n");
1013
1015}
static int no_role_passwords
Definition: pg_dumpall.c:107
static int binary_upgrade
Definition: pg_dumpall.c:88
static void dumpUserConfig(PGconn *conn, const char *username)
Definition: pg_dumpall.c:1516
static int no_comments
Definition: pg_dumpall.c:97
static int no_security_labels
Definition: pg_dumpall.c:100
static void buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
Definition: pg_dumpall.c:1761
unsigned int Oid
Definition: postgres_ext.h:32
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:446

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), atooid, binary_upgrade, buf, buildShSecLabels(), conn, createPQExpBuffer(), destroyPQExpBuffer(), dumpUserConfig(), executeQuery(), fmtId(), fprintf, i, no_comments, no_role_passwords, no_security_labels, OPF, pg_log_warning, PQclear, PQfnumber(), PQgetisnull, PQgetvalue, PQntuples, printfPQExpBuffer(), resetPQExpBuffer(), role_catalog, and server_version.

Referenced by main().

dumpTablespaces()

static void dumpTablespaces ( PGconnconn )
static

Definition at line 1365 of file pg_dumpall.c.

1366{
1367 PGresult *res;
1368 int i;
1369
1370 /*
1371 * Get all tablespaces except built-in ones (which we assume are named
1372 * pg_xxx)
1373 */
1374 res = executeQuery(conn, "SELECT oid, spcname, "
1375 "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1376 "pg_catalog.pg_tablespace_location(oid), "
1377 "spcacl, acldefault('t', spcowner) AS acldefault, "
1378 "array_to_string(spcoptions, ', '),"
1379 "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1380 "FROM pg_catalog.pg_tablespace "
1381 "WHERE spcname !~ '^pg_' "
1382 "ORDER BY 1");
1383
1384 if (PQntuples(res) > 0)
1385 fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1386
1387 for (i = 0; i < PQntuples(res); i++)
1388 {
1390 Oid spcoid = atooid(PQgetvalue(res, i, 0));
1391 char *spcname = PQgetvalue(res, i, 1);
1392 char *spcowner = PQgetvalue(res, i, 2);
1393 char *spclocation = PQgetvalue(res, i, 3);
1394 char *spcacl = PQgetvalue(res, i, 4);
1395 char *acldefault = PQgetvalue(res, i, 5);
1396 char *spcoptions = PQgetvalue(res, i, 6);
1397 char *spccomment = PQgetvalue(res, i, 7);
1398 char *fspcname;
1399
1400 /* needed for buildACLCommands() */
1401 fspcname = pg_strdup(fmtId(spcname));
1402
1403 if (binary_upgrade)
1404 {
1405 appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_tablespace oid\n");
1406 appendPQExpBuffer(buf, "SELECT pg_catalog.binary_upgrade_set_next_pg_tablespace_oid('%u'::pg_catalog.oid);\n", spcoid);
1407 }
1408
1409 appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
1410 appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
1411
1412 appendPQExpBufferStr(buf, " LOCATION ");
1413
1414 /*
1415 * In-place tablespaces use a relative path, and need to be dumped
1416 * with an empty string as location.
1417 */
1418 if (is_absolute_path(spclocation))
1419 appendStringLiteralConn(buf, spclocation, conn);
1420 else
1422
1423 appendPQExpBufferStr(buf, ";\n");
1424
1425 if (spcoptions && spcoptions[0] != '0円')
1426 appendPQExpBuffer(buf, "ALTER TABLESPACE %s SET (%s);\n",
1427 fspcname, spcoptions);
1428
1429 /* tablespaces can't have initprivs */
1430
1431 if (!skip_acls &&
1432 !buildACLCommands(fspcname, NULL, NULL, "TABLESPACE",
1433 spcacl, acldefault,
1434 spcowner, "", server_version, buf))
1435 {
1436 pg_log_error("could not parse ACL list (%s) for tablespace \"%s\"",
1437 spcacl, spcname);
1438 PQfinish(conn);
1439 exit_nicely(1);
1440 }
1441
1442 if (!no_comments && spccomment && spccomment[0] != '0円')
1443 {
1444 appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
1445 appendStringLiteralConn(buf, spccomment, conn);
1446 appendPQExpBufferStr(buf, ";\n");
1447 }
1448
1449 if (!no_security_labels)
1450 buildShSecLabels(conn, "pg_tablespace", spcoid,
1451 "TABLESPACE", spcname,
1452 buf);
1453
1454 fprintf(OPF, "%s", buf->data);
1455
1456 free(fspcname);
1458 }
1459
1460 PQclear(res);
1461 fprintf(OPF, "\n\n");
1462}
static bool skip_acls
Definition: pg_dumpall.c:84
#define is_absolute_path(filename)
Definition: port.h:104

References acldefault(), appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), atooid, binary_upgrade, buf, buildACLCommands(), buildShSecLabels(), conn, createPQExpBuffer(), destroyPQExpBuffer(), executeQuery(), exit_nicely(), fmtId(), fprintf, free, i, is_absolute_path, no_comments, no_security_labels, OPF, pg_log_error, pg_strdup(), PQclear, PQfinish(), PQgetvalue, PQntuples, server_version, and skip_acls.

Referenced by main().

dumpTimestamp()

static void dumpTimestamp ( const char *  msg )
static

Definition at line 1804 of file pg_dumpall.c.

1805{
1806 char buf[64];
1807 time_t now = time(NULL);
1808
1809 if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
1810 fprintf(OPF, "-- %s %s\n\n", msg, buf);
1811}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33

References buf, fprintf, now(), OPF, and PGDUMP_STRFTIME_FMT.

Referenced by main().

dumpUserConfig()

static void dumpUserConfig ( PGconnconn,
const char *  username 
)
static

Definition at line 1516 of file pg_dumpall.c.

1517{
1519 PGresult *res;
1520
1521 printfPQExpBuffer(buf, "SELECT unnest(setconfig) FROM pg_db_role_setting "
1522 "WHERE setdatabase = 0 AND setrole = "
1523 "(SELECT oid FROM %s WHERE rolname = ",
1524 role_catalog);
1527
1528 res = executeQuery(conn, buf->data);
1529
1530 if (PQntuples(res) > 0)
1531 {
1532 char *sanitized;
1533
1534 sanitized = sanitize_line(username, true);
1535 fprintf(OPF, "\n--\n-- User Config \"%s\"\n--\n\n", sanitized);
1536 free(sanitized);
1537 }
1538
1539 for (int i = 0; i < PQntuples(res); i++)
1540 {
1543 "ROLE", username, NULL, NULL,
1544 buf);
1545 fprintf(OPF, "%s", buf->data);
1546 }
1547
1548 PQclear(res);
1549
1551}
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
Definition: dumputils.c:864
static char * username
Definition: initdb.c:153
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378

References appendPQExpBufferChar(), appendStringLiteralConn(), buf, conn, createPQExpBuffer(), destroyPQExpBuffer(), executeQuery(), fprintf, free, i, makeAlterConfigCommand(), OPF, PQclear, PQgetvalue, PQntuples, printfPQExpBuffer(), resetPQExpBuffer(), role_catalog, sanitize_line(), and username.

Referenced by dumpRoles().

executeCommand()

static void executeCommand ( PGconnconn,
const char *  query 
)
static

Definition at line 1780 of file pg_dumpall.c.

1781{
1782 PGresult *res;
1783
1784 pg_log_info("executing %s", query);
1785
1786 res = PQexec(conn, query);
1787 if (!res ||
1789 {
1790 pg_log_error("query failed: %s", PQerrorMessage(conn));
1791 pg_log_error_detail("Query was: %s", query);
1792 PQfinish(conn);
1793 exit_nicely(1);
1794 }
1795
1796 PQclear(res);
1797}
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7679
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
#define PQresultStatus
Definition: libpq-be-fe.h:247
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
#define pg_log_error_detail(...)
Definition: logging.h:109

References conn, exit_nicely(), pg_log_error, pg_log_error_detail, pg_log_info, PGRES_COMMAND_OK, PQclear, PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus.

Referenced by appendQualifiedRelation(), connect_slot(), main(), retrieve_objects(), and vacuum_one_database().

expand_dbname_patterns()

static void expand_dbname_patterns ( PGconnconn,
SimpleStringListpatterns,
SimpleStringListnames 
)
static

Definition at line 1558 of file pg_dumpall.c.

1561{
1562 PQExpBuffer query;
1563 PGresult *res;
1564
1565 if (patterns->head == NULL)
1566 return; /* nothing to do */
1567
1568 query = createPQExpBuffer();
1569
1570 /*
1571 * The loop below runs multiple SELECTs, which might sometimes result in
1572 * duplicate entries in the name list, but we don't care, since all we're
1573 * going to do is test membership of the list.
1574 */
1575
1576 for (SimpleStringListCell *cell = patterns->head; cell; cell = cell->next)
1577 {
1578 int dotcnt;
1579
1581 "SELECT datname FROM pg_catalog.pg_database n\n");
1582 processSQLNamePattern(conn, query, cell->val, false,
1583 false, NULL, "datname", NULL, NULL, NULL,
1584 &dotcnt);
1585
1586 if (dotcnt > 0)
1587 {
1588 pg_log_error("improper qualified name (too many dotted names): %s",
1589 cell->val);
1590 PQfinish(conn);
1591 exit_nicely(1);
1592 }
1593
1594 res = executeQuery(conn, query->data);
1595 for (int i = 0; i < PQntuples(res); i++)
1596 {
1597 simple_string_list_append(names, PQgetvalue(res, i, 0));
1598 }
1599
1600 PQclear(res);
1601 resetPQExpBuffer(query);
1602 }
1603
1604 destroyPQExpBuffer(query);
1605}
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
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)
Definition: string_utils.c:1053
struct SimpleStringListCell * next
Definition: simple_list.h:34
SimpleStringListCell * head
Definition: simple_list.h:42

References appendPQExpBufferStr(), conn, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), exit_nicely(), SimpleStringList::head, i, SimpleStringListCell::next, pg_log_error, PQclear, PQfinish(), PQgetvalue, PQntuples, processSQLNamePattern(), resetPQExpBuffer(), and simple_string_list_append().

Referenced by main().

help()

static void help ( void  )
static

Definition at line 691 of file pg_dumpall.c.

692{
693 printf(_("%s exports a PostgreSQL database cluster as an SQL script.\n\n"), progname);
694 printf(_("Usage:\n"));
695 printf(_(" %s [OPTION]...\n"), progname);
696
697 printf(_("\nGeneral options:\n"));
698 printf(_(" -f, --file=FILENAME output file name\n"));
699 printf(_(" -v, --verbose verbose mode\n"));
700 printf(_(" -V, --version output version information, then exit\n"));
701 printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
702 printf(_(" -?, --help show this help, then exit\n"));
703 printf(_("\nOptions controlling the output content:\n"));
704 printf(_(" -a, --data-only dump only the data, not the schema or statistics\n"));
705 printf(_(" -c, --clean clean (drop) databases before recreating\n"));
706 printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
707 printf(_(" -g, --globals-only dump only global objects, no databases\n"));
708 printf(_(" -O, --no-owner skip restoration of object ownership\n"));
709 printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
710 printf(_(" -s, --schema-only dump only the schema, no data or statistics\n"));
711 printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
712 printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
713 printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
714 printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
715 printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
716 printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
717 printf(_(" --disable-triggers disable triggers during data-only restore\n"));
718 printf(_(" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
719 printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
720 printf(_(" --filter=FILENAME exclude databases based on expressions in FILENAME\n"));
721 printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
722 printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
723 printf(_(" --load-via-partition-root load partitions via the root table\n"));
724 printf(_(" --no-comments do not dump comment commands\n"));
725 printf(_(" --no-data do not dump data\n"));
726 printf(_(" --no-policies do not dump row security policies\n"));
727 printf(_(" --no-publications do not dump publications\n"));
728 printf(_(" --no-role-passwords do not dump passwords for roles\n"));
729 printf(_(" --no-schema do not dump schema\n"));
730 printf(_(" --no-security-labels do not dump security label assignments\n"));
731 printf(_(" --no-statistics do not dump statistics\n"));
732 printf(_(" --no-subscriptions do not dump subscriptions\n"));
733 printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
734 printf(_(" --no-table-access-method do not dump table access methods\n"));
735 printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
736 printf(_(" --no-toast-compression do not dump TOAST compression methods\n"));
737 printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
738 printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
739 printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
740 printf(_(" --restrict-key=RESTRICT_KEY use provided string as psql \\restrict key\n"));
741 printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
742 printf(_(" --sequence-data include sequence data in dump\n"));
743 printf(_(" --statistics dump the statistics\n"));
744 printf(_(" --statistics-only dump only the statistics, not schema or data\n"));
745 printf(_(" --use-set-session-authorization\n"
746 " use SET SESSION AUTHORIZATION commands instead of\n"
747 " ALTER OWNER commands to set ownership\n"));
748
749 printf(_("\nConnection options:\n"));
750 printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
751 printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
752 printf(_(" -l, --database=DBNAME alternative default database\n"));
753 printf(_(" -p, --port=PORT database server port number\n"));
754 printf(_(" -U, --username=NAME connect as specified database user\n"));
755 printf(_(" -w, --no-password never prompt for password\n"));
756 printf(_(" -W, --password force password prompt (should happen automatically)\n"));
757 printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
758
759 printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
760 "output.\n\n"));
761 printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
762 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
763}
_
#define _(x)
Definition: elog.c:91
const char * progname
Definition: main.c:44
#define printf(...)
Definition: port.h:245

References _, printf, and progname.

Referenced by main().

main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 128 of file pg_dumpall.c.

129{
130 static struct option long_options[] = {
131 {"data-only", no_argument, NULL, 'a'},
132 {"clean", no_argument, NULL, 'c'},
133 {"encoding", required_argument, NULL, 'E'},
134 {"file", required_argument, NULL, 'f'},
135 {"globals-only", no_argument, NULL, 'g'},
136 {"host", required_argument, NULL, 'h'},
137 {"dbname", required_argument, NULL, 'd'},
138 {"database", required_argument, NULL, 'l'},
139 {"no-owner", no_argument, NULL, 'O'},
140 {"port", required_argument, NULL, 'p'},
141 {"roles-only", no_argument, NULL, 'r'},
142 {"schema-only", no_argument, NULL, 's'},
143 {"superuser", required_argument, NULL, 'S'},
144 {"tablespaces-only", no_argument, NULL, 't'},
145 {"username", required_argument, NULL, 'U'},
146 {"verbose", no_argument, NULL, 'v'},
147 {"no-password", no_argument, NULL, 'w'},
148 {"password", no_argument, NULL, 'W'},
149 {"no-privileges", no_argument, NULL, 'x'},
150 {"no-acl", no_argument, NULL, 'x'},
151
152 /*
153 * the following options don't have an equivalent short option letter
154 */
155 {"attribute-inserts", no_argument, &column_inserts, 1},
156 {"binary-upgrade", no_argument, &binary_upgrade, 1},
157 {"column-inserts", no_argument, &column_inserts, 1},
158 {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
159 {"disable-triggers", no_argument, &disable_triggers, 1},
160 {"exclude-database", required_argument, NULL, 6},
161 {"extra-float-digits", required_argument, NULL, 5},
162 {"if-exists", no_argument, &if_exists, 1},
163 {"inserts", no_argument, &inserts, 1},
164 {"lock-wait-timeout", required_argument, NULL, 2},
165 {"no-table-access-method", no_argument, &no_table_access_method, 1},
166 {"no-tablespaces", no_argument, &no_tablespaces, 1},
167 {"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
168 {"load-via-partition-root", no_argument, &load_via_partition_root, 1},
169 {"role", required_argument, NULL, 3},
170 {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
171 {"no-comments", no_argument, &no_comments, 1},
172 {"no-data", no_argument, &no_data, 1},
173 {"no-policies", no_argument, &no_policies, 1},
174 {"no-publications", no_argument, &no_publications, 1},
175 {"no-role-passwords", no_argument, &no_role_passwords, 1},
176 {"no-schema", no_argument, &no_schema, 1},
177 {"no-security-labels", no_argument, &no_security_labels, 1},
178 {"no-subscriptions", no_argument, &no_subscriptions, 1},
179 {"no-statistics", no_argument, &no_statistics, 1},
180 {"no-sync", no_argument, NULL, 4},
181 {"no-toast-compression", no_argument, &no_toast_compression, 1},
182 {"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
183 {"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1},
184 {"rows-per-insert", required_argument, NULL, 7},
185 {"statistics", no_argument, &with_statistics, 1},
186 {"statistics-only", no_argument, &statistics_only, 1},
187 {"filter", required_argument, NULL, 8},
188 {"sequence-data", no_argument, &sequence_data, 1},
189 {"restrict-key", required_argument, NULL, 9},
190
191 {NULL, 0, NULL, 0}
192 };
193
194 char *pghost = NULL;
195 char *pgport = NULL;
196 char *pguser = NULL;
197 char *pgdb = NULL;
198 char *use_role = NULL;
199 const char *dumpencoding = NULL;
200 trivalue prompt_password = TRI_DEFAULT;
201 bool data_only = false;
202 bool globals_only = false;
203 bool roles_only = false;
204 bool tablespaces_only = false;
205 PGconn *conn;
206 int encoding;
207 const char *std_strings;
208 int c,
209 ret;
210 int optindex;
211
212 pg_logging_init(argv[0]);
214 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
215 progname = get_progname(argv[0]);
216
217 if (argc > 1)
218 {
219 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
220 {
221 help();
222 exit_nicely(0);
223 }
224 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
225 {
226 puts("pg_dumpall (PostgreSQL) " PG_VERSION);
227 exit_nicely(0);
228 }
229 }
230
231 if ((ret = find_other_exec(argv[0], "pg_dump", PGDUMP_VERSIONSTR,
232 pg_dump_bin)) < 0)
233 {
234 char full_path[MAXPGPATH];
235
236 if (find_my_exec(argv[0], full_path) < 0)
237 strlcpy(full_path, progname, sizeof(full_path));
238
239 if (ret == -1)
240 pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
241 "pg_dump", progname, full_path);
242 else
243 pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
244 "pg_dump", full_path, progname);
245 }
246
248
249 while ((c = getopt_long(argc, argv, "acd:E:f:gh:l:Op:rsS:tU:vwWx", long_options, &optindex)) != -1)
250 {
251 switch (c)
252 {
253 case 'a':
254 data_only = true;
256 break;
257
258 case 'c':
259 output_clean = true;
260 break;
261
262 case 'd':
264 break;
265
266 case 'E':
267 dumpencoding = pg_strdup(optarg);
270 break;
271
272 case 'f':
276 break;
277
278 case 'g':
279 globals_only = true;
280 break;
281
282 case 'h':
284 break;
285
286 case 'l':
287 pgdb = pg_strdup(optarg);
288 break;
289
290 case 'O':
292 break;
293
294 case 'p':
296 break;
297
298 case 'r':
299 roles_only = true;
300 break;
301
302 case 's':
304 break;
305
306 case 'S':
309 break;
310
311 case 't':
312 tablespaces_only = true;
313 break;
314
315 case 'U':
316 pguser = pg_strdup(optarg);
317 break;
318
319 case 'v':
320 verbose = true;
323 break;
324
325 case 'w':
326 prompt_password = TRI_NO;
328 break;
329
330 case 'W':
331 prompt_password = TRI_YES;
333 break;
334
335 case 'x':
336 skip_acls = true;
338 break;
339
340 case 0:
341 break;
342
343 case 2:
344 appendPQExpBufferStr(pgdumpopts, " --lock-wait-timeout ");
346 break;
347
348 case 3:
349 use_role = pg_strdup(optarg);
350 appendPQExpBufferStr(pgdumpopts, " --role ");
351 appendShellString(pgdumpopts, use_role);
352 break;
353
354 case 4:
355 dosync = false;
356 appendPQExpBufferStr(pgdumpopts, " --no-sync");
357 break;
358
359 case 5:
360 appendPQExpBufferStr(pgdumpopts, " --extra-float-digits ");
362 break;
363
364 case 6:
366 break;
367
368 case 7:
369 appendPQExpBufferStr(pgdumpopts, " --rows-per-insert ");
371 break;
372
373 case 8:
375 break;
376
377 case 9:
379 appendPQExpBufferStr(pgdumpopts, " --restrict-key ");
381 break;
382
383 default:
384 /* getopt_long already emitted a complaint */
385 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
386 exit_nicely(1);
387 }
388 }
389
390 /* Complain if any arguments remain */
391 if (optind < argc)
392 {
393 pg_log_error("too many command-line arguments (first is \"%s\")",
394 argv[optind]);
395 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
396 exit_nicely(1);
397 }
398
399 if (database_exclude_patterns.head != NULL &&
400 (globals_only || roles_only || tablespaces_only))
401 {
402 pg_log_error("option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only, or -t/--tablespaces-only");
403 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
404 exit_nicely(1);
405 }
406
407 /* Make sure the user hasn't specified a mix of globals-only options */
408 if (globals_only && roles_only)
409 {
410 pg_log_error("options -g/--globals-only and -r/--roles-only cannot be used together");
411 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
412 exit_nicely(1);
413 }
414
415 if (globals_only && tablespaces_only)
416 {
417 pg_log_error("options -g/--globals-only and -t/--tablespaces-only cannot be used together");
418 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
419 exit_nicely(1);
420 }
421
422 if (if_exists && !output_clean)
423 pg_fatal("option --if-exists requires option -c/--clean");
424
425 if (roles_only && tablespaces_only)
426 {
427 pg_log_error("options -r/--roles-only and -t/--tablespaces-only cannot be used together");
428 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
429 exit_nicely(1);
430 }
431
432 /*
433 * If password values are not required in the dump, switch to using
434 * pg_roles which is equally useful, just more likely to have unrestricted
435 * access than pg_authid.
436 */
439 else
441
442 /* Add long options to the pg_dump argument list */
443 if (binary_upgrade)
444 appendPQExpBufferStr(pgdumpopts, " --binary-upgrade");
445 if (column_inserts)
446 appendPQExpBufferStr(pgdumpopts, " --column-inserts");
448 appendPQExpBufferStr(pgdumpopts, " --disable-dollar-quoting");
450 appendPQExpBufferStr(pgdumpopts, " --disable-triggers");
451 if (inserts)
452 appendPQExpBufferStr(pgdumpopts, " --inserts");
454 appendPQExpBufferStr(pgdumpopts, " --no-table-access-method");
455 if (no_tablespaces)
456 appendPQExpBufferStr(pgdumpopts, " --no-tablespaces");
458 appendPQExpBufferStr(pgdumpopts, " --quote-all-identifiers");
460 appendPQExpBufferStr(pgdumpopts, " --load-via-partition-root");
461 if (use_setsessauth)
462 appendPQExpBufferStr(pgdumpopts, " --use-set-session-authorization");
463 if (no_comments)
464 appendPQExpBufferStr(pgdumpopts, " --no-comments");
465 if (no_data)
466 appendPQExpBufferStr(pgdumpopts, " --no-data");
467 if (no_policies)
468 appendPQExpBufferStr(pgdumpopts, " --no-policies");
469 if (no_publications)
470 appendPQExpBufferStr(pgdumpopts, " --no-publications");
472 appendPQExpBufferStr(pgdumpopts, " --no-security-labels");
473 if (no_schema)
474 appendPQExpBufferStr(pgdumpopts, " --no-schema");
475 if (no_statistics)
476 appendPQExpBufferStr(pgdumpopts, " --no-statistics");
478 appendPQExpBufferStr(pgdumpopts, " --no-subscriptions");
480 appendPQExpBufferStr(pgdumpopts, " --no-toast-compression");
482 appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data");
483 if (with_statistics)
484 appendPQExpBufferStr(pgdumpopts, " --statistics");
486 appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
487 if (statistics_only)
488 appendPQExpBufferStr(pgdumpopts, " --statistics-only");
489 if (sequence_data)
490 appendPQExpBufferStr(pgdumpopts, " --sequence-data");
491
492 /*
493 * If you don't provide a restrict key, one will be appointed for you.
494 */
495 if (!restrict_key)
497 if (!restrict_key)
498 pg_fatal("could not generate restrict key");
500 pg_fatal("invalid restrict key");
501
502 /*
503 * If there was a database specified on the command line, use that,
504 * otherwise try to connect to database "postgres", and failing that
505 * "template1".
506 */
507 if (pgdb)
508 {
509 conn = ConnectDatabase(pgdb, connstr, pghost, pgport, pguser,
510 prompt_password, false,
511 progname, &connstr, &server_version, NULL, NULL);
512
513 if (!conn)
514 pg_fatal("could not connect to database \"%s\"", pgdb);
515 }
516 else
517 {
518 conn = ConnectDatabase("postgres", connstr, pghost, pgport, pguser,
519 prompt_password, false,
520 progname, &connstr, &server_version, NULL, NULL);
521 if (!conn)
522 conn = ConnectDatabase("template1", connstr, pghost, pgport, pguser,
523 prompt_password, true,
524 progname, &connstr, &server_version, NULL, NULL);
525
526 if (!conn)
527 {
528 pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
529 "Please specify an alternative database.");
530 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
531 exit_nicely(1);
532 }
533 }
534
535 /*
536 * Get a list of database names that match the exclude patterns
537 */
540
541 /*
542 * Open the output file if required, otherwise use stdout
543 */
544 if (filename)
545 {
546 OPF = fopen(filename, PG_BINARY_W);
547 if (!OPF)
548 pg_fatal("could not open output file \"%s\": %m",
549 filename);
550 }
551 else
552 OPF = stdout;
553
554 /*
555 * Set the client encoding if requested.
556 */
557 if (dumpencoding)
558 {
559 if (PQsetClientEncoding(conn, dumpencoding) < 0)
560 pg_fatal("invalid client encoding \"%s\" specified",
561 dumpencoding);
562 }
563
564 /*
565 * Get the active encoding and the standard_conforming_strings setting, so
566 * we know how to escape strings.
567 */
570 std_strings = PQparameterStatus(conn, "standard_conforming_strings");
571 if (!std_strings)
572 std_strings = "off";
573
574 /* Set the role if requested */
575 if (use_role)
576 {
578
579 appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
580 executeCommand(conn, query->data);
581 destroyPQExpBuffer(query);
582 }
583
584 /* Force quoting of all identifiers if requested. */
586 executeCommand(conn, "SET quote_all_identifiers = true");
587
588 fprintf(OPF, "--\n-- PostgreSQL database cluster dump\n--\n\n");
589 if (verbose)
590 dumpTimestamp("Started on");
591
592 /*
593 * Enter restricted mode to block any unexpected psql meta-commands. A
594 * malicious source might try to inject a variety of things via bogus
595 * responses to queries. While we cannot prevent such sources from
596 * affecting the destination at restore time, we can block psql
597 * meta-commands so that the client machine that runs psql with the dump
598 * output remains unaffected.
599 */
600 fprintf(OPF, "\\restrict %s\n\n", restrict_key);
601
602 /*
603 * We used to emit \connect postgres here, but that served no purpose
604 * other than to break things for installations without a postgres
605 * database. Everything we're restoring here is a global, so whichever
606 * database we're connected to at the moment is fine.
607 */
608
609 /* Restore will need to write to the target cluster */
610 fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
611
612 /* Replicate encoding and std_strings in output */
613 fprintf(OPF, "SET client_encoding = '%s';\n",
615 fprintf(OPF, "SET standard_conforming_strings = %s;\n", std_strings);
616 if (strcmp(std_strings, "off") == 0)
617 fprintf(OPF, "SET escape_string_warning = off;\n");
618 fprintf(OPF, "\n");
619
620 if (!data_only && !statistics_only && !no_schema)
621 {
622 /*
623 * If asked to --clean, do that first. We can avoid detailed
624 * dependency analysis because databases never depend on each other,
625 * and tablespaces never depend on each other. Roles could have
626 * grants to each other, but DROP ROLE will clean those up silently.
627 */
628 if (output_clean)
629 {
630 if (!globals_only && !roles_only && !tablespaces_only)
631 dropDBs(conn);
632
633 if (!roles_only && !no_tablespaces)
635
636 if (!tablespaces_only)
638 }
639
640 /*
641 * Now create objects as requested. Be careful that option logic here
642 * is the same as for drops above.
643 */
644 if (!tablespaces_only)
645 {
646 /* Dump roles (users) */
648
649 /* Dump role memberships */
651
652 /* Dump role GUC privileges */
653 if (server_version >= 150000 && !skip_acls)
655 }
656
657 /* Dump tablespaces */
658 if (!roles_only && !no_tablespaces)
660 }
661
662 /*
663 * Exit restricted mode just before dumping the databases. pg_dump will
664 * handle entering restricted mode again as appropriate.
665 */
666 fprintf(OPF, "\\unrestrict %s\n\n", restrict_key);
667
668 if (!globals_only && !roles_only && !tablespaces_only)
670
671 PQfinish(conn);
672
673 if (verbose)
674 dumpTimestamp("Completed on");
675 fprintf(OPF, "--\n-- PostgreSQL database cluster dump complete\n--\n\n");
676
677 if (filename)
678 {
679 fclose(OPF);
680
681 /* sync the resulting file, errors are not fatal */
682 if (dosync)
683 (void) fsync_fname(filename, false);
684 }
685
686 exit_nicely(0);
687}
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1213
#define PG_BINARY_W
Definition: c.h:1275
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:161
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:430
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:311
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)
Definition: connectdb.c:40
char * generate_restrict_key(void)
Definition: dumputils.c:968
bool valid_restrict_key(const char *restrict_key)
Definition: dumputils.c:992
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:753
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7634
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7769
int PQsetClientEncoding(PGconn *conn, const char *encoding)
Definition: fe-connect.c:7777
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:25
#define required_argument
Definition: getopt_long.h:26
void pg_logging_increase_verbosity(void)
Definition: logging.c:185
void pg_logging_init(const char *argv0)
Definition: logging.c:83
void pg_logging_set_level(enum pg_log_level new_level)
Definition: logging.c:176
#define pg_log_error_hint(...)
Definition: logging.h:112
@ PG_LOG_WARNING
Definition: logging.h:38
#define MAXPGPATH
int32 encoding
Definition: pg_database.h:41
static void dumpTimestamp(const char *msg)
Definition: pg_dumpall.c:1804
static int on_conflict_do_nothing
Definition: pg_dumpall.c:111
static void dropTablespaces(PGconn *conn)
Definition: pg_dumpall.c:1330
static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns, SimpleStringList *names)
Definition: pg_dumpall.c:1558
static PQExpBuffer pgdumpopts
Definition: pg_dumpall.c:81
static int statistics_only
Definition: pg_dumpall.c:112
static int no_table_access_method
Definition: pg_dumpall.c:94
static int no_unlogged_table_data
Definition: pg_dumpall.c:106
#define PG_AUTHID
Definition: pg_dumpall.c:116
static int no_policies
Definition: pg_dumpall.c:98
static int disable_triggers
Definition: pg_dumpall.c:91
static bool dosync
Definition: pg_dumpall.c:86
static const char * connstr
Definition: pg_dumpall.c:82
static SimpleStringList database_exclude_patterns
Definition: pg_dumpall.c:122
static void dumpTablespaces(PGconn *conn)
Definition: pg_dumpall.c:1365
static void dumpRoleMembership(PGconn *conn)
Definition: pg_dumpall.c:1025
static char pg_dump_bin[MAXPGPATH]
Definition: pg_dumpall.c:80
static int no_publications
Definition: pg_dumpall.c:99
static int sequence_data
Definition: pg_dumpall.c:113
static char * restrict_key
Definition: pg_dumpall.c:125
static void dumpDatabases(PGconn *conn)
Definition: pg_dumpall.c:1611
static int disable_dollar_quoting
Definition: pg_dumpall.c:90
static int no_tablespaces
Definition: pg_dumpall.c:95
static int no_toast_compression
Definition: pg_dumpall.c:105
#define PG_ROLES
Definition: pg_dumpall.c:117
static int inserts
Definition: pg_dumpall.c:93
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1780
static bool verbose
Definition: pg_dumpall.c:85
static void dumpRoleGUCPrivs(PGconn *conn)
Definition: pg_dumpall.c:1275
static int no_statistics
Definition: pg_dumpall.c:103
static void dumpRoles(PGconn *conn)
Definition: pg_dumpall.c:817
static void help(void)
Definition: pg_dumpall.c:691
static int use_setsessauth
Definition: pg_dumpall.c:96
static int no_data
Definition: pg_dumpall.c:101
static int load_via_partition_root
Definition: pg_dumpall.c:110
static int column_inserts
Definition: pg_dumpall.c:89
static void read_dumpall_filters(const char *filename, SimpleStringList *pattern)
Definition: pg_dumpall.c:1823
static void dropRoles(PGconn *conn)
Definition: pg_dumpall.c:770
static void dropDBs(PGconn *conn)
Definition: pg_dumpall.c:1469
static int no_schema
Definition: pg_dumpall.c:102
static int no_subscriptions
Definition: pg_dumpall.c:104
static int with_statistics
Definition: pg_dumpall.c:108
#define PGDUMP_VERSIONSTR
Definition: pg_dumpall.c:35
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
#define pg_encoding_to_char
Definition: pg_wchar.h:630
static const char * pghost
Definition: pgbench.c:295
static const char * pgport
Definition: pgbench.c:296
#define sprintf
Definition: port.h:241
const char * get_progname(const char *argv0)
Definition: path.c:652
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
c
char * c
Definition: preproc-cursor.c:31
bool quote_all_identifiers
Definition: ruleutils.c:339
void setFmtEncoding(int encoding)
Definition: string_utils.c:69
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:582
trivalue
Definition: vacuumlo.c:35
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37

References appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), binary_upgrade, column_inserts, conn, ConnectDatabase(), connstr, createPQExpBuffer(), PQExpBufferData::data, database_exclude_names, database_exclude_patterns, destroyPQExpBuffer(), disable_dollar_quoting, disable_triggers, dosync, dropDBs(), dropRoles(), dropTablespaces(), dumpDatabases(), dumpRoleGUCPrivs(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpTimestamp(), encoding, executeCommand(), exit_nicely(), expand_dbname_patterns(), filename, find_my_exec(), find_other_exec(), fmtId(), fprintf, fsync_fname(), generate_restrict_key(), get_progname(), getopt_long(), SimpleStringList::head, help(), if_exists, inserts, load_via_partition_root, MAXPGPATH, no_argument, no_comments, no_data, no_policies, no_publications, no_role_passwords, no_schema, no_security_labels, no_statistics, no_subscriptions, no_table_access_method, no_tablespaces, no_toast_compression, no_unlogged_table_data, on_conflict_do_nothing, OPF, optarg, optind, output_clean, PG_AUTHID, PG_BINARY_W, pg_dump_bin, pg_encoding_to_char, pg_fatal, pg_log_error, pg_log_error_hint, PG_LOG_WARNING, pg_logging_increase_verbosity(), pg_logging_init(), pg_logging_set_level(), PG_ROLES, pg_strdup(), PG_TEXTDOMAIN, PGDUMP_VERSIONSTR, pgdumpopts, pghost, pgport, PQclientEncoding(), PQfinish(), PQparameterStatus(), PQsetClientEncoding(), progname, quote_all_identifiers, read_dumpall_filters(), required_argument, restrict_key, role_catalog, sequence_data, server_version, set_pglocale_pgservice(), setFmtEncoding(), simple_string_list_append(), skip_acls, sprintf, statistics_only, generate_unaccent_rules::stdout, strlcpy(), TRI_DEFAULT, TRI_NO, TRI_YES, use_setsessauth, valid_restrict_key(), verbose, and with_statistics.

read_dumpall_filters()

static void read_dumpall_filters ( const char *  filename,
SimpleStringListpattern 
)
static

Definition at line 1823 of file pg_dumpall.c.

1824{
1825 FilterStateData fstate;
1826 char *objname;
1827 FilterCommandType comtype;
1828 FilterObjectType objtype;
1829
1830 filter_init(&fstate, filename, exit);
1831
1832 while (filter_read_item(&fstate, &objname, &comtype, &objtype))
1833 {
1834 if (comtype == FILTER_COMMAND_TYPE_INCLUDE)
1835 {
1836 pg_log_filter_error(&fstate, _("%s filter for \"%s\" is not allowed"),
1837 "include",
1838 filter_object_type_name(objtype));
1839 exit_nicely(1);
1840 }
1841
1842 switch (objtype)
1843 {
1845 break;
1856 pg_log_filter_error(&fstate, _("unsupported filter object"));
1857 exit_nicely(1);
1858 break;
1859
1861 simple_string_list_append(pattern, objname);
1862 break;
1863 }
1864
1865 if (objname)
1866 free(objname);
1867 }
1868
1869 filter_free(&fstate);
1870}
void filter_init(FilterStateData *fstate, const char *filename, exit_function f_exit)
Definition: filter.c:36
void filter_free(FilterStateData *fstate)
Definition: filter.c:60
const char * filter_object_type_name(FilterObjectType fot)
Definition: filter.c:82
bool filter_read_item(FilterStateData *fstate, char **objname, FilterCommandType *comtype, FilterObjectType *objtype)
Definition: filter.c:392
void pg_log_filter_error(FilterStateData *fstate, const char *fmt,...)
Definition: filter.c:154
FilterObjectType
Definition: filter.h:48
@ FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN
Definition: filter.h:51
@ FILTER_OBJECT_TYPE_SCHEMA
Definition: filter.h:57
@ FILTER_OBJECT_TYPE_INDEX
Definition: filter.h:56
@ FILTER_OBJECT_TYPE_TRIGGER
Definition: filter.h:60
@ FILTER_OBJECT_TYPE_FOREIGN_DATA
Definition: filter.h:54
@ FILTER_OBJECT_TYPE_DATABASE
Definition: filter.h:52
@ FILTER_OBJECT_TYPE_FUNCTION
Definition: filter.h:55
@ FILTER_OBJECT_TYPE_TABLE_DATA
Definition: filter.h:50
@ FILTER_OBJECT_TYPE_NONE
Definition: filter.h:49
@ FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN
Definition: filter.h:59
@ FILTER_OBJECT_TYPE_EXTENSION
Definition: filter.h:53
@ FILTER_OBJECT_TYPE_TABLE
Definition: filter.h:58
FilterCommandType
Definition: filter.h:38
@ FILTER_COMMAND_TYPE_INCLUDE
Definition: filter.h:40

References _, exit_nicely(), filename, FILTER_COMMAND_TYPE_INCLUDE, filter_free(), filter_init(), FILTER_OBJECT_TYPE_DATABASE, FILTER_OBJECT_TYPE_EXTENSION, FILTER_OBJECT_TYPE_FOREIGN_DATA, FILTER_OBJECT_TYPE_FUNCTION, FILTER_OBJECT_TYPE_INDEX, filter_object_type_name(), FILTER_OBJECT_TYPE_NONE, FILTER_OBJECT_TYPE_SCHEMA, FILTER_OBJECT_TYPE_TABLE, FILTER_OBJECT_TYPE_TABLE_AND_CHILDREN, FILTER_OBJECT_TYPE_TABLE_DATA, FILTER_OBJECT_TYPE_TABLE_DATA_AND_CHILDREN, FILTER_OBJECT_TYPE_TRIGGER, filter_read_item(), free, pg_log_filter_error(), and simple_string_list_append().

Referenced by main().

runPgDump()

static int runPgDump ( const char *  dbname,
const char *  create_opts 
)
static

Definition at line 1707 of file pg_dumpall.c.

1708{
1709 PQExpBufferData connstrbuf;
1710 PQExpBufferData cmd;
1711 int ret;
1712
1713 initPQExpBuffer(&connstrbuf);
1714 initPQExpBuffer(&cmd);
1715
1716 printfPQExpBuffer(&cmd, "\"%s\" %s %s", pg_dump_bin,
1717 pgdumpopts->data, create_opts);
1718
1719 /*
1720 * If we have a filename, use the undocumented plain-append pg_dump
1721 * format.
1722 */
1723 if (filename)
1724 appendPQExpBufferStr(&cmd, " -Fa ");
1725 else
1726 appendPQExpBufferStr(&cmd, " -Fp ");
1727
1728 /*
1729 * Append the database name to the already-constructed stem of connection
1730 * string.
1731 */
1732 appendPQExpBuffer(&connstrbuf, "%s dbname=", connstr);
1733 appendConnStrVal(&connstrbuf, dbname);
1734
1735 appendShellString(&cmd, connstrbuf.data);
1736
1737 pg_log_info("running \"%s\"", cmd.data);
1738
1739 fflush(NULL);
1740
1741 ret = system(cmd.data);
1742
1743 termPQExpBuffer(&cmd);
1744 termPQExpBuffer(&connstrbuf);
1745
1746 return ret;
1747}
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:698

References appendConnStrVal(), appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), connstr, PQExpBufferData::data, dbname, filename, initPQExpBuffer(), pg_dump_bin, pg_log_info, pgdumpopts, printfPQExpBuffer(), and termPQExpBuffer().

Referenced by dumpDatabases().

Variable Documentation

binary_upgrade

int binary_upgrade = 0
static

Definition at line 88 of file pg_dumpall.c.

Referenced by dumpRoles(), dumpTablespaces(), and main().

column_inserts

int column_inserts = 0
static

Definition at line 89 of file pg_dumpall.c.

Referenced by main().

connstr

const char* connstr = ""
static

Definition at line 82 of file pg_dumpall.c.

Referenced by appendPsqlMetaConnect(), ConnectDatabase(), constructConnStr(), dblink_connect(), dblink_connstr_check(), dblink_connstr_has_pw(), dblink_connstr_has_required_scram_options(), dblink_get_conn(), dblink_security_check(), generate_old_dump(), GetDbnameFromConnectionOptions(), main(), parse_connection_string(), recognized_connection_string(), runPgDump(), and uri_prefix_length().

database_exclude_names

SimpleStringList database_exclude_names = {NULL, NULL}
static

Definition at line 123 of file pg_dumpall.c.

Referenced by dumpDatabases(), and main().

database_exclude_patterns

SimpleStringList database_exclude_patterns = {NULL, NULL}
static

Definition at line 122 of file pg_dumpall.c.

Referenced by main().

disable_dollar_quoting

int disable_dollar_quoting = 0
static

Definition at line 90 of file pg_dumpall.c.

Referenced by main().

disable_triggers

int disable_triggers = 0
static

Definition at line 91 of file pg_dumpall.c.

Referenced by main().

dosync

bool dosync = true
static

Definition at line 86 of file pg_dumpall.c.

Referenced by main().

filename

char* filename = NULL
static

Definition at line 120 of file pg_dumpall.c.

Referenced by _fileExistsInDirectory(), _PrintFileData(), _tarPositionTo(), _tarWriteHeader(), adjust_data_dir(), BaseBackup(), basebackup_read_file(), bbsink_server_begin_archive(), bbsink_server_end_manifest(), be_lo_export(), be_lo_import(), be_lo_import_with_oid(), BeginCopyFrom(), BeginCopyTo(), check_file_excluded(), CheckXLogRemoved(), convert_and_check_filename(), create_file_for_extract(), CreateExtensionInternal(), CreateLockFile(), db_dir_size(), dir_close(), dir_get_file_name(), dir_open_for_write(), dispell_init(), dsynonym_init(), dumpDatabases(), dxsyn_init(), ecpg_postprocess_result(), errfinish(), errsave_finish(), execute_extension_script(), execute_sql_string(), expand_tilde(), exportFile(), extract_directory(), extract_link(), file_acquire_sample_rows(), file_fdw_validator(), fileAnalyzeForeignTable(), fileBeginForeignScan(), fileExplainForeignScan(), fileGetOptions(), fill_hba_line(), fill_ident_line(), filter_init(), first_dir_separator(), get_sock_dir(), GetConfFilesInDir(), hasSuffix(), importFile(), initTrie(), internal_load_library(), is_extension_control_filename(), is_extension_script_filename(), is_xlogfilename(), IsWalSummaryFilename(), last_dir_separator(), libpqrcv_readtimelinehistoryfile(), llvm_compile_module(), lo_export(), lo_import(), lo_import_internal(), lo_import_with_oid(), load_dh_file(), load_external_function(), load_file(), load_libraries(), load_tzoffsets(), log_locus_callback(), logfile_getname(), logfile_open(), logfile_rotate_dest(), LogicalTapeImport(), LogicalTapeSetCreate(), main(), make_incremental_rfile(), make_native_path(), make_outputdirs(), make_rfile(), next_field_expand(), NIImportAffixes(), NIImportDictionary(), NIImportOOAffixes(), open_auth_file(), parse_backup_label(), parse_extension_control_file(), parse_relfilenumber(), parse_snapshot_filename(), parseIntFromText(), ParseTzFile(), parseVxidFromText(), parseXidFromText(), pg_log_generic_v(), pg_stat_file(), PLy_traceback(), process_file(), process_psqlrc_file(), process_queued_fetch_requests(), progress_update_filename(), read_binary_file(), read_dictionary(), read_dump_filters(), read_dumpall_filters(), read_extension_script_file(), read_pg_version_file(), read_restore_filters(), read_text_file(), read_whole_file(), readstoplist(), reconstruct_from_incremental_file(), regcomp_auth_token(), run_diff(), runPgDump(), sendFileWithContent(), set_input(), set_stack_entry_location(), SetOutput(), setup_run_file(), shell_construct_command(), shell_run_command(), should_allow_existing_directory(), slurp_file(), splitTzLine(), SysLogger_Start(), tar_get_file_name(), tarCreateHeader(), tarOpen(), test_slru_scan_cb(), thesaurusRead(), tokenize_auth_file(), TrimExtension(), tsearch_readline_begin(), UpdateLastRemovedPtr(), verify_plain_backup_directory(), verify_tar_backup(), write_auto_conf_file(), WriteRecoveryConfig(), writeTimeLineHistoryFile(), and XLogRecordSaveFPWs().

if_exists

int if_exists = 0
static

Definition at line 92 of file pg_dumpall.c.

Referenced by dropDBs(), dropRoles(), dropTablespaces(), and main().

inserts

int inserts = 0
static

Definition at line 93 of file pg_dumpall.c.

Referenced by main().

load_via_partition_root

int load_via_partition_root = 0
static

Definition at line 110 of file pg_dumpall.c.

Referenced by main().

no_comments

int no_comments = 0
static

Definition at line 97 of file pg_dumpall.c.

Referenced by dumpRoles(), dumpTablespaces(), and main().

no_data

int no_data = 0
static

Definition at line 101 of file pg_dumpall.c.

Referenced by main().

no_policies

int no_policies = 0
static

Definition at line 98 of file pg_dumpall.c.

Referenced by main().

no_publications

int no_publications = 0
static

Definition at line 99 of file pg_dumpall.c.

Referenced by main().

no_role_passwords

int no_role_passwords = 0
static

Definition at line 107 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

no_schema

int no_schema = 0
static

Definition at line 102 of file pg_dumpall.c.

Referenced by main().

no_security_labels

int no_security_labels = 0
static

Definition at line 100 of file pg_dumpall.c.

Referenced by dumpRoles(), dumpTablespaces(), and main().

no_statistics

int no_statistics = 0
static

Definition at line 103 of file pg_dumpall.c.

Referenced by main().

no_subscriptions

int no_subscriptions = 0
static

Definition at line 104 of file pg_dumpall.c.

Referenced by main().

no_table_access_method

int no_table_access_method = 0
static

Definition at line 94 of file pg_dumpall.c.

Referenced by main().

no_tablespaces

int no_tablespaces = 0
static

Definition at line 95 of file pg_dumpall.c.

Referenced by main().

no_toast_compression

int no_toast_compression = 0
static

Definition at line 105 of file pg_dumpall.c.

Referenced by main().

no_unlogged_table_data

int no_unlogged_table_data = 0
static

Definition at line 106 of file pg_dumpall.c.

Referenced by main().

on_conflict_do_nothing

int on_conflict_do_nothing = 0
static

Definition at line 111 of file pg_dumpall.c.

Referenced by main().

OPF

FILE* OPF
static

Definition at line 119 of file pg_dumpall.c.

Referenced by dropDBs(), dropRoles(), dropTablespaces(), dumpDatabases(), dumpRoleGUCPrivs(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpTimestamp(), dumpUserConfig(), and main().

output_clean

bool output_clean = false
static

Definition at line 83 of file pg_dumpall.c.

Referenced by dumpDatabases(), and main().

pg_dump_bin

char pg_dump_bin[MAXPGPATH]
static

Definition at line 80 of file pg_dumpall.c.

Referenced by main(), and runPgDump().

pgdumpopts

PQExpBuffer pgdumpopts
static

Definition at line 81 of file pg_dumpall.c.

Referenced by main(), and runPgDump().

restrict_key

char* restrict_key
static

Definition at line 125 of file pg_dumpall.c.

Referenced by main(), and valid_restrict_key().

role_catalog

char role_catalog[10]
static

Definition at line 115 of file pg_dumpall.c.

Referenced by dropRoles(), dumpRoleMembership(), dumpRoles(), dumpUserConfig(), and main().

sequence_data

int sequence_data = 0
static

Definition at line 113 of file pg_dumpall.c.

Referenced by main().

server_version

int server_version
static

Definition at line 109 of file pg_dumpall.c.

Referenced by ConnectDatabase(), connection_warnings(), dropRoles(), dumpRoleGUCPrivs(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), fetch_remote_table_info(), fetch_table_list(), main(), printVersion(), set_stream_options(), and SyncVariables().

skip_acls

bool skip_acls = false
static

Definition at line 84 of file pg_dumpall.c.

Referenced by dumpTablespaces(), and main().

statistics_only

int statistics_only = 0
static

Definition at line 112 of file pg_dumpall.c.

Referenced by main().

use_setsessauth

int use_setsessauth = 0
static

Definition at line 96 of file pg_dumpall.c.

Referenced by main().

verbose

bool verbose = false
static

Definition at line 85 of file pg_dumpall.c.

Referenced by main().

with_statistics

int with_statistics = 0
static

Definition at line 108 of file pg_dumpall.c.

Referenced by main().

AltStyle によって変換されたページ (->オリジナル) /