4 * information support functions
6 * Copyright (c) 2010-2012, PostgreSQL Global Development Group
7 * contrib/pg_upgrade/info.c
12 #include "pg_upgrade.h"
14 #include "access/transam.h"
17 static void create_rel_filename_map(const char *old_data, const char *new_data,
18 const DbInfo *old_db, const DbInfo *new_db,
19 const RelInfo *old_rel, const RelInfo *new_rel,
21 static void get_db_infos(ClusterInfo *cluster);
22 static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo);
23 static void free_rel_infos(RelInfoArr *rel_arr);
24 static void print_db_infos(DbInfoArr *dbinfo);
25 static void print_rel_infos(RelInfoArr *arr);
31 * generates database mappings for "old_db" and "new_db". Returns a malloc'ed
32 * array of mappings. nmaps is a return parameter which refers to the number
36 gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
37 int *nmaps, const char *old_pgdata, const char *new_pgdata)
43 if (old_db->rel_arr.nrels != new_db->rel_arr.nrels)
44 pg_log(PG_FATAL, "old and new databases \"%s\" have a different number of relations\n",
47 maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
48 old_db->rel_arr.nrels);
50 for (relnum = 0; relnum < old_db->rel_arr.nrels; relnum++)
52 RelInfo *old_rel = &old_db->rel_arr.rels[relnum];
53 RelInfo *new_rel = &new_db->rel_arr.rels[relnum];
55 if (old_rel->reloid != new_rel->reloid)
56 pg_log(PG_FATAL, "Mismatch of relation OID in database \"%s\": old OID %d, new OID %d\n",
57 old_db->db_name, old_rel->reloid, new_rel->reloid);
60 * TOAST table names initially match the heap pg_class oid.
61 * In pre-8.4, TOAST table names change during CLUSTER; in pre-9.0,
62 * TOAST table names change during ALTER TABLE ALTER COLUMN SET TYPE.
63 * In >= 9.0, TOAST relation names always use heap table oids, hence
64 * we cannot check relation names when upgrading from pre-9.0.
65 * Clusters upgraded to 9.0 will get matching TOAST names.
67 if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
68 ((GET_MAJOR_VERSION(old_cluster.major_version) >= 900 ||
69 strcmp(old_rel->nspname, "pg_toast") != 0) &&
70 strcmp(old_rel->relname, new_rel->relname) != 0))
71 pg_log(PG_FATAL, "Mismatch of relation names in database \"%s\": "
72 "old name \"%s.%s\", new name \"%s.%s\"\n",
73 old_db->db_name, old_rel->nspname, old_rel->relname,
74 new_rel->nspname, new_rel->relname);
76 create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
77 old_rel, new_rel, maps + num_maps);
87 * create_rel_filename_map()
89 * fills a file node map structure and returns it in "map".
92 create_rel_filename_map(const char *old_data, const char *new_data,
93 const DbInfo *old_db, const DbInfo *new_db,
94 const RelInfo *old_rel, const RelInfo *new_rel,
97 if (strlen(old_rel->tablespace) == 0)
100 * relation belongs to the default tablespace, hence relfiles should
101 * exist in the data directories.
103 snprintf(map->old_dir, sizeof(map->old_dir), "%s/base/%u", old_data,
105 snprintf(map->new_dir, sizeof(map->new_dir), "%s/base/%u", new_data,
110 /* relation belongs to a tablespace, so use the tablespace location */
111 snprintf(map->old_dir, sizeof(map->old_dir), "%s%s/%u", old_rel->tablespace,
112 old_cluster.tablespace_suffix, old_db->db_oid);
113 snprintf(map->new_dir, sizeof(map->new_dir), "%s%s/%u", new_rel->tablespace,
114 new_cluster.tablespace_suffix, new_db->db_oid);
118 * old_relfilenode might differ from pg_class.oid (and hence
119 * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
121 map->old_relfilenode = old_rel->relfilenode;
123 /* new_relfilenode will match old and new pg_class.oid */
124 map->new_relfilenode = new_rel->relfilenode;
126 /* used only for logging and error reporing, old/new are identical */
127 snprintf(map->nspname, sizeof(map->nspname), "%s", old_rel->nspname);
128 snprintf(map->relname, sizeof(map->relname), "%s", old_rel->relname);
133 print_maps(FileNameMap *maps, int n_maps, const char *db_name)
135 if (log_opts.verbose)
139 pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
141 for (mapnum = 0; mapnum < n_maps; mapnum++)
142 pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
143 maps[mapnum].nspname, maps[mapnum].relname,
144 maps[mapnum].old_relfilenode,
145 maps[mapnum].new_relfilenode);
147 pg_log(PG_VERBOSE, "\n\n");
153 * get_db_and_rel_infos()
155 * higher level routine to generate dbinfos for the database running
156 * on the given "port". Assumes that server is already running.
159 get_db_and_rel_infos(ClusterInfo *cluster)
163 if (cluster->dbarr.dbs != NULL)
164 free_db_and_rel_infos(&cluster->dbarr);
166 get_db_infos(cluster);
168 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
169 get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
171 pg_log(PG_VERBOSE, "\n%s databases:\n", CLUSTER_NAME(cluster));
172 if (log_opts.verbose)
173 print_db_infos(&cluster->dbarr);
180 * Scans pg_database system catalog and populates all user
184 get_db_infos(ClusterInfo *cluster)
186 PGconn *conn = connectToServer(cluster, "template1");
194 char query[QUERY_ALLOC];
196 snprintf(query, sizeof(query),
197 "SELECT d.oid, d.datname, %s "
198 "FROM pg_catalog.pg_database d "
199 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
200 " ON d.dattablespace = t.oid "
201 "WHERE d.datallowconn = true "
202 /* we don't preserve pg_database.oid so we sort by name */
204 /* 9.2 removed the spclocation column */
205 (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
206 "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation");
208 res = executeQueryOrDie(conn, "%s", query);
210 i_oid = PQfnumber(res, "oid");
211 i_datname = PQfnumber(res, "datname");
212 i_spclocation = PQfnumber(res, "spclocation");
214 ntups = PQntuples(res);
215 dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
217 for (tupnum = 0; tupnum < ntups; tupnum++)
219 dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
220 snprintf(dbinfos[tupnum].db_name, sizeof(dbinfos[tupnum].db_name), "%s",
221 PQgetvalue(res, tupnum, i_datname));
222 snprintf(dbinfos[tupnum].db_tblspace, sizeof(dbinfos[tupnum].db_tblspace), "%s",
223 PQgetvalue(res, tupnum, i_spclocation));
229 cluster->dbarr.dbs = dbinfos;
230 cluster->dbarr.ndbs = ntups;
237 * gets the relinfos for all the user tables of the database refered
240 * NOTE: we assume that relations/entities with oids greater than
241 * FirstNormalObjectId belongs to the user
244 get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
246 PGconn *conn = connectToServer(cluster,
253 char *nspname = NULL;
254 char *relname = NULL;
260 char query[QUERY_ALLOC];
263 * pg_largeobject contains user data that does not appear in pg_dumpall
264 * --schema-only output, so we have to copy that system table heap and
265 * index. We could grab the pg_largeobject oids from template1, but it is
266 * easy to treat it as a normal table. Order by oid so we can join old/new
267 * structures efficiently.
270 snprintf(query, sizeof(query),
271 "SELECT c.oid, n.nspname, c.relname, "
272 " c.relfilenode, %s "
273 "FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
274 " ON c.relnamespace = n.oid "
275 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
276 " ON c.reltablespace = t.oid "
277 "WHERE relkind IN ('r','t', 'i'%s) AND "
278 /* exclude possible orphaned temp tables */
279 " ((n.nspname !~ '^pg_temp_' AND "
280 " n.nspname !~ '^pg_toast_temp_' AND "
281 " n.nspname NOT IN ('pg_catalog', 'information_schema', 'binary_upgrade') AND "
283 " OR (n.nspname = 'pg_catalog' AND "
284 " relname IN ('pg_largeobject', 'pg_largeobject_loid_pn_index'%s) )) "
285 /* we preserve pg_class.oid so we sort by it to match old/new */
287 /* 9.2 removed the spclocation column */
288 (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
289 "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation",
290 /* see the comment at the top of old_8_3_create_sequence_script() */
291 (GET_MAJOR_VERSION(old_cluster.major_version) <= 803) ?
293 /* this oid allows us to skip system toast tables */
295 /* does pg_largeobject_metadata need to be migrated? */
296 (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) ?
297 "" : ", 'pg_largeobject_metadata', 'pg_largeobject_metadata_oid_index'");
299 res = executeQueryOrDie(conn, "%s", query);
301 ntups = PQntuples(res);
303 relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
305 i_oid = PQfnumber(res, "oid");
306 i_nspname = PQfnumber(res, "nspname");
307 i_relname = PQfnumber(res, "relname");
308 i_relfilenode = PQfnumber(res, "relfilenode");
309 i_spclocation = PQfnumber(res, "spclocation");
311 for (relnum = 0; relnum < ntups; relnum++)
313 RelInfo *curr = &relinfos[num_rels++];
314 const char *tblspace;
316 curr->reloid = atooid(PQgetvalue(res, relnum, i_oid));
318 nspname = PQgetvalue(res, relnum, i_nspname);
319 strlcpy(curr->nspname, nspname, sizeof(curr->nspname));
321 relname = PQgetvalue(res, relnum, i_relname);
322 strlcpy(curr->relname, relname, sizeof(curr->relname));
324 curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
326 tblspace = PQgetvalue(res, relnum, i_spclocation);
327 /* if no table tablespace, use the database tablespace */
328 if (strlen(tblspace) == 0)
329 tblspace = dbinfo->db_tblspace;
330 strlcpy(curr->tablespace, tblspace, sizeof(curr->tablespace));
336 dbinfo->rel_arr.rels = relinfos;
337 dbinfo->rel_arr.nrels = num_rels;
342 free_db_and_rel_infos(DbInfoArr *db_arr)
346 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
347 free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
348 pg_free(db_arr->dbs);
355 free_rel_infos(RelInfoArr *rel_arr)
357 pg_free(rel_arr->rels);
363 print_db_infos(DbInfoArr *db_arr)
367 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
369 pg_log(PG_VERBOSE, "Database: %s\n", db_arr->dbs[dbnum].db_name);
370 print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
371 pg_log(PG_VERBOSE, "\n\n");
377 print_rel_infos(RelInfoArr *arr)
381 for (relnum = 0; relnum < arr->nrels; relnum++)
382 pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
383 arr->rels[relnum].nspname, arr->rels[relnum].relname,
384 arr->rels[relnum].reloid, arr->rels[relnum].tablespace);