Age Owner Branch data TLA 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 *
5815 bruce@momjian.us 45 :CBC 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;
5975 52 : 32 : int num_maps = 0;
3789 tgl@sss.pgh.pa.us 53 : 32 : bool all_matched = true;
54 : :
55 : : /* There will certainly not be more mappings than there are old rels */
205 michael@paquier.xyz 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 : : */
3789 tgl@sss.pgh.pa.us 63 : 32 : old_relnum = new_relnum = 0;
64 [ + + ]: 1675 : while (old_relnum < old_db->rel_arr.nrels ||
65 [ - + ]: 32 : new_relnum < new_db->rel_arr.nrels)
66 : : {
67 : 3286 : RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
1220 68 [ + - ]: 1643 : &old_db->rel_arr.rels[old_relnum] : NULL;
3789 69 : 3286 : RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
1220 70 [ + - ]: 1643 : &new_db->rel_arr.rels[new_relnum] : NULL;
71 : :
72 : : /* handle running off one array before the other */
3789 73 [ - + ]: 1643 : 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 : : */
3789 tgl@sss.pgh.pa.us 79 :UBC 0 : report_unmatched_relation(old_rel, old_db, false);
80 : 0 : all_matched = false;
81 : 0 : old_relnum++;
82 : 0 : continue;
83 : : }
3789 tgl@sss.pgh.pa.us 84 [ - + ]:CBC 1643 : 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 : : */
3789 tgl@sss.pgh.pa.us 92 [ # # ]:UBC 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 */
3789 tgl@sss.pgh.pa.us 102 [ - + ]:CBC 1643 : if (old_rel->reloid < new_rel->reloid)
103 : : {
104 : : /* old_rel is unmatched, see comment above */
3789 tgl@sss.pgh.pa.us 105 :UBC 0 : report_unmatched_relation(old_rel, old_db, false);
106 : 0 : all_matched = false;
107 : 0 : old_relnum++;
108 : 0 : continue;
109 : : }
3789 tgl@sss.pgh.pa.us 110 [ - + ]:CBC 1643 : else if (old_rel->reloid > new_rel->reloid)
111 : : {
112 : : /* new_rel is unmatched, see comment above */
3789 tgl@sss.pgh.pa.us 113 [ # # ]:UBC 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 : : */
5677 bruce@momjian.us 127 [ + - ]:CBC 1643 : if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
1741 tgl@sss.pgh.pa.us 128 [ - + ]: 1643 : strcmp(old_rel->relname, new_rel->relname) != 0)
129 : : {
3789 tgl@sss.pgh.pa.us 130 :UBC 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 */
5738 bruce@momjian.us 142 :CBC 1643 : create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
5642 143 : 1643 : old_rel, new_rel, maps + num_maps);
5975 144 : 1643 : num_maps++;
4458 145 : 1643 : old_relnum++;
3789 tgl@sss.pgh.pa.us 146 : 1643 : new_relnum++;
147 : : }
148 : :
149 [ - + ]: 32 : if (!all_matched)
1531 tgl@sss.pgh.pa.us 150 :UBC 0 : pg_fatal("Failed to match up old and new tables in database \"%s\"",
151 : : old_db->db_name);
152 : :
5975 bruce@momjian.us 153 :CBC 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
5738 164 : 1643 : 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 [ + + ]: 1643 : 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 : : */
4603 176 : 1619 : map->old_tablespace = old_data;
177 : 1619 : 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 */
4027 187 [ + + ]: 1643 : if (strlen(new_rel->tablespace) == 0)
188 : : {
189 : 1619 : map->new_tablespace = new_data;
190 : 1619 : map->new_tablespace_suffix = "/base";
191 : : }
192 : : else
193 : : {
194 : 24 : map->new_tablespace = new_rel->tablespace;
4603 195 : 24 : map->new_tablespace_suffix = new_cluster.tablespace_suffix;
196 : : }
197 : :
198 : : /* DB oid and relfilenumbers are preserved between old and new cluster */
1700 rhaas@postgresql.org 199 : 1643 : map->db_oid = old_db->db_oid;
1537 200 : 1643 : map->relfilenumber = old_rel->relfilenumber;
201 : :
202 : : /* used only for logging and error reporting, old/new are identical */
5022 bruce@momjian.us 203 : 1643 : map->nspname = old_rel->nspname;
204 : 1643 : map->relname = old_rel->relname;
5975 205 : 1643 : }
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
3789 tgl@sss.pgh.pa.us 213 :UBC 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),
3628 peter_e@gmx.net 231 : 0 : _(" which is an index on \"%s.%s\""),
3789 tgl@sss.pgh.pa.us 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),
3628 peter_e@gmx.net 241 : 0 : _(" which is an index on OID %u"), rel->indtable);
242 : : }
3789 tgl@sss.pgh.pa.us 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),
3628 peter_e@gmx.net 253 : 0 : _(" which is the TOAST table for \"%s.%s\""),
3789 tgl@sss.pgh.pa.us 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),
3378 261 : 0 : _(" which is the TOAST table for OID %u"), rel->toastheap);
262 : : }
263 : :
3789 264 [ # # ]: 0 : if (is_new_db)
1531 265 : 0 : pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s",
3789 266 : 0 : reloid, db->db_name, reldesc);
267 : : else
1531 268 : 0 : pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s",
3789 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
786 nathan@postgresql.or 279 :CBC 46 : get_db_rel_and_slot_infos(ClusterInfo *cluster)
280 : : {
734 281 : 46 : UpgradeTask *task = upgrade_task_create();
282 : 46 : char *rel_infos_query = NULL;
283 : :
5696 bruce@momjian.us 284 [ + + ]: 46 : if (cluster->dbarr.dbs != NULL)
285 : 10 : free_db_and_rel_infos(&cluster->dbarr);
286 : :
1291 jdavis@postgresql.or 287 : 46 : get_template0_info(cluster);
5732 bruce@momjian.us 288 : 46 : get_db_infos(cluster);
289 : :
734 nathan@postgresql.or 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 : :
3355 alvherre@alvh.no-ip. 317 [ + + ]: 46 : if (cluster == &old_cluster)
1531 tgl@sss.pgh.pa.us 318 : 19 : pg_log(PG_VERBOSE, "\nsource databases:");
319 : : else
320 : 27 : pg_log(PG_VERBOSE, "\ntarget databases:");
321 : :
5305 bruce@momjian.us 322 [ - + ]: 46 : if (log_opts.verbose)
5732 bruce@momjian.us 323 :UBC 0 : print_db_infos(&cluster->dbarr);
5732 bruce@momjian.us 324 :CBC 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
1291 jdavis@postgresql.or 332 : 46 : get_template0_info(ClusterInfo *cluster)
333 : : {
1220 tgl@sss.pgh.pa.us 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 : :
925 jdavis@postgresql.or 343 [ + - ]: 46 : if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
1291 344 : 46 : dbres = executeQueryOrDie(conn,
345 : : "SELECT encoding, datlocprovider, "
346 : : " datcollate, datctype, datlocale "
347 : : "FROM pg_catalog.pg_database "
348 : : "WHERE datname='template0'");
925 jdavis@postgresql.or 349 [ # # ]:UBC 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
1291 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 : :
1291 jdavis@postgresql.or 363 [ - + ]:CBC 46 : if (PQntuples(dbres) != 1)
1291 jdavis@postgresql.or 364 :UBC 0 : pg_fatal("template0 not found");
365 : :
205 michael@paquier.xyz 366 :CBC 46 : locale = pg_malloc_object(DbLocaleInfo);
367 : :
1291 jdavis@postgresql.or 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");
925 372 : 46 : i_datlocale = PQfnumber(dbres, "datlocale");
373 : :
1291 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));
925 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 : :
1291 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
5741 bruce@momjian.us 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 : :
5401 magnus@hagander.net 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 "
416 : : "ORDER BY 1");
417 : :
418 : 46 : res = executeQueryOrDie(conn, "%s", query);
419 : :
5975 bruce@momjian.us 420 : 46 : i_oid = PQfnumber(res, "oid");
5678 421 : 46 : i_datname = PQfnumber(res, "datname");
5975 422 : 46 : i_spclocation = PQfnumber(res, "spclocation");
423 : :
424 : 46 : ntups = PQntuples(res);
205 michael@paquier.xyz 425 : 46 : dbinfos = pg_malloc0_array(DbInfo, ntups);
426 : :
5975 bruce@momjian.us 427 [ + + ]: 172 : for (tupnum = 0; tupnum < ntups; tupnum++)
428 : : {
417 nathan@postgresql.or 429 : 126 : char *spcloc = PQgetvalue(res, tupnum, i_spclocation);
430 [ + + + - ]: 126 : bool inplace = spcloc[0] && !is_absolute_path(spcloc);
431 : :
5836 bruce@momjian.us 432 : 126 : dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
5022 433 : 126 : dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
434 : :
435 : : /*
436 : : * The tablespace location might be "", meaning the cluster default
437 : : * location, i.e. pg_default or pg_global. For in-place tablespaces,
438 : : * pg_tablespace_location() returns a path relative to the data
439 : : * directory.
440 : : */
417 nathan@postgresql.or 441 [ + + ]: 126 : if (inplace)
442 : 9 : snprintf(dbinfos[tupnum].db_tablespace,
443 : : sizeof(dbinfos[tupnum].db_tablespace),
444 : : "%s/%s", cluster->pgdata, spcloc);
445 : : else
446 : 117 : snprintf(dbinfos[tupnum].db_tablespace,
447 : : sizeof(dbinfos[tupnum].db_tablespace),
448 : : "%s", spcloc);
449 : : }
5975 bruce@momjian.us 450 : 46 : PQclear(res);
451 : :
452 : 46 : PQfinish(conn);
453 : :
5741 454 : 46 : cluster->dbarr.dbs = dbinfos;
455 : 46 : cluster->dbarr.ndbs = ntups;
5975 456 : 46 : }
457 : :
458 : :
459 : : /*
460 : : * get_rel_infos_query()
461 : : *
462 : : * Returns the query for retrieving the relation information for all the user
463 : : * tables and indexes in the database, for use by get_db_rel_and_slot_infos()'s
464 : : * UpgradeTask.
465 : : *
466 : : * Note: the result is assumed to be sorted by OID. This allows later
467 : : * processing to match up old and new databases efficiently.
468 : : */
469 : : static char *
734 nathan@postgresql.or 470 : 46 : get_rel_infos_query(void)
471 : : {
472 : : PQExpBufferData query;
473 : :
474 : 46 : initPQExpBuffer(&query);
475 : :
476 : : /*
477 : : * Create a CTE that collects OIDs of regular user tables and matviews,
478 : : * but excluding toast tables and indexes. We assume that relations with
479 : : * OIDs >= FirstNormalObjectId belong to the user. (That's probably
480 : : * redundant with the namespace-name exclusions, but let's be safe.)
481 : : *
482 : : * pg_largeobject contains user data that does not appear in pg_dump
483 : : * output, so we have to copy that system table. It's easiest to do that
484 : : * by treating it as a user table. We can do the same for
485 : : * pg_largeobject_metadata for upgrades from v16 and newer. pg_upgrade
486 : : * can't copy/link the files from older versions because aclitem (needed
487 : : * by pg_largeobject_metadata.lomacl) changed its storage format in v16.
488 : : */
489 : 92 : appendPQExpBuffer(&query,
490 : : "WITH regular_heap (reloid, indtable, toastheap) AS ( "
491 : : " SELECT c.oid, 0::oid, 0::oid "
492 : : " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
493 : : " ON c.relnamespace = n.oid "
494 : : " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
495 : : CppAsString2(RELKIND_MATVIEW) "%s) AND "
496 : : /* exclude possible orphaned temp tables */
497 : : " ((n.nspname !~ '^pg_temp_' AND "
498 : : " n.nspname !~ '^pg_toast_temp_' AND "
499 : : " n.nspname NOT IN ('pg_catalog', 'information_schema', "
500 : : " 'binary_upgrade', 'pg_toast') AND "
501 : : " c.oid >= %u::pg_catalog.oid) OR "
502 : : " (n.nspname = 'pg_catalog' AND "
503 : : " relname IN ('pg_largeobject'%s) ))), ",
544 504 [ + + ]: 46 : (user_opts.transfer_mode == TRANSFER_MODE_SWAP) ?
505 : : ", " CppAsString2(RELKIND_SEQUENCE) : "",
506 : : FirstNormalObjectId,
377 507 [ + - ]: 46 : (GET_MAJOR_VERSION(old_cluster.major_version) >= 1600) ?
508 : : ", 'pg_largeobject_metadata'" : "");
509 : :
510 : : /*
511 : : * Add a CTE that collects OIDs of toast tables belonging to the tables
512 : : * selected by the regular_heap CTE. (We have to do this separately
513 : : * because the namespace-name rules above don't work for toast tables.)
514 : : */
734 515 : 46 : appendPQExpBufferStr(&query,
516 : : " toast_heap (reloid, indtable, toastheap) AS ( "
517 : : " SELECT c.reltoastrelid, 0::oid, c.oid "
518 : : " FROM regular_heap JOIN pg_catalog.pg_class c "
519 : : " ON regular_heap.reloid = c.oid "
520 : : " WHERE c.reltoastrelid != 0), ");
521 : :
522 : : /*
523 : : * Add a CTE that collects OIDs of all valid indexes on the previously
524 : : * selected tables. We can ignore invalid indexes since pg_dump does.
525 : : * Testing indisready is necessary in 9.2, and harmless in earlier/later
526 : : * versions.
527 : : */
528 : 46 : appendPQExpBufferStr(&query,
529 : : " all_index (reloid, indtable, toastheap) AS ( "
530 : : " SELECT indexrelid, indrelid, 0::oid "
531 : : " FROM pg_catalog.pg_index "
532 : : " WHERE indisvalid AND indisready "
533 : : " AND indrelid IN "
534 : : " (SELECT reloid FROM regular_heap "
535 : : " UNION ALL "
536 : : " SELECT reloid FROM toast_heap)) ");
537 : :
538 : : /*
539 : : * And now we can write the query that retrieves the data we want for each
540 : : * heap and index relation. Make sure result is sorted by OID.
541 : : */
542 : 46 : appendPQExpBufferStr(&query,
543 : : "SELECT all_rels.*, n.nspname, c.relname, "
544 : : " c.relfilenode, c.reltablespace, "
545 : : " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
546 : : "FROM (SELECT * FROM regular_heap "
547 : : " UNION ALL "
548 : : " SELECT * FROM toast_heap "
549 : : " UNION ALL "
550 : : " SELECT * FROM all_index) all_rels "
551 : : " JOIN pg_catalog.pg_class c "
552 : : " ON all_rels.reloid = c.oid "
553 : : " JOIN pg_catalog.pg_namespace n "
554 : : " ON c.relnamespace = n.oid "
555 : : " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
556 : : " ON c.reltablespace = t.oid "
557 : : "ORDER BY 1");
558 : :
559 : 46 : return query.data;
560 : : }
561 : :
562 : : /*
563 : : * Callback function for processing results of the query returned by
564 : : * get_rel_infos_query(), which is used for get_db_rel_and_slot_infos()'s
565 : : * UpgradeTask. This function stores the relation information for later use.
566 : : */
567 : : static void
568 : 126 : process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg)
569 : : {
570 : 126 : int ntups = PQntuples(res);
205 michael@paquier.xyz 571 : 126 : RelInfo *relinfos = pg_malloc_array(RelInfo, ntups);
734 nathan@postgresql.or 572 : 126 : int i_reloid = PQfnumber(res, "reloid");
573 : 126 : int i_indtable = PQfnumber(res, "indtable");
574 : 126 : int i_toastheap = PQfnumber(res, "toastheap");
575 : 126 : int i_nspname = PQfnumber(res, "nspname");
576 : 126 : int i_relname = PQfnumber(res, "relname");
577 : 126 : int i_relfilenumber = PQfnumber(res, "relfilenode");
578 : 126 : int i_reltablespace = PQfnumber(res, "reltablespace");
579 : 126 : int i_spclocation = PQfnumber(res, "spclocation");
580 : 126 : int num_rels = 0;
581 : 126 : char *nspname = NULL;
582 : 126 : char *relname = NULL;
583 : 126 : char *tablespace = NULL;
584 : 126 : char *last_namespace = NULL;
585 : 126 : char *last_tablespace = NULL;
586 : :
587 [ + + ]: 6651 : for (int relnum = 0; relnum < ntups; relnum++)
588 : : {
5975 bruce@momjian.us 589 : 6525 : RelInfo *curr = &relinfos[num_rels++];
590 : :
3789 tgl@sss.pgh.pa.us 591 : 6525 : curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
592 : 6525 : curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
593 : 6525 : curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
594 : :
5975 bruce@momjian.us 595 : 6525 : nspname = PQgetvalue(res, relnum, i_nspname);
4603 596 : 6525 : curr->nsp_alloc = false;
597 : :
598 : : /*
599 : : * Many of the namespace and tablespace strings are identical, so we
600 : : * try to reuse the allocated string pointers where possible to reduce
601 : : * memory consumption.
602 : : */
603 : : /* Can we reuse the previous string allocation? */
604 [ + + + + ]: 6525 : if (last_namespace && strcmp(nspname, last_namespace) == 0)
605 : 3988 : curr->nspname = last_namespace;
606 : : else
607 : : {
608 : 2537 : last_namespace = curr->nspname = pg_strdup(nspname);
609 : 2537 : curr->nsp_alloc = true;
610 : : }
611 : :
5975 612 : 6525 : relname = PQgetvalue(res, relnum, i_relname);
5022 613 : 6525 : curr->relname = pg_strdup(relname);
614 : :
1453 rhaas@postgresql.org 615 : 6525 : curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
4603 bruce@momjian.us 616 : 6525 : curr->tblsp_alloc = false;
617 : :
618 : : /* Is the tablespace oid non-default? */
5276 619 [ + + ]: 6525 : if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
620 : : {
417 nathan@postgresql.or 621 : 9 : char *spcloc = PQgetvalue(res, relnum, i_spclocation);
622 [ + - + - ]: 9 : bool inplace = spcloc[0] && !is_absolute_path(spcloc);
623 : :
624 : : /*
625 : : * The tablespace location might be "", meaning the cluster
626 : : * default location, i.e. pg_default or pg_global. For in-place
627 : : * tablespaces, pg_tablespace_location() returns a path relative
628 : : * to the data directory.
629 : : */
630 [ + - ]: 9 : if (inplace)
631 : 9 : tablespace = psprintf("%s/%s",
632 : 9 : os_info.running_cluster->pgdata,
633 : : spcloc);
634 : : else
417 nathan@postgresql.or 635 :UBC 0 : tablespace = spcloc;
636 : :
637 : : /* Can we reuse the previous string allocation? */
4603 bruce@momjian.us 638 [ - + - - ]:CBC 9 : if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
4603 bruce@momjian.us 639 :UBC 0 : curr->tablespace = last_tablespace;
640 : : else
641 : : {
4603 bruce@momjian.us 642 :CBC 9 : last_tablespace = curr->tablespace = pg_strdup(tablespace);
643 : 9 : curr->tblsp_alloc = true;
644 : : }
645 : :
646 : : /* Free palloc'd string for in-place tablespaces. */
417 nathan@postgresql.or 647 [ + - ]: 9 : if (inplace)
648 : 9 : pfree(tablespace);
649 : : }
650 : : else
651 : : /* A zero reltablespace oid indicates the database tablespace. */
4603 bruce@momjian.us 652 : 6516 : curr->tablespace = dbinfo->db_tablespace;
653 : : }
654 : :
5738 655 : 126 : dbinfo->rel_arr.rels = relinfos;
656 : 126 : dbinfo->rel_arr.nrels = num_rels;
5975 657 : 126 : }
658 : :
659 : : /*
660 : : * get_old_cluster_logical_slot_infos_query()
661 : : *
662 : : * Returns the query for retrieving the logical slot information for all the
663 : : * logical replication slots in the database, for use by
664 : : * get_db_rel_and_slot_infos()'s UpgradeTask. The status of each logical slot
665 : : * is checked in check_old_cluster_for_valid_slots().
666 : : */
667 : : static const char *
228 msawada@postgresql.o 668 : 19 : get_old_cluster_logical_slot_infos_query(ClusterInfo *cluster)
669 : : {
670 : : /*
671 : : * Fetch the logical replication slot information. The check whether the
672 : : * slot is considered caught up is done by an upgrade function. This
673 : : * regards the slot as caught up if we don't find any decodable changes.
674 : : * The implementation of this check varies depending on the server
675 : : * version.
676 : : *
677 : : * We intentionally skip checking the WALs for invalidated slots as the
678 : : * corresponding WALs could have been removed for such slots.
679 : : *
680 : : * The temporary slots are explicitly ignored while checking because such
681 : : * slots cannot exist after the upgrade. During the upgrade, clusters are
682 : : * started and stopped several times causing any temporary slots to be
683 : : * removed.
684 : : */
685 : :
686 [ + + ]: 19 : if (user_opts.live_check)
687 : : {
688 : : /*
689 : : * We skip the caught-up check during live_check. We cannot verify
690 : : * whether the slot is caught up in this mode, as new WAL records
691 : : * could be generated concurrently.
692 : : */
228 msawada@postgresql.o 693 :GBC 1 : return "SELECT slot_name, plugin, two_phase, failover, "
694 : : "FALSE as caught_up, "
695 : : "invalidation_reason IS NOT NULL as invalid "
696 : : "FROM pg_catalog.pg_replication_slots "
697 : : "WHERE slot_type = 'logical' AND "
698 : : "database = current_database() AND "
699 : : "temporary IS FALSE";
700 : : }
228 msawada@postgresql.o 701 [ + - ]:CBC 18 : else if (GET_MAJOR_VERSION(cluster->major_version) >= 1900)
702 : : {
703 : : /*
704 : : * For PG19 and later, we optimize the slot caught-up check to avoid
705 : : * reading the same WAL stream multiple times: execute the caught-up
706 : : * check only for the slot with the minimum confirmed_flush_lsn, and
707 : : * apply the same result to all other slots in the same database. This
708 : : * limits the check to at most one logical slot per database. We also
709 : : * use the maximum confirmed_flush_lsn among all logical slots on the
710 : : * database as an early scan cutoff; finding a decodable WAL record
711 : : * beyond this point implies that no slot has caught up.
712 : : *
713 : : * Note that we don't distinguish slots based on their output plugin.
714 : : * If a plugin applies replication origin filters, we might get a
715 : : * false positive (i.e., erroneously considering a slot caught up).
716 : : * However, such cases are very rare, and the impact of a false
717 : : * positive is minimal.
718 : : */
719 : 18 : return "WITH check_caught_up AS ( "
720 : : " SELECT pg_catalog.binary_upgrade_check_logical_slot_pending_wal(slot_name, "
721 : : " MAX(confirmed_flush_lsn) OVER ()) as last_pending_wal "
722 : : " FROM pg_replication_slots "
723 : : " WHERE slot_type = 'logical' AND "
724 : : " database = current_database() AND "
725 : : " temporary IS FALSE AND "
726 : : " invalidation_reason IS NULL "
727 : : " ORDER BY confirmed_flush_lsn ASC "
728 : : " LIMIT 1 "
729 : : ") "
730 : : "SELECT slot_name, plugin, two_phase, failover, "
731 : : "CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
732 : : "ELSE last_pending_wal IS NULL OR "
733 : : " confirmed_flush_lsn > last_pending_wal "
734 : : "END as caught_up, "
735 : : "invalidation_reason IS NOT NULL as invalid "
736 : : "FROM pg_catalog.pg_replication_slots "
737 : : "LEFT JOIN check_caught_up ON true "
738 : : "WHERE slot_type = 'logical' AND "
739 : : "database = current_database() AND "
740 : : "temporary IS FALSE ";
741 : : }
742 : :
743 : : /*
744 : : * For PG18 and earlier, we call
745 : : * binary_upgrade_logical_slot_has_caught_up() for each logical slot.
746 : : */
228 msawada@postgresql.o 747 :UBC 0 : return "SELECT slot_name, plugin, two_phase, failover, "
748 : : "CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
749 : : "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
750 : : "END as caught_up, "
751 : : "invalidation_reason IS NOT NULL as invalid "
752 : : "FROM pg_catalog.pg_replication_slots "
753 : : "WHERE slot_type = 'logical' AND "
754 : : "database = current_database() AND "
755 : : "temporary IS FALSE ";
756 : : }
757 : :
758 : : /*
759 : : * Callback function for processing results of the query, which is used for
760 : : * get_db_rel_and_slot_infos()'s UpgradeTask. This function stores the logical
761 : : * slot information for later use.
762 : : */
763 : : static void
734 nathan@postgresql.or 764 :CBC 60 : process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg)
765 : : {
766 : 60 : LogicalSlotInfo *slotinfos = NULL;
767 : 60 : int num_slots = PQntuples(res);
768 : :
1060 akapila@postgresql.o 769 [ + + ]: 60 : if (num_slots)
770 : : {
771 : : int i_slotname;
772 : : int i_plugin;
773 : : int i_twophase;
774 : : int i_failover;
775 : : int i_caught_up;
776 : : int i_invalid;
777 : :
205 michael@paquier.xyz 778 : 4 : slotinfos = pg_malloc_array(LogicalSlotInfo, num_slots);
779 : :
1060 akapila@postgresql.o 780 : 4 : i_slotname = PQfnumber(res, "slot_name");
781 : 4 : i_plugin = PQfnumber(res, "plugin");
782 : 4 : i_twophase = PQfnumber(res, "two_phase");
969 783 : 4 : i_failover = PQfnumber(res, "failover");
1060 784 : 4 : i_caught_up = PQfnumber(res, "caught_up");
785 : 4 : i_invalid = PQfnumber(res, "invalid");
786 : :
787 [ + + ]: 14 : for (int slotnum = 0; slotnum < num_slots; slotnum++)
788 : : {
789 : 10 : LogicalSlotInfo *curr = &slotinfos[slotnum];
790 : :
791 : 10 : curr->slotname = pg_strdup(PQgetvalue(res, slotnum, i_slotname));
792 : 10 : curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin));
793 : 10 : curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0);
969 794 : 10 : curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0);
1060 795 : 10 : curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0);
796 : 10 : curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0);
797 : : }
798 : : }
799 : :
800 : 60 : dbinfo->slot_arr.slots = slotinfos;
801 : 60 : dbinfo->slot_arr.nslots = num_slots;
802 : 60 : }
803 : :
804 : :
805 : : /*
806 : : * count_old_cluster_logical_slots()
807 : : *
808 : : * Returns the number of logical replication slots for all databases.
809 : : *
810 : : * Note: this function always returns 0 if the old_cluster is PG16 and prior
811 : : * because we gather slot information only for cluster versions greater than or
812 : : * equal to PG17. See get_db_rel_and_slot_infos().
813 : : */
814 : : int
815 : 45 : count_old_cluster_logical_slots(void)
816 : : {
817 : 45 : int slot_count = 0;
818 : :
819 [ + + ]: 189 : for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
820 : 144 : slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots;
821 : :
822 : 45 : return slot_count;
823 : : }
824 : :
825 : : /*
826 : : * get_subscription_info()
827 : : *
828 : : * Gets the information of subscriptions in the cluster.
829 : : */
830 : : void
424 831 : 18 : get_subscription_info(ClusterInfo *cluster)
832 : : {
833 : : PGconn *conn;
834 : : PGresult *res;
835 : : int i_nsub;
836 : : int i_retain_dead_tuples;
837 : :
788 nathan@postgresql.or 838 : 18 : conn = connectToServer(cluster, "template1");
424 akapila@postgresql.o 839 [ + - ]: 18 : if (GET_MAJOR_VERSION(cluster->major_version) >= 1900)
840 : 18 : res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
841 : : "COUNT(CASE WHEN subretaindeadtuples THEN 1 END) > 0 AS retain_dead_tuples "
842 : : "FROM pg_catalog.pg_subscription");
843 : : else
424 akapila@postgresql.o 844 :UBC 0 : res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
845 : : "'f' AS retain_dead_tuples "
846 : : "FROM pg_catalog.pg_subscription");
847 : :
424 akapila@postgresql.o 848 :CBC 18 : i_nsub = PQfnumber(res, "nsub");
849 : 18 : i_retain_dead_tuples = PQfnumber(res, "retain_dead_tuples");
850 : :
851 : 18 : cluster->nsubs = atoi(PQgetvalue(res, 0, i_nsub));
852 : 18 : cluster->sub_retain_dead_tuples = (strcmp(PQgetvalue(res, 0, i_retain_dead_tuples), "t") == 0);
853 : :
992 854 : 18 : PQclear(res);
855 : 18 : PQfinish(conn);
856 : 18 : }
857 : :
858 : : static void
5732 bruce@momjian.us 859 : 10 : free_db_and_rel_infos(DbInfoArr *db_arr)
860 : : {
861 : : int dbnum;
862 : :
5975 863 [ + + ]: 30 : for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
864 : : {
5732 865 : 20 : free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
5022 866 : 20 : pg_free(db_arr->dbs[dbnum].db_name);
867 : : }
5732 868 : 10 : pg_free(db_arr->dbs);
5696 869 : 10 : db_arr->dbs = NULL;
5975 870 : 10 : db_arr->ndbs = 0;
871 : 10 : }
872 : :
873 : :
874 : : static void
5732 875 : 20 : free_rel_infos(RelInfoArr *rel_arr)
876 : : {
877 : : int relnum;
878 : :
5022 879 [ + + ]: 100 : for (relnum = 0; relnum < rel_arr->nrels; relnum++)
880 : : {
4603 881 [ + + ]: 80 : if (rel_arr->rels[relnum].nsp_alloc)
882 : 20 : pg_free(rel_arr->rels[relnum].nspname);
5022 883 : 80 : pg_free(rel_arr->rels[relnum].relname);
4603 884 [ - + ]: 80 : if (rel_arr->rels[relnum].tblsp_alloc)
4603 bruce@momjian.us 885 :UBC 0 : pg_free(rel_arr->rels[relnum].tablespace);
886 : : }
5732 bruce@momjian.us 887 :CBC 20 : pg_free(rel_arr->rels);
888 : 20 : rel_arr->nrels = 0;
889 : 20 : }
890 : :
891 : :
892 : : static void
5732 bruce@momjian.us 893 :UBC 0 : print_db_infos(DbInfoArr *db_arr)
894 : : {
895 : : int dbnum;
896 : :
897 [ # # ]: 0 : for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
898 : : {
1060 akapila@postgresql.o 899 : 0 : DbInfo *pDbInfo = &db_arr->dbs[dbnum];
900 : :
901 : 0 : pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
902 : 0 : print_rel_infos(&pDbInfo->rel_arr);
903 : 0 : print_slot_infos(&pDbInfo->slot_arr);
904 : : }
5975 bruce@momjian.us 905 : 0 : }
906 : :
907 : :
908 : : static void
5022 909 : 0 : print_rel_infos(RelInfoArr *rel_arr)
910 : : {
911 : : int relnum;
912 : :
913 [ # # ]: 0 : for (relnum = 0; relnum < rel_arr->nrels; relnum++)
1129 michael@paquier.xyz 914 : 0 : pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
4859 sfrost@snowman.net 915 : 0 : rel_arr->rels[relnum].nspname,
916 : 0 : rel_arr->rels[relnum].relname,
917 : 0 : rel_arr->rels[relnum].reloid,
918 : 0 : rel_arr->rels[relnum].tablespace);
5975 bruce@momjian.us 919 : 0 : }
920 : :
921 : : static void
1060 akapila@postgresql.o 922 : 0 : print_slot_infos(LogicalSlotInfoArr *slot_arr)
923 : : {
924 : : /* Quick return if there are no logical slots. */
925 [ # # ]: 0 : if (slot_arr->nslots == 0)
926 : 0 : return;
927 : :
755 peter@eisentraut.org 928 : 0 : pg_log(PG_VERBOSE, "Logical replication slots in the database:");
929 : :
1060 akapila@postgresql.o 930 [ # # ]: 0 : for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
931 : : {
932 : 0 : LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
933 : :
755 peter@eisentraut.org 934 : 0 : pg_log(PG_VERBOSE, "slot name: \"%s\", output plugin: \"%s\", two_phase: %s",
935 : : slot_info->slotname,
936 : : slot_info->plugin,
1060 akapila@postgresql.o 937 [ # # ]: 0 : slot_info->two_phase ? "true" : "false");
938 : : }
939 : : }
|