2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* dump.c
|
|
|
|
*
|
|
|
|
* dump functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2014-01-08 05:05:30 +08:00
|
|
|
* Copyright (c) 2010-2014, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/dump.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 21:33:40 +08:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 09:16:24 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
#include <sys/types.h>
|
2014-08-08 02:56:13 +08:00
|
|
|
#include "catalog/binary_upgrade.h"
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
generate_old_dump(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-12-01 05:30:13 +08:00
|
|
|
int dbnum;
|
2013-07-25 23:33:15 +08:00
|
|
|
mode_t old_umask;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-12-01 11:32:19 +08:00
|
|
|
prep_status("Creating dump of global objects");
|
2012-12-01 05:30:13 +08:00
|
|
|
|
|
|
|
/* run new pg_dumpall binary for globals */
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
2014-07-01 07:55:55 +08:00
|
|
|
"\"%s/pg_dumpall\" %s --globals-only --quote-all-identifiers "
|
|
|
|
"--binary-upgrade %s -f %s",
|
2012-09-04 01:52:34 +08:00
|
|
|
new_cluster.bindir, cluster_conn_opts(&old_cluster),
|
2012-03-13 07:47:54 +08:00
|
|
|
log_opts.verbose ? "--verbose" : "",
|
2012-12-01 05:30:13 +08:00
|
|
|
GLOBALS_DUMP_FILE);
|
2012-12-01 11:32:19 +08:00
|
|
|
check_ok();
|
|
|
|
|
|
|
|
prep_status("Creating dump of database schemas\n");
|
2012-09-06 05:41:43 +08:00
|
|
|
|
2013-07-25 23:33:15 +08:00
|
|
|
/*
|
|
|
|
* Set umask for this function, all functions it calls, and all
|
2014-05-07 00:12:18 +08:00
|
|
|
* subprocesses/threads it creates. We can't use fopen_priv() as Windows
|
|
|
|
* uses threads and umask is process-global.
|
2013-07-25 23:33:15 +08:00
|
|
|
*/
|
|
|
|
old_umask = umask(S_IRWXG | S_IRWXO);
|
|
|
|
|
2013-05-10 05:34:34 +08:00
|
|
|
/* create per-db dump files */
|
2012-12-01 05:30:13 +08:00
|
|
|
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2013-05-10 05:34:34 +08:00
|
|
|
char sql_file_name[MAXPGPATH],
|
|
|
|
log_file_name[MAXPGPATH];
|
|
|
|
DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-12-08 01:26:13 +08:00
|
|
|
pg_log(PG_STATUS, "%s", old_db->db_name);
|
2012-12-27 08:26:30 +08:00
|
|
|
snprintf(sql_file_name, sizeof(sql_file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
|
|
|
|
snprintf(log_file_name, sizeof(log_file_name), DB_DUMP_LOG_FILE_MASK, old_db->db_oid);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
parallel_exec_prog(log_file_name, NULL,
|
2013-05-10 05:34:34 +08:00
|
|
|
"\"%s/pg_dump\" %s --schema-only --quote-all-identifiers "
|
|
|
|
"--binary-upgrade --format=custom %s --file=\"%s\" \"%s\"",
|
|
|
|
new_cluster.bindir, cluster_conn_opts(&old_cluster),
|
|
|
|
log_opts.verbose ? "--verbose" : "",
|
|
|
|
sql_file_name, old_db->db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
2012-12-27 08:26:30 +08:00
|
|
|
/* reap all children */
|
|
|
|
while (reap_child(true) == true)
|
|
|
|
;
|
2013-05-10 05:34:34 +08:00
|
|
|
|
2013-07-25 23:33:15 +08:00
|
|
|
umask(old_umask);
|
|
|
|
|
2012-12-01 05:30:13 +08:00
|
|
|
end_progress_output();
|
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
2014-08-08 02:56:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is possible for there to be a mismatch in the need for TOAST tables
|
|
|
|
* between the old and new servers, e.g. some pre-9.1 tables didn't need
|
|
|
|
* TOAST tables but will need them in 9.1+. (There are also opposite cases,
|
|
|
|
* but these are handled by setting binary_upgrade_next_toast_pg_class_oid.)
|
|
|
|
*
|
|
|
|
* We can't allow the TOAST table to be created by pg_dump with a
|
|
|
|
* pg_dump-assigned oid because it might conflict with a later table that
|
|
|
|
* uses that oid, causing a "file exists" error for pg_class conflicts, and
|
|
|
|
* a "duplicate oid" error for pg_type conflicts. (TOAST tables need pg_type
|
|
|
|
* entries.)
|
|
|
|
*
|
|
|
|
* Therefore, a backend in binary-upgrade mode will not create a TOAST
|
|
|
|
* table unless an OID as passed in via pg_upgrade_support functions.
|
|
|
|
* This function is called after the restore and uses ALTER TABLE to
|
|
|
|
* auto-create any needed TOAST tables which will not conflict with
|
|
|
|
* restored oids.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
optionally_create_toast_tables(void)
|
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
|
|
|
|
prep_status("Creating newly-required TOAST tables");
|
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
|
|
|
|
{
|
|
|
|
PGresult *res;
|
|
|
|
int ntups;
|
|
|
|
int rowno;
|
|
|
|
int i_nspname,
|
|
|
|
i_relname;
|
|
|
|
DbInfo *active_db = &new_cluster.dbarr.dbs[dbnum];
|
|
|
|
PGconn *conn = connectToServer(&new_cluster, active_db->db_name);
|
|
|
|
|
|
|
|
res = executeQueryOrDie(conn,
|
|
|
|
"SELECT n.nspname, c.relname "
|
|
|
|
"FROM pg_catalog.pg_class c, "
|
|
|
|
" pg_catalog.pg_namespace n "
|
|
|
|
"WHERE c.relnamespace = n.oid AND "
|
|
|
|
" n.nspname NOT IN ('pg_catalog', 'information_schema') AND "
|
|
|
|
"c.relkind IN ('r', 'm') AND "
|
|
|
|
"c.reltoastrelid = 0");
|
|
|
|
|
2014-08-26 05:06:40 +08:00
|
|
|
/* Suppress NOTICE output from non-existant constraints */
|
|
|
|
PQclear(executeQueryOrDie(conn, "SET client_min_messages = warning;"));
|
|
|
|
PQclear(executeQueryOrDie(conn, "SET log_min_messages = warning;"));
|
2014-08-26 05:24:33 +08:00
|
|
|
PQclear(executeQueryOrDie(conn, "SET log_min_error_statement = warning;"));
|
2014-08-26 05:06:40 +08:00
|
|
|
|
2014-08-08 02:56:13 +08:00
|
|
|
ntups = PQntuples(res);
|
|
|
|
i_nspname = PQfnumber(res, "nspname");
|
|
|
|
i_relname = PQfnumber(res, "relname");
|
|
|
|
for (rowno = 0; rowno < ntups; rowno++)
|
|
|
|
{
|
|
|
|
/* enable auto-oid-numbered TOAST creation if needed */
|
|
|
|
PQclear(executeQueryOrDie(conn, "SELECT binary_upgrade.set_next_toast_pg_class_oid('%d'::pg_catalog.oid);",
|
|
|
|
OPTIONALLY_CREATE_TOAST_OID));
|
|
|
|
|
|
|
|
/* dummy command that also triggers check for required TOAST table */
|
2014-08-26 05:06:40 +08:00
|
|
|
PQclear(executeQueryOrDie(conn, "ALTER TABLE %s.%s DROP CONSTRAINT IF EXISTS binary_upgrade_dummy_constraint;",
|
2014-08-08 02:56:13 +08:00
|
|
|
quote_identifier(PQgetvalue(res, rowno, i_nspname)),
|
|
|
|
quote_identifier(PQgetvalue(res, rowno, i_relname))));
|
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
2014-08-26 05:06:40 +08:00
|
|
|
PQclear(executeQueryOrDie(conn, "RESET client_min_messages;"));
|
|
|
|
PQclear(executeQueryOrDie(conn, "RESET log_min_messages;"));
|
2014-08-26 05:24:33 +08:00
|
|
|
PQclear(executeQueryOrDie(conn, "RESET log_min_error_statement;"));
|
2014-08-26 05:06:40 +08:00
|
|
|
|
2014-08-08 02:56:13 +08:00
|
|
|
PQfinish(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
check_ok();
|
|
|
|
}
|