2010-05-12 04:19:11 +02:00
|
|
|
/*
|
|
|
|
* relfilenode.c
|
|
|
|
*
|
|
|
|
* relfilenode functions
|
2010-07-03 16:23:14 +02:00
|
|
|
*
|
2015-01-06 17:43:47 +01:00
|
|
|
* Copyright (c) 2010-2015, PostgreSQL Global Development Group
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/pg_upgrade/relfilenode.c
|
2010-05-12 04:19:11 +02:00
|
|
|
*/
|
|
|
|
|
Create libpgcommon, and move pg_malloc et al to it
libpgcommon is a new static library to allow sharing code among the
various frontend programs and backend; this lets us eliminate duplicate
implementations of common routines. We avoid libpgport, because that's
intended as a place for porting issues; per discussion, it seems better
to keep them separate.
The first use case, and the only implemented by this patch, is pg_malloc
and friends, which many frontend programs were already using.
At the same time, we can use this to provide palloc emulation functions
for the frontend; this way, some palloc-using files in the backend can
also be used by the frontend cleanly. To do this, we change palloc() in
the backend to be a function instead of a macro on top of
MemoryContextAlloc(). This was previously believed to cause loss of
performance, but this implementation has been tweaked by Tom and Andres
so that on modern compilers it provides a slight improvement over the
previous one.
This lets us clean up some places that were already with
localized hacks.
Most of the pg_malloc/palloc changes in this patch were authored by
Andres Freund. Zoltán Böszörményi also independently provided a form of
that. libpgcommon infrastructure was authored by Álvaro.
2013-02-12 14:33:40 +01:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 03:16:24 +02:00
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
#include "pg_upgrade.h"
|
|
|
|
|
|
|
|
#include "catalog/pg_class.h"
|
|
|
|
#include "access/transam.h"
|
|
|
|
|
|
|
|
|
2010-10-19 23:38:16 +02:00
|
|
|
static void transfer_single_new_db(pageCnvCtx *pageConverter,
|
2013-01-09 14:57:47 +01:00
|
|
|
FileNameMap *maps, int size, char *old_tablespace);
|
2012-11-14 23:32:04 +01:00
|
|
|
static void transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
|
2013-05-29 22:58:43 +02:00
|
|
|
const char *suffix);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
|
2013-01-09 14:57:47 +01:00
|
|
|
/*
|
|
|
|
* transfer_all_new_tablespaces()
|
|
|
|
*
|
|
|
|
* Responsible for upgrading all database. invokes routines to generate mappings and then
|
|
|
|
* physically link the databases.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
|
2013-05-29 22:58:43 +02:00
|
|
|
char *old_pgdata, char *new_pgdata)
|
2013-01-09 14:57:47 +01:00
|
|
|
{
|
|
|
|
pg_log(PG_REPORT, "%s user relation files\n",
|
|
|
|
user_opts.transfer_mode == TRANSFER_MODE_LINK ? "Linking" : "Copying");
|
|
|
|
|
|
|
|
/*
|
2013-05-29 22:58:43 +02:00
|
|
|
* Transfering files by tablespace is tricky because a single database can
|
|
|
|
* use multiple tablespaces. For non-parallel mode, we just pass a NULL
|
2014-05-06 18:12:18 +02:00
|
|
|
* tablespace path, which matches all tablespaces. In parallel mode, we
|
2013-05-29 22:58:43 +02:00
|
|
|
* pass the default tablespace and all user-created tablespaces and let
|
|
|
|
* those operations happen in parallel.
|
2013-01-09 14:57:47 +01:00
|
|
|
*/
|
|
|
|
if (user_opts.jobs <= 1)
|
|
|
|
parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
|
|
|
|
new_pgdata, NULL);
|
|
|
|
else
|
|
|
|
{
|
2013-05-29 22:58:43 +02:00
|
|
|
int tblnum;
|
2013-01-09 14:57:47 +01:00
|
|
|
|
|
|
|
/* transfer default tablespace */
|
|
|
|
parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
|
2013-05-29 22:58:43 +02:00
|
|
|
new_pgdata, old_pgdata);
|
2013-01-09 14:57:47 +01:00
|
|
|
|
|
|
|
for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
|
2013-06-01 15:38:15 +02:00
|
|
|
parallel_transfer_all_new_dbs(old_db_arr,
|
|
|
|
new_db_arr,
|
|
|
|
old_pgdata,
|
|
|
|
new_pgdata,
|
|
|
|
os_info.old_tablespaces[tblnum]);
|
2013-01-09 14:57:47 +01:00
|
|
|
/* reap all children */
|
|
|
|
while (reap_child(true) == true)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_progress_output();
|
|
|
|
check_ok();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
/*
|
|
|
|
* transfer_all_new_dbs()
|
|
|
|
*
|
|
|
|
* Responsible for upgrading all database. invokes routines to generate mappings and then
|
|
|
|
* physically link the databases.
|
|
|
|
*/
|
2013-01-03 03:20:13 +01:00
|
|
|
void
|
2013-01-09 14:57:47 +01:00
|
|
|
transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
|
2013-05-29 22:58:43 +02:00
|
|
|
char *old_pgdata, char *new_pgdata, char *old_tablespace)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
int old_dbnum,
|
|
|
|
new_dbnum;
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2011-11-01 18:49:03 +01:00
|
|
|
/* Scan the old cluster databases and transfer their files */
|
|
|
|
for (old_dbnum = new_dbnum = 0;
|
2011-11-03 18:56:56 +01:00
|
|
|
old_dbnum < old_db_arr->ndbs;
|
2011-11-01 18:49:03 +01:00
|
|
|
old_dbnum++, new_dbnum++)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2012-06-10 21:20:04 +02:00
|
|
|
DbInfo *old_db = &old_db_arr->dbs[old_dbnum],
|
|
|
|
*new_db = NULL;
|
2010-05-12 04:19:11 +02:00
|
|
|
FileNameMap *mappings;
|
|
|
|
int n_maps;
|
|
|
|
pageCnvCtx *pageConverter = NULL;
|
|
|
|
|
2011-11-01 18:49:03 +01:00
|
|
|
/*
|
2012-06-10 21:20:04 +02: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-01 18:49:03 +01:00
|
|
|
*/
|
2011-11-03 18:56:56 +01: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-01 18:49:03 +01:00
|
|
|
|
2011-11-03 18:56:56 +01:00
|
|
|
if (new_dbnum >= new_db_arr->ndbs)
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("old database \"%s\" not found in the new cluster\n",
|
2014-05-06 18:12:18 +02:00
|
|
|
old_db->db_name);
|
2011-04-10 17:42:00 +02:00
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
n_maps = 0;
|
2010-10-19 23:38:16 +02:00
|
|
|
mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
|
2010-05-12 04:19:11 +02:00
|
|
|
new_pgdata);
|
|
|
|
|
|
|
|
if (n_maps)
|
|
|
|
{
|
2010-10-19 23:38:16 +02:00
|
|
|
print_maps(mappings, n_maps, new_db->db_name);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
#ifdef PAGE_CONVERSION
|
2013-01-03 03:20:13 +01:00
|
|
|
pageConverter = setupPageConverter();
|
2010-05-12 04:19:11 +02:00
|
|
|
#endif
|
2013-01-09 14:57:47 +01:00
|
|
|
transfer_single_new_db(pageConverter, mappings, n_maps,
|
|
|
|
old_tablespace);
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
pg_free(mappings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-03 03:20:13 +01:00
|
|
|
return;
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_pg_database_relfilenode()
|
|
|
|
*
|
2014-05-06 18:12:18 +02:00
|
|
|
* Retrieves the relfilenode for a few system-catalog tables. We need these
|
2010-05-12 04:19:11 +02:00
|
|
|
* relfilenodes later in the upgrade process.
|
|
|
|
*/
|
|
|
|
void
|
2011-01-01 18:06:36 +01:00
|
|
|
get_pg_database_relfilenode(ClusterInfo *cluster)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2011-01-01 18:06:36 +01:00
|
|
|
PGconn *conn = connectToServer(cluster, "template1");
|
2010-05-12 04:19:11 +02:00
|
|
|
PGresult *res;
|
|
|
|
int i_relfile;
|
|
|
|
|
2010-10-19 23:38:16 +02:00
|
|
|
res = executeQueryOrDie(conn,
|
2010-05-12 04:19:11 +02:00
|
|
|
"SELECT c.relname, c.relfilenode "
|
2011-07-12 06:13:51 +02:00
|
|
|
"FROM pg_catalog.pg_class c, "
|
2010-05-12 04:19:11 +02:00
|
|
|
" pg_catalog.pg_namespace n "
|
2011-07-12 06:13:51 +02:00
|
|
|
"WHERE c.relnamespace = n.oid AND "
|
2010-05-12 04:19:11 +02:00
|
|
|
" n.nspname = 'pg_catalog' AND "
|
|
|
|
" c.relname = 'pg_database' "
|
|
|
|
"ORDER BY c.relname");
|
|
|
|
|
|
|
|
i_relfile = PQfnumber(res, "relfilenode");
|
2011-01-01 18:06:36 +01:00
|
|
|
cluster->pg_database_oid = atooid(PQgetvalue(res, 0, i_relfile));
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
PQfinish(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* transfer_single_new_db()
|
|
|
|
*
|
|
|
|
* create links for mappings stored in "maps" array.
|
|
|
|
*/
|
|
|
|
static void
|
2010-10-19 23:38:16 +02:00
|
|
|
transfer_single_new_db(pageCnvCtx *pageConverter,
|
2013-01-09 14:57:47 +01:00
|
|
|
FileNameMap *maps, int size, char *old_tablespace)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
int mapnum;
|
2012-11-14 23:32:04 +01:00
|
|
|
bool vm_crashsafe_match = true;
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
/*
|
|
|
|
* Do the old and new cluster disagree on the crash-safetiness of the vm
|
2013-05-29 22:58:43 +02:00
|
|
|
* files? If so, do not copy them.
|
|
|
|
*/
|
2011-08-19 17:20:30 +02:00
|
|
|
if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_CRASHSAFE_CAT_VER &&
|
|
|
|
new_cluster.controldata.cat_ver >= VISIBILITY_MAP_CRASHSAFE_CAT_VER)
|
2012-11-14 23:32:04 +01:00
|
|
|
vm_crashsafe_match = false;
|
2012-06-10 21:20:04 +02:00
|
|
|
|
2010-05-12 04:19:11 +02:00
|
|
|
for (mapnum = 0; mapnum < size; mapnum++)
|
|
|
|
{
|
2013-01-09 14:57:47 +01:00
|
|
|
if (old_tablespace == NULL ||
|
|
|
|
strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2013-01-09 14:57:47 +01:00
|
|
|
/* transfer primary file */
|
|
|
|
transfer_relfile(pageConverter, &maps[mapnum], "");
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2013-01-09 14:57:47 +01:00
|
|
|
/* fsm/vm files added in PG 8.4 */
|
|
|
|
if (GET_MAJOR_VERSION(old_cluster.major_version) >= 804)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Copy/link any fsm and vm files, if they exist
|
|
|
|
*/
|
|
|
|
transfer_relfile(pageConverter, &maps[mapnum], "_fsm");
|
|
|
|
if (vm_crashsafe_match)
|
|
|
|
transfer_relfile(pageConverter, &maps[mapnum], "_vm");
|
|
|
|
}
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
2010-10-20 02:33:19 +02:00
|
|
|
}
|
2010-05-12 04:19:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* transfer_relfile()
|
|
|
|
*
|
|
|
|
* Copy or link file from old cluster to new one.
|
|
|
|
*/
|
|
|
|
static void
|
2012-11-14 23:32:04 +01:00
|
|
|
transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
|
|
|
|
const char *type_suffix)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
|
|
|
const char *msg;
|
2012-11-14 23:32:04 +01:00
|
|
|
char old_file[MAXPGPATH];
|
|
|
|
char new_file[MAXPGPATH];
|
|
|
|
int fd;
|
|
|
|
int segno;
|
|
|
|
char extent_suffix[65];
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
/*
|
2013-05-29 22:58:43 +02: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. copied.
|
2012-11-14 23:32:04 +01:00
|
|
|
*/
|
|
|
|
for (segno = 0;; segno++)
|
2010-05-12 04:19:11 +02:00
|
|
|
{
|
2012-11-14 23:32:04 +01:00
|
|
|
if (segno == 0)
|
|
|
|
extent_suffix[0] = '\0';
|
|
|
|
else
|
|
|
|
snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
|
|
|
|
|
2013-06-01 15:38:15 +02:00
|
|
|
snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
|
|
|
|
map->old_tablespace,
|
|
|
|
map->old_tablespace_suffix,
|
|
|
|
map->old_db_oid,
|
|
|
|
map->old_relfilenode,
|
|
|
|
type_suffix,
|
|
|
|
extent_suffix);
|
|
|
|
snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
|
|
|
|
map->new_tablespace,
|
|
|
|
map->new_tablespace_suffix,
|
|
|
|
map->new_db_oid,
|
|
|
|
map->new_relfilenode,
|
|
|
|
type_suffix,
|
|
|
|
extent_suffix);
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
/* Is it an extent, fsm, or vm file? */
|
|
|
|
if (type_suffix[0] != '\0' || segno != 0)
|
|
|
|
{
|
|
|
|
/* Did file open fail? */
|
2012-11-15 01:01:29 +01:00
|
|
|
if ((fd = open(old_file, O_RDONLY, 0)) == -1)
|
2012-11-14 23:32:04 +01:00
|
|
|
{
|
|
|
|
/* File does not exist? That's OK, just return */
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return;
|
|
|
|
else
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2014-05-06 18:12:18 +02:00
|
|
|
map->nspname, map->relname, old_file, new_file,
|
|
|
|
getErrorText(errno));
|
2012-11-14 23:32:04 +01:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2010-05-12 04:19:11 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
unlink(new_file);
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
/* Copying files might take some time, so give feedback. */
|
2012-12-07 18:26:13 +01:00
|
|
|
pg_log(PG_STATUS, "%s", old_file);
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("This upgrade requires page-by-page conversion, "
|
2014-05-06 18:12:18 +02:00
|
|
|
"you must use copy mode instead of link mode.\n");
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
|
|
|
|
{
|
|
|
|
pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"\n", old_file, new_file);
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
if ((msg = copyAndUpdateFile(pageConverter, old_file, new_file, true)) != NULL)
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2014-05-06 18:12:18 +02:00
|
|
|
map->nspname, map->relname, old_file, new_file, msg);
|
2012-11-14 23:32:04 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n", old_file, new_file);
|
2013-05-29 22:58:43 +02:00
|
|
|
|
2012-11-14 23:32:04 +01:00
|
|
|
if ((msg = linkAndUpdateFile(pageConverter, old_file, new_file)) != NULL)
|
2013-10-02 03:24:56 +02:00
|
|
|
pg_fatal("error while creating link for relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2014-05-06 18:12:18 +02:00
|
|
|
map->nspname, map->relname, old_file, new_file, msg);
|
2012-11-14 23:32:04 +01:00
|
|
|
}
|
2013-05-29 22:58:43 +02:00
|
|
|
}
|
2010-05-12 04:19:11 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|