6 * Copyright (c) 2010, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/tablespace.c
10 #include "pg_upgrade.h"
12 static void get_tablespace_paths(void);
13 static void set_tablespace_directory_suffix(
14 Cluster whichCluster);
18 init_tablespaces(void)
20 get_tablespace_paths();
22 set_tablespace_directory_suffix(CLUSTER_OLD);
23 set_tablespace_directory_suffix(CLUSTER_NEW);
25 if (os_info.num_tablespaces > 0 &&
26 strcmp(old_cluster.tablespace_suffix, new_cluster.tablespace_suffix) == 0)
28 "Cannot upgrade to/from the same system catalog version when\n"
29 "using tablespaces.\n");
34 * get_tablespace_paths()
36 * Scans pg_tablespace and returns a malloc'ed array of all tablespace
37 * paths. Its the caller's responsibility to free the array.
40 get_tablespace_paths(void)
42 PGconn *conn = connectToServer("template1", CLUSTER_OLD);
47 res = executeQueryOrDie(conn,
49 "FROM pg_catalog.pg_tablespace "
50 "WHERE spcname != 'pg_default' AND "
51 " spcname != 'pg_global'");
53 if ((os_info.num_tablespaces = PQntuples(res)) != 0)
54 os_info.tablespaces = (char **) pg_malloc(
55 os_info.num_tablespaces * sizeof(char *));
57 os_info.tablespaces = NULL;
59 i_spclocation = PQfnumber(res, "spclocation");
61 for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
62 os_info.tablespaces[tblnum] = pg_strdup(
63 PQgetvalue(res, tblnum, i_spclocation));
74 set_tablespace_directory_suffix(Cluster whichCluster)
76 ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster);
78 if (GET_MAJOR_VERSION(active_cluster->major_version) <= 804)
79 active_cluster->tablespace_suffix = pg_strdup("");
82 /* This cluster has a version-specific subdirectory */
83 active_cluster->tablespace_suffix = pg_malloc(4 +
84 strlen(active_cluster->major_version_str) +
85 10 /* OIDCHARS */ + 1);
87 /* The leading slash is needed to start a new directory. */
88 sprintf(active_cluster->tablespace_suffix, "/PG_%s_%d", active_cluster->major_version_str,
89 active_cluster->controldata.cat_ver);