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 charget_old_cluster_logical_slot_infos_query (void)
 
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 774 of file info.c.

775{
776 int slot_count = 0;
777
778 for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
780
781 return slot_count;
782}
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 818 of file info.c.

819{
820 int dbnum;
821
822 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
823 {
824 free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
825 pg_free(db_arr->dbs[dbnum].db_name);
826 }
827 pg_free(db_arr->dbs);
828 db_arr->dbs = NULL;
829 db_arr->ndbs = 0;
830}
void pg_free(void *ptr)
static void free_rel_infos(RelInfoArr *rel_arr)
Definition info.c:834

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 834 of file info.c.

835{
836 int relnum;
837
838 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
839 {
840 if (rel_arr->rels[relnum].nsp_alloc)
841 pg_free(rel_arr->rels[relnum].nspname);
842 pg_free(rel_arr->rels[relnum].relname);
843 if (rel_arr->rels[relnum].tblsp_alloc)
844 pg_free(rel_arr->rels[relnum].tablespace);
845 }
846 pg_free(rel_arr->rels);
847 rel_arr->nrels = 0;
848}
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 404 of file info.c.

405{
406 PGconn *conn = connectToServer(cluster, "template1");
407 PGresult *res;
408 int ntups;
409 int tupnum;
411 int i_datname,
412 i_oid,
414 char query[QUERY_ALLOC];
415
416 snprintf(query, sizeof(query),
417 "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
418 if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
419 snprintf(query + strlen(query), sizeof(query) - strlen(query),
420 "datlocprovider, datlocale, ");
421 else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
422 snprintf(query + strlen(query), sizeof(query) - strlen(query),
423 "datlocprovider, daticulocale AS datlocale, ");
424 else
425 snprintf(query + strlen(query), sizeof(query) - strlen(query),
426 "'c' AS datlocprovider, NULL AS datlocale, ");
427 snprintf(query + strlen(query), sizeof(query) - strlen(query),
428 "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
429 "FROM pg_catalog.pg_database d "
430 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
431 " ON d.dattablespace = t.oid "
432 "WHERE d.datallowconn = true "
433 "ORDER BY 1");
434
435 res = executeQueryOrDie(conn, "%s", query);
436
437 i_oid = PQfnumber(res, "oid");
438 i_datname = PQfnumber(res, "datname");
439 i_spclocation = PQfnumber(res, "spclocation");
440
441 ntups = PQntuples(res);
442 dbinfos = (DbInfo *) pg_malloc0(sizeof(DbInfo) * ntups);
443
444 for (tupnum = 0; tupnum < ntups; tupnum++)
445 {
446 char *spcloc = PQgetvalue(res, tupnum, i_spclocation);
447 bool inplace = spcloc[0] && !is_absolute_path(spcloc);
448
449 dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
451
452 /*
453 * The tablespace location might be "", meaning the cluster default
454 * location, i.e. pg_default or pg_global. For in-place tablespaces,
455 * pg_tablespace_location() returns a path relative to the data
456 * directory.
457 */
458 if (inplace)
459 snprintf(dbinfos[tupnum].db_tablespace,
460 sizeof(dbinfos[tupnum].db_tablespace),
461 "%s/%s", cluster->pgdata, spcloc);
462 else
463 snprintf(dbinfos[tupnum].db_tablespace,
464 sizeof(dbinfos[tupnum].db_tablespace),
465 "%s", spcloc);
466 }
467 PQclear(res);
468
469 PQfinish(conn);
470
471 cluster->dbarr.dbs = dbinfos;
472 cluster->dbarr.ndbs = ntups;
473}
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;
285
286 if (cluster->dbarr.dbs != NULL)
288
291
296 true, NULL);
297
298 /*
299 * Logical slots are only carried over to the new cluster when the old
300 * cluster is on PG17 or newer. This is because before that the logical
301 * slots are not saved at shutdown, so there is no guarantee that the
302 * latest confirmed_flush_lsn is saved to disk which can lead to data
303 * loss. It is still not guaranteed for manually created slots in PG17, so
304 * subsequent checks done in check_old_cluster_for_valid_slots() would
305 * raise a FATAL error if such slots are included.
306 */
307 if (cluster == &old_cluster &&
308 GET_MAJOR_VERSION(cluster->major_version) > 1600)
309 {
314 true, NULL);
315 }
316
319
323
324 if (cluster == &old_cluster)
325 pg_log(PG_VERBOSE, "\nsource databases:");
326 else
327 pg_log(PG_VERBOSE, "\ntarget databases:");
328
329 if (log_opts.verbose)
330 print_db_infos(&cluster->dbarr);
331}
static void process_rel_infos(DbInfo *dbinfo, PGresult *res, void *arg)
Definition info.c:585
static void get_template0_info(ClusterInfo *cluster)
Definition info.c:339
static void get_db_infos(ClusterInfo *cluster)
Definition info.c:404
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition info.c:818
static char * get_rel_infos_query(void)
Definition info.c:487
static void process_old_cluster_logical_slot_infos(DbInfo *dbinfo, PGresult *res, void *arg)
Definition info.c:723
static void print_db_infos(DbInfoArr *db_arr)
Definition info.c:852
static char * get_old_cluster_logical_slot_infos_query(void)
Definition info.c:685
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 char * get_old_cluster_logical_slot_infos_query ( void  )
static

Definition at line 685 of file info.c.

686{
687 /*
688 * Fetch the logical replication slot information. The check whether the
689 * slot is considered caught up is done by an upgrade function. This
690 * regards the slot as caught up if we don't find any decodable changes.
691 * See binary_upgrade_logical_slot_has_caught_up().
692 *
693 * Note that we can't ensure whether the slot is caught up during
694 * live_check as the new WAL records could be generated.
695 *
696 * We intentionally skip checking the WALs for invalidated slots as the
697 * corresponding WALs could have been removed for such slots.
698 *
699 * The temporary slots are explicitly ignored while checking because such
700 * slots cannot exist after the upgrade. During the upgrade, clusters are
701 * started and stopped several times causing any temporary slots to be
702 * removed.
703 */
704 return psprintf("SELECT slot_name, plugin, two_phase, failover, "
705 "%s as caught_up, 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 user_opts.live_check ? "FALSE" :
711 "(CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
712 "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
713 "END)");
714}
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
UserOpts user_opts
Definition option.c:30
bool live_check
Definition pg_upgrade.h:342

References UserOpts::live_check, psprintf(), 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 487 of file info.c.

488{
489 PQExpBufferData query;
490
491 initPQExpBuffer(&query);
492
493 /*
494 * Create a CTE that collects OIDs of regular user tables and matviews,
495 * but excluding toast tables and indexes. We assume that relations with
496 * OIDs >= FirstNormalObjectId belong to the user. (That's probably
497 * redundant with the namespace-name exclusions, but let's be safe.)
498 *
499 * pg_largeobject contains user data that does not appear in pg_dump
500 * output, so we have to copy that system table. It's easiest to do that
501 * by treating it as a user table. We can do the same for
502 * pg_largeobject_metadata for upgrades from v16 and newer. pg_upgrade
503 * can't copy/link the files from older versions because aclitem (needed
504 * by pg_largeobject_metadata.lomacl) changed its storage format in v16.
505 */
506 appendPQExpBuffer(&query,
507 "WITH regular_heap (reloid, indtable, toastheap) AS ( "
508 " SELECT c.oid, 0::oid, 0::oid "
509 " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
510 " ON c.relnamespace = n.oid "
511 " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
512 CppAsString2(RELKIND_MATVIEW) "%s) AND "
513 /* exclude possible orphaned temp tables */
514 " ((n.nspname !~ '^pg_temp_' AND "
515 " n.nspname !~ '^pg_toast_temp_' AND "
516 " n.nspname NOT IN ('pg_catalog', 'information_schema', "
517 " 'binary_upgrade', 'pg_toast') AND "
518 " c.oid >= %u::pg_catalog.oid) OR "
519 " (n.nspname = 'pg_catalog' AND "
520 " relname IN ('pg_largeobject'%s) ))), ",
524 (GET_MAJOR_VERSION(old_cluster.major_version) >= 1600) ?
525 ", 'pg_largeobject_metadata'" : "");
526
527 /*
528 * Add a CTE that collects OIDs of toast tables belonging to the tables
529 * selected by the regular_heap CTE. (We have to do this separately
530 * because the namespace-name rules above don't work for toast tables.)
531 */
533 " toast_heap (reloid, indtable, toastheap) AS ( "
534 " SELECT c.reltoastrelid, 0::oid, c.oid "
535 " FROM regular_heap JOIN pg_catalog.pg_class c "
536 " ON regular_heap.reloid = c.oid "
537 " WHERE c.reltoastrelid != 0), ");
538
539 /*
540 * Add a CTE that collects OIDs of all valid indexes on the previously
541 * selected tables. We can ignore invalid indexes since pg_dump does.
542 * Testing indisready is necessary in 9.2, and harmless in earlier/later
543 * versions.
544 */
546 " all_index (reloid, indtable, toastheap) AS ( "
547 " SELECT indexrelid, indrelid, 0::oid "
548 " FROM pg_catalog.pg_index "
549 " WHERE indisvalid AND indisready "
550 " AND indrelid IN "
551 " (SELECT reloid FROM regular_heap "
552 " UNION ALL "
553 " SELECT reloid FROM toast_heap)) ");
554
555 /*
556 * And now we can write the query that retrieves the data we want for each
557 * heap and index relation. Make sure result is sorted by OID.
558 */
560 "SELECT all_rels.*, n.nspname, c.relname, "
561 " c.relfilenode, c.reltablespace, "
562 " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
563 "FROM (SELECT * FROM regular_heap "
564 " UNION ALL "
565 " SELECT * FROM toast_heap "
566 " UNION ALL "
567 " SELECT * FROM all_index) all_rels "
568 " JOIN pg_catalog.pg_class c "
569 " ON all_rels.reloid = c.oid "
570 " JOIN pg_catalog.pg_namespace n "
571 " ON c.relnamespace = n.oid "
572 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
573 " ON c.reltablespace = t.oid "
574 "ORDER BY 1");
575
576 return query.data;
577}
#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 790 of file info.c.

791{
792 PGconn *conn;
793 PGresult *res;
794 int i_nsub;
796
797 conn = connectToServer(cluster, "template1");
798 if (GET_MAJOR_VERSION(cluster->major_version) >= 1900)
799 res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
800 "COUNT(CASE WHEN subretaindeadtuples THEN 1 END) > 0 AS retain_dead_tuples "
801 "FROM pg_catalog.pg_subscription");
802 else
803 res = executeQueryOrDie(conn, "SELECT count(*) AS nsub,"
804 "'f' AS retain_dead_tuples "
805 "FROM pg_catalog.pg_subscription");
806
807 i_nsub = PQfnumber(res, "nsub");
808 i_retain_dead_tuples = PQfnumber(res, "retain_dead_tuples");
809
810 cluster->nsubs = atoi(PQgetvalue(res, 0, i_nsub));
811 cluster->sub_retain_dead_tuples = (strcmp(PQgetvalue(res, 0, i_retain_dead_tuples), "t") == 0);
812
813 PQclear(res);
814 PQfinish(conn);
815}

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 339 of file info.c.

340{
341 PGconn *conn = connectToServer(cluster, "template1");
342 DbLocaleInfo *locale;
344 int i_datencoding;
346 int i_datcollate;
347 int i_datctype;
348 int i_datlocale;
349
350 if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
352 "SELECT encoding, datlocprovider, "
353 " datcollate, datctype, datlocale "
354 "FROM pg_catalog.pg_database "
355 "WHERE datname='template0'");
356 else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
358 "SELECT encoding, datlocprovider, "
359 " datcollate, datctype, daticulocale AS datlocale "
360 "FROM pg_catalog.pg_database "
361 "WHERE datname='template0'");
362 else
364 "SELECT encoding, 'c' AS datlocprovider, "
365 " datcollate, datctype, NULL AS datlocale "
366 "FROM pg_catalog.pg_database "
367 "WHERE datname='template0'");
368
369
370 if (PQntuples(dbres) != 1)
371 pg_fatal("template0 not found");
372
373 locale = pg_malloc(sizeof(DbLocaleInfo));
374
375 i_datencoding = PQfnumber(dbres, "encoding");
376 i_datlocprovider = PQfnumber(dbres, "datlocprovider");
377 i_datcollate = PQfnumber(dbres, "datcollate");
378 i_datctype = PQfnumber(dbres, "datctype");
379 i_datlocale = PQfnumber(dbres, "datlocale");
380
386 locale->db_locale = NULL;
387 else
389
390 cluster->template0 = locale;
391
392 PQclear(dbres);
393 PQfinish(conn);
394}
#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 852 of file info.c.

853{
854 int dbnum;
855
856 for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
857 {
858 DbInfo *pDbInfo = &db_arr->dbs[dbnum];
859
860 pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
861 print_rel_infos(&pDbInfo->rel_arr);
862 print_slot_infos(&pDbInfo->slot_arr);
863 }
864}
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
Definition info.c:881
static void print_rel_infos(RelInfoArr *rel_arr)
Definition info.c:868

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 868 of file info.c.

869{
870 int relnum;
871
872 for (relnum = 0; relnum < rel_arr->nrels; relnum++)
873 pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
874 rel_arr->rels[relnum].nspname,
875 rel_arr->rels[relnum].relname,
876 rel_arr->rels[relnum].reloid,
877 rel_arr->rels[relnum].tablespace);
878}
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 881 of file info.c.

882{
883 /* Quick return if there are no logical slots. */
884 if (slot_arr->nslots == 0)
885 return;
886
887 pg_log(PG_VERBOSE, "Logical replication slots in the database:");
888
889 for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
890 {
891 LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
892
893 pg_log(PG_VERBOSE, "slot name: \"%s\", output plugin: \"%s\", two_phase: %s",
894 slot_info->slotname,
895 slot_info->plugin,
896 slot_info->two_phase ? "true" : "false");
897 }
898}
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 723 of file info.c.

724{
726 int num_slots = PQntuples(res);
727
728 if (num_slots)
729 {
730 int i_slotname;
731 int i_plugin;
732 int i_twophase;
733 int i_failover;
734 int i_caught_up;
735 int i_invalid;
736
737 slotinfos = (LogicalSlotInfo *) pg_malloc(sizeof(LogicalSlotInfo) * num_slots);
738
739 i_slotname = PQfnumber(res, "slot_name");
740 i_plugin = PQfnumber(res, "plugin");
741 i_twophase = PQfnumber(res, "two_phase");
742 i_failover = PQfnumber(res, "failover");
743 i_caught_up = PQfnumber(res, "caught_up");
744 i_invalid = PQfnumber(res, "invalid");
745
746 for (int slotnum = 0; slotnum < num_slots; slotnum++)
747 {
749
751 curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin));
752 curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0);
753 curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0);
754 curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0);
755 curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0);
756 }
757 }
758
759 dbinfo->slot_arr.slots = slotinfos;
760 dbinfo->slot_arr.nslots = num_slots;
761}

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 585 of file info.c.

586{
587 int ntups = PQntuples(res);
588 RelInfo *relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
589 int i_reloid = PQfnumber(res, "reloid");
590 int i_indtable = PQfnumber(res, "indtable");
591 int i_toastheap = PQfnumber(res, "toastheap");
592 int i_nspname = PQfnumber(res, "nspname");
593 int i_relname = PQfnumber(res, "relname");
594 int i_relfilenumber = PQfnumber(res, "relfilenode");
595 int i_reltablespace = PQfnumber(res, "reltablespace");
596 int i_spclocation = PQfnumber(res, "spclocation");
597 int num_rels = 0;
598 char *nspname = NULL;
599 char *relname = NULL;
600 char *tablespace = NULL;
601 char *last_namespace = NULL;
602 char *last_tablespace = NULL;
603
604 for (int relnum = 0; relnum < ntups; relnum++)
605 {
607
609 curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
610 curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
611
612 nspname = PQgetvalue(res, relnum, i_nspname);
613 curr->nsp_alloc = false;
614
615 /*
616 * Many of the namespace and tablespace strings are identical, so we
617 * try to reuse the allocated string pointers where possible to reduce
618 * memory consumption.
619 */
620 /* Can we reuse the previous string allocation? */
621 if (last_namespace && strcmp(nspname, last_namespace) == 0)
622 curr->nspname = last_namespace;
623 else
624 {
625 last_namespace = curr->nspname = pg_strdup(nspname);
626 curr->nsp_alloc = true;
627 }
628
630 curr->relname = pg_strdup(relname);
631
632 curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
633 curr->tblsp_alloc = false;
634
635 /* Is the tablespace oid non-default? */
636 if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
637 {
638 char *spcloc = PQgetvalue(res, relnum, i_spclocation);
639 bool inplace = spcloc[0] && !is_absolute_path(spcloc);
640
641 /*
642 * The tablespace location might be "", meaning the cluster
643 * default location, i.e. pg_default or pg_global. For in-place
644 * tablespaces, pg_tablespace_location() returns a path relative
645 * to the data directory.
646 */
647 if (inplace)
648 tablespace = psprintf("%s/%s",
650 spcloc);
651 else
653
654 /* Can we reuse the previous string allocation? */
656 curr->tablespace = last_tablespace;
657 else
658 {
659 last_tablespace = curr->tablespace = pg_strdup(tablespace);
660 curr->tblsp_alloc = true;
661 }
662
663 /* Free palloc'd string for in-place tablespaces. */
664 if (inplace)
666 }
667 else
668 /* A zero reltablespace oid indicates the database tablespace. */
669 curr->tablespace = dbinfo->db_tablespace;
670 }
671
672 dbinfo->rel_arr.rels = relinfos;
673 dbinfo->rel_arr.nrels = num_rels;
674}
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 * 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().