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