2010-05-12 10:19:11 +08:00
|
|
|
/*
|
|
|
|
* tablespace.c
|
|
|
|
*
|
|
|
|
* tablespace functions
|
2010-07-03 22:23:14 +08:00
|
|
|
*
|
2011-01-02 02:18:15 +08:00
|
|
|
* Copyright (c) 2010-2011, 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
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if (os_info.num_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;
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
res = executeQueryOrDie(conn,
|
2010-05-12 10:19:11 +08:00
|
|
|
"SELECT spclocation "
|
|
|
|
"FROM pg_catalog.pg_tablespace "
|
|
|
|
"WHERE spcname != 'pg_default' AND "
|
|
|
|
" spcname != 'pg_global'");
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
if ((os_info.num_tablespaces = PQntuples(res)) != 0)
|
|
|
|
os_info.tablespaces = (char **) pg_malloc(
|
2010-10-20 06:37:04 +08:00
|
|
|
os_info.num_tablespaces * sizeof(char *));
|
2010-06-17 03:43:11 +08:00
|
|
|
else
|
2010-10-20 05:38:16 +08:00
|
|
|
os_info.tablespaces = NULL;
|
2010-05-12 10:19:11 +08:00
|
|
|
|
|
|
|
i_spclocation = PQfnumber(res, "spclocation");
|
|
|
|
|
2010-10-20 05:38:16 +08:00
|
|
|
for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
|
|
|
|
os_info.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
|
|
|
}
|
|
|
|
}
|