mirror of
https://github.com/postgres/postgres.git
synced 2025-07-31 22:04:40 +03:00
Restructure the pg_upgrade code to use several global structures rather
than packing everything into 'ctx' and passing that to every function.
This commit is contained in:
@ -13,9 +13,9 @@
|
||||
#include "access/transam.h"
|
||||
|
||||
|
||||
static void transfer_single_new_db(migratorContext *ctx, pageCnvCtx *pageConverter,
|
||||
static void transfer_single_new_db(pageCnvCtx *pageConverter,
|
||||
FileNameMap *maps, int size);
|
||||
static void transfer_relfile(migratorContext *ctx, pageCnvCtx *pageConverter,
|
||||
static void transfer_relfile(pageCnvCtx *pageConverter,
|
||||
const char *fromfile, const char *tofile,
|
||||
const char *oldnspname, const char *oldrelname,
|
||||
const char *newnspname, const char *newrelname);
|
||||
@ -30,13 +30,13 @@ char scandir_file_pattern[MAXPGPATH];
|
||||
* physically link the databases.
|
||||
*/
|
||||
const char *
|
||||
transfer_all_new_dbs(migratorContext *ctx, DbInfoArr *olddb_arr,
|
||||
transfer_all_new_dbs(DbInfoArr *olddb_arr,
|
||||
DbInfoArr *newdb_arr, char *old_pgdata, char *new_pgdata)
|
||||
{
|
||||
int dbnum;
|
||||
const char *msg = NULL;
|
||||
|
||||
prep_status(ctx, "Restoring user relation files\n");
|
||||
prep_status("Restoring user relation files\n");
|
||||
|
||||
for (dbnum = 0; dbnum < newdb_arr->ndbs; dbnum++)
|
||||
{
|
||||
@ -47,24 +47,24 @@ transfer_all_new_dbs(migratorContext *ctx, DbInfoArr *olddb_arr,
|
||||
pageCnvCtx *pageConverter = NULL;
|
||||
|
||||
n_maps = 0;
|
||||
mappings = gen_db_file_maps(ctx, old_db, new_db, &n_maps, old_pgdata,
|
||||
mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
|
||||
new_pgdata);
|
||||
|
||||
if (n_maps)
|
||||
{
|
||||
print_maps(ctx, mappings, n_maps, new_db->db_name);
|
||||
print_maps(mappings, n_maps, new_db->db_name);
|
||||
|
||||
#ifdef PAGE_CONVERSION
|
||||
msg = setupPageConverter(ctx, &pageConverter);
|
||||
msg = setupPageConverter(&pageConverter);
|
||||
#endif
|
||||
transfer_single_new_db(ctx, pageConverter, mappings, n_maps);
|
||||
transfer_single_new_db(pageConverter, mappings, n_maps);
|
||||
|
||||
pg_free(mappings);
|
||||
}
|
||||
}
|
||||
|
||||
prep_status(ctx, ""); /* in case nothing printed */
|
||||
check_ok(ctx);
|
||||
prep_status(""); /* in case nothing printed */
|
||||
check_ok();
|
||||
|
||||
return msg;
|
||||
}
|
||||
@ -77,13 +77,14 @@ transfer_all_new_dbs(migratorContext *ctx, DbInfoArr *olddb_arr,
|
||||
* relfilenodes later in the upgrade process.
|
||||
*/
|
||||
void
|
||||
get_pg_database_relfilenode(migratorContext *ctx, Cluster whichCluster)
|
||||
get_pg_database_relfilenode(Cluster whichCluster)
|
||||
{
|
||||
PGconn *conn = connectToServer(ctx, "template1", whichCluster);
|
||||
PGconn *conn = connectToServer("template1", whichCluster);
|
||||
ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster);
|
||||
PGresult *res;
|
||||
int i_relfile;
|
||||
|
||||
res = executeQueryOrDie(ctx, conn,
|
||||
res = executeQueryOrDie(conn,
|
||||
"SELECT c.relname, c.relfilenode "
|
||||
"FROM pg_catalog.pg_class c, "
|
||||
" pg_catalog.pg_namespace n "
|
||||
@ -93,10 +94,7 @@ get_pg_database_relfilenode(migratorContext *ctx, Cluster whichCluster)
|
||||
"ORDER BY c.relname");
|
||||
|
||||
i_relfile = PQfnumber(res, "relfilenode");
|
||||
if (whichCluster == CLUSTER_OLD)
|
||||
ctx->old.pg_database_oid = atooid(PQgetvalue(res, 0, i_relfile));
|
||||
else
|
||||
ctx->new.pg_database_oid = atooid(PQgetvalue(res, 0, i_relfile));
|
||||
active_cluster->pg_database_oid = atooid(PQgetvalue(res, 0, i_relfile));
|
||||
|
||||
PQclear(res);
|
||||
PQfinish(conn);
|
||||
@ -109,7 +107,7 @@ get_pg_database_relfilenode(migratorContext *ctx, Cluster whichCluster)
|
||||
* create links for mappings stored in "maps" array.
|
||||
*/
|
||||
static void
|
||||
transfer_single_new_db(migratorContext *ctx, pageCnvCtx *pageConverter,
|
||||
transfer_single_new_db(pageCnvCtx *pageConverter,
|
||||
FileNameMap *maps, int size)
|
||||
{
|
||||
int mapnum;
|
||||
@ -123,36 +121,39 @@ transfer_single_new_db(migratorContext *ctx, pageCnvCtx *pageConverter,
|
||||
|
||||
/* Copying files might take some time, so give feedback. */
|
||||
|
||||
snprintf(old_file, sizeof(old_file), "%s/%u", maps[mapnum].old_file, maps[mapnum].old);
|
||||
snprintf(new_file, sizeof(new_file), "%s/%u", maps[mapnum].new_file, maps[mapnum].new);
|
||||
pg_log(ctx, PG_REPORT, OVERWRITE_MESSAGE, old_file);
|
||||
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);
|
||||
|
||||
/*
|
||||
* Copy/link the relation file to the new cluster
|
||||
*/
|
||||
unlink(new_file);
|
||||
transfer_relfile(ctx, pageConverter, old_file, new_file,
|
||||
transfer_relfile(pageConverter, old_file, new_file,
|
||||
maps[mapnum].old_nspname, maps[mapnum].old_relname,
|
||||
maps[mapnum].new_nspname, maps[mapnum].new_relname);
|
||||
|
||||
/* fsm/vm files added in PG 8.4 */
|
||||
if (GET_MAJOR_VERSION(ctx->old.major_version) >= 804)
|
||||
if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
|
||||
{
|
||||
/*
|
||||
* Now copy/link any fsm and vm files, if they exist
|
||||
*/
|
||||
snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u_", maps[mapnum].old);
|
||||
numFiles = pg_scandir(ctx, maps[mapnum].old_file, &namelist, dir_matching_filenames);
|
||||
snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u_",
|
||||
maps[mapnum].old_relfilenode);
|
||||
numFiles = pg_scandir(maps[mapnum].old_file, &namelist, dir_matching_filenames);
|
||||
|
||||
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,
|
||||
maps[mapnum].new, strchr(namelist[numFiles]->d_name, '_'));
|
||||
maps[mapnum].new_relfilenode, strchr(namelist[numFiles]->d_name, '_'));
|
||||
|
||||
unlink(new_file);
|
||||
transfer_relfile(ctx, pageConverter, old_file, new_file,
|
||||
transfer_relfile(pageConverter, old_file, new_file,
|
||||
maps[mapnum].old_nspname, maps[mapnum].old_relname,
|
||||
maps[mapnum].new_nspname, maps[mapnum].new_relname);
|
||||
|
||||
@ -169,18 +170,19 @@ transfer_single_new_db(migratorContext *ctx, pageCnvCtx *pageConverter,
|
||||
* relfilenode.3, ... 'fsm' and 'vm' files use underscores so are not
|
||||
* copied.
|
||||
*/
|
||||
snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u.", maps[mapnum].old);
|
||||
numFiles = pg_scandir(ctx, maps[mapnum].old_file, &namelist, dir_matching_filenames);
|
||||
snprintf(scandir_file_pattern, sizeof(scandir_file_pattern), "%u.",
|
||||
maps[mapnum].old_relfilenode);
|
||||
numFiles = pg_scandir(maps[mapnum].old_file, &namelist, dir_matching_filenames);
|
||||
|
||||
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,
|
||||
maps[mapnum].new, strchr(namelist[numFiles]->d_name, '.'));
|
||||
maps[mapnum].new_relfilenode, strchr(namelist[numFiles]->d_name, '.'));
|
||||
|
||||
unlink(new_file);
|
||||
transfer_relfile(ctx, pageConverter, old_file, new_file,
|
||||
transfer_relfile(pageConverter, old_file, new_file,
|
||||
maps[mapnum].old_nspname, maps[mapnum].old_relname,
|
||||
maps[mapnum].new_nspname, maps[mapnum].new_relname);
|
||||
|
||||
@ -198,30 +200,30 @@ transfer_single_new_db(migratorContext *ctx, pageCnvCtx *pageConverter,
|
||||
* Copy or link file from old cluster to new one.
|
||||
*/
|
||||
static void
|
||||
transfer_relfile(migratorContext *ctx, pageCnvCtx *pageConverter, const char *oldfile,
|
||||
transfer_relfile(pageCnvCtx *pageConverter, const char *oldfile,
|
||||
const char *newfile, const char *oldnspname, const char *oldrelname,
|
||||
const char *newnspname, const char *newrelname)
|
||||
{
|
||||
const char *msg;
|
||||
|
||||
if ((ctx->transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
|
||||
pg_log(ctx, PG_FATAL, "this migration requires page-by-page conversion, "
|
||||
if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
|
||||
pg_log(PG_FATAL, "this migration requires page-by-page conversion, "
|
||||
"you must use copy-mode instead of link-mode\n");
|
||||
|
||||
if (ctx->transfer_mode == TRANSFER_MODE_COPY)
|
||||
if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
|
||||
{
|
||||
pg_log(ctx, PG_INFO, "copying %s to %s\n", oldfile, newfile);
|
||||
pg_log(PG_INFO, "copying %s to %s\n", oldfile, newfile);
|
||||
|
||||
if ((msg = copyAndUpdateFile(ctx, pageConverter, oldfile, newfile, true)) != NULL)
|
||||
pg_log(ctx, PG_FATAL, "error while copying %s.%s(%s) to %s.%s(%s): %s\n",
|
||||
if ((msg = copyAndUpdateFile(pageConverter, oldfile, newfile, true)) != NULL)
|
||||
pg_log(PG_FATAL, "error while copying %s.%s(%s) to %s.%s(%s): %s\n",
|
||||
oldnspname, oldrelname, oldfile, newnspname, newrelname, newfile, msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
pg_log(ctx, PG_INFO, "linking %s to %s\n", newfile, oldfile);
|
||||
pg_log(PG_INFO, "linking %s to %s\n", newfile, oldfile);
|
||||
|
||||
if ((msg = linkAndUpdateFile(ctx, pageConverter, oldfile, newfile)) != NULL)
|
||||
pg_log(ctx, PG_FATAL,
|
||||
if ((msg = linkAndUpdateFile(pageConverter, oldfile, newfile)) != NULL)
|
||||
pg_log(PG_FATAL,
|
||||
"error while creating link from %s.%s(%s) to %s.%s(%s): %s\n",
|
||||
oldnspname, oldrelname, oldfile, newnspname, newrelname,
|
||||
newfile, msg);
|
||||
|
Reference in New Issue
Block a user