2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* relfilenode.c
|
|
|
|
*
|
|
|
|
* relfilenode functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2014-01-08 05:05:30 +08:00
|
|
|
* Copyright (c) 2010-2014, 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
|
|
|
*/
|
|
|
|
|
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 21:33:40 +08:00
|
|
|
#include "postgres_fe.h"
|
2011-08-27 09:16:24 +08:00
|
|
|
|
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,
|
2013-01-09 21:57:47 +08:00
|
|
|
FileNameMap *maps, int size, char *old_tablespace);
|
2012-11-15 06:32:04 +08:00
|
|
|
static void transfer_relfile(pageCnvCtx *pageConverter, FileNameMap *map,
|
2013-05-30 04:58:43 +08:00
|
|
|
const char *suffix);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2010-07-07 03:19:02 +08:00
|
|
|
|
2013-01-09 21:57:47 +08: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-30 04:58:43 +08:00
|
|
|
char *old_pgdata, char *new_pgdata)
|
2013-01-09 21:57:47 +08:00
|
|
|
{
|
|
|
|
pg_log(PG_REPORT, "%s user relation files\n",
|
|
|
|
user_opts.transfer_mode == TRANSFER_MODE_LINK ? "Linking" : "Copying");
|
|
|
|
|
|
|
|
/*
|
2013-05-30 04:58:43 +08: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-07 00:12:18 +08:00
|
|
|
* tablespace path, which matches all tablespaces. In parallel mode, we
|
2013-05-30 04:58:43 +08:00
|
|
|
* pass the default tablespace and all user-created tablespaces and let
|
|
|
|
* those operations happen in parallel.
|
2013-01-09 21:57:47 +08: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-30 04:58:43 +08:00
|
|
|
int tblnum;
|
2013-01-09 21:57:47 +08:00
|
|
|
|
|
|
|
/* transfer default tablespace */
|
|
|
|
parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
|
2013-05-30 04:58:43 +08:00
|
|
|
new_pgdata, old_pgdata);
|
2013-01-09 21:57:47 +08:00
|
|
|
|
|
|
|
for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
|
2013-06-01 21:38:15 +08:00
|
|
|
parallel_transfer_all_new_dbs(old_db_arr,
|
|
|
|
new_db_arr,
|
|
|
|
old_pgdata,
|
|
|
|
new_pgdata,
|
|
|
|
os_info.old_tablespaces[tblnum]);
|
2013-01-09 21:57:47 +08:00
|
|
|
/* reap all children */
|
|
|
|
while (reap_child(true) == true)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
end_progress_output();
|
|
|
|
check_ok();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-01-03 10:20:13 +08:00
|
|
|
void
|
2013-01-09 21:57:47 +08:00
|
|
|
transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
|
2013-05-30 04:58:43 +08:00
|
|
|
char *old_pgdata, char *new_pgdata, char *old_tablespace)
|
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
|
|
|
|
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)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("old database \"%s\" not found in the new cluster\n",
|
2014-05-07 00:12:18 +08:00
|
|
|
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
|
2013-01-03 10:20:13 +08:00
|
|
|
pageConverter = setupPageConverter();
|
2010-05-12 10:19:11 +08:00
|
|
|
#endif
|
2013-01-09 21:57:47 +08:00
|
|
|
transfer_single_new_db(pageConverter, mappings, n_maps,
|
|
|
|
old_tablespace);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
pg_free(mappings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-03 10:20:13 +08:00
|
|
|
return;
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_pg_database_relfilenode()
|
|
|
|
*
|
2014-05-07 00:12:18 +08:00
|
|
|
* Retrieves the relfilenode for a few system-catalog tables. We need these
|
2010-05-12 10:19:11 +08:00
|
|
|
* 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,
|
2013-01-09 21:57:47 +08:00
|
|
|
FileNameMap *maps, int size, char *old_tablespace)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
|
|
|
int mapnum;
|
2012-11-15 06:32:04 +08:00
|
|
|
bool vm_crashsafe_match = true;
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
/*
|
|
|
|
* Do the old and new cluster disagree on the crash-safetiness of the vm
|
2013-05-30 04:58:43 +08:00
|
|
|
* 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++)
|
|
|
|
{
|
2013-01-09 21:57:47 +08:00
|
|
|
if (old_tablespace == NULL ||
|
|
|
|
strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2013-01-09 21:57:47 +08:00
|
|
|
/* transfer primary file */
|
|
|
|
transfer_relfile(pageConverter, &maps[mapnum], "");
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2013-01-09 21:57:47 +08: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 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];
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
/*
|
2013-05-30 04:58:43 +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. copied.
|
2012-11-15 06:32:04 +08:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
|
2013-06-01 21:38:15 +08: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-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
/* 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
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2014-05-07 00:12:18 +08:00
|
|
|
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);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
/* Copying files might take some time, so give feedback. */
|
2012-12-08 01:26:13 +08:00
|
|
|
pg_log(PG_STATUS, "%s", old_file);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
if ((user_opts.transfer_mode == TRANSFER_MODE_LINK) && (pageConverter != NULL))
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("This upgrade requires page-by-page conversion, "
|
2014-05-07 00:12:18 +08:00
|
|
|
"you must use copy mode instead of link mode.\n");
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
if (user_opts.transfer_mode == TRANSFER_MODE_COPY)
|
|
|
|
{
|
|
|
|
pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"\n", old_file, new_file);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
if ((msg = copyAndUpdateFile(pageConverter, old_file, new_file, true)) != NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2014-05-07 00:12:18 +08:00
|
|
|
map->nspname, map->relname, old_file, new_file, msg);
|
2012-11-15 06:32:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"\n", old_file, new_file);
|
2013-05-30 04:58:43 +08:00
|
|
|
|
2012-11-15 06:32:04 +08:00
|
|
|
if ((msg = linkAndUpdateFile(pageConverter, old_file, new_file)) != NULL)
|
2013-10-02 09:24:56 +08:00
|
|
|
pg_fatal("error while creating link for relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
|
2014-05-07 00:12:18 +08:00
|
|
|
map->nspname, map->relname, old_file, new_file, msg);
|
2012-11-15 06:32:04 +08:00
|
|
|
}
|
2013-05-30 04:58:43 +08:00
|
|
|
}
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|