4 * information support functions
6 * Copyright (c) 2010, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/info.c
10 #include "pg_upgrade.h"
12 #include "access/transam.h"
15 static void get_db_infos(DbInfoArr *dbinfos,
16 Cluster whichCluster);
17 static void dbarr_print(DbInfoArr *arr,
18 Cluster whichCluster);
19 static void relarr_print(RelInfoArr *arr);
20 static void get_rel_infos(const DbInfo *dbinfo,
21 RelInfoArr *relarr, Cluster whichCluster);
22 static void relarr_free(RelInfoArr *rel_arr);
23 static void map_rel(const RelInfo *oldrel,
24 const RelInfo *newrel, const DbInfo *old_db,
25 const DbInfo *new_db, const char *olddata,
26 const char *newdata, FileNameMap *map);
27 static void map_rel_by_id(Oid oldid, Oid newid,
28 const char *old_nspname, const char *old_relname,
29 const char *new_nspname, const char *new_relname,
30 const char *old_tablespace, const DbInfo *old_db,
31 const DbInfo *new_db, const char *olddata,
32 const char *newdata, FileNameMap *map);
33 static RelInfo *relarr_lookup_reloid(RelInfoArr *rel_arr,
34 Oid oid, Cluster whichCluster);
35 static RelInfo *relarr_lookup_rel(RelInfoArr *rel_arr,
36 const char *nspname, const char *relname,
37 Cluster whichCluster);
43 * generates database mappings for "old_db" and "new_db". Returns a malloc'ed
44 * array of mappings. nmaps is a return parameter which refers to the number
47 * NOTE: Its the Caller's responsibility to free the returned array.
50 gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
51 int *nmaps, const char *old_pgdata, const char *new_pgdata)
57 maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
58 new_db->rel_arr.nrels);
60 for (relnum = 0; relnum < new_db->rel_arr.nrels; relnum++)
62 RelInfo *newrel = &new_db->rel_arr.rels[relnum];
65 /* toast tables are handled by their parent */
66 if (strcmp(newrel->nspname, "pg_toast") == 0)
69 oldrel = relarr_lookup_rel(&old_db->rel_arr, newrel->nspname,
70 newrel->relname, CLUSTER_OLD);
72 map_rel(oldrel, newrel, old_db, new_db, old_pgdata, new_pgdata,
77 * So much for mapping this relation; now we need a mapping
78 * for its corresponding toast relation, if any.
80 if (oldrel->toastrelid > 0)
84 char new_name[MAXPGPATH];
85 char old_name[MAXPGPATH];
87 /* construct the new and old relnames for the toast relation */
88 snprintf(old_name, sizeof(old_name), "pg_toast_%u",
90 snprintf(new_name, sizeof(new_name), "pg_toast_%u",
93 /* look them up in their respective arrays */
94 old_toast = relarr_lookup_reloid(&old_db->rel_arr,
95 oldrel->toastrelid, CLUSTER_OLD);
96 new_toast = relarr_lookup_rel(&new_db->rel_arr,
97 "pg_toast", new_name, CLUSTER_NEW);
99 /* finally create a mapping for them */
100 map_rel(old_toast, new_toast, old_db, new_db, old_pgdata, new_pgdata,
105 * also need to provide a mapping for the index of this toast
106 * relation. The procedure is similar to what we did above for
107 * toast relation itself, the only difference being that the
108 * relnames need to be appended with _index.
112 * construct the new and old relnames for the toast index
115 snprintf(old_name, sizeof(old_name), "%s_index", old_toast->relname);
116 snprintf(new_name, sizeof(new_name), "pg_toast_%u_index",
119 /* look them up in their respective arrays */
120 /* we lose our cache location here */
121 old_toast = relarr_lookup_rel(&old_db->rel_arr,
122 "pg_toast", old_name, CLUSTER_OLD);
123 new_toast = relarr_lookup_rel(&new_db->rel_arr,
124 "pg_toast", new_name, CLUSTER_NEW);
126 /* finally create a mapping for them */
127 map_rel(old_toast, new_toast, old_db, new_db, old_pgdata,
128 new_pgdata, maps + num_maps);
139 map_rel(const RelInfo *oldrel, const RelInfo *newrel,
140 const DbInfo *old_db, const DbInfo *new_db, const char *olddata,
141 const char *newdata, FileNameMap *map)
143 map_rel_by_id(oldrel->relfilenode, newrel->relfilenode, oldrel->nspname,
144 oldrel->relname, newrel->nspname, newrel->relname, oldrel->tablespace, old_db,
145 new_db, olddata, newdata, map);
152 * fills a file node map structure and returns it in "map".
155 map_rel_by_id(Oid oldid, Oid newid,
156 const char *old_nspname, const char *old_relname,
157 const char *new_nspname, const char *new_relname,
158 const char *old_tablespace, const DbInfo *old_db,
159 const DbInfo *new_db, const char *olddata,
160 const char *newdata, FileNameMap *map)
162 map->old_relfilenode = oldid;
163 map->new_relfilenode = newid;
165 snprintf(map->old_nspname, sizeof(map->old_nspname), "%s", old_nspname);
166 snprintf(map->old_relname, sizeof(map->old_relname), "%s", old_relname);
167 snprintf(map->new_nspname, sizeof(map->new_nspname), "%s", new_nspname);
168 snprintf(map->new_relname, sizeof(map->new_relname), "%s", new_relname);
170 if (strlen(old_tablespace) == 0)
173 * relation belongs to the default tablespace, hence relfiles would
174 * exist in the data directories.
176 snprintf(map->old_dir, sizeof(map->old_dir), "%s/base/%u", olddata, old_db->db_oid);
177 snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", newdata, new_db->db_oid);
182 * relation belongs to some tablespace, hence copy its physical
185 snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_tablespace,
186 old_cluster.tablespace_suffix, old_db->db_oid);
187 snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", old_tablespace,
188 new_cluster.tablespace_suffix, new_db->db_oid);
194 print_maps(FileNameMap *maps, int n, const char *dbName)
200 pg_log(PG_DEBUG, "mappings for db %s:\n", dbName);
202 for (mapnum = 0; mapnum < n; mapnum++)
203 pg_log(PG_DEBUG, "%s.%s:%u ==> %s.%s:%u\n",
204 maps[mapnum].old_nspname, maps[mapnum].old_relname,
205 maps[mapnum].old_relfilenode,
206 maps[mapnum].new_nspname, maps[mapnum].new_relname,
207 maps[mapnum].new_relfilenode);
209 pg_log(PG_DEBUG, "\n\n");
217 * Scans pg_database system catalog and returns (in dbinfs_arr) all user
221 get_db_infos(DbInfoArr *dbinfs_arr, Cluster whichCluster)
223 PGconn *conn = connectToServer("template1", whichCluster);
232 res = executeQueryOrDie(conn,
233 "SELECT d.oid, d.datname, t.spclocation "
234 "FROM pg_catalog.pg_database d "
235 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
236 " ON d.dattablespace = t.oid "
237 "WHERE d.datallowconn = true");
239 i_datname = PQfnumber(res, "datname");
240 i_oid = PQfnumber(res, "oid");
241 i_spclocation = PQfnumber(res, "spclocation");
243 ntups = PQntuples(res);
244 dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
246 for (tupnum = 0; tupnum < ntups; tupnum++)
248 dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
250 snprintf(dbinfos[tupnum].db_name, sizeof(dbinfos[tupnum].db_name), "%s",
251 PQgetvalue(res, tupnum, i_datname));
252 snprintf(dbinfos[tupnum].db_tblspace, sizeof(dbinfos[tupnum].db_tblspace), "%s",
253 PQgetvalue(res, tupnum, i_spclocation));
259 dbinfs_arr->dbs = dbinfos;
260 dbinfs_arr->ndbs = ntups;
265 * get_db_and_rel_infos()
267 * higher level routine to generate dbinfos for the database running
268 * on the given "port". Assumes that server is already running.
271 get_db_and_rel_infos(DbInfoArr *db_arr, Cluster whichCluster)
275 get_db_infos(db_arr, whichCluster);
277 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
278 get_rel_infos(&db_arr->dbs[dbnum],
279 &db_arr->dbs[dbnum].rel_arr, whichCluster);
282 dbarr_print(db_arr, whichCluster);
289 * gets the relinfos for all the user tables of the database refered
292 * NOTE: we assume that relations/entities with oids greater than
293 * FirstNormalObjectId belongs to the user
296 get_rel_infos(const DbInfo *dbinfo, RelInfoArr *relarr, Cluster whichCluster)
298 PGconn *conn = connectToServer(dbinfo->db_name, whichCluster);
304 char *nspname = NULL;
305 char *relname = NULL;
306 int i_spclocation = -1;
310 int i_relfilenode = -1;
311 int i_reltoastrelid = -1;
312 char query[QUERY_ALLOC];
315 * pg_largeobject contains user data that does not appear the pg_dumpall
316 * --schema-only output, so we have to upgrade that system table heap and
317 * index. Ideally we could just get the relfilenode from template1 but
318 * pg_largeobject_loid_pn_index's relfilenode can change if the table was
319 * reindexed so we get the relfilenode for each database and upgrade it as
320 * a normal user table.
321 * Order by tablespace so we can cache the directory contents efficiently.
324 snprintf(query, sizeof(query),
325 "SELECT DISTINCT c.oid, n.nspname, c.relname, "
326 " c.relfilenode, c.reltoastrelid, t.spclocation "
327 "FROM pg_catalog.pg_class c JOIN "
328 " pg_catalog.pg_namespace n "
329 " ON c.relnamespace = n.oid "
330 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
331 " ON c.reltablespace = t.oid "
332 "WHERE (( n.nspname NOT IN ('pg_catalog', 'information_schema') "
335 " n.nspname = 'pg_catalog' "
337 " ('pg_largeobject', 'pg_largeobject_loid_pn_index') )) "
338 " AND relkind IN ('r','t', 'i'%s)"
339 "GROUP BY c.oid, n.nspname, c.relname, c.relfilenode,"
340 " c.reltoastrelid, t.spclocation, "
342 "ORDER BY t.spclocation, n.nspname, c.relname;",
344 /* see the comment at the top of old_8_3_create_sequence_script() */
345 (GET_MAJOR_VERSION(old_cluster.major_version) <= 803) ?
348 res = executeQueryOrDie(conn, query);
350 ntups = PQntuples(res);
352 relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
354 i_oid = PQfnumber(res, "oid");
355 i_nspname = PQfnumber(res, "nspname");
356 i_relname = PQfnumber(res, "relname");
357 i_relfilenode = PQfnumber(res, "relfilenode");
358 i_reltoastrelid = PQfnumber(res, "reltoastrelid");
359 i_spclocation = PQfnumber(res, "spclocation");
361 for (relnum = 0; relnum < ntups; relnum++)
363 RelInfo *curr = &relinfos[num_rels++];
364 const char *tblspace;
366 curr->reloid = atooid(PQgetvalue(res, relnum, i_oid));
368 nspname = PQgetvalue(res, relnum, i_nspname);
369 strlcpy(curr->nspname, nspname, sizeof(curr->nspname));
371 relname = PQgetvalue(res, relnum, i_relname);
372 strlcpy(curr->relname, relname, sizeof(curr->relname));
374 curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
375 curr->toastrelid = atooid(PQgetvalue(res, relnum, i_reltoastrelid));
377 tblspace = PQgetvalue(res, relnum, i_spclocation);
378 /* if no table tablespace, use the database tablespace */
379 if (strlen(tblspace) == 0)
380 tblspace = dbinfo->db_tblspace;
381 strlcpy(curr->tablespace, tblspace, sizeof(curr->tablespace));
387 relarr->rels = relinfos;
388 relarr->nrels = num_rels;
389 relarr->last_relname_lookup = 0;
396 * Returns the pointer to the DbInfo structure
399 dbarr_lookup_db(DbInfoArr *db_arr, const char *db_name)
403 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
405 if (strcmp(db_arr->dbs[dbnum].db_name, db_name) == 0)
406 return &db_arr->dbs[dbnum];
414 * relarr_lookup_rel()
416 * Searches "relname" in rel_arr. Returns the *real* pointer to the
420 relarr_lookup_rel(RelInfoArr *rel_arr, const char *nspname,
421 const char *relname, Cluster whichCluster)
425 /* Test next lookup first, for speed */
426 if (rel_arr->last_relname_lookup + 1 < rel_arr->nrels &&
427 strcmp(rel_arr->rels[rel_arr->last_relname_lookup + 1].nspname, nspname) == 0 &&
428 strcmp(rel_arr->rels[rel_arr->last_relname_lookup + 1].relname, relname) == 0)
430 rel_arr->last_relname_lookup++;
431 return &rel_arr->rels[rel_arr->last_relname_lookup];
434 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
436 if (strcmp(rel_arr->rels[relnum].nspname, nspname) == 0 &&
437 strcmp(rel_arr->rels[relnum].relname, relname) == 0)
439 rel_arr->last_relname_lookup = relnum;
440 return &rel_arr->rels[relnum];
443 pg_log(PG_FATAL, "Could not find %s.%s in %s cluster\n",
444 nspname, relname, CLUSTERNAME(whichCluster));
450 * relarr_lookup_reloid()
452 * Returns a pointer to the RelInfo structure for the
453 * given oid or NULL if the desired entry cannot be
457 relarr_lookup_reloid(RelInfoArr *rel_arr, Oid oid,
458 Cluster whichCluster)
462 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
464 if (rel_arr->rels[relnum].reloid == oid)
465 return &rel_arr->rels[relnum];
467 pg_log(PG_FATAL, "Could not find %d in %s cluster\n",
468 oid, CLUSTERNAME(whichCluster));
474 relarr_free(RelInfoArr *rel_arr)
476 pg_free(rel_arr->rels);
478 rel_arr->last_relname_lookup = 0;
483 dbarr_free(DbInfoArr *db_arr)
487 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
488 relarr_free(&db_arr->dbs[dbnum].rel_arr);
494 dbarr_print(DbInfoArr *arr, Cluster whichCluster)
498 pg_log(PG_DEBUG, "%s databases\n", CLUSTERNAME(whichCluster));
500 for (dbnum = 0; dbnum < arr->ndbs; dbnum++)
502 pg_log(PG_DEBUG, "Database: %s\n", arr->dbs[dbnum].db_name);
503 relarr_print(&arr->dbs[dbnum].rel_arr);
504 pg_log(PG_DEBUG, "\n\n");
510 relarr_print(RelInfoArr *arr)
514 for (relnum = 0; relnum < arr->nrels; relnum++)
515 pg_log(PG_DEBUG, "relname: %s.%s: reloid: %u reltblspace: %s\n",
516 arr->rels[relnum].nspname, arr->rels[relnum].relname,
517 arr->rels[relnum].reloid, arr->rels[relnum].tablespace);