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