PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_dumpall.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include <unistd.h>
#include "getopt_long.h"
#include "dumputils.h"
#include "pg_backup.h"
#include "common/file_utils.h"
#include "fe_utils/string_utils.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 dumpCreateDB (PGconn *conn)
 
static void dumpDatabaseConfig (PGconn *conn, const char *dbname)
 
static void dumpUserConfig (PGconn *conn, const char *username)
 
static void dumpDbRoleConfig (PGconn *conn)
 
static void makeAlterConfigCommand (PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
 
static void dumpDatabases (PGconn *conn)
 
static void dumpTimestamp (const char *msg)
 
static int runPgDump (const char *dbname)
 
static void buildShSecLabels (PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer buffer, const char *target, const char *objname)
 
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)
 
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 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_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 char role_catalog [10]
 
static FILE * OPF
 
static char * filename = NULL
 

Macro Definition Documentation

#define PG_AUTHID   "pg_authid"

Definition at line 86 of file pg_dumpall.c.

Referenced by main().

#define PG_ROLES   "pg_roles "

Definition at line 87 of file pg_dumpall.c.

Referenced by main().

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

Definition at line 29 of file pg_dumpall.c.

Referenced by main().

Function Documentation

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

Definition at line 1866 of file pg_dumpall.c.

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

Referenced by dumpRoles(), and dumpTablespaces().

1868 {
1870  PGresult *res;
1871 
1872  buildShSecLabelQuery(conn, catalog_name, objectId, sql);
1873  res = executeQuery(conn, sql->data);
1874  emitShSecLabels(conn, res, buffer, target, objname);
1875 
1876  PQclear(res);
1877  destroyPQExpBuffer(sql);
1878 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void buildShSecLabelQuery(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer sql)
Definition: dumputils.c:643
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *target, const char *objname)
Definition: dumputils.c:659
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 1891 of file pg_dumpall.c.

References _, conn, CONNECTION_BAD, connstr, constructConnStr(), dbname, executeCommand(), exit_nicely, free, have_password, i, _PQconninfoOption::keyword, password, pg_malloc0(), pghost, pgport, 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(), reindex_one_database(), reindex_system_catalogs(), and vacuum_one_database().

1894 {
1895  PGconn *conn;
1896  bool new_pass;
1897  const char *remoteversion_str;
1898  int my_version;
1899  const char **keywords = NULL;
1900  const char **values = NULL;
1901  PQconninfoOption *conn_opts = NULL;
1902  static bool have_password = false;
1903  static char password[100];
1904 
1905  if (prompt_password == TRI_YES && !have_password)
1906  {
1907  simple_prompt("Password: ", password, sizeof(password), false);
1908  have_password = true;
1909  }
1910 
1911  /*
1912  * Start the connection. Loop until we have a password if requested by
1913  * backend.
1914  */
1915  do
1916  {
1917  int argcount = 6;
1918  PQconninfoOption *conn_opt;
1919  char *err_msg = NULL;
1920  int i = 0;
1921 
1922  if (keywords)
1923  free(keywords);
1924  if (values)
1925  free(values);
1926  if (conn_opts)
1927  PQconninfoFree(conn_opts);
1928 
1929  /*
1930  * Merge the connection info inputs given in form of connection string
1931  * and other options. Explicitly discard any dbname value in the
1932  * connection string; otherwise, PQconnectdbParams() would interpret
1933  * that value as being itself a connection string.
1934  */
1935  if (connection_string)
1936  {
1937  conn_opts = PQconninfoParse(connection_string, &err_msg);
1938  if (conn_opts == NULL)
1939  {
1940  fprintf(stderr, "%s: %s", progname, err_msg);
1941  exit_nicely(1);
1942  }
1943 
1944  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1945  {
1946  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1947  strcmp(conn_opt->keyword, "dbname") != 0)
1948  argcount++;
1949  }
1950 
1951  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1952  values = pg_malloc0((argcount + 1) * sizeof(*values));
1953 
1954  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1955  {
1956  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1957  strcmp(conn_opt->keyword, "dbname") != 0)
1958  {
1959  keywords[i] = conn_opt->keyword;
1960  values[i] = conn_opt->val;
1961  i++;
1962  }
1963  }
1964  }
1965  else
1966  {
1967  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1968  values = pg_malloc0((argcount + 1) * sizeof(*values));
1969  }
1970 
1971  if (pghost)
1972  {
1973  keywords[i] = "host";
1974  values[i] = pghost;
1975  i++;
1976  }
1977  if (pgport)
1978  {
1979  keywords[i] = "port";
1980  values[i] = pgport;
1981  i++;
1982  }
1983  if (pguser)
1984  {
1985  keywords[i] = "user";
1986  values[i] = pguser;
1987  i++;
1988  }
1989  if (have_password)
1990  {
1991  keywords[i] = "password";
1992  values[i] = password;
1993  i++;
1994  }
1995  if (dbname)
1996  {
1997  keywords[i] = "dbname";
1998  values[i] = dbname;
1999  i++;
2000  }
2001  keywords[i] = "fallback_application_name";
2002  values[i] = progname;
2003  i++;
2004 
2005  new_pass = false;
2006  conn = PQconnectdbParams(keywords, values, true);
2007 
2008  if (!conn)
2009  {
2010  fprintf(stderr, _("%s: could not connect to database \"%s\"\n"),
2011  progname, dbname);
2012  exit_nicely(1);
2013  }
2014 
2015  if (PQstatus(conn) == CONNECTION_BAD &&
2016  PQconnectionNeedsPassword(conn) &&
2017  !have_password &&
2018  prompt_password != TRI_NO)
2019  {
2020  PQfinish(conn);
2021  simple_prompt("Password: ", password, sizeof(password), false);
2022  have_password = true;
2023  new_pass = true;
2024  }
2025  } while (new_pass);
2026 
2027  /* check to see that the backend connection was successfully made */
2028  if (PQstatus(conn) == CONNECTION_BAD)
2029  {
2030  if (fail_on_error)
2031  {
2032  fprintf(stderr,
2033  _("%s: could not connect to database \"%s\": %s\n"),
2034  progname, dbname, PQerrorMessage(conn));
2035  exit_nicely(1);
2036  }
2037  else
2038  {
2039  PQfinish(conn);
2040 
2041  free(keywords);
2042  free(values);
2043  PQconninfoFree(conn_opts);
2044 
2045  return NULL;
2046  }
2047  }
2048 
2049  /*
2050  * Ok, connected successfully. Remember the options used, in the form of a
2051  * connection string.
2052  */
2053  connstr = constructConnStr(keywords, values);
2054 
2055  free(keywords);
2056  free(values);
2057  PQconninfoFree(conn_opts);
2058 
2059  /* Check version */
2060  remoteversion_str = PQparameterStatus(conn, "server_version");
2061  if (!remoteversion_str)
2062  {
2063  fprintf(stderr, _("%s: could not get server version\n"), progname);
2064  exit_nicely(1);
2065  }
2067  if (server_version == 0)
2068  {
2069  fprintf(stderr, _("%s: could not parse server version \"%s\"\n"),
2070  progname, remoteversion_str);
2071  exit_nicely(1);
2072  }
2073 
2074  my_version = PG_VERSION_NUM;
2075 
2076  /*
2077  * We allow the server to be back to 8.0, and up to any minor release of
2078  * our own major version. (See also version check in pg_dump.c.)
2079  */
2080  if (my_version != server_version
2081  && (server_version < 80000 ||
2082  (server_version / 100) > (my_version / 100)))
2083  {
2084  fprintf(stderr, _("server version: %s; %s version: %s\n"),
2085  remoteversion_str, progname, PG_VERSION);
2086  fprintf(stderr, _("aborting because of server version mismatch\n"));
2087  exit_nicely(1);
2088  }
2089 
2090  /*
2091  * Make sure we are not fooled by non-system schemas in the search path.
2092  */
2093  executeCommand(conn, "SET search_path = pg_catalog");
2094 
2095  return conn;
2096 }
static char password[100]
Definition: streamutil.c:45
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6062
static char * constructConnStr(const char **keywords, const char **values)
Definition: pg_dumpall.c:2109
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:6087
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:529
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:4717
char * connection_string
Definition: streamutil.c:38
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
PGconn * conn
Definition: streamutil.c:46
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2166
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:5947
char * pghost
Definition: pgbench.c:180
#define exit_nicely(code)
Definition: pg_dumpall.c:92
#define free(a)
Definition: header.h:65
static bool have_password
Definition: streamutil.c:44
static int server_version
Definition: pg_dumpall.c:82
char * dbname
Definition: streamutil.c:42
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6131
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
#define _(x)
Definition: elog.c:84
char * pgport
Definition: pgbench.c:181
static char * connstr
Definition: pg_dumpall.c:64
static char * constructConnStr ( const char **  keywords,
const char **  values 
)
static

Definition at line 2109 of file pg_dumpall.c.

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

Referenced by connectDatabase().

2110 {
2112  char *connstr;
2113  int i;
2114  bool firstkeyword = true;
2115 
2116  /* Construct a new connection string in key='value' format. */
2117  for (i = 0; keywords[i] != NULL; i++)
2118  {
2119  if (strcmp(keywords[i], "dbname") == 0 ||
2120  strcmp(keywords[i], "password") == 0 ||
2121  strcmp(keywords[i], "fallback_application_name") == 0)
2122  continue;
2123 
2124  if (!firstkeyword)
2125  appendPQExpBufferChar(buf, ' ');
2126  firstkeyword = false;
2127  appendPQExpBuffer(buf, "%s=", keywords[i]);
2128  appendConnStrVal(buf, values[i]);
2129  }
2130 
2131  connstr = pg_strdup(buf->data);
2132  destroyPQExpBuffer(buf);
2133  return connstr;
2134 }
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
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:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int i
static char * connstr
Definition: pg_dumpall.c:64
static void dropDBs ( PGconn conn)
static

Definition at line 1269 of file pg_dumpall.c.

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

Referenced by main().

1270 {
1271  PGresult *res;
1272  int i;
1273 
1274  res = executeQuery(conn,
1275  "SELECT datname "
1276  "FROM pg_database d "
1277  "WHERE datallowconn ORDER BY 1");
1278 
1279  if (PQntuples(res) > 0)
1280  fprintf(OPF, "--\n-- Drop databases\n--\n\n");
1281 
1282  for (i = 0; i < PQntuples(res); i++)
1283  {
1284  char *dbname = PQgetvalue(res, i, 0);
1285 
1286  /*
1287  * Skip "template1" and "postgres"; the restore script is almost
1288  * certainly going to be run in one or the other, and we don't know
1289  * which. This must agree with dumpCreateDB's choices!
1290  */
1291  if (strcmp(dbname, "template1") != 0 &&
1292  strcmp(dbname, "postgres") != 0)
1293  {
1294  fprintf(OPF, "DROP DATABASE %s%s;\n",
1295  if_exists ? "IF EXISTS " : "",
1296  fmtId(dbname));
1297  }
1298  }
1299 
1300  PQclear(res);
1301 
1302  fprintf(OPF, "\n\n");
1303 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static int if_exists
Definition: pg_dumpall.c:73
char * dbname
Definition: streamutil.c:42
int i
static void dropRoles ( PGconn conn)
static

Definition at line 660 of file pg_dumpall.c.

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

Referenced by main().

661 {
663  PGresult *res;
664  int i_rolname;
665  int i;
666 
667  if (server_version >= 90600)
668  printfPQExpBuffer(buf,
669  "SELECT rolname "
670  "FROM %s "
671  "WHERE rolname !~ '^pg_' "
672  "ORDER BY 1", role_catalog);
673  else if (server_version >= 80100)
674  printfPQExpBuffer(buf,
675  "SELECT rolname "
676  "FROM %s "
677  "ORDER BY 1", role_catalog);
678  else
679  printfPQExpBuffer(buf,
680  "SELECT usename as rolname "
681  "FROM pg_shadow "
682  "UNION "
683  "SELECT groname as rolname "
684  "FROM pg_group "
685  "ORDER BY 1");
686 
687  res = executeQuery(conn, buf->data);
688 
689  i_rolname = PQfnumber(res, "rolname");
690 
691  if (PQntuples(res) > 0)
692  fprintf(OPF, "--\n-- Drop roles\n--\n\n");
693 
694  for (i = 0; i < PQntuples(res); i++)
695  {
696  const char *rolename;
697 
698  rolename = PQgetvalue(res, i, i_rolname);
699 
700  fprintf(OPF, "DROP ROLE %s%s;\n",
701  if_exists ? "IF EXISTS " : "",
702  fmtId(rolename));
703  }
704 
705  PQclear(res);
706  destroyPQExpBuffer(buf);
707 
708  fprintf(OPF, "\n\n");
709 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:67
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static int if_exists
Definition: pg_dumpall.c:73
static int server_version
Definition: pg_dumpall.c:82
int i
static char role_catalog[10]
Definition: pg_dumpall.c:85
static void dropTablespaces ( PGconn conn)
static

Definition at line 1097 of file pg_dumpall.c.

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

Referenced by main().

1098 {
1099  PGresult *res;
1100  int i;
1101 
1102  /*
1103  * Get all tablespaces except built-in ones (which we assume are named
1104  * pg_xxx)
1105  */
1106  res = executeQuery(conn, "SELECT spcname "
1107  "FROM pg_catalog.pg_tablespace "
1108  "WHERE spcname !~ '^pg_' "
1109  "ORDER BY 1");
1110 
1111  if (PQntuples(res) > 0)
1112  fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1113 
1114  for (i = 0; i < PQntuples(res); i++)
1115  {
1116  char *spcname = PQgetvalue(res, i, 0);
1117 
1118  fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1119  if_exists ? "IF EXISTS " : "",
1120  fmtId(spcname));
1121  }
1122 
1123  PQclear(res);
1124 
1125  fprintf(OPF, "\n\n");
1126 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static int if_exists
Definition: pg_dumpall.c:73
int i
static void dumpCreateDB ( PGconn conn)
static

Definition at line 1317 of file pg_dumpall.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), appendPsqlMetaConnect(), appendStringLiteralConn(), atooid, binary_upgrade, buf, buildACLCommands(), createPQExpBuffer(), PQExpBufferData::data, dbname, destroyPQExpBuffer(), dumpDatabaseConfig(), executeQuery(), exit_nicely, fmtId(), free, i, no_tablespaces, OPF, pg_strdup(), PQclear(), PQfinish(), PQgetisnull(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), progname, resetPQExpBuffer(), role_catalog, server_version, and skip_acls.

Referenced by main().

1318 {
1320  char *default_encoding = NULL;
1321  char *default_collate = NULL;
1322  char *default_ctype = NULL;
1323  PGresult *res;
1324  int i;
1325 
1326  fprintf(OPF, "--\n-- Database creation\n--\n\n");
1327 
1328  /*
1329  * First, get the installation's default encoding and locale information.
1330  * We will dump encoding and locale specifications in the CREATE DATABASE
1331  * commands for just those databases with values different from defaults.
1332  *
1333  * We consider template0's encoding and locale to define the installation
1334  * default. Pre-8.4 installations do not have per-database locale
1335  * settings; for them, every database must necessarily be using the
1336  * installation default, so there's no need to do anything.
1337  */
1338  if (server_version >= 80400)
1339  res = executeQuery(conn,
1340  "SELECT pg_encoding_to_char(encoding), "
1341  "datcollate, datctype "
1342  "FROM pg_database "
1343  "WHERE datname = 'template0'");
1344  else
1345  res = executeQuery(conn,
1346  "SELECT pg_encoding_to_char(encoding), "
1347  "null::text AS datcollate, null::text AS datctype "
1348  "FROM pg_database "
1349  "WHERE datname = 'template0'");
1350 
1351  /* If for some reason the template DB isn't there, treat as unknown */
1352  if (PQntuples(res) > 0)
1353  {
1354  if (!PQgetisnull(res, 0, 0))
1355  default_encoding = pg_strdup(PQgetvalue(res, 0, 0));
1356  if (!PQgetisnull(res, 0, 1))
1357  default_collate = pg_strdup(PQgetvalue(res, 0, 1));
1358  if (!PQgetisnull(res, 0, 2))
1359  default_ctype = pg_strdup(PQgetvalue(res, 0, 2));
1360  }
1361 
1362  PQclear(res);
1363 
1364 
1365  /*
1366  * Now collect all the information about databases to dump.
1367  *
1368  * For the database ACLs, as of 9.6, we extract both the positive (as
1369  * datacl) and negative (as rdatacl) ACLs, relative to the default ACL for
1370  * databases, which are then passed to buildACLCommands() below.
1371  *
1372  * See buildACLQueries() and buildACLCommands().
1373  *
1374  * Note that we do not support initial privileges (pg_init_privs) on
1375  * databases.
1376  */
1377  if (server_version >= 90600)
1378  printfPQExpBuffer(buf,
1379  "SELECT datname, "
1380  "coalesce(rolname, (select rolname from %s where oid=(select datdba from pg_database where datname='template0'))), "
1381  "pg_encoding_to_char(d.encoding), "
1382  "datcollate, datctype, datfrozenxid, datminmxid, "
1383  "datistemplate, "
1384  "(SELECT pg_catalog.array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
1385  " SELECT pg_catalog.unnest(coalesce(datacl,pg_catalog.acldefault('d',datdba))) AS acl "
1386  " EXCEPT SELECT pg_catalog.unnest(pg_catalog.acldefault('d',datdba))) as datacls)"
1387  "AS datacl, "
1388  "(SELECT pg_catalog.array_agg(acl ORDER BY acl::text COLLATE \"C\") FROM ( "
1389  " SELECT pg_catalog.unnest(pg_catalog.acldefault('d',datdba)) AS acl "
1390  " EXCEPT SELECT pg_catalog.unnest(coalesce(datacl,pg_catalog.acldefault('d',datdba)))) as rdatacls)"
1391  "AS rdatacl, "
1392  "datconnlimit, "
1393  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
1394  "FROM pg_database d LEFT JOIN %s u ON (datdba = u.oid) "
1395  "WHERE datallowconn ORDER BY 1", role_catalog, role_catalog);
1396  else if (server_version >= 90300)
1397  printfPQExpBuffer(buf,
1398  "SELECT datname, "
1399  "coalesce(rolname, (select rolname from %s where oid=(select datdba from pg_database where datname='template0'))), "
1400  "pg_encoding_to_char(d.encoding), "
1401  "datcollate, datctype, datfrozenxid, datminmxid, "
1402  "datistemplate, datacl, '' as rdatacl, "
1403  "datconnlimit, "
1404  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
1405  "FROM pg_database d LEFT JOIN %s u ON (datdba = u.oid) "
1406  "WHERE datallowconn ORDER BY 1", role_catalog, role_catalog);
1407  else if (server_version >= 80400)
1408  printfPQExpBuffer(buf,
1409  "SELECT datname, "
1410  "coalesce(rolname, (select rolname from %s where oid=(select datdba from pg_database where datname='template0'))), "
1411  "pg_encoding_to_char(d.encoding), "
1412  "datcollate, datctype, datfrozenxid, 0 AS datminmxid, "
1413  "datistemplate, datacl, '' as rdatacl, "
1414  "datconnlimit, "
1415  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
1416  "FROM pg_database d LEFT JOIN %s u ON (datdba = u.oid) "
1417  "WHERE datallowconn ORDER BY 1", role_catalog, role_catalog);
1418  else if (server_version >= 80100)
1419  printfPQExpBuffer(buf,
1420  "SELECT datname, "
1421  "coalesce(rolname, (select rolname from %s where oid=(select datdba from pg_database where datname='template0'))), "
1422  "pg_encoding_to_char(d.encoding), "
1423  "null::text AS datcollate, null::text AS datctype, datfrozenxid, 0 AS datminmxid, "
1424  "datistemplate, datacl, '' as rdatacl, "
1425  "datconnlimit, "
1426  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
1427  "FROM pg_database d LEFT JOIN %s u ON (datdba = u.oid) "
1428  "WHERE datallowconn ORDER BY 1", role_catalog, role_catalog);
1429  else
1430  printfPQExpBuffer(buf,
1431  "SELECT datname, "
1432  "coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "
1433  "pg_encoding_to_char(d.encoding), "
1434  "null::text AS datcollate, null::text AS datctype, datfrozenxid, 0 AS datminmxid, "
1435  "datistemplate, datacl, '' as rdatacl, "
1436  "-1 as datconnlimit, "
1437  "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "
1438  "FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) "
1439  "WHERE datallowconn ORDER BY 1");
1440 
1441  res = executeQuery(conn, buf->data);
1442 
1443  for (i = 0; i < PQntuples(res); i++)
1444  {
1445  char *dbname = PQgetvalue(res, i, 0);
1446  char *dbowner = PQgetvalue(res, i, 1);
1447  char *dbencoding = PQgetvalue(res, i, 2);
1448  char *dbcollate = PQgetvalue(res, i, 3);
1449  char *dbctype = PQgetvalue(res, i, 4);
1450  uint32 dbfrozenxid = atooid(PQgetvalue(res, i, 5));
1451  uint32 dbminmxid = atooid(PQgetvalue(res, i, 6));
1452  char *dbistemplate = PQgetvalue(res, i, 7);
1453  char *dbacl = PQgetvalue(res, i, 8);
1454  char *rdbacl = PQgetvalue(res, i, 9);
1455  char *dbconnlimit = PQgetvalue(res, i, 10);
1456  char *dbtablespace = PQgetvalue(res, i, 11);
1457  char *fdbname;
1458 
1459  fdbname = pg_strdup(fmtId(dbname));
1460 
1461  resetPQExpBuffer(buf);
1462 
1463  /*
1464  * Skip the CREATE DATABASE commands for "template1" and "postgres",
1465  * since they are presumably already there in the destination cluster.
1466  * We do want to emit their ACLs and config options if any, however.
1467  */
1468  if (strcmp(dbname, "template1") != 0 &&
1469  strcmp(dbname, "postgres") != 0)
1470  {
1471  appendPQExpBuffer(buf, "CREATE DATABASE %s", fdbname);
1472 
1473  appendPQExpBufferStr(buf, " WITH TEMPLATE = template0");
1474 
1475  if (strlen(dbowner) != 0)
1476  appendPQExpBuffer(buf, " OWNER = %s", fmtId(dbowner));
1477 
1478  if (default_encoding && strcmp(dbencoding, default_encoding) != 0)
1479  {
1480  appendPQExpBufferStr(buf, " ENCODING = ");
1481  appendStringLiteralConn(buf, dbencoding, conn);
1482  }
1483 
1484  if (default_collate && strcmp(dbcollate, default_collate) != 0)
1485  {
1486  appendPQExpBufferStr(buf, " LC_COLLATE = ");
1487  appendStringLiteralConn(buf, dbcollate, conn);
1488  }
1489 
1490  if (default_ctype && strcmp(dbctype, default_ctype) != 0)
1491  {
1492  appendPQExpBufferStr(buf, " LC_CTYPE = ");
1493  appendStringLiteralConn(buf, dbctype, conn);
1494  }
1495 
1496  /*
1497  * Output tablespace if it isn't the default. For default, it
1498  * uses the default from the template database. If tablespace is
1499  * specified and tablespace creation failed earlier, (e.g. no such
1500  * directory), the database creation will fail too. One solution
1501  * would be to use 'SET default_tablespace' like we do in pg_dump
1502  * for setting non-default database locations.
1503  */
1504  if (strcmp(dbtablespace, "pg_default") != 0 && !no_tablespaces)
1505  appendPQExpBuffer(buf, " TABLESPACE = %s",
1506  fmtId(dbtablespace));
1507 
1508  if (strcmp(dbistemplate, "t") == 0)
1509  appendPQExpBuffer(buf, " IS_TEMPLATE = true");
1510 
1511  if (strcmp(dbconnlimit, "-1") != 0)
1512  appendPQExpBuffer(buf, " CONNECTION LIMIT = %s",
1513  dbconnlimit);
1514 
1515  appendPQExpBufferStr(buf, ";\n");
1516  }
1517  else if (strcmp(dbtablespace, "pg_default") != 0 && !no_tablespaces)
1518  {
1519  /*
1520  * Cannot change tablespace of the database we're connected to, so
1521  * to move "postgres" to another tablespace, we connect to
1522  * "template1", and vice versa.
1523  */
1524  if (strcmp(dbname, "postgres") == 0)
1525  appendPQExpBuffer(buf, "\\connect template1\n");
1526  else
1527  appendPQExpBuffer(buf, "\\connect postgres\n");
1528 
1529  appendPQExpBuffer(buf, "ALTER DATABASE %s SET TABLESPACE %s;\n",
1530  fdbname, fmtId(dbtablespace));
1531 
1532  /* connect to original database */
1533  appendPsqlMetaConnect(buf, dbname);
1534  }
1535 
1536  if (binary_upgrade)
1537  {
1538  appendPQExpBufferStr(buf, "-- For binary upgrade, set datfrozenxid and datminmxid.\n");
1539  appendPQExpBuffer(buf, "UPDATE pg_catalog.pg_database "
1540  "SET datfrozenxid = '%u', datminmxid = '%u' "
1541  "WHERE datname = ",
1542  dbfrozenxid, dbminmxid);
1543  appendStringLiteralConn(buf, dbname, conn);
1544  appendPQExpBufferStr(buf, ";\n");
1545  }
1546 
1547  if (!skip_acls &&
1548  !buildACLCommands(fdbname, NULL, "DATABASE",
1549  dbacl, rdbacl, dbowner,
1550  "", server_version, buf))
1551  {
1552  fprintf(stderr, _("%s: could not parse ACL list (%s) for database \"%s\"\n"),
1553  progname, dbacl, fdbname);
1554  PQfinish(conn);
1555  exit_nicely(1);
1556  }
1557 
1558  fprintf(OPF, "%s", buf->data);
1559 
1560  dumpDatabaseConfig(conn, dbname);
1561 
1562  free(fdbname);
1563  }
1564 
1565  if (default_encoding)
1566  free(default_encoding);
1567  if (default_collate)
1568  free(default_collate);
1569  if (default_ctype)
1570  free(default_ctype);
1571 
1572  PQclear(res);
1573  destroyPQExpBuffer(buf);
1574 
1575  fprintf(OPF, "\n\n");
1576 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
Definition: string_utils.c:596
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static int no_tablespaces
Definition: pg_dumpall.c:75
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:258
#define atooid(x)
Definition: postgres_ext.h:42
#define exit_nicely(code)
Definition: pg_dumpall.c:92
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
bool buildACLCommands(const char *name, const char *subname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:55
static int server_version
Definition: pg_dumpall.c:82
static bool skip_acls
Definition: pg_dumpall.c:65
char * dbname
Definition: streamutil.c:42
static int binary_upgrade
Definition: pg_dumpall.c:69
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
int i
static void dumpDatabaseConfig(PGconn *conn, const char *dbname)
Definition: pg_dumpall.c:1583
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
#define _(x)
Definition: elog.c:84
static char role_catalog[10]
Definition: pg_dumpall.c:85
static void dumpDatabaseConfig ( PGconn conn,
const char *  dbname 
)
static

Definition at line 1583 of file pg_dumpall.c.

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

Referenced by dumpCreateDB().

1584 {
1586  int count = 1;
1587 
1588  for (;;)
1589  {
1590  PGresult *res;
1591 
1592  if (server_version >= 90000)
1593  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1594  "setrole = 0 AND setdatabase = (SELECT oid FROM pg_database WHERE datname = ", count);
1595  else
1596  printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE datname = ", count);
1597  appendStringLiteralConn(buf, dbname, conn);
1598 
1599  if (server_version >= 90000)
1600  appendPQExpBufferChar(buf, ')');
1601 
1602  res = executeQuery(conn, buf->data);
1603  if (PQntuples(res) == 1 &&
1604  !PQgetisnull(res, 0, 0))
1605  {
1606  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1607  "DATABASE", dbname, NULL, NULL);
1608  PQclear(res);
1609  count++;
1610  }
1611  else
1612  {
1613  PQclear(res);
1614  break;
1615  }
1616  }
1617 
1618  destroyPQExpBuffer(buf);
1619 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:67
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1706
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static int server_version
Definition: pg_dumpall.c:82
char * dbname
Definition: streamutil.c:42
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
static void dumpDatabases ( PGconn conn)
static

Definition at line 1751 of file pg_dumpall.c.

References _, appendPsqlMetaConnect(), PQExpBufferData::data, dbname, executeQuery(), exit_nicely, filename, i, initPQExpBuffer(), OPF, PG_BINARY_A, PQclear(), PQgetvalue(), PQntuples(), progname, runPgDump(), strerror(), termPQExpBuffer(), and verbose.

Referenced by main().

1752 {
1753  PGresult *res;
1754  int i;
1755 
1756  res = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1");
1757 
1758  for (i = 0; i < PQntuples(res); i++)
1759  {
1760  int ret;
1761 
1762  char *dbname = PQgetvalue(res, i, 0);
1763  PQExpBufferData connectbuf;
1764 
1765  if (verbose)
1766  fprintf(stderr, _("%s: dumping database \"%s\"...\n"), progname, dbname);
1767 
1768  initPQExpBuffer(&connectbuf);
1769  appendPsqlMetaConnect(&connectbuf, dbname);
1770  fprintf(OPF, "%s\n", connectbuf.data);
1771  termPQExpBuffer(&connectbuf);
1772 
1773  /*
1774  * Restore will need to write to the target cluster. This connection
1775  * setting is emitted for pg_dumpall rather than in the code also used
1776  * by pg_dump, so that a cluster with databases or users which have
1777  * this flag turned on can still be replicated through pg_dumpall
1778  * without editing the file or stream. With pg_dump there are many
1779  * other ways to allow the file to be used, and leaving it out allows
1780  * users to protect databases from being accidental restore targets.
1781  */
1782  fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
1783 
1784  if (filename)
1785  fclose(OPF);
1786 
1787  ret = runPgDump(dbname);
1788  if (ret != 0)
1789  {
1790  fprintf(stderr, _("%s: pg_dump failed on database \"%s\", exiting\n"), progname, dbname);
1791  exit_nicely(1);
1792  }
1793 
1794  if (filename)
1795  {
1796  OPF = fopen(filename, PG_BINARY_A);
1797  if (!OPF)
1798  {
1799  fprintf(stderr, _("%s: could not re-open the output file \"%s\": %s\n"),
1800  progname, filename, strerror(errno));
1801  exit_nicely(1);
1802  }
1803  }
1804 
1805  }
1806 
1807  PQclear(res);
1808 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
static FILE * OPF
Definition: pg_dumpall.c:89
static const char * progname
Definition: pg_dumpall.c:62
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
static bool verbose
Definition: pg_dumpall.c:66
void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
Definition: string_utils.c:596
#define exit_nicely(code)
Definition: pg_dumpall.c:92
static int runPgDump(const char *dbname)
Definition: pg_dumpall.c:1816
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define PG_BINARY_A
Definition: c.h:1045
char * dbname
Definition: streamutil.c:42
static char * filename
Definition: pg_dumpall.c:90
int i
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static void dumpDbRoleConfig ( PGconn conn)
static

Definition at line 1672 of file pg_dumpall.c.

References buf, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), executeQuery(), i, makeAlterConfigCommand(), OPF, PQclear(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), and role_catalog.

Referenced by main().

1673 {
1675  PGresult *res;
1676  int i;
1677 
1678  printfPQExpBuffer(buf, "SELECT rolname, datname, unnest(setconfig) "
1679  "FROM pg_db_role_setting, %s u, pg_database "
1680  "WHERE setrole = u.oid AND setdatabase = pg_database.oid", role_catalog);
1681  res = executeQuery(conn, buf->data);
1682 
1683  if (PQntuples(res) > 0)
1684  {
1685  fprintf(OPF, "--\n-- Per-Database Role Settings \n--\n\n");
1686 
1687  for (i = 0; i < PQntuples(res); i++)
1688  {
1689  makeAlterConfigCommand(conn, PQgetvalue(res, i, 2),
1690  "ROLE", PQgetvalue(res, i, 0),
1691  "DATABASE", PQgetvalue(res, i, 1));
1692  }
1693 
1694  fprintf(OPF, "\n\n");
1695  }
1696 
1697  PQclear(res);
1698  destroyPQExpBuffer(buf);
1699 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
static FILE * OPF
Definition: pg_dumpall.c:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:67
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1706
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int i
static char role_catalog[10]
Definition: pg_dumpall.c:85
static void dumpGroups ( PGconn conn)
static

Definition at line 1032 of file pg_dumpall.c.

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

Referenced by main().

1033 {
1035  PGresult *res;
1036  int i;
1037 
1038  res = executeQuery(conn,
1039  "SELECT groname, grolist FROM pg_group ORDER BY 1");
1040 
1041  if (PQntuples(res) > 0)
1042  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1043 
1044  for (i = 0; i < PQntuples(res); i++)
1045  {
1046  char *groname = PQgetvalue(res, i, 0);
1047  char *grolist = PQgetvalue(res, i, 1);
1048  PGresult *res2;
1049  int j;
1050 
1051  /*
1052  * Array representation is {1,2,3} ... convert to (1,2,3)
1053  */
1054  if (strlen(grolist) < 3)
1055  continue;
1056 
1057  grolist = pg_strdup(grolist);
1058  grolist[0] = '(';
1059  grolist[strlen(grolist) - 1] = ')';
1060  printfPQExpBuffer(buf,
1061  "SELECT usename FROM pg_shadow "
1062  "WHERE usesysid IN %s ORDER BY 1",
1063  grolist);
1064  free(grolist);
1065 
1066  res2 = executeQuery(conn, buf->data);
1067 
1068  for (j = 0; j < PQntuples(res2); j++)
1069  {
1070  char *usename = PQgetvalue(res2, j, 0);
1071 
1072  /*
1073  * Don't try to grant a role to itself; can happen if old
1074  * installation has identically named user and group.
1075  */
1076  if (strcmp(groname, usename) == 0)
1077  continue;
1078 
1079  fprintf(OPF, "GRANT %s", fmtId(groname));
1080  fprintf(OPF, " TO %s;\n", fmtId(usename));
1081  }
1082 
1083  PQclear(res2);
1084  }
1085 
1086  PQclear(res);
1087  destroyPQExpBuffer(buf);
1088 
1089  fprintf(OPF, "\n\n");
1090 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
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:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
int i
static void dumpRoleMembership ( PGconn conn)
static

Definition at line 972 of file pg_dumpall.c.

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

Referenced by main().

973 {
975  PGresult *res;
976  int i;
977 
978  printfPQExpBuffer(buf, "SELECT ur.rolname AS roleid, "
979  "um.rolname AS member, "
980  "a.admin_option, "
981  "ug.rolname AS grantor "
982  "FROM pg_auth_members a "
983  "LEFT JOIN %s ur on ur.oid = a.roleid "
984  "LEFT JOIN %s um on um.oid = a.member "
985  "LEFT JOIN %s ug on ug.oid = a.grantor "
986  "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
987  "ORDER BY 1,2,3", role_catalog, role_catalog, role_catalog);
988  res = executeQuery(conn, buf->data);
989 
990  if (PQntuples(res) > 0)
991  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
992 
993  for (i = 0; i < PQntuples(res); i++)
994  {
995  char *roleid = PQgetvalue(res, i, 0);
996  char *member = PQgetvalue(res, i, 1);
997  char *option = PQgetvalue(res, i, 2);
998 
999  fprintf(OPF, "GRANT %s", fmtId(roleid));
1000  fprintf(OPF, " TO %s", fmtId(member));
1001  if (*option == 't')
1002  fprintf(OPF, " WITH ADMIN OPTION");
1003 
1004  /*
1005  * We don't track the grantor very carefully in the backend, so cope
1006  * with the possibility that it has been dropped.
1007  */
1008  if (!PQgetisnull(res, i, 3))
1009  {
1010  char *grantor = PQgetvalue(res, i, 3);
1011 
1012  fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
1013  }
1014  fprintf(OPF, ";\n");
1015  }
1016 
1017  PQclear(res);
1018  destroyPQExpBuffer(buf);
1019 
1020  fprintf(OPF, "\n\n");
1021 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:67
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int i
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
static char role_catalog[10]
Definition: pg_dumpall.c:85
static void dumpRoles ( PGconn conn)
static

Definition at line 715 of file pg_dumpall.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), atooid, binary_upgrade, buf, buildShSecLabels(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), dumpUserConfig(), executeQuery(), fmtId(), i, no_role_passwords, no_security_labels, OPF, PQclear(), PQfnumber(), PQgetisnull(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), progname, resetPQExpBuffer(), role_catalog, and server_version.

Referenced by main().

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

Definition at line 1132 of file pg_dumpall.c.

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

Referenced by main().

1133 {
1134  PGresult *res;
1135  int i;
1136 
1137  /*
1138  * Get all tablespaces except built-in ones (which we assume are named
1139  * pg_xxx)
1140  *
1141  * For the tablespace ACLs, as of 9.6, we extract both the positive (as
1142  * spcacl) and negative (as rspcacl) ACLs, relative to the default ACL for
1143  * tablespaces, which are then passed to buildACLCommands() below.
1144  *
1145  * See buildACLQueries() and buildACLCommands().
1146  *
1147  * Note that we do not support initial privileges (pg_init_privs) on
1148  * tablespaces.
1149  */
1150  if (server_version >= 90600)
1151  res = executeQuery(conn, "SELECT oid, spcname, "
1152  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1153  "pg_catalog.pg_tablespace_location(oid), "
1154  "(SELECT pg_catalog.array_agg(acl) FROM (SELECT pg_catalog.unnest(coalesce(spcacl,pg_catalog.acldefault('t',spcowner))) AS acl "
1155  "EXCEPT SELECT pg_catalog.unnest(pg_catalog.acldefault('t',spcowner))) as foo)"
1156  "AS spcacl,"
1157  "(SELECT pg_catalog.array_agg(acl) FROM (SELECT pg_catalog.unnest(pg_catalog.acldefault('t',spcowner)) AS acl "
1158  "EXCEPT SELECT pg_catalog.unnest(coalesce(spcacl,pg_catalog.acldefault('t',spcowner)))) as foo)"
1159  "AS rspcacl,"
1160  "array_to_string(spcoptions, ', '),"
1161  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1162  "FROM pg_catalog.pg_tablespace "
1163  "WHERE spcname !~ '^pg_' "
1164  "ORDER BY 1");
1165  else if (server_version >= 90200)
1166  res = executeQuery(conn, "SELECT oid, spcname, "
1167  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1168  "pg_catalog.pg_tablespace_location(oid), "
1169  "spcacl, '' as rspcacl, "
1170  "array_to_string(spcoptions, ', '),"
1171  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1172  "FROM pg_catalog.pg_tablespace "
1173  "WHERE spcname !~ '^pg_' "
1174  "ORDER BY 1");
1175  else if (server_version >= 90000)
1176  res = executeQuery(conn, "SELECT oid, spcname, "
1177  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1178  "spclocation, spcacl, '' as rspcacl, "
1179  "array_to_string(spcoptions, ', '),"
1180  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1181  "FROM pg_catalog.pg_tablespace "
1182  "WHERE spcname !~ '^pg_' "
1183  "ORDER BY 1");
1184  else if (server_version >= 80200)
1185  res = executeQuery(conn, "SELECT oid, spcname, "
1186  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1187  "spclocation, spcacl, '' as rspcacl, null, "
1188  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1189  "FROM pg_catalog.pg_tablespace "
1190  "WHERE spcname !~ '^pg_' "
1191  "ORDER BY 1");
1192  else
1193  res = executeQuery(conn, "SELECT oid, spcname, "
1194  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1195  "spclocation, spcacl, '' as rspcacl, "
1196  "null, null "
1197  "FROM pg_catalog.pg_tablespace "
1198  "WHERE spcname !~ '^pg_' "
1199  "ORDER BY 1");
1200 
1201  if (PQntuples(res) > 0)
1202  fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1203 
1204  for (i = 0; i < PQntuples(res); i++)
1205  {
1207  uint32 spcoid = atooid(PQgetvalue(res, i, 0));
1208  char *spcname = PQgetvalue(res, i, 1);
1209  char *spcowner = PQgetvalue(res, i, 2);
1210  char *spclocation = PQgetvalue(res, i, 3);
1211  char *spcacl = PQgetvalue(res, i, 4);
1212  char *rspcacl = PQgetvalue(res, i, 5);
1213  char *spcoptions = PQgetvalue(res, i, 6);
1214  char *spccomment = PQgetvalue(res, i, 7);
1215  char *fspcname;
1216 
1217  /* needed for buildACLCommands() */
1218  fspcname = pg_strdup(fmtId(spcname));
1219 
1220  appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
1221  appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
1222 
1223  appendPQExpBufferStr(buf, " LOCATION ");
1224  appendStringLiteralConn(buf, spclocation, conn);
1225  appendPQExpBufferStr(buf, ";\n");
1226 
1227  if (spcoptions && spcoptions[0] != '\0')
1228  appendPQExpBuffer(buf, "ALTER TABLESPACE %s SET (%s);\n",
1229  fspcname, spcoptions);
1230 
1231  if (!skip_acls &&
1232  !buildACLCommands(fspcname, NULL, "TABLESPACE", spcacl, rspcacl,
1233  spcowner, "", server_version, buf))
1234  {
1235  fprintf(stderr, _("%s: could not parse ACL list (%s) for tablespace \"%s\"\n"),
1236  progname, spcacl, fspcname);
1237  PQfinish(conn);
1238  exit_nicely(1);
1239  }
1240 
1241  if (spccomment && strlen(spccomment))
1242  {
1243  appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
1244  appendStringLiteralConn(buf, spccomment, conn);
1245  appendPQExpBufferStr(buf, ";\n");
1246  }
1247 
1248  if (!no_security_labels && server_version >= 90200)
1249  buildShSecLabels(conn, "pg_tablespace", spcoid,
1250  buf, "TABLESPACE", fspcname);
1251 
1252  fprintf(OPF, "%s", buf->data);
1253 
1254  free(fspcname);
1255  destroyPQExpBuffer(buf);
1256  }
1257 
1258  PQclear(res);
1259  fprintf(OPF, "\n\n");
1260 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
static void buildShSecLabels(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer buffer, const char *target, const char *objname)
Definition: pg_dumpall.c:1866
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:67
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:258
#define atooid(x)
Definition: postgres_ext.h:42
#define exit_nicely(code)
Definition: pg_dumpall.c:92
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
bool buildACLCommands(const char *name, const char *subname, const char *type, const char *acls, const char *racls, const char *owner, const char *prefix, int remoteVersion, PQExpBuffer sql)
Definition: dumputils.c:55
static int server_version
Definition: pg_dumpall.c:82
static bool skip_acls
Definition: pg_dumpall.c:65
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
int i
static int no_security_labels
Definition: pg_dumpall.c:78
#define _(x)
Definition: elog.c:84
static void dumpTimestamp ( const char *  msg)
static

Definition at line 2193 of file pg_dumpall.c.

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

Referenced by main().

2194 {
2195  char buf[64];
2196  time_t now = time(NULL);
2197 
2198  if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
2199  fprintf(OPF, "-- %s %s\n\n", msg, buf);
2200 }
static FILE * OPF
Definition: pg_dumpall.c:89
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:1534
static void dumpUserConfig ( PGconn conn,
const char *  username 
)
static

Definition at line 1627 of file pg_dumpall.c.

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

Referenced by dumpRoles().

1628 {
1630  int count = 1;
1631 
1632  for (;;)
1633  {
1634  PGresult *res;
1635 
1636  if (server_version >= 90000)
1637  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1638  "setdatabase = 0 AND setrole = "
1639  "(SELECT oid FROM %s WHERE rolname = ", count, role_catalog);
1640  else if (server_version >= 80100)
1641  printfPQExpBuffer(buf, "SELECT rolconfig[%d] FROM %s WHERE rolname = ", count, role_catalog);
1642  else
1643  printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
1644  appendStringLiteralConn(buf, username, conn);
1645  if (server_version >= 90000)
1646  appendPQExpBufferChar(buf, ')');
1647 
1648  res = executeQuery(conn, buf->data);
1649  if (PQntuples(res) == 1 &&
1650  !PQgetisnull(res, 0, 0))
1651  {
1652  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1653  "ROLE", username, NULL, NULL);
1654  PQclear(res);
1655  count++;
1656  }
1657  else
1658  {
1659  PQclear(res);
1660  break;
1661  }
1662  }
1663 
1664  destroyPQExpBuffer(buf);
1665 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2140
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:67
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1706
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
static char * username
Definition: initdb.c:132
void PQclear(PGresult *res)
Definition: fe-exec.c:671
static int server_version
Definition: pg_dumpall.c:82
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3143
static char role_catalog[10]
Definition: pg_dumpall.c:85
static void executeCommand ( PGconn conn,
const char *  query 
)
static

Definition at line 2166 of file pg_dumpall.c.

References _, exit_nicely, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), progname, and verbose.

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

2167 {
2168  PGresult *res;
2169 
2170  if (verbose)
2171  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2172 
2173  res = PQexec(conn, query);
2174  if (!res ||
2176  {
2177  fprintf(stderr, _("%s: query failed: %s"),
2178  progname, PQerrorMessage(conn));
2179  fprintf(stderr, _("%s: query was: %s\n"),
2180  progname, query);
2181  PQfinish(conn);
2182  exit_nicely(1);
2183  }
2184 
2185  PQclear(res);
2186 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static bool verbose
Definition: pg_dumpall.c:66
#define exit_nicely(code)
Definition: pg_dumpall.c:92
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84
static PGresult * executeQuery ( PGconn conn,
const char *  query 
)
static

Definition at line 2140 of file pg_dumpall.c.

References _, exit_nicely, PGRES_TUPLES_OK, PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), progname, and verbose.

Referenced by buildShSecLabels(), cluster_all_databases(), dropDBs(), dropRoles(), dropTablespaces(), dumpCreateDB(), dumpDatabaseConfig(), dumpDatabases(), dumpDbRoleConfig(), dumpGroups(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpUserConfig(), reindex_all_databases(), vacuum_all_databases(), and vacuum_one_database().

2141 {
2142  PGresult *res;
2143 
2144  if (verbose)
2145  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2146 
2147  res = PQexec(conn, query);
2148  if (!res ||
2150  {
2151  fprintf(stderr, _("%s: query failed: %s"),
2152  progname, PQerrorMessage(conn));
2153  fprintf(stderr, _("%s: query was: %s\n"),
2154  progname, query);
2155  PQfinish(conn);
2156  exit_nicely(1);
2157  }
2158 
2159  return res;
2160 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static bool verbose
Definition: pg_dumpall.c:66
#define exit_nicely(code)
Definition: pg_dumpall.c:92
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84
static void help ( void  )
static

Definition at line 597 of file pg_dumpall.c.

References _, and progname.

Referenced by main().

598 {
599  printf(_("%s extracts a PostgreSQL database cluster into an SQL script file.\n\n"), progname);
600  printf(_("Usage:\n"));
601  printf(_(" %s [OPTION]...\n"), progname);
602 
603  printf(_("\nGeneral options:\n"));
604  printf(_(" -f, --file=FILENAME output file name\n"));
605  printf(_(" -v, --verbose verbose mode\n"));
606  printf(_(" -V, --version output version information, then exit\n"));
607  printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
608  printf(_(" -?, --help show this help, then exit\n"));
609  printf(_("\nOptions controlling the output content:\n"));
610  printf(_(" -a, --data-only dump only the data, not the schema\n"));
611  printf(_(" -c, --clean clean (drop) databases before recreating\n"));
612  printf(_(" -E, --encoding=ENCODING dump the data in encoding ENCODING\n"));
613  printf(_(" -g, --globals-only dump only global objects, no databases\n"));
614  printf(_(" -o, --oids include OIDs in dump\n"));
615  printf(_(" -O, --no-owner skip restoration of object ownership\n"));
616  printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
617  printf(_(" -s, --schema-only dump only the schema, no data\n"));
618  printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
619  printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
620  printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
621  printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
622  printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
623  printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
624  printf(_(" --disable-triggers disable triggers during data-only restore\n"));
625  printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
626  printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
627  printf(_(" --no-publications do not dump publications\n"));
628  printf(_(" --no-role-passwords do not dump passwords for roles\n"));
629  printf(_(" --no-security-labels do not dump security label assignments\n"));
630  printf(_(" --no-subscriptions do not dump subscriptions\n"));
631  printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
632  printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
633  printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
634  printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
635  printf(_(" --load-via-partition-root load partitions via the root table\n"));
636  printf(_(" --use-set-session-authorization\n"
637  " use SET SESSION AUTHORIZATION commands instead of\n"
638  " ALTER OWNER commands to set ownership\n"));
639 
640  printf(_("\nConnection options:\n"));
641  printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
642  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
643  printf(_(" -l, --database=DBNAME alternative default database\n"));
644  printf(_(" -p, --port=PORT database server port number\n"));
645  printf(_(" -U, --username=NAME connect as specified database user\n"));
646  printf(_(" -w, --no-password never prompt for password\n"));
647  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
648  printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
649 
650  printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
651  "output.\n\n"));
652  printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
653 }
static const char * progname
Definition: pg_dumpall.c:62
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char *  argv[] 
)

Definition at line 95 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(), dumpCreateDB(), dumpDatabases(), dumpDbRoleConfig(), dumpGroups(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpTimestamp(), encoding, executeCommand(), exit_nicely, filename, find_my_exec(), find_other_exec(), fmtId(), fsync_fname(), get_progname(), getopt_long(), help(), if_exists, inserts, load_via_partition_root, MAXPGPATH, no_argument, no_publications, no_role_passwords, no_security_labels, no_subscriptions, no_tablespaces, no_unlogged_table_data, OPF, optarg, optind, PG_AUTHID, PG_BINARY_W, pg_dump_bin, pg_encoding_to_char(), 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(), skip_acls, strerror(), strlcpy(), TRI_DEFAULT, TRI_NO, TRI_YES, use_setsessauth, and verbose.

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

Definition at line 1706 of file pg_dumpall.c.

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), buf, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), fmtId(), free, OPF, pg_strcasecmp(), and pg_strdup().

Referenced by dumpDatabaseConfig(), dumpDbRoleConfig(), and dumpUserConfig().

1709 {
1710  char *pos;
1711  char *mine;
1712  PQExpBuffer buf;
1713 
1714  mine = pg_strdup(arrayitem);
1715  pos = strchr(mine, '=');
1716  if (pos == NULL)
1717  {
1718  free(mine);
1719  return;
1720  }
1721 
1722  buf = createPQExpBuffer();
1723 
1724  *pos = 0;
1725  appendPQExpBuffer(buf, "ALTER %s %s ", type, fmtId(name));
1726  if (type2 != NULL && name2 != NULL)
1727  appendPQExpBuffer(buf, "IN %s %s ", type2, fmtId(name2));
1728  appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
1729 
1730  /*
1731  * Some GUC variable names are 'LIST' type and hence must not be quoted.
1732  */
1733  if (pg_strcasecmp(mine, "DateStyle") == 0
1734  || pg_strcasecmp(mine, "search_path") == 0)
1735  appendPQExpBufferStr(buf, pos + 1);
1736  else
1737  appendStringLiteralConn(buf, pos + 1, conn);
1738  appendPQExpBufferStr(buf, ";\n");
1739 
1740  fprintf(OPF, "%s", buf->data);
1741  destroyPQExpBuffer(buf);
1742  free(mine);
1743 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
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:71
#define free(a)
Definition: header.h:65
const char * name
Definition: encode.c:521
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
static int runPgDump ( const char *  dbname)
static

Definition at line 1816 of file pg_dumpall.c.

References _, appendConnStrVal(), appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), connstr, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), filename, pg_dump_bin, progname, and verbose.

Referenced by dumpDatabases().

1817 {
1818  PQExpBuffer connstrbuf = createPQExpBuffer();
1820  int ret;
1821 
1822  appendPQExpBuffer(cmd, "\"%s\" %s", pg_dump_bin,
1823  pgdumpopts->data);
1824 
1825  /*
1826  * If we have a filename, use the undocumented plain-append pg_dump
1827  * format.
1828  */
1829  if (filename)
1830  appendPQExpBufferStr(cmd, " -Fa ");
1831  else
1832  appendPQExpBufferStr(cmd, " -Fp ");
1833 
1834  /*
1835  * Append the database name to the already-constructed stem of connection
1836  * string.
1837  */
1838  appendPQExpBuffer(connstrbuf, "%s dbname=", connstr);
1839  appendConnStrVal(connstrbuf, dbname);
1840 
1841  appendShellString(cmd, connstrbuf->data);
1842 
1843  if (verbose)
1844  fprintf(stderr, _("%s: running \"%s\"\n"), progname, cmd->data);
1845 
1846  fflush(stdout);
1847  fflush(stderr);
1848 
1849  ret = system(cmd->data);
1850 
1851  destroyPQExpBuffer(cmd);
1852  destroyPQExpBuffer(connstrbuf);
1853 
1854  return ret;
1855 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
static const char * progname
Definition: pg_dumpall.c:62
static bool verbose
Definition: pg_dumpall.c:66
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static PQExpBuffer pgdumpopts
Definition: pg_dumpall.c:63
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:434
static char pg_dump_bin[MAXPGPATH]
Definition: pg_dumpall.c:61
char * dbname
Definition: streamutil.c:42
static char * filename
Definition: pg_dumpall.c:90
#define _(x)
Definition: elog.c:84
static char * connstr
Definition: pg_dumpall.c:64

Variable Documentation

int binary_upgrade = 0
static

Definition at line 69 of file pg_dumpall.c.

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

int column_inserts = 0
static

Definition at line 70 of file pg_dumpall.c.

Referenced by main().

int disable_dollar_quoting = 0
static

Definition at line 71 of file pg_dumpall.c.

Referenced by main().

int disable_triggers = 0
static

Definition at line 72 of file pg_dumpall.c.

Referenced by main().

bool dosync = true
static

Definition at line 67 of file pg_dumpall.c.

Referenced by main().

int if_exists = 0
static

Definition at line 73 of file pg_dumpall.c.

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

int inserts = 0
static

Definition at line 74 of file pg_dumpall.c.

Referenced by main().

int load_via_partition_root = 0
static

Definition at line 83 of file pg_dumpall.c.

Referenced by main().

int no_publications = 0
static

Definition at line 77 of file pg_dumpall.c.

Referenced by main().

int no_role_passwords = 0
static

Definition at line 81 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

int no_security_labels = 0
static

Definition at line 78 of file pg_dumpall.c.

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

int no_subscriptions = 0
static

Definition at line 79 of file pg_dumpall.c.

Referenced by main().

int no_tablespaces = 0
static

Definition at line 75 of file pg_dumpall.c.

Referenced by dumpCreateDB(), and main().

int no_unlogged_table_data = 0
static

Definition at line 80 of file pg_dumpall.c.

Referenced by main().

char pg_dump_bin[MAXPGPATH]
static

Definition at line 61 of file pg_dumpall.c.

Referenced by main(), and runPgDump().

PQExpBuffer pgdumpopts
static

Definition at line 63 of file pg_dumpall.c.

const char* progname
static
char role_catalog[10]
static
bool skip_acls = false
static

Definition at line 65 of file pg_dumpall.c.

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

int use_setsessauth = 0
static

Definition at line 76 of file pg_dumpall.c.

Referenced by main().

bool verbose = false
static

Definition at line 66 of file pg_dumpall.c.

Referenced by dumpDatabases(), executeCommand(), executeQuery(), main(), and runPgDump().