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 : 34 : 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 : 34 : int num_maps = 0;
53 : 34 : bool all_matched = true;
54 : :
55 : : /* There will certainly not be more mappings than there are old rels */
56 : 34 : 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 : 34 : old_relnum = new_relnum = 0;
64 [ + + ]: 1735 : while (old_relnum < old_db->rel_arr.nrels ||
65 [ - + ]: 34 : new_relnum < new_db->rel_arr.nrels)
66 : : {
67 : 3402 : RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
68 [ + - ]: 1701 : &old_db->rel_arr.rels[old_relnum] : NULL;
69 : 3402 : RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
70 [ + - ]: 1701 : &new_db->rel_arr.rels[new_relnum] : NULL;
71 : :
72 : : /* handle running off one array before the other */
73 [ - + ]: 1701 : 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 [ - + ]: 1701 : 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 [ - + ]: 1701 : 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 [ - + ]: 1701 : 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 [ + - ]: 1701 : if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
128 [ - + ]: 1701 : 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 : 1701 : create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
143 : 1701 : old_rel, new_rel, maps + num_maps);
144 : 1701 : num_maps++;
145 : 1701 : old_relnum++;
146 : 1701 : new_relnum++;
147 : : }
148 : :
149 [ - + ]: 34 : 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 : 34 : *nmaps = num_maps;
154 : 34 : 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 : 1701 : 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 [ + + ]: 1701 : 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 : 1677 : map->old_tablespace = old_data;
177 : 1677 : 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 [ + + ]: 1701 : if (strlen(new_rel->tablespace) == 0)
188 : : {
189 : 1677 : map->new_tablespace = new_data;
190 : 1677 : 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 : 1701 : map->db_oid = old_db->db_oid;
200 : 1701 : map->relfilenumber = old_rel->relfilenumber;
201 : :
202 : : /* used only for logging and error reporting, old/new are identical */
203 : 1701 : map->nspname = old_rel->nspname;
204 : 1701 : map->relname = old_rel->relname;
205 : 1701 : }
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 : 49 : get_db_rel_and_slot_infos(ClusterInfo *cluster)
280 : : {
281 : 49 : UpgradeTask *task = upgrade_task_create();
282 : 49 : char *rel_infos_query = NULL;
283 : :
284 [ + + ]: 49 : if (cluster->dbarr.dbs != NULL)
285 : 11 : free_db_and_rel_infos(&cluster->dbarr);
286 : :
287 : 49 : get_template0_info(cluster);
288 : 49 : get_db_infos(cluster);
289 : :
290 : 49 : rel_infos_query = get_rel_infos_query();
291 : 49 : 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 [ + + ]: 49 : if (cluster == &old_cluster &&
306 [ + - ]: 20 : GET_MAJOR_VERSION(cluster->major_version) > 1600)
307 : 20 : 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 : 49 : upgrade_task_run(task, cluster);
313 : 49 : upgrade_task_free(task);
314 : :
315 : 49 : pg_free(rel_infos_query);
316 : :
317 [ + + ]: 49 : if (cluster == &old_cluster)
318 : 20 : pg_log(PG_VERBOSE, "\nsource databases:");
319 : : else
320 : 29 : pg_log(PG_VERBOSE, "\ntarget databases:");
321 : :
322 [ - + ]: 49 : if (log_opts.verbose)
323 : 0 : print_db_infos(&cluster->dbarr);
324 : 49 : }
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 : 49 : get_template0_info(ClusterInfo *cluster)
333 : : {
334 : 49 : 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 : : int i_daticurules;
343 : :
344 [ + - ]: 49 : if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
345 : 49 : dbres = executeQueryOrDie(conn,
346 : : "SELECT encoding, datlocprovider, "
347 : : " datcollate, datctype, datlocale, "
348 : : " daticurules "
349 : : "FROM pg_catalog.pg_database "
350 : : "WHERE datname='template0'");
351 [ # # ]: 0 : else if (GET_MAJOR_VERSION(cluster->major_version) >= 1600)
352 : 0 : dbres = executeQueryOrDie(conn,
353 : : "SELECT encoding, datlocprovider, "
354 : : " datcollate, datctype, daticulocale AS datlocale, "
355 : : " daticurules "
356 : : "FROM pg_catalog.pg_database "
357 : : "WHERE datname='template0'");
358 [ # # ]: 0 : else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
359 : 0 : dbres = executeQueryOrDie(conn,
360 : : "SELECT encoding, datlocprovider, "
361 : : " datcollate, datctype, daticulocale AS datlocale, "
362 : : " NULL AS daticurules "
363 : : "FROM pg_catalog.pg_database "
364 : : "WHERE datname='template0'");
365 : : else
366 : 0 : dbres = executeQueryOrDie(conn,
367 : : "SELECT encoding, 'c' AS datlocprovider, "
368 : : " datcollate, datctype, NULL AS datlocale, "
369 : : " NULL AS daticurules "
370 : : "FROM pg_catalog.pg_database "
371 : : "WHERE datname='template0'");
372 : :
373 : :
374 [ - + ]: 49 : if (PQntuples(dbres) != 1)
375 : 0 : pg_fatal("template0 not found");
376 : :
377 : 49 : locale = pg_malloc_object(DbLocaleInfo);
378 : :
379 : 49 : i_datencoding = PQfnumber(dbres, "encoding");
380 : 49 : i_datlocprovider = PQfnumber(dbres, "datlocprovider");
381 : 49 : i_datcollate = PQfnumber(dbres, "datcollate");
382 : 49 : i_datctype = PQfnumber(dbres, "datctype");
383 : 49 : i_datlocale = PQfnumber(dbres, "datlocale");
384 : 49 : i_daticurules = PQfnumber(dbres, "daticurules");
385 : :
386 : 49 : locale->db_encoding = atoi(PQgetvalue(dbres, 0, i_datencoding));
387 : 49 : locale->db_collprovider = PQgetvalue(dbres, 0, i_datlocprovider)[0];
388 : 49 : locale->db_collate = pg_strdup(PQgetvalue(dbres, 0, i_datcollate));
389 : 49 : locale->db_ctype = pg_strdup(PQgetvalue(dbres, 0, i_datctype));
390 [ + + ]: 49 : if (PQgetisnull(dbres, 0, i_datlocale))
391 : 45 : locale->db_locale = NULL;
392 : : else
393 : 4 : locale->db_locale = pg_strdup(PQgetvalue(dbres, 0, i_datlocale));
394 [ + - ]: 49 : if (PQgetisnull(dbres, 0, i_daticurules))
395 : 49 : locale->db_icu_rules = NULL;
396 : : else
397 : 0 : locale->db_icu_rules = pg_strdup(PQgetvalue(dbres, 0, i_daticurules));
398 : :
399 : 49 : cluster->template0 = locale;
400 : :
401 : 49 : PQclear(dbres);
402 : 49 : PQfinish(conn);
403 : 49 : }
404 : :
405 : :
406 : : /*
407 : : * get_db_infos()
408 : : *
409 : : * Scans pg_database system catalog and populates all user
410 : : * databases.
411 : : */
412 : : static void
413 : 49 : get_db_infos(ClusterInfo *cluster)
414 : : {
415 : 49 : PGconn *conn = connectToServer(cluster, "template1");
416 : : PGresult *res;
417 : : int ntups;
418 : : int tupnum;
419 : : DbInfo *dbinfos;
420 : : int i_oid,
421 : : i_datname,
422 : : i_spclocation;
423 : : char query[QUERY_ALLOC];
424 : :
425 [ + + ]: 49 : snprintf(query, sizeof(query),
426 : : "SELECT d.oid, d.datname, "
427 : : "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
428 : : "FROM pg_catalog.pg_database d "
429 : : " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
430 : : " ON d.dattablespace = t.oid "
431 : : "WHERE d.datallowconn = true %s"
432 : : "ORDER BY 1",
433 : : cluster == &old_cluster ? "AND d.datconnlimit <> -2 " : "");
434 : :
435 : 49 : res = executeQueryOrDie(conn, "%s", query);
436 : :
437 : 49 : i_oid = PQfnumber(res, "oid");
438 : 49 : i_datname = PQfnumber(res, "datname");
439 : 49 : i_spclocation = PQfnumber(res, "spclocation");
440 : :
441 : 49 : ntups = PQntuples(res);
442 : 49 : dbinfos = pg_malloc0_array(DbInfo, ntups);
443 : :
444 [ + + ]: 181 : for (tupnum = 0; tupnum < ntups; tupnum++)
445 : : {
446 : 132 : char *spcloc = PQgetvalue(res, tupnum, i_spclocation);
447 [ + + + - ]: 132 : bool inplace = spcloc[0] && !is_absolute_path(spcloc);
448 : :
449 : 132 : dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
450 : 132 : 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 [ + + ]: 132 : if (inplace)
459 : 9 : snprintf(dbinfos[tupnum].db_tablespace,
460 : : sizeof(dbinfos[tupnum].db_tablespace),
461 : : "%s/%s", cluster->pgdata, spcloc);
462 : : else
463 : 123 : snprintf(dbinfos[tupnum].db_tablespace,
464 : : sizeof(dbinfos[tupnum].db_tablespace),
465 : : "%s", spcloc);
466 : : }
467 : 49 : PQclear(res);
468 : :
469 : 49 : PQfinish(conn);
470 : :
471 : 49 : cluster->dbarr.dbs = dbinfos;
472 : 49 : cluster->dbarr.ndbs = ntups;
473 : 49 : }
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 : 49 : get_rel_infos_query(void)
488 : : {
489 : : PQExpBufferData query;
490 : :
491 : 49 : 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 : 98 : 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 [ + + ]: 49 : (user_opts.transfer_mode == TRANSFER_MODE_SWAP) ?
522 : : ", " CppAsString2(RELKIND_SEQUENCE) : "",
523 : : FirstNormalObjectId,
524 [ + - ]: 49 : (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 : 49 : 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 : 49 : 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 : 49 : 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 : 49 : 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 : 132 : process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg)
586 : : {
587 : 132 : int ntups = PQntuples(res);
588 : 132 : RelInfo *relinfos = pg_malloc_array(RelInfo, ntups);
589 : 132 : int i_reloid = PQfnumber(res, "reloid");
590 : 132 : int i_indtable = PQfnumber(res, "indtable");
591 : 132 : int i_toastheap = PQfnumber(res, "toastheap");
592 : 132 : int i_nspname = PQfnumber(res, "nspname");
593 : 132 : int i_relname = PQfnumber(res, "relname");
594 : 132 : int i_relfilenumber = PQfnumber(res, "relfilenode");
595 : 132 : int i_reltablespace = PQfnumber(res, "reltablespace");
596 : 132 : int i_spclocation = PQfnumber(res, "spclocation");
597 : 132 : int num_rels = 0;
598 : 132 : char *nspname = NULL;
599 : 132 : char *relname = NULL;
600 : 132 : char *tablespace = NULL;
601 : 132 : char *last_namespace = NULL;
602 : 132 : char *last_tablespace = NULL;
603 : :
604 [ + + ]: 6821 : for (int relnum = 0; relnum < ntups; relnum++)
605 : : {
606 : 6689 : RelInfo *curr = &relinfos[num_rels++];
607 : :
608 : 6689 : curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
609 : 6689 : curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
610 : 6689 : curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
611 : :
612 : 6689 : nspname = PQgetvalue(res, relnum, i_nspname);
613 : 6689 : curr->nsp_alloc = false;
614 : :
615 : : /*
616 : : * Many of the namespace and tablespace strings are identical, so we
617 : : * try to reuse the allocated string pointers where possible to reduce
618 : : * memory consumption.
619 : : */
620 : : /* Can we reuse the previous string allocation? */
621 [ + + + + ]: 6689 : if (last_namespace && strcmp(nspname, last_namespace) == 0)
622 : 4062 : curr->nspname = last_namespace;
623 : : else
624 : : {
625 : 2627 : last_namespace = curr->nspname = pg_strdup(nspname);
626 : 2627 : curr->nsp_alloc = true;
627 : : }
628 : :
629 : 6689 : relname = PQgetvalue(res, relnum, i_relname);
630 : 6689 : curr->relname = pg_strdup(relname);
631 : :
632 : 6689 : curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
633 : 6689 : curr->tblsp_alloc = false;
634 : :
635 : : /* Is the tablespace oid non-default? */
636 [ + + ]: 6689 : if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
637 : : {
638 : 9 : char *spcloc = PQgetvalue(res, relnum, i_spclocation);
639 [ + - + - ]: 9 : bool inplace = spcloc[0] && !is_absolute_path(spcloc);
640 : :
641 : : /*
642 : : * The tablespace location might be "", meaning the cluster
643 : : * default location, i.e. pg_default or pg_global. For in-place
644 : : * tablespaces, pg_tablespace_location() returns a path relative
645 : : * to the data directory.
646 : : */
647 [ + - ]: 9 : if (inplace)
648 : 9 : tablespace = psprintf("%s/%s",
649 : 9 : os_info.running_cluster->pgdata,
650 : : spcloc);
651 : : else
652 : 0 : tablespace = spcloc;
653 : :
654 : : /* Can we reuse the previous string allocation? */
655 [ - + - - ]: 9 : if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
656 : 0 : curr->tablespace = last_tablespace;
657 : : else
658 : : {
659 : 9 : last_tablespace = curr->tablespace = pg_strdup(tablespace);
660 : 9 : curr->tblsp_alloc = true;
661 : : }
662 : :
663 : : /* Free palloc'd string for in-place tablespaces. */
664 [ + - ]: 9 : if (inplace)
665 : 9 : pfree(tablespace);
666 : : }
667 : : else
668 : : /* A zero reltablespace oid indicates the database tablespace. */
669 : 6680 : curr->tablespace = dbinfo->db_tablespace;
670 : : }
671 : :
672 : 132 : dbinfo->rel_arr.rels = relinfos;
673 : 132 : dbinfo->rel_arr.nrels = num_rels;
674 : 132 : }
675 : :
676 : : /*
677 : : * get_old_cluster_logical_slot_infos_query()
678 : : *
679 : : * Returns the query for retrieving the logical slot information for all the
680 : : * logical replication slots in the database, for use by
681 : : * get_db_rel_and_slot_infos()'s UpgradeTask. The status of each logical slot
682 : : * is checked in check_old_cluster_for_valid_slots().
683 : : */
684 : : static const char *
685 : 20 : get_old_cluster_logical_slot_infos_query(ClusterInfo *cluster)
686 : : {
687 : : /*
688 : : * Fetch the logical replication slot information. The check whether the
689 : : * slot is considered caught up is done by an upgrade function. This
690 : : * regards the slot as caught up if we don't find any decodable changes.
691 : : * The implementation of this check varies depending on the server
692 : : * version.
693 : : *
694 : : * We intentionally skip checking the WALs for invalidated slots as the
695 : : * corresponding WALs could have been removed for such slots.
696 : : *
697 : : * The temporary slots are explicitly ignored while checking because such
698 : : * slots cannot exist after the upgrade. During the upgrade, clusters are
699 : : * started and stopped several times causing any temporary slots to be
700 : : * removed.
701 : : */
702 : :
703 [ + + ]: 20 : if (user_opts.live_check)
704 : : {
705 : : /*
706 : : * We skip the caught-up check during live_check. We cannot verify
707 : : * whether the slot is caught up in this mode, as new WAL records
708 : : * could be generated concurrently.
709 : : */
710 : 1 : return "SELECT slot_name, plugin, two_phase, failover, "
711 : : "FALSE as caught_up, "
712 : : "invalidation_reason IS NOT NULL as invalid "
713 : : "FROM pg_catalog.pg_replication_slots "
714 : : "WHERE slot_type = 'logical' AND "
715 : : "database = current_database() AND "
716 : : "temporary IS FALSE";
717 : : }
718 [ + - ]: 19 : else if (GET_MAJOR_VERSION(cluster->major_version) >= 1900)
719 : : {
720 : : /*
721 : : * For PG19 and later, we optimize the slot caught-up check to avoid
722 : : * reading the same WAL stream multiple times: execute the caught-up
723 : : * check only for the slot with the minimum confirmed_flush_lsn, and
724 : : * apply the same result to all other slots in the same database. This
725 : : * limits the check to at most one logical slot per database. We also
726 : : * use the maximum confirmed_flush_lsn among all logical slots on the
727 : : * database as an early scan cutoff; finding a decodable WAL record
728 : : * beyond this point implies that no slot has caught up.
729 : : *
730 : : * Note that we don't distinguish slots based on their output plugin.
731 : : * If a plugin applies replication origin filters, we might get a
732 : : * false positive (i.e., erroneously considering a slot caught up).
733 : : * However, such cases are very rare, and the impact of a false
734 : : * positive is minimal.
735 : : */
736 : 19 : return "WITH check_caught_up AS ( "
737 : : " SELECT pg_catalog.binary_upgrade_check_logical_slot_pending_wal(slot_name, "
738 : : " MAX(confirmed_flush_lsn) OVER ()) as last_pending_wal "
739 : : " FROM pg_replication_slots "
740 : : " WHERE slot_type = 'logical' AND "
741 : : " database = current_database() AND "
742 : : " temporary IS FALSE AND "
743 : : " invalidation_reason IS NULL "
744 : : " ORDER BY confirmed_flush_lsn ASC "
745 : : " LIMIT 1 "
746 : : ") "
747 : : "SELECT slot_name, plugin, two_phase, failover, "
748 : : "CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
749 : : "ELSE last_pending_wal IS NULL OR "
750 : : " confirmed_flush_lsn > last_pending_wal "
751 : : "END as caught_up, "
752 : : "invalidation_reason IS NOT NULL as invalid "
753 : : "FROM pg_catalog.pg_replication_slots "
754 : : "LEFT JOIN check_caught_up ON true "
755 : : "WHERE slot_type = 'logical' AND "
756 : : "database = current_database() AND "
757 : : "temporary IS FALSE ";
758 : : }
759 : :
760 : : /*
761 : : * For PG18 and earlier, we call
762 : : * binary_upgrade_logical_slot_has_caught_up() for each logical slot.
763 : : */
764 : 0 : return "SELECT slot_name, plugin, two_phase, failover, "
765 : : "CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
766 : : "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
767 : : "END as caught_up, "
768 : : "invalidation_reason IS NOT NULL as invalid "
769 : : "FROM pg_catalog.pg_replication_slots "
770 : : "WHERE slot_type = 'logical' AND "
771 : : "database = current_database() AND "
772 : : "temporary IS FALSE ";
773 : : }
774 : :
775 : : /*
776 : : * Callback function for processing results of the query, which is used for
777 : : * get_db_rel_and_slot_infos()'s UpgradeTask. This function stores the logical
778 : : * slot information for later use.
779 : : */
780 : : static void
781 : 62 : process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg)
782 : : {
783 : 62 : LogicalSlotInfo *slotinfos = NULL;
784 : 62 : int num_slots = PQntuples(res);
785 : :
786 [ + + ]: 62 : if (num_slots)
787 : : {
788 : : int i_slotname;
789 : : int i_plugin;
790 : : int i_twophase;
791 : : int i_failover;
792 : : int i_caught_up;
793 : : int i_invalid;
794 : :
795 : 4 : slotinfos = pg_malloc_array(LogicalSlotInfo, num_slots);
796 : :
797 : 4 : i_slotname = PQfnumber(res, "slot_name");
798 : 4 : i_plugin = PQfnumber(res, "plugin");
799 : 4 : i_twophase = PQfnumber(res, "two_phase");
800 : 4 : i_failover = PQfnumber(res, "failover");
801 : 4 : i_caught_up = PQfnumber(res, "caught_up");
802 : 4 : i_invalid = PQfnumber(res, "invalid");
803 : :
804 [ + + ]: 14 : for (int slotnum = 0; slotnum < num_slots; slotnum++)
805 : : {
806 : 10 : LogicalSlotInfo *curr = &slotinfos[slotnum];
807 : :
808 : 10 : curr->slotname = pg_strdup(PQgetvalue(res, slotnum, i_slotname));
809 : 10 : curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin));
810 : 10 : curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0);
811 : 10 : curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0);
812 : 10 : curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0);
813 : 10 : curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0);
814 : : }
815 : : }
816 : :
817 : 62 : dbinfo->slot_arr.slots = slotinfos;
818 : 62 : dbinfo->slot_arr.nslots = num_slots;
819 : 62 : }
820 : :
821 : :
822 : : /*
823 : : * count_old_cluster_logical_slots()
824 : : *
825 : : * Returns the number of logical replication slots for all databases.
826 : : *
827 : : * Note: this function always returns 0 if the old_cluster is PG16 and prior
828 : : * because we gather slot information only for cluster versions greater than or
829 : : * equal to PG17. See get_db_rel_and_slot_infos().
830 : : */
831 : : int
832 : 48 : count_old_cluster_logical_slots(void)
833 : : {
834 : 48 : int slot_count = 0;
835 : :
836 [ + + ]: 198 : for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
837 : 150 : slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots;
838 : :
839 : 48 : return slot_count;
840 : : }
841 : :
842 : : /*
843 : : * get_subscription_info()
844 : : *
845 : : * Gets the information of subscriptions in the cluster.
846 : : */
847 : : void
848 : 19 : get_subscription_info(ClusterInfo *cluster)
849 : : {
850 : : PGconn *conn;
851 : : PGresult *res;
852 : : int i_nsub;
853 : : int i_retain_dead_tuples;
854 : :
855 : 19 : conn = connectToServer(cluster, "template1");
856 [ + - ]: 19 : if (GET_MAJOR_VERSION(cluster->major_version) >= 1900)
857 : 19 : res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
858 : : "COUNT(CASE WHEN subretaindeadtuples THEN 1 END) > 0 AS retain_dead_tuples "
859 : : "FROM pg_catalog.pg_subscription");
860 : : else
861 : 0 : res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
862 : : "'f' AS retain_dead_tuples "
863 : : "FROM pg_catalog.pg_subscription");
864 : :
865 : 19 : i_nsub = PQfnumber(res, "nsub");
866 : 19 : i_retain_dead_tuples = PQfnumber(res, "retain_dead_tuples");
867 : :
868 : 19 : cluster->nsubs = atoi(PQgetvalue(res, 0, i_nsub));
869 : 19 : cluster->sub_retain_dead_tuples = (strcmp(PQgetvalue(res, 0, i_retain_dead_tuples), "t") == 0);
870 : :
871 : 19 : PQclear(res);
872 : 19 : PQfinish(conn);
873 : 19 : }
874 : :
875 : : static void
876 : 11 : free_db_and_rel_infos(DbInfoArr *db_arr)
877 : : {
878 : : int dbnum;
879 : :
880 [ + + ]: 33 : for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
881 : : {
882 : 22 : free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
883 : 22 : pg_free(db_arr->dbs[dbnum].db_name);
884 : : }
885 : 11 : pg_free(db_arr->dbs);
886 : 11 : db_arr->dbs = NULL;
887 : 11 : db_arr->ndbs = 0;
888 : 11 : }
889 : :
890 : :
891 : : static void
892 : 22 : free_rel_infos(RelInfoArr *rel_arr)
893 : : {
894 : : int relnum;
895 : :
896 [ + + ]: 110 : for (relnum = 0; relnum < rel_arr->nrels; relnum++)
897 : : {
898 [ + + ]: 88 : if (rel_arr->rels[relnum].nsp_alloc)
899 : 22 : pg_free(rel_arr->rels[relnum].nspname);
900 : 88 : pg_free(rel_arr->rels[relnum].relname);
901 [ - + ]: 88 : if (rel_arr->rels[relnum].tblsp_alloc)
902 : 0 : pg_free(rel_arr->rels[relnum].tablespace);
903 : : }
904 : 22 : pg_free(rel_arr->rels);
905 : 22 : rel_arr->nrels = 0;
906 : 22 : }
907 : :
908 : :
909 : : static void
910 : 0 : print_db_infos(DbInfoArr *db_arr)
911 : : {
912 : : int dbnum;
913 : :
914 [ # # ]: 0 : for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
915 : : {
916 : 0 : DbInfo *pDbInfo = &db_arr->dbs[dbnum];
917 : :
918 : 0 : pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
919 : 0 : print_rel_infos(&pDbInfo->rel_arr);
920 : 0 : print_slot_infos(&pDbInfo->slot_arr);
921 : : }
922 : 0 : }
923 : :
924 : :
925 : : static void
926 : 0 : print_rel_infos(RelInfoArr *rel_arr)
927 : : {
928 : : int relnum;
929 : :
930 [ # # ]: 0 : for (relnum = 0; relnum < rel_arr->nrels; relnum++)
931 : 0 : pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
932 : 0 : rel_arr->rels[relnum].nspname,
933 : 0 : rel_arr->rels[relnum].relname,
934 : 0 : rel_arr->rels[relnum].reloid,
935 : 0 : rel_arr->rels[relnum].tablespace);
936 : 0 : }
937 : :
938 : : static void
939 : 0 : print_slot_infos(LogicalSlotInfoArr *slot_arr)
940 : : {
941 : : /* Quick return if there are no logical slots. */
942 [ # # ]: 0 : if (slot_arr->nslots == 0)
943 : 0 : return;
944 : :
945 : 0 : pg_log(PG_VERBOSE, "Logical replication slots in the database:");
946 : :
947 [ # # ]: 0 : for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
948 : : {
949 : 0 : LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
950 : :
951 : 0 : pg_log(PG_VERBOSE, "slot name: \"%s\", output plugin: \"%s\", two_phase: %s",
952 : : slot_info->slotname,
953 : : slot_info->plugin,
954 [ # # ]: 0 : slot_info->two_phase ? "true" : "false");
955 : : }
956 : : }
|