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