6 * Copyright (c) 2010-2011, 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(ClusterInfo *cluster);
17 init_tablespaces(void)
19 get_tablespace_paths();
21 set_tablespace_directory_suffix(&old_cluster);
22 set_tablespace_directory_suffix(&new_cluster);
24 if (os_info.num_tablespaces > 0 &&
25 strcmp(old_cluster.tablespace_suffix, new_cluster.tablespace_suffix) == 0)
27 "Cannot upgrade to/from the same system catalog version when\n"
28 "using tablespaces.\n");
33 * get_tablespace_paths()
35 * Scans pg_tablespace and returns a malloc'ed array of all tablespace
36 * paths. Its the caller's responsibility to free the array.
39 get_tablespace_paths(void)
41 PGconn *conn = connectToServer(&old_cluster, "template1");
46 res = executeQueryOrDie(conn,
48 "FROM pg_catalog.pg_tablespace "
49 "WHERE spcname != 'pg_default' AND "
50 " spcname != 'pg_global'");
52 if ((os_info.num_tablespaces = PQntuples(res)) != 0)
53 os_info.tablespaces = (char **) pg_malloc(
54 os_info.num_tablespaces * sizeof(char *));
56 os_info.tablespaces = NULL;
58 i_spclocation = PQfnumber(res, "spclocation");
60 for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
61 os_info.tablespaces[tblnum] = pg_strdup(
62 PQgetvalue(res, tblnum, i_spclocation));
73 set_tablespace_directory_suffix(ClusterInfo *cluster)
75 if (GET_MAJOR_VERSION(cluster->major_version) <= 804)
76 cluster->tablespace_suffix = pg_strdup("");
79 /* This cluster has a version-specific subdirectory */
80 cluster->tablespace_suffix = pg_malloc(4 +
81 strlen(cluster->major_version_str) +
82 10 /* OIDCHARS */ + 1);
84 /* The leading slash is needed to start a new directory. */
85 sprintf(cluster->tablespace_suffix, "/PG_%s_%d", cluster->major_version_str,
86 cluster->controldata.cat_ver);