aboutsummaryrefslogtreecommitdiff
path: root/contrib/pg_upgrade/tablespace.c
blob: 2dc4c47ac75ac78cccb47b08609239550e784196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 *	tablespace.c
 *
 *	tablespace functions
 *
 *	$PostgreSQL: pgsql/contrib/pg_upgrade/tablespace.c,v 1.3 2010/07/03 14:23:14 momjian Exp $
 */

#include "pg_upgrade.h"

static void get_tablespace_paths(migratorContext *ctx);
static void set_tablespace_directory_suffix(migratorContext *ctx,
							Cluster whichCluster);


void
init_tablespaces(migratorContext *ctx)
{
	get_tablespace_paths(ctx);

	set_tablespace_directory_suffix(ctx, CLUSTER_OLD);
	set_tablespace_directory_suffix(ctx, CLUSTER_NEW);

	if (ctx->num_tablespaces > 0 &&
		strcmp(ctx->old.tablespace_suffix, ctx->new.tablespace_suffix) == 0)
		pg_log(ctx, PG_FATAL,
			   "Cannot migrate to/from the same system catalog version when\n"
			   "using tablespaces.\n");
}


/*
 * get_tablespace_paths()
 *
 * Scans pg_tablespace and returns a malloc'ed array of all tablespace
 * paths. Its the caller's responsibility to free the array.
 */
static void
get_tablespace_paths(migratorContext *ctx)
{
	PGconn	   *conn = connectToServer(ctx, "template1", CLUSTER_OLD);
	PGresult   *res;
	int			tblnum;
	int			i_spclocation;

	res = executeQueryOrDie(ctx, conn,
							"SELECT	spclocation "
							"FROM	pg_catalog.pg_tablespace "
							"WHERE	spcname != 'pg_default' AND "
							"		spcname != 'pg_global'");

	if ((ctx->num_tablespaces = PQntuples(res)) != 0)
		ctx->tablespaces = (char **) pg_malloc(ctx,
									ctx->num_tablespaces * sizeof(char *));
	else
		ctx->tablespaces = NULL;

	i_spclocation = PQfnumber(res, "spclocation");

	for (tblnum = 0; tblnum < ctx->num_tablespaces; tblnum++)
		ctx->tablespaces[tblnum] = pg_strdup(ctx,
									 PQgetvalue(res, tblnum, i_spclocation));

	PQclear(res);

	PQfinish(conn);

	return;
}


static void
set_tablespace_directory_suffix(migratorContext *ctx, Cluster whichCluster)
{
	ClusterInfo *cluster = (whichCluster == CLUSTER_OLD) ? &ctx->old : &ctx->new;

	if (GET_MAJOR_VERSION(cluster->major_version) <= 804)
		cluster->tablespace_suffix = pg_strdup(ctx, "");
	else
	{
		/* This cluster has a version-specific subdirectory */
		cluster->tablespace_suffix = pg_malloc(ctx, 4 + strlen(cluster->major_version_str) +
											   10 /* OIDCHARS */ + 1);

		/* The leading slash is needed to start a new directory. */
		sprintf(cluster->tablespace_suffix, "/PG_%s_%d", cluster->major_version_str,
				cluster->controldata.cat_ver);
	}
}