Consistently use "superuser" instead of "super user"
[pgsql.git] / src / bin / pg_upgrade / info.c
blob5d9a26cf8229048a77af685c82fd8a4c07148c85
1 /*
2 * info.c
4 * information support functions
6 * Copyright (c) 2010-2021, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/info.c
8 */
10 #include "postgres_fe.h"
12 #include "access/transam.h"
13 #include "catalog/pg_class_d.h"
14 #include "pg_upgrade.h"
16 static void create_rel_filename_map(const char *old_data, const char *new_data,
17 const DbInfo *old_db, const DbInfo *new_db,
18 const RelInfo *old_rel, const RelInfo *new_rel,
19 FileNameMap *map);
20 static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db,
21 bool is_new_db);
22 static void free_db_and_rel_infos(DbInfoArr *db_arr);
23 static void get_db_infos(ClusterInfo *cluster);
24 static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo);
25 static void free_rel_infos(RelInfoArr *rel_arr);
26 static void print_db_infos(DbInfoArr *dbinfo);
27 static void print_rel_infos(RelInfoArr *rel_arr);
31 * gen_db_file_maps()
33 * generates a database mapping from "old_db" to "new_db".
35 * Returns a malloc'ed array of mappings. The length of the array
36 * is returned into *nmaps.
38 FileNameMap *
39 gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
40 int *nmaps,
41 const char *old_pgdata, const char *new_pgdata)
43 FileNameMap *maps;
44 int old_relnum,
45 new_relnum;
46 int num_maps = 0;
47 bool all_matched = true;
49 /* There will certainly not be more mappings than there are old rels */
50 maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
51 old_db->rel_arr.nrels);
54 * Each of the RelInfo arrays should be sorted by OID. Scan through them
55 * and match them up. If we fail to match everything, we'll abort, but
56 * first print as much info as we can about mismatches.
58 old_relnum = new_relnum = 0;
59 while (old_relnum < old_db->rel_arr.nrels ||
60 new_relnum < new_db->rel_arr.nrels)
62 RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
63 &old_db->rel_arr.rels[old_relnum] : NULL;
64 RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
65 &new_db->rel_arr.rels[new_relnum] : NULL;
67 /* handle running off one array before the other */
68 if (!new_rel)
71 * old_rel is unmatched. This should never happen, because we
72 * force new rels to have TOAST tables if the old one did.
74 report_unmatched_relation(old_rel, old_db, false);
75 all_matched = false;
76 old_relnum++;
77 continue;
79 if (!old_rel)
82 * new_rel is unmatched. This shouldn't really happen either, but
83 * if it's a TOAST table, we can ignore it and continue
84 * processing, assuming that the new server made a TOAST table
85 * that wasn't needed.
87 if (strcmp(new_rel->nspname, "pg_toast") != 0)
89 report_unmatched_relation(new_rel, new_db, true);
90 all_matched = false;
92 new_relnum++;
93 continue;
96 /* check for mismatched OID */
97 if (old_rel->reloid < new_rel->reloid)
99 /* old_rel is unmatched, see comment above */
100 report_unmatched_relation(old_rel, old_db, false);
101 all_matched = false;
102 old_relnum++;
103 continue;
105 else if (old_rel->reloid > new_rel->reloid)
107 /* new_rel is unmatched, see comment above */
108 if (strcmp(new_rel->nspname, "pg_toast") != 0)
110 report_unmatched_relation(new_rel, new_db, true);
111 all_matched = false;
113 new_relnum++;
114 continue;
118 * Verify that rels of same OID have same name. The namespace name
119 * should always match, but the relname might not match for TOAST
120 * tables (and, therefore, their indexes).
122 * TOAST table names initially match the heap pg_class oid, but
123 * pre-9.0 they can change during certain commands such as CLUSTER, so
124 * don't insist on a match if old cluster is < 9.0.
126 if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
127 (strcmp(old_rel->relname, new_rel->relname) != 0 &&
128 (GET_MAJOR_VERSION(old_cluster.major_version) >= 900 ||
129 strcmp(old_rel->nspname, "pg_toast") != 0)))
131 pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: "
132 "old name \"%s.%s\", new name \"%s.%s\"\n",
133 old_rel->reloid, old_db->db_name,
134 old_rel->nspname, old_rel->relname,
135 new_rel->nspname, new_rel->relname);
136 all_matched = false;
137 old_relnum++;
138 new_relnum++;
139 continue;
142 /* OK, create a mapping entry */
143 create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
144 old_rel, new_rel, maps + num_maps);
145 num_maps++;
146 old_relnum++;
147 new_relnum++;
150 if (!all_matched)
151 pg_fatal("Failed to match up old and new tables in database \"%s\"\n",
152 old_db->db_name);
154 *nmaps = num_maps;
155 return maps;
160 * create_rel_filename_map()
162 * fills a file node map structure and returns it in "map".
164 static void
165 create_rel_filename_map(const char *old_data, const char *new_data,
166 const DbInfo *old_db, const DbInfo *new_db,
167 const RelInfo *old_rel, const RelInfo *new_rel,
168 FileNameMap *map)
170 /* In case old/new tablespaces don't match, do them separately. */
171 if (strlen(old_rel->tablespace) == 0)
174 * relation belongs to the default tablespace, hence relfiles should
175 * exist in the data directories.
177 map->old_tablespace = old_data;
178 map->old_tablespace_suffix = "/base";
180 else
182 /* relation belongs to a tablespace, so use the tablespace location */
183 map->old_tablespace = old_rel->tablespace;
184 map->old_tablespace_suffix = old_cluster.tablespace_suffix;
187 /* Do the same for new tablespaces */
188 if (strlen(new_rel->tablespace) == 0)
190 map->new_tablespace = new_data;
191 map->new_tablespace_suffix = "/base";
193 else
195 map->new_tablespace = new_rel->tablespace;
196 map->new_tablespace_suffix = new_cluster.tablespace_suffix;
199 map->old_db_oid = old_db->db_oid;
200 map->new_db_oid = new_db->db_oid;
203 * old_relfilenode might differ from pg_class.oid (and hence
204 * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
206 map->old_relfilenode = old_rel->relfilenode;
208 /* new_relfilenode will match old and new pg_class.oid */
209 map->new_relfilenode = new_rel->relfilenode;
211 /* used only for logging and error reporting, old/new are identical */
212 map->nspname = old_rel->nspname;
213 map->relname = old_rel->relname;
218 * Complain about a relation we couldn't match to the other database,
219 * identifying it as best we can.
221 static void
222 report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
224 Oid reloid = rel->reloid; /* we might change rel below */
225 char reldesc[1000];
226 int i;
228 snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
229 rel->nspname, rel->relname);
230 if (rel->indtable)
232 for (i = 0; i < db->rel_arr.nrels; i++)
234 const RelInfo *hrel = &db->rel_arr.rels[i];
236 if (hrel->reloid == rel->indtable)
238 snprintf(reldesc + strlen(reldesc),
239 sizeof(reldesc) - strlen(reldesc),
240 _(" which is an index on \"%s.%s\""),
241 hrel->nspname, hrel->relname);
242 /* Shift attention to index's table for toast check */
243 rel = hrel;
244 break;
247 if (i >= db->rel_arr.nrels)
248 snprintf(reldesc + strlen(reldesc),
249 sizeof(reldesc) - strlen(reldesc),
250 _(" which is an index on OID %u"), rel->indtable);
252 if (rel->toastheap)
254 for (i = 0; i < db->rel_arr.nrels; i++)
256 const RelInfo *brel = &db->rel_arr.rels[i];
258 if (brel->reloid == rel->toastheap)
260 snprintf(reldesc + strlen(reldesc),
261 sizeof(reldesc) - strlen(reldesc),
262 _(" which is the TOAST table for \"%s.%s\""),
263 brel->nspname, brel->relname);
264 break;
267 if (i >= db->rel_arr.nrels)
268 snprintf(reldesc + strlen(reldesc),
269 sizeof(reldesc) - strlen(reldesc),
270 _(" which is the TOAST table for OID %u"), rel->toastheap);
273 if (is_new_db)
274 pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s\n",
275 reloid, db->db_name, reldesc);
276 else
277 pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s\n",
278 reloid, db->db_name, reldesc);
282 void
283 print_maps(FileNameMap *maps, int n_maps, const char *db_name)
285 if (log_opts.verbose)
287 int mapnum;
289 pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
291 for (mapnum = 0; mapnum < n_maps; mapnum++)
292 pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
293 maps[mapnum].nspname, maps[mapnum].relname,
294 maps[mapnum].old_relfilenode,
295 maps[mapnum].new_relfilenode);
297 pg_log(PG_VERBOSE, "\n\n");
303 * get_db_and_rel_infos()
305 * higher level routine to generate dbinfos for the database running
306 * on the given "port". Assumes that server is already running.
308 void
309 get_db_and_rel_infos(ClusterInfo *cluster)
311 int dbnum;
313 if (cluster->dbarr.dbs != NULL)
314 free_db_and_rel_infos(&cluster->dbarr);
316 get_db_infos(cluster);
318 for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
319 get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
321 if (cluster == &old_cluster)
322 pg_log(PG_VERBOSE, "\nsource databases:\n");
323 else
324 pg_log(PG_VERBOSE, "\ntarget databases:\n");
326 if (log_opts.verbose)
327 print_db_infos(&cluster->dbarr);
332 * get_db_infos()
334 * Scans pg_database system catalog and populates all user
335 * databases.
337 static void
338 get_db_infos(ClusterInfo *cluster)
340 PGconn *conn = connectToServer(cluster, "template1");
341 PGresult *res;
342 int ntups;
343 int tupnum;
344 DbInfo *dbinfos;
345 int i_datname,
346 i_oid,
347 i_encoding,
348 i_datcollate,
349 i_datctype,
350 i_spclocation;
351 char query[QUERY_ALLOC];
353 snprintf(query, sizeof(query),
354 "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, "
355 "%s AS spclocation "
356 "FROM pg_catalog.pg_database d "
357 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
358 " ON d.dattablespace = t.oid "
359 "WHERE d.datallowconn = true "
360 /* we don't preserve pg_database.oid so we sort by name */
361 "ORDER BY 2",
362 /* 9.2 removed the spclocation column */
363 (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
364 "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
366 res = executeQueryOrDie(conn, "%s", query);
368 i_oid = PQfnumber(res, "oid");
369 i_datname = PQfnumber(res, "datname");
370 i_encoding = PQfnumber(res, "encoding");
371 i_datcollate = PQfnumber(res, "datcollate");
372 i_datctype = PQfnumber(res, "datctype");
373 i_spclocation = PQfnumber(res, "spclocation");
375 ntups = PQntuples(res);
376 dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
378 for (tupnum = 0; tupnum < ntups; tupnum++)
380 dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
381 dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
382 dbinfos[tupnum].db_encoding = atoi(PQgetvalue(res, tupnum, i_encoding));
383 dbinfos[tupnum].db_collate = pg_strdup(PQgetvalue(res, tupnum, i_datcollate));
384 dbinfos[tupnum].db_ctype = pg_strdup(PQgetvalue(res, tupnum, i_datctype));
385 snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
386 PQgetvalue(res, tupnum, i_spclocation));
388 PQclear(res);
390 PQfinish(conn);
392 cluster->dbarr.dbs = dbinfos;
393 cluster->dbarr.ndbs = ntups;
398 * get_rel_infos()
400 * gets the relinfos for all the user tables and indexes of the database
401 * referred to by "dbinfo".
403 * Note: the resulting RelInfo array is assumed to be sorted by OID.
404 * This allows later processing to match up old and new databases efficiently.
406 static void
407 get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
409 PGconn *conn = connectToServer(cluster,
410 dbinfo->db_name);
411 PGresult *res;
412 RelInfo *relinfos;
413 int ntups;
414 int relnum;
415 int num_rels = 0;
416 char *nspname = NULL;
417 char *relname = NULL;
418 char *tablespace = NULL;
419 int i_spclocation,
420 i_nspname,
421 i_relname,
422 i_reloid,
423 i_indtable,
424 i_toastheap,
425 i_relfilenode,
426 i_reltablespace;
427 char query[QUERY_ALLOC];
428 char *last_namespace = NULL,
429 *last_tablespace = NULL;
431 query[0] = '\0'; /* initialize query string to empty */
434 * Create a CTE that collects OIDs of regular user tables, including
435 * matviews and sequences, but excluding toast tables and indexes. We
436 * assume that relations with OIDs >= FirstNormalObjectId belong to the
437 * user. (That's probably redundant with the namespace-name exclusions,
438 * but let's be safe.)
440 * pg_largeobject contains user data that does not appear in pg_dump
441 * output, so we have to copy that system table. It's easiest to do that
442 * by treating it as a user table.
444 snprintf(query + strlen(query), sizeof(query) - strlen(query),
445 "WITH regular_heap (reloid, indtable, toastheap) AS ( "
446 " SELECT c.oid, 0::oid, 0::oid "
447 " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
448 " ON c.relnamespace = n.oid "
449 " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
450 CppAsString2(RELKIND_MATVIEW) ") AND "
451 /* exclude possible orphaned temp tables */
452 " ((n.nspname !~ '^pg_temp_' AND "
453 " n.nspname !~ '^pg_toast_temp_' AND "
454 " n.nspname NOT IN ('pg_catalog', 'information_schema', "
455 " 'binary_upgrade', 'pg_toast') AND "
456 " c.oid >= %u::pg_catalog.oid) OR "
457 " (n.nspname = 'pg_catalog' AND "
458 " relname IN ('pg_largeobject') ))), ",
459 FirstNormalObjectId);
462 * Add a CTE that collects OIDs of toast tables belonging to the tables
463 * selected by the regular_heap CTE. (We have to do this separately
464 * because the namespace-name rules above don't work for toast tables.)
466 snprintf(query + strlen(query), sizeof(query) - strlen(query),
467 " toast_heap (reloid, indtable, toastheap) AS ( "
468 " SELECT c.reltoastrelid, 0::oid, c.oid "
469 " FROM regular_heap JOIN pg_catalog.pg_class c "
470 " ON regular_heap.reloid = c.oid "
471 " WHERE c.reltoastrelid != 0), ");
474 * Add a CTE that collects OIDs of all valid indexes on the previously
475 * selected tables. We can ignore invalid indexes since pg_dump does.
476 * Testing indisready is necessary in 9.2, and harmless in earlier/later
477 * versions.
479 snprintf(query + strlen(query), sizeof(query) - strlen(query),
480 " all_index (reloid, indtable, toastheap) AS ( "
481 " SELECT indexrelid, indrelid, 0::oid "
482 " FROM pg_catalog.pg_index "
483 " WHERE indisvalid AND indisready "
484 " AND indrelid IN "
485 " (SELECT reloid FROM regular_heap "
486 " UNION ALL "
487 " SELECT reloid FROM toast_heap)) ");
490 * And now we can write the query that retrieves the data we want for each
491 * heap and index relation. Make sure result is sorted by OID.
493 snprintf(query + strlen(query), sizeof(query) - strlen(query),
494 "SELECT all_rels.*, n.nspname, c.relname, "
495 " c.relfilenode, c.reltablespace, %s "
496 "FROM (SELECT * FROM regular_heap "
497 " UNION ALL "
498 " SELECT * FROM toast_heap "
499 " UNION ALL "
500 " SELECT * FROM all_index) all_rels "
501 " JOIN pg_catalog.pg_class c "
502 " ON all_rels.reloid = c.oid "
503 " JOIN pg_catalog.pg_namespace n "
504 " ON c.relnamespace = n.oid "
505 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
506 " ON c.reltablespace = t.oid "
507 "ORDER BY 1;",
508 /* 9.2 removed the pg_tablespace.spclocation column */
509 (GET_MAJOR_VERSION(cluster->major_version) >= 902) ?
510 "pg_catalog.pg_tablespace_location(t.oid) AS spclocation" :
511 "t.spclocation");
513 res = executeQueryOrDie(conn, "%s", query);
515 ntups = PQntuples(res);
517 relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
519 i_reloid = PQfnumber(res, "reloid");
520 i_indtable = PQfnumber(res, "indtable");
521 i_toastheap = PQfnumber(res, "toastheap");
522 i_nspname = PQfnumber(res, "nspname");
523 i_relname = PQfnumber(res, "relname");
524 i_relfilenode = PQfnumber(res, "relfilenode");
525 i_reltablespace = PQfnumber(res, "reltablespace");
526 i_spclocation = PQfnumber(res, "spclocation");
528 for (relnum = 0; relnum < ntups; relnum++)
530 RelInfo *curr = &relinfos[num_rels++];
532 curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
533 curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
534 curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
536 nspname = PQgetvalue(res, relnum, i_nspname);
537 curr->nsp_alloc = false;
540 * Many of the namespace and tablespace strings are identical, so we
541 * try to reuse the allocated string pointers where possible to reduce
542 * memory consumption.
544 /* Can we reuse the previous string allocation? */
545 if (last_namespace && strcmp(nspname, last_namespace) == 0)
546 curr->nspname = last_namespace;
547 else
549 last_namespace = curr->nspname = pg_strdup(nspname);
550 curr->nsp_alloc = true;
553 relname = PQgetvalue(res, relnum, i_relname);
554 curr->relname = pg_strdup(relname);
556 curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
557 curr->tblsp_alloc = false;
559 /* Is the tablespace oid non-default? */
560 if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
563 * The tablespace location might be "", meaning the cluster
564 * default location, i.e. pg_default or pg_global.
566 tablespace = PQgetvalue(res, relnum, i_spclocation);
568 /* Can we reuse the previous string allocation? */
569 if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
570 curr->tablespace = last_tablespace;
571 else
573 last_tablespace = curr->tablespace = pg_strdup(tablespace);
574 curr->tblsp_alloc = true;
577 else
578 /* A zero reltablespace oid indicates the database tablespace. */
579 curr->tablespace = dbinfo->db_tablespace;
581 PQclear(res);
583 PQfinish(conn);
585 dbinfo->rel_arr.rels = relinfos;
586 dbinfo->rel_arr.nrels = num_rels;
590 static void
591 free_db_and_rel_infos(DbInfoArr *db_arr)
593 int dbnum;
595 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
597 free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
598 pg_free(db_arr->dbs[dbnum].db_name);
600 pg_free(db_arr->dbs);
601 db_arr->dbs = NULL;
602 db_arr->ndbs = 0;
606 static void
607 free_rel_infos(RelInfoArr *rel_arr)
609 int relnum;
611 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
613 if (rel_arr->rels[relnum].nsp_alloc)
614 pg_free(rel_arr->rels[relnum].nspname);
615 pg_free(rel_arr->rels[relnum].relname);
616 if (rel_arr->rels[relnum].tblsp_alloc)
617 pg_free(rel_arr->rels[relnum].tablespace);
619 pg_free(rel_arr->rels);
620 rel_arr->nrels = 0;
624 static void
625 print_db_infos(DbInfoArr *db_arr)
627 int dbnum;
629 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
631 pg_log(PG_VERBOSE, "Database: %s\n", db_arr->dbs[dbnum].db_name);
632 print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
633 pg_log(PG_VERBOSE, "\n\n");
638 static void
639 print_rel_infos(RelInfoArr *rel_arr)
641 int relnum;
643 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
644 pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
645 rel_arr->rels[relnum].nspname,
646 rel_arr->rels[relnum].relname,
647 rel_arr->rels[relnum].reloid,
648 rel_arr->rels[relnum].tablespace);