PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_dumpall.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include <unistd.h>
#include "getopt_long.h"
#include "dumputils.h"
#include "pg_backup.h"
#include "common/file_utils.h"
#include "fe_utils/string_utils.h"
Include dependency graph for pg_dumpall.c:

Go to the source code of this file.

Macros

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

Functions

static void help (void)
 
static void dropRoles (PGconn *conn)
 
static void dumpRoles (PGconn *conn)
 
static void dumpRoleMembership (PGconn *conn)
 
static void dumpGroups (PGconn *conn)
 
static void dropTablespaces (PGconn *conn)
 
static void dumpTablespaces (PGconn *conn)
 
static void dropDBs (PGconn *conn)
 
static void dumpCreateDB (PGconn *conn)
 
static void dumpDatabaseConfig (PGconn *conn, const char *dbname)
 
static void dumpUserConfig (PGconn *conn, const char *username)
 
static void dumpDbRoleConfig (PGconn *conn)
 
static void makeAlterConfigCommand (PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
 
static void dumpDatabases (PGconn *conn)
 
static void dumpTimestamp (const char *msg)
 
static int runPgDump (const char *dbname)
 
static void buildShSecLabels (PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer buffer, const char *target, const char *objname)
 
static PGconnconnectDatabase (const char *dbname, const char *connstr, const char *pghost, const char *pgport, const char *pguser, trivalue prompt_password, bool fail_on_error)
 
static char * constructConnStr (const char **keywords, const char **values)
 
static PGresultexecuteQuery (PGconn *conn, const char *query)
 
static void executeCommand (PGconn *conn, const char *query)
 
int main (int argc, char *argv[])
 

Variables

static char pg_dump_bin [MAXPGPATH]
 
static const char * progname
 
static PQExpBuffer pgdumpopts
 
static char * connstr = ""
 
static bool skip_acls = false
 
static bool verbose = false
 
static bool dosync = true
 
static int binary_upgrade = 0
 
static int column_inserts = 0
 
static int disable_dollar_quoting = 0
 
static int disable_triggers = 0
 
static int if_exists = 0
 
static int inserts = 0
 
static int no_tablespaces = 0
 
static int use_setsessauth = 0
 
static int no_publications = 0
 
static int no_security_labels = 0
 
static int no_subscriptions = 0
 
static int no_unlogged_table_data = 0
 
static int no_role_passwords = 0
 
static int server_version
 
static int load_via_partition_root = 0
 
static char role_catalog [10]
 
static FILE * OPF
 
static char * filename = NULL
 

Macro Definition Documentation

#define PG_AUTHID   "pg_authid"

Definition at line 86 of file pg_dumpall.c.

Referenced by main().

#define PG_ROLES   "pg_roles "

Definition at line 87 of file pg_dumpall.c.

Referenced by main().

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

Definition at line 29 of file pg_dumpall.c.

Referenced by main().

Function Documentation

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

Definition at line 1844 of file pg_dumpall.c.

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

Referenced by dumpRoles(), and dumpTablespaces().

1846 {
1848  PGresult *res;
1849 
1850  buildShSecLabelQuery(conn, catalog_name, objectId, sql);
1851  res = executeQuery(conn, sql->data);
1852  emitShSecLabels(conn, res, buffer, target, objname);
1853 
1854  PQclear(res);
1855  destroyPQExpBuffer(sql);
1856 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
void buildShSecLabelQuery(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer sql)
Definition: dumputils.c:643
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
void emitShSecLabels(PGconn *conn, PGresult *res, PQExpBuffer buffer, const char *target, const char *objname)
Definition: dumputils.c:659
static PGconn * connectDatabase ( const char *  dbname,
const char *  connstr,
const char *  pghost,
const char *  pgport,
const char *  pguser,
trivalue  prompt_password,
bool  fail_on_error 
)
static

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

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

Definition at line 2087 of file pg_dumpall.c.

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

Referenced by connectDatabase().

2088 {
2090  char *connstr;
2091  int i;
2092  bool firstkeyword = true;
2093 
2094  /* Construct a new connection string in key='value' format. */
2095  for (i = 0; keywords[i] != NULL; i++)
2096  {
2097  if (strcmp(keywords[i], "dbname") == 0 ||
2098  strcmp(keywords[i], "password") == 0 ||
2099  strcmp(keywords[i], "fallback_application_name") == 0)
2100  continue;
2101 
2102  if (!firstkeyword)
2103  appendPQExpBufferChar(buf, ' ');
2104  firstkeyword = false;
2105  appendPQExpBuffer(buf, "%s=", keywords[i]);
2106  appendConnStrVal(buf, values[i]);
2107  }
2108 
2109  connstr = pg_strdup(buf->data);
2110  destroyPQExpBuffer(buf);
2111  return connstr;
2112 }
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 1247 of file pg_dumpall.c.

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

Referenced by main().

1248 {
1249  PGresult *res;
1250  int i;
1251 
1252  res = executeQuery(conn,
1253  "SELECT datname "
1254  "FROM pg_database d "
1255  "WHERE datallowconn ORDER BY 1");
1256 
1257  if (PQntuples(res) > 0)
1258  fprintf(OPF, "--\n-- Drop databases\n--\n\n");
1259 
1260  for (i = 0; i < PQntuples(res); i++)
1261  {
1262  char *dbname = PQgetvalue(res, i, 0);
1263 
1264  /*
1265  * Skip "template1" and "postgres"; the restore script is almost
1266  * certainly going to be run in one or the other, and we don't know
1267  * which. This must agree with dumpCreateDB's choices!
1268  */
1269  if (strcmp(dbname, "template1") != 0 &&
1270  strcmp(dbname, "postgres") != 0)
1271  {
1272  fprintf(OPF, "DROP DATABASE %s%s;\n",
1273  if_exists ? "IF EXISTS " : "",
1274  fmtId(dbname));
1275  }
1276  }
1277 
1278  PQclear(res);
1279 
1280  fprintf(OPF, "\n\n");
1281 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:89
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:39
int i
static void dropRoles ( PGconn conn)
static

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

639 {
641  PGresult *res;
642  int i_rolname;
643  int i;
644 
645  if (server_version >= 90600)
646  printfPQExpBuffer(buf,
647  "SELECT rolname "
648  "FROM %s "
649  "WHERE rolname !~ '^pg_' "
650  "ORDER BY 1", role_catalog);
651  else if (server_version >= 80100)
652  printfPQExpBuffer(buf,
653  "SELECT rolname "
654  "FROM %s "
655  "ORDER BY 1", role_catalog);
656  else
657  printfPQExpBuffer(buf,
658  "SELECT usename as rolname "
659  "FROM pg_shadow "
660  "UNION "
661  "SELECT groname as rolname "
662  "FROM pg_group "
663  "ORDER BY 1");
664 
665  res = executeQuery(conn, buf->data);
666 
667  i_rolname = PQfnumber(res, "rolname");
668 
669  if (PQntuples(res) > 0)
670  fprintf(OPF, "--\n-- Drop roles\n--\n\n");
671 
672  for (i = 0; i < PQntuples(res); i++)
673  {
674  const char *rolename;
675 
676  rolename = PQgetvalue(res, i, i_rolname);
677 
678  fprintf(OPF, "DROP ROLE %s%s;\n",
679  if_exists ? "IF EXISTS " : "",
680  fmtId(rolename));
681  }
682 
683  PQclear(res);
684  destroyPQExpBuffer(buf);
685 
686  fprintf(OPF, "\n\n");
687 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:89
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:82
int i
static char role_catalog[10]
Definition: pg_dumpall.c:85
static void dropTablespaces ( PGconn conn)
static

Definition at line 1075 of file pg_dumpall.c.

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

Referenced by main().

1076 {
1077  PGresult *res;
1078  int i;
1079 
1080  /*
1081  * Get all tablespaces except built-in ones (which we assume are named
1082  * pg_xxx)
1083  */
1084  res = executeQuery(conn, "SELECT spcname "
1085  "FROM pg_catalog.pg_tablespace "
1086  "WHERE spcname !~ '^pg_' "
1087  "ORDER BY 1");
1088 
1089  if (PQntuples(res) > 0)
1090  fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1091 
1092  for (i = 0; i < PQntuples(res); i++)
1093  {
1094  char *spcname = PQgetvalue(res, i, 0);
1095 
1096  fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1097  if_exists ? "IF EXISTS " : "",
1098  fmtId(spcname));
1099  }
1100 
1101  PQclear(res);
1102 
1103  fprintf(OPF, "\n\n");
1104 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:89
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 1295 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().

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

Definition at line 1561 of file pg_dumpall.c.

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

Referenced by dumpCreateDB().

1562 {
1564  int count = 1;
1565 
1566  for (;;)
1567  {
1568  PGresult *res;
1569 
1570  if (server_version >= 90000)
1571  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1572  "setrole = 0 AND setdatabase = (SELECT oid FROM pg_database WHERE datname = ", count);
1573  else
1574  printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE datname = ", count);
1575  appendStringLiteralConn(buf, dbname, conn);
1576 
1577  if (server_version >= 90000)
1578  appendPQExpBufferChar(buf, ')');
1579 
1580  res = executeQuery(conn, buf->data);
1581  if (PQntuples(res) == 1 &&
1582  !PQgetisnull(res, 0, 0))
1583  {
1584  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1585  "DATABASE", dbname, NULL, NULL);
1586  PQclear(res);
1587  count++;
1588  }
1589  else
1590  {
1591  PQclear(res);
1592  break;
1593  }
1594  }
1595 
1596  destroyPQExpBuffer(buf);
1597 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:1684
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
static int server_version
Definition: pg_dumpall.c:82
char * dbname
Definition: streamutil.c:39
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 1729 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().

1730 {
1731  PGresult *res;
1732  int i;
1733 
1734  res = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1");
1735 
1736  for (i = 0; i < PQntuples(res); i++)
1737  {
1738  int ret;
1739 
1740  char *dbname = PQgetvalue(res, i, 0);
1741  PQExpBufferData connectbuf;
1742 
1743  if (verbose)
1744  fprintf(stderr, _("%s: dumping database \"%s\"...\n"), progname, dbname);
1745 
1746  initPQExpBuffer(&connectbuf);
1747  appendPsqlMetaConnect(&connectbuf, dbname);
1748  fprintf(OPF, "%s\n", connectbuf.data);
1749  termPQExpBuffer(&connectbuf);
1750 
1751  /*
1752  * Restore will need to write to the target cluster. This connection
1753  * setting is emitted for pg_dumpall rather than in the code also used
1754  * by pg_dump, so that a cluster with databases or users which have
1755  * this flag turned on can still be replicated through pg_dumpall
1756  * without editing the file or stream. With pg_dump there are many
1757  * other ways to allow the file to be used, and leaving it out allows
1758  * users to protect databases from being accidental restore targets.
1759  */
1760  fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
1761 
1762  if (filename)
1763  fclose(OPF);
1764 
1765  ret = runPgDump(dbname);
1766  if (ret != 0)
1767  {
1768  fprintf(stderr, _("%s: pg_dump failed on database \"%s\", exiting\n"), progname, dbname);
1769  exit_nicely(1);
1770  }
1771 
1772  if (filename)
1773  {
1774  OPF = fopen(filename, PG_BINARY_A);
1775  if (!OPF)
1776  {
1777  fprintf(stderr, _("%s: could not re-open the output file \"%s\": %s\n"),
1778  progname, filename, strerror(errno));
1779  exit_nicely(1);
1780  }
1781  }
1782 
1783  }
1784 
1785  PQclear(res);
1786 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:89
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:92
static int runPgDump(const char *dbname)
Definition: pg_dumpall.c:1794
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define PG_BINARY_A
Definition: c.h:1040
char * dbname
Definition: streamutil.c:39
static char * filename
Definition: pg_dumpall.c:90
int i
const char * strerror(int errnum)
Definition: strerror.c:19
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static void dumpDbRoleConfig ( PGconn conn)
static

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

1651 {
1653  PGresult *res;
1654  int i;
1655 
1656  printfPQExpBuffer(buf, "SELECT rolname, datname, unnest(setconfig) "
1657  "FROM pg_db_role_setting, %s u, pg_database "
1658  "WHERE setrole = u.oid AND setdatabase = pg_database.oid", role_catalog);
1659  res = executeQuery(conn, buf->data);
1660 
1661  if (PQntuples(res) > 0)
1662  {
1663  fprintf(OPF, "--\n-- Per-Database Role Settings \n--\n\n");
1664 
1665  for (i = 0; i < PQntuples(res); i++)
1666  {
1667  makeAlterConfigCommand(conn, PQgetvalue(res, i, 2),
1668  "ROLE", PQgetvalue(res, i, 0),
1669  "DATABASE", PQgetvalue(res, i, 1));
1670  }
1671 
1672  fprintf(OPF, "\n\n");
1673  }
1674 
1675  PQclear(res);
1676  destroyPQExpBuffer(buf);
1677 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:89
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:1684
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:85
static void dumpGroups ( PGconn conn)
static

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

1011 {
1013  PGresult *res;
1014  int i;
1015 
1016  res = executeQuery(conn,
1017  "SELECT groname, grolist FROM pg_group ORDER BY 1");
1018 
1019  if (PQntuples(res) > 0)
1020  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
1021 
1022  for (i = 0; i < PQntuples(res); i++)
1023  {
1024  char *groname = PQgetvalue(res, i, 0);
1025  char *grolist = PQgetvalue(res, i, 1);
1026  PGresult *res2;
1027  int j;
1028 
1029  /*
1030  * Array representation is {1,2,3} ... convert to (1,2,3)
1031  */
1032  if (strlen(grolist) < 3)
1033  continue;
1034 
1035  grolist = pg_strdup(grolist);
1036  grolist[0] = '(';
1037  grolist[strlen(grolist) - 1] = ')';
1038  printfPQExpBuffer(buf,
1039  "SELECT usename FROM pg_shadow "
1040  "WHERE usesysid IN %s ORDER BY 1",
1041  grolist);
1042  free(grolist);
1043 
1044  res2 = executeQuery(conn, buf->data);
1045 
1046  for (j = 0; j < PQntuples(res2); j++)
1047  {
1048  char *usename = PQgetvalue(res2, j, 0);
1049 
1050  /*
1051  * Don't try to grant a role to itself; can happen if old
1052  * installation has identically named user and group.
1053  */
1054  if (strcmp(groname, usename) == 0)
1055  continue;
1056 
1057  fprintf(OPF, "GRANT %s", fmtId(groname));
1058  fprintf(OPF, " TO %s;\n", fmtId(usename));
1059  }
1060 
1061  PQclear(res2);
1062  }
1063 
1064  PQclear(res);
1065  destroyPQExpBuffer(buf);
1066 
1067  fprintf(OPF, "\n\n");
1068 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:89
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 950 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().

951 {
953  PGresult *res;
954  int i;
955 
956  printfPQExpBuffer(buf, "SELECT ur.rolname AS roleid, "
957  "um.rolname AS member, "
958  "a.admin_option, "
959  "ug.rolname AS grantor "
960  "FROM pg_auth_members a "
961  "LEFT JOIN %s ur on ur.oid = a.roleid "
962  "LEFT JOIN %s um on um.oid = a.member "
963  "LEFT JOIN %s ug on ug.oid = a.grantor "
964  "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
965  "ORDER BY 1,2,3", role_catalog, role_catalog, role_catalog);
966  res = executeQuery(conn, buf->data);
967 
968  if (PQntuples(res) > 0)
969  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
970 
971  for (i = 0; i < PQntuples(res); i++)
972  {
973  char *roleid = PQgetvalue(res, i, 0);
974  char *member = PQgetvalue(res, i, 1);
975  char *option = PQgetvalue(res, i, 2);
976 
977  fprintf(OPF, "GRANT %s", fmtId(roleid));
978  fprintf(OPF, " TO %s", fmtId(member));
979  if (*option == 't')
980  fprintf(OPF, " WITH ADMIN OPTION");
981 
982  /*
983  * We don't track the grantor very carefully in the backend, so cope
984  * with the possibility that it has been dropped.
985  */
986  if (!PQgetisnull(res, i, 3))
987  {
988  char *grantor = PQgetvalue(res, i, 3);
989 
990  fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
991  }
992  fprintf(OPF, ";\n");
993  }
994 
995  PQclear(res);
996  destroyPQExpBuffer(buf);
997 
998  fprintf(OPF, "\n\n");
999 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:89
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:85
static void dumpRoles ( PGconn conn)
static

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

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

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

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

Definition at line 2171 of file pg_dumpall.c.

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

Referenced by main().

2172 {
2173  char buf[64];
2174  time_t now = time(NULL);
2175 
2176  if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
2177  fprintf(OPF, "-- %s %s\n\n", msg, buf);
2178 }
static FILE * OPF
Definition: pg_dumpall.c:89
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 1605 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().

1606 {
1608  int count = 1;
1609 
1610  for (;;)
1611  {
1612  PGresult *res;
1613 
1614  if (server_version >= 90000)
1615  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1616  "setdatabase = 0 AND setrole = "
1617  "(SELECT oid FROM %s WHERE rolname = ", count, role_catalog);
1618  else if (server_version >= 80100)
1619  printfPQExpBuffer(buf, "SELECT rolconfig[%d] FROM %s WHERE rolname = ", count, role_catalog);
1620  else
1621  printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
1622  appendStringLiteralConn(buf, username, conn);
1623  if (server_version >= 90000)
1624  appendPQExpBufferChar(buf, ')');
1625 
1626  res = executeQuery(conn, buf->data);
1627  if (PQntuples(res) == 1 &&
1628  !PQgetisnull(res, 0, 0))
1629  {
1630  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1631  "ROLE", username, NULL, NULL);
1632  PQclear(res);
1633  count++;
1634  }
1635  else
1636  {
1637  PQclear(res);
1638  break;
1639  }
1640  }
1641 
1642  destroyPQExpBuffer(buf);
1643 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2118
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:1684
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:82
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
static char role_catalog[10]
Definition: pg_dumpall.c:85
static void executeCommand ( PGconn conn,
const char *  query 
)
static

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

2145 {
2146  PGresult *res;
2147 
2148  if (verbose)
2149  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2150 
2151  res = PQexec(conn, query);
2152  if (!res ||
2154  {
2155  fprintf(stderr, _("%s: query failed: %s"),
2156  progname, PQerrorMessage(conn));
2157  fprintf(stderr, _("%s: query was: %s\n"),
2158  progname, query);
2159  PQfinish(conn);
2160  exit_nicely(1);
2161  }
2162 
2163  PQclear(res);
2164 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static bool verbose
Definition: pg_dumpall.c:66
#define exit_nicely(code)
Definition: pg_dumpall.c:92
void PQclear(PGresult *res)
Definition: fe-exec.c: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 2118 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().

2119 {
2120  PGresult *res;
2121 
2122  if (verbose)
2123  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2124 
2125  res = PQexec(conn, query);
2126  if (!res ||
2128  {
2129  fprintf(stderr, _("%s: query failed: %s"),
2130  progname, PQerrorMessage(conn));
2131  fprintf(stderr, _("%s: query was: %s\n"),
2132  progname, query);
2133  PQfinish(conn);
2134  exit_nicely(1);
2135  }
2136 
2137  return res;
2138 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
static const char * progname
Definition: pg_dumpall.c:62
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static bool verbose
Definition: pg_dumpall.c:66
#define exit_nicely(code)
Definition: pg_dumpall.c:92
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
#define _(x)
Definition: elog.c:84
static void help ( void  )
static

Definition at line 576 of file pg_dumpall.c.

References _, and progname.

Referenced by main().

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

Definition at line 95 of file pg_dumpall.c.

References _, appendPQExpBuffer(), appendPQExpBufferStr(), appendShellString(), binary_upgrade, column_inserts, conn, connectDatabase(), connstr, createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), disable_dollar_quoting, disable_triggers, dosync, dropDBs(), dropRoles(), dropTablespaces(), dumpCreateDB(), dumpDatabases(), dumpDbRoleConfig(), dumpGroups(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpTimestamp(), encoding, executeCommand(), exit_nicely, filename, find_my_exec(), find_other_exec(), fmtId(), fsync_fname(), get_progname(), getopt_long(), help(), if_exists, inserts, load_via_partition_root, MAXPGPATH, no_argument, no_publications, no_role_passwords, no_security_labels, no_subscriptions, no_tablespaces, no_unlogged_table_data, 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.

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

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

1687 {
1688  char *pos;
1689  char *mine;
1690  PQExpBuffer buf;
1691 
1692  mine = pg_strdup(arrayitem);
1693  pos = strchr(mine, '=');
1694  if (pos == NULL)
1695  {
1696  free(mine);
1697  return;
1698  }
1699 
1700  buf = createPQExpBuffer();
1701 
1702  *pos = 0;
1703  appendPQExpBuffer(buf, "ALTER %s %s ", type, fmtId(name));
1704  if (type2 != NULL && name2 != NULL)
1705  appendPQExpBuffer(buf, "IN %s %s ", type2, fmtId(name2));
1706  appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
1707 
1708  /*
1709  * Some GUC variable names are 'LIST' type and hence must not be quoted.
1710  */
1711  if (pg_strcasecmp(mine, "DateStyle") == 0
1712  || pg_strcasecmp(mine, "search_path") == 0)
1713  appendPQExpBufferStr(buf, pos + 1);
1714  else
1715  appendStringLiteralConn(buf, pos + 1, conn);
1716  appendPQExpBufferStr(buf, ";\n");
1717 
1718  fprintf(OPF, "%s", buf->data);
1719  destroyPQExpBuffer(buf);
1720  free(mine);
1721 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:89
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c: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 1794 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().

1795 {
1796  PQExpBuffer connstrbuf = createPQExpBuffer();
1798  int ret;
1799 
1800  appendPQExpBuffer(cmd, "\"%s\" %s", pg_dump_bin,
1801  pgdumpopts->data);
1802 
1803  /*
1804  * If we have a filename, use the undocumented plain-append pg_dump
1805  * format.
1806  */
1807  if (filename)
1808  appendPQExpBufferStr(cmd, " -Fa ");
1809  else
1810  appendPQExpBufferStr(cmd, " -Fp ");
1811 
1812  /*
1813  * Append the database name to the already-constructed stem of connection
1814  * string.
1815  */
1816  appendPQExpBuffer(connstrbuf, "%s dbname=", connstr);
1817  appendConnStrVal(connstrbuf, dbname);
1818 
1819  appendShellString(cmd, connstrbuf->data);
1820 
1821  if (verbose)
1822  fprintf(stderr, _("%s: running \"%s\"\n"), progname, cmd->data);
1823 
1824  fflush(stdout);
1825  fflush(stderr);
1826 
1827  ret = system(cmd->data);
1828 
1829  destroyPQExpBuffer(cmd);
1830  destroyPQExpBuffer(connstrbuf);
1831 
1832  return ret;
1833 }
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:39
static char * filename
Definition: pg_dumpall.c:90
#define _(x)
Definition: elog.c:84
static char * connstr
Definition: pg_dumpall.c:64

Variable Documentation

int binary_upgrade = 0
static

Definition at line 69 of file pg_dumpall.c.

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

int column_inserts = 0
static

Definition at line 70 of file pg_dumpall.c.

Referenced by main().

int disable_dollar_quoting = 0
static

Definition at line 71 of file pg_dumpall.c.

Referenced by main().

int disable_triggers = 0
static

Definition at line 72 of file pg_dumpall.c.

Referenced by main().

bool dosync = true
static

Definition at line 67 of file pg_dumpall.c.

Referenced by main().

int if_exists = 0
static

Definition at line 73 of file pg_dumpall.c.

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

int inserts = 0
static

Definition at line 74 of file pg_dumpall.c.

Referenced by main().

int load_via_partition_root = 0
static

Definition at line 83 of file pg_dumpall.c.

Referenced by main().

int no_publications = 0
static

Definition at line 77 of file pg_dumpall.c.

Referenced by main().

int no_role_passwords = 0
static

Definition at line 81 of file pg_dumpall.c.

Referenced by dumpRoles(), and main().

int no_security_labels = 0
static

Definition at line 78 of file pg_dumpall.c.

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

int no_subscriptions = 0
static

Definition at line 79 of file pg_dumpall.c.

Referenced by main().

int no_tablespaces = 0
static

Definition at line 75 of file pg_dumpall.c.

Referenced by dumpCreateDB(), and main().

int no_unlogged_table_data = 0
static

Definition at line 80 of file pg_dumpall.c.

Referenced by main().

char pg_dump_bin[MAXPGPATH]
static

Definition at line 61 of file pg_dumpall.c.

Referenced by main(), and runPgDump().

PQExpBuffer pgdumpopts
static

Definition at line 63 of file pg_dumpall.c.

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

Definition at line 65 of file pg_dumpall.c.

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

int use_setsessauth = 0
static

Definition at line 76 of file pg_dumpall.c.

Referenced by main().

bool verbose = false
static

Definition at line 66 of file pg_dumpall.c.

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