PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tablespace.c File Reference
#include "postgres_fe.h"
#include "pg_upgrade.h"
Include dependency graph for tablespace.c:

Go to the source code of this file.

Functions

static void get_tablespace_paths (void)
 
static void set_tablespace_directory_suffix (ClusterInfo *cluster)
 
void init_tablespaces (void)
 

Function Documentation

static void get_tablespace_paths ( void  )
static

Definition at line 40 of file tablespace.c.

References conn, connectToServer(), executeQueryOrDie(), GET_MAJOR_VERSION, ClusterInfo::major_version, NULL, OSInfo::num_old_tablespaces, old_cluster, OSInfo::old_tablespaces, os_info, PG_FATAL, pg_malloc(), pg_strdup(), PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), QUERY_ALLOC, report_status(), snprintf(), and strerror().

Referenced by init_tablespaces().

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
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 }
uint32 major_version
Definition: pg_upgrade.h:270
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
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
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
#define QUERY_ALLOC
Definition: logging.c:26
PGconn * conn
Definition: streamutil.c:43
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:27
char ** old_tablespaces
Definition: pg_upgrade.h:309
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2781
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
int num_old_tablespaces
Definition: pg_upgrade.h:310
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
void init_tablespaces ( void  )

Definition at line 19 of file tablespace.c.

References get_tablespace_paths(), new_cluster, OSInfo::num_old_tablespaces, old_cluster, os_info, pg_fatal(), set_tablespace_directory_suffix(), and ClusterInfo::tablespace_suffix.

Referenced by check_and_dump_old_cluster().

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 }
static void set_tablespace_directory_suffix(ClusterInfo *cluster)
Definition: tablespace.c:109
static void get_tablespace_paths(void)
Definition: tablespace.c:40
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
const char * tablespace_suffix
Definition: pg_upgrade.h:273
int num_old_tablespaces
Definition: pg_upgrade.h:310
OSInfo os_info
Definition: pg_upgrade.c:58
static void set_tablespace_directory_suffix ( ClusterInfo cluster)
static

Definition at line 109 of file tablespace.c.

References ControlData::cat_ver, ClusterInfo::controldata, GET_MAJOR_VERSION, ClusterInfo::major_version, ClusterInfo::major_version_str, pg_strdup(), psprintf(), and ClusterInfo::tablespace_suffix.

Referenced by init_tablespaces().

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 }
uint32 major_version
Definition: pg_upgrade.h:270
ControlData controldata
Definition: pg_upgrade.h:260
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
char major_version_str[64]
Definition: pg_upgrade.h:271
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
uint32 cat_ver
Definition: pg_upgrade.h:206
const char * tablespace_suffix
Definition: pg_upgrade.h:273