PostgreSQL Source Code git master
Loading...
Searching...
No Matches
info.c
Go to the documentation of this file.
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
17static 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);
21static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db,
22 bool is_new_db);
25static void get_db_infos(ClusterInfo *cluster);
26static char *get_rel_infos_query(void);
27static void process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg);
28static void free_rel_infos(RelInfoArr *rel_arr);
29static void print_db_infos(DbInfoArr *db_arr);
30static void print_rel_infos(RelInfoArr *rel_arr);
31static void print_slot_infos(LogicalSlotInfoArr *slot_arr);
33static 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 */
46 int *nmaps,
47 const char *old_pgdata, const char *new_pgdata)
48{
50 int old_relnum,
52 int num_maps = 0;
53 bool all_matched = true;
54
55 /* There will certainly not be more mappings than there are old rels */
56 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 */
64 while (old_relnum < old_db->rel_arr.nrels ||
65 new_relnum < new_db->rel_arr.nrels)
66 {
67 RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
68 &old_db->rel_arr.rels[old_relnum] : NULL;
69 RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
70 &new_db->rel_arr.rels[new_relnum] : NULL;
71
72 /* handle running off one array before the other */
73 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 */
80 all_matched = false;
81 old_relnum++;
82 continue;
83 }
84 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 if (strcmp(new_rel->nspname, "pg_toast") != 0)
93 {
95 all_matched = false;
96 }
97 new_relnum++;
98 continue;
99 }
100
101 /* check for mismatched OID */
102 if (old_rel->reloid < new_rel->reloid)
103 {
104 /* old_rel is unmatched, see comment above */
106 all_matched = false;
107 old_relnum++;
108 continue;
109 }
110 else if (old_rel->reloid > new_rel->reloid)
111 {
112 /* new_rel is unmatched, see comment above */
113 if (strcmp(new_rel->nspname, "pg_toast") != 0)
114 {
116 all_matched = false;
117 }
118 new_relnum++;
119 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 if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
128 strcmp(old_rel->relname, new_rel->relname) != 0)
129 {
130 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 all_matched = false;
136 old_relnum++;
137 new_relnum++;
138 continue;
139 }
140
141 /* OK, create a mapping entry */
144 num_maps++;
145 old_relnum++;
146 new_relnum++;
147 }
148
149 if (!all_matched)
150 pg_fatal("Failed to match up old and new tables in database \"%s\"",
151 old_db->db_name);
152
153 *nmaps = num_maps;
154 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 */
163static void
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 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 */
177 map->old_tablespace_suffix = "/base";
178 }
179 else
180 {
181 /* relation belongs to a tablespace, so use the tablespace location */
182 map->old_tablespace = old_rel->tablespace;
184 }
185
186 /* Do the same for new tablespaces */
187 if (strlen(new_rel->tablespace) == 0)
188 {
190 map->new_tablespace_suffix = "/base";
191 }
192 else
193 {
194 map->new_tablespace = new_rel->tablespace;
196 }
197
198 /* DB oid and relfilenumbers are preserved between old and new cluster */
199 map->db_oid = old_db->db_oid;
200 map->relfilenumber = old_rel->relfilenumber;
201
202 /* used only for logging and error reporting, old/new are identical */
203 map->nspname = old_rel->nspname;
204 map->relname = old_rel->relname;
205}
206
207
208/*
209 * Complain about a relation we couldn't match to the other database,
210 * identifying it as best we can.
211 */
212static void
214{
215 Oid reloid = rel->reloid; /* we might change rel below */
216 char reldesc[1000];
217 int i;
218
219 snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
220 rel->nspname, rel->relname);
221 if (rel->indtable)
222 {
223 for (i = 0; i < db->rel_arr.nrels; i++)
224 {
225 const RelInfo *hrel = &db->rel_arr.rels[i];
226
227 if (hrel->reloid == rel->indtable)
228 {
229 snprintf(reldesc + strlen(reldesc),
230 sizeof(reldesc) - strlen(reldesc),
231 _(" which is an index on \"%s.%s\""),
232 hrel->nspname, hrel->relname);
233 /* Shift attention to index's table for toast check */
234 rel = hrel;
235 break;
236 }
237 }
238 if (i >= db->rel_arr.nrels)
239 snprintf(reldesc + strlen(reldesc),
240 sizeof(reldesc) - strlen(reldesc),
241 _(" which is an index on OID %u"), rel->indtable);
242 }
243 if (rel->toastheap)
244 {
245 for (i = 0; i < db->rel_arr.nrels; i++)
246 {
247 const RelInfo *brel = &db->rel_arr.rels[i];
248
249 if (brel->reloid == rel->toastheap)
250 {
251 snprintf(reldesc + strlen(reldesc),
252 sizeof(reldesc) - strlen(reldesc),
253 _(" which is the TOAST table for \"%s.%s\""),
254 brel->nspname, brel->relname);
255 break;
256 }
257 }
258 if (i >= db->rel_arr.nrels)
259 snprintf(reldesc + strlen(reldesc),
260 sizeof(reldesc) - strlen(reldesc),
261 _(" which is the TOAST table for OID %u"), rel->toastheap);
262 }
263
264 if (is_new_db)
265 pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s",
266 reloid, db->db_name, reldesc);
267 else
268 pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s",
269 reloid, db->db_name, reldesc);
270}
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 */
278void
280{
282 char *rel_infos_query = NULL;
283
284 if (cluster->dbarr.dbs != NULL)
286
289
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 if (cluster == &old_cluster &&
306 GET_MAJOR_VERSION(cluster->major_version) > 1600)
310 true, NULL);
311
314
316
317 if (cluster == &old_cluster)
318 pg_log(PG_VERBOSE, "\nsource databases:");
319 else
320 pg_log(PG_VERBOSE, "\ntarget databases:");
321
322 if (log_opts.verbose)
323 print_db_infos(&cluster->dbarr);
324}
325
326
327/*
328 * Get information about template0, which will be copied from the old cluster
329 * to the new cluster.
330 */
331static void
333{
334 PGconn *conn = connectToServer(cluster, "template1");
335 DbLocaleInfo *locale;
337 int i_datencoding;
339 int i_datcollate;
340 int i_datctype;
341 int i_datlocale;
342
343 if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
345 "SELECT encoding, datlocprovider, "
346 " datcollate, datctype, datlocale "
347 "FROM pg_catalog.pg_database "
348 "WHERE datname='template0'");
349 else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
351 "SELECT encoding, datlocprovider, "
352 " datcollate, datctype, daticulocale AS datlocale "
353 "FROM pg_catalog.pg_database "
354 "WHERE datname='template0'");
355 else
357 "SELECT encoding, 'c' AS datlocprovider, "
358 " datcollate, datctype, NULL AS datlocale "
359 "FROM pg_catalog.pg_database "
360 "WHERE datname='template0'");
361
362
363 if (PQntuples(dbres) != 1)
364 pg_fatal("template0 not found");
365
367
368 i_datencoding = PQfnumber(dbres, "encoding");
369 i_datlocprovider = PQfnumber(dbres, "datlocprovider");
370 i_datcollate = PQfnumber(dbres, "datcollate");
371 i_datctype = PQfnumber(dbres, "datctype");
372 i_datlocale = PQfnumber(dbres, "datlocale");
373
379 locale->db_locale = NULL;
380 else
382
383 cluster->template0 = locale;
384
385 PQclear(dbres);
386 PQfinish(conn);
387}
388
389
390/*
391 * get_db_infos()
392 *
393 * Scans pg_database system catalog and populates all user
394 * databases.
395 */
396static void
398{
399 PGconn *conn = connectToServer(cluster, "template1");
400 PGresult *res;
401 int ntups;
402 int tupnum;
404 int i_datname,
405 i_oid,
407 char query[QUERY_ALLOC];
408
409 snprintf(query, sizeof(query),
410 "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
411 if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
412 snprintf(query + strlen(query), sizeof(query) - strlen(query),
413 "datlocprovider, datlocale, ");
414 else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
415 snprintf(query + strlen(query), sizeof(query) - strlen(query),
416 "datlocprovider, daticulocale AS datlocale, ");
417 else
418 snprintf(query + strlen(query), sizeof(query) - strlen(query),
419 "'c' AS datlocprovider, NULL AS datlocale, ");
420 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
428 res = executeQueryOrDie(conn, "%s", query);
429
430 i_oid = PQfnumber(res, "oid");
431 i_datname = PQfnumber(res, "datname");
432 i_spclocation = PQfnumber(res, "spclocation");
433
434 ntups = PQntuples(res);
436
437 for (tupnum = 0; tupnum < ntups; tupnum++)
438 {
439 char *spcloc = PQgetvalue(res, tupnum, i_spclocation);
440 bool inplace = spcloc[0] && !is_absolute_path(spcloc);
441
442 dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
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 */
451 if (inplace)
452 snprintf(dbinfos[tupnum].db_tablespace,
453 sizeof(dbinfos[tupnum].db_tablespace),
454 "%s/%s", cluster->pgdata, spcloc);
455 else
456 snprintf(dbinfos[tupnum].db_tablespace,
457 sizeof(dbinfos[tupnum].db_tablespace),
458 "%s", spcloc);
459 }
460 PQclear(res);
461
462 PQfinish(conn);
463
464 cluster->dbarr.dbs = dbinfos;
465 cluster->dbarr.ndbs = ntups;
466}
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 */
479static char *
481{
482 PQExpBufferData query;
483
484 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 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) ))), ",
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 */
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 */
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 */
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 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 */
577static void
579{
580 int ntups = PQntuples(res);
582 int i_reloid = PQfnumber(res, "reloid");
583 int i_indtable = PQfnumber(res, "indtable");
584 int i_toastheap = PQfnumber(res, "toastheap");
585 int i_nspname = PQfnumber(res, "nspname");
586 int i_relname = PQfnumber(res, "relname");
587 int i_relfilenumber = PQfnumber(res, "relfilenode");
588 int i_reltablespace = PQfnumber(res, "reltablespace");
589 int i_spclocation = PQfnumber(res, "spclocation");
590 int num_rels = 0;
591 char *nspname = NULL;
592 char *relname = NULL;
593 char *tablespace = NULL;
594 char *last_namespace = NULL;
595 char *last_tablespace = NULL;
596
597 for (int relnum = 0; relnum < ntups; relnum++)
598 {
600
602 curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
603 curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
604
605 nspname = PQgetvalue(res, relnum, i_nspname);
606 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 if (last_namespace && strcmp(nspname, last_namespace) == 0)
615 curr->nspname = last_namespace;
616 else
617 {
618 last_namespace = curr->nspname = pg_strdup(nspname);
619 curr->nsp_alloc = true;
620 }
621
623 curr->relname = pg_strdup(relname);
624
625 curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
626 curr->tblsp_alloc = false;
627
628 /* Is the tablespace oid non-default? */
629 if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
630 {
631 char *spcloc = PQgetvalue(res, relnum, i_spclocation);
632 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 if (inplace)
641 tablespace = psprintf("%s/%s",
643 spcloc);
644 else
646
647 /* Can we reuse the previous string allocation? */
649 curr->tablespace = last_tablespace;
650 else
651 {
652 last_tablespace = curr->tablespace = pg_strdup(tablespace);
653 curr->tblsp_alloc = true;
654 }
655
656 /* Free palloc'd string for in-place tablespaces. */
657 if (inplace)
659 }
660 else
661 /* A zero reltablespace oid indicates the database tablespace. */
662 curr->tablespace = dbinfo->db_tablespace;
663 }
664
665 dbinfo->rel_arr.rels = relinfos;
666 dbinfo->rel_arr.nrels = num_rels;
667}
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 */
677static const char *
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
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 */
703 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 }
711 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 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 */
756 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 */
772static void
774{
776 int num_slots = PQntuples(res);
777
778 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
788
789 i_slotname = PQfnumber(res, "slot_name");
790 i_plugin = PQfnumber(res, "plugin");
791 i_twophase = PQfnumber(res, "two_phase");
792 i_failover = PQfnumber(res, "failover");
793 i_caught_up = PQfnumber(res, "caught_up");
794 i_invalid = PQfnumber(res, "invalid");
795
796 for (int slotnum = 0; slotnum < num_slots; slotnum++)
797 {
799
801 curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin));
802 curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0);
803 curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0);
804 curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0);
805 curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0);
806 }
807 }
808
809 dbinfo->slot_arr.slots = slotinfos;
810 dbinfo->slot_arr.nslots = num_slots;
811}
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 */
823int
825{
826 int slot_count = 0;
827
828 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
830
831 return slot_count;
832}
833
834/*
835 * get_subscription_info()
836 *
837 * Gets the information of subscriptions in the cluster.
838 */
839void
841{
842 PGconn *conn;
843 PGresult *res;
844 int i_nsub;
846
847 conn = connectToServer(cluster, "template1");
848 if (GET_MAJOR_VERSION(cluster->major_version) >= 1900)
849 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
853 res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
854 "'f' AS retain_dead_tuples "
855 "FROM pg_catalog.pg_subscription");
856
857 i_nsub = PQfnumber(res, "nsub");
858 i_retain_dead_tuples = PQfnumber(res, "retain_dead_tuples");
859
860 cluster->nsubs = atoi(PQgetvalue(res, 0, i_nsub));
861 cluster->sub_retain_dead_tuples = (strcmp(PQgetvalue(res, 0, i_retain_dead_tuples), "t") == 0);
862
863 PQclear(res);
864 PQfinish(conn);
865}
866
867static void
869{
870 int dbnum;
871
872 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
873 {
874 free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
875 pg_free(db_arr->dbs[dbnum].db_name);
876 }
877 pg_free(db_arr->dbs);
878 db_arr->dbs = NULL;
879 db_arr->ndbs = 0;
880}
881
882
883static void
885{
886 int relnum;
887
888 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
889 {
890 if (rel_arr->rels[relnum].nsp_alloc)
891 pg_free(rel_arr->rels[relnum].nspname);
892 pg_free(rel_arr->rels[relnum].relname);
893 if (rel_arr->rels[relnum].tblsp_alloc)
894 pg_free(rel_arr->rels[relnum].tablespace);
895 }
896 pg_free(rel_arr->rels);
897 rel_arr->nrels = 0;
898}
899
900
901static void
903{
904 int dbnum;
905
906 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
907 {
908 DbInfo *pDbInfo = &db_arr->dbs[dbnum];
909
910 pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
911 print_rel_infos(&pDbInfo->rel_arr);
912 print_slot_infos(&pDbInfo->slot_arr);
913 }
914}
915
916
917static void
919{
920 int relnum;
921
922 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
923 pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
924 rel_arr->rels[relnum].nspname,
925 rel_arr->rels[relnum].relname,
926 rel_arr->rels[relnum].reloid,
927 rel_arr->rels[relnum].tablespace);
928}
929
930static void
932{
933 /* Quick return if there are no logical slots. */
934 if (slot_arr->nslots == 0)
935 return;
936
937 pg_log(PG_VERBOSE, "Logical replication slots in the database:");
938
939 for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
940 {
941 LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
942
943 pg_log(PG_VERBOSE, "slot name: \"%s\", output plugin: \"%s\", two_phase: %s",
944 slot_info->slotname,
945 slot_info->plugin,
946 slot_info->two_phase ? "true" : "false");
947 }
948}
#define CppAsString2(x)
Definition c.h:440
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition cluster.c:107
Datum arg
Definition elog.c:1322
#define _(x)
Definition elog.c:95
void PQfinish(PGconn *conn)
int PQfnumber(const PGresult *res, const char *field_name)
Definition fe-exec.c:3606
char * pg_strdup(const char *in)
Definition fe_memutils.c:85
void pg_free(void *ptr)
#define pg_malloc_array(type, count)
Definition fe_memutils.h:56
#define pg_malloc_object(type)
Definition fe_memutils.h:50
#define pg_malloc0_array(type, count)
Definition fe_memutils.h:57
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
Definition info.c:931
static void process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg)
Definition info.c:578
static void get_template0_info(ClusterInfo *cluster)
Definition info.c:332
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
Definition info.c:45
static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
Definition info.c:213
static void get_db_infos(ClusterInfo *cluster)
Definition info.c:397
static void free_rel_infos(RelInfoArr *rel_arr)
Definition info.c:884
static void print_rel_infos(RelInfoArr *rel_arr)
Definition info.c:918
static void create_rel_filename_map(const char *old_data, const char *new_data, const DbInfo *old_db, const DbInfo *new_db, const RelInfo *old_rel, const RelInfo *new_rel, FileNameMap *map)
Definition info.c:164
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition info.c:868
static char * get_rel_infos_query(void)
Definition info.c:480
static void process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg)
Definition info.c:773
static const char * get_old_cluster_logical_slot_infos_query(ClusterInfo *cluster)
Definition info.c:678
static void print_db_infos(DbInfoArr *db_arr)
Definition info.c:902
void get_subscription_info(ClusterInfo *cluster)
Definition info.c:840
int count_old_cluster_logical_slots(void)
Definition info.c:824
void get_db_rel_and_slot_infos(ClusterInfo *cluster)
Definition info.c:279
int i
Definition isn.c:77
#define PQgetvalue
#define PQclear
#define PQgetisnull
#define PQntuples
void pfree(void *pointer)
Definition mcxt.c:1616
#define pg_fatal(...)
NameData relname
Definition pg_class.h:40
static struct LogicalRepInfos dbinfos
OSInfo os_info
Definition pg_upgrade.c:75
ClusterInfo new_cluster
Definition pg_upgrade.c:74
ClusterInfo old_cluster
Definition pg_upgrade.c:73
UpgradeTask * upgrade_task_create(void)
Definition task.c:117
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition server.c:28
void upgrade_task_run(const UpgradeTask *task, const ClusterInfo *cluster)
Definition task.c:421
#define QUERY_ALLOC
Definition pg_upgrade.h:23
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ TRANSFER_MODE_SWAP
Definition pg_upgrade.h:272
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
LogOpts log_opts
Definition util.c:17
void upgrade_task_free(UpgradeTask *task)
Definition task.c:133
@ PG_WARNING
Definition pg_upgrade.h:284
@ PG_VERBOSE
Definition pg_upgrade.h:280
#define GET_MAJOR_VERSION(v)
Definition pg_upgrade.h:27
void upgrade_task_add_step(UpgradeTask *task, const char *query, UpgradeTaskProcessCB process_cb, bool free_result, void *arg)
Definition task.c:151
static char * tablespace
Definition pgbench.c:217
#define is_absolute_path(filename)
Definition port.h:104
#define snprintf
Definition port.h:260
unsigned int Oid
#define atooid(x)
void initPQExpBuffer(PQExpBuffer str)
Definition pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
static int fb(int x)
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
UserOpts user_opts
Definition option.c:30
PGconn * conn
Definition streamutil.c:52
char * pgdata
Definition pg_upgrade.h:299
DbInfoArr dbarr
Definition pg_upgrade.h:298
uint32 major_version
Definition pg_upgrade.h:307
const char * tablespace_suffix
Definition pg_upgrade.h:312
DbInfo * dbs
Definition pg_upgrade.h:227
LogicalSlotInfoArr slot_arr
Definition pg_upgrade.h:210
char db_tablespace[MAXPGPATH]
Definition pg_upgrade.h:207
char * db_name
Definition pg_upgrade.h:206
RelInfoArr rel_arr
Definition pg_upgrade.h:209
char db_collprovider
Definition pg_upgrade.h:220
char * db_locale
Definition pg_upgrade.h:221
char * db_collate
Definition pg_upgrade.h:218
char * db_ctype
Definition pg_upgrade.h:219
const char * new_tablespace
Definition pg_upgrade.h:190
const char * old_tablespace_suffix
Definition pg_upgrade.h:191
const char * old_tablespace
Definition pg_upgrade.h:189
RelFileNumber relfilenumber
Definition pg_upgrade.h:194
char * relname
Definition pg_upgrade.h:197
char * nspname
Definition pg_upgrade.h:196
const char * new_tablespace_suffix
Definition pg_upgrade.h:192
bool verbose
Definition pg_upgrade.h:325
LogicalSlotInfo * slots
Definition pg_upgrade.h:181
ClusterInfo * running_cluster
Definition pg_upgrade.h:370
RelInfo * rels
Definition pg_upgrade.h:160
Oid toastheap
Definition pg_upgrade.h:152
bool tblsp_alloc
Definition pg_upgrade.h:155
Oid reloid
Definition pg_upgrade.h:149
char * nspname
Definition pg_upgrade.h:147
char * tablespace
Definition pg_upgrade.h:153
Oid indtable
Definition pg_upgrade.h:151
bool nsp_alloc
Definition pg_upgrade.h:154
char * relname
Definition pg_upgrade.h:148
bool live_check
Definition pg_upgrade.h:342
transferMode transfer_mode
Definition pg_upgrade.h:344
#define FirstNormalObjectId
Definition transam.h:197
static const pg_conv_map maps[]