2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* relfilenode.c
|
|
|
|
*
|
|
|
|
* relfilenode functions
|
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/relfilenode.c
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
|
|
|
|
2011-08-27 09:16:24 +08:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include "catalog/pg_class.h"
|
|
|
|
#include "access/transam.h"
|
|
|
|
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
static void transfer_single_new_db(pageCnvCtx *pageConverter,
|
2010-05-12 10:19:11 +08:00
|
|
|
FileNameMap *maps, int size);
|
2012-11-15 06:32:04 +08:00
|
|
|
static void transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
|
|
|
|
const char *suffix);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* transfer_all_new_dbs()
|
|
|
|
*
|
|
|
|
* Responsible for upgrading all database. invokes routines to generate mappings and then
|
|
|
|
* physically link the databases.
|
|
|
|
*/
|
|
|
|
const char *
|
2011-01-09 02:44:44 +08:00
|
|
|
transfer_all_new_dbs(DbInfoArr *old_db_arr,
|
2011-04-10 23:42:00 +08:00
|
|
|
DbInfoArr *new_db_arr, char *old_pgdata, char *new_pgdata)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
int old_dbnum,
|
|
|
|
new_dbnum;
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *msg = NULL;
|
|
|
|
|
2012-08-04 00:43:37 +08:00
|
|
|
pg_log(PG_REPORT, "%s user relation files\n",
|
2012-06-11 03:20:04 +08:00
|
|
|
user_opts.transfer_mode == TRANSFER_MODE_LINK ? "Linking" : "Copying");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-11-02 01:49:03 +08:00
|
|
|
/* Scan the old cluster databases and transfer their files */
|
|
|
|
for (old_dbnum = new_dbnum = 0;
|
2011-11-04 01:56:56 +08:00
|
|
|
old_dbnum < old_db_arr->ndbs;
|
2011-11-02 01:49:03 +08:00
|
|
|
old_dbnum++, new_dbnum++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
DbInfo *old_db = &old_db_arr->dbs[old_dbnum],
|
|
|
|
*new_db = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
FileNameMap *mappings;
|
|
|
|
int n_maps;
|
|
|
|
pageCnvCtx *pageConverter = NULL;
|
|
|
|
|
2011-11-02 01:49:03 +08:00
|
|
|
/*
|
2012-06-11 03:20:04 +08:00
|
|
|
* Advance past any databases that exist in the new cluster but not in
|
|
|
|
* the old, e.g. "postgres". (The user might have removed the
|
|
|
|
* 'postgres' database from the old cluster.)
|
2011-11-02 01:49:03 +08:00
|
|
|
*/
|
2011-11-04 01:56:56 +08:00
|
|
|
for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
|
|
|
|
{
|
|
|
|
new_db = &new_db_arr->dbs[new_dbnum];
|
|
|
|
if (strcmp(old_db->db_name, new_db->db_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2011-11-02 01:49:03 +08:00
|
|
|
|
2011-11-04 01:56:56 +08:00
|
|
|
if (new_dbnum >= new_db_arr->ndbs)
|
|
|
|
pg_log(PG_FATAL, "old database \"%s\" not found in the new cluster\n",
|
|
|
|
old_db->db_name);
|
2011-04-10 23:42:00 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
n_maps = 0;
|
2010-10-20 05:38:16 +08:00
|
|
|
mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
|
2010-05-12 10:19:11 +08:00
|
|
|
new_pgdata);
|
|
|
|
|
|
|
|
if (n_maps)
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
print_maps(mappings, n_maps, new_db->db_name);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
#ifdef PAGE_CONVERSION
|
2010-10-20 05:38:16 +08:00
|
|
|
msg = setupPageConverter(&pageConverter);
|
2010-05-12 10:19:11 +08:00
|
|
|
#endif
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_single_new_db(pageConverter, mappings, n_maps);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
pg_free(mappings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-11 03:20:04 +08:00
|
|
|
prep_status(" "); /* in case nothing printed; pass a space so
|
|
|
|
* gcc doesn't complain about empty format
|
2011-09-11 04:12:46 +08:00
|
|
|
* string */
|
2010-10-20 05:38:16 +08:00
|
|
|
check_ok();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_pg_database_relfilenode()
|
|
|
|
*
|
|
|
|
* Retrieves the relfilenode for a few system-catalog tables. We need these
|
|
|
|
* relfilenodes later in the upgrade process.
|
|
|
|
*/
|
|
|
|
void
|
2011-01-02 01:06:36 +08:00
|
|
|
get_pg_database_relfilenode(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:06:36 +08:00
|
|
|
PGconn *conn = connectToServer(cluster, "template1");
|
2010-05-12 10:19:11 +08:00
|
|
|
PGresult *res;
|
|
|
|
int i_relfile;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
res = executeQueryOrDie(conn,
|
2010-05-12 10:19:11 +08:00
|
|
|
"SELECT c.relname, c.relfilenode "
|
2011-07-12 12:13:51 +08:00
|
|
|
"FROM pg_catalog.pg_class c, "
|
2010-05-12 10:19:11 +08:00
|
|
|
" pg_catalog.pg_namespace n "
|
2011-07-12 12:13:51 +08:00
|
|
|
"WHERE c.relnamespace = n.oid AND "
|
2010-05-12 10:19:11 +08:00
|
|
|
" n.nspname = 'pg_catalog' AND "
|
|
|
|
" c.relname = 'pg_database' "
|
|
|
|
"ORDER BY c.relname");
|
|
|
|
|
|
|
|
i_relfile = PQfnumber(res, "relfilenode");
|
2011-01-02 01:06:36 +08:00
|
|
|
cluster->pg_database_oid = atooid(PQgetvalue(res, 0, i_relfile));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* transfer_single_new_db()
|
|
|
|
*
|
|
|
|
* create links for mappings stored in "maps" array.
|
|
|
|
*/
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_single_new_db(pageCnvCtx *pageConverter,
|
2010-05-12 10:19:11 +08:00
|
|
|
FileNameMap *maps, int size)
|
|
|
|
{
|
|
|
|
int mapnum;
|
2012-11-15 06:32:04 +08:00
|
|
|
bool vm_crashsafe_match = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the old and new cluster disagree on the crash-safetiness of the vm
|
|
|
|
* files? If so, do not copy them.
|
|
|
|
*/
|
2011-08-19 23:20:30 +08:00
|
|
|
if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_CRASHSAFE_CAT_VER &&
|
|
|
|
new_cluster.controldata.cat_ver >= VISIBILITY_MAP_CRASHSAFE_CAT_VER)
|
2012-11-15 06:32:04 +08:00
|
|
|
vm_crashsafe_match = false;
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
for (mapnum = 0; mapnum < size; mapnum++)
|
|
|
|
{
|
2012-11-15 06:32:04 +08:00
|
|
|
/* transfer primary file */
|
|
|
|
transfer_relfile(pageConverter, &maps[mapnum], "");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* fsm/vm files added in PG 8.4 */
|
2010-10-20 05:38:16 +08:00
|
|
|
if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
/*
|
2010-10-20 08:33:19 +08:00
|
|
|
* Copy/link any fsm and vm files, if they exist
|
2010-05-12 10:19:11 +08:00
|
|
|
*/
|
2012-11-15 06:32:04 +08:00
|
|
|
transfer_relfile(pageConverter, &maps[mapnum], "_fsm");
|
|
|
|
if (vm_crashsafe_match)
|
|
|
|
transfer_relfile(pageConverter, &maps[mapnum], "_vm");
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
2010-10-20 08:33:19 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* transfer_relfile()
|
|
|
|
*
|
|
|
|
* Copy or link file from old cluster to new one.
|
|
|
|
*/
|
|
|
|
static void
|
2012-11-15 06:32:04 +08:00
|
|
|
transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
|
|
|
|
const char *type_suffix)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
const char *msg;
|
2012-11-15 06:32:04 +08:00
|
|
|
char old_file[MAXPGPATH];
|
|
|
|
char new_file[MAXPGPATH];
|
|
|
|
int fd;
|
|
|
|
int segno;
|
|
|
|
char extent_suffix[65];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now copy/link any related segments as well. Remember, PG breaks
|
|
|
|
* large files into 1GB segments, the first segment has no extension,
|
|
|
|
* subsequent segments are named relfilenode.1, relfilenode.2,
|
|
|
|
* relfilenode.3.
|
|
|
|
* copied.
|
|
|
|
*/
|
|
|
|
for (segno = 0;; segno++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2012-11-15 06:32:04 +08:00
|
|
|
if (segno == 0)
|
|
|
|
extent_suffix[0] = '\0';
|
|
|
|
else
|
|
|
|
snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
|
|
|
|
|
|
|
|
snprintf(old_file, sizeof(old_file), "%s/%u%s%s", map->old_dir,
|
|
|
|
map->old_relfilenode, type_suffix, extent_suffix);
|
|
|
|
snprintf(new_file, sizeof(new_file), "%s/%u%s%s", map->new_dir,
|
|
|
|
map->new_relfilenode, type_suffix, extent_suffix);
|
|
|
|
|
|
|
|
/* Is it an extent, fsm, or vm file? */
|
|
|
|
if (type_suffix[0] != '\0' || segno != 0)
|
|
|
|
{
|
|
|
|
/* Did file open fail? */
|
2012-11-15 08:01:29 +08:00
|
|
|
if ((fd = open(old_file, O_RDONLY, 0)) == -1)
|
2012-11-15 06:32:04 +08:00
|
|
|
{
|
|
|
|
/* File does not exist? That's OK, just return */
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return;
|
|
|
|
else
|
2012-11-20 05:41:58 +08:00
|
|
|
pg_log(PG_FATAL, "error while checking for file existance \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
|
|
|
map->nspname, map->relname, old_file, new_file,
|
|
|
|
getErrorText(errno));
|
2012-11-15 06:32:04 +08:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
unlink(new_file);
|
|
|
|
|
|
|
|
/* Copying files might take some time, so give feedback. */
|
|
|
|
pg_log(PG_REPORT, OVERWRITE_MESSAGE, old_file);
|
|
|
|
|
|
|
|
if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
|
|
|
|
pg_log(PG_FATAL, "This upgrade requires page-by-page conversion, "
|
|
|
|
"you must use copy mode instead of link mode.\n");
|
|
|
|
|
|
|
|
if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
|
|
|
|
{
|
|
|
|
pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"\n", old_file, new_file);
|
|
|
|
|
|
|
|
if ((msg = copyAndUpdateFile(pageConverter, old_file, new_file, true)) != NULL)
|
|
|
|
pg_log(PG_FATAL, "error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
|
|
|
map->nspname, map->relname, old_file, new_file, msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n", old_file, new_file);
|
|
|
|
|
|
|
|
if ((msg = linkAndUpdateFile(pageConverter, old_file, new_file)) != NULL)
|
|
|
|
pg_log(PG_FATAL,
|
|
|
|
"error while creating link for relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
|
|
|
map->nspname, map->relname, old_file, new_file, msg);
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|