2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* relfilenode.c
|
|
|
|
*
|
|
|
|
* relfilenode functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, 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);
|
2010-10-20 05:38:16 +08:00
|
|
|
static void transfer_relfile(pageCnvCtx *pageConverter,
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *fromfile, const char *tofile,
|
2011-01-08 11:36:51 +08:00
|
|
|
const char *nspname, const char *relname);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-06-15 10:08:01 +08:00
|
|
|
/* used by scandir(), must be global */
|
2010-07-07 03:19:02 +08:00
|
|
|
char scandir_file_pattern[MAXPGPATH];
|
|
|
|
|
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
|
|
|
{
|
2011-11-02 01:49:03 +08:00
|
|
|
int old_dbnum, new_dbnum;
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *msg = NULL;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status("Restoring user relation files\n");
|
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
|
|
|
{
|
2011-11-04 22:40:52 +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
|
|
|
/*
|
|
|
|
* Advance past any databases that exist in the new cluster
|
2011-11-02 02:33:51 +08:00
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-11 04:12:46 +08:00
|
|
|
prep_status(" "); /* in case nothing printed; pass a space so gcc
|
|
|
|
* doesn't complain about empty format
|
|
|
|
* 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)
|
|
|
|
{
|
2010-10-20 08:33:19 +08:00
|
|
|
char old_dir[MAXPGPATH];
|
|
|
|
struct dirent **namelist = NULL;
|
|
|
|
int numFiles = 0;
|
2010-05-12 10:19:11 +08:00
|
|
|
int mapnum;
|
2010-10-20 08:33:19 +08:00
|
|
|
int fileno;
|
2011-08-19 23:20:30 +08:00
|
|
|
bool vm_crashsafe_change = false;
|
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
old_dir[0] = '\0';
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-08-19 23:20:30 +08:00
|
|
|
/* Do not copy non-crashsafe vm files for binaries that assume crashsafety */
|
|
|
|
if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_CRASHSAFE_CAT_VER &&
|
|
|
|
new_cluster.controldata.cat_ver >= VISIBILITY_MAP_CRASHSAFE_CAT_VER)
|
|
|
|
vm_crashsafe_change = true;
|
|
|
|
|
2010-05-12 10:19:11 +08:00
|
|
|
for (mapnum = 0; mapnum < size; mapnum++)
|
|
|
|
{
|
|
|
|
char old_file[MAXPGPATH];
|
|
|
|
char new_file[MAXPGPATH];
|
2010-10-20 08:33:19 +08:00
|
|
|
|
|
|
|
/* Changed tablespaces? Need a new directory scan? */
|
|
|
|
if (strcmp(maps[mapnum].old_dir, old_dir) != 0)
|
|
|
|
{
|
|
|
|
if (numFiles > 0)
|
|
|
|
{
|
|
|
|
for (fileno = 0; fileno < numFiles; fileno++)
|
|
|
|
pg_free(namelist[fileno]);
|
|
|
|
pg_free(namelist);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(old_dir, sizeof(old_dir), "%s", maps[mapnum].old_dir);
|
|
|
|
numFiles = pg_scandir(old_dir, &namelist, NULL);
|
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* Copying files might take some time, so give feedback. */
|
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
snprintf(old_file, sizeof(old_file), "%s/%u", maps[mapnum].old_dir,
|
2010-10-20 06:37:04 +08:00
|
|
|
maps[mapnum].old_relfilenode);
|
2010-10-20 08:33:19 +08:00
|
|
|
snprintf(new_file, sizeof(new_file), "%s/%u", maps[mapnum].new_dir,
|
2010-10-20 06:37:04 +08:00
|
|
|
maps[mapnum].new_relfilenode);
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_REPORT, OVERWRITE_MESSAGE, old_file);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy/link the relation file to the new cluster
|
|
|
|
*/
|
|
|
|
unlink(new_file);
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_relfile(pageConverter, old_file, new_file,
|
2011-01-08 11:36:51 +08:00
|
|
|
maps[mapnum].nspname, maps[mapnum].relname);
|
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
|
|
|
*/
|
2010-10-20 05:38:16 +08:00
|
|
|
snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u_",
|
2010-10-20 06:37:04 +08:00
|
|
|
maps[mapnum].old_relfilenode);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
for (fileno = 0; fileno < numFiles; fileno++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-08-19 23:20:30 +08:00
|
|
|
char *vm_offset = strstr(namelist[fileno]->d_name, "_vm");
|
|
|
|
bool is_vm_file = false;
|
|
|
|
|
|
|
|
/* Is a visibility map file? (name ends with _vm) */
|
|
|
|
if (vm_offset && strlen(vm_offset) == strlen("_vm"))
|
|
|
|
is_vm_file = true;
|
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
if (strncmp(namelist[fileno]->d_name, scandir_file_pattern,
|
2011-08-19 23:20:30 +08:00
|
|
|
strlen(scandir_file_pattern)) == 0 &&
|
|
|
|
(!is_vm_file || !vm_crashsafe_change))
|
2010-10-20 08:33:19 +08:00
|
|
|
{
|
|
|
|
snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_dir,
|
|
|
|
namelist[fileno]->d_name);
|
|
|
|
snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_dir,
|
|
|
|
maps[mapnum].new_relfilenode, strchr(namelist[fileno]->d_name, '_'));
|
2010-11-24 04:27:50 +08:00
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
unlink(new_file);
|
|
|
|
transfer_relfile(pageConverter, old_file, new_file,
|
2011-04-10 23:42:00 +08:00
|
|
|
maps[mapnum].nspname, maps[mapnum].relname);
|
2010-10-20 08:33:19 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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, ... 'fsm' and 'vm' files use underscores so are not
|
|
|
|
* copied.
|
|
|
|
*/
|
2010-10-20 05:38:16 +08:00
|
|
|
snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u.",
|
2010-10-20 06:37:04 +08:00
|
|
|
maps[mapnum].old_relfilenode);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
for (fileno = 0; fileno < numFiles; fileno++)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 08:33:19 +08:00
|
|
|
if (strncmp(namelist[fileno]->d_name, scandir_file_pattern,
|
2011-04-10 23:42:00 +08:00
|
|
|
strlen(scandir_file_pattern)) == 0)
|
2010-10-20 08:33:19 +08:00
|
|
|
{
|
|
|
|
snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_dir,
|
|
|
|
namelist[fileno]->d_name);
|
|
|
|
snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_dir,
|
|
|
|
maps[mapnum].new_relfilenode, strchr(namelist[fileno]->d_name, '.'));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
unlink(new_file);
|
|
|
|
transfer_relfile(pageConverter, old_file, new_file,
|
2011-04-10 23:42:00 +08:00
|
|
|
maps[mapnum].nspname, maps[mapnum].relname);
|
2010-10-20 08:33:19 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
2010-10-20 08:33:19 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 08:33:19 +08:00
|
|
|
|
|
|
|
if (numFiles > 0)
|
|
|
|
{
|
|
|
|
for (fileno = 0; fileno < numFiles; fileno++)
|
|
|
|
pg_free(namelist[fileno]);
|
2010-05-12 10:19:11 +08:00
|
|
|
pg_free(namelist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* transfer_relfile()
|
|
|
|
*
|
|
|
|
* Copy or link file from old cluster to new one.
|
|
|
|
*/
|
|
|
|
static void
|
2011-01-06 01:07:45 +08:00
|
|
|
transfer_relfile(pageCnvCtx *pageConverter, const char *old_file,
|
2011-04-10 23:42:00 +08:00
|
|
|
const char *new_file, const char *nspname, const char *relname)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
const char *msg;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "This upgrade requires page-by-page conversion, "
|
|
|
|
"you must use copy mode instead of link mode.\n");
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_INFO, "copying \"%s\" to \"%s\"\n", old_file, new_file);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-06 01:07:45 +08:00
|
|
|
if ((msg = copyAndUpdateFile(pageConverter, old_file, new_file, true)) != NULL)
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_FATAL, "error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2011-01-08 11:36:51 +08:00
|
|
|
nspname, relname, old_file, new_file, msg);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-12 12:13:51 +08:00
|
|
|
pg_log(PG_INFO, "linking \"%s\" to \"%s\"\n", old_file, new_file);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-06 01:07:45 +08:00
|
|
|
if ((msg = linkAndUpdateFile(pageConverter, old_file, new_file)) != NULL)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL,
|
2011-07-12 12:13:51 +08:00
|
|
|
"error while creating link for relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2011-01-08 11:36:51 +08:00
|
|
|
nspname, relname, old_file, new_file, msg);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|