PostgreSQL Source Code  git master
pg_dumpall.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include <unistd.h>
#include "common/connect.h"
#include "common/file_utils.h"
#include "common/logging.h"
#include "common/string.h"
#include "dumputils.h"
#include "fe_utils/string_utils.h"
#include "getopt_long.h"
#include "pg_backup.h"
Include dependency graph for pg_dumpall.c:

Go to the source code of this file.

Macros

#define PGDUMP_VERSIONSTR   "pg_dump (PostgreSQL) " PG_VERSION "\n"
 
#define PG_AUTHID   "pg_authid"
 
#define PG_ROLES   "pg_roles "
 
#define exit_nicely(code)   exit(code)
 

Functions

static void help (void)
 
static void dropRoles (PGconn *conn)
 
static void dumpRoles (PGconn *conn)
 
static void dumpRoleMembership (PGconn *conn)
 
static void dumpGroups (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 PGconnconnectDatabase (const char *dbname, const char *connstr, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
 
static char * constructConnStr (const char **keywords, const char **values)
 
static PGresultexecuteQuery (PGconn *conn, const char *query)
 
static void executeCommand (PGconn *conn, const char *query)
 
static void expand_dbname_patterns (PGconn *conn, SimpleStringList *patterns, SimpleStringList *names)
 
int main (int argc, char *argv[])
 

Variables

static char pg_dump_bin [MAXPGPATH]
 
static const char * progname
 
static PQExpBuffer pgdumpopts
 
static 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_tablespaces = 0
 
static int use_setsessauth = 0
 
static int no_comments = 0
 
static int no_publications = 0
 
static int no_security_labels = 0
 
static int no_subscriptions = 0
 
static int no_unlogged_table_data = 0
 
static int no_role_passwords = 0
 
static int server_version
 
static int load_via_partition_root = 0
 
static int on_conflict_do_nothing = 0
 
static char role_catalog [10]
 
static FILE * OPF
 
static char * filename = NULL
 
static SimpleStringList database_exclude_patterns = {NULL, NULL}
 
static SimpleStringList database_exclude_names = {NULL, NULL}
 

Macro Definition Documentation

◆ exit_nicely

◆ PG_AUTHID

#define PG_AUTHID   "pg_authid"

Definition at line 87 of file pg_dumpall.c.

Referenced by main().

◆ PG_ROLES

#define PG_ROLES   "pg_roles "

Definition at line 88 of file pg_dumpall.c.

Referenced by main().

◆ PGDUMP_VERSIONSTR

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

Definition at line 31 of file pg_dumpall.c.

Referenced by main().

Function Documentation

◆ buildShSecLabels()

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

Definition at line 1610 of file pg_dumpall.c.

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

Referenced by dumpRoles(), and dumpTablespaces().

1613 {
1615  PGresult *res;
1616 
1617  buildShSecLabelQuery(catalog_name, objectId, sql);
1618  res = executeQuery(conn, sql->data);
1619  emitShSecLabels(conn, res, buffer, objtype, objname);
1620 
1621  PQclear(res);
1622  destroyPQExpBuffer(sql);
1623 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
void buildShSecLabelQuery(const char *catalog_name, Oid objectId, PQExpBuffer sql)
Definition: dumputils.c:688
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *objtype, const char *objname)
Definition: dumputils.c:706
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694

◆ connectDatabase()

static PGconn * connectDatabase ( const char *  dbname,
const char *  connstr,
const char *  pghost,
const char *  pgport,
const char *  pguser,
trivalue  prompt_password,
bool  fail_on_error 
)
static

Definition at line 1636 of file pg_dumpall.c.

References ALWAYS_SECURE_SEARCH_PATH_SQL, conn, CONNECTION_BAD, connstr, constructConnStr(), dbname, executeQuery(), exit_nicely, free, i, _PQconninfoOption::keyword, password, pg_log_error, pg_malloc0(), pghost, pgport, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQconninfoFree(), PQconninfoParse(), PQerrorMessage(), PQfinish(), PQparameterStatus(), PQserverVersion(), PQstatus(), progname, server_version, simple_prompt(), TRI_NO, TRI_YES, _PQconninfoOption::val, and values.

Referenced by cluster_one_database(), main(), ParallelSlotsSetup(), reindex_one_database(), and vacuum_one_database().

1639 {
1640  PGconn *conn;
1641  bool new_pass;
1642  const char *remoteversion_str;
1643  int my_version;
1644  const char **keywords = NULL;
1645  const char **values = NULL;
1646  PQconninfoOption *conn_opts = NULL;
1647  static char *password = NULL;
1648 
1649  if (prompt_password == TRI_YES && !password)
1650  password = simple_prompt("Password: ", false);
1651 
1652  /*
1653  * Start the connection. Loop until we have a password if requested by
1654  * backend.
1655  */
1656  do
1657  {
1658  int argcount = 6;
1659  PQconninfoOption *conn_opt;
1660  char *err_msg = NULL;
1661  int i = 0;
1662 
1663  if (keywords)
1664  free(keywords);
1665  if (values)
1666  free(values);
1667  if (conn_opts)
1668  PQconninfoFree(conn_opts);
1669 
1670  /*
1671  * Merge the connection info inputs given in form of connection string
1672  * and other options. Explicitly discard any dbname value in the
1673  * connection string; otherwise, PQconnectdbParams() would interpret
1674  * that value as being itself a connection string.
1675  */
1676  if (connection_string)
1677  {
1678  conn_opts = PQconninfoParse(connection_string, &err_msg);
1679  if (conn_opts == NULL)
1680  {
1681  pg_log_error("%s", err_msg);
1682  exit_nicely(1);
1683  }
1684 
1685  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1686  {
1687  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1688  strcmp(conn_opt->keyword, "dbname") != 0)
1689  argcount++;
1690  }
1691 
1692  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1693  values = pg_malloc0((argcount + 1) * sizeof(*values));
1694 
1695  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1696  {
1697  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1698  strcmp(conn_opt->keyword, "dbname") != 0)
1699  {
1700  keywords[i] = conn_opt->keyword;
1701  values[i] = conn_opt->val;
1702  i++;
1703  }
1704  }
1705  }
1706  else
1707  {
1708  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1709  values = pg_malloc0((argcount + 1) * sizeof(*values));
1710  }
1711 
1712  if (pghost)
1713  {
1714  keywords[i] = "host";
1715  values[i] = pghost;
1716  i++;
1717  }
1718  if (pgport)
1719  {
1720  keywords[i] = "port";
1721  values[i] = pgport;
1722  i++;
1723  }
1724  if (pguser)
1725  {
1726  keywords[i] = "user";
1727  values[i] = pguser;
1728  i++;
1729  }
1730  if (password)
1731  {
1732  keywords[i] = "password";
1733  values[i] = password;
1734  i++;
1735  }
1736  if (dbname)
1737  {
1738  keywords[i] = "dbname";
1739  values[i] = dbname;
1740  i++;
1741  }
1742  keywords[i] = "fallback_application_name";
1743  values[i] = progname;
1744  i++;
1745 
1746  new_pass = false;
1747  conn = PQconnectdbParams(keywords, values, true);
1748 
1749  if (!conn)
1750  {
1751  pg_log_error("could not connect to database \"%s\"", dbname);
1752  exit_nicely(1);
1753  }
1754 
1755  if (PQstatus(conn) == CONNECTION_BAD &&
1756  PQconnectionNeedsPassword(conn) &&
1757  !password &&
1758  prompt_password != TRI_NO)
1759  {
1760  PQfinish(conn);
1761  password = simple_prompt("Password: ", false);
1762  new_pass = true;
1763  }
1764  } while (new_pass);
1765 
1766  /* check to see that the backend connection was successfully made */
1767  if (PQstatus(conn) == CONNECTION_BAD)
1768  {
1769  if (fail_on_error)
1770  {
1771  pg_log_error("could not connect to database \"%s\": %s",
1772  dbname, PQerrorMessage(conn));
1773  exit_nicely(1);
1774  }
1775  else
1776  {
1777  PQfinish(conn);
1778 
1779  free(keywords);
1780  free(values);
1781  PQconninfoFree(conn_opts);
1782 
1783  return NULL;
1784  }
1785  }
1786 
1787  /*
1788  * Ok, connected successfully. Remember the options used, in the form of a
1789  * connection string.
1790  */
1791  connstr = constructConnStr(keywords, values);
1792 
1793  free(keywords);
1794  free(values);
1795  PQconninfoFree(conn_opts);
1796 
1797  /* Check version */
1798  remoteversion_str = PQparameterStatus(conn, "server_version");
1799  if (!remoteversion_str)
1800  {
1801  pg_log_error("could not get server version");
1802  exit_nicely(1);
1803  }
1805  if (server_version == 0)
1806  {
1807  pg_log_error("could not parse server version \"%s\"",
1808  remoteversion_str);
1809  exit_nicely(1);
1810  }
1811 
1812  my_version = PG_VERSION_NUM;
1813 
1814  /*
1815  * We allow the server to be back to 8.0, and up to any minor release of
1816  * our own major version. (See also version check in pg_dump.c.)
1817  */
1818  if (my_version != server_version
1819  && (server_version < 80000 ||
1820  (server_version / 100) > (my_version / 100)))
1821  {
1822  pg_log_error("server version: %s; %s version: %s",
1823  remoteversion_str, progname, PG_VERSION);
1824  pg_log_error("aborting because of server version mismatch");
1825  exit_nicely(1);
1826  }
1827 
1829 
1830  return conn;
1831 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6666
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6631
#define pg_log_error(...)
Definition: logging.h:80
static char * constructConnStr(const char **keywords, const char **values)
Definition: pg_dumpall.c:1844
static const char * progname
Definition: pg_dumpall.c:60
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4171
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6656
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:647
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5268
char * connection_string
Definition: streamutil.c:47
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
PGconn * conn
Definition: streamutil.c:54
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:6498
static char * password
Definition: streamutil.c:53
char * pghost
Definition: pgbench.c:243
#define exit_nicely(code)
Definition: pg_dumpall.c:96
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
static int server_version
Definition: pg_dumpall.c:82
char * dbname
Definition: streamutil.c:51
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition: connect.h:25
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6700
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6613
char * pgport
Definition: pgbench.c:244
static char * connstr
Definition: pg_dumpall.c:62

◆ constructConnStr()

static char * constructConnStr ( const char **  keywords,
const char **  values 
)
static

Definition at line 1844 of file pg_dumpall.c.

References appendConnStrVal(), appendPQExpBuffer(), appendPQExpBufferChar(), buf, connstr, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), i, and pg_strdup().

Referenced by connectDatabase().

1845 {
1847  char *connstr;
1848  int i;
1849  bool firstkeyword = true;
1850 
1851  /* Construct a new connection string in key='value' format. */
1852  for (i = 0; keywords[i] != NULL; i++)
1853  {
1854  if (strcmp(keywords[i], "dbname") == 0 ||
1855  strcmp(keywords[i], "password") == 0 ||
1856  strcmp(keywords[i], "fallback_application_name") == 0)
1857  continue;
1858 
1859  if (!firstkeyword)
1860  appendPQExpBufferChar(buf, ' ');
1861  firstkeyword = false;
1862  appendPQExpBuffer(buf, "%s=", keywords[i]);
1863  appendConnStrVal(buf, values[i]);
1864  }
1865 
1866  connstr = pg_strdup(buf->data);
1867  destroyPQExpBuffer(buf);
1868  return connstr;
1869 }
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:545
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:68
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int i
static char * connstr
Definition: pg_dumpall.c:62

◆ dropDBs()

static void dropDBs ( PGconn conn)
static

Definition at line 1312 of file pg_dumpall.c.

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

Referenced by main().

1313 {
1314  PGresult *res;
1315  int i;
1316 
1317  /*
1318  * Skip databases marked not datallowconn, since we'd be unable to connect
1319  * to them anyway. This must agree with dumpDatabases().
1320  */
1321  res = executeQuery(conn,
1322  "SELECT datname "
1323  "FROM pg_database d "
1324  "WHERE datallowconn "
1325  "ORDER BY datname");
1326 
1327  if (PQntuples(res) > 0)
1328  fprintf(OPF, "--\n-- Drop databases (except postgres and template1)\n--\n\n");
1329 
1330  for (i = 0; i < PQntuples(res); i++)
1331  {
1332  char *dbname = PQgetvalue(res, i, 0);
1333 
1334  /*
1335  * Skip "postgres" and "template1"; dumpDatabases() will deal with
1336  * them specially. Also, be sure to skip "template0", even if for
1337  * some reason it's not marked !datallowconn.
1338  */
1339  if (strcmp(dbname, "template1") != 0 &&
1340  strcmp(dbname, "template0") != 0 &&
1341  strcmp(dbname, "postgres") != 0)
1342  {
1343  fprintf(OPF, "DROP DATABASE %s%s;\n",
1344  if_exists ? "IF EXISTS " : "",
1345  fmtId(dbname));
1346  }
1347  }
1348 
1349  PQclear(res);
1350 
1351  fprintf(OPF, "\n\n");
1352 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
static FILE * OPF
Definition: pg_dumpall.c:90
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int if_exists
Definition: pg_dumpall.c:72
char * dbname
Definition: streamutil.c:51
int i

◆ dropRoles()

static void dropRoles ( PGconn conn)
static

Definition at line 683 of file pg_dumpall.c.

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

Referenced by main().

684 {
686  PGresult *res;
687  int i_rolname;
688  int i;
689 
690  if (server_version >= 90600)
691  printfPQExpBuffer(buf,
692  "SELECT rolname "
693  "FROM %s "
694  "WHERE rolname !~ '^pg_' "
695  "ORDER BY 1", role_catalog);
696  else if (server_version >= 80100)
697  printfPQExpBuffer(buf,
698  "SELECT rolname "
699  "FROM %s "
700  "ORDER BY 1", role_catalog);
701  else
702  printfPQExpBuffer(buf,
703  "SELECT usename as rolname "
704  "FROM pg_shadow "
705  "UNION "
706  "SELECT groname as rolname "
707  "FROM pg_group "
708  "ORDER BY 1");
709 
710  res = executeQuery(conn, buf->data);
711 
712  i_rolname = PQfnumber(res, "rolname");
713 
714  if (PQntuples(res) > 0)
715  fprintf(OPF, "--\n-- Drop roles\n--\n\n");
716 
717  for (i = 0; i < PQntuples(res); i++)
718  {
719  const char *rolename;
720 
721  rolename = PQgetvalue(res, i, i_rolname);
722 
723  fprintf(OPF, "DROP ROLE %s%s;\n",
724  if_exists ? "IF EXISTS " : "",
725  fmtId(rolename));
726  }
727 
728  PQclear(res);
729  destroyPQExpBuffer(buf);
730 
731  fprintf(OPF, "\n\n");
732 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
static FILE * OPF
Definition: pg_dumpall.c:90
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static char * buf
Definition: pg_test_fsync.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int if_exists
Definition: pg_dumpall.c:72
static int server_version
Definition: pg_dumpall.c:82
int i
static char role_catalog[10]
Definition: pg_dumpall.c:86

◆ dropTablespaces()

static void dropTablespaces ( PGconn conn)
static

Definition at line 1121 of file pg_dumpall.c.

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

Referenced by main().

1122 {
1123  PGresult *res;
1124  int i;
1125 
1126  /*
1127  * Get all tablespaces except built-in ones (which we assume are named
1128  * pg_xxx)
1129  */
1130  res = executeQuery(conn, "SELECT spcname "
1131  "FROM pg_catalog.pg_tablespace "
1132  "WHERE spcname !~ '^pg_' "
1133  "ORDER BY 1");
1134 
1135  if (PQntuples(res) > 0)
1136  fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1137 
1138  for (i = 0; i < PQntuples(res); i++)
1139  {
1140  char *spcname = PQgetvalue(res, i, 0);
1141 
1142  fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1143  if_exists ? "IF EXISTS " : "",
1144  fmtId(spcname));
1145  }
1146 
1147  PQclear(res);
1148 
1149  fprintf(OPF, "\n\n");
1150 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
static FILE * OPF
Definition: pg_dumpall.c:90
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int if_exists
Definition: pg_dumpall.c:72
int i

◆ dumpDatabases()

static void dumpDatabases ( PGconn conn)
static

Definition at line 1458 of file pg_dumpall.c.

References dbname, executeQuery(), exit_nicely, filename, fprintf, i, OPF, output_clean, PG_BINARY_A, pg_log_error, pg_log_info, PQclear(), PQgetvalue(), PQntuples(), runPgDump(), and simple_string_list_member().

Referenced by main().

1459 {
1460  PGresult *res;
1461  int i;
1462 
1463  /*
1464  * Skip databases marked not datallowconn, since we'd be unable to connect
1465  * to them anyway. This must agree with dropDBs().
1466  *
1467  * We arrange for template1 to be processed first, then we process other
1468  * DBs in alphabetical order. If we just did them all alphabetically, we
1469  * might find ourselves trying to drop the "postgres" database while still
1470  * connected to it. This makes trying to run the restore script while
1471  * connected to "template1" a bad idea, but there's no fixed order that
1472  * doesn't have some failure mode with --clean.
1473  */
1474  res = executeQuery(conn,
1475  "SELECT datname "
1476  "FROM pg_database d "
1477  "WHERE datallowconn "
1478  "ORDER BY (datname <> 'template1'), datname");
1479 
1480  if (PQntuples(res) > 0)
1481  fprintf(OPF, "--\n-- Databases\n--\n\n");
1482 
1483  for (i = 0; i < PQntuples(res); i++)
1484  {
1485  char *dbname = PQgetvalue(res, i, 0);
1486  const char *create_opts;
1487  int ret;
1488 
1489  /* Skip template0, even if it's not marked !datallowconn. */
1490  if (strcmp(dbname, "template0") == 0)
1491  continue;
1492 
1493  /* Skip any explicitly excluded database */
1495  {
1496  pg_log_info("excluding database \"%s\"", dbname);
1497  continue;
1498  }
1499 
1500  pg_log_info("dumping database \"%s\"", dbname);
1501 
1502  fprintf(OPF, "--\n-- Database \"%s\" dump\n--\n\n", dbname);
1503 
1504  /*
1505  * We assume that "template1" and "postgres" already exist in the
1506  * target installation. dropDBs() won't have removed them, for fear
1507  * of removing the DB the restore script is initially connected to. If
1508  * --clean was specified, tell pg_dump to drop and recreate them;
1509  * otherwise we'll merely restore their contents. Other databases
1510  * should simply be created.
1511  */
1512  if (strcmp(dbname, "template1") == 0 || strcmp(dbname, "postgres") == 0)
1513  {
1514  if (output_clean)
1515  create_opts = "--clean --create";
1516  else
1517  {
1518  create_opts = "";
1519  /* Since pg_dump won't emit a \connect command, we must */
1520  fprintf(OPF, "\\connect %s\n\n", dbname);
1521  }
1522  }
1523  else
1524  create_opts = "--create";
1525 
1526  if (filename)
1527  fclose(OPF);
1528 
1529  ret = runPgDump(dbname, create_opts);
1530  if (ret != 0)
1531  {
1532  pg_log_error("pg_dump failed on database \"%s\", exiting", dbname);
1533  exit_nicely(1);
1534  }
1535 
1536  if (filename)
1537  {
1538  OPF = fopen(filename, PG_BINARY_A);
1539  if (!OPF)
1540  {
1541  pg_log_error("could not re-open the output file \"%s\": %m",
1542  filename);
1543  exit_nicely(1);
1544  }
1545  }
1546 
1547  }
1548 
1549  PQclear(res);
1550 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
bool simple_string_list_member(SimpleStringList *list, const char *val)
Definition: simple_list.c:87
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
#define pg_log_error(...)
Definition: logging.h:80
static FILE * OPF
Definition: pg_dumpall.c:90
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
static bool output_clean
Definition: pg_dumpall.c:63
#define exit_nicely(code)
Definition: pg_dumpall.c:96
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define PG_BINARY_A
Definition: c.h:1212
char * dbname
Definition: streamutil.c:51
static char * filename
Definition: pg_dumpall.c:91
int i
static int runPgDump(const char *dbname, const char *create_opts)
Definition: pg_dumpall.c:1558
#define pg_log_info(...)
Definition: logging.h:88
static SimpleStringList database_exclude_names
Definition: pg_dumpall.c:94

◆ dumpGroups()

static void dumpGroups ( PGconn conn)
static

Definition at line 1056 of file pg_dumpall.c.

References buf, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), fmtId(), fprintf, free, i, OPF, pg_strdup(), PQclear(), PQgetvalue(), PQntuples(), and printfPQExpBuffer().

Referenced by main().

1057 {
1059  PGresult *res;
1060  int i;
1061 
1062  res = executeQuery(conn,
1063  "SELECT groname, grolist FROM pg_group ORDER BY 1");
1064 
1065  if (PQntuples(res) > 0)
1066  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1067 
1068  for (i = 0; i < PQntuples(res); i++)
1069  {
1070  char *groname = PQgetvalue(res, i, 0);
1071  char *grolist = PQgetvalue(res, i, 1);
1072  PGresult *res2;
1073  int j;
1074 
1075  /*
1076  * Array representation is {1,2,3} ... convert to (1,2,3)
1077  */
1078  if (strlen(grolist) < 3)
1079  continue;
1080 
1081  grolist = pg_strdup(grolist);
1082  grolist[0] = '(';
1083  grolist[strlen(grolist) - 1] = ')';
1084  printfPQExpBuffer(buf,
1085  "SELECT usename FROM pg_shadow "
1086  "WHERE usesysid IN %s ORDER BY 1",
1087  grolist);
1088  free(grolist);
1089 
1090  res2 = executeQuery(conn, buf->data);
1091 
1092  for (j = 0; j < PQntuples(res2); j++)
1093  {
1094  char *usename = PQgetvalue(res2, j, 0);
1095 
1096  /*
1097  * Don't try to grant a role to itself; can happen if old
1098  * installation has identically named user and group.
1099  */
1100  if (strcmp(groname, usename) == 0)
1101  continue;
1102 
1103  fprintf(OPF, "GRANT %s", fmtId(groname));
1104  fprintf(OPF, " TO %s;\n", fmtId(usename));
1105  }
1106 
1107  PQclear(res2);
1108  }
1109 
1110  PQclear(res);
1111  destroyPQExpBuffer(buf);
1112 
1113  fprintf(OPF, "\n\n");
1114 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
static FILE * OPF
Definition: pg_dumpall.c:90
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static char * buf
Definition: pg_test_fsync.c:68
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
int i

◆ dumpRoleMembership()

static void dumpRoleMembership ( PGconn conn)
static

Definition at line 996 of file pg_dumpall.c.

References buf, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), fmtId(), fprintf, grantor, i, member, OPF, PQclear(), PQgetisnull(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), and role_catalog.

Referenced by main().

997 {
999  PGresult *res;
1000  int i;
1001 
1002  printfPQExpBuffer(buf, "SELECT ur.rolname AS roleid, "
1003  "um.rolname AS member, "
1004  "a.admin_option, "
1005  "ug.rolname AS grantor "
1006  "FROM pg_auth_members a "
1007  "LEFT JOIN %s ur on ur.oid = a.roleid "
1008  "LEFT JOIN %s um on um.oid = a.member "
1009  "LEFT JOIN %s ug on ug.oid = a.grantor "
1010  "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
1011  "ORDER BY 1,2,3", role_catalog, role_catalog, role_catalog);
1012  res = executeQuery(conn, buf->data);
1013 
1014  if (PQntuples(res) > 0)
1015  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1016 
1017  for (i = 0; i < PQntuples(res); i++)
1018  {
1019  char *roleid = PQgetvalue(res, i, 0);
1020  char *member = PQgetvalue(res, i, 1);
1021  char *option = PQgetvalue(res, i, 2);
1022 
1023  fprintf(OPF, "GRANT %s", fmtId(roleid));
1024  fprintf(OPF, " TO %s", fmtId(member));
1025  if (*option == 't')
1026  fprintf(OPF, " WITH ADMIN OPTION");
1027 
1028  /*
1029  * We don't track the grantor very carefully in the backend, so cope
1030  * with the possibility that it has been dropped.
1031  */
1032  if (!PQgetisnull(res, i, 3))
1033  {
1034  char *grantor = PQgetvalue(res, i, 3);
1035 
1036  fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
1037  }
1038  fprintf(OPF, ";\n");
1039  }
1040 
1041  PQclear(res);
1042  destroyPQExpBuffer(buf);
1043 
1044  fprintf(OPF, "\n\n");
1045 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
static FILE * OPF
Definition: pg_dumpall.c:90
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
Oid grantor
Oid member
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static char * buf
Definition: pg_test_fsync.c:68
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694
int i
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
static char role_catalog[10]
Definition: pg_dumpall.c:86

◆ dumpRoles()

static void dumpRoles ( PGconn conn)
static

Definition at line 738 of file pg_dumpall.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), atooid, binary_upgrade, buf, buildShSecLabels(), createPQExpBuffer(), PQExpBufferData::data, 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().

739 {
741  PGresult *res;
742  int i_oid,
743  i_rolname,
744  i_rolsuper,
745  i_rolinherit,
746  i_rolcreaterole,
747  i_rolcreatedb,
748  i_rolcanlogin,
749  i_rolconnlimit,
750  i_rolpassword,
751  i_rolvaliduntil,
752  i_rolreplication,
753  i_rolbypassrls,
754  i_rolcomment,
755  i_is_current_user;
756  int i;
757 
758  /* note: rolconfig is dumped later */
759  if (server_version >= 90600)
760  printfPQExpBuffer(buf,
761  "SELECT oid, rolname, rolsuper, rolinherit, "
762  "rolcreaterole, rolcreatedb, "
763  "rolcanlogin, rolconnlimit, rolpassword, "
764  "rolvaliduntil, rolreplication, rolbypassrls, "
765  "pg_catalog.shobj_description(oid, '%s') as rolcomment, "
766  "rolname = current_user AS is_current_user "
767  "FROM %s "
768  "WHERE rolname !~ '^pg_' "
769  "ORDER BY 2", role_catalog, role_catalog);
770  else if (server_version >= 90500)
771  printfPQExpBuffer(buf,
772  "SELECT oid, rolname, rolsuper, rolinherit, "
773  "rolcreaterole, rolcreatedb, "
774  "rolcanlogin, rolconnlimit, rolpassword, "
775  "rolvaliduntil, rolreplication, rolbypassrls, "
776  "pg_catalog.shobj_description(oid, '%s') as rolcomment, "
777  "rolname = current_user AS is_current_user "
778  "FROM %s "
779  "ORDER BY 2", role_catalog, role_catalog);
780  else if (server_version >= 90100)
781  printfPQExpBuffer(buf,
782  "SELECT oid, rolname, rolsuper, rolinherit, "
783  "rolcreaterole, rolcreatedb, "
784  "rolcanlogin, rolconnlimit, rolpassword, "
785  "rolvaliduntil, rolreplication, "
786  "false as rolbypassrls, "
787  "pg_catalog.shobj_description(oid, '%s') as rolcomment, "
788  "rolname = current_user AS is_current_user "
789  "FROM %s "
790  "ORDER BY 2", role_catalog, role_catalog);
791  else if (server_version >= 80200)
792  printfPQExpBuffer(buf,
793  "SELECT oid, rolname, rolsuper, rolinherit, "
794  "rolcreaterole, rolcreatedb, "
795  "rolcanlogin, rolconnlimit, rolpassword, "
796  "rolvaliduntil, false as rolreplication, "
797  "false as rolbypassrls, "
798  "pg_catalog.shobj_description(oid, '%s') as rolcomment, "
799  "rolname = current_user AS is_current_user "
800  "FROM %s "
801  "ORDER BY 2", role_catalog, role_catalog);
802  else if (server_version >= 80100)
803  printfPQExpBuffer(buf,
804  "SELECT oid, rolname, rolsuper, rolinherit, "
805  "rolcreaterole, rolcreatedb, "
806  "rolcanlogin, rolconnlimit, rolpassword, "
807  "rolvaliduntil, false as rolreplication, "
808  "false as rolbypassrls, "
809  "null as rolcomment, "
810  "rolname = current_user AS is_current_user "
811  "FROM %s "
812  "ORDER BY 2", role_catalog);
813  else
814  printfPQExpBuffer(buf,
815  "SELECT 0 as oid, usename as rolname, "
816  "usesuper as rolsuper, "
817  "true as rolinherit, "
818  "usesuper as rolcreaterole, "
819  "usecreatedb as rolcreatedb, "
820  "true as rolcanlogin, "
821  "-1 as rolconnlimit, "
822  "passwd as rolpassword, "
823  "valuntil as rolvaliduntil, "
824  "false as rolreplication, "
825  "false as rolbypassrls, "
826  "null as rolcomment, "
827  "usename = current_user AS is_current_user "
828  "FROM pg_shadow "
829  "UNION ALL "
830  "SELECT 0 as oid, groname as rolname, "
831  "false as rolsuper, "
832  "true as rolinherit, "
833  "false as rolcreaterole, "
834  "false as rolcreatedb, "
835  "false as rolcanlogin, "
836  "-1 as rolconnlimit, "
837  "null::text as rolpassword, "
838  "null::timestamptz as rolvaliduntil, "
839  "false as rolreplication, "
840  "false as rolbypassrls, "
841  "null as rolcomment, "
842  "false AS is_current_user "
843  "FROM pg_group "
844  "WHERE NOT EXISTS (SELECT 1 FROM pg_shadow "
845  " WHERE usename = groname) "
846  "ORDER BY 2");
847 
848  res = executeQuery(conn, buf->data);
849 
850  i_oid = PQfnumber(res, "oid");
851  i_rolname = PQfnumber(res, "rolname");
852  i_rolsuper = PQfnumber(res, "rolsuper");
853  i_rolinherit = PQfnumber(res, "rolinherit");
854  i_rolcreaterole = PQfnumber(res, "rolcreaterole");
855  i_rolcreatedb = PQfnumber(res, "rolcreatedb");
856  i_rolcanlogin = PQfnumber(res, "rolcanlogin");
857  i_rolconnlimit = PQfnumber(res, "rolconnlimit");
858  i_rolpassword = PQfnumber(res, "rolpassword");
859  i_rolvaliduntil = PQfnumber(res, "rolvaliduntil");
860  i_rolreplication = PQfnumber(res, "rolreplication");
861  i_rolbypassrls = PQfnumber(res, "rolbypassrls");
862  i_rolcomment = PQfnumber(res, "rolcomment");
863  i_is_current_user = PQfnumber(res, "is_current_user");
864 
865  if (PQntuples(res) > 0)
866  fprintf(OPF, "--\n-- Roles\n--\n\n");
867 
868  for (i = 0; i < PQntuples(res); i++)
869  {
870  const char *rolename;
871  Oid auth_oid;
872 
873  auth_oid = atooid(PQgetvalue(res, i, i_oid));
874  rolename = PQgetvalue(res, i, i_rolname);
875 
876  if (strncmp(rolename, "pg_", 3) == 0)
877  {
878  pg_log_warning("role name starting with \"pg_\" skipped (%s)",
879  rolename);
880  continue;
881  }
882 
883  resetPQExpBuffer(buf);
884 
885  if (binary_upgrade)
886  {
887  appendPQExpBufferStr(buf, "\n-- For binary upgrade, must preserve pg_authid.oid\n");
888  appendPQExpBuffer(buf,
889  "SELECT pg_catalog.binary_upgrade_set_next_pg_authid_oid('%u'::pg_catalog.oid);\n\n",
890  auth_oid);
891  }
892 
893  /*
894  * We dump CREATE ROLE followed by ALTER ROLE to ensure that the role
895  * will acquire the right properties even if it already exists (ie, it
896  * won't hurt for the CREATE to fail). This is particularly important
897  * for the role we are connected as, since even with --clean we will
898  * have failed to drop it. binary_upgrade cannot generate any errors,
899  * so we assume the current role is already created.
900  */
901  if (!binary_upgrade ||
902  strcmp(PQgetvalue(res, i, i_is_current_user), "f") == 0)
903  appendPQExpBuffer(buf, "CREATE ROLE %s;\n", fmtId(rolename));
904  appendPQExpBuffer(buf, "ALTER ROLE %s WITH", fmtId(rolename));
905 
906  if (strcmp(PQgetvalue(res, i, i_rolsuper), "t") == 0)
907  appendPQExpBufferStr(buf, " SUPERUSER");
908  else
909  appendPQExpBufferStr(buf, " NOSUPERUSER");
910 
911  if (strcmp(PQgetvalue(res, i, i_rolinherit), "t") == 0)
912  appendPQExpBufferStr(buf, " INHERIT");
913  else
914  appendPQExpBufferStr(buf, " NOINHERIT");
915 
916  if (strcmp(PQgetvalue(res, i, i_rolcreaterole), "t") == 0)
917  appendPQExpBufferStr(buf, " CREATEROLE");
918  else
919  appendPQExpBufferStr(buf, " NOCREATEROLE");
920 
921  if (strcmp(PQgetvalue(res, i, i_rolcreatedb), "t") == 0)
922  appendPQExpBufferStr(buf, " CREATEDB");
923  else
924  appendPQExpBufferStr(buf, " NOCREATEDB");
925 
926  if (strcmp(PQgetvalue(res, i, i_rolcanlogin), "t") == 0)
927  appendPQExpBufferStr(buf, " LOGIN");
928  else
929  appendPQExpBufferStr(buf, " NOLOGIN");
930 
931  if (strcmp(PQgetvalue(res, i, i_rolreplication), "t") == 0)
932  appendPQExpBufferStr(buf, " REPLICATION");
933  else
934  appendPQExpBufferStr(buf, " NOREPLICATION");
935 
936  if (strcmp(PQgetvalue(res, i, i_rolbypassrls), "t") == 0)
937  appendPQExpBufferStr(buf, " BYPASSRLS");
938  else
939  appendPQExpBufferStr(buf, " NOBYPASSRLS");
940 
941  if (strcmp(PQgetvalue(res, i, i_rolconnlimit), "-1") != 0)
942  appendPQExpBuffer(buf, " CONNECTION LIMIT %s",
943  PQgetvalue(res, i, i_rolconnlimit));
944 
945 
946  if (!PQgetisnull(res, i, i_rolpassword) && !no_role_passwords)
947  {
948  appendPQExpBufferStr(buf, " PASSWORD ");
949  appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolpassword), conn);
950  }
951 
952  if (!PQgetisnull(res, i, i_rolvaliduntil))
953  appendPQExpBuffer(buf, " VALID UNTIL '%s'",
954  PQgetvalue(res, i, i_rolvaliduntil));
955 
956  appendPQExpBufferStr(buf, ";\n");
957 
958  if (!no_comments && !PQgetisnull(res, i, i_rolcomment))
959  {
960  appendPQExpBuffer(buf, "COMMENT ON ROLE %s IS ", fmtId(rolename));
961  appendStringLiteralConn(buf, PQgetvalue(res, i, i_rolcomment), conn);
962  appendPQExpBufferStr(buf, ";\n");
963  }
964 
965  if (!no_security_labels && server_version >= 90200)
966  buildShSecLabels(conn, "pg_authid", auth_oid,
967  "ROLE", rolename,
968  buf);
969 
970  fprintf(OPF, "%s", buf->data);
971  }
972 
973  /*
974  * Dump configuration settings for roles after all roles have been dumped.
975  * We do it this way because config settings for roles could mention the
976  * names of other roles.
977  */
978  for (i = 0; i < PQntuples(res); i++)
979  dumpUserConfig(conn, PQgetvalue(res, i, i_rolname));
980 
981  PQclear(res);
982 
983  fprintf(OPF, "\n\n");
984 
985  destroyPQExpBuffer(buf);
986 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
static FILE * OPF
Definition: pg_dumpall.c:90
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:68
static void dumpUserConfig(PGconn *conn, const char *username)
Definition: pg_dumpall.c:1359
#define atooid(x)
Definition: postgres_ext.h:42
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int no_comments
Definition: pg_dumpall.c:76
static int server_version
Definition: pg_dumpall.c:82
static int binary_upgrade
Definition: pg_dumpall.c:68
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
static void buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
Definition: pg_dumpall.c:1610
static int no_role_passwords
Definition: pg_dumpall.c:81
int i
static int no_security_labels
Definition: pg_dumpall.c:78
#define pg_log_warning(...)
Definition: pgfnames.c:24
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
static char role_catalog[10]
Definition: pg_dumpall.c:86

◆ dumpTablespaces()

static void dumpTablespaces ( PGconn conn)
static

Definition at line 1156 of file pg_dumpall.c.

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

Referenced by main().

1157 {
1158  PGresult *res;
1159  int i;
1160 
1161  /*
1162  * Get all tablespaces except built-in ones (which we assume are named
1163  * pg_xxx)
1164  *
1165  * For the tablespace ACLs, as of 9.6, we extract both the positive (as
1166  * spcacl) and negative (as rspcacl) ACLs, relative to the default ACL for
1167  * tablespaces, which are then passed to buildACLCommands() below.
1168  *
1169  * See buildACLQueries() and buildACLCommands().
1170  *
1171  * The order in which privileges are in the ACL string (the order they
1172  * have been GRANT'd in, which the backend maintains) must be preserved to
1173  * ensure that GRANTs WITH GRANT OPTION and subsequent GRANTs based on
1174  * those are dumped in the correct order.
1175  *
1176  * Note that we do not support initial privileges (pg_init_privs) on
1177  * tablespaces, so this logic cannot make use of buildACLQueries().
1178  */
1179  if (server_version >= 90600)
1180  res = executeQuery(conn, "SELECT oid, spcname, "
1181  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1182  "pg_catalog.pg_tablespace_location(oid), "
1183  "(SELECT array_agg(acl ORDER BY row_n) FROM "
1184  " (SELECT acl, row_n FROM "
1185  " unnest(coalesce(spcacl,acldefault('t',spcowner))) "
1186  " WITH ORDINALITY AS perm(acl,row_n) "
1187  " WHERE NOT EXISTS ( "
1188  " SELECT 1 "
1189  " FROM unnest(acldefault('t',spcowner)) "
1190  " AS init(init_acl) "
1191  " WHERE acl = init_acl)) AS spcacls) "
1192  " AS spcacl, "
1193  "(SELECT array_agg(acl ORDER BY row_n) FROM "
1194  " (SELECT acl, row_n FROM "
1195  " unnest(acldefault('t',spcowner)) "
1196  " WITH ORDINALITY AS initp(acl,row_n) "
1197  " WHERE NOT EXISTS ( "
1198  " SELECT 1 "
1199  " FROM unnest(coalesce(spcacl,acldefault('t',spcowner))) "
1200  " AS permp(orig_acl) "
1201  " WHERE acl = orig_acl)) AS rspcacls) "
1202  " AS rspcacl, "
1203  "array_to_string(spcoptions, ', '),"
1204  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1205  "FROM pg_catalog.pg_tablespace "
1206  "WHERE spcname !~ '^pg_' "
1207  "ORDER BY 1");
1208  else if (server_version >= 90200)
1209  res = executeQuery(conn, "SELECT oid, spcname, "
1210  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1211  "pg_catalog.pg_tablespace_location(oid), "
1212  "spcacl, '' as rspcacl, "
1213  "array_to_string(spcoptions, ', '),"
1214  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1215  "FROM pg_catalog.pg_tablespace "
1216  "WHERE spcname !~ '^pg_' "
1217  "ORDER BY 1");
1218  else if (server_version >= 90000)
1219  res = executeQuery(conn, "SELECT oid, spcname, "
1220  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1221  "spclocation, spcacl, '' as rspcacl, "
1222  "array_to_string(spcoptions, ', '),"
1223  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1224  "FROM pg_catalog.pg_tablespace "
1225  "WHERE spcname !~ '^pg_' "
1226  "ORDER BY 1");
1227  else if (server_version >= 80200)
1228  res = executeQuery(conn, "SELECT oid, spcname, "
1229  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1230  "spclocation, spcacl, '' as rspcacl, null, "
1231  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1232  "FROM pg_catalog.pg_tablespace "
1233  "WHERE spcname !~ '^pg_' "
1234  "ORDER BY 1");
1235  else
1236  res = executeQuery(conn, "SELECT oid, spcname, "
1237  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1238  "spclocation, spcacl, '' as rspcacl, "
1239  "null, null "
1240  "FROM pg_catalog.pg_tablespace "
1241  "WHERE spcname !~ '^pg_' "
1242  "ORDER BY 1");
1243 
1244  if (PQntuples(res) > 0)
1245  fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1246 
1247  for (i = 0; i < PQntuples(res); i++)
1248  {
1250  Oid spcoid = atooid(PQgetvalue(res, i, 0));
1251  char *spcname = PQgetvalue(res, i, 1);
1252  char *spcowner = PQgetvalue(res, i, 2);
1253  char *spclocation = PQgetvalue(res, i, 3);
1254  char *spcacl = PQgetvalue(res, i, 4);
1255  char *rspcacl = PQgetvalue(res, i, 5);
1256  char *spcoptions = PQgetvalue(res, i, 6);
1257  char *spccomment = PQgetvalue(res, i, 7);
1258  char *fspcname;
1259 
1260  /* needed for buildACLCommands() */
1261  fspcname = pg_strdup(fmtId(spcname));
1262 
1263  appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
1264  appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
1265 
1266  appendPQExpBufferStr(buf, " LOCATION ");
1267  appendStringLiteralConn(buf, spclocation, conn);
1268  appendPQExpBufferStr(buf, ";\n");
1269 
1270  if (spcoptions && spcoptions[0] != '\0')
1271  appendPQExpBuffer(buf, "ALTER TABLESPACE %s SET (%s);\n",
1272  fspcname, spcoptions);
1273 
1274  if (!skip_acls &&
1275  !buildACLCommands(fspcname, NULL, NULL, "TABLESPACE",
1276  spcacl, rspcacl,
1277  spcowner, "", server_version, buf))
1278  {
1279  pg_log_error("could not parse ACL list (%s) for tablespace \"%s\"",
1280  spcacl, spcname);
1281  PQfinish(conn);
1282  exit_nicely(1);
1283  }
1284 
1285  if (!no_comments && spccomment && spccomment[0] != '\0')
1286  {
1287  appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
1288  appendStringLiteralConn(buf, spccomment, conn);
1289  appendPQExpBufferStr(buf, ";\n");
1290  }
1291 
1292  if (!no_security_labels && server_version >= 90200)
1293  buildShSecLabels(conn, "pg_tablespace", spcoid,
1294  "TABLESPACE", spcname,
1295  buf);
1296 
1297  fprintf(OPF, "%s", buf->data);
1298 
1299  free(fspcname);
1300  destroyPQExpBuffer(buf);
1301  }
1302 
1303  PQclear(res);
1304  fprintf(OPF, "\n\n");
1305 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
#define pg_log_error(...)
Definition: logging.h:80
static FILE * OPF
Definition: pg_dumpall.c:90
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4171
unsigned int Oid
Definition: postgres_ext.h:31
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static char * buf
Definition: pg_test_fsync.c:68
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define atooid(x)
Definition: postgres_ext.h:42
#define exit_nicely(code)
Definition: pg_dumpall.c:96
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define free(a)
Definition: header.h:65
static int no_comments
Definition: pg_dumpall.c:76
static int server_version
Definition: pg_dumpall.c:82
bool buildACLCommands(const char *name, const char *subname, const char *nspname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:58
static bool skip_acls
Definition: pg_dumpall.c:64
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
static void buildShSecLabels(PGconn *conn, const char *catalog_name, Oid objectId, const char *objtype, const char *objname, PQExpBuffer buffer)
Definition: pg_dumpall.c:1610
int i
static int no_security_labels
Definition: pg_dumpall.c:78

◆ dumpTimestamp()

static void dumpTimestamp ( const char *  msg)
static

Definition at line 1922 of file pg_dumpall.c.

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

Referenced by main().

1923 {
1924  char buf[64];
1925  time_t now = time(NULL);
1926 
1927  if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
1928  fprintf(OPF, "-- %s %s\n\n", msg, buf);
1929 }
static FILE * OPF
Definition: pg_dumpall.c:90
#define fprintf
Definition: port.h:197
static char * buf
Definition: pg_test_fsync.c:68
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1542

◆ dumpUserConfig()

static void dumpUserConfig ( PGconn conn,
const char *  username 
)
static

Definition at line 1359 of file pg_dumpall.c.

References appendPQExpBufferChar(), appendStringLiteralConn(), buf, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), fprintf, makeAlterConfigCommand(), OPF, PQclear(), PQgetisnull(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), resetPQExpBuffer(), role_catalog, and server_version.

Referenced by dumpRoles().

1360 {
1362  int count = 1;
1363  bool first = true;
1364 
1365  for (;;)
1366  {
1367  PGresult *res;
1368 
1369  if (server_version >= 90000)
1370  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1371  "setdatabase = 0 AND setrole = "
1372  "(SELECT oid FROM %s WHERE rolname = ", count, role_catalog);
1373  else if (server_version >= 80100)
1374  printfPQExpBuffer(buf, "SELECT rolconfig[%d] FROM %s WHERE rolname = ", count, role_catalog);
1375  else
1376  printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
1377  appendStringLiteralConn(buf, username, conn);
1378  if (server_version >= 90000)
1379  appendPQExpBufferChar(buf, ')');
1380 
1381  res = executeQuery(conn, buf->data);
1382  if (PQntuples(res) == 1 &&
1383  !PQgetisnull(res, 0, 0))
1384  {
1385  /* comment at section start, only if needed */
1386  if (first)
1387  {
1388  fprintf(OPF, "--\n-- User Configurations\n--\n\n");
1389  first = false;
1390  }
1391 
1392  fprintf(OPF, "--\n-- User Config \"%s\"\n--\n\n", username);
1393  resetPQExpBuffer(buf);
1394  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1395  "ROLE", username, NULL, NULL,
1396  buf);
1397  fprintf(OPF, "%s", buf->data);
1398  PQclear(res);
1399  count++;
1400  }
1401  else
1402  {
1403  PQclear(res);
1404  break;
1405  }
1406  }
1407 
1408  destroyPQExpBuffer(buf);
1409 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:237
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
static FILE * OPF
Definition: pg_dumpall.c:90
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:197
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static char * buf
Definition: pg_test_fsync.c:68
void makeAlterConfigCommand(PGconn *conn, const char *configitem, const char *type, const char *name, const char *type2, const char *name2, PQExpBuffer buf)
Definition: dumputils.c:999
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:380
static char * username
Definition: initdb.c:134
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int server_version
Definition: pg_dumpall.c:82
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
static char role_catalog[10]
Definition: pg_dumpall.c:86

◆ executeCommand()

static void executeCommand ( PGconn conn,
const char *  query 
)
static

Definition at line 1898 of file pg_dumpall.c.

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

Referenced by main(), and vacuum_one_database().

1899 {
1900  PGresult *res;
1901 
1902  pg_log_info("executing %s", query);
1903 
1904  res = PQexec(conn, query);
1905  if (!res ||
1907  {
1908  pg_log_error("query failed: %s", PQerrorMessage(conn));
1909  pg_log_error("query was: %s", query);
1910  PQfinish(conn);
1911  exit_nicely(1);
1912  }
1913 
1914  PQclear(res);
1915 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6666
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4171
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define exit_nicely(code)
Definition: pg_dumpall.c:96
void PQclear(PGresult *res)
Definition: fe-exec.c:694
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
#define pg_log_info(...)
Definition: logging.h:88

◆ executeQuery()

static PGresult * executeQuery ( PGconn conn,
const char *  query 
)
static

Definition at line 1875 of file pg_dumpall.c.

References exit_nicely, pg_log_error, pg_log_info, PGRES_TUPLES_OK, PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus().

Referenced by buildShSecLabels(), cluster_all_databases(), connectDatabase(), dropDBs(), dropRoles(), dropTablespaces(), dumpDatabases(), dumpGroups(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpUserConfig(), expand_dbname_patterns(), get_parallel_object_list(), reindex_all_databases(), vacuum_all_databases(), and vacuum_one_database().

1876 {
1877  PGresult *res;
1878 
1879  pg_log_info("executing %s", query);
1880 
1881  res = PQexec(conn, query);
1882  if (!res ||
1884  {
1885  pg_log_error("query failed: %s", PQerrorMessage(conn));
1886  pg_log_error("query was: %s", query);
1887  PQfinish(conn);
1888  exit_nicely(1);
1889  }
1890 
1891  return res;
1892 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6666
#define pg_log_error(...)
Definition: logging.h:80
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4171
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define exit_nicely(code)
Definition: pg_dumpall.c:96
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
#define pg_log_info(...)
Definition: logging.h:88

◆ expand_dbname_patterns()

static void expand_dbname_patterns ( PGconn conn,
SimpleStringList patterns,
SimpleStringList names 
)
static

Definition at line 1416 of file pg_dumpall.c.

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

Referenced by main().

1419 {
1420  PQExpBuffer query;
1421  PGresult *res;
1422 
1423  if (patterns->head == NULL)
1424  return; /* nothing to do */
1425 
1426  query = createPQExpBuffer();
1427 
1428  /*
1429  * The loop below runs multiple SELECTs, which might sometimes result in
1430  * duplicate entries in the name list, but we don't care, since all we're
1431  * going to do is test membership of the list.
1432  */
1433 
1434  for (SimpleStringListCell *cell = patterns->head; cell; cell = cell->next)
1435  {
1436  appendPQExpBufferStr(query,
1437  "SELECT datname FROM pg_catalog.pg_database n\n");
1438  processSQLNamePattern(conn, query, cell->val, false,
1439  false, NULL, "datname", NULL, NULL);
1440 
1441  res = executeQuery(conn, query->data);
1442  for (int i = 0; i < PQntuples(res); i++)
1443  {
1444  simple_string_list_append(names, PQgetvalue(res, i, 0));
1445  }
1446 
1447  PQclear(res);
1448  resetPQExpBuffer(query);
1449  }
1450 
1451  destroyPQExpBuffer(query);
1452 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1875
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
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)
Definition: string_utils.c:827
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
struct SimpleStringListCell * next
Definition: simple_list.h:34
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void PQclear(PGresult *res)
Definition: fe-exec.c:694
SimpleStringListCell * head
Definition: simple_list.h:42
int i
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:148

◆ help()

static void help ( void  )
static

Definition at line 615 of file pg_dumpall.c.

References _, printf, and progname.

Referenced by main().

616 {
617  printf(_("%s extracts a PostgreSQL database cluster into an SQL script file.\n\n"), progname);
618  printf(_("Usage:\n"));
619  printf(_(" %s [OPTION]...\n"), progname);
620 
621  printf(_("\nGeneral options:\n"));
622  printf(_(" -f, --file=FILENAME output file name\n"));
623  printf(_(" -v, --verbose verbose mode\n"));
624  printf(_(" -V, --version output version information, then exit\n"));
625  printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
626  printf(_(" -?, --help show this help, then exit\n"));
627  printf(_("\nOptions controlling the output content:\n"));
628  printf(_(" -a, --data-only dump only the data, not the schema\n"));
629  printf(_(" -c, --clean clean (drop) databases before recreating\n"));
630  printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
631  printf(_(" -g, --globals-only dump only global objects, no databases\n"));
632  printf(_(" -O, --no-owner skip restoration of object ownership\n"));
633  printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
634  printf(_(" -s, --schema-only dump only the schema, no data\n"));
635  printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
636  printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
637  printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
638  printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
639  printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
640  printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
641  printf(_(" --disable-triggers disable triggers during data-only restore\n"));
642  printf(_(" --exclude-database=PATTERN exclude databases whose name matches PATTERN\n"));
643  printf(_(" --extra-float-digits=NUM override default setting for extra_float_digits\n"));
644  printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
645  printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
646  printf(_(" --load-via-partition-root load partitions via the root table\n"));
647  printf(_(" --no-comments do not dump comments\n"));
648  printf(_(" --no-publications do not dump publications\n"));
649  printf(_(" --no-role-passwords do not dump passwords for roles\n"));
650  printf(_(" --no-security-labels do not dump security label assignments\n"));
651  printf(_(" --no-subscriptions do not dump subscriptions\n"));
652  printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
653  printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
654  printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
655  printf(_(" --on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands\n"));
656  printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
657  printf(_(" --rows-per-insert=NROWS number of rows per INSERT; implies --inserts\n"));
658  printf(_(" --use-set-session-authorization\n"
659  " use SET SESSION AUTHORIZATION commands instead of\n"
660  " ALTER OWNER commands to set ownership\n"));
661 
662  printf(_("\nConnection options:\n"));
663  printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
664  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
665  printf(_(" -l, --database=DBNAME alternative default database\n"));
666  printf(_(" -p, --port=PORT database server port number\n"));
667  printf(_(" -U, --username=NAME connect as specified database user\n"));
668  printf(_(" -w, --no-password never prompt for password\n"));
669  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
670  printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
671 
672  printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
673  "output.\n\n"));
674  printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
675  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
676 }
static const char * progname
Definition: pg_dumpall.c:60
#define printf(...)
Definition: port.h:199
#define _(x)
Definition: elog.c:88

◆ main()

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

Definition at line 99 of file pg_dumpall.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), binary_upgrade, column_inserts, conn, connectDatabase(), connstr, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), disable_dollar_quoting, disable_triggers, dosync, dropDBs(), dropRoles(), dropTablespaces(), dumpDatabases(), dumpGroups(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpTimestamp(), encoding, executeCommand(), exit_nicely, expand_dbname_patterns(), filename, find_my_exec(), find_other_exec(), fmtId(), fprintf, fsync_fname(), get_progname(), getopt_long(), SimpleStringList::head, help(), if_exists, inserts, load_via_partition_root, MAXPGPATH, no_argument, no_comments, no_publications, no_role_passwords, no_security_labels, no_subscriptions, no_tablespaces, 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_log_error, PG_LOG_WARNING, pg_logging_increase_verbosity(), pg_logging_init(), pg_logging_set_level(), PG_ROLES, pg_strdup(), PG_TEXTDOMAIN, PGDUMP_VERSIONSTR, pghost, pgport, PQclientEncoding(), PQfinish(), PQparameterStatus(), PQsetClientEncoding(), progname, quote_all_identifiers, required_argument, role_catalog, server_version, set_pglocale_pgservice(), simple_string_list_append(), skip_acls, sprintf, generate_unaccent_rules::stdout, strlcpy(), TRI_DEFAULT, TRI_NO, TRI_YES, use_setsessauth, and verbose.

100 {
101  static struct option long_options[] = {
102  {"data-only", no_argument, NULL, 'a'},
103  {"clean", no_argument, NULL, 'c'},
104  {"encoding", required_argument, NULL, 'E'},
105  {"file", required_argument, NULL, 'f'},
106  {"globals-only", no_argument, NULL, 'g'},
107  {"host", required_argument, NULL, 'h'},
108  {"dbname", required_argument, NULL, 'd'},
109  {"database", required_argument, NULL, 'l'},
110  {"no-owner", no_argument, NULL, 'O'},
111  {"port", required_argument, NULL, 'p'},
112  {"roles-only", no_argument, NULL, 'r'},
113  {"schema-only", no_argument, NULL, 's'},
114  {"superuser", required_argument, NULL, 'S'},
115  {"tablespaces-only", no_argument, NULL, 't'},
116  {"username", required_argument, NULL, 'U'},
117  {"verbose", no_argument, NULL, 'v'},
118  {"no-password", no_argument, NULL, 'w'},
119  {"password", no_argument, NULL, 'W'},
120  {"no-privileges", no_argument, NULL, 'x'},
121  {"no-acl", no_argument, NULL, 'x'},
122 
123  /*
124  * the following options don't have an equivalent short option letter
125  */
126  {"attribute-inserts", no_argument, &column_inserts, 1},
127  {"binary-upgrade", no_argument, &binary_upgrade, 1},
128  {"column-inserts", no_argument, &column_inserts, 1},
129  {"disable-dollar-quoting", no_argument, &disable_dollar_quoting, 1},
130  {"disable-triggers", no_argument, &disable_triggers, 1},
131  {"exclude-database", required_argument, NULL, 6},
132  {"extra-float-digits", required_argument, NULL, 5},
133  {"if-exists", no_argument, &if_exists, 1},
134  {"inserts", no_argument, &inserts, 1},
135  {"lock-wait-timeout", required_argument, NULL, 2},
136  {"no-tablespaces", no_argument, &no_tablespaces, 1},
137  {"quote-all-identifiers", no_argument, &quote_all_identifiers, 1},
138  {"load-via-partition-root", no_argument, &load_via_partition_root, 1},
139  {"role", required_argument, NULL, 3},
140  {"use-set-session-authorization", no_argument, &use_setsessauth, 1},
141  {"no-comments", no_argument, &no_comments, 1},
142  {"no-publications", no_argument, &no_publications, 1},
143  {"no-role-passwords", no_argument, &no_role_passwords, 1},
144  {"no-security-labels", no_argument, &no_security_labels, 1},
145  {"no-subscriptions", no_argument, &no_subscriptions, 1},
146  {"no-sync", no_argument, NULL, 4},
147  {"no-unlogged-table-data", no_argument, &no_unlogged_table_data, 1},
148  {"on-conflict-do-nothing", no_argument, &on_conflict_do_nothing, 1},
149  {"rows-per-insert", required_argument, NULL, 7},
150 
151  {NULL, 0, NULL, 0}
152  };
153 
154  char *pghost = NULL;
155  char *pgport = NULL;
156  char *pguser = NULL;
157  char *pgdb = NULL;
158  char *use_role = NULL;
159  const char *dumpencoding = NULL;
160  trivalue prompt_password = TRI_DEFAULT;
161  bool data_only = false;
162  bool globals_only = false;
163  bool roles_only = false;
164  bool tablespaces_only = false;
165  PGconn *conn;
166  int encoding;
167  const char *std_strings;
168  int c,
169  ret;
170  int optindex;
171 
172  pg_logging_init(argv[0]);
174  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_dump"));
175  progname = get_progname(argv[0]);
176 
177  if (argc > 1)
178  {
179  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
180  {
181  help();
182  exit_nicely(0);
183  }
184  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
185  {
186  puts("pg_dumpall (PostgreSQL) " PG_VERSION);
187  exit_nicely(0);
188  }
189  }
190 
191  if ((ret = find_other_exec(argv[0], "pg_dump", PGDUMP_VERSIONSTR,
192  pg_dump_bin)) < 0)
193  {
194  char full_path[MAXPGPATH];
195 
196  if (find_my_exec(argv[0], full_path) < 0)
197  strlcpy(full_path, progname, sizeof(full_path));
198 
199  if (ret == -1)
200  pg_log_error("The program \"%s\" is needed by %s but was not found in the\n"
201  "same directory as \"%s\".\n"
202  "Check your installation.",
203  "pg_dump", progname, full_path);
204  else
205  pg_log_error("The program \"%s\" was found by \"%s\"\n"
206  "but was not the same version as %s.\n"
207  "Check your installation.",
208  "pg_dump", full_path, progname);
209  exit_nicely(1);
210  }
211 
213 
214  while ((c = getopt_long(argc, argv, "acd:E:f:gh:l:Op:rsS:tU:vwWx", long_options, &optindex)) != -1)
215  {
216  switch (c)
217  {
218  case 'a':
219  data_only = true;
221  break;
222 
223  case 'c':
224  output_clean = true;
225  break;
226 
227  case 'd':
229  break;
230 
231  case 'E':
232  dumpencoding = pg_strdup(optarg);
235  break;
236 
237  case 'f':
241  break;
242 
243  case 'g':
244  globals_only = true;
245  break;
246 
247  case 'h':
248  pghost = pg_strdup(optarg);
249  break;
250 
251  case 'l':
252  pgdb = pg_strdup(optarg);
253  break;
254 
255  case 'O':
257  break;
258 
259  case 'p':
260  pgport = pg_strdup(optarg);
261  break;
262 
263  case 'r':
264  roles_only = true;
265  break;
266 
267  case 's':
269  break;
270 
271  case 'S':
274  break;
275 
276  case 't':
277  tablespaces_only = true;
278  break;
279 
280  case 'U':
281  pguser = pg_strdup(optarg);
282  break;
283 
284  case 'v':
285  verbose = true;
288  break;
289 
290  case 'w':
291  prompt_password = TRI_NO;
293  break;
294 
295  case 'W':
296  prompt_password = TRI_YES;
298  break;
299 
300  case 'x':
301  skip_acls = true;
303  break;
304 
305  case 0:
306  break;
307 
308  case 2:
309  appendPQExpBufferStr(pgdumpopts, " --lock-wait-timeout ");
311  break;
312 
313  case 3:
314  use_role = pg_strdup(optarg);
315  appendPQExpBufferStr(pgdumpopts, " --role ");
316  appendShellString(pgdumpopts, use_role);
317  break;
318 
319  case 4:
320  dosync = false;
321  appendPQExpBufferStr(pgdumpopts, " --no-sync");
322  break;
323 
324  case 5:
325  appendPQExpBufferStr(pgdumpopts, " --extra-float-digits ");
327  break;
328 
329  case 6:
331  break;
332 
333  case 7:
334  appendPQExpBufferStr(pgdumpopts, " --rows-per-insert ");
336  break;
337 
338  default:
339  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
340  exit_nicely(1);
341  }
342  }
343 
344  /* Complain if any arguments remain */
345  if (optind < argc)
346  {
347  pg_log_error("too many command-line arguments (first is \"%s\")",
348  argv[optind]);
349  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
350  progname);
351  exit_nicely(1);
352  }
353 
354  if (database_exclude_patterns.head != NULL &&
355  (globals_only || roles_only || tablespaces_only))
356  {
357  pg_log_error("option --exclude-database cannot be used together with -g/--globals-only, -r/--roles-only, or -t/--tablespaces-only");
358  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
359  progname);
360  exit_nicely(1);
361  }
362 
363  /* Make sure the user hasn't specified a mix of globals-only options */
364  if (globals_only && roles_only)
365  {
366  pg_log_error("options -g/--globals-only and -r/--roles-only cannot be used together");
367  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
368  progname);
369  exit_nicely(1);
370  }
371 
372  if (globals_only && tablespaces_only)
373  {
374  pg_log_error("options -g/--globals-only and -t/--tablespaces-only cannot be used together");
375  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
376  progname);
377  exit_nicely(1);
378  }
379 
380  if (if_exists && !output_clean)
381  {
382  pg_log_error("option --if-exists requires option -c/--clean");
383  exit_nicely(1);
384  }
385 
386  if (roles_only && tablespaces_only)
387  {
388  pg_log_error("options -r/--roles-only and -t/--tablespaces-only cannot be used together");
389  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
390  progname);
391  exit_nicely(1);
392  }
393 
394  /*
395  * If password values are not required in the dump, switch to using
396  * pg_roles which is equally useful, just more likely to have unrestricted
397  * access than pg_authid.
398  */
399  if (no_role_passwords)
400  sprintf(role_catalog, "%s", PG_ROLES);
401  else
403 
404  /* Add long options to the pg_dump argument list */
405  if (binary_upgrade)
406  appendPQExpBufferStr(pgdumpopts, " --binary-upgrade");
407  if (column_inserts)
408  appendPQExpBufferStr(pgdumpopts, " --column-inserts");
410  appendPQExpBufferStr(pgdumpopts, " --disable-dollar-quoting");
411  if (disable_triggers)
412  appendPQExpBufferStr(pgdumpopts, " --disable-triggers");
413  if (inserts)
414  appendPQExpBufferStr(pgdumpopts, " --inserts");
415  if (no_tablespaces)
416  appendPQExpBufferStr(pgdumpopts, " --no-tablespaces");
418  appendPQExpBufferStr(pgdumpopts, " --quote-all-identifiers");
420  appendPQExpBufferStr(pgdumpopts, " --load-via-partition-root");
421  if (use_setsessauth)
422  appendPQExpBufferStr(pgdumpopts, " --use-set-session-authorization");
423  if (no_comments)
424  appendPQExpBufferStr(pgdumpopts, " --no-comments");
425  if (no_publications)
426  appendPQExpBufferStr(pgdumpopts, " --no-publications");
427  if (no_security_labels)
428  appendPQExpBufferStr(pgdumpopts, " --no-security-labels");
429  if (no_subscriptions)
430  appendPQExpBufferStr(pgdumpopts, " --no-subscriptions");
432  appendPQExpBufferStr(pgdumpopts, " --no-unlogged-table-data");
434  appendPQExpBufferStr(pgdumpopts, " --on-conflict-do-nothing");
435 
436  /*
437  * If there was a database specified on the command line, use that,
438  * otherwise try to connect to database "postgres", and failing that
439  * "template1". "postgres" is the preferred choice for 8.1 and later
440  * servers, but it usually will not exist on older ones.
441  */
442  if (pgdb)
443  {
444  conn = connectDatabase(pgdb, connstr, pghost, pgport, pguser,
445  prompt_password, false);
446 
447  if (!conn)
448  {
449  pg_log_error("could not connect to database \"%s\"", pgdb);
450  exit_nicely(1);
451  }
452  }
453  else
454  {
455  conn = connectDatabase("postgres", connstr, pghost, pgport, pguser,
456  prompt_password, false);
457  if (!conn)
458  conn = connectDatabase("template1", connstr, pghost, pgport, pguser,
459  prompt_password, true);
460 
461  if (!conn)
462  {
463  pg_log_error("could not connect to databases \"postgres\" or \"template1\"\n"
464  "Please specify an alternative database.");
465  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
466  progname);
467  exit_nicely(1);
468  }
469  }
470 
471  /*
472  * Get a list of database names that match the exclude patterns
473  */
476 
477  /*
478  * Open the output file if required, otherwise use stdout
479  */
480  if (filename)
481  {
482  OPF = fopen(filename, PG_BINARY_W);
483  if (!OPF)
484  {
485  pg_log_error("could not open output file \"%s\": %m",
486  filename);
487  exit_nicely(1);
488  }
489  }
490  else
491  OPF = stdout;
492 
493  /*
494  * Set the client encoding if requested.
495  */
496  if (dumpencoding)
497  {
498  if (PQsetClientEncoding(conn, dumpencoding) < 0)
499  {
500  pg_log_error("invalid client encoding \"%s\" specified",
501  dumpencoding);
502  exit_nicely(1);
503  }
504  }
505 
506  /*
507  * Get the active encoding and the standard_conforming_strings setting, so
508  * we know how to escape strings.
509  */
510  encoding = PQclientEncoding(conn);
511  std_strings = PQparameterStatus(conn, "standard_conforming_strings");
512  if (!std_strings)
513  std_strings = "off";
514 
515  /* Set the role if requested */
516  if (use_role && server_version >= 80100)
517  {
518  PQExpBuffer query = createPQExpBuffer();
519 
520  appendPQExpBuffer(query, "SET ROLE %s", fmtId(use_role));
521  executeCommand(conn, query->data);
522  destroyPQExpBuffer(query);
523  }
524 
525  /* Force quoting of all identifiers if requested. */
526  if (quote_all_identifiers && server_version >= 90100)
527  executeCommand(conn, "SET quote_all_identifiers = true");
528 
529  fprintf(OPF, "--\n-- PostgreSQL database cluster dump\n--\n\n");
530  if (verbose)
531  dumpTimestamp("Started on");
532 
533  /*
534  * We used to emit \connect postgres here, but that served no purpose
535  * other than to break things for installations without a postgres
536  * database. Everything we're restoring here is a global, so whichever
537  * database we're connected to at the moment is fine.
538  */
539 
540  /* Restore will need to write to the target cluster */
541  fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
542 
543  /* Replicate encoding and std_strings in output */
544  fprintf(OPF, "SET client_encoding = '%s';\n",
545  pg_encoding_to_char(encoding));
546  fprintf(OPF, "SET standard_conforming_strings = %s;\n", std_strings);
547  if (strcmp(std_strings, "off") == 0)
548  fprintf(OPF, "SET escape_string_warning = off;\n");
549  fprintf(OPF, "\n");
550 
551  if (!data_only)
552  {
553  /*
554  * If asked to --clean, do that first. We can avoid detailed
555  * dependency analysis because databases never depend on each other,
556  * and tablespaces never depend on each other. Roles could have
557  * grants to each other, but DROP ROLE will clean those up silently.
558  */
559  if (output_clean)
560  {
561  if (!globals_only && !roles_only && !tablespaces_only)
562  dropDBs(conn);
563 
564  if (!roles_only && !no_tablespaces)
565  dropTablespaces(conn);
566 
567  if (!tablespaces_only)
568  dropRoles(conn);
569  }
570 
571  /*
572  * Now create objects as requested. Be careful that option logic here
573  * is the same as for drops above.
574  */
575  if (!tablespaces_only)
576  {
577  /* Dump roles (users) */
578  dumpRoles(conn);
579 
580  /* Dump role memberships --- need different method for pre-8.1 */
581  if (server_version >= 80100)
582  dumpRoleMembership(conn);
583  else
584  dumpGroups(conn);
585  }
586 
587  /* Dump tablespaces */
588  if (!roles_only && !no_tablespaces)
589  dumpTablespaces(conn);
590  }
591 
592  if (!globals_only && !roles_only && !tablespaces_only)
593  dumpDatabases(conn);
594 
595  PQfinish(conn);
596 
597  if (verbose)
598  dumpTimestamp("Completed on");
599  fprintf(OPF, "--\n-- PostgreSQL database cluster dump complete\n--\n\n");
600 
601  if (filename)
602  {
603  fclose(OPF);
604 
605  /* sync the resulting file, errors are not fatal */
606  if (dosync)
607  (void) fsync_fname(filename, false);
608  }
609 
610  exit_nicely(0);
611 }
static void dumpTimestamp(const char *msg)
Definition: pg_dumpall.c:1922
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
Definition: exec.c:323
static int column_inserts
Definition: pg_dumpall.c:69
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6631
static int disable_dollar_quoting
Definition: pg_dumpall.c:70
static int no_publications
Definition: pg_dumpall.c:77
#define PGDUMP_VERSIONSTR
Definition: pg_dumpall.c:31
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:80
static FILE * OPF
Definition: pg_dumpall.c:90
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void pg_logging_init(const char *argv0)
Definition: logging.c:81
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:633
static void dropRoles(PGconn *conn)
Definition: pg_dumpall.c:683
static const char * progname
Definition: pg_dumpall.c:60
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4171
#define PG_BINARY_W
Definition: c.h:1214
static void dumpTablespaces(PGconn *conn)
Definition: pg_dumpall.c:1156
static int inserts
Definition: pg_dumpall.c:73
#define fprintf
Definition: port.h:197
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6726
#define PG_AUTHID
Definition: pg_dumpall.c:87
static bool verbose
Definition: pg_dumpall.c:65
static void dropDBs(PGconn *conn)
Definition: pg_dumpall.c:1312
#define sprintf
Definition: port.h:195
static int disable_triggers
Definition: pg_dumpall.c:71
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
#define required_argument
Definition: getopt_long.h:25
int optind
Definition: getopt.c:50
static int no_tablespaces
Definition: pg_dumpall.c:74
static bool output_clean
Definition: pg_dumpall.c:63
PGconn * conn
Definition: streamutil.c:54
#define MAXPGPATH
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static SimpleStringList database_exclude_patterns
Definition: pg_dumpall.c:93
static int on_conflict_do_nothing
Definition: pg_dumpall.c:84
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:128
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1898
static int no_unlogged_table_data
Definition: pg_dumpall.c:80
char * c
static PQExpBuffer pgdumpopts
Definition: pg_dumpall.c:61
static void dumpRoleMembership(PGconn *conn)
Definition: pg_dumpall.c:996
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void dumpGroups(PGconn *conn)
Definition: pg_dumpall.c:1056
static bool dosync
Definition: pg_dumpall.c:66
char * pghost
Definition: pgbench.c:243
trivalue
Definition: vacuumlo.c:34
#define no_argument
Definition: getopt_long.h:24
#define exit_nicely(code)
Definition: pg_dumpall.c:96
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1155
static void dumpDatabases(PGconn *conn)
Definition: pg_dumpall.c:1458
void simple_string_list_append(SimpleStringList *list, const char *val)
Definition: simple_list.c:63
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
int PQsetClientEncoding(PGconn *conn, const char *encoding)
Definition: fe-connect.c:6734
static void help(void)
Definition: pg_dumpall.c:615
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:429
static int no_subscriptions
Definition: pg_dumpall.c:79
bool quote_all_identifiers
Definition: ruleutils.c:316
static int load_via_partition_root
Definition: pg_dumpall.c:83
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
static int use_setsessauth
Definition: pg_dumpall.c:75
static int no_comments
Definition: pg_dumpall.c:76
static PGconn * connectDatabase(const char *dbname, const char *connstr, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
Definition: pg_dumpall.c:1636
static int if_exists
Definition: pg_dumpall.c:72
SimpleStringListCell * head
Definition: simple_list.h:42
static int server_version
Definition: pg_dumpall.c:82
static char pg_dump_bin[MAXPGPATH]
Definition: pg_dumpall.c:59
static bool skip_acls
Definition: pg_dumpall.c:64
static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns, SimpleStringList *names)
Definition: pg_dumpall.c:1416
void pg_logging_increase_verbosity(void)
Definition: logging.c:174
int32 encoding
Definition: pg_database.h:41
static int binary_upgrade
Definition: pg_dumpall.c:68
#define PG_ROLES
Definition: pg_dumpall.c:88
static char * filename
Definition: pg_dumpall.c:91
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
char * optarg
Definition: getopt.c:52
static int no_role_passwords
Definition: pg_dumpall.c:81
void pg_logging_set_level(enum pg_log_level new_level)
Definition: logging.c:165
static int no_security_labels
Definition: pg_dumpall.c:78
#define _(x)
Definition: elog.c:88
char * pgport
Definition: pgbench.c:244
static void dumpRoles(PGconn *conn)
Definition: pg_dumpall.c:738
static SimpleStringList database_exclude_names
Definition: pg_dumpall.c:94
static char * connstr
Definition: pg_dumpall.c:62
static void dropTablespaces(PGconn *conn)
Definition: pg_dumpall.c:1121
static char role_catalog[10]
Definition: pg_dumpall.c:86

◆ runPgDump()

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

Definition at line 1558 of file pg_dumpall.c.

References appendConnStrVal(), appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), connstr, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), filename, pg_dump_bin, pg_log_info, and generate_unaccent_rules::stdout.

Referenced by dumpDatabases().

1559 {
1560  PQExpBuffer connstrbuf = createPQExpBuffer();
1562  int ret;
1563 
1564  appendPQExpBuffer(cmd, "\"%s\" %s %s", pg_dump_bin,
1565  pgdumpopts->data, create_opts);
1566 
1567  /*
1568  * If we have a filename, use the undocumented plain-append pg_dump
1569  * format.
1570  */
1571  if (filename)
1572  appendPQExpBufferStr(cmd, " -Fa ");
1573  else
1574  appendPQExpBufferStr(cmd, " -Fp ");
1575 
1576  /*
1577  * Append the database name to the already-constructed stem of connection
1578  * string.
1579  */
1580  appendPQExpBuffer(connstrbuf, "%s dbname=", connstr);
1581  appendConnStrVal(connstrbuf, dbname);
1582 
1583  appendShellString(cmd, connstrbuf->data);
1584 
1585  pg_log_info("running \"%s\"", cmd->data);
1586 
1587  fflush(stdout);
1588  fflush(stderr);
1589 
1590  ret = system(cmd->data);
1591 
1592  destroyPQExpBuffer(cmd);
1593  destroyPQExpBuffer(connstrbuf);
1594 
1595  return ret;
1596 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:545
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
static PQExpBuffer pgdumpopts
Definition: pg_dumpall.c:61
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:74
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:429
static char pg_dump_bin[MAXPGPATH]
Definition: pg_dumpall.c:59
char * dbname
Definition: streamutil.c:51
static char * filename
Definition: pg_dumpall.c:91
#define pg_log_info(...)
Definition: logging.h:88
static char * connstr
Definition: pg_dumpall.c:62

Variable Documentation

◆ binary_upgrade

int binary_upgrade = 0
static

Definition at line 68 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

◆ column_inserts

int column_inserts = 0
static

Definition at line 69 of file pg_dumpall.c.

Referenced by main().

◆ connstr

◆ database_exclude_names

SimpleStringList database_exclude_names = {NULL, NULL}
static

Definition at line 94 of file pg_dumpall.c.

◆ database_exclude_patterns

SimpleStringList database_exclude_patterns = {NULL, NULL}
static

Definition at line 93 of file pg_dumpall.c.

◆ disable_dollar_quoting

int disable_dollar_quoting = 0
static

Definition at line 70 of file pg_dumpall.c.

Referenced by main().

◆ disable_triggers

int disable_triggers = 0
static

Definition at line 71 of file pg_dumpall.c.

Referenced by main().

◆ dosync

bool dosync = true
static

Definition at line 66 of file pg_dumpall.c.

Referenced by main().

◆ filename

◆ if_exists

int if_exists = 0
static

Definition at line 72 of file pg_dumpall.c.

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

◆ inserts

int inserts = 0
static

Definition at line 73 of file pg_dumpall.c.

Referenced by main().

◆ load_via_partition_root

int load_via_partition_root = 0
static

Definition at line 83 of file pg_dumpall.c.

Referenced by main().

◆ no_comments

int no_comments = 0
static

Definition at line 76 of file pg_dumpall.c.

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

◆ no_publications

int no_publications = 0
static

Definition at line 77 of file pg_dumpall.c.

Referenced by main().

◆ no_role_passwords

int no_role_passwords = 0
static

Definition at line 81 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

◆ no_security_labels

int no_security_labels = 0
static

Definition at line 78 of file pg_dumpall.c.

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

◆ no_subscriptions

int no_subscriptions = 0
static

Definition at line 79 of file pg_dumpall.c.

Referenced by main().

◆ no_tablespaces

int no_tablespaces = 0
static

Definition at line 74 of file pg_dumpall.c.

Referenced by main().

◆ no_unlogged_table_data

int no_unlogged_table_data = 0
static

Definition at line 80 of file pg_dumpall.c.

Referenced by main().

◆ on_conflict_do_nothing

int on_conflict_do_nothing = 0
static

Definition at line 84 of file pg_dumpall.c.

Referenced by main().

◆ OPF

◆ output_clean

bool output_clean = false
static

Definition at line 63 of file pg_dumpall.c.

Referenced by dumpDatabases(), and main().

◆ pg_dump_bin

char pg_dump_bin[MAXPGPATH]
static

Definition at line 59 of file pg_dumpall.c.

Referenced by main(), and runPgDump().

◆ pgdumpopts

PQExpBuffer pgdumpopts
static

Definition at line 61 of file pg_dumpall.c.

◆ progname

const char* progname
static

Definition at line 60 of file pg_dumpall.c.

Referenced by connectDatabase(), help(), and main().

◆ role_catalog

char role_catalog[10]
static

Definition at line 86 of file pg_dumpall.c.

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

◆ server_version

int server_version
static

◆ skip_acls

bool skip_acls = false
static

Definition at line 64 of file pg_dumpall.c.

Referenced by dumpTablespaces(), and main().

◆ use_setsessauth

int use_setsessauth = 0
static

Definition at line 75 of file pg_dumpall.c.

Referenced by main().

◆ verbose

bool verbose = false
static

Definition at line 65 of file pg_dumpall.c.

Referenced by main().