2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* tablespace.c
|
|
|
|
*
|
|
|
|
* tablespace functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2013-01-02 06:15:01 +08:00
|
|
|
* Copyright (c) 2010-2013, PostgreSQL Global Development Group
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pg_upgrade/tablespace.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"
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
static void get_tablespace_paths(void);
|
2011-01-02 01:06:36 +08:00
|
|
|
static void set_tablespace_directory_suffix(ClusterInfo *cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-10-20 05:38:16 +08:00
|
|
|
init_tablespaces(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2010-10-20 05:38:16 +08:00
|
|
|
get_tablespace_paths();
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2011-01-02 01:06:36 +08:00
|
|
|
set_tablespace_directory_suffix(&old_cluster);
|
|
|
|
set_tablespace_directory_suffix(&new_cluster);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2013-01-09 21:57:47 +08:00
|
|
|
if (os_info.num_old_tablespaces > 0 &&
|
2010-10-20 06:37:04 +08:00
|
|
|
strcmp(old_cluster.tablespace_suffix, new_cluster.tablespace_suffix) == 0)
|
2010-10-20 05:38:16 +08:00
|
|
|
pg_log(PG_FATAL,
|
2010-12-15 20:11:31 +08:00
|
|
|
"Cannot upgrade to/from the same system catalog version when\n"
|
2010-05-12 10:19:11 +08:00
|
|
|
"using tablespaces.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_tablespace_paths()
|
|
|
|
*
|
|
|
|
* Scans pg_tablespace and returns a malloc'ed array of all tablespace
|
|
|
|
* paths. Its the caller's responsibility to free the array.
|
|
|
|
*/
|
|
|
|
static void
|
2010-10-20 05:38:16 +08:00
|
|
|
get_tablespace_paths(void)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:06:36 +08:00
|
|
|
PGconn *conn = connectToServer(&old_cluster, "template1");
|
2010-05-12 10:19:11 +08:00
|
|
|
PGresult *res;
|
|
|
|
int tblnum;
|
|
|
|
int i_spclocation;
|
2011-12-07 17:35:00 +08:00
|
|
|
char query[QUERY_ALLOC];
|
|
|
|
|
|
|
|
snprintf(query, sizeof(query),
|
|
|
|
"SELECT %s "
|
|
|
|
"FROM pg_catalog.pg_tablespace "
|
|
|
|
"WHERE spcname != 'pg_default' AND "
|
|
|
|
" spcname != 'pg_global'",
|
|
|
|
/* 9.2 removed the spclocation column */
|
2012-06-11 03:20:04 +08:00
|
|
|
(GET_MAJOR_VERSION(old_cluster.major_version) <= 901) ?
|
|
|
|
"spclocation" : "pg_catalog.pg_tablespace_location(oid) AS spclocation");
|
2011-12-07 17:35:00 +08:00
|
|
|
|
|
|
|
res = executeQueryOrDie(conn, "%s", query);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
2013-01-09 21:57:47 +08:00
|
|
|
if ((os_info.num_old_tablespaces = PQntuples(res)) != 0)
|
|
|
|
os_info.old_tablespaces = (char **) pg_malloc(
|
|
|
|
os_info.num_old_tablespaces * sizeof(char *));
|
2010-06-17 03:43:11 +08:00
|
|
|
else
|
2013-01-09 21:57:47 +08:00
|
|
|
os_info.old_tablespaces = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
i_spclocation = PQfnumber(res, "spclocation");
|
|
|
|
|
2013-01-09 21:57:47 +08:00
|
|
|
for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
|
|
|
|
os_info.old_tablespaces[tblnum] = pg_strdup(
|
2010-05-12 10:19:11 +08:00
|
|
|
PQgetvalue(res, tblnum, i_spclocation));
|
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
PQfinish(conn);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2011-01-02 01:06:36 +08:00
|
|
|
set_tablespace_directory_suffix(ClusterInfo *cluster)
|
2010-05-12 10:19:11 +08:00
|
|
|
{
|
2011-01-02 01:06:36 +08:00
|
|
|
if (GET_MAJOR_VERSION(cluster->major_version) <= 804)
|
|
|
|
cluster->tablespace_suffix = pg_strdup("");
|
2010-05-12 10:19:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This cluster has a version-specific subdirectory */
|
2011-01-02 01:06:36 +08:00
|
|
|
cluster->tablespace_suffix = pg_malloc(4 +
|
2011-04-10 23:42:00 +08:00
|
|
|
strlen(cluster->major_version_str) +
|
|
|
|
10 /* OIDCHARS */ + 1);
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
/* The leading slash is needed to start a new directory. */
|
2011-01-02 01:06:36 +08:00
|
|
|
sprintf(cluster->tablespace_suffix, "/PG_%s_%d", cluster->major_version_str,
|
|
|
|
cluster->controldata.cat_ver);
|
2010-05-12 10:19:11 +08:00
|
|
|
}
|
|
|
|
}
|