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.

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 pg_catalog.pg_tablespace_location(oid) AS spclocation "
50  "FROM pg_catalog.pg_tablespace "
51  "WHERE spcname != 'pg_default' AND "
52  " spcname != 'pg_global'");
53 
54  res = executeQueryOrDie(conn, "%s", query);
55 
58  (char **) pg_malloc(os_info.num_old_tablespaces * sizeof(char *));
59  else
60  os_info.old_tablespaces = NULL;
61 
62  i_spclocation = PQfnumber(res, "spclocation");
63 
64  for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
65  {
66  struct stat statBuf;
67 
68  os_info.old_tablespaces[tblnum] = pg_strdup(PQgetvalue(res, tblnum, i_spclocation));
69 
70  /*
71  * Check that the tablespace path exists and is a directory.
72  * Effectively, this is checking only for tables/indexes in
73  * non-existent tablespace directories. Databases located in
74  * non-existent tablespaces already throw a backend error.
75  * Non-existent tablespace directories can occur when a data directory
76  * that contains user tablespaces is moved as part of pg_upgrade
77  * preparation and the symbolic links are not updated.
78  */
79  if (stat(os_info.old_tablespaces[tblnum], &statBuf) != 0)
80  {
81  if (errno == ENOENT)
83  "tablespace directory \"%s\" does not exist",
84  os_info.old_tablespaces[tblnum]);
85  else
87  "could not stat tablespace directory \"%s\": %m",
88  os_info.old_tablespaces[tblnum]);
89  }
90  if (!S_ISDIR(statBuf.st_mode))
92  "tablespace path \"%s\" is not a directory",
93  os_info.old_tablespaces[tblnum]);
94  }
95 
96  PQclear(res);
97 
98  PQfinish(conn);
99 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4893
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
OSInfo os_info
Definition: pg_upgrade.c:69
ClusterInfo old_cluster
Definition: pg_upgrade.c:67
#define QUERY_ALLOC
Definition: pg_upgrade.h:23
@ PG_FATAL
Definition: pg_upgrade.h:271
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
void report_status(eLogType type, const char *fmt,...) pg_attribute_printf(2
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define snprintf
Definition: port.h:238
PGconn * conn
Definition: streamutil.c:55
int num_old_tablespaces
Definition: pg_upgrade.h:347
char ** old_tablespaces
Definition: pg_upgrade.h:346
#define stat
Definition: win32_port.h:284
#define S_ISDIR(m)
Definition: win32_port.h:325

References conn, connectToServer(), executeQueryOrDie(), 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(), res, S_ISDIR, snprintf, stat::st_mode, and stat.

Referenced by init_tablespaces().

◆ init_tablespaces()

void init_tablespaces ( void  )

Definition at line 19 of file tablespace.c.

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.");
30 }
static void set_tablespace_directory_suffix(ClusterInfo *cluster)
Definition: tablespace.c:103
static void get_tablespace_paths(void)
Definition: tablespace.c:40
#define pg_fatal(...)
ClusterInfo new_cluster
Definition: pg_upgrade.c:68
const char * tablespace_suffix
Definition: pg_upgrade.h:296

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().

◆ set_tablespace_directory_suffix()

static void set_tablespace_directory_suffix ( ClusterInfo cluster)
static

Definition at line 103 of file tablespace.c.

104 {
105  /* This cluster has a version-specific subdirectory */
106 
107  /* The leading slash is needed to start a new directory. */
108  cluster->tablespace_suffix = psprintf("/PG_%s_%d",
109  cluster->major_version_str,
110  cluster->controldata.cat_ver);
111 }
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

References cluster(), and psprintf().

Referenced by init_tablespaces().