2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* relfilenode.c
|
|
|
|
*
|
|
|
|
* relfilenode functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2010-07-04 00:33:15 +08:00
|
|
|
* Copyright (c) 2010, 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
|
|
|
*/
|
|
|
|
|
|
|
|
#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,
|
|
|
|
const char *oldnspname, const char *oldrelname,
|
|
|
|
const char *newnspname, const char *newrelname);
|
|
|
|
|
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 *
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_all_new_dbs(DbInfoArr *olddb_arr,
|
2010-05-12 10:19:11 +08:00
|
|
|
DbInfoArr *newdb_arr, char *old_pgdata, char *new_pgdata)
|
|
|
|
{
|
|
|
|
int dbnum;
|
|
|
|
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
|
|
|
|
|
|
|
for (dbnum = 0; dbnum < newdb_arr->ndbs; dbnum++)
|
|
|
|
{
|
|
|
|
DbInfo *new_db = &newdb_arr->dbs[dbnum];
|
|
|
|
DbInfo *old_db = dbarr_lookup_db(olddb_arr, new_db->db_name);
|
|
|
|
FileNameMap *mappings;
|
|
|
|
int n_maps;
|
|
|
|
pageCnvCtx *pageConverter = NULL;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
prep_status(""); /* in case nothing printed */
|
|
|
|
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
|
2010-10-20 05:38:16 +08:00
|
|
|
get_pg_database_relfilenode(Cluster whichCluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
PGconn *conn = connectToServer("template1", whichCluster);
|
|
|
|
ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster);
|
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 "
|
|
|
|
"FROM pg_catalog.pg_class c, "
|
|
|
|
" pg_catalog.pg_namespace n "
|
|
|
|
"WHERE c.relnamespace = n.oid AND "
|
|
|
|
" n.nspname = 'pg_catalog' AND "
|
|
|
|
" c.relname = 'pg_database' "
|
|
|
|
"ORDER BY c.relname");
|
|
|
|
|
|
|
|
i_relfile = PQfnumber(res, "relfilenode");
|
2010-10-20 05:38:16 +08:00
|
|
|
active_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;
|
|
|
|
|
|
|
|
for (mapnum = 0; mapnum < size; mapnum++)
|
|
|
|
{
|
|
|
|
char old_file[MAXPGPATH];
|
|
|
|
char new_file[MAXPGPATH];
|
|
|
|
struct dirent **namelist = NULL;
|
|
|
|
int numFiles;
|
|
|
|
|
|
|
|
/* Copying files might take some time, so give feedback. */
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
snprintf(old_file, sizeof(old_file), "%s/%u", maps[mapnum].old_file,
|
|
|
|
maps[mapnum].old_relfilenode);
|
|
|
|
snprintf(new_file, sizeof(new_file), "%s/%u", maps[mapnum].new_file,
|
|
|
|
maps[mapnum].new_relfilenode);
|
|
|
|
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,
|
2010-05-12 10:19:11 +08:00
|
|
|
maps[mapnum].old_nspname, maps[mapnum].old_relname,
|
|
|
|
maps[mapnum].new_nspname, maps[mapnum].new_relname);
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Now copy/link any fsm and vm files, if they exist
|
|
|
|
*/
|
2010-10-20 05:38:16 +08:00
|
|
|
snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u_",
|
|
|
|
maps[mapnum].old_relfilenode);
|
|
|
|
numFiles = pg_scandir(maps[mapnum].old_file, &namelist, dir_matching_filenames);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
while (numFiles--)
|
|
|
|
{
|
|
|
|
snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_file,
|
|
|
|
namelist[numFiles]->d_name);
|
|
|
|
snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_file,
|
2010-10-20 05:38:16 +08:00
|
|
|
maps[mapnum].new_relfilenode, strchr(namelist[numFiles]->d_name, '_'));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
unlink(new_file);
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_relfile(pageConverter, old_file, new_file,
|
2010-05-12 10:19:11 +08:00
|
|
|
maps[mapnum].old_nspname, maps[mapnum].old_relname,
|
|
|
|
maps[mapnum].new_nspname, maps[mapnum].new_relname);
|
|
|
|
|
|
|
|
pg_free(namelist[numFiles]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_free(namelist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.",
|
|
|
|
maps[mapnum].old_relfilenode);
|
|
|
|
numFiles = pg_scandir(maps[mapnum].old_file, &namelist, dir_matching_filenames);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
while (numFiles--)
|
|
|
|
{
|
|
|
|
snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_file,
|
|
|
|
namelist[numFiles]->d_name);
|
|
|
|
snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_file,
|
2010-10-20 05:38:16 +08:00
|
|
|
maps[mapnum].new_relfilenode, strchr(namelist[numFiles]->d_name, '.'));
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
unlink(new_file);
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_relfile(pageConverter, old_file, new_file,
|
2010-05-12 10:19:11 +08:00
|
|
|
maps[mapnum].old_nspname, maps[mapnum].old_relname,
|
|
|
|
maps[mapnum].new_nspname, maps[mapnum].new_relname);
|
|
|
|
|
|
|
|
pg_free(namelist[numFiles]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_free(namelist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* transfer_relfile()
|
|
|
|
*
|
|
|
|
* Copy or link file from old cluster to new one.
|
|
|
|
*/
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
transfer_relfile(pageCnvCtx *pageConverter, const char *oldfile,
|
2010-05-12 10:19:11 +08:00
|
|
|
const char *newfile, const char *oldnspname, const char *oldrelname,
|
|
|
|
const char *newnspname, const char *newrelname)
|
|
|
|
{
|
|
|
|
const char *msg;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
|
|
|
|
pg_log(PG_FATAL, "this migration requires page-by-page conversion, "
|
2010-05-12 10:19:11 +08:00
|
|
|
"you must use copy-mode instead of link-mode\n");
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_INFO, "copying %s to %s\n", oldfile, newfile);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((msg = copyAndUpdateFile(pageConverter, oldfile, newfile, true)) != NULL)
|
|
|
|
pg_log(PG_FATAL, "error while copying %s.%s(%s) to %s.%s(%s): %s\n",
|
2010-05-12 10:19:11 +08:00
|
|
|
oldnspname, oldrelname, oldfile, newnspname, newrelname, newfile, msg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_INFO, "linking %s to %s\n", newfile, oldfile);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((msg = linkAndUpdateFile(pageConverter, oldfile, newfile)) != NULL)
|
|
|
|
pg_log(PG_FATAL,
|
2010-05-12 10:19:11 +08:00
|
|
|
"error while creating link from %s.%s(%s) to %s.%s(%s): %s\n",
|
|
|
|
oldnspname, oldrelname, oldfile, newnspname, newrelname,
|
|
|
|
newfile, msg);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|