PostgreSQL Source Code  git master
pg_dumpall.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include <unistd.h>
#include "common/file_utils.h"
#include "common/logging.h"
#include "dumputils.h"
#include "fe_utils/connect.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 86 of file pg_dumpall.c.

Referenced by main().

◆ PG_ROLES

#define PG_ROLES   "pg_roles "

Definition at line 87 of file pg_dumpall.c.

Referenced by main().

◆ PGDUMP_VERSIONSTR

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

Definition at line 30 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 1608 of file pg_dumpall.c.

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

Referenced by dumpRoles(), and dumpTablespaces().

1611 {
1613  PGresult *res;
1614 
1615  buildShSecLabelQuery(conn, catalog_name, objectId, sql);
1616  res = executeQuery(conn, sql->data);
1617  emitShSecLabels(conn, res, buffer, objtype, objname);
1618 
1619  PQclear(res);
1620  destroyPQExpBuffer(sql);
1621 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1878
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
void buildShSecLabelQuery(PGconn *conn, const char *catalog_name, Oid objectId, PQExpBuffer sql)
Definition: dumputils.c:688

◆ 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 1634 of file pg_dumpall.c.

References ALWAYS_SECURE_SEARCH_PATH_SQL, conn, CONNECTION_BAD, connstr, constructConnStr(), dbname, executeQuery(), exit_nicely, free, have_password, 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().

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

◆ constructConnStr()

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

Definition at line 1847 of file pg_dumpall.c.

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

Referenced by connectDatabase().

1848 {
1850  char *connstr;
1851  int i;
1852  bool firstkeyword = true;
1853 
1854  /* Construct a new connection string in key='value' format. */
1855  for (i = 0; keywords[i] != NULL; i++)
1856  {
1857  if (strcmp(keywords[i], "dbname") == 0 ||
1858  strcmp(keywords[i], "password") == 0 ||
1859  strcmp(keywords[i], "fallback_application_name") == 0)
1860  continue;
1861 
1862  if (!firstkeyword)
1863  appendPQExpBufferChar(buf, ' ');
1864  firstkeyword = false;
1865  appendPQExpBuffer(buf, "%s=", keywords[i]);
1866  appendConnStrVal(buf, values[i]);
1867  }
1868 
1869  connstr = pg_strdup(buf->data);
1870  destroyPQExpBuffer(buf);
1871  return connstr;
1872 }
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:67
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:167
int i
static char * connstr
Definition: pg_dumpall.c:61

◆ dropDBs()

static void dropDBs ( PGconn conn)
static

Definition at line 1310 of file pg_dumpall.c.

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

Referenced by main().

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

◆ dropRoles()

static void dropRoles ( PGconn conn)
static

Definition at line 681 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().

682 {
684  PGresult *res;
685  int i_rolname;
686  int i;
687 
688  if (server_version >= 90600)
689  printfPQExpBuffer(buf,
690  "SELECT rolname "
691  "FROM %s "
692  "WHERE rolname !~ '^pg_' "
693  "ORDER BY 1", role_catalog);
694  else if (server_version >= 80100)
695  printfPQExpBuffer(buf,
696  "SELECT rolname "
697  "FROM %s "
698  "ORDER BY 1", role_catalog);
699  else
700  printfPQExpBuffer(buf,
701  "SELECT usename as rolname "
702  "FROM pg_shadow "
703  "UNION "
704  "SELECT groname as rolname "
705  "FROM pg_group "
706  "ORDER BY 1");
707 
708  res = executeQuery(conn, buf->data);
709 
710  i_rolname = PQfnumber(res, "rolname");
711 
712  if (PQntuples(res) > 0)
713  fprintf(OPF, "--\n-- Drop roles\n--\n\n");
714 
715  for (i = 0; i < PQntuples(res); i++)
716  {
717  const char *rolename;
718 
719  rolename = PQgetvalue(res, i, i_rolname);
720 
721  fprintf(OPF, "DROP ROLE %s%s;\n",
722  if_exists ? "IF EXISTS " : "",
723  fmtId(rolename));
724  }
725 
726  PQclear(res);
727  destroyPQExpBuffer(buf);
728 
729  fprintf(OPF, "\n\n");
730 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1878
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:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:196
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static char * buf
Definition: pg_test_fsync.c:67
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:71
static int server_version
Definition: pg_dumpall.c:81
int i
static char role_catalog[10]
Definition: pg_dumpall.c:85

◆ dropTablespaces()

static void dropTablespaces ( PGconn conn)
static

Definition at line 1119 of file pg_dumpall.c.

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

Referenced by main().

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

◆ dumpDatabases()

static void dumpDatabases ( PGconn conn)
static

Definition at line 1456 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().

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

◆ dumpGroups()

static void dumpGroups ( PGconn conn)
static

Definition at line 1054 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().

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

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

◆ dumpRoles()

static void dumpRoles ( PGconn conn)
static

Definition at line 736 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().

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

◆ dumpTablespaces()

static void dumpTablespaces ( PGconn conn)
static

Definition at line 1154 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().

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

◆ dumpTimestamp()

static void dumpTimestamp ( const char *  msg)
static

Definition at line 1925 of file pg_dumpall.c.

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

Referenced by main().

1926 {
1927  char buf[64];
1928  time_t now = time(NULL);
1929 
1930  if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
1931  fprintf(OPF, "-- %s %s\n\n", msg, buf);
1932 }
static FILE * OPF
Definition: pg_dumpall.c:89
#define fprintf
Definition: port.h:196
static char * buf
Definition: pg_test_fsync.c:67
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547

◆ dumpUserConfig()

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

Definition at line 1357 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().

1358 {
1360  int count = 1;
1361  bool first = true;
1362 
1363  for (;;)
1364  {
1365  PGresult *res;
1366 
1367  if (server_version >= 90000)
1368  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1369  "setdatabase = 0 AND setrole = "
1370  "(SELECT oid FROM %s WHERE rolname = ", count, role_catalog);
1371  else if (server_version >= 80100)
1372  printfPQExpBuffer(buf, "SELECT rolconfig[%d] FROM %s WHERE rolname = ", count, role_catalog);
1373  else
1374  printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
1375  appendStringLiteralConn(buf, username, conn);
1376  if (server_version >= 90000)
1377  appendPQExpBufferChar(buf, ')');
1378 
1379  res = executeQuery(conn, buf->data);
1380  if (PQntuples(res) == 1 &&
1381  !PQgetisnull(res, 0, 0))
1382  {
1383  /* comment at section start, only if needed */
1384  if (first)
1385  {
1386  fprintf(OPF, "--\n-- User Configurations\n--\n\n");
1387  first = false;
1388  }
1389 
1390  fprintf(OPF, "--\n-- User Config \"%s\"\n--\n\n", username);
1391  resetPQExpBuffer(buf);
1392  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1393  "ROLE", username, NULL, NULL,
1394  buf);
1395  fprintf(OPF, "%s", buf->data);
1396  PQclear(res);
1397  count++;
1398  }
1399  else
1400  {
1401  PQclear(res);
1402  break;
1403  }
1404  }
1405 
1406  destroyPQExpBuffer(buf);
1407 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:1878
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:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define fprintf
Definition: port.h:196
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:116
static char * buf
Definition: pg_test_fsync.c:67
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:133
void PQclear(PGresult *res)
Definition: fe-exec.c:694
static int server_version
Definition: pg_dumpall.c:81
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:85

◆ executeCommand()

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

Definition at line 1901 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().

1902 {
1903  PGresult *res;
1904 
1905  pg_log_info("executing %s", query);
1906 
1907  res = PQexec(conn, query);
1908  if (!res ||
1910  {
1911  pg_log_error("query failed: %s", PQerrorMessage(conn));
1912  pg_log_error("query was: %s", query);
1913  PQfinish(conn);
1914  exit_nicely(1);
1915  }
1916 
1917  PQclear(res);
1918 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6631
#define pg_log_error(...)
Definition: logging.h:79
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define exit_nicely(code)
Definition: pg_dumpall.c:95
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:87

◆ executeQuery()

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

Definition at line 1878 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().

1879 {
1880  PGresult *res;
1881 
1882  pg_log_info("executing %s", query);
1883 
1884  res = PQexec(conn, query);
1885  if (!res ||
1887  {
1888  pg_log_error("query failed: %s", PQerrorMessage(conn));
1889  pg_log_error("query was: %s", query);
1890  PQfinish(conn);
1891  exit_nicely(1);
1892  }
1893 
1894  return res;
1895 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6631
#define pg_log_error(...)
Definition: logging.h:79
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
#define exit_nicely(code)
Definition: pg_dumpall.c:95
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
#define pg_log_info(...)
Definition: logging.h:87

◆ expand_dbname_patterns()

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

Definition at line 1414 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().

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

References _, printf, and progname.

Referenced by main().

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

◆ main()

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

Definition at line 98 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_INFO, PG_LOG_WARNING, 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.

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

◆ runPgDump()

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

Definition at line 1556 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().

1557 {
1558  PQExpBuffer connstrbuf = createPQExpBuffer();
1560  int ret;
1561 
1562  appendPQExpBuffer(cmd, "\"%s\" %s %s", pg_dump_bin,
1563  pgdumpopts->data, create_opts);
1564 
1565  /*
1566  * If we have a filename, use the undocumented plain-append pg_dump
1567  * format.
1568  */
1569  if (filename)
1570  appendPQExpBufferStr(cmd, " -Fa ");
1571  else
1572  appendPQExpBufferStr(cmd, " -Fp ");
1573 
1574  /*
1575  * Append the database name to the already-constructed stem of connection
1576  * string.
1577  */
1578  appendPQExpBuffer(connstrbuf, "%s dbname=", connstr);
1579  appendConnStrVal(connstrbuf, dbname);
1580 
1581  appendShellString(cmd, connstrbuf->data);
1582 
1583  pg_log_info("running \"%s\"", cmd->data);
1584 
1585  fflush(stdout);
1586  fflush(stderr);
1587 
1588  ret = system(cmd->data);
1589 
1590  destroyPQExpBuffer(cmd);
1591  destroyPQExpBuffer(connstrbuf);
1592 
1593  return ret;
1594 }
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:60
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:58
char * dbname
Definition: streamutil.c:50
static char * filename
Definition: pg_dumpall.c:90
#define pg_log_info(...)
Definition: logging.h:87
static char * connstr
Definition: pg_dumpall.c:61

Variable Documentation

◆ binary_upgrade

int binary_upgrade = 0
static

Definition at line 67 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

◆ column_inserts

int column_inserts = 0
static

Definition at line 68 of file pg_dumpall.c.

Referenced by main().

◆ connstr

◆ database_exclude_names

SimpleStringList database_exclude_names = {NULL, NULL}
static

Definition at line 93 of file pg_dumpall.c.

◆ database_exclude_patterns

SimpleStringList database_exclude_patterns = {NULL, NULL}
static

Definition at line 92 of file pg_dumpall.c.

◆ disable_dollar_quoting

int disable_dollar_quoting = 0
static

Definition at line 69 of file pg_dumpall.c.

Referenced by main().

◆ disable_triggers

int disable_triggers = 0
static

Definition at line 70 of file pg_dumpall.c.

Referenced by main().

◆ dosync

bool dosync = true
static

Definition at line 65 of file pg_dumpall.c.

Referenced by main().

◆ filename

◆ if_exists

int if_exists = 0
static

Definition at line 71 of file pg_dumpall.c.

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

◆ inserts

int inserts = 0
static

Definition at line 72 of file pg_dumpall.c.

Referenced by main().

◆ load_via_partition_root

int load_via_partition_root = 0
static

Definition at line 82 of file pg_dumpall.c.

Referenced by main().

◆ no_comments

int no_comments = 0
static

Definition at line 75 of file pg_dumpall.c.

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

◆ no_publications

int no_publications = 0
static

Definition at line 76 of file pg_dumpall.c.

Referenced by main().

◆ no_role_passwords

int no_role_passwords = 0
static

Definition at line 80 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

◆ no_security_labels

int no_security_labels = 0
static

Definition at line 77 of file pg_dumpall.c.

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

◆ no_subscriptions

int no_subscriptions = 0
static

Definition at line 78 of file pg_dumpall.c.

Referenced by main().

◆ no_tablespaces

int no_tablespaces = 0
static

Definition at line 73 of file pg_dumpall.c.

Referenced by main().

◆ no_unlogged_table_data

int no_unlogged_table_data = 0
static

Definition at line 79 of file pg_dumpall.c.

Referenced by main().

◆ on_conflict_do_nothing

int on_conflict_do_nothing = 0
static

Definition at line 83 of file pg_dumpall.c.

Referenced by main().

◆ OPF

◆ output_clean

bool output_clean = false
static

Definition at line 62 of file pg_dumpall.c.

Referenced by dumpDatabases(), and main().

◆ pg_dump_bin

char pg_dump_bin[MAXPGPATH]
static

Definition at line 58 of file pg_dumpall.c.

Referenced by main(), and runPgDump().

◆ pgdumpopts

PQExpBuffer pgdumpopts
static

Definition at line 60 of file pg_dumpall.c.

◆ progname

const char* progname
static

Definition at line 59 of file pg_dumpall.c.

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

◆ role_catalog

char role_catalog[10]
static

Definition at line 85 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 63 of file pg_dumpall.c.

Referenced by dumpTablespaces(), and main().

◆ use_setsessauth

int use_setsessauth = 0
static

Definition at line 74 of file pg_dumpall.c.

Referenced by main().

◆ verbose

bool verbose = false
static

Definition at line 64 of file pg_dumpall.c.

Referenced by main().