]> granicus.if.org Git - postgresql/blob - contrib/pg_upgrade/tablespace.c
Use "upgrade" in preference over "migrate" in pg_upgrade messages and
[postgresql] / contrib / pg_upgrade / tablespace.c
1 /*
2  *      tablespace.c
3  *
4  *      tablespace functions
5  *
6  *      Copyright (c) 2010, PostgreSQL Global Development Group
7  *      contrib/pg_upgrade/tablespace.c
8  */
9
10 #include "pg_upgrade.h"
11
12 static void get_tablespace_paths(void);
13 static void set_tablespace_directory_suffix(
14                                                                 Cluster whichCluster);
15
16
17 void
18 init_tablespaces(void)
19 {
20         get_tablespace_paths();
21
22         set_tablespace_directory_suffix(CLUSTER_OLD);
23         set_tablespace_directory_suffix(CLUSTER_NEW);
24
25         if (os_info.num_tablespaces > 0 &&
26         strcmp(old_cluster.tablespace_suffix, new_cluster.tablespace_suffix) == 0)
27                 pg_log(PG_FATAL,
28                            "Cannot upgrade to/from the same system catalog version when\n"
29                            "using tablespaces.\n");
30 }
31
32
33 /*
34  * get_tablespace_paths()
35  *
36  * Scans pg_tablespace and returns a malloc'ed array of all tablespace
37  * paths. Its the caller's responsibility to free the array.
38  */
39 static void
40 get_tablespace_paths(void)
41 {
42         PGconn     *conn = connectToServer("template1", CLUSTER_OLD);
43         PGresult   *res;
44         int                     tblnum;
45         int                     i_spclocation;
46
47         res = executeQueryOrDie(conn,
48                                                         "SELECT spclocation "
49                                                         "FROM   pg_catalog.pg_tablespace "
50                                                         "WHERE  spcname != 'pg_default' AND "
51                                                         "               spcname != 'pg_global'");
52
53         if ((os_info.num_tablespaces = PQntuples(res)) != 0)
54                 os_info.tablespaces = (char **) pg_malloc(
55                                                                    os_info.num_tablespaces * sizeof(char *));
56         else
57                 os_info.tablespaces = NULL;
58
59         i_spclocation = PQfnumber(res, "spclocation");
60
61         for (tblnum = 0; tblnum < os_info.num_tablespaces; tblnum++)
62                 os_info.tablespaces[tblnum] = pg_strdup(
63                                                                          PQgetvalue(res, tblnum, i_spclocation));
64
65         PQclear(res);
66
67         PQfinish(conn);
68
69         return;
70 }
71
72
73 static void
74 set_tablespace_directory_suffix(Cluster whichCluster)
75 {
76         ClusterInfo *active_cluster = ACTIVE_CLUSTER(whichCluster);
77
78         if (GET_MAJOR_VERSION(active_cluster->major_version) <= 804)
79                 active_cluster->tablespace_suffix = pg_strdup("");
80         else
81         {
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);
86
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);
90         }
91 }