Line data Source code
1 : /*
2 : * info.c
3 : *
4 : * information support functions
5 : *
6 : * Copyright (c) 2010-2025, PostgreSQL Global Development Group
7 : * src/bin/pg_upgrade/info.c
8 : */
9 :
10 : #include "postgres_fe.h"
11 :
12 : #include "access/transam.h"
13 : #include "catalog/pg_class_d.h"
14 : #include "pg_upgrade.h"
15 : #include "pqexpbuffer.h"
16 :
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,
20 : FileNameMap *map);
21 : static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db,
22 : bool is_new_db);
23 : static void free_db_and_rel_infos(DbInfoArr *db_arr);
24 : static void get_template0_info(ClusterInfo *cluster);
25 : static void get_db_infos(ClusterInfo *cluster);
26 : static char *get_rel_infos_query(void);
27 : static void process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg);
28 : static void free_rel_infos(RelInfoArr *rel_arr);
29 : static void print_db_infos(DbInfoArr *db_arr);
30 : static void print_rel_infos(RelInfoArr *rel_arr);
31 : static void print_slot_infos(LogicalSlotInfoArr *slot_arr);
32 : static char *get_old_cluster_logical_slot_infos_query(void);
33 : static void process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg);
34 :
35 :
36 : /*
37 : * gen_db_file_maps()
38 : *
39 : * generates a database mapping from "old_db" to "new_db".
40 : *
41 : * Returns a malloc'ed array of mappings. The length of the array
42 : * is returned into *nmaps.
43 : */
44 : FileNameMap *
45 56 : gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
46 : int *nmaps,
47 : const char *old_pgdata, const char *new_pgdata)
48 : {
49 : FileNameMap *maps;
50 : int old_relnum,
51 : new_relnum;
52 56 : int num_maps = 0;
53 56 : bool all_matched = true;
54 :
55 : /* There will certainly not be more mappings than there are old rels */
56 56 : maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
57 56 : old_db->rel_arr.nrels);
58 :
59 : /*
60 : * Each of the RelInfo arrays should be sorted by OID. Scan through them
61 : * and match them up. If we fail to match everything, we'll abort, but
62 : * first print as much info as we can about mismatches.
63 : */
64 56 : old_relnum = new_relnum = 0;
65 3210 : while (old_relnum < old_db->rel_arr.nrels ||
66 56 : new_relnum < new_db->rel_arr.nrels)
67 : {
68 6308 : RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
69 3154 : &old_db->rel_arr.rels[old_relnum] : NULL;
70 6308 : RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
71 3154 : &new_db->rel_arr.rels[new_relnum] : NULL;
72 :
73 : /* handle running off one array before the other */
74 3154 : if (!new_rel)
75 : {
76 : /*
77 : * old_rel is unmatched. This should never happen, because we
78 : * force new rels to have TOAST tables if the old one did.
79 : */
80 0 : report_unmatched_relation(old_rel, old_db, false);
81 0 : all_matched = false;
82 0 : old_relnum++;
83 0 : continue;
84 : }
85 3154 : if (!old_rel)
86 : {
87 : /*
88 : * new_rel is unmatched. This shouldn't really happen either, but
89 : * if it's a TOAST table, we can ignore it and continue
90 : * processing, assuming that the new server made a TOAST table
91 : * that wasn't needed.
92 : */
93 0 : if (strcmp(new_rel->nspname, "pg_toast") != 0)
94 : {
95 0 : report_unmatched_relation(new_rel, new_db, true);
96 0 : all_matched = false;
97 : }
98 0 : new_relnum++;
99 0 : continue;
100 : }
101 :
102 : /* check for mismatched OID */
103 3154 : if (old_rel->reloid < new_rel->reloid)
104 : {
105 : /* old_rel is unmatched, see comment above */
106 0 : report_unmatched_relation(old_rel, old_db, false);
107 0 : all_matched = false;
108 0 : old_relnum++;
109 0 : continue;
110 : }
111 3154 : else if (old_rel->reloid > new_rel->reloid)
112 : {
113 : /* new_rel is unmatched, see comment above */
114 0 : if (strcmp(new_rel->nspname, "pg_toast") != 0)
115 : {
116 0 : report_unmatched_relation(new_rel, new_db, true);
117 0 : all_matched = false;
118 : }
119 0 : new_relnum++;
120 0 : continue;
121 : }
122 :
123 : /*
124 : * Verify that rels of same OID have same name. The namespace name
125 : * should always match, but the relname might not match for TOAST
126 : * tables (and, therefore, their indexes).
127 : */
128 3154 : if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
129 3154 : strcmp(old_rel->relname, new_rel->relname) != 0)
130 : {
131 0 : pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: "
132 : "old name \"%s.%s\", new name \"%s.%s\"",
133 : old_rel->reloid, old_db->db_name,
134 : old_rel->nspname, old_rel->relname,
135 : new_rel->nspname, new_rel->relname);
136 0 : all_matched = false;
137 0 : old_relnum++;
138 0 : new_relnum++;
139 0 : continue;
140 : }
141 :
142 : /* OK, create a mapping entry */
143 3154 : create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
144 3154 : old_rel, new_rel, maps + num_maps);
145 3154 : num_maps++;
146 3154 : old_relnum++;
147 3154 : new_relnum++;
148 : }
149 :
150 56 : if (!all_matched)
151 0 : pg_fatal("Failed to match up old and new tables in database \"%s\"",
152 : old_db->db_name);
153 :
154 56 : *nmaps = num_maps;
155 56 : return maps;
156 : }
157 :
158 :
159 : /*
160 : * create_rel_filename_map()
161 : *
162 : * fills a file node map structure and returns it in "map".
163 : */
164 : static void
165 3154 : 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)
169 : {
170 : /* In case old/new tablespaces don't match, do them separately. */
171 3154 : if (strlen(old_rel->tablespace) == 0)
172 : {
173 : /*
174 : * relation belongs to the default tablespace, hence relfiles should
175 : * exist in the data directories.
176 : */
177 3106 : map->old_tablespace = old_data;
178 3106 : map->old_tablespace_suffix = "/base";
179 : }
180 : else
181 : {
182 : /* relation belongs to a tablespace, so use the tablespace location */
183 48 : map->old_tablespace = old_rel->tablespace;
184 48 : map->old_tablespace_suffix = old_cluster.tablespace_suffix;
185 : }
186 :
187 : /* Do the same for new tablespaces */
188 3154 : if (strlen(new_rel->tablespace) == 0)
189 : {
190 3106 : map->new_tablespace = new_data;
191 3106 : map->new_tablespace_suffix = "/base";
192 : }
193 : else
194 : {
195 48 : map->new_tablespace = new_rel->tablespace;
196 48 : map->new_tablespace_suffix = new_cluster.tablespace_suffix;
197 : }
198 :
199 : /* DB oid and relfilenumbers are preserved between old and new cluster */
200 3154 : map->db_oid = old_db->db_oid;
201 3154 : map->relfilenumber = old_rel->relfilenumber;
202 :
203 : /* used only for logging and error reporting, old/new are identical */
204 3154 : map->nspname = old_rel->nspname;
205 3154 : map->relname = old_rel->relname;
206 3154 : }
207 :
208 :
209 : /*
210 : * Complain about a relation we couldn't match to the other database,
211 : * identifying it as best we can.
212 : */
213 : static void
214 0 : report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
215 : {
216 0 : Oid reloid = rel->reloid; /* we might change rel below */
217 : char reldesc[1000];
218 : int i;
219 :
220 0 : snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
221 0 : rel->nspname, rel->relname);
222 0 : if (rel->indtable)
223 : {
224 0 : for (i = 0; i < db->rel_arr.nrels; i++)
225 : {
226 0 : const RelInfo *hrel = &db->rel_arr.rels[i];
227 :
228 0 : if (hrel->reloid == rel->indtable)
229 : {
230 0 : snprintf(reldesc + strlen(reldesc),
231 0 : sizeof(reldesc) - strlen(reldesc),
232 0 : _(" which is an index on \"%s.%s\""),
233 0 : hrel->nspname, hrel->relname);
234 : /* Shift attention to index's table for toast check */
235 0 : rel = hrel;
236 0 : break;
237 : }
238 : }
239 0 : if (i >= db->rel_arr.nrels)
240 0 : snprintf(reldesc + strlen(reldesc),
241 0 : sizeof(reldesc) - strlen(reldesc),
242 0 : _(" which is an index on OID %u"), rel->indtable);
243 : }
244 0 : if (rel->toastheap)
245 : {
246 0 : for (i = 0; i < db->rel_arr.nrels; i++)
247 : {
248 0 : const RelInfo *brel = &db->rel_arr.rels[i];
249 :
250 0 : if (brel->reloid == rel->toastheap)
251 : {
252 0 : snprintf(reldesc + strlen(reldesc),
253 0 : sizeof(reldesc) - strlen(reldesc),
254 0 : _(" which is the TOAST table for \"%s.%s\""),
255 0 : brel->nspname, brel->relname);
256 0 : break;
257 : }
258 : }
259 0 : if (i >= db->rel_arr.nrels)
260 0 : snprintf(reldesc + strlen(reldesc),
261 0 : sizeof(reldesc) - strlen(reldesc),
262 0 : _(" which is the TOAST table for OID %u"), rel->toastheap);
263 : }
264 :
265 0 : if (is_new_db)
266 0 : pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s",
267 0 : reloid, db->db_name, reldesc);
268 : else
269 0 : pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s",
270 0 : reloid, db->db_name, reldesc);
271 0 : }
272 :
273 : /*
274 : * get_db_rel_and_slot_infos()
275 : *
276 : * higher level routine to generate dbinfos for the database running
277 : * on the given "port". Assumes that server is already running.
278 : */
279 : void
280 72 : get_db_rel_and_slot_infos(ClusterInfo *cluster)
281 : {
282 72 : UpgradeTask *task = upgrade_task_create();
283 72 : char *rel_infos_query = NULL;
284 72 : char *logical_slot_infos_query = NULL;
285 :
286 72 : if (cluster->dbarr.dbs != NULL)
287 16 : free_db_and_rel_infos(&cluster->dbarr);
288 :
289 72 : get_template0_info(cluster);
290 72 : get_db_infos(cluster);
291 :
292 72 : rel_infos_query = get_rel_infos_query();
293 72 : upgrade_task_add_step(task,
294 : rel_infos_query,
295 : process_rel_infos,
296 : true, NULL);
297 :
298 : /*
299 : * Logical slots are only carried over to the new cluster when the old
300 : * cluster is on PG17 or newer. This is because before that the logical
301 : * slots are not saved at shutdown, so there is no guarantee that the
302 : * latest confirmed_flush_lsn is saved to disk which can lead to data
303 : * loss. It is still not guaranteed for manually created slots in PG17, so
304 : * subsequent checks done in check_old_cluster_for_valid_slots() would
305 : * raise a FATAL error if such slots are included.
306 : */
307 72 : if (cluster == &old_cluster &&
308 30 : GET_MAJOR_VERSION(cluster->major_version) > 1600)
309 : {
310 30 : logical_slot_infos_query = get_old_cluster_logical_slot_infos_query();
311 30 : upgrade_task_add_step(task,
312 : logical_slot_infos_query,
313 : process_old_cluster_logical_slot_infos,
314 : true, NULL);
315 : }
316 :
317 72 : upgrade_task_run(task, cluster);
318 72 : upgrade_task_free(task);
319 :
320 72 : pg_free(rel_infos_query);
321 72 : if (logical_slot_infos_query)
322 30 : pg_free(logical_slot_infos_query);
323 :
324 72 : if (cluster == &old_cluster)
325 30 : pg_log(PG_VERBOSE, "\nsource databases:");
326 : else
327 42 : pg_log(PG_VERBOSE, "\ntarget databases:");
328 :
329 72 : if (log_opts.verbose)
330 0 : print_db_infos(&cluster->dbarr);
331 72 : }
332 :
333 :
334 : /*
335 : * Get information about template0, which will be copied from the old cluster
336 : * to the new cluster.
337 : */
338 : static void
339 72 : get_template0_info(ClusterInfo *cluster)
340 : {
341 72 : PGconn *conn = connectToServer(cluster, "template1");
342 : DbLocaleInfo *locale;
343 : PGresult *dbres;
344 : int i_datencoding;
345 : int i_datlocprovider;
346 : int i_datcollate;
347 : int i_datctype;
348 : int i_datlocale;
349 :
350 72 : if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
351 72 : dbres = executeQueryOrDie(conn,
352 : "SELECT encoding, datlocprovider, "
353 : " datcollate, datctype, datlocale "
354 : "FROM pg_catalog.pg_database "
355 : "WHERE datname='template0'");
356 0 : else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
357 0 : dbres = executeQueryOrDie(conn,
358 : "SELECT encoding, datlocprovider, "
359 : " datcollate, datctype, daticulocale AS datlocale "
360 : "FROM pg_catalog.pg_database "
361 : "WHERE datname='template0'");
362 : else
363 0 : dbres = executeQueryOrDie(conn,
364 : "SELECT encoding, 'c' AS datlocprovider, "
365 : " datcollate, datctype, NULL AS datlocale "
366 : "FROM pg_catalog.pg_database "
367 : "WHERE datname='template0'");
368 :
369 :
370 72 : if (PQntuples(dbres) != 1)
371 0 : pg_fatal("template0 not found");
372 :
373 72 : locale = pg_malloc(sizeof(DbLocaleInfo));
374 :
375 72 : i_datencoding = PQfnumber(dbres, "encoding");
376 72 : i_datlocprovider = PQfnumber(dbres, "datlocprovider");
377 72 : i_datcollate = PQfnumber(dbres, "datcollate");
378 72 : i_datctype = PQfnumber(dbres, "datctype");
379 72 : i_datlocale = PQfnumber(dbres, "datlocale");
380 :
381 72 : locale->db_encoding = atoi(PQgetvalue(dbres, 0, i_datencoding));
382 72 : locale->db_collprovider = PQgetvalue(dbres, 0, i_datlocprovider)[0];
383 72 : locale->db_collate = pg_strdup(PQgetvalue(dbres, 0, i_datcollate));
384 72 : locale->db_ctype = pg_strdup(PQgetvalue(dbres, 0, i_datctype));
385 72 : if (PQgetisnull(dbres, 0, i_datlocale))
386 66 : locale->db_locale = NULL;
387 : else
388 6 : locale->db_locale = pg_strdup(PQgetvalue(dbres, 0, i_datlocale));
389 :
390 72 : cluster->template0 = locale;
391 :
392 72 : PQclear(dbres);
393 72 : PQfinish(conn);
394 72 : }
395 :
396 :
397 : /*
398 : * get_db_infos()
399 : *
400 : * Scans pg_database system catalog and populates all user
401 : * databases.
402 : */
403 : static void
404 72 : get_db_infos(ClusterInfo *cluster)
405 : {
406 72 : PGconn *conn = connectToServer(cluster, "template1");
407 : PGresult *res;
408 : int ntups;
409 : int tupnum;
410 : DbInfo *dbinfos;
411 : int i_datname,
412 : i_oid,
413 : i_spclocation;
414 : char query[QUERY_ALLOC];
415 :
416 72 : snprintf(query, sizeof(query),
417 : "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
418 72 : if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
419 72 : snprintf(query + strlen(query), sizeof(query) - strlen(query),
420 : "datlocprovider, datlocale, ");
421 0 : else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
422 0 : snprintf(query + strlen(query), sizeof(query) - strlen(query),
423 : "datlocprovider, daticulocale AS datlocale, ");
424 : else
425 0 : snprintf(query + strlen(query), sizeof(query) - strlen(query),
426 : "'c' AS datlocprovider, NULL AS datlocale, ");
427 72 : snprintf(query + strlen(query), sizeof(query) - strlen(query),
428 : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
429 : "FROM pg_catalog.pg_database d "
430 : " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
431 : " ON d.dattablespace = t.oid "
432 : "WHERE d.datallowconn = true "
433 : "ORDER BY 1");
434 :
435 72 : res = executeQueryOrDie(conn, "%s", query);
436 :
437 72 : i_oid = PQfnumber(res, "oid");
438 72 : i_datname = PQfnumber(res, "datname");
439 72 : i_spclocation = PQfnumber(res, "spclocation");
440 :
441 72 : ntups = PQntuples(res);
442 72 : dbinfos = (DbInfo *) pg_malloc0(sizeof(DbInfo) * ntups);
443 :
444 276 : for (tupnum = 0; tupnum < ntups; tupnum++)
445 : {
446 204 : char *spcloc = PQgetvalue(res, tupnum, i_spclocation);
447 204 : bool inplace = spcloc[0] && !is_absolute_path(spcloc);
448 :
449 204 : dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
450 204 : dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
451 :
452 : /*
453 : * The tablespace location might be "", meaning the cluster default
454 : * location, i.e. pg_default or pg_global. For in-place tablespaces,
455 : * pg_tablespace_location() returns a path relative to the data
456 : * directory.
457 : */
458 204 : if (inplace)
459 18 : snprintf(dbinfos[tupnum].db_tablespace,
460 : sizeof(dbinfos[tupnum].db_tablespace),
461 : "%s/%s", cluster->pgdata, spcloc);
462 : else
463 186 : snprintf(dbinfos[tupnum].db_tablespace,
464 : sizeof(dbinfos[tupnum].db_tablespace),
465 : "%s", spcloc);
466 : }
467 72 : PQclear(res);
468 :
469 72 : PQfinish(conn);
470 :
471 72 : cluster->dbarr.dbs = dbinfos;
472 72 : cluster->dbarr.ndbs = ntups;
473 72 : }
474 :
475 :
476 : /*
477 : * get_rel_infos_query()
478 : *
479 : * Returns the query for retrieving the relation information for all the user
480 : * tables and indexes in the database, for use by get_db_rel_and_slot_infos()'s
481 : * UpgradeTask.
482 : *
483 : * Note: the result is assumed to be sorted by OID. This allows later
484 : * processing to match up old and new databases efficiently.
485 : */
486 : static char *
487 72 : get_rel_infos_query(void)
488 : {
489 : PQExpBufferData query;
490 :
491 72 : initPQExpBuffer(&query);
492 :
493 : /*
494 : * Create a CTE that collects OIDs of regular user tables and matviews,
495 : * but excluding toast tables and indexes. We assume that relations with
496 : * OIDs >= FirstNormalObjectId belong to the user. (That's probably
497 : * redundant with the namespace-name exclusions, but let's be safe.)
498 : *
499 : * pg_largeobject contains user data that does not appear in pg_dump
500 : * output, so we have to copy that system table. It's easiest to do that
501 : * by treating it as a user table. We can do the same for
502 : * pg_largeobject_metadata for upgrades from v16 and newer. pg_upgrade
503 : * can't copy/link the files from older versions because aclitem (needed
504 : * by pg_largeobject_metadata.lomacl) changed its storage format in v16.
505 : */
506 144 : appendPQExpBuffer(&query,
507 : "WITH regular_heap (reloid, indtable, toastheap) AS ( "
508 : " SELECT c.oid, 0::oid, 0::oid "
509 : " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
510 : " ON c.relnamespace = n.oid "
511 : " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
512 : CppAsString2(RELKIND_MATVIEW) "%s) AND "
513 : /* exclude possible orphaned temp tables */
514 : " ((n.nspname !~ '^pg_temp_' AND "
515 : " n.nspname !~ '^pg_toast_temp_' AND "
516 : " n.nspname NOT IN ('pg_catalog', 'information_schema', "
517 : " 'binary_upgrade', 'pg_toast') AND "
518 : " c.oid >= %u::pg_catalog.oid) OR "
519 : " (n.nspname = 'pg_catalog' AND "
520 : " relname IN ('pg_largeobject'%s) ))), ",
521 72 : (user_opts.transfer_mode == TRANSFER_MODE_SWAP) ?
522 : ", " CppAsString2(RELKIND_SEQUENCE) : "",
523 : FirstNormalObjectId,
524 72 : (GET_MAJOR_VERSION(old_cluster.major_version) >= 1600) ?
525 : ", 'pg_largeobject_metadata'" : "");
526 :
527 : /*
528 : * Add a CTE that collects OIDs of toast tables belonging to the tables
529 : * selected by the regular_heap CTE. (We have to do this separately
530 : * because the namespace-name rules above don't work for toast tables.)
531 : */
532 72 : appendPQExpBufferStr(&query,
533 : " toast_heap (reloid, indtable, toastheap) AS ( "
534 : " SELECT c.reltoastrelid, 0::oid, c.oid "
535 : " FROM regular_heap JOIN pg_catalog.pg_class c "
536 : " ON regular_heap.reloid = c.oid "
537 : " WHERE c.reltoastrelid != 0), ");
538 :
539 : /*
540 : * Add a CTE that collects OIDs of all valid indexes on the previously
541 : * selected tables. We can ignore invalid indexes since pg_dump does.
542 : * Testing indisready is necessary in 9.2, and harmless in earlier/later
543 : * versions.
544 : */
545 72 : appendPQExpBufferStr(&query,
546 : " all_index (reloid, indtable, toastheap) AS ( "
547 : " SELECT indexrelid, indrelid, 0::oid "
548 : " FROM pg_catalog.pg_index "
549 : " WHERE indisvalid AND indisready "
550 : " AND indrelid IN "
551 : " (SELECT reloid FROM regular_heap "
552 : " UNION ALL "
553 : " SELECT reloid FROM toast_heap)) ");
554 :
555 : /*
556 : * And now we can write the query that retrieves the data we want for each
557 : * heap and index relation. Make sure result is sorted by OID.
558 : */
559 72 : appendPQExpBufferStr(&query,
560 : "SELECT all_rels.*, n.nspname, c.relname, "
561 : " c.relfilenode, c.reltablespace, "
562 : " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
563 : "FROM (SELECT * FROM regular_heap "
564 : " UNION ALL "
565 : " SELECT * FROM toast_heap "
566 : " UNION ALL "
567 : " SELECT * FROM all_index) all_rels "
568 : " JOIN pg_catalog.pg_class c "
569 : " ON all_rels.reloid = c.oid "
570 : " JOIN pg_catalog.pg_namespace n "
571 : " ON c.relnamespace = n.oid "
572 : " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
573 : " ON c.reltablespace = t.oid "
574 : "ORDER BY 1");
575 :
576 72 : return query.data;
577 : }
578 :
579 : /*
580 : * Callback function for processing results of the query returned by
581 : * get_rel_infos_query(), which is used for get_db_rel_and_slot_infos()'s
582 : * UpgradeTask. This function stores the relation information for later use.
583 : */
584 : static void
585 204 : process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg)
586 : {
587 204 : int ntups = PQntuples(res);
588 204 : RelInfo *relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
589 204 : int i_reloid = PQfnumber(res, "reloid");
590 204 : int i_indtable = PQfnumber(res, "indtable");
591 204 : int i_toastheap = PQfnumber(res, "toastheap");
592 204 : int i_nspname = PQfnumber(res, "nspname");
593 204 : int i_relname = PQfnumber(res, "relname");
594 204 : int i_relfilenumber = PQfnumber(res, "relfilenode");
595 204 : int i_reltablespace = PQfnumber(res, "reltablespace");
596 204 : int i_spclocation = PQfnumber(res, "spclocation");
597 204 : int num_rels = 0;
598 204 : char *nspname = NULL;
599 204 : char *relname = NULL;
600 204 : char *tablespace = NULL;
601 204 : char *last_namespace = NULL;
602 204 : char *last_tablespace = NULL;
603 :
604 : AssertVariableIsOfType(&process_rel_infos, UpgradeTaskProcessCB);
605 :
606 9784 : for (int relnum = 0; relnum < ntups; relnum++)
607 : {
608 9580 : RelInfo *curr = &relinfos[num_rels++];
609 :
610 9580 : curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
611 9580 : curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
612 9580 : curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
613 :
614 9580 : nspname = PQgetvalue(res, relnum, i_nspname);
615 9580 : curr->nsp_alloc = false;
616 :
617 : /*
618 : * Many of the namespace and tablespace strings are identical, so we
619 : * try to reuse the allocated string pointers where possible to reduce
620 : * memory consumption.
621 : */
622 : /* Can we reuse the previous string allocation? */
623 9580 : if (last_namespace && strcmp(nspname, last_namespace) == 0)
624 5806 : curr->nspname = last_namespace;
625 : else
626 : {
627 3774 : last_namespace = curr->nspname = pg_strdup(nspname);
628 3774 : curr->nsp_alloc = true;
629 : }
630 :
631 9580 : relname = PQgetvalue(res, relnum, i_relname);
632 9580 : curr->relname = pg_strdup(relname);
633 :
634 9580 : curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
635 9580 : curr->tblsp_alloc = false;
636 :
637 : /* Is the tablespace oid non-default? */
638 9580 : if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
639 : {
640 18 : char *spcloc = PQgetvalue(res, relnum, i_spclocation);
641 18 : bool inplace = spcloc[0] && !is_absolute_path(spcloc);
642 :
643 : /*
644 : * The tablespace location might be "", meaning the cluster
645 : * default location, i.e. pg_default or pg_global. For in-place
646 : * tablespaces, pg_tablespace_location() returns a path relative
647 : * to the data directory.
648 : */
649 18 : if (inplace)
650 18 : tablespace = psprintf("%s/%s",
651 18 : os_info.running_cluster->pgdata,
652 : spcloc);
653 : else
654 0 : tablespace = spcloc;
655 :
656 : /* Can we reuse the previous string allocation? */
657 18 : if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
658 0 : curr->tablespace = last_tablespace;
659 : else
660 : {
661 18 : last_tablespace = curr->tablespace = pg_strdup(tablespace);
662 18 : curr->tblsp_alloc = true;
663 : }
664 :
665 : /* Free palloc'd string for in-place tablespaces. */
666 18 : if (inplace)
667 18 : pfree(tablespace);
668 : }
669 : else
670 : /* A zero reltablespace oid indicates the database tablespace. */
671 9562 : curr->tablespace = dbinfo->db_tablespace;
672 : }
673 :
674 204 : dbinfo->rel_arr.rels = relinfos;
675 204 : dbinfo->rel_arr.nrels = num_rels;
676 204 : }
677 :
678 : /*
679 : * get_old_cluster_logical_slot_infos_query()
680 : *
681 : * Returns the query for retrieving the logical slot information for all the
682 : * logical replication slots in the database, for use by
683 : * get_db_rel_and_slot_infos()'s UpgradeTask. The status of each logical slot
684 : * is checked in check_old_cluster_for_valid_slots().
685 : */
686 : static char *
687 30 : get_old_cluster_logical_slot_infos_query(void)
688 : {
689 : /*
690 : * Fetch the logical replication slot information. The check whether the
691 : * slot is considered caught up is done by an upgrade function. This
692 : * regards the slot as caught up if we don't find any decodable changes.
693 : * See binary_upgrade_logical_slot_has_caught_up().
694 : *
695 : * Note that we can't ensure whether the slot is caught up during
696 : * live_check as the new WAL records could be generated.
697 : *
698 : * We intentionally skip checking the WALs for invalidated slots as the
699 : * corresponding WALs could have been removed for such slots.
700 : *
701 : * The temporary slots are explicitly ignored while checking because such
702 : * slots cannot exist after the upgrade. During the upgrade, clusters are
703 : * started and stopped several times causing any temporary slots to be
704 : * removed.
705 : */
706 30 : return psprintf("SELECT slot_name, plugin, two_phase, failover, "
707 : "%s as caught_up, invalidation_reason IS NOT NULL as invalid "
708 : "FROM pg_catalog.pg_replication_slots "
709 : "WHERE slot_type = 'logical' AND "
710 : "database = current_database() AND "
711 : "temporary IS FALSE;",
712 30 : user_opts.live_check ? "FALSE" :
713 : "(CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
714 : "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
715 : "END)");
716 : }
717 :
718 : /*
719 : * Callback function for processing results of the query returned by
720 : * get_old_cluster_logical_slot_infos_query(), which is used for
721 : * get_db_rel_and_slot_infos()'s UpgradeTask. This function stores the logical
722 : * slot information for later use.
723 : */
724 : static void
725 96 : process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg)
726 : {
727 96 : LogicalSlotInfo *slotinfos = NULL;
728 96 : int num_slots = PQntuples(res);
729 :
730 : AssertVariableIsOfType(&process_old_cluster_logical_slot_infos,
731 : UpgradeTaskProcessCB);
732 :
733 96 : if (num_slots)
734 : {
735 : int i_slotname;
736 : int i_plugin;
737 : int i_twophase;
738 : int i_failover;
739 : int i_caught_up;
740 : int i_invalid;
741 :
742 6 : slotinfos = (LogicalSlotInfo *) pg_malloc(sizeof(LogicalSlotInfo) * num_slots);
743 :
744 6 : i_slotname = PQfnumber(res, "slot_name");
745 6 : i_plugin = PQfnumber(res, "plugin");
746 6 : i_twophase = PQfnumber(res, "two_phase");
747 6 : i_failover = PQfnumber(res, "failover");
748 6 : i_caught_up = PQfnumber(res, "caught_up");
749 6 : i_invalid = PQfnumber(res, "invalid");
750 :
751 16 : for (int slotnum = 0; slotnum < num_slots; slotnum++)
752 : {
753 10 : LogicalSlotInfo *curr = &slotinfos[slotnum];
754 :
755 10 : curr->slotname = pg_strdup(PQgetvalue(res, slotnum, i_slotname));
756 10 : curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin));
757 10 : curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0);
758 10 : curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0);
759 10 : curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0);
760 10 : curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0);
761 : }
762 : }
763 :
764 96 : dbinfo->slot_arr.slots = slotinfos;
765 96 : dbinfo->slot_arr.nslots = num_slots;
766 96 : }
767 :
768 :
769 : /*
770 : * count_old_cluster_logical_slots()
771 : *
772 : * Returns the number of logical replication slots for all databases.
773 : *
774 : * Note: this function always returns 0 if the old_cluster is PG16 and prior
775 : * because we gather slot information only for cluster versions greater than or
776 : * equal to PG17. See get_old_cluster_logical_slot_infos().
777 : */
778 : int
779 70 : count_old_cluster_logical_slots(void)
780 : {
781 70 : int slot_count = 0;
782 :
783 302 : for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
784 232 : slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots;
785 :
786 70 : return slot_count;
787 : }
788 :
789 : /*
790 : * get_subscription_info()
791 : *
792 : * Gets the information of subscriptions in the cluster.
793 : */
794 : void
795 28 : get_subscription_info(ClusterInfo *cluster)
796 : {
797 : PGconn *conn;
798 : PGresult *res;
799 : int i_nsub;
800 : int i_retain_dead_tuples;
801 :
802 28 : conn = connectToServer(cluster, "template1");
803 28 : if (GET_MAJOR_VERSION(cluster->major_version) >= 1900)
804 28 : res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
805 : "COUNT(CASE WHEN subretaindeadtuples THEN 1 END) > 0 AS retain_dead_tuples "
806 : "FROM pg_catalog.pg_subscription");
807 : else
808 0 : res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
809 : "'f' AS retain_dead_tuples "
810 : "FROM pg_catalog.pg_subscription");
811 :
812 28 : i_nsub = PQfnumber(res, "nsub");
813 28 : i_retain_dead_tuples = PQfnumber(res, "retain_dead_tuples");
814 :
815 28 : cluster->nsubs = atoi(PQgetvalue(res, 0, i_nsub));
816 28 : cluster->sub_retain_dead_tuples = (strcmp(PQgetvalue(res, 0, i_retain_dead_tuples), "t") == 0);
817 :
818 28 : PQclear(res);
819 28 : PQfinish(conn);
820 28 : }
821 :
822 : static void
823 16 : free_db_and_rel_infos(DbInfoArr *db_arr)
824 : {
825 : int dbnum;
826 :
827 48 : for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
828 : {
829 32 : free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
830 32 : pg_free(db_arr->dbs[dbnum].db_name);
831 : }
832 16 : pg_free(db_arr->dbs);
833 16 : db_arr->dbs = NULL;
834 16 : db_arr->ndbs = 0;
835 16 : }
836 :
837 :
838 : static void
839 32 : free_rel_infos(RelInfoArr *rel_arr)
840 : {
841 : int relnum;
842 :
843 160 : for (relnum = 0; relnum < rel_arr->nrels; relnum++)
844 : {
845 128 : if (rel_arr->rels[relnum].nsp_alloc)
846 32 : pg_free(rel_arr->rels[relnum].nspname);
847 128 : pg_free(rel_arr->rels[relnum].relname);
848 128 : if (rel_arr->rels[relnum].tblsp_alloc)
849 0 : pg_free(rel_arr->rels[relnum].tablespace);
850 : }
851 32 : pg_free(rel_arr->rels);
852 32 : rel_arr->nrels = 0;
853 32 : }
854 :
855 :
856 : static void
857 0 : print_db_infos(DbInfoArr *db_arr)
858 : {
859 : int dbnum;
860 :
861 0 : for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
862 : {
863 0 : DbInfo *pDbInfo = &db_arr->dbs[dbnum];
864 :
865 0 : pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
866 0 : print_rel_infos(&pDbInfo->rel_arr);
867 0 : print_slot_infos(&pDbInfo->slot_arr);
868 : }
869 0 : }
870 :
871 :
872 : static void
873 0 : print_rel_infos(RelInfoArr *rel_arr)
874 : {
875 : int relnum;
876 :
877 0 : for (relnum = 0; relnum < rel_arr->nrels; relnum++)
878 0 : pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
879 0 : rel_arr->rels[relnum].nspname,
880 0 : rel_arr->rels[relnum].relname,
881 0 : rel_arr->rels[relnum].reloid,
882 0 : rel_arr->rels[relnum].tablespace);
883 0 : }
884 :
885 : static void
886 0 : print_slot_infos(LogicalSlotInfoArr *slot_arr)
887 : {
888 : /* Quick return if there are no logical slots. */
889 0 : if (slot_arr->nslots == 0)
890 0 : return;
891 :
892 0 : pg_log(PG_VERBOSE, "Logical replication slots in the database:");
893 :
894 0 : for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
895 : {
896 0 : LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
897 :
898 0 : pg_log(PG_VERBOSE, "slot name: \"%s\", output plugin: \"%s\", two_phase: %s",
899 : slot_info->slotname,
900 : slot_info->plugin,
901 0 : slot_info->two_phase ? "true" : "false");
902 : }
903 : }
|