PostgreSQL Source Code git master
Loading...
Searching...
No Matches
info.c File Reference
#include "postgres_fe.h"
#include "access/transam.h"
#include "catalog/pg_class_d.h"
#include "pg_upgrade.h"
#include "pqexpbuffer.h"
Include dependency graph for info.c:

Go to the source code of this file.

Functions

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)
 
static void report_unmatched_relation (const RelInfo *rel, const DbInfo *db, bool is_new_db)
 
static void free_db_and_rel_infos (DbInfoArr *db_arr)
 
static void get_template0_info (ClusterInfo *cluster)
 
static void get_db_infos (ClusterInfo *cluster)
 
static charget_rel_infos_query (void)
 
static void process_rel_infos (DbInfo *dbinfo, PGresult *res, void *arg)
 
static void free_rel_infos (RelInfoArr *rel_arr)
 
static void print_db_infos (DbInfoArr *db_arr)
 
static void print_rel_infos (RelInfoArr *rel_arr)
 
static void print_slot_infos (LogicalSlotInfoArr *slot_arr)
 
static const charget_old_cluster_logical_slot_infos_query (ClusterInfo *cluster)
 
static void process_old_cluster_logical_slot_infos (DbInfo *dbinfo, PGresult *res, void *arg)
 
FileNameMapgen_db_file_maps (DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
 
void get_db_rel_and_slot_infos (ClusterInfo *cluster)
 
int count_old_cluster_logical_slots (void)
 
void get_subscription_info (ClusterInfo *cluster)
 

Function Documentation

◆ count_old_cluster_logical_slots()

int count_old_cluster_logical_slots ( void  )

Definition at line 825 of file info.c.

826{
827 int slot_count = 0;
828
829 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
831
832 return slot_count;
833}
ClusterInfo old_cluster
Definition pg_upgrade.c:73
static int fb(int x)
DbInfoArr dbarr
Definition pg_upgrade.h:298
DbInfo * dbs
Definition pg_upgrade.h:227
LogicalSlotInfoArr slot_arr
Definition pg_upgrade.h:210

References ClusterInfo::dbarr, DbInfoArr::dbs, fb(), DbInfoArr::ndbs, LogicalSlotInfoArr::nslots, old_cluster, and DbInfo::slot_arr.

Referenced by check_new_cluster_replication_slots(), get_loadable_libraries(), and main().

◆ create_rel_filename_map()

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 
)
static

Definition at line 165 of file info.c.

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}
ClusterInfo new_cluster
Definition pg_upgrade.c:74
const char * tablespace_suffix
Definition pg_upgrade.h:312
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

References FileNameMap::db_oid, fb(), new_cluster, FileNameMap::new_tablespace, FileNameMap::new_tablespace_suffix, FileNameMap::nspname, old_cluster, FileNameMap::old_tablespace, FileNameMap::old_tablespace_suffix, FileNameMap::relfilenumber, FileNameMap::relname, and ClusterInfo::tablespace_suffix.

Referenced by gen_db_file_maps().

◆ free_db_and_rel_infos()

static void free_db_and_rel_infos ( DbInfoArr db_arr)
static

Definition at line 869 of file info.c.

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}
void pg_free(void *ptr)
static void free_rel_infos(RelInfoArr *rel_arr)
Definition info.c:885

References fb(), free_rel_infos(), and pg_free().

Referenced by get_db_rel_and_slot_infos().

◆ free_rel_infos()

static void free_rel_infos ( RelInfoArr rel_arr)
static

Definition at line 885 of file info.c.

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}
RelInfo * rels
Definition pg_upgrade.h:160
bool tblsp_alloc
Definition pg_upgrade.h:155
char * nspname
Definition pg_upgrade.h:147
char * tablespace
Definition pg_upgrade.h:153
bool nsp_alloc
Definition pg_upgrade.h:154
char * relname
Definition pg_upgrade.h:148

References fb(), RelInfoArr::nrels, RelInfo::nsp_alloc, RelInfo::nspname, pg_free(), RelInfo::relname, RelInfoArr::rels, RelInfo::tablespace, and RelInfo::tblsp_alloc.

Referenced by free_db_and_rel_infos().

◆ gen_db_file_maps()

FileNameMap * gen_db_file_maps ( DbInfo old_db,
DbInfo new_db,
int nmaps,
const char old_pgdata,
const char new_pgdata 
)

Definition at line 45 of file info.c.

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}
void * pg_malloc(size_t size)
Definition fe_memutils.c:47
static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
Definition info.c:214
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
#define pg_fatal(...)
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ PG_WARNING
Definition pg_upgrade.h:284
static const pg_conv_map maps[]

References create_rel_filename_map(), fb(), maps, pg_fatal, pg_log(), pg_malloc(), PG_WARNING, and report_unmatched_relation().

Referenced by transfer_all_new_dbs().

◆ get_db_infos()

static void get_db_infos ( ClusterInfo cluster)
static

Definition at line 398 of file info.c.

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}
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition cluster.c:107
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_malloc0(size_t size)
Definition fe_memutils.c:53
#define PQgetvalue
#define PQclear
#define PQntuples
static struct LogicalRepInfos dbinfos
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition server.c:28
#define QUERY_ALLOC
Definition pg_upgrade.h:23
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define GET_MAJOR_VERSION(v)
Definition pg_upgrade.h:27
#define is_absolute_path(filename)
Definition port.h:104
#define snprintf
Definition port.h:260
#define atooid(x)
PGconn * conn
Definition streamutil.c:52

References atooid, cluster(), conn, connectToServer(), dbinfos, executeQueryOrDie(), fb(), GET_MAJOR_VERSION, is_absolute_path, pg_malloc0(), pg_strdup(), PQclear, PQfinish(), PQfnumber(), PQgetvalue, PQntuples, QUERY_ALLOC, and snprintf.

Referenced by get_db_rel_and_slot_infos().

◆ get_db_rel_and_slot_infos()

void get_db_rel_and_slot_infos ( ClusterInfo cluster)

Definition at line 280 of file info.c.

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}
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
static void get_db_infos(ClusterInfo *cluster)
Definition info.c:398
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
UpgradeTask * upgrade_task_create(void)
Definition task.c:117
void upgrade_task_run(const UpgradeTask *task, const ClusterInfo *cluster)
Definition task.c:419
LogOpts log_opts
Definition util.c:17
void upgrade_task_free(UpgradeTask *task)
Definition task.c:133
@ PG_VERBOSE
Definition pg_upgrade.h:280
void upgrade_task_add_step(UpgradeTask *task, const char *query, UpgradeTaskProcessCB process_cb, bool free_result, void *arg)
Definition task.c:151
bool verbose
Definition pg_upgrade.h:325

References cluster(), fb(), free_db_and_rel_infos(), get_db_infos(), GET_MAJOR_VERSION, get_old_cluster_logical_slot_infos_query(), get_rel_infos_query(), get_template0_info(), log_opts, old_cluster, pg_free(), pg_log(), PG_VERBOSE, print_db_infos(), process_old_cluster_logical_slot_infos(), process_rel_infos(), upgrade_task_add_step(), upgrade_task_create(), upgrade_task_free(), upgrade_task_run(), and LogOpts::verbose.

Referenced by check_and_dump_old_cluster(), check_new_cluster(), and create_new_objects().

◆ get_old_cluster_logical_slot_infos_query()

static const char * get_old_cluster_logical_slot_infos_query ( ClusterInfo cluster)
static

Definition at line 679 of file info.c.

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}
UserOpts user_opts
Definition option.c:30
bool live_check
Definition pg_upgrade.h:342

References cluster(), GET_MAJOR_VERSION, UserOpts::live_check, and user_opts.

Referenced by get_db_rel_and_slot_infos().

◆ get_rel_infos_query()

static char * get_rel_infos_query ( void  )
static

Definition at line 481 of file info.c.

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) ))), ",
518 (GET_MAJOR_VERSION(old_cluster.major_version) >= 1600) ?
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}
#define CppAsString2(x)
Definition c.h:428
@ TRANSFER_MODE_SWAP
Definition pg_upgrade.h:272
void initPQExpBuffer(PQExpBuffer str)
Definition pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
transferMode transfer_mode
Definition pg_upgrade.h:344
#define FirstNormalObjectId
Definition transam.h:197

References appendPQExpBuffer(), appendPQExpBufferStr(), CppAsString2, PQExpBufferData::data, fb(), FirstNormalObjectId, GET_MAJOR_VERSION, initPQExpBuffer(), ClusterInfo::major_version, old_cluster, UserOpts::transfer_mode, TRANSFER_MODE_SWAP, and user_opts.

Referenced by get_db_rel_and_slot_infos().

◆ get_subscription_info()

void get_subscription_info ( ClusterInfo cluster)

Definition at line 841 of file info.c.

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}

References cluster(), conn, connectToServer(), executeQueryOrDie(), fb(), GET_MAJOR_VERSION, PQclear, PQfinish(), PQfnumber(), and PQgetvalue.

Referenced by check_and_dump_old_cluster().

◆ get_template0_info()

static void get_template0_info ( ClusterInfo cluster)
static

Definition at line 333 of file info.c.

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}
#define PQgetisnull
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

References cluster(), conn, connectToServer(), DbLocaleInfo::db_collate, DbLocaleInfo::db_collprovider, DbLocaleInfo::db_ctype, DbLocaleInfo::db_encoding, DbLocaleInfo::db_locale, executeQueryOrDie(), fb(), GET_MAJOR_VERSION, pg_fatal, pg_malloc(), pg_strdup(), PQclear, PQfinish(), PQfnumber(), PQgetisnull, PQgetvalue, and PQntuples.

Referenced by get_db_rel_and_slot_infos().

◆ print_db_infos()

static void print_db_infos ( DbInfoArr db_arr)
static

Definition at line 903 of file info.c.

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}
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
Definition info.c:932
static void print_rel_infos(RelInfoArr *rel_arr)
Definition info.c:919

References fb(), pg_log(), PG_VERBOSE, print_rel_infos(), and print_slot_infos().

Referenced by get_db_rel_and_slot_infos().

◆ print_rel_infos()

static void print_rel_infos ( RelInfoArr rel_arr)
static

Definition at line 919 of file info.c.

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}
Oid reloid
Definition pg_upgrade.h:149

References fb(), RelInfoArr::nrels, RelInfo::nspname, pg_log(), PG_VERBOSE, RelInfo::relname, RelInfo::reloid, RelInfoArr::rels, and RelInfo::tablespace.

Referenced by print_db_infos().

◆ print_slot_infos()

static void print_slot_infos ( LogicalSlotInfoArr slot_arr)
static

Definition at line 932 of file info.c.

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}
LogicalSlotInfo * slots
Definition pg_upgrade.h:181

References fb(), LogicalSlotInfoArr::nslots, pg_log(), PG_VERBOSE, and LogicalSlotInfoArr::slots.

Referenced by print_db_infos().

◆ process_old_cluster_logical_slot_infos()

static void process_old_cluster_logical_slot_infos ( DbInfo dbinfo,
PGresult res,
void arg 
)
static

Definition at line 774 of file info.c.

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}

References fb(), LogicalSlotInfoArr::nslots, pg_malloc(), pg_strdup(), PQfnumber(), PQgetvalue, PQntuples, DbInfo::slot_arr, LogicalSlotInfo::slotname, and LogicalSlotInfoArr::slots.

Referenced by get_db_rel_and_slot_infos().

◆ process_rel_infos()

static void process_rel_infos ( DbInfo dbinfo,
PGresult res,
void arg 
)
static

Definition at line 579 of file info.c.

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}
void pfree(void *pointer)
Definition mcxt.c:1616
NameData relname
Definition pg_class.h:38
OSInfo os_info
Definition pg_upgrade.c:75
static char * tablespace
Definition pgbench.c:217
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
char * pgdata
Definition pg_upgrade.h:299
char db_tablespace[MAXPGPATH]
Definition pg_upgrade.h:207
RelInfoArr rel_arr
Definition pg_upgrade.h:209
ClusterInfo * running_cluster
Definition pg_upgrade.h:370

References atooid, DbInfo::db_tablespace, fb(), is_absolute_path, RelInfoArr::nrels, os_info, pfree(), pg_malloc(), pg_strdup(), ClusterInfo::pgdata, PQfnumber(), PQgetvalue, PQntuples, psprintf(), DbInfo::rel_arr, relname, RelInfo::reloid, RelInfoArr::rels, OSInfo::running_cluster, and tablespace.

Referenced by get_db_rel_and_slot_infos().

◆ report_unmatched_relation()

static void report_unmatched_relation ( const RelInfo rel,
const DbInfo db,
bool  is_new_db 
)
static

Definition at line 214 of file info.c.

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}
#define _(x)
Definition elog.c:91
int i
Definition isn.c:77
unsigned int Oid
char * db_name
Definition pg_upgrade.h:206
Oid toastheap
Definition pg_upgrade.h:152
Oid indtable
Definition pg_upgrade.h:151

References _, DbInfo::db_name, fb(), i, RelInfo::indtable, RelInfoArr::nrels, RelInfo::nspname, pg_log(), PG_WARNING, DbInfo::rel_arr, RelInfo::relname, RelInfo::reloid, RelInfoArr::rels, snprintf, and RelInfo::toastheap.

Referenced by gen_db_file_maps().