info.c 10.1 KB
Newer Older
1 2 3 4
/*
 *	info.c
 *
 *	information support functions
5
 *
6
 *	Copyright (c) 2010-2011, PostgreSQL Global Development Group
7
 *	contrib/pg_upgrade/info.c
8 9 10 11 12 13 14
 */

#include "pg_upgrade.h"

#include "access/transam.h"


15 16 17 18
static void create_rel_filename_map(const char *old_data, const char *new_data,
			  const DbInfo *old_db, const DbInfo *new_db,
			  const RelInfo *old_rel, const RelInfo *new_rel,
			  FileNameMap *map);
19 20 21 22 23
static void get_db_infos(ClusterInfo *cluster);
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo);
static void free_rel_infos(RelInfoArr *rel_arr);
static void print_db_infos(DbInfoArr *dbinfo);
static void print_rel_infos(RelInfoArr *arr);
24 25 26 27 28 29 30 31 32 33


/*
 * gen_db_file_maps()
 *
 * generates database mappings for "old_db" and "new_db". Returns a malloc'ed
 * array of mappings. nmaps is a return parameter which refers to the number
 * mappings.
 */
FileNameMap *
34
gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
35 36 37 38 39 40
				 int *nmaps, const char *old_pgdata, const char *new_pgdata)
{
	FileNameMap *maps;
	int			relnum;
	int			num_maps = 0;

41 42 43 44
	if (old_db->rel_arr.nrels != new_db->rel_arr.nrels)
		pg_log(PG_FATAL, "old and new databases \"%s\" have a different number of relations\n",
					old_db->db_name);

45
	maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
46
									 old_db->rel_arr.nrels);
47

48
	for (relnum = 0; relnum < old_db->rel_arr.nrels; relnum++)
49
	{
50
		RelInfo    *old_rel = &old_db->rel_arr.rels[relnum];
51
		RelInfo    *new_rel = &new_db->rel_arr.rels[relnum];
52

53
		if (old_rel->reloid != new_rel->reloid)
54
			pg_log(PG_FATAL, "Mismatch of relation id: database \"%s\", old relid %d, new relid %d\n",
55
			old_db->db_name, old_rel->reloid, new_rel->reloid);
56

57 58
		/*
		 *	In pre-8.4, TOAST table names change during CLUSTER;  in >= 8.4
59
		 *	TOAST relation names always use heap table oids, hence we
60 61
		 *	cannot check relation names when upgrading from pre-8.4.
		 */
62
		if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
63 64 65
			((GET_MAJOR_VERSION(old_cluster.major_version) >= 804 ||
			  strcmp(old_rel->nspname, "pg_toast") != 0) &&
			 strcmp(old_rel->relname, new_rel->relname) != 0))
66 67 68 69 70
			pg_log(PG_FATAL, "Mismatch of relation names: database \"%s\", "
				"old rel %s.%s, new rel %s.%s\n",
				old_db->db_name, old_rel->nspname, old_rel->relname,
				new_rel->nspname, new_rel->relname);

71
		create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
72
				old_rel, new_rel, maps + num_maps);
73 74 75 76 77 78 79 80 81
		num_maps++;
	}

	*nmaps = num_maps;
	return maps;
}


/*
82
 * create_rel_filename_map()
83 84 85 86
 *
 * fills a file node map structure and returns it in "map".
 */
static void
87 88 89 90
create_rel_filename_map(const char *old_data, const char *new_data,
			  const DbInfo *old_db, const DbInfo *new_db,
			  const RelInfo *old_rel, const RelInfo *new_rel,
			  FileNameMap *map)
91
{
92
	if (strlen(old_rel->tablespace) == 0)
93 94
	{
		/*
95
		 * relation belongs to the default tablespace, hence relfiles should
96 97
		 * exist in the data directories.
		 */
98 99 100 101
		snprintf(map->old_dir, sizeof(map->old_dir), "%s/base/%u", old_data,
				 old_db->db_oid);
		snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", new_data,
				 new_db->db_oid);
102 103 104
	}
	else
	{
105
		/* relation belongs to a tablespace, so use the tablespace location */
106
		snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_rel->tablespace,
107
				 old_cluster.tablespace_suffix, old_db->db_oid);
108
		snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", new_rel->tablespace,
109
				 new_cluster.tablespace_suffix, new_db->db_oid);
110
	}
111

112 113 114 115
	/*
	 *	old_relfilenode might differ from pg_class.oid (and hence
	 *	new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
	 */
116
	map->old_relfilenode = old_rel->relfilenode;
117 118

	/* new_relfilenode will match old and new pg_class.oid */
119 120
	map->new_relfilenode = new_rel->relfilenode;

121 122 123
	/* used only for logging and error reporing, old/new are identical */
	snprintf(map->nspname, sizeof(map->nspname), "%s", old_rel->nspname);
	snprintf(map->relname, sizeof(map->relname), "%s", old_rel->relname);
124 125 126 127
}


void
128
print_maps(FileNameMap *maps, int n_maps, const char *db_name)
129
{
130
	if (log_opts.debug)
131 132 133
	{
		int			mapnum;

134
		pg_log(PG_DEBUG, "mappings for db %s:\n", db_name);
135

136
		for (mapnum = 0; mapnum < n_maps; mapnum++)
137 138
			pg_log(PG_DEBUG, "%s.%s: %u to %u\n",
				   maps[mapnum].nspname, maps[mapnum].relname,
139 140
				   maps[mapnum].old_relfilenode,
				   maps[mapnum].new_relfilenode);
141

142
		pg_log(PG_DEBUG, "\n\n");
143 144 145 146
	}
}


147 148 149 150 151 152 153 154 155 156 157
/*
 * get_db_and_rel_infos()
 *
 * higher level routine to generate dbinfos for the database running
 * on the given "port". Assumes that server is already running.
 */
void
get_db_and_rel_infos(ClusterInfo *cluster)
{
	int			dbnum;

158 159
	if (cluster->dbarr.dbs != NULL)
		free_db_and_rel_infos(&cluster->dbarr);
160

161 162 163 164 165 166 167
	get_db_infos(cluster);

	for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
		get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);

	if (log_opts.debug)
	{
168
		pg_log(PG_DEBUG, "\n%s databases:\n", CLUSTER_NAME(cluster));
169 170 171 172 173
		print_db_infos(&cluster->dbarr);
	}
}


174 175 176
/*
 * get_db_infos()
 *
177
 * Scans pg_database system catalog and populates all user
178 179 180
 * databases.
 */
static void
181
get_db_infos(ClusterInfo *cluster)
182
{
183
	PGconn	   *conn = connectToServer(cluster, "template1");
184 185 186 187
	PGresult   *res;
	int			ntups;
	int			tupnum;
	DbInfo	   *dbinfos;
188
	int			i_datname, i_oid, i_spclocation;
189

190
	res = executeQueryOrDie(conn,
191 192 193 194
							"SELECT d.oid, d.datname, t.spclocation "
							"FROM pg_catalog.pg_database d "
							" LEFT OUTER JOIN pg_catalog.pg_tablespace t "
							" ON d.dattablespace = t.oid "
195 196 197
							"WHERE d.datallowconn = true "
	/* we don't preserve pg_database.oid so we sort by name */
							"ORDER BY 2");
198

199
	i_oid = PQfnumber(res, "oid");
200
	i_datname = PQfnumber(res, "datname");
201 202 203
	i_spclocation = PQfnumber(res, "spclocation");

	ntups = PQntuples(res);
204
	dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
205 206 207

	for (tupnum = 0; tupnum < ntups; tupnum++)
	{
208
		dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
209 210 211 212 213 214 215 216 217
		snprintf(dbinfos[tupnum].db_name, sizeof(dbinfos[tupnum].db_name), "%s",
				 PQgetvalue(res, tupnum, i_datname));
		snprintf(dbinfos[tupnum].db_tblspace, sizeof(dbinfos[tupnum].db_tblspace), "%s",
				 PQgetvalue(res, tupnum, i_spclocation));
	}
	PQclear(res);

	PQfinish(conn);

218 219
	cluster->dbarr.dbs = dbinfos;
	cluster->dbarr.ndbs = ntups;
220 221 222 223 224 225 226 227 228 229 230 231 232
}


/*
 * get_rel_infos()
 *
 * gets the relinfos for all the user tables of the database refered
 * by "db".
 *
 * NOTE: we assume that relations/entities with oids greater than
 * FirstNormalObjectId belongs to the user
 */
static void
233
get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
234
{
235
	PGconn	   *conn = connectToServer(cluster,
236
									   dbinfo->db_name);
237 238 239 240 241 242 243
	PGresult   *res;
	RelInfo    *relinfos;
	int			ntups;
	int			relnum;
	int			num_rels = 0;
	char	   *nspname = NULL;
	char	   *relname = NULL;
244
	int			i_spclocation, i_nspname, i_relname, i_oid, i_relfilenode;
245 246 247
	char		query[QUERY_ALLOC];

	/*
248
	 * pg_largeobject contains user data that does not appear in pg_dumpall
249
	 * --schema-only output, so we have to copy that system table heap and
250 251 252
	 * index.  We could grab the pg_largeobject oids from template1, but
	 * it is easy to treat it as a normal table.
	 * Order by oid so we can join old/new structures efficiently.
253 254 255
	 */

	snprintf(query, sizeof(query),
256 257
			 "SELECT c.oid, n.nspname, c.relname, "
			 "	c.relfilenode, t.spclocation "
258 259 260 261 262 263 264 265 266
			 "FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
			 "	   ON c.relnamespace = n.oid "
			 "  LEFT OUTER JOIN pg_catalog.pg_tablespace t "
			 "	   ON c.reltablespace = t.oid "
			 "WHERE relkind IN ('r','t', 'i'%s) AND "
			 "  ((n.nspname NOT IN ('pg_catalog', 'information_schema', 'binary_upgrade') AND "
			 "	  c.oid >= %u) "
			 "  OR (n.nspname = 'pg_catalog' AND "
			 "    relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) )) "
267 268
	/* we preserve pg_class.oid so we sort by it to match old/new */
			 "ORDER BY 1;",
269 270 271 272
	/* see the comment at the top of old_8_3_create_sequence_script() */
			 (GET_MAJOR_VERSION(old_cluster.major_version) <= 803) ?
			 "" : ", 'S'",
	/* this oid allows us to skip system toast tables */
273
			 FirstNormalObjectId,
274 275
	/* does pg_largeobject_metadata need to be migrated? */
			 (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ?
276
			 "" : ", 'pg_largeobject_metadata', 'pg_largeobject_metadata_oid_index'");
277

278
	res = executeQueryOrDie(conn, query);
279 280 281

	ntups = PQntuples(res);

282
	relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
283 284 285 286 287 288 289 290 291 292

	i_oid = PQfnumber(res, "oid");
	i_nspname = PQfnumber(res, "nspname");
	i_relname = PQfnumber(res, "relname");
	i_relfilenode = PQfnumber(res, "relfilenode");
	i_spclocation = PQfnumber(res, "spclocation");

	for (relnum = 0; relnum < ntups; relnum++)
	{
		RelInfo    *curr = &relinfos[num_rels++];
293
		const char *tblspace;
294

295
		curr->reloid = atooid(PQgetvalue(res, relnum, i_oid));
296 297

		nspname = PQgetvalue(res, relnum, i_nspname);
298
		strlcpy(curr->nspname, nspname, sizeof(curr->nspname));
299 300

		relname = PQgetvalue(res, relnum, i_relname);
301
		strlcpy(curr->relname, relname, sizeof(curr->relname));
302

303
		curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
304 305 306 307

		tblspace = PQgetvalue(res, relnum, i_spclocation);
		/* if no table tablespace, use the database tablespace */
		if (strlen(tblspace) == 0)
308
			tblspace = dbinfo->db_tblspace;
309
		strlcpy(curr->tablespace, tblspace, sizeof(curr->tablespace));
310 311 312 313 314
	}
	PQclear(res);

	PQfinish(conn);

315 316
	dbinfo->rel_arr.rels = relinfos;
	dbinfo->rel_arr.nrels = num_rels;
317 318 319 320
}


void
321
free_db_and_rel_infos(DbInfoArr *db_arr)
322 323 324 325
{
	int			dbnum;

	for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
326 327
		free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
	pg_free(db_arr->dbs);
328
	db_arr->dbs = NULL;
329 330 331 332 333
	db_arr->ndbs = 0;
}


static void
334
free_rel_infos(RelInfoArr *rel_arr)
335
{
336 337 338
	pg_free(rel_arr->rels);
	rel_arr->nrels = 0;
}
339 340


341 342 343 344 345 346
static void
print_db_infos(DbInfoArr *db_arr)
{
	int			dbnum;

	for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
347
	{
348 349
		pg_log(PG_DEBUG, "Database: %s\n", db_arr->dbs[dbnum].db_name);
		print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
350
		pg_log(PG_DEBUG, "\n\n");
351 352 353 354 355
	}
}


static void
356
print_rel_infos(RelInfoArr *arr)
357 358 359 360
{
	int			relnum;

	for (relnum = 0; relnum < arr->nrels; relnum++)
361
		pg_log(PG_DEBUG, "relname: %s.%s: reloid: %u reltblspace: %s\n",
362 363 364
			   arr->rels[relnum].nspname, arr->rels[relnum].relname,
			   arr->rels[relnum].reloid, arr->rels[relnum].tablespace);
}