2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* pg_upgrade.c
|
|
|
|
*
|
|
|
|
* main source file
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2012-01-02 07:01:58 +08:00
|
|
|
* Copyright (c) 2010-2012, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/pg_upgrade.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
2011-01-07 11:44:57 +08:00
|
|
|
/*
|
2011-01-08 10:25:34 +08:00
|
|
|
* To simplify the upgrade process, we force certain system values to be
|
|
|
|
* identical between old and new clusters:
|
2011-01-07 11:44:57 +08:00
|
|
|
*
|
2011-01-08 10:25:34 +08:00
|
|
|
* We control all assignments of pg_class.oid (and relfilenode) so toast
|
|
|
|
* oids are the same between old and new clusters. This is important
|
|
|
|
* because toast oids are stored as toast pointers in user tables.
|
2011-01-07 11:44:57 +08:00
|
|
|
*
|
2012-04-24 10:43:09 +08:00
|
|
|
* FYI, while pg_class.oid and pg_class.relfilenode are initially the same
|
2011-01-08 11:46:41 +08:00
|
|
|
* in a cluster, but they can diverge due to CLUSTER, REINDEX, or VACUUM
|
|
|
|
* FULL. The new cluster will have matching pg_class.oid and
|
2011-04-10 23:42:00 +08:00
|
|
|
* pg_class.relfilenode values and be based on the old oid value. This can
|
2011-01-08 11:57:30 +08:00
|
|
|
* cause the old and new pg_class.relfilenode values to differ. In summary,
|
|
|
|
* old and new pg_class.oid and new pg_class.relfilenode will have the
|
|
|
|
* same value, and old pg_class.relfilenode might differ.
|
2011-01-08 10:25:34 +08:00
|
|
|
*
|
2011-01-08 11:46:41 +08:00
|
|
|
* We control all assignments of pg_type.oid because these oids are stored
|
2011-01-08 10:25:34 +08:00
|
|
|
* in user composite type values.
|
|
|
|
*
|
2011-01-08 11:46:41 +08:00
|
|
|
* We control all assignments of pg_enum.oid because these oids are stored
|
2011-01-08 10:25:34 +08:00
|
|
|
* in user tables as enum values.
|
2011-01-08 12:02:03 +08:00
|
|
|
*
|
2012-06-14 00:19:18 +08:00
|
|
|
* We control all assignments of pg_authid.oid because these oids are stored
|
2011-01-08 12:02:03 +08:00
|
|
|
* in pg_largeobject_metadata.
|
2011-01-07 11:44:57 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-08-27 09:16:24 +08:00
|
|
|
|
|
|
|
#include "postgres.h"
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_LANGINFO_H
|
|
|
|
#include <langinfo.h>
|
|
|
|
#endif
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
static void prepare_new_cluster(void);
|
|
|
|
static void prepare_new_databases(void);
|
|
|
|
static void create_new_objects(void);
|
|
|
|
static void copy_clog_xlog_xid(void);
|
|
|
|
static void set_frozenxids(void);
|
|
|
|
static void setup(char *argv0, bool live_check);
|
|
|
|
static void cleanup(void);
|
|
|
|
|
2011-04-10 23:42:00 +08:00
|
|
|
ClusterInfo old_cluster,
|
|
|
|
new_cluster;
|
2010-10-20 05:38:16 +08:00
|
|
|
OSInfo os_info;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-06-11 03:20:04 +08:00
|
|
|
char *output_files[] = {
|
2012-03-13 07:47:54 +08:00
|
|
|
SERVER_LOG_FILE,
|
2012-05-24 08:19:21 +08:00
|
|
|
#ifdef WIN32
|
2012-05-25 21:09:51 +08:00
|
|
|
/* unique file for pg_ctl start */
|
2012-05-25 08:30:39 +08:00
|
|
|
SERVER_START_LOG_FILE,
|
2012-05-24 08:19:21 +08:00
|
|
|
#endif
|
2012-03-13 07:47:54 +08:00
|
|
|
RESTORE_LOG_FILE,
|
|
|
|
UTILITY_LOG_FILE,
|
2012-05-24 08:19:21 +08:00
|
|
|
INTERNAL_LOG_FILE,
|
|
|
|
NULL
|
2012-03-13 07:47:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *sequence_script_file_name = NULL;
|
2012-03-17 06:54:11 +08:00
|
|
|
char *analyze_script_file_name = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
char *deletion_script_file_name = NULL;
|
|
|
|
bool live_check = false;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
parseCommandLine(argc, argv);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-10-08 02:40:23 +08:00
|
|
|
adjust_data_dir(&old_cluster);
|
|
|
|
adjust_data_dir(&new_cluster);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
output_check_banner(&live_check);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
setup(argv[0], live_check);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
check_cluster_versions();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-09-04 01:52:34 +08:00
|
|
|
get_sock_dir(&old_cluster, live_check);
|
|
|
|
get_sock_dir(&new_cluster, false);
|
|
|
|
|
2012-09-04 10:15:09 +08:00
|
|
|
check_cluster_compatibility(live_check);
|
|
|
|
|
2012-12-01 05:30:13 +08:00
|
|
|
check_and_dump_old_cluster(live_check, &sequence_script_file_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* -- NEW -- */
|
2011-04-26 08:17:48 +08:00
|
|
|
start_postmaster(&new_cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
check_new_cluster();
|
|
|
|
report_clusters_compatible();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-12-15 20:11:31 +08:00
|
|
|
pg_log(PG_REPORT, "\nPerforming Upgrade\n");
|
|
|
|
pg_log(PG_REPORT, "------------------\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prepare_new_cluster();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-04-26 08:17:48 +08:00
|
|
|
stop_postmaster(false);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destructive Changes to New Cluster
|
|
|
|
*/
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
copy_clog_xlog_xid();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* New now using xids of the old system */
|
|
|
|
|
2011-04-26 08:17:48 +08:00
|
|
|
/* -- NEW -- */
|
|
|
|
start_postmaster(&new_cluster);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prepare_new_databases();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
create_new_objects();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-04-26 08:17:48 +08:00
|
|
|
stop_postmaster(false);
|
|
|
|
|
2012-03-06 10:19:54 +08:00
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* Most failures happen in create_new_objects(), which has completed at
|
|
|
|
* this point. We do this here because it is just before linking, which
|
|
|
|
* will link the old and new cluster data files, preventing the old
|
|
|
|
* cluster from being safely started once the new cluster is started.
|
2012-03-06 10:19:54 +08:00
|
|
|
*/
|
|
|
|
if (user_opts.transfer_mode == TRANSFER_MODE_LINK)
|
|
|
|
disable_old_cluster();
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_all_new_dbs(&old_cluster.dbarr, &new_cluster.dbarr,
|
|
|
|
old_cluster.pgdata, new_cluster.pgdata);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Assuming OIDs are only used in system tables, there is no need to
|
|
|
|
* restore the OID counter because we have not transferred any OIDs from
|
|
|
|
* the old system, but we do it anyway just in case. We do it late here
|
|
|
|
* because there is no need to have the schema load use new oids.
|
|
|
|
*/
|
2011-07-12 12:13:51 +08:00
|
|
|
prep_status("Setting next OID for new cluster");
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
|
|
|
"\"%s/pg_resetxlog\" -o %u \"%s\"",
|
2012-03-13 07:47:54 +08:00
|
|
|
new_cluster.bindir, old_cluster.controldata.chkpnt_nxtoid,
|
2012-08-28 02:21:09 +08:00
|
|
|
new_cluster.pgdata);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-12-04 11:47:59 +08:00
|
|
|
prep_status("Sync data directory to disk");
|
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
|
|
|
"\"%s/initdb\" --sync-only \"%s\"", new_cluster.bindir,
|
|
|
|
new_cluster.pgdata);
|
|
|
|
check_ok();
|
|
|
|
|
2012-03-17 06:54:11 +08:00
|
|
|
create_script_for_cluster_analyze(&analyze_script_file_name);
|
2010-10-20 05:38:16 +08:00
|
|
|
create_script_for_old_cluster_deletion(&deletion_script_file_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
issue_warnings(sequence_script_file_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-03-15 07:22:01 +08:00
|
|
|
pg_log(PG_REPORT, "\nUpgrade Complete\n");
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, "----------------\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-03-17 06:54:11 +08:00
|
|
|
output_completion_banner(analyze_script_file_name,
|
|
|
|
deletion_script_file_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-03-17 06:54:11 +08:00
|
|
|
pg_free(analyze_script_file_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
pg_free(deletion_script_file_name);
|
|
|
|
pg_free(sequence_script_file_name);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
cleanup();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
setup(char *argv0, bool live_check)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
char exec_path[MAXPGPATH]; /* full path to my executable */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make sure the user has a clean environment, otherwise, we may confuse
|
|
|
|
* libpq when we connect to one (or both) of the servers.
|
|
|
|
*/
|
2011-05-16 22:46:52 +08:00
|
|
|
check_pghost_envvar();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
verify_directories();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* no postmasters should be running */
|
2010-10-20 05:38:16 +08:00
|
|
|
if (!live_check && is_server_running(old_cluster.pgdata))
|
|
|
|
pg_log(PG_FATAL, "There seems to be a postmaster servicing the old cluster.\n"
|
2010-05-12 10:19:11 +08:00
|
|
|
"Please shutdown that postmaster and try again.\n");
|
|
|
|
|
|
|
|
/* same goes for the new postmaster */
|
2010-10-20 05:38:16 +08:00
|
|
|
if (is_server_running(new_cluster.pgdata))
|
|
|
|
pg_log(PG_FATAL, "There seems to be a postmaster servicing the new cluster.\n"
|
2010-05-12 10:19:11 +08:00
|
|
|
"Please shutdown that postmaster and try again.\n");
|
|
|
|
|
|
|
|
/* get path to pg_upgrade executable */
|
|
|
|
if (find_my_exec(argv0, exec_path) < 0)
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "Could not get path name to pg_upgrade: %s\n", getErrorText(errno));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* Trim off program name and keep just path */
|
|
|
|
*last_dir_separator(exec_path) = '\0';
|
|
|
|
canonicalize_path(exec_path);
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.exec_path = pg_strdup(exec_path);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
prepare_new_cluster(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* It would make more sense to freeze after loading the schema, but that
|
|
|
|
* would cause us to lose the frozenids restored by the load. We use
|
|
|
|
* --analyze so autovacuum doesn't update statistics later
|
|
|
|
*/
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Analyzing all rows in the new cluster");
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
2012-09-04 01:52:34 +08:00
|
|
|
"\"%s/vacuumdb\" %s --all --analyze %s",
|
|
|
|
new_cluster.bindir, cluster_conn_opts(&new_cluster),
|
2012-08-28 02:21:09 +08:00
|
|
|
log_opts.verbose ? "--verbose" : "");
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
2010-07-07 03:19:02 +08:00
|
|
|
* We do freeze after analyze so pg_statistic is also frozen. template0 is
|
|
|
|
* not frozen here, but data rows were frozen by initdb, and we set its
|
|
|
|
* datfrozenxid and relfrozenxids later to match the new xid counter
|
|
|
|
* later.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Freezing all rows on the new cluster");
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
2012-09-04 01:52:34 +08:00
|
|
|
"\"%s/vacuumdb\" %s --all --freeze %s",
|
|
|
|
new_cluster.bindir, cluster_conn_opts(&new_cluster),
|
2012-08-28 02:21:09 +08:00
|
|
|
log_opts.verbose ? "--verbose" : "");
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
get_pg_database_relfilenode(&new_cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
prepare_new_databases(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We set autovacuum_freeze_max_age to its maximum value so autovacuum
|
|
|
|
* does not launch here and delete clog files, before the frozen xids are
|
|
|
|
* set.
|
|
|
|
*/
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
set_frozenxids();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-12-01 11:32:19 +08:00
|
|
|
prep_status("Restoring global objects in the new cluster");
|
2011-01-08 10:59:29 +08:00
|
|
|
|
2011-01-08 22:11:48 +08:00
|
|
|
/*
|
2011-10-29 09:18:36 +08:00
|
|
|
* Install support functions in the global-object restore database to
|
2012-06-11 03:20:04 +08:00
|
|
|
* preserve pg_authid.oid. pg_dumpall uses 'template0' as its template
|
|
|
|
* database so objects we add into 'template1' are not propogated. They
|
2011-10-29 09:18:36 +08:00
|
|
|
* are removed on pg_upgrade exit.
|
2011-01-08 22:11:48 +08:00
|
|
|
*/
|
2011-10-29 09:18:36 +08:00
|
|
|
install_support_functions_in_new_db("template1");
|
2011-01-08 10:59:29 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
2011-01-08 10:59:29 +08:00
|
|
|
* We have to create the databases first so we can install support
|
2011-04-10 23:42:00 +08:00
|
|
|
* functions in all the other databases. Ideally we could create the
|
|
|
|
* support functions in template1 but pg_dumpall creates database using
|
|
|
|
* the template0 template.
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(RESTORE_LOG_FILE, NULL, true,
|
2012-09-04 01:52:34 +08:00
|
|
|
"\"%s/psql\" " EXEC_PSQL_ARGS " %s -f \"%s\"",
|
|
|
|
new_cluster.bindir, cluster_conn_opts(&new_cluster),
|
2012-08-28 02:21:09 +08:00
|
|
|
GLOBALS_DUMP_FILE);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-09 02:44:44 +08:00
|
|
|
/* we load this to get a current list of databases */
|
2011-01-02 01:06:36 +08:00
|
|
|
get_db_and_rel_infos(&new_cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
create_new_objects(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-08 10:59:29 +08:00
|
|
|
int dbnum;
|
|
|
|
|
|
|
|
prep_status("Adding support functions to new cluster");
|
|
|
|
|
2012-12-01 05:30:13 +08:00
|
|
|
/*
|
|
|
|
* Technically, we only need to install these support functions in new
|
|
|
|
* databases that also exist in the old cluster, but for completeness
|
|
|
|
* we process all new databases.
|
|
|
|
*/
|
2011-01-08 10:59:29 +08:00
|
|
|
for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
|
|
|
|
{
|
|
|
|
DbInfo *new_db = &new_cluster.dbarr.dbs[dbnum];
|
|
|
|
|
2011-01-08 22:11:48 +08:00
|
|
|
/* skip db we already installed */
|
2011-10-29 09:18:36 +08:00
|
|
|
if (strcmp(new_db->db_name, "template1") != 0)
|
2011-01-09 02:44:44 +08:00
|
|
|
install_support_functions_in_new_db(new_db->db_name);
|
2011-01-08 10:59:29 +08:00
|
|
|
}
|
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-12-01 11:32:19 +08:00
|
|
|
prep_status("Restoring database schemas in the new cluster\n");
|
2012-12-01 05:30:13 +08:00
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
|
|
|
|
{
|
|
|
|
char file_name[MAXPGPATH];
|
|
|
|
DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
|
|
|
|
|
2012-12-08 01:26:13 +08:00
|
|
|
pg_log(PG_STATUS, "%s", old_db->db_name);
|
2012-12-01 05:30:13 +08:00
|
|
|
snprintf(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Using pg_restore --single-transaction is faster than other
|
2012-12-01 23:21:45 +08:00
|
|
|
* methods, like --jobs. pg_dump only produces its output at the
|
2012-12-01 05:30:13 +08:00
|
|
|
* end, so there is little parallelism using the pipe.
|
|
|
|
*/
|
|
|
|
exec_prog(RESTORE_LOG_FILE, NULL, true,
|
2012-12-01 23:21:45 +08:00
|
|
|
"\"%s/pg_restore\" %s --exit-on-error --single-transaction --verbose --dbname \"%s\" \"%s\"",
|
2012-12-01 05:30:13 +08:00
|
|
|
new_cluster.bindir, cluster_conn_opts(&new_cluster),
|
|
|
|
old_db->db_name, file_name);
|
|
|
|
}
|
|
|
|
end_progress_output();
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-09 02:44:44 +08:00
|
|
|
/* regenerate now that we have objects in the databases */
|
2011-01-02 01:06:36 +08:00
|
|
|
get_db_and_rel_infos(&new_cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-09 02:44:44 +08:00
|
|
|
uninstall_support_functions_from_new_cluster();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
2012-07-05 23:38:42 +08:00
|
|
|
/*
|
|
|
|
* Delete the given subdirectory contents from the new cluster, and copy the
|
|
|
|
* files from the old cluster into it.
|
|
|
|
*/
|
2010-05-12 10:19:11 +08:00
|
|
|
static void
|
2012-07-05 23:38:42 +08:00
|
|
|
copy_subdir_files(char *subdir)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-07-05 23:38:42 +08:00
|
|
|
char old_path[MAXPGPATH];
|
|
|
|
char new_path[MAXPGPATH];
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-07-05 23:38:42 +08:00
|
|
|
prep_status("Deleting files from new %s", subdir);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-07-05 23:38:42 +08:00
|
|
|
snprintf(old_path, sizeof(old_path), "%s/%s", old_cluster.pgdata, subdir);
|
|
|
|
snprintf(new_path, sizeof(new_path), "%s/%s", new_cluster.pgdata, subdir);
|
|
|
|
if (!rmtree(new_path, true))
|
|
|
|
pg_log(PG_FATAL, "could not delete directory \"%s\"\n", new_path);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-07-05 23:38:42 +08:00
|
|
|
prep_status("Copying old %s to new server", subdir);
|
|
|
|
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
2010-05-12 10:19:11 +08:00
|
|
|
#ifndef WIN32
|
2012-08-28 02:21:09 +08:00
|
|
|
"cp -Rf \"%s\" \"%s\"",
|
2010-05-12 10:19:11 +08:00
|
|
|
#else
|
|
|
|
/* flags: everything, no confirm, quiet, overwrite read-only */
|
2012-08-28 02:21:09 +08:00
|
|
|
"xcopy /e /y /q /r \"%s\" \"%s\\\"",
|
2010-05-12 10:19:11 +08:00
|
|
|
#endif
|
2012-08-28 02:21:09 +08:00
|
|
|
old_path, new_path);
|
2012-07-05 23:38:42 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2012-07-05 23:38:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
copy_clog_xlog_xid(void)
|
|
|
|
{
|
|
|
|
/* copy old commit logs to new data dir */
|
|
|
|
copy_subdir_files("pg_clog");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* set the next transaction id of the new cluster */
|
2011-07-12 12:13:51 +08:00
|
|
|
prep_status("Setting next transaction ID for new cluster");
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
|
|
|
"\"%s/pg_resetxlog\" -f -x %u \"%s\"",
|
|
|
|
new_cluster.bindir, old_cluster.controldata.chkpnt_nxtxid,
|
|
|
|
new_cluster.pgdata);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* now reset the wal archives in the new cluster */
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Resetting WAL archives");
|
2012-08-28 02:21:09 +08:00
|
|
|
exec_prog(UTILITY_LOG_FILE, NULL, true,
|
|
|
|
"\"%s/pg_resetxlog\" -l %s \"%s\"", new_cluster.bindir,
|
2012-06-26 12:35:57 +08:00
|
|
|
old_cluster.controldata.nextxlogfile,
|
2012-08-28 02:21:09 +08:00
|
|
|
new_cluster.pgdata);
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set_frozenxids()
|
|
|
|
*
|
|
|
|
* We have frozen all xids, so set relfrozenxid and datfrozenxid
|
|
|
|
* to be the old cluster's xid counter, which we just set in the new
|
|
|
|
* cluster. User-table frozenxid values will be set by pg_dumpall
|
|
|
|
* --binary-upgrade, but objects not set by the pg_dump must have
|
|
|
|
* proper frozen counters.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
set_frozenxids(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int dbnum;
|
2010-07-07 03:19:02 +08:00
|
|
|
PGconn *conn,
|
|
|
|
*conn_template1;
|
2010-05-12 10:19:11 +08:00
|
|
|
PGresult *dbres;
|
|
|
|
int ntups;
|
2010-05-20 02:27:43 +08:00
|
|
|
int i_datname;
|
|
|
|
int i_datallowconn;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Setting frozenxid counters in new cluster");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
conn_template1 = connectToServer(&new_cluster, "template1");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* set pg_database.datfrozenxid */
|
2010-10-20 05:38:16 +08:00
|
|
|
PQclear(executeQueryOrDie(conn_template1,
|
2010-05-12 10:19:11 +08:00
|
|
|
"UPDATE pg_catalog.pg_database "
|
2010-05-20 02:27:43 +08:00
|
|
|
"SET datfrozenxid = '%u'",
|
2010-10-20 05:38:16 +08:00
|
|
|
old_cluster.controldata.chkpnt_nxtxid));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* get database names */
|
2010-10-20 05:38:16 +08:00
|
|
|
dbres = executeQueryOrDie(conn_template1,
|
2010-05-20 02:27:43 +08:00
|
|
|
"SELECT datname, datallowconn "
|
|
|
|
"FROM pg_catalog.pg_database");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-05-20 02:27:43 +08:00
|
|
|
i_datname = PQfnumber(dbres, "datname");
|
|
|
|
i_datallowconn = PQfnumber(dbres, "datallowconn");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
ntups = PQntuples(dbres);
|
|
|
|
for (dbnum = 0; dbnum < ntups; dbnum++)
|
|
|
|
{
|
2010-07-07 03:19:02 +08:00
|
|
|
char *datname = PQgetvalue(dbres, dbnum, i_datname);
|
|
|
|
char *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
|
2010-05-20 02:27:43 +08:00
|
|
|
|
|
|
|
/*
|
2010-07-07 03:19:02 +08:00
|
|
|
* We must update databases where datallowconn = false, e.g.
|
|
|
|
* template0, because autovacuum increments their datfrozenxids and
|
|
|
|
* relfrozenxids even if autovacuum is turned off, and even though all
|
|
|
|
* the data rows are already frozen To enable this, we temporarily
|
|
|
|
* change datallowconn.
|
2010-05-20 02:27:43 +08:00
|
|
|
*/
|
|
|
|
if (strcmp(datallowconn, "f") == 0)
|
2010-10-20 05:38:16 +08:00
|
|
|
PQclear(executeQueryOrDie(conn_template1,
|
2010-07-07 03:19:02 +08:00
|
|
|
"UPDATE pg_catalog.pg_database "
|
|
|
|
"SET datallowconn = true "
|
|
|
|
"WHERE datname = '%s'", datname));
|
2010-05-20 02:27:43 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
conn = connectToServer(&new_cluster, datname);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* set pg_class.relfrozenxid */
|
2010-10-20 05:38:16 +08:00
|
|
|
PQclear(executeQueryOrDie(conn,
|
2010-05-12 10:19:11 +08:00
|
|
|
"UPDATE pg_catalog.pg_class "
|
|
|
|
"SET relfrozenxid = '%u' "
|
|
|
|
/* only heap and TOAST are vacuumed */
|
2010-05-20 02:27:43 +08:00
|
|
|
"WHERE relkind IN ('r', 't')",
|
2010-10-20 05:38:16 +08:00
|
|
|
old_cluster.controldata.chkpnt_nxtxid));
|
2010-05-12 10:19:11 +08:00
|
|
|
PQfinish(conn);
|
2010-05-20 02:27:43 +08:00
|
|
|
|
|
|
|
/* Reset datallowconn flag */
|
|
|
|
if (strcmp(datallowconn, "f") == 0)
|
2010-10-20 05:38:16 +08:00
|
|
|
PQclear(executeQueryOrDie(conn_template1,
|
2010-07-07 03:19:02 +08:00
|
|
|
"UPDATE pg_catalog.pg_database "
|
|
|
|
"SET datallowconn = false "
|
|
|
|
"WHERE datname = '%s'", datname));
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PQclear(dbres);
|
|
|
|
|
2010-05-20 02:27:43 +08:00
|
|
|
PQfinish(conn_template1);
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
cleanup(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
fclose(log_opts.internal);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-03-13 07:47:54 +08:00
|
|
|
/* Remove dump and log files? */
|
|
|
|
if (!log_opts.retain)
|
|
|
|
{
|
2012-12-01 05:30:13 +08:00
|
|
|
int dbnum;
|
2012-06-11 03:20:04 +08:00
|
|
|
char **filename;
|
2012-03-13 07:47:54 +08:00
|
|
|
|
2012-05-24 08:19:21 +08:00
|
|
|
for (filename = output_files; *filename != NULL; filename++)
|
|
|
|
unlink(*filename);
|
2012-03-13 07:47:54 +08:00
|
|
|
|
2012-12-01 05:30:13 +08:00
|
|
|
/* remove dump files */
|
2012-05-24 08:19:21 +08:00
|
|
|
unlink(GLOBALS_DUMP_FILE);
|
2012-12-01 05:30:13 +08:00
|
|
|
|
|
|
|
if (old_cluster.dbarr.dbs)
|
|
|
|
for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
|
|
|
|
{
|
|
|
|
char file_name[MAXPGPATH];
|
|
|
|
DbInfo *old_db = &old_cluster.dbarr.dbs[dbnum];
|
|
|
|
|
|
|
|
snprintf(file_name, sizeof(file_name), DB_DUMP_FILE_MASK, old_db->db_oid);
|
|
|
|
unlink(file_name);
|
|
|
|
}
|
2012-03-13 07:47:54 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|