PostgreSQL Source Code  git master
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

◆ get_tablespace_paths()

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, 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(), S_ISDIR, snprintf, stat, 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)
61  (char **) pg_malloc(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 
71  os_info.old_tablespaces[tblnum] = pg_strdup(PQgetvalue(res, tblnum, i_spclocation));
72 
73  /*
74  * Check that the tablespace path exists and is a directory.
75  * Effectively, this is checking only for tables/indexes in
76  * non-existent tablespace directories. Databases located in
77  * non-existent tablespaces already throw a backend error.
78  * Non-existent tablespace directories can occur when a data directory
79  * that contains user tablespaces is moved as part of pg_upgrade
80  * preparation and the symbolic links are not updated.
81  */
82  if (stat(os_info.old_tablespaces[tblnum], &statBuf) != 0)
83  {
84  if (errno == ENOENT)
86  "tablespace directory \"%s\" does not exist\n",
87  os_info.old_tablespaces[tblnum]);
88  else
90  "could not stat tablespace directory \"%s\": %s\n",
91  os_info.old_tablespaces[tblnum], strerror(errno));
92  }
93  if (!S_ISDIR(statBuf.st_mode))
95  "tablespace path \"%s\" is not a directory\n",
96  os_info.old_tablespaces[tblnum]);
97  }
98 
99  PQclear(res);
100 
101  PQfinish(conn);
102 }
uint32 major_version
Definition: pg_upgrade.h:267
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
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:23
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4185
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
PGconn * conn
Definition: streamutil.c:54
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:27
#define stat(a, b)
Definition: win32_port.h:255
char ** old_tablespaces
Definition: pg_upgrade.h:311
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define strerror
Definition: port.h:206
#define S_ISDIR(m)
Definition: win32_port.h:296
int num_old_tablespaces
Definition: pg_upgrade.h:312
OSInfo os_info
Definition: pg_upgrade.c:61
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define snprintf
Definition: port.h:193
#define QUERY_ALLOC
Definition: pg_upgrade.h:19

◆ init_tablespaces()

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:106
static void get_tablespace_paths(void)
Definition: tablespace.c:40
#define pg_fatal(...)
Definition: pg_rewind.h:41
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
const char * tablespace_suffix
Definition: pg_upgrade.h:270
int num_old_tablespaces
Definition: pg_upgrade.h:312
OSInfo os_info
Definition: pg_upgrade.c:61

◆ set_tablespace_directory_suffix()

static void set_tablespace_directory_suffix ( ClusterInfo cluster)
static

Definition at line 106 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().

107 {
108  if (GET_MAJOR_VERSION(cluster->major_version) <= 804)
109  cluster->tablespace_suffix = pg_strdup("");
110  else
111  {
112  /* This cluster has a version-specific subdirectory */
113 
114  /* The leading slash is needed to start a new directory. */
115  cluster->tablespace_suffix = psprintf("/PG_%s_%d",
116  cluster->major_version_str,
117  cluster->controldata.cat_ver);
118  }
119 }
uint32 major_version
Definition: pg_upgrade.h:267
ControlData controldata
Definition: pg_upgrade.h:257
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
char major_version_str[64]
Definition: pg_upgrade.h:268
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
uint32 cat_ver
Definition: pg_upgrade.h:202
const char * tablespace_suffix
Definition: pg_upgrade.h:270