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_security_labels = 0
 
static int no_unlogged_table_data = 0
 
static int no_role_passwords = 0
 
static int server_version
 
static char role_catalog [10]
 
static FILE * OPF
 
static char * filename = NULL
 

Macro Definition Documentation

#define PG_AUTHID   "pg_authid"

Definition at line 83 of file pg_dumpall.c.

Referenced by main().

#define PG_ROLES   "pg_roles "

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

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

Referenced by dumpRoles(), and dumpTablespaces().

1831 {
1833  PGresult *res;
1834 
1835  buildShSecLabelQuery(conn, catalog_name, objectId, sql);
1836  res = executeQuery(conn, sql->data);
1837  emitShSecLabels(conn, res, buffer, target, objname);
1838 
1839  PQclear(res);
1840  destroyPQExpBuffer(sql);
1841 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
void buildShSecLabelQuery(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer sql)
Definition: dumputils.c:638
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *target, const char *objname)
Definition: dumputils.c:654
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 1854 of file pg_dumpall.c.

References _, conn, CONNECTION_BAD, connstr, constructConnStr(), dbname, executeCommand(), exit_nicely, free, have_password, i, _PQconninfoOption::keyword, NULL, 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().

1857 {
1858  PGconn *conn;
1859  bool new_pass;
1860  const char *remoteversion_str;
1861  int my_version;
1862  const char **keywords = NULL;
1863  const char **values = NULL;
1864  PQconninfoOption *conn_opts = NULL;
1865  static bool have_password = false;
1866  static char password[100];
1867 
1868  if (prompt_password == TRI_YES && !have_password)
1869  {
1870  simple_prompt("Password: ", password, sizeof(password), false);
1871  have_password = true;
1872  }
1873 
1874  /*
1875  * Start the connection. Loop until we have a password if requested by
1876  * backend.
1877  */
1878  do
1879  {
1880  int argcount = 6;
1881  PQconninfoOption *conn_opt;
1882  char *err_msg = NULL;
1883  int i = 0;
1884 
1885  if (keywords)
1886  free(keywords);
1887  if (values)
1888  free(values);
1889  if (conn_opts)
1890  PQconninfoFree(conn_opts);
1891 
1892  /*
1893  * Merge the connection info inputs given in form of connection string
1894  * and other options. Explicitly discard any dbname value in the
1895  * connection string; otherwise, PQconnectdbParams() would interpret
1896  * that value as being itself a connection string.
1897  */
1898  if (connection_string)
1899  {
1900  conn_opts = PQconninfoParse(connection_string, &err_msg);
1901  if (conn_opts == NULL)
1902  {
1903  fprintf(stderr, "%s: %s", progname, err_msg);
1904  exit_nicely(1);
1905  }
1906 
1907  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1908  {
1909  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1910  strcmp(conn_opt->keyword, "dbname") != 0)
1911  argcount++;
1912  }
1913 
1914  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1915  values = pg_malloc0((argcount + 1) * sizeof(*values));
1916 
1917  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1918  {
1919  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1920  strcmp(conn_opt->keyword, "dbname") != 0)
1921  {
1922  keywords[i] = conn_opt->keyword;
1923  values[i] = conn_opt->val;
1924  i++;
1925  }
1926  }
1927  }
1928  else
1929  {
1930  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1931  values = pg_malloc0((argcount + 1) * sizeof(*values));
1932  }
1933 
1934  if (pghost)
1935  {
1936  keywords[i] = "host";
1937  values[i] = pghost;
1938  i++;
1939  }
1940  if (pgport)
1941  {
1942  keywords[i] = "port";
1943  values[i] = pgport;
1944  i++;
1945  }
1946  if (pguser)
1947  {
1948  keywords[i] = "user";
1949  values[i] = pguser;
1950  i++;
1951  }
1952  if (have_password)
1953  {
1954  keywords[i] = "password";
1955  values[i] = password;
1956  i++;
1957  }
1958  if (dbname)
1959  {
1960  keywords[i] = "dbname";
1961  values[i] = dbname;
1962  i++;
1963  }
1964  keywords[i] = "fallback_application_name";
1965  values[i] = progname;
1966  i++;
1967 
1968  new_pass = false;
1969  conn = PQconnectdbParams(keywords, values, true);
1970 
1971  if (!conn)
1972  {
1973  fprintf(stderr, _("%s: could not connect to database \"%s\"\n"),
1974  progname, dbname);
1975  exit_nicely(1);
1976  }
1977 
1978  if (PQstatus(conn) == CONNECTION_BAD &&
1979  PQconnectionNeedsPassword(conn) &&
1980  !have_password &&
1981  prompt_password != TRI_NO)
1982  {
1983  PQfinish(conn);
1984  simple_prompt("Password: ", password, sizeof(password), false);
1985  have_password = true;
1986  new_pass = true;
1987  }
1988  } while (new_pass);
1989 
1990  /* check to see that the backend connection was successfully made */
1991  if (PQstatus(conn) == CONNECTION_BAD)
1992  {
1993  if (fail_on_error)
1994  {
1995  fprintf(stderr,
1996  _("%s: could not connect to database \"%s\": %s\n"),
1997  progname, dbname, PQerrorMessage(conn));
1998  exit_nicely(1);
1999  }
2000  else
2001  {
2002  PQfinish(conn);
2003 
2004  free(keywords);
2005  free(values);
2006  PQconninfoFree(conn_opts);
2007 
2008  return NULL;
2009  }
2010  }
2011 
2012  /*
2013  * Ok, connected successfully. Remember the options used, in the form of a
2014  * connection string.
2015  */
2016  connstr = constructConnStr(keywords, values);
2017 
2018  free(keywords);
2019  free(values);
2020  PQconninfoFree(conn_opts);
2021 
2022  /* Check version */
2023  remoteversion_str = PQparameterStatus(conn, "server_version");
2024  if (!remoteversion_str)
2025  {
2026  fprintf(stderr, _("%s: could not get server version\n"), progname);
2027  exit_nicely(1);
2028  }
2030  if (server_version == 0)
2031  {
2032  fprintf(stderr, _("%s: could not parse server version \"%s\"\n"),
2033  progname, remoteversion_str);
2034  exit_nicely(1);
2035  }
2036 
2037  my_version = PG_VERSION_NUM;
2038 
2039  /*
2040  * We allow the server to be back to 8.0, and up to any minor release of
2041  * our own major version. (See also version check in pg_dump.c.)
2042  */
2043  if (my_version != server_version
2044  && (server_version < 80000 ||
2045  (server_version / 100) > (my_version / 100)))
2046  {
2047  fprintf(stderr, _("server version: %s; %s version: %s\n"),
2048  remoteversion_str, progname, PG_VERSION);
2049  fprintf(stderr, _("aborting because of server version mismatch\n"));
2050  exit_nicely(1);
2051  }
2052 
2053  /*
2054  * Make sure we are not fooled by non-system schemas in the search path.
2055  */
2056  executeCommand(conn, "SET search_path = pg_catalog");
2057 
2058  return conn;
2059 }
static char password[100]
Definition: streamutil.c:41
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5934
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:5899
static char * constructConnStr(const char **keywords, const char **values)
Definition: pg_dumpall.c:2072
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3491
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:5924
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:466
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:4578
char * connection_string
Definition: streamutil.c:34
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
PGconn * conn
Definition: streamutil.c:42
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2129
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:5784
char * pghost
Definition: pgbench.c:180
#define exit_nicely(code)
Definition: pg_dumpall.c:89
#define free(a)
Definition: header.h:65
static bool have_password
Definition: streamutil.c:40
#define NULL
Definition: c.h:229
static int server_version
Definition: pg_dumpall.c:80
char * dbname
Definition: streamutil.c:38
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:5968
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5881
#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 2072 of file pg_dumpall.c.

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

Referenced by connectDatabase().

2073 {
2075  char *connstr;
2076  int i;
2077  bool firstkeyword = true;
2078 
2079  /* Construct a new connection string in key='value' format. */
2080  for (i = 0; keywords[i] != NULL; i++)
2081  {
2082  if (strcmp(keywords[i], "dbname") == 0 ||
2083  strcmp(keywords[i], "password") == 0 ||
2084  strcmp(keywords[i], "fallback_application_name") == 0)
2085  continue;
2086 
2087  if (!firstkeyword)
2088  appendPQExpBufferChar(buf, ' ');
2089  firstkeyword = false;
2090  appendPQExpBuffer(buf, "%s=", keywords[i]);
2091  appendConnStrVal(buf, values[i]);
2092  }
2093 
2094  connstr = pg_strdup(buf->data);
2095  destroyPQExpBuffer(buf);
2096  return connstr;
2097 }
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:66
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
#define NULL
Definition: c.h:229
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int i
static char * connstr
Definition: pg_dumpall.c:64
static void dropDBs ( PGconn conn)
static

Definition at line 1232 of file pg_dumpall.c.

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

Referenced by main().

1233 {
1234  PGresult *res;
1235  int i;
1236 
1237  res = executeQuery(conn,
1238  "SELECT datname "
1239  "FROM pg_database d "
1240  "WHERE datallowconn ORDER BY 1");
1241 
1242  if (PQntuples(res) > 0)
1243  fprintf(OPF, "--\n-- Drop databases\n--\n\n");
1244 
1245  for (i = 0; i < PQntuples(res); i++)
1246  {
1247  char *dbname = PQgetvalue(res, i, 0);
1248 
1249  /*
1250  * Skip "template1" and "postgres"; the restore script is almost
1251  * certainly going to be run in one or the other, and we don't know
1252  * which. This must agree with dumpCreateDB's choices!
1253  */
1254  if (strcmp(dbname, "template1") != 0 &&
1255  strcmp(dbname, "postgres") != 0)
1256  {
1257  fprintf(OPF, "DROP DATABASE %s%s;\n",
1258  if_exists ? "IF EXISTS " : "",
1259  fmtId(dbname));
1260  }
1261  }
1262 
1263  PQclear(res);
1264 
1265  fprintf(OPF, "\n\n");
1266 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:86
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void PQclear(PGresult *res)
Definition: fe-exec.c:650
static int if_exists
Definition: pg_dumpall.c:73
char * dbname
Definition: streamutil.c:38
int i
static void dropRoles ( PGconn conn)
static

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

624 {
626  PGresult *res;
627  int i_rolname;
628  int i;
629 
630  if (server_version >= 90600)
631  printfPQExpBuffer(buf,
632  "SELECT rolname "
633  "FROM %s "
634  "WHERE rolname !~ '^pg_' "
635  "ORDER BY 1", role_catalog);
636  else if (server_version >= 80100)
637  printfPQExpBuffer(buf,
638  "SELECT rolname "
639  "FROM %s "
640  "ORDER BY 1", role_catalog);
641  else
642  printfPQExpBuffer(buf,
643  "SELECT usename as rolname "
644  "FROM pg_shadow "
645  "UNION "
646  "SELECT groname as rolname "
647  "FROM pg_group "
648  "ORDER BY 1");
649 
650  res = executeQuery(conn, buf->data);
651 
652  i_rolname = PQfnumber(res, "rolname");
653 
654  if (PQntuples(res) > 0)
655  fprintf(OPF, "--\n-- Drop roles\n--\n\n");
656 
657  for (i = 0; i < PQntuples(res); i++)
658  {
659  const char *rolename;
660 
661  rolename = PQgetvalue(res, i, i_rolname);
662 
663  fprintf(OPF, "DROP ROLE %s%s;\n",
664  if_exists ? "IF EXISTS " : "",
665  fmtId(rolename));
666  }
667 
668  PQclear(res);
669  destroyPQExpBuffer(buf);
670 
671  fprintf(OPF, "\n\n");
672 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
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:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:86
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:66
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void PQclear(PGresult *res)
Definition: fe-exec.c:650
static int if_exists
Definition: pg_dumpall.c:73
static int server_version
Definition: pg_dumpall.c:80
int i
static char role_catalog[10]
Definition: pg_dumpall.c:82
static void dropTablespaces ( PGconn conn)
static

Definition at line 1060 of file pg_dumpall.c.

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

Referenced by main().

1061 {
1062  PGresult *res;
1063  int i;
1064 
1065  /*
1066  * Get all tablespaces except built-in ones (which we assume are named
1067  * pg_xxx)
1068  */
1069  res = executeQuery(conn, "SELECT spcname "
1070  "FROM pg_catalog.pg_tablespace "
1071  "WHERE spcname !~ '^pg_' "
1072  "ORDER BY 1");
1073 
1074  if (PQntuples(res) > 0)
1075  fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1076 
1077  for (i = 0; i < PQntuples(res); i++)
1078  {
1079  char *spcname = PQgetvalue(res, i, 0);
1080 
1081  fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1082  if_exists ? "IF EXISTS " : "",
1083  fmtId(spcname));
1084  }
1085 
1086  PQclear(res);
1087 
1088  fprintf(OPF, "\n\n");
1089 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:86
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void PQclear(PGresult *res)
Definition: fe-exec.c:650
static int if_exists
Definition: pg_dumpall.c:73
int i
static void dumpCreateDB ( PGconn conn)
static

Definition at line 1280 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, NULL, OPF, pg_strdup(), PQclear(), PQfinish(), PQgetisnull(), PQgetvalue(), PQntuples(), printfPQExpBuffer(), progname, resetPQExpBuffer(), role_catalog, server_version, and skip_acls.

Referenced by main().

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

Definition at line 1546 of file pg_dumpall.c.

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

Referenced by dumpCreateDB().

1547 {
1549  int count = 1;
1550 
1551  for (;;)
1552  {
1553  PGresult *res;
1554 
1555  if (server_version >= 90000)
1556  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1557  "setrole = 0 AND setdatabase = (SELECT oid FROM pg_database WHERE datname = ", count);
1558  else
1559  printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE datname = ", count);
1560  appendStringLiteralConn(buf, dbname, conn);
1561 
1562  if (server_version >= 90000)
1563  appendPQExpBuffer(buf, ")");
1564 
1565  res = executeQuery(conn, buf->data);
1566  if (PQntuples(res) == 1 &&
1567  !PQgetisnull(res, 0, 0))
1568  {
1569  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1570  "DATABASE", dbname, NULL, NULL);
1571  PQclear(res);
1572  count++;
1573  }
1574  else
1575  {
1576  PQclear(res);
1577  break;
1578  }
1579  }
1580 
1581  destroyPQExpBuffer(buf);
1582 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
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:3067
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
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:66
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1669
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
static int server_version
Definition: pg_dumpall.c:80
char * dbname
Definition: streamutil.c:38
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:3092
static void dumpDatabases ( PGconn conn)
static

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

1715 {
1716  PGresult *res;
1717  int i;
1718 
1719  res = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1");
1720 
1721  for (i = 0; i < PQntuples(res); i++)
1722  {
1723  int ret;
1724 
1725  char *dbname = PQgetvalue(res, i, 0);
1726  PQExpBufferData connectbuf;
1727 
1728  if (verbose)
1729  fprintf(stderr, _("%s: dumping database \"%s\"...\n"), progname, dbname);
1730 
1731  initPQExpBuffer(&connectbuf);
1732  appendPsqlMetaConnect(&connectbuf, dbname);
1733  fprintf(OPF, "%s\n", connectbuf.data);
1734  termPQExpBuffer(&connectbuf);
1735 
1736  /*
1737  * Restore will need to write to the target cluster. This connection
1738  * setting is emitted for pg_dumpall rather than in the code also used
1739  * by pg_dump, so that a cluster with databases or users which have
1740  * this flag turned on can still be replicated through pg_dumpall
1741  * without editing the file or stream. With pg_dump there are many
1742  * other ways to allow the file to be used, and leaving it out allows
1743  * users to protect databases from being accidental restore targets.
1744  */
1745  fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
1746 
1747  if (filename)
1748  fclose(OPF);
1749 
1750  ret = runPgDump(dbname);
1751  if (ret != 0)
1752  {
1753  fprintf(stderr, _("%s: pg_dump failed on database \"%s\", exiting\n"), progname, dbname);
1754  exit_nicely(1);
1755  }
1756 
1757  if (filename)
1758  {
1759  OPF = fopen(filename, PG_BINARY_A);
1760  if (!OPF)
1761  {
1762  fprintf(stderr, _("%s: could not re-open the output file \"%s\": %s\n"),
1763  progname, filename, strerror(errno));
1764  exit_nicely(1);
1765  }
1766  }
1767 
1768  }
1769 
1770  PQclear(res);
1771 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
static FILE * OPF
Definition: pg_dumpall.c:86
static const char * progname
Definition: pg_dumpall.c:62
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
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:89
static int runPgDump(const char *dbname)
Definition: pg_dumpall.c:1779
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define PG_BINARY_A
Definition: c.h:1039
char * dbname
Definition: streamutil.c:38
static char * filename
Definition: pg_dumpall.c:87
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 1635 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().

1636 {
1638  PGresult *res;
1639  int i;
1640 
1641  printfPQExpBuffer(buf, "SELECT rolname, datname, unnest(setconfig) "
1642  "FROM pg_db_role_setting, %s u, pg_database "
1643  "WHERE setrole = u.oid AND setdatabase = pg_database.oid", role_catalog);
1644  res = executeQuery(conn, buf->data);
1645 
1646  if (PQntuples(res) > 0)
1647  {
1648  fprintf(OPF, "--\n-- Per-Database Role Settings \n--\n\n");
1649 
1650  for (i = 0; i < PQntuples(res); i++)
1651  {
1652  makeAlterConfigCommand(conn, PQgetvalue(res, i, 2),
1653  "ROLE", PQgetvalue(res, i, 0),
1654  "DATABASE", PQgetvalue(res, i, 1));
1655  }
1656 
1657  fprintf(OPF, "\n\n");
1658  }
1659 
1660  PQclear(res);
1661  destroyPQExpBuffer(buf);
1662 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
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:3067
static FILE * OPF
Definition: pg_dumpall.c:86
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:66
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1669
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
int i
static char role_catalog[10]
Definition: pg_dumpall.c:82
static void dumpGroups ( PGconn conn)
static

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

996 {
998  PGresult *res;
999  int i;
1000 
1001  res = executeQuery(conn,
1002  "SELECT groname, grolist FROM pg_group ORDER BY 1");
1003 
1004  if (PQntuples(res) > 0)
1005  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1006 
1007  for (i = 0; i < PQntuples(res); i++)
1008  {
1009  char *groname = PQgetvalue(res, i, 0);
1010  char *grolist = PQgetvalue(res, i, 1);
1011  PGresult *res2;
1012  int j;
1013 
1014  /*
1015  * Array representation is {1,2,3} ... convert to (1,2,3)
1016  */
1017  if (strlen(grolist) < 3)
1018  continue;
1019 
1020  grolist = pg_strdup(grolist);
1021  grolist[0] = '(';
1022  grolist[strlen(grolist) - 1] = ')';
1023  printfPQExpBuffer(buf,
1024  "SELECT usename FROM pg_shadow "
1025  "WHERE usesysid IN %s ORDER BY 1",
1026  grolist);
1027  free(grolist);
1028 
1029  res2 = executeQuery(conn, buf->data);
1030 
1031  for (j = 0; j < PQntuples(res2); j++)
1032  {
1033  char *usename = PQgetvalue(res2, j, 0);
1034 
1035  /*
1036  * Don't try to grant a role to itself; can happen if old
1037  * installation has identically named user and group.
1038  */
1039  if (strcmp(groname, usename) == 0)
1040  continue;
1041 
1042  fprintf(OPF, "GRANT %s", fmtId(groname));
1043  fprintf(OPF, " TO %s;\n", fmtId(usename));
1044  }
1045 
1046  PQclear(res2);
1047  }
1048 
1049  PQclear(res);
1050  destroyPQExpBuffer(buf);
1051 
1052  fprintf(OPF, "\n\n");
1053 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
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:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:86
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:66
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:650
#define free(a)
Definition: header.h:65
int i
static void dumpRoleMembership ( PGconn conn)
static

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

936 {
938  PGresult *res;
939  int i;
940 
941  printfPQExpBuffer(buf, "SELECT ur.rolname AS roleid, "
942  "um.rolname AS member, "
943  "a.admin_option, "
944  "ug.rolname AS grantor "
945  "FROM pg_auth_members a "
946  "LEFT JOIN %s ur on ur.oid = a.roleid "
947  "LEFT JOIN %s um on um.oid = a.member "
948  "LEFT JOIN %s ug on ug.oid = a.grantor "
949  "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
950  "ORDER BY 1,2,3", role_catalog, role_catalog, role_catalog);
951  res = executeQuery(conn, buf->data);
952 
953  if (PQntuples(res) > 0)
954  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
955 
956  for (i = 0; i < PQntuples(res); i++)
957  {
958  char *roleid = PQgetvalue(res, i, 0);
959  char *member = PQgetvalue(res, i, 1);
960  char *option = PQgetvalue(res, i, 2);
961 
962  fprintf(OPF, "GRANT %s", fmtId(roleid));
963  fprintf(OPF, " TO %s", fmtId(member));
964  if (*option == 't')
965  fprintf(OPF, " WITH ADMIN OPTION");
966 
967  /*
968  * We don't track the grantor very carefully in the backend, so cope
969  * with the possibility that it has been dropped.
970  */
971  if (!PQgetisnull(res, i, 3))
972  {
973  char *grantor = PQgetvalue(res, i, 3);
974 
975  fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
976  }
977  fprintf(OPF, ";\n");
978  }
979 
980  PQclear(res);
981  destroyPQExpBuffer(buf);
982 
983  fprintf(OPF, "\n\n");
984 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
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:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:86
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:66
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
int i
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
static char role_catalog[10]
Definition: pg_dumpall.c:82
static void dumpRoles ( PGconn conn)
static

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

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

Definition at line 1095 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, NULL, OPF, pg_strdup(), PQclear(), PQfinish(), PQgetvalue(), PQntuples(), progname, server_version, and skip_acls.

Referenced by main().

1096 {
1097  PGresult *res;
1098  int i;
1099 
1100  /*
1101  * Get all tablespaces except built-in ones (which we assume are named
1102  * pg_xxx)
1103  *
1104  * For the tablespace ACLs, as of 9.6, we extract both the positive (as
1105  * spcacl) and negative (as rspcacl) ACLs, relative to the default ACL for
1106  * tablespaces, which are then passed to buildACLCommands() below.
1107  *
1108  * See buildACLQueries() and buildACLCommands().
1109  *
1110  * Note that we do not support initial privileges (pg_init_privs) on
1111  * tablespaces.
1112  */
1113  if (server_version >= 90600)
1114  res = executeQuery(conn, "SELECT oid, spcname, "
1115  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1116  "pg_catalog.pg_tablespace_location(oid), "
1117  "(SELECT pg_catalog.array_agg(acl) FROM (SELECT pg_catalog.unnest(coalesce(spcacl,pg_catalog.acldefault('t',spcowner))) AS acl "
1118  "EXCEPT SELECT pg_catalog.unnest(pg_catalog.acldefault('t',spcowner))) as foo)"
1119  "AS spcacl,"
1120  "(SELECT pg_catalog.array_agg(acl) FROM (SELECT pg_catalog.unnest(pg_catalog.acldefault('t',spcowner)) AS acl "
1121  "EXCEPT SELECT pg_catalog.unnest(coalesce(spcacl,pg_catalog.acldefault('t',spcowner)))) as foo)"
1122  "AS rspcacl,"
1123  "array_to_string(spcoptions, ', '),"
1124  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1125  "FROM pg_catalog.pg_tablespace "
1126  "WHERE spcname !~ '^pg_' "
1127  "ORDER BY 1");
1128  else if (server_version >= 90200)
1129  res = executeQuery(conn, "SELECT oid, spcname, "
1130  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1131  "pg_catalog.pg_tablespace_location(oid), "
1132  "spcacl, '' as rspcacl, "
1133  "array_to_string(spcoptions, ', '),"
1134  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1135  "FROM pg_catalog.pg_tablespace "
1136  "WHERE spcname !~ '^pg_' "
1137  "ORDER BY 1");
1138  else if (server_version >= 90000)
1139  res = executeQuery(conn, "SELECT oid, spcname, "
1140  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1141  "spclocation, spcacl, '' as rspcacl, "
1142  "array_to_string(spcoptions, ', '),"
1143  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1144  "FROM pg_catalog.pg_tablespace "
1145  "WHERE spcname !~ '^pg_' "
1146  "ORDER BY 1");
1147  else if (server_version >= 80200)
1148  res = executeQuery(conn, "SELECT oid, spcname, "
1149  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1150  "spclocation, spcacl, '' as rspcacl, null, "
1151  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1152  "FROM pg_catalog.pg_tablespace "
1153  "WHERE spcname !~ '^pg_' "
1154  "ORDER BY 1");
1155  else
1156  res = executeQuery(conn, "SELECT oid, spcname, "
1157  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1158  "spclocation, spcacl, '' as rspcacl, "
1159  "null, null "
1160  "FROM pg_catalog.pg_tablespace "
1161  "WHERE spcname !~ '^pg_' "
1162  "ORDER BY 1");
1163 
1164  if (PQntuples(res) > 0)
1165  fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1166 
1167  for (i = 0; i < PQntuples(res); i++)
1168  {
1170  uint32 spcoid = atooid(PQgetvalue(res, i, 0));
1171  char *spcname = PQgetvalue(res, i, 1);
1172  char *spcowner = PQgetvalue(res, i, 2);
1173  char *spclocation = PQgetvalue(res, i, 3);
1174  char *spcacl = PQgetvalue(res, i, 4);
1175  char *rspcacl = PQgetvalue(res, i, 5);
1176  char *spcoptions = PQgetvalue(res, i, 6);
1177  char *spccomment = PQgetvalue(res, i, 7);
1178  char *fspcname;
1179 
1180  /* needed for buildACLCommands() */
1181  fspcname = pg_strdup(fmtId(spcname));
1182 
1183  appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
1184  appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
1185 
1186  appendPQExpBufferStr(buf, " LOCATION ");
1187  appendStringLiteralConn(buf, spclocation, conn);
1188  appendPQExpBufferStr(buf, ";\n");
1189 
1190  if (spcoptions && spcoptions[0] != '\0')
1191  appendPQExpBuffer(buf, "ALTER TABLESPACE %s SET (%s);\n",
1192  fspcname, spcoptions);
1193 
1194  if (!skip_acls &&
1195  !buildACLCommands(fspcname, NULL, "TABLESPACE", spcacl, rspcacl,
1196  spcowner, "", server_version, buf))
1197  {
1198  fprintf(stderr, _("%s: could not parse ACL list (%s) for tablespace \"%s\"\n"),
1199  progname, spcacl, fspcname);
1200  PQfinish(conn);
1201  exit_nicely(1);
1202  }
1203 
1204  if (spccomment && strlen(spccomment))
1205  {
1206  appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
1207  appendStringLiteralConn(buf, spccomment, conn);
1208  appendPQExpBufferStr(buf, ";\n");
1209  }
1210 
1211  if (!no_security_labels && server_version >= 90200)
1212  buildShSecLabels(conn, "pg_tablespace", spcoid,
1213  buf, "TABLESPACE", fspcname);
1214 
1215  fprintf(OPF, "%s", buf->data);
1216 
1217  free(fspcname);
1218  destroyPQExpBuffer(buf);
1219  }
1220 
1221  PQclear(res);
1222  fprintf(OPF, "\n\n");
1223 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:86
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:3491
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
static void buildShSecLabels(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer buffer, const char *target, const char *objname)
Definition: pg_dumpall.c:1829
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:66
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:268
#define atooid(x)
Definition: postgres_ext.h:42
#define exit_nicely(code)
Definition: pg_dumpall.c:89
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
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:80
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:77
#define _(x)
Definition: elog.c:84
static void dumpTimestamp ( const char *  msg)
static

Definition at line 2156 of file pg_dumpall.c.

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

Referenced by main().

2157 {
2158  char buf[64];
2159  time_t now = time(NULL);
2160 
2161  if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
2162  fprintf(OPF, "-- %s %s\n\n", msg, buf);
2163 }
static FILE * OPF
Definition: pg_dumpall.c:86
static char * buf
Definition: pg_test_fsync.c:66
#define NULL
Definition: c.h:229
#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 1590 of file pg_dumpall.c.

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

Referenced by dumpRoles().

1591 {
1593  int count = 1;
1594 
1595  for (;;)
1596  {
1597  PGresult *res;
1598 
1599  if (server_version >= 90000)
1600  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1601  "setdatabase = 0 AND setrole = "
1602  "(SELECT oid FROM %s WHERE rolname = ", count, role_catalog);
1603  else if (server_version >= 80100)
1604  printfPQExpBuffer(buf, "SELECT rolconfig[%d] FROM %s WHERE rolname = ", count, role_catalog);
1605  else
1606  printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
1607  appendStringLiteralConn(buf, username, conn);
1608  if (server_version >= 90000)
1609  appendPQExpBufferChar(buf, ')');
1610 
1611  res = executeQuery(conn, buf->data);
1612  if (PQntuples(res) == 1 &&
1613  !PQgetisnull(res, 0, 0))
1614  {
1615  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1616  "ROLE", username, NULL, NULL);
1617  PQclear(res);
1618  count++;
1619  }
1620  else
1621  {
1622  PQclear(res);
1623  break;
1624  }
1625  }
1626 
1627  destroyPQExpBuffer(buf);
1628 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2103
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:3067
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
static char * buf
Definition: pg_test_fsync.c:66
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1669
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
static char * username
Definition: initdb.c:131
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
static int server_version
Definition: pg_dumpall.c:80
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:3092
static char role_catalog[10]
Definition: pg_dumpall.c:82
static void executeCommand ( PGconn conn,
const char *  query 
)
static

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

2130 {
2131  PGresult *res;
2132 
2133  if (verbose)
2134  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2135 
2136  res = PQexec(conn, query);
2137  if (!res ||
2139  {
2140  fprintf(stderr, _("%s: query failed: %s"),
2141  progname, PQerrorMessage(conn));
2142  fprintf(stderr, _("%s: query was: %s\n"),
2143  progname, query);
2144  PQfinish(conn);
2145  exit_nicely(1);
2146  }
2147 
2148  PQclear(res);
2149 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5934
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3491
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static bool verbose
Definition: pg_dumpall.c:66
#define exit_nicely(code)
Definition: pg_dumpall.c:89
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
#define _(x)
Definition: elog.c:84
static PGresult * executeQuery ( PGconn conn,
const char *  query 
)
static

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

2104 {
2105  PGresult *res;
2106 
2107  if (verbose)
2108  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2109 
2110  res = PQexec(conn, query);
2111  if (!res ||
2113  {
2114  fprintf(stderr, _("%s: query failed: %s"),
2115  progname, PQerrorMessage(conn));
2116  fprintf(stderr, _("%s: query was: %s\n"),
2117  progname, query);
2118  PQfinish(conn);
2119  exit_nicely(1);
2120  }
2121 
2122  return res;
2123 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5934
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3491
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static bool verbose
Definition: pg_dumpall.c:66
#define exit_nicely(code)
Definition: pg_dumpall.c:89
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
#define _(x)
Definition: elog.c:84
static void help ( void  )
static

Definition at line 564 of file pg_dumpall.c.

References _, and progname.

Referenced by main().

565 {
566  printf(_("%s extracts a PostgreSQL database cluster into an SQL script file.\n\n"), progname);
567  printf(_("Usage:\n"));
568  printf(_(" %s [OPTION]...\n"), progname);
569 
570  printf(_("\nGeneral options:\n"));
571  printf(_(" -f, --file=FILENAME output file name\n"));
572  printf(_(" -v, --verbose verbose mode\n"));
573  printf(_(" -V, --version output version information, then exit\n"));
574  printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
575  printf(_(" -?, --help show this help, then exit\n"));
576  printf(_("\nOptions controlling the output content:\n"));
577  printf(_(" -a, --data-only dump only the data, not the schema\n"));
578  printf(_(" -c, --clean clean (drop) databases before recreating\n"));
579  printf(_(" -g, --globals-only dump only global objects, no databases\n"));
580  printf(_(" -o, --oids include OIDs in dump\n"));
581  printf(_(" -O, --no-owner skip restoration of object ownership\n"));
582  printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
583  printf(_(" -s, --schema-only dump only the schema, no data\n"));
584  printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
585  printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
586  printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
587  printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
588  printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
589  printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
590  printf(_(" --disable-triggers disable triggers during data-only restore\n"));
591  printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
592  printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
593  printf(_(" --no-security-labels do not dump security label assignments\n"));
594  printf(_(" --no-sync do not wait for changes to be written safely to disk\n"));
595  printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
596  printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
597  printf(_(" --no-role-passwords do not dump passwords for roles\n"));
598  printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
599  printf(_(" --use-set-session-authorization\n"
600  " use SET SESSION AUTHORIZATION commands instead of\n"
601  " ALTER OWNER commands to set ownership\n"));
602 
603  printf(_("\nConnection options:\n"));
604  printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
605  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
606  printf(_(" -l, --database=DBNAME alternative default database\n"));
607  printf(_(" -p, --port=PORT database server port number\n"));
608  printf(_(" -U, --username=NAME connect as specified database user\n"));
609  printf(_(" -w, --no-password never prompt for password\n"));
610  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
611  printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
612 
613  printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
614  "output.\n\n"));
615  printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
616 }
static const char * progname
Definition: pg_dumpall.c:62
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char *  argv[] 
)

Definition at line 92 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, MAXPGPATH, no_argument, no_role_passwords, no_security_labels, no_tablespaces, no_unlogged_table_data, NULL, 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(), 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.

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

Definition at line 1669 of file pg_dumpall.c.

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

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

1672 {
1673  char *pos;
1674  char *mine;
1675  PQExpBuffer buf;
1676 
1677  mine = pg_strdup(arrayitem);
1678  pos = strchr(mine, '=');
1679  if (pos == NULL)
1680  {
1681  free(mine);
1682  return;
1683  }
1684 
1685  buf = createPQExpBuffer();
1686 
1687  *pos = 0;
1688  appendPQExpBuffer(buf, "ALTER %s %s ", type, fmtId(name));
1689  if (type2 != NULL && name2 != NULL)
1690  appendPQExpBuffer(buf, "IN %s %s ", type2, fmtId(name2));
1691  appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
1692 
1693  /*
1694  * Some GUC variable names are 'LIST' type and hence must not be quoted.
1695  */
1696  if (pg_strcasecmp(mine, "DateStyle") == 0
1697  || pg_strcasecmp(mine, "search_path") == 0)
1698  appendPQExpBufferStr(buf, pos + 1);
1699  else
1700  appendStringLiteralConn(buf, pos + 1, conn);
1701  appendPQExpBufferStr(buf, ";\n");
1702 
1703  fprintf(OPF, "%s", buf->data);
1704  destroyPQExpBuffer(buf);
1705  free(mine);
1706 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:86
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:66
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
#define NULL
Definition: c.h:229
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 1779 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().

1780 {
1781  PQExpBuffer connstrbuf = createPQExpBuffer();
1783  int ret;
1784 
1785  appendPQExpBuffer(cmd, "\"%s\" %s", pg_dump_bin,
1786  pgdumpopts->data);
1787 
1788  /*
1789  * If we have a filename, use the undocumented plain-append pg_dump
1790  * format.
1791  */
1792  if (filename)
1793  appendPQExpBufferStr(cmd, " -Fa ");
1794  else
1795  appendPQExpBufferStr(cmd, " -Fp ");
1796 
1797  /*
1798  * Append the database name to the already-constructed stem of connection
1799  * string.
1800  */
1801  appendPQExpBuffer(connstrbuf, "%s dbname=", connstr);
1802  appendConnStrVal(connstrbuf, dbname);
1803 
1804  appendShellString(cmd, connstrbuf->data);
1805 
1806  if (verbose)
1807  fprintf(stderr, _("%s: running \"%s\"\n"), progname, cmd->data);
1808 
1809  fflush(stdout);
1810  fflush(stderr);
1811 
1812  ret = system(cmd->data);
1813 
1814  destroyPQExpBuffer(cmd);
1815  destroyPQExpBuffer(connstrbuf);
1816 
1817  return ret;
1818 }
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:38
static char * filename
Definition: pg_dumpall.c:87
#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 no_role_passwords = 0
static

Definition at line 79 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

int no_security_labels = 0
static

Definition at line 77 of file pg_dumpall.c.

Referenced by dumpRoles(), dumpTablespaces(), and 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 78 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().