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 "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 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 int binary_upgrade = 0
 
static int column_inserts = 0
 
static int disable_dollar_quoting = 0
 
static int disable_triggers = 0
 
static int if_exists = 0
 
static int inserts = 0
 
static int no_tablespaces = 0
 
static int use_setsessauth = 0
 
static int no_security_labels = 0
 
static int no_unlogged_table_data = 0
 
static int server_version
 
static FILE * OPF
 
static char * filename = NULL
 

Macro Definition Documentation

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

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

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

Referenced by dumpRoles(), and dumpTablespaces().

1789 {
1791  PGresult *res;
1792 
1793  buildShSecLabelQuery(conn, catalog_name, objectId, sql);
1794  res = executeQuery(conn, sql->data);
1795  emitShSecLabels(conn, res, buffer, target, objname);
1796 
1797  PQclear(res);
1798  destroyPQExpBuffer(sql);
1799 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
void buildShSecLabelQuery(PGconn *conn, const char *catalog_name, uint32 objectId, PQExpBuffer sql)
Definition: dumputils.c:636
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:652
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 1812 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().

1815 {
1816  PGconn *conn;
1817  bool new_pass;
1818  const char *remoteversion_str;
1819  int my_version;
1820  const char **keywords = NULL;
1821  const char **values = NULL;
1822  PQconninfoOption *conn_opts = NULL;
1823  static bool have_password = false;
1824  static char password[100];
1825 
1826  if (prompt_password == TRI_YES && !have_password)
1827  {
1828  simple_prompt("Password: ", password, sizeof(password), false);
1829  have_password = true;
1830  }
1831 
1832  /*
1833  * Start the connection. Loop until we have a password if requested by
1834  * backend.
1835  */
1836  do
1837  {
1838  int argcount = 6;
1839  PQconninfoOption *conn_opt;
1840  char *err_msg = NULL;
1841  int i = 0;
1842 
1843  if (keywords)
1844  free(keywords);
1845  if (values)
1846  free(values);
1847  if (conn_opts)
1848  PQconninfoFree(conn_opts);
1849 
1850  /*
1851  * Merge the connection info inputs given in form of connection string
1852  * and other options. Explicitly discard any dbname value in the
1853  * connection string; otherwise, PQconnectdbParams() would interpret
1854  * that value as being itself a connection string.
1855  */
1856  if (connection_string)
1857  {
1858  conn_opts = PQconninfoParse(connection_string, &err_msg);
1859  if (conn_opts == NULL)
1860  {
1861  fprintf(stderr, "%s: %s", progname, err_msg);
1862  exit_nicely(1);
1863  }
1864 
1865  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1866  {
1867  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1868  strcmp(conn_opt->keyword, "dbname") != 0)
1869  argcount++;
1870  }
1871 
1872  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1873  values = pg_malloc0((argcount + 1) * sizeof(*values));
1874 
1875  for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
1876  {
1877  if (conn_opt->val != NULL && conn_opt->val[0] != '\0' &&
1878  strcmp(conn_opt->keyword, "dbname") != 0)
1879  {
1880  keywords[i] = conn_opt->keyword;
1881  values[i] = conn_opt->val;
1882  i++;
1883  }
1884  }
1885  }
1886  else
1887  {
1888  keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
1889  values = pg_malloc0((argcount + 1) * sizeof(*values));
1890  }
1891 
1892  if (pghost)
1893  {
1894  keywords[i] = "host";
1895  values[i] = pghost;
1896  i++;
1897  }
1898  if (pgport)
1899  {
1900  keywords[i] = "port";
1901  values[i] = pgport;
1902  i++;
1903  }
1904  if (pguser)
1905  {
1906  keywords[i] = "user";
1907  values[i] = pguser;
1908  i++;
1909  }
1910  if (have_password)
1911  {
1912  keywords[i] = "password";
1913  values[i] = password;
1914  i++;
1915  }
1916  if (dbname)
1917  {
1918  keywords[i] = "dbname";
1919  values[i] = dbname;
1920  i++;
1921  }
1922  keywords[i] = "fallback_application_name";
1923  values[i] = progname;
1924  i++;
1925 
1926  new_pass = false;
1927  conn = PQconnectdbParams(keywords, values, true);
1928 
1929  if (!conn)
1930  {
1931  fprintf(stderr, _("%s: could not connect to database \"%s\"\n"),
1932  progname, dbname);
1933  exit_nicely(1);
1934  }
1935 
1936  if (PQstatus(conn) == CONNECTION_BAD &&
1937  PQconnectionNeedsPassword(conn) &&
1938  !have_password &&
1939  prompt_password != TRI_NO)
1940  {
1941  PQfinish(conn);
1942  simple_prompt("Password: ", password, sizeof(password), false);
1943  have_password = true;
1944  new_pass = true;
1945  }
1946  } while (new_pass);
1947 
1948  /* check to see that the backend connection was successfully made */
1949  if (PQstatus(conn) == CONNECTION_BAD)
1950  {
1951  if (fail_on_error)
1952  {
1953  fprintf(stderr,
1954  _("%s: could not connect to database \"%s\": %s\n"),
1955  progname, dbname, PQerrorMessage(conn));
1956  exit_nicely(1);
1957  }
1958  else
1959  {
1960  PQfinish(conn);
1961 
1962  free(keywords);
1963  free(values);
1964  PQconninfoFree(conn_opts);
1965 
1966  return NULL;
1967  }
1968  }
1969 
1970  /*
1971  * Ok, connected successfully. Remember the options used, in the form of a
1972  * connection string.
1973  */
1974  connstr = constructConnStr(keywords, values);
1975 
1976  free(keywords);
1977  free(values);
1978  PQconninfoFree(conn_opts);
1979 
1980  /* Check version */
1981  remoteversion_str = PQparameterStatus(conn, "server_version");
1982  if (!remoteversion_str)
1983  {
1984  fprintf(stderr, _("%s: could not get server version\n"), progname);
1985  exit_nicely(1);
1986  }
1988  if (server_version == 0)
1989  {
1990  fprintf(stderr, _("%s: could not parse server version \"%s\"\n"),
1991  progname, remoteversion_str);
1992  exit_nicely(1);
1993  }
1994 
1995  my_version = PG_VERSION_NUM;
1996 
1997  /*
1998  * We allow the server to be back to 8.0, and up to any minor release of
1999  * our own major version. (See also version check in pg_dump.c.)
2000  */
2001  if (my_version != server_version
2002  && (server_version < 80000 ||
2003  (server_version / 100) > (my_version / 100)))
2004  {
2005  fprintf(stderr, _("server version: %s; %s version: %s\n"),
2006  remoteversion_str, progname, PG_VERSION);
2007  fprintf(stderr, _("aborting because of server version mismatch\n"));
2008  exit_nicely(1);
2009  }
2010 
2011  /*
2012  * Make sure we are not fooled by non-system schemas in the search path.
2013  */
2014  executeCommand(conn, "SET search_path = pg_catalog");
2015 
2016  return conn;
2017 }
static char password[100]
Definition: streamutil.c:41
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:5924
static char * constructConnStr(const char **keywords, const char **values)
Definition: pg_dumpall.c:2030
static const char * progname
Definition: pg_dumpall.c:61
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:5949
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:470
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:4603
char * connection_string
Definition: streamutil.c:34
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
PGconn * conn
Definition: streamutil.c:42
static void executeCommand(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2087
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:5809
char * pghost
Definition: pgbench.c:180
#define exit_nicely(code)
Definition: pg_dumpall.c:82
#define free(a)
Definition: header.h:60
static bool have_password
Definition: streamutil.c:40
#define NULL
Definition: c.h:226
static int server_version
Definition: pg_dumpall.c:77
char * dbname
Definition: streamutil.c:38
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:5993
int i
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5906
#define _(x)
Definition: elog.c:84
char * pgport
Definition: pgbench.c:181
static char * connstr
Definition: pg_dumpall.c:63
static char * constructConnStr ( const char **  keywords,
const char **  values 
)
static

Definition at line 2030 of file pg_dumpall.c.

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

Referenced by connectDatabase().

2031 {
2033  char *connstr;
2034  int i;
2035  bool firstkeyword = true;
2036 
2037  /* Construct a new connection string in key='value' format. */
2038  for (i = 0; keywords[i] != NULL; i++)
2039  {
2040  if (strcmp(keywords[i], "dbname") == 0 ||
2041  strcmp(keywords[i], "password") == 0 ||
2042  strcmp(keywords[i], "fallback_application_name") == 0)
2043  continue;
2044 
2045  if (!firstkeyword)
2046  appendPQExpBufferChar(buf, ' ');
2047  firstkeyword = false;
2048  appendPQExpBuffer(buf, "%s=", keywords[i]);
2049  appendConnStrVal(buf, values[i]);
2050  }
2051 
2052  connstr = pg_strdup(buf->data);
2053  destroyPQExpBuffer(buf);
2054  return connstr;
2055 }
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:536
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:65
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:226
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int i
static char * connstr
Definition: pg_dumpall.c:63
static void dropDBs ( PGconn conn)
static

Definition at line 1192 of file pg_dumpall.c.

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

Referenced by main().

1193 {
1194  PGresult *res;
1195  int i;
1196 
1197  res = executeQuery(conn,
1198  "SELECT datname "
1199  "FROM pg_database d "
1200  "WHERE datallowconn ORDER BY 1");
1201 
1202  if (PQntuples(res) > 0)
1203  fprintf(OPF, "--\n-- Drop databases\n--\n\n");
1204 
1205  for (i = 0; i < PQntuples(res); i++)
1206  {
1207  char *dbname = PQgetvalue(res, i, 0);
1208 
1209  /*
1210  * Skip "template1" and "postgres"; the restore script is almost
1211  * certainly going to be run in one or the other, and we don't know
1212  * which. This must agree with dumpCreateDB's choices!
1213  */
1214  if (strcmp(dbname, "template1") != 0 &&
1215  strcmp(dbname, "postgres") != 0)
1216  {
1217  fprintf(OPF, "DROP DATABASE %s%s;\n",
1218  if_exists ? "IF EXISTS " : "",
1219  fmtId(dbname));
1220  }
1221  }
1222 
1223  PQclear(res);
1224 
1225  fprintf(OPF, "\n\n");
1226 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
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:71
char * dbname
Definition: streamutil.c:38
int i
static void dropRoles ( PGconn conn)
static

Definition at line 591 of file pg_dumpall.c.

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

Referenced by main().

592 {
593  PGresult *res;
594  int i_rolname;
595  int i;
596 
597  if (server_version >= 90600)
598  res = executeQuery(conn,
599  "SELECT rolname "
600  "FROM pg_authid "
601  "WHERE rolname !~ '^pg_' "
602  "ORDER BY 1");
603  else if (server_version >= 80100)
604  res = executeQuery(conn,
605  "SELECT rolname "
606  "FROM pg_authid "
607  "ORDER BY 1");
608  else
609  res = executeQuery(conn,
610  "SELECT usename as rolname "
611  "FROM pg_shadow "
612  "UNION "
613  "SELECT groname as rolname "
614  "FROM pg_group "
615  "ORDER BY 1");
616 
617  i_rolname = PQfnumber(res, "rolname");
618 
619  if (PQntuples(res) > 0)
620  fprintf(OPF, "--\n-- Drop roles\n--\n\n");
621 
622  for (i = 0; i < PQntuples(res); i++)
623  {
624  const char *rolename;
625 
626  rolename = PQgetvalue(res, i, i_rolname);
627 
628  fprintf(OPF, "DROP ROLE %s%s;\n",
629  if_exists ? "IF EXISTS " : "",
630  fmtId(rolename));
631  }
632 
633  PQclear(res);
634 
635  fprintf(OPF, "\n\n");
636 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
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:71
static int server_version
Definition: pg_dumpall.c:77
int i
static void dropTablespaces ( PGconn conn)
static

Definition at line 1020 of file pg_dumpall.c.

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

Referenced by main().

1021 {
1022  PGresult *res;
1023  int i;
1024 
1025  /*
1026  * Get all tablespaces except built-in ones (which we assume are named
1027  * pg_xxx)
1028  */
1029  res = executeQuery(conn, "SELECT spcname "
1030  "FROM pg_catalog.pg_tablespace "
1031  "WHERE spcname !~ '^pg_' "
1032  "ORDER BY 1");
1033 
1034  if (PQntuples(res) > 0)
1035  fprintf(OPF, "--\n-- Drop tablespaces\n--\n\n");
1036 
1037  for (i = 0; i < PQntuples(res); i++)
1038  {
1039  char *spcname = PQgetvalue(res, i, 0);
1040 
1041  fprintf(OPF, "DROP TABLESPACE %s%s;\n",
1042  if_exists ? "IF EXISTS " : "",
1043  fmtId(spcname));
1044  }
1045 
1046  PQclear(res);
1047 
1048  fprintf(OPF, "\n\n");
1049 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
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:71
int i
static void dumpCreateDB ( PGconn conn)
static

Definition at line 1240 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(), progname, resetPQExpBuffer(), server_version, and skip_acls.

Referenced by main().

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

Definition at line 1504 of file pg_dumpall.c.

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

Referenced by dumpCreateDB().

1505 {
1507  int count = 1;
1508 
1509  for (;;)
1510  {
1511  PGresult *res;
1512 
1513  if (server_version >= 90000)
1514  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1515  "setrole = 0 AND setdatabase = (SELECT oid FROM pg_database WHERE datname = ", count);
1516  else
1517  printfPQExpBuffer(buf, "SELECT datconfig[%d] FROM pg_database WHERE datname = ", count);
1518  appendStringLiteralConn(buf, dbname, conn);
1519 
1520  if (server_version >= 90000)
1521  appendPQExpBuffer(buf, ")");
1522 
1523  res = executeQuery(conn, buf->data);
1524  if (PQntuples(res) == 1 &&
1525  !PQgetisnull(res, 0, 0))
1526  {
1527  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1528  "DATABASE", dbname, NULL, NULL);
1529  PQclear(res);
1530  count++;
1531  }
1532  else
1533  {
1534  PQclear(res);
1535  break;
1536  }
1537  }
1538 
1539  destroyPQExpBuffer(buf);
1540 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
static char * buf
Definition: pg_test_fsync.c:65
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1627
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:226
static int server_version
Definition: pg_dumpall.c:77
char * dbname
Definition: streamutil.c:38
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3092
static void dumpDatabases ( PGconn conn)
static

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

1673 {
1674  PGresult *res;
1675  int i;
1676 
1677  res = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1");
1678 
1679  for (i = 0; i < PQntuples(res); i++)
1680  {
1681  int ret;
1682 
1683  char *dbname = PQgetvalue(res, i, 0);
1684  PQExpBufferData connectbuf;
1685 
1686  if (verbose)
1687  fprintf(stderr, _("%s: dumping database \"%s\"...\n"), progname, dbname);
1688 
1689  initPQExpBuffer(&connectbuf);
1690  appendPsqlMetaConnect(&connectbuf, dbname);
1691  fprintf(OPF, "%s\n", connectbuf.data);
1692  termPQExpBuffer(&connectbuf);
1693 
1694  /*
1695  * Restore will need to write to the target cluster. This connection
1696  * setting is emitted for pg_dumpall rather than in the code also used
1697  * by pg_dump, so that a cluster with databases or users which have
1698  * this flag turned on can still be replicated through pg_dumpall
1699  * without editing the file or stream. With pg_dump there are many
1700  * other ways to allow the file to be used, and leaving it out allows
1701  * users to protect databases from being accidental restore targets.
1702  */
1703  fprintf(OPF, "SET default_transaction_read_only = off;\n\n");
1704 
1705  if (filename)
1706  fclose(OPF);
1707 
1708  ret = runPgDump(dbname);
1709  if (ret != 0)
1710  {
1711  fprintf(stderr, _("%s: pg_dump failed on database \"%s\", exiting\n"), progname, dbname);
1712  exit_nicely(1);
1713  }
1714 
1715  if (filename)
1716  {
1717  OPF = fopen(filename, PG_BINARY_A);
1718  if (!OPF)
1719  {
1720  fprintf(stderr, _("%s: could not re-open the output file \"%s\": %s\n"),
1721  progname, filename, strerror(errno));
1722  exit_nicely(1);
1723  }
1724  }
1725 
1726  }
1727 
1728  PQclear(res);
1729 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
static const char * progname
Definition: pg_dumpall.c:61
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
static bool verbose
Definition: pg_dumpall.c:65
void appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
Definition: string_utils.c:581
#define exit_nicely(code)
Definition: pg_dumpall.c:82
static int runPgDump(const char *dbname)
Definition: pg_dumpall.c:1737
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define PG_BINARY_A
Definition: c.h:1039
char * dbname
Definition: streamutil.c:38
static char * filename
Definition: pg_dumpall.c:80
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 1593 of file pg_dumpall.c.

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

Referenced by main().

1594 {
1596  PGresult *res;
1597  int i;
1598 
1599  printfPQExpBuffer(buf, "SELECT rolname, datname, unnest(setconfig) "
1600  "FROM pg_db_role_setting, pg_authid, pg_database "
1601  "WHERE setrole = pg_authid.oid AND setdatabase = pg_database.oid");
1602  res = executeQuery(conn, buf->data);
1603 
1604  if (PQntuples(res) > 0)
1605  {
1606  fprintf(OPF, "--\n-- Per-Database Role Settings \n--\n\n");
1607 
1608  for (i = 0; i < PQntuples(res); i++)
1609  {
1610  makeAlterConfigCommand(conn, PQgetvalue(res, i, 2),
1611  "ROLE", PQgetvalue(res, i, 0),
1612  "DATABASE", PQgetvalue(res, i, 1));
1613  }
1614 
1615  fprintf(OPF, "\n\n");
1616  }
1617 
1618  PQclear(res);
1619  destroyPQExpBuffer(buf);
1620 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
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:65
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1627
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
int i
static void dumpGroups ( PGconn conn)
static

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

956 {
958  PGresult *res;
959  int i;
960 
961  res = executeQuery(conn,
962  "SELECT groname, grolist FROM pg_group ORDER BY 1");
963 
964  if (PQntuples(res) > 0)
965  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
966 
967  for (i = 0; i < PQntuples(res); i++)
968  {
969  char *groname = PQgetvalue(res, i, 0);
970  char *grolist = PQgetvalue(res, i, 1);
971  PGresult *res2;
972  int j;
973 
974  /*
975  * Array representation is {1,2,3} ... convert to (1,2,3)
976  */
977  if (strlen(grolist) < 3)
978  continue;
979 
980  grolist = pg_strdup(grolist);
981  grolist[0] = '(';
982  grolist[strlen(grolist) - 1] = ')';
983  printfPQExpBuffer(buf,
984  "SELECT usename FROM pg_shadow "
985  "WHERE usesysid IN %s ORDER BY 1",
986  grolist);
987  free(grolist);
988 
989  res2 = executeQuery(conn, buf->data);
990 
991  for (j = 0; j < PQntuples(res2); j++)
992  {
993  char *usename = PQgetvalue(res2, j, 0);
994 
995  /*
996  * Don't try to grant a role to itself; can happen if old
997  * installation has identically named user and group.
998  */
999  if (strcmp(groname, usename) == 0)
1000  continue;
1001 
1002  fprintf(OPF, "GRANT %s", fmtId(groname));
1003  fprintf(OPF, " TO %s;\n", fmtId(usename));
1004  }
1005 
1006  PQclear(res2);
1007  }
1008 
1009  PQclear(res);
1010  destroyPQExpBuffer(buf);
1011 
1012  fprintf(OPF, "\n\n");
1013 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
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:65
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:60
int i
static void dumpRoleMembership ( PGconn conn)
static

Definition at line 898 of file pg_dumpall.c.

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

Referenced by main().

899 {
900  PGresult *res;
901  int i;
902 
903  res = executeQuery(conn, "SELECT ur.rolname AS roleid, "
904  "um.rolname AS member, "
905  "a.admin_option, "
906  "ug.rolname AS grantor "
907  "FROM pg_auth_members a "
908  "LEFT JOIN pg_authid ur on ur.oid = a.roleid "
909  "LEFT JOIN pg_authid um on um.oid = a.member "
910  "LEFT JOIN pg_authid ug on ug.oid = a.grantor "
911  "WHERE NOT (ur.rolname ~ '^pg_' AND um.rolname ~ '^pg_')"
912  "ORDER BY 1,2,3");
913 
914  if (PQntuples(res) > 0)
915  fprintf(OPF, "--\n-- Role memberships\n--\n\n");
916 
917  for (i = 0; i < PQntuples(res); i++)
918  {
919  char *roleid = PQgetvalue(res, i, 0);
920  char *member = PQgetvalue(res, i, 1);
921  char *option = PQgetvalue(res, i, 2);
922 
923  fprintf(OPF, "GRANT %s", fmtId(roleid));
924  fprintf(OPF, " TO %s", fmtId(member));
925  if (*option == 't')
926  fprintf(OPF, " WITH ADMIN OPTION");
927 
928  /*
929  * We don't track the grantor very carefully in the backend, so cope
930  * with the possibility that it has been dropped.
931  */
932  if (!PQgetisnull(res, i, 3))
933  {
934  char *grantor = PQgetvalue(res, i, 3);
935 
936  fprintf(OPF, " GRANTED BY %s", fmtId(grantor));
937  }
938  fprintf(OPF, ";\n");
939  }
940 
941  PQclear(res);
942 
943  fprintf(OPF, "\n\n");
944 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
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 void dumpRoles ( PGconn conn)
static

Definition at line 642 of file pg_dumpall.c.

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

Referenced by main().

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

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

1056 {
1057  PGresult *res;
1058  int i;
1059 
1060  /*
1061  * Get all tablespaces except built-in ones (which we assume are named
1062  * pg_xxx)
1063  *
1064  * For the tablespace ACLs, as of 9.6, we extract both the positive (as
1065  * spcacl) and negative (as rspcacl) ACLs, relative to the default ACL for
1066  * tablespaces, which are then passed to buildACLCommands() below.
1067  *
1068  * See buildACLQueries() and buildACLCommands().
1069  *
1070  * Note that we do not support initial privileges (pg_init_privs) on
1071  * tablespaces.
1072  */
1073  if (server_version >= 90600)
1074  res = executeQuery(conn, "SELECT oid, spcname, "
1075  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1076  "pg_catalog.pg_tablespace_location(oid), "
1077  "(SELECT pg_catalog.array_agg(acl) FROM (SELECT pg_catalog.unnest(coalesce(spcacl,pg_catalog.acldefault('t',spcowner))) AS acl "
1078  "EXCEPT SELECT pg_catalog.unnest(pg_catalog.acldefault('t',spcowner))) as foo)"
1079  "AS spcacl,"
1080  "(SELECT pg_catalog.array_agg(acl) FROM (SELECT pg_catalog.unnest(pg_catalog.acldefault('t',spcowner)) AS acl "
1081  "EXCEPT SELECT pg_catalog.unnest(coalesce(spcacl,pg_catalog.acldefault('t',spcowner)))) as foo)"
1082  "AS rspcacl,"
1083  "array_to_string(spcoptions, ', '),"
1084  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1085  "FROM pg_catalog.pg_tablespace "
1086  "WHERE spcname !~ '^pg_' "
1087  "ORDER BY 1");
1088  else if (server_version >= 90200)
1089  res = executeQuery(conn, "SELECT oid, spcname, "
1090  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1091  "pg_catalog.pg_tablespace_location(oid), "
1092  "spcacl, '' as rspcacl, "
1093  "array_to_string(spcoptions, ', '),"
1094  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1095  "FROM pg_catalog.pg_tablespace "
1096  "WHERE spcname !~ '^pg_' "
1097  "ORDER BY 1");
1098  else if (server_version >= 90000)
1099  res = executeQuery(conn, "SELECT oid, spcname, "
1100  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1101  "spclocation, spcacl, '' as rspcacl, "
1102  "array_to_string(spcoptions, ', '),"
1103  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1104  "FROM pg_catalog.pg_tablespace "
1105  "WHERE spcname !~ '^pg_' "
1106  "ORDER BY 1");
1107  else if (server_version >= 80200)
1108  res = executeQuery(conn, "SELECT oid, spcname, "
1109  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1110  "spclocation, spcacl, '' as rspcacl, null, "
1111  "pg_catalog.shobj_description(oid, 'pg_tablespace') "
1112  "FROM pg_catalog.pg_tablespace "
1113  "WHERE spcname !~ '^pg_' "
1114  "ORDER BY 1");
1115  else
1116  res = executeQuery(conn, "SELECT oid, spcname, "
1117  "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "
1118  "spclocation, spcacl, '' as rspcacl, "
1119  "null, null "
1120  "FROM pg_catalog.pg_tablespace "
1121  "WHERE spcname !~ '^pg_' "
1122  "ORDER BY 1");
1123 
1124  if (PQntuples(res) > 0)
1125  fprintf(OPF, "--\n-- Tablespaces\n--\n\n");
1126 
1127  for (i = 0; i < PQntuples(res); i++)
1128  {
1130  uint32 spcoid = atooid(PQgetvalue(res, i, 0));
1131  char *spcname = PQgetvalue(res, i, 1);
1132  char *spcowner = PQgetvalue(res, i, 2);
1133  char *spclocation = PQgetvalue(res, i, 3);
1134  char *spcacl = PQgetvalue(res, i, 4);
1135  char *rspcacl = PQgetvalue(res, i, 5);
1136  char *spcoptions = PQgetvalue(res, i, 6);
1137  char *spccomment = PQgetvalue(res, i, 7);
1138  char *fspcname;
1139 
1140  /* needed for buildACLCommands() */
1141  fspcname = pg_strdup(fmtId(spcname));
1142 
1143  appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);
1144  appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));
1145 
1146  appendPQExpBufferStr(buf, " LOCATION ");
1147  appendStringLiteralConn(buf, spclocation, conn);
1148  appendPQExpBufferStr(buf, ";\n");
1149 
1150  if (spcoptions && spcoptions[0] != '\0')
1151  appendPQExpBuffer(buf, "ALTER TABLESPACE %s SET (%s);\n",
1152  fspcname, spcoptions);
1153 
1154  if (!skip_acls &&
1155  !buildACLCommands(fspcname, NULL, "TABLESPACE", spcacl, rspcacl,
1156  spcowner, "", server_version, buf))
1157  {
1158  fprintf(stderr, _("%s: could not parse ACL list (%s) for tablespace \"%s\"\n"),
1159  progname, spcacl, fspcname);
1160  PQfinish(conn);
1161  exit_nicely(1);
1162  }
1163 
1164  if (spccomment && strlen(spccomment))
1165  {
1166  appendPQExpBuffer(buf, "COMMENT ON TABLESPACE %s IS ", fspcname);
1167  appendStringLiteralConn(buf, spccomment, conn);
1168  appendPQExpBufferStr(buf, ";\n");
1169  }
1170 
1171  if (!no_security_labels && server_version >= 90200)
1172  buildShSecLabels(conn, "pg_tablespace", spcoid,
1173  buf, "TABLESPACE", fspcname);
1174 
1175  fprintf(OPF, "%s", buf->data);
1176 
1177  free(fspcname);
1178  destroyPQExpBuffer(buf);
1179  }
1180 
1181  PQclear(res);
1182  fprintf(OPF, "\n\n");
1183 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:79
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
static const char * progname
Definition: pg_dumpall.c:61
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
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:1787
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:65
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
unsigned int uint32
Definition: c.h:265
#define exit_nicely(code)
Definition: pg_dumpall.c:82
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
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:77
static bool skip_acls
Definition: pg_dumpall.c:64
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
#define atooid(x)
Definition: lo.c:17
int i
static int no_security_labels
Definition: pg_dumpall.c:75
#define _(x)
Definition: elog.c:84
static void dumpTimestamp ( const char *  msg)
static

Definition at line 2114 of file pg_dumpall.c.

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

Referenced by main().

2115 {
2116  char buf[64];
2117  time_t now = time(NULL);
2118 
2119  if (strftime(buf, sizeof(buf), PGDUMP_STRFTIME_FMT, localtime(&now)) != 0)
2120  fprintf(OPF, "-- %s %s\n\n", msg, buf);
2121 }
static FILE * OPF
Definition: pg_dumpall.c:79
static char * buf
Definition: pg_test_fsync.c:65
#define NULL
Definition: c.h:226
#define PGDUMP_STRFTIME_FMT
Definition: dumputils.h:33
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1533
static void dumpUserConfig ( PGconn conn,
const char *  username 
)
static

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

1549 {
1551  int count = 1;
1552 
1553  for (;;)
1554  {
1555  PGresult *res;
1556 
1557  if (server_version >= 90000)
1558  printfPQExpBuffer(buf, "SELECT setconfig[%d] FROM pg_db_role_setting WHERE "
1559  "setdatabase = 0 AND setrole = "
1560  "(SELECT oid FROM pg_authid WHERE rolname = ", count);
1561  else if (server_version >= 80100)
1562  printfPQExpBuffer(buf, "SELECT rolconfig[%d] FROM pg_authid WHERE rolname = ", count);
1563  else
1564  printfPQExpBuffer(buf, "SELECT useconfig[%d] FROM pg_shadow WHERE usename = ", count);
1565  appendStringLiteralConn(buf, username, conn);
1566  if (server_version >= 90000)
1567  appendPQExpBufferChar(buf, ')');
1568 
1569  res = executeQuery(conn, buf->data);
1570  if (PQntuples(res) == 1 &&
1571  !PQgetisnull(res, 0, 0))
1572  {
1573  makeAlterConfigCommand(conn, PQgetvalue(res, 0, 0),
1574  "ROLE", username, NULL, NULL);
1575  PQclear(res);
1576  count++;
1577  }
1578  else
1579  {
1580  PQclear(res);
1581  break;
1582  }
1583  }
1584 
1585  destroyPQExpBuffer(buf);
1586 }
static PGresult * executeQuery(PGconn *conn, const char *query)
Definition: pg_dumpall.c:2061
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:65
static void makeAlterConfigCommand(PGconn *conn, const char *arrayitem, const char *type, const char *name, const char *type2, const char *name2)
Definition: pg_dumpall.c:1627
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
static char * username
Definition: initdb.c:129
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:226
static int server_version
Definition: pg_dumpall.c:77
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 executeCommand ( PGconn conn,
const char *  query 
)
static

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

2088 {
2089  PGresult *res;
2090 
2091  if (verbose)
2092  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2093 
2094  res = PQexec(conn, query);
2095  if (!res ||
2097  {
2098  fprintf(stderr, _("%s: query failed: %s"),
2099  progname, PQerrorMessage(conn));
2100  fprintf(stderr, _("%s: query was: %s\n"),
2101  progname, query);
2102  PQfinish(conn);
2103  exit_nicely(1);
2104  }
2105 
2106  PQclear(res);
2107 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
static const char * progname
Definition: pg_dumpall.c:61
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static bool verbose
Definition: pg_dumpall.c:65
#define exit_nicely(code)
Definition: pg_dumpall.c:82
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 2061 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(), main(), reindex_all_databases(), vacuum_all_databases(), and vacuum_one_database().

2062 {
2063  PGresult *res;
2064 
2065  if (verbose)
2066  fprintf(stderr, _("%s: executing %s\n"), progname, query);
2067 
2068  res = PQexec(conn, query);
2069  if (!res ||
2071  {
2072  fprintf(stderr, _("%s: query failed: %s"),
2073  progname, PQerrorMessage(conn));
2074  fprintf(stderr, _("%s: query was: %s\n"),
2075  progname, query);
2076  PQfinish(conn);
2077  exit_nicely(1);
2078  }
2079 
2080  return res;
2081 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5959
static const char * progname
Definition: pg_dumpall.c:61
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3516
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static bool verbose
Definition: pg_dumpall.c:65
#define exit_nicely(code)
Definition: pg_dumpall.c:82
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 534 of file pg_dumpall.c.

References _, and progname.

Referenced by main().

535 {
536  printf(_("%s extracts a PostgreSQL database cluster into an SQL script file.\n\n"), progname);
537  printf(_("Usage:\n"));
538  printf(_(" %s [OPTION]...\n"), progname);
539 
540  printf(_("\nGeneral options:\n"));
541  printf(_(" -f, --file=FILENAME output file name\n"));
542  printf(_(" -v, --verbose verbose mode\n"));
543  printf(_(" -V, --version output version information, then exit\n"));
544  printf(_(" --lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock\n"));
545  printf(_(" -?, --help show this help, then exit\n"));
546  printf(_("\nOptions controlling the output content:\n"));
547  printf(_(" -a, --data-only dump only the data, not the schema\n"));
548  printf(_(" -c, --clean clean (drop) databases before recreating\n"));
549  printf(_(" -g, --globals-only dump only global objects, no databases\n"));
550  printf(_(" -o, --oids include OIDs in dump\n"));
551  printf(_(" -O, --no-owner skip restoration of object ownership\n"));
552  printf(_(" -r, --roles-only dump only roles, no databases or tablespaces\n"));
553  printf(_(" -s, --schema-only dump only the schema, no data\n"));
554  printf(_(" -S, --superuser=NAME superuser user name to use in the dump\n"));
555  printf(_(" -t, --tablespaces-only dump only tablespaces, no databases or roles\n"));
556  printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
557  printf(_(" --binary-upgrade for use by upgrade utilities only\n"));
558  printf(_(" --column-inserts dump data as INSERT commands with column names\n"));
559  printf(_(" --disable-dollar-quoting disable dollar quoting, use SQL standard quoting\n"));
560  printf(_(" --disable-triggers disable triggers during data-only restore\n"));
561  printf(_(" --if-exists use IF EXISTS when dropping objects\n"));
562  printf(_(" --inserts dump data as INSERT commands, rather than COPY\n"));
563  printf(_(" --no-security-labels do not dump security label assignments\n"));
564  printf(_(" --no-tablespaces do not dump tablespace assignments\n"));
565  printf(_(" --no-unlogged-table-data do not dump unlogged table data\n"));
566  printf(_(" --quote-all-identifiers quote all identifiers, even if not key words\n"));
567  printf(_(" --use-set-session-authorization\n"
568  " use SET SESSION AUTHORIZATION commands instead of\n"
569  " ALTER OWNER commands to set ownership\n"));
570 
571  printf(_("\nConnection options:\n"));
572  printf(_(" -d, --dbname=CONNSTR connect using connection string\n"));
573  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
574  printf(_(" -l, --database=DBNAME alternative default database\n"));
575  printf(_(" -p, --port=PORT database server port number\n"));
576  printf(_(" -U, --username=NAME connect as specified database user\n"));
577  printf(_(" -w, --no-password never prompt for password\n"));
578  printf(_(" -W, --password force password prompt (should happen automatically)\n"));
579  printf(_(" --role=ROLENAME do SET ROLE before dump\n"));
580 
581  printf(_("\nIf -f/--file is not used, then the SQL script will be written to the standard\n"
582  "output.\n\n"));
583  printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
584 }
static const char * progname
Definition: pg_dumpall.c:61
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char *  argv[] 
)

Definition at line 85 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, dropDBs(), dropRoles(), dropTablespaces(), dumpCreateDB(), dumpDatabases(), dumpDbRoleConfig(), dumpGroups(), dumpRoleMembership(), dumpRoles(), dumpTablespaces(), dumpTimestamp(), encoding, executeCommand(), exit_nicely, filename, find_my_exec(), find_other_exec(), fmtId(), get_progname(), getopt_long(), help(), if_exists, inserts, MAXPGPATH, no_argument, no_security_labels, no_tablespaces, no_unlogged_table_data, NULL, OPF, optarg, optind, PG_BINARY_W, pg_dump_bin, pg_encoding_to_char(), pg_strdup(), PG_TEXTDOMAIN, PGDUMP_VERSIONSTR, pghost, pgport, PQclientEncoding(), PQfinish(), PQparameterStatus(), progname, quote_all_identifiers, required_argument, server_version, set_pglocale_pgservice(), skip_acls, strerror(), strlcpy(), TRI_DEFAULT, TRI_NO, TRI_YES, use_setsessauth, and verbose.

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

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

1630 {
1631  char *pos;
1632  char *mine;
1633  PQExpBuffer buf;
1634 
1635  mine = pg_strdup(arrayitem);
1636  pos = strchr(mine, '=');
1637  if (pos == NULL)
1638  {
1639  free(mine);
1640  return;
1641  }
1642 
1643  buf = createPQExpBuffer();
1644 
1645  *pos = 0;
1646  appendPQExpBuffer(buf, "ALTER %s %s ", type, fmtId(name));
1647  if (type2 != NULL && name2 != NULL)
1648  appendPQExpBuffer(buf, "IN %s %s ", type2, fmtId(name2));
1649  appendPQExpBuffer(buf, "SET %s TO ", fmtId(mine));
1650 
1651  /*
1652  * Some GUC variable names are 'LIST' type and hence must not be quoted.
1653  */
1654  if (pg_strcasecmp(mine, "DateStyle") == 0
1655  || pg_strcasecmp(mine, "search_path") == 0)
1656  appendPQExpBufferStr(buf, pos + 1);
1657  else
1658  appendStringLiteralConn(buf, pos + 1, conn);
1659  appendPQExpBufferStr(buf, ";\n");
1660 
1661  fprintf(OPF, "%s", buf->data);
1662  destroyPQExpBuffer(buf);
1663  free(mine);
1664 }
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
static FILE * OPF
Definition: pg_dumpall.c:79
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:65
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define free(a)
Definition: header.h:60
#define NULL
Definition: c.h:226
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 1737 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().

1738 {
1739  PQExpBuffer connstrbuf = createPQExpBuffer();
1741  int ret;
1742 
1743  appendPQExpBuffer(cmd, "\"%s\" %s", pg_dump_bin,
1744  pgdumpopts->data);
1745 
1746  /*
1747  * If we have a filename, use the undocumented plain-append pg_dump
1748  * format.
1749  */
1750  if (filename)
1751  appendPQExpBufferStr(cmd, " -Fa ");
1752  else
1753  appendPQExpBufferStr(cmd, " -Fp ");
1754 
1755  /*
1756  * Append the database name to the already-constructed stem of connection
1757  * string.
1758  */
1759  appendPQExpBuffer(connstrbuf, "%s dbname=", connstr);
1760  appendConnStrVal(connstrbuf, dbname);
1761 
1762  appendShellString(cmd, connstrbuf->data);
1763 
1764  if (verbose)
1765  fprintf(stderr, _("%s: running \"%s\"\n"), progname, cmd->data);
1766 
1767  fflush(stdout);
1768  fflush(stderr);
1769 
1770  ret = system(cmd->data);
1771 
1772  destroyPQExpBuffer(cmd);
1773  destroyPQExpBuffer(connstrbuf);
1774 
1775  return ret;
1776 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:536
static const char * progname
Definition: pg_dumpall.c:61
static bool verbose
Definition: pg_dumpall.c:65
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:62
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
void appendShellString(PQExpBuffer buf, const char *str)
Definition: string_utils.c:430
static char pg_dump_bin[MAXPGPATH]
Definition: pg_dumpall.c:60
char * dbname
Definition: streamutil.c:38
static char * filename
Definition: pg_dumpall.c:80
#define _(x)
Definition: elog.c:84
static char * connstr
Definition: pg_dumpall.c:63

Variable Documentation

int binary_upgrade = 0
static

Definition at line 67 of file pg_dumpall.c.

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

int column_inserts = 0
static

Definition at line 68 of file pg_dumpall.c.

Referenced by main().

int disable_dollar_quoting = 0
static

Definition at line 69 of file pg_dumpall.c.

Referenced by main().

int disable_triggers = 0
static

Definition at line 70 of file pg_dumpall.c.

Referenced by main().

int if_exists = 0
static

Definition at line 71 of file pg_dumpall.c.

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

int inserts = 0
static

Definition at line 72 of file pg_dumpall.c.

Referenced by main().

int no_security_labels = 0
static

Definition at line 75 of file pg_dumpall.c.

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

int no_tablespaces = 0
static

Definition at line 73 of file pg_dumpall.c.

Referenced by dumpCreateDB(), and main().

int no_unlogged_table_data = 0
static

Definition at line 76 of file pg_dumpall.c.

Referenced by main().

char pg_dump_bin[MAXPGPATH]
static

Definition at line 60 of file pg_dumpall.c.

Referenced by main(), and runPgDump().

PQExpBuffer pgdumpopts
static

Definition at line 62 of file pg_dumpall.c.

const char* progname
static
bool skip_acls = false
static

Definition at line 64 of file pg_dumpall.c.

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

int use_setsessauth = 0
static

Definition at line 74 of file pg_dumpall.c.

Referenced by main().

bool verbose = false
static

Definition at line 65 of file pg_dumpall.c.

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