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