PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tablespace.c
Go to the documentation of this file.
1 /*
2  * tablespace.c
3  *
4  * tablespace functions
5  *
6  * Copyright (c) 2010-2017, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/tablespace.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include "pg_upgrade.h"
13 
14 static void get_tablespace_paths(void);
16 
17 
18 void
20 {
22 
25 
26  if (os_info.num_old_tablespaces > 0 &&
28  pg_fatal("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
41 {
42  PGconn *conn = connectToServer(&old_cluster, "template1");
43  PGresult *res;
44  int tblnum;
45  int i_spclocation;
46  char query[QUERY_ALLOC];
47 
48  snprintf(query, sizeof(query),
49  "SELECT %s "
50  "FROM pg_catalog.pg_tablespace "
51  "WHERE spcname != 'pg_default' AND "
52  " spcname != 'pg_global'",
53  /* 9.2 removed the spclocation column */
55  "spclocation" : "pg_catalog.pg_tablespace_location(oid) AS spclocation");
56 
57  res = executeQueryOrDie(conn, "%s", query);
58 
59  if ((os_info.num_old_tablespaces = PQntuples(res)) != 0)
60  os_info.old_tablespaces = (char **) pg_malloc(
61  os_info.num_old_tablespaces * sizeof(char *));
62  else
63  os_info.old_tablespaces = NULL;
64 
65  i_spclocation = PQfnumber(res, "spclocation");
66 
67  for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
68  {
69  struct stat statBuf;
70 
72  PQgetvalue(res, tblnum, i_spclocation));
73 
74  /*
75  * Check that the tablespace path exists and is a directory.
76  * Effectively, this is checking only for tables/indexes in
77  * non-existent tablespace directories. Databases located in
78  * non-existent tablespaces already throw a backend error.
79  * Non-existent tablespace directories can occur when a data directory
80  * that contains user tablespaces is moved as part of pg_upgrade
81  * preparation and the symbolic links are not updated.
82  */
83  if (stat(os_info.old_tablespaces[tblnum], &statBuf) != 0)
84  {
85  if (errno == ENOENT)
87  "tablespace directory \"%s\" does not exist\n",
88  os_info.old_tablespaces[tblnum]);
89  else
91  "could not stat tablespace directory \"%s\": %s\n",
92  os_info.old_tablespaces[tblnum], strerror(errno));
93  }
94  if (!S_ISDIR(statBuf.st_mode))
96  "tablespace path \"%s\" is not a directory\n",
97  os_info.old_tablespaces[tblnum]);
98  }
99 
100  PQclear(res);
101 
102  PQfinish(conn);
103 
104  return;
105 }
106 
107 
108 static void
110 {
111  if (GET_MAJOR_VERSION(cluster->major_version) <= 804)
112  cluster->tablespace_suffix = pg_strdup("");
113  else
114  {
115  /* This cluster has a version-specific subdirectory */
116 
117  /* The leading slash is needed to start a new directory. */
118  cluster->tablespace_suffix = psprintf("/PG_%s_%d",
119  cluster->major_version_str,
120  cluster->controldata.cat_ver);
121  }
122 }
static void set_tablespace_directory_suffix(ClusterInfo *cluster)
Definition: tablespace.c:109
static void get_tablespace_paths(void)
Definition: tablespace.c:40
uint32 major_version
Definition: pg_upgrade.h:274
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
ControlData controldata
Definition: pg_upgrade.h:264
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
#define QUERY_ALLOC
Definition: logging.c:26
char major_version_str[64]
Definition: pg_upgrade.h:275
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
PGconn * conn
Definition: streamutil.c:49
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void cluster(ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:106
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:27
uint32 cat_ver
Definition: pg_upgrade.h:210
char ** old_tablespaces
Definition: pg_upgrade.h:313
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2832
const char * tablespace_suffix
Definition: pg_upgrade.h:277
void init_tablespaces(void)
Definition: tablespace.c:19
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int num_old_tablespaces
Definition: pg_upgrade.h:314
const char * strerror(int errnum)
Definition: strerror.c:19
OSInfo os_info
Definition: pg_upgrade.c:58
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2