PostgreSQL Source Code  git master
info.c File Reference
#include "postgres_fe.h"
#include "access/transam.h"
#include "catalog/pg_class_d.h"
#include "pg_upgrade.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 void get_rel_infos (ClusterInfo *cluster, DbInfo *dbinfo)
 
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 void get_old_cluster_logical_slot_infos (DbInfo *dbinfo, bool live_check)
 
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, bool live_check)
 
int count_old_cluster_logical_slots (void)
 
void get_subscription_count (ClusterInfo *cluster)
 

Function Documentation

◆ count_old_cluster_logical_slots()

int count_old_cluster_logical_slots ( void  )

Definition at line 732 of file info.c.

733 {
734  int slot_count = 0;
735 
736  for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
737  slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots;
738 
739  return slot_count;
740 }
ClusterInfo old_cluster
Definition: pg_upgrade.c:71
DbInfoArr dbarr
Definition: pg_upgrade.h:284
DbInfo * dbs
Definition: pg_upgrade.h:215
LogicalSlotInfoArr slot_arr
Definition: pg_upgrade.h:198

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

Referenced by check_new_cluster_logical_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 162 of file info.c.

166 {
167  /* In case old/new tablespaces don't match, do them separately. */
168  if (strlen(old_rel->tablespace) == 0)
169  {
170  /*
171  * relation belongs to the default tablespace, hence relfiles should
172  * exist in the data directories.
173  */
174  map->old_tablespace = old_data;
175  map->old_tablespace_suffix = "/base";
176  }
177  else
178  {
179  /* relation belongs to a tablespace, so use the tablespace location */
180  map->old_tablespace = old_rel->tablespace;
182  }
183 
184  /* Do the same for new tablespaces */
185  if (strlen(new_rel->tablespace) == 0)
186  {
187  map->new_tablespace = new_data;
188  map->new_tablespace_suffix = "/base";
189  }
190  else
191  {
192  map->new_tablespace = new_rel->tablespace;
194  }
195 
196  /* DB oid and relfilenumbers are preserved between old and new cluster */
197  map->db_oid = old_db->db_oid;
198  map->relfilenumber = old_rel->relfilenumber;
199 
200  /* used only for logging and error reporting, old/new are identical */
201  map->nspname = old_rel->nspname;
202  map->relname = old_rel->relname;
203 }
ClusterInfo new_cluster
Definition: pg_upgrade.c:72
const char * tablespace_suffix
Definition: pg_upgrade.h:296
Oid db_oid
Definition: pg_upgrade.h:193
const char * new_tablespace
Definition: pg_upgrade.h:178
const char * old_tablespace_suffix
Definition: pg_upgrade.h:179
const char * old_tablespace
Definition: pg_upgrade.h:177
RelFileNumber relfilenumber
Definition: pg_upgrade.h:182
char * relname
Definition: pg_upgrade.h:185
char * nspname
Definition: pg_upgrade.h:184
const char * new_tablespace_suffix
Definition: pg_upgrade.h:180
RelFileNumber relfilenumber
Definition: pg_upgrade.h:138
char * nspname
Definition: pg_upgrade.h:135
char * tablespace
Definition: pg_upgrade.h:141
char * relname
Definition: pg_upgrade.h:136

References FileNameMap::db_oid, DbInfo::db_oid, new_cluster, FileNameMap::new_tablespace, FileNameMap::new_tablespace_suffix, RelInfo::nspname, FileNameMap::nspname, old_cluster, FileNameMap::old_tablespace, FileNameMap::old_tablespace_suffix, RelInfo::relfilenumber, FileNameMap::relfilenumber, RelInfo::relname, FileNameMap::relname, RelInfo::tablespace, 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 763 of file info.c.

764 {
765  int dbnum;
766 
767  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
768  {
769  free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
770  pg_free(db_arr->dbs[dbnum].db_name);
771  }
772  pg_free(db_arr->dbs);
773  db_arr->dbs = NULL;
774  db_arr->ndbs = 0;
775 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static void free_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:779
char * db_name
Definition: pg_upgrade.h:194
RelInfoArr rel_arr
Definition: pg_upgrade.h:197

References DbInfo::db_name, DbInfoArr::dbs, free_rel_infos(), DbInfoArr::ndbs, pg_free(), and DbInfo::rel_arr.

Referenced by get_db_rel_and_slot_infos().

◆ free_rel_infos()

static void free_rel_infos ( RelInfoArr rel_arr)
static

Definition at line 779 of file info.c.

780 {
781  int relnum;
782 
783  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
784  {
785  if (rel_arr->rels[relnum].nsp_alloc)
786  pg_free(rel_arr->rels[relnum].nspname);
787  pg_free(rel_arr->rels[relnum].relname);
788  if (rel_arr->rels[relnum].tblsp_alloc)
789  pg_free(rel_arr->rels[relnum].tablespace);
790  }
791  pg_free(rel_arr->rels);
792  rel_arr->nrels = 0;
793 }
RelInfo * rels
Definition: pg_upgrade.h:148
bool tblsp_alloc
Definition: pg_upgrade.h:143
bool nsp_alloc
Definition: pg_upgrade.h:142

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

45 {
47  int old_relnum,
48  new_relnum;
49  int num_maps = 0;
50  bool all_matched = true;
51 
52  /* There will certainly not be more mappings than there are old rels */
53  maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
54  old_db->rel_arr.nrels);
55 
56  /*
57  * Each of the RelInfo arrays should be sorted by OID. Scan through them
58  * and match them up. If we fail to match everything, we'll abort, but
59  * first print as much info as we can about mismatches.
60  */
61  old_relnum = new_relnum = 0;
62  while (old_relnum < old_db->rel_arr.nrels ||
63  new_relnum < new_db->rel_arr.nrels)
64  {
65  RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
66  &old_db->rel_arr.rels[old_relnum] : NULL;
67  RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
68  &new_db->rel_arr.rels[new_relnum] : NULL;
69 
70  /* handle running off one array before the other */
71  if (!new_rel)
72  {
73  /*
74  * old_rel is unmatched. This should never happen, because we
75  * force new rels to have TOAST tables if the old one did.
76  */
77  report_unmatched_relation(old_rel, old_db, false);
78  all_matched = false;
79  old_relnum++;
80  continue;
81  }
82  if (!old_rel)
83  {
84  /*
85  * new_rel is unmatched. This shouldn't really happen either, but
86  * if it's a TOAST table, we can ignore it and continue
87  * processing, assuming that the new server made a TOAST table
88  * that wasn't needed.
89  */
90  if (strcmp(new_rel->nspname, "pg_toast") != 0)
91  {
92  report_unmatched_relation(new_rel, new_db, true);
93  all_matched = false;
94  }
95  new_relnum++;
96  continue;
97  }
98 
99  /* check for mismatched OID */
100  if (old_rel->reloid < new_rel->reloid)
101  {
102  /* old_rel is unmatched, see comment above */
103  report_unmatched_relation(old_rel, old_db, false);
104  all_matched = false;
105  old_relnum++;
106  continue;
107  }
108  else if (old_rel->reloid > new_rel->reloid)
109  {
110  /* new_rel is unmatched, see comment above */
111  if (strcmp(new_rel->nspname, "pg_toast") != 0)
112  {
113  report_unmatched_relation(new_rel, new_db, true);
114  all_matched = false;
115  }
116  new_relnum++;
117  continue;
118  }
119 
120  /*
121  * Verify that rels of same OID have same name. The namespace name
122  * should always match, but the relname might not match for TOAST
123  * tables (and, therefore, their indexes).
124  */
125  if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
126  strcmp(old_rel->relname, new_rel->relname) != 0)
127  {
128  pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: "
129  "old name \"%s.%s\", new name \"%s.%s\"",
130  old_rel->reloid, old_db->db_name,
131  old_rel->nspname, old_rel->relname,
132  new_rel->nspname, new_rel->relname);
133  all_matched = false;
134  old_relnum++;
135  new_relnum++;
136  continue;
137  }
138 
139  /* OK, create a mapping entry */
140  create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
141  old_rel, new_rel, maps + num_maps);
142  num_maps++;
143  old_relnum++;
144  new_relnum++;
145  }
146 
147  if (!all_matched)
148  pg_fatal("Failed to match up old and new tables in database \"%s\"",
149  old_db->db_name);
150 
151  *nmaps = num_maps;
152  return maps;
153 }
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:211
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:162
#define pg_fatal(...)
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ PG_WARNING
Definition: pg_upgrade.h:270
Oid reloid
Definition: pg_upgrade.h:137
static const pg_conv_map maps[]

References create_rel_filename_map(), DbInfo::db_name, maps, RelInfoArr::nrels, RelInfo::nspname, pg_fatal, pg_log(), pg_malloc(), PG_WARNING, DbInfo::rel_arr, RelInfo::relname, RelInfo::reloid, RelInfoArr::rels, and report_unmatched_relation().

Referenced by transfer_all_new_dbs().

◆ get_db_infos()

static void get_db_infos ( ClusterInfo cluster)
static

Definition at line 379 of file info.c.

380 {
381  PGconn *conn = connectToServer(cluster, "template1");
382  PGresult *res;
383  int ntups;
384  int tupnum;
385  DbInfo *dbinfos;
386  int i_datname,
387  i_oid,
388  i_spclocation;
389  char query[QUERY_ALLOC];
390 
391  snprintf(query, sizeof(query),
392  "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
393  if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
394  snprintf(query + strlen(query), sizeof(query) - strlen(query),
395  "datlocprovider, datlocale, ");
396  else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
397  snprintf(query + strlen(query), sizeof(query) - strlen(query),
398  "datlocprovider, daticulocale AS datlocale, ");
399  else
400  snprintf(query + strlen(query), sizeof(query) - strlen(query),
401  "'c' AS datlocprovider, NULL AS datlocale, ");
402  snprintf(query + strlen(query), sizeof(query) - strlen(query),
403  "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
404  "FROM pg_catalog.pg_database d "
405  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
406  " ON d.dattablespace = t.oid "
407  "WHERE d.datallowconn = true "
408  "ORDER BY 1");
409 
410  res = executeQueryOrDie(conn, "%s", query);
411 
412  i_oid = PQfnumber(res, "oid");
413  i_datname = PQfnumber(res, "datname");
414  i_spclocation = PQfnumber(res, "spclocation");
415 
416  ntups = PQntuples(res);
417  dbinfos = (DbInfo *) pg_malloc0(sizeof(DbInfo) * ntups);
418 
419  for (tupnum = 0; tupnum < ntups; tupnum++)
420  {
421  dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
422  dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
423  snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
424  PQgetvalue(res, tupnum, i_spclocation));
425  }
426  PQclear(res);
427 
428  PQfinish(conn);
429 
430  cluster->dbarr.dbs = dbinfos;
431  cluster->dbarr.ndbs = ntups;
432 }
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4892
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define QUERY_ALLOC
Definition: pg_upgrade.h:23
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:28
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:27
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define snprintf
Definition: port.h:238
#define atooid(x)
Definition: postgres_ext.h:42
PGconn * conn
Definition: streamutil.c:55

References atooid, cluster(), conn, connectToServer(), DbInfo::db_name, DbInfo::db_oid, executeQueryOrDie(), GET_MAJOR_VERSION, pg_malloc0(), pg_strdup(), PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), QUERY_ALLOC, res, 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,
bool  live_check 
)

Definition at line 279 of file info.c.

280 {
281  int dbnum;
282 
283  if (cluster->dbarr.dbs != NULL)
285 
288 
289  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
290  {
291  DbInfo *pDbInfo = &cluster->dbarr.dbs[dbnum];
292 
293  get_rel_infos(cluster, pDbInfo);
294 
295  if (cluster == &old_cluster)
296  get_old_cluster_logical_slot_infos(pDbInfo, live_check);
297  }
298 
299  if (cluster == &old_cluster)
300  pg_log(PG_VERBOSE, "\nsource databases:");
301  else
302  pg_log(PG_VERBOSE, "\ntarget databases:");
303 
304  if (log_opts.verbose)
305  print_db_infos(&cluster->dbarr);
306 }
static void get_template0_info(ClusterInfo *cluster)
Definition: info.c:314
static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check)
Definition: info.c:640
static void get_db_infos(ClusterInfo *cluster)
Definition: info.c:379
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition: info.c:763
static void print_db_infos(DbInfoArr *db_arr)
Definition: info.c:797
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
Definition: info.c:445
LogOpts log_opts
Definition: util.c:17
@ PG_VERBOSE
Definition: pg_upgrade.h:266
bool verbose
Definition: pg_upgrade.h:307

References cluster(), free_db_and_rel_infos(), get_db_infos(), get_old_cluster_logical_slot_infos(), get_rel_infos(), get_template0_info(), log_opts, old_cluster, pg_log(), PG_VERBOSE, print_db_infos(), and LogOpts::verbose.

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

◆ get_old_cluster_logical_slot_infos()

static void get_old_cluster_logical_slot_infos ( DbInfo dbinfo,
bool  live_check 
)
static

Definition at line 640 of file info.c.

641 {
642  PGconn *conn;
643  PGresult *res;
644  LogicalSlotInfo *slotinfos = NULL;
645  int num_slots;
646 
647  /* Logical slots can be migrated since PG17. */
649  return;
650 
651  conn = connectToServer(&old_cluster, dbinfo->db_name);
652 
653  /*
654  * Fetch the logical replication slot information. The check whether the
655  * slot is considered caught up is done by an upgrade function. This
656  * regards the slot as caught up if we don't find any decodable changes.
657  * See binary_upgrade_logical_slot_has_caught_up().
658  *
659  * Note that we can't ensure whether the slot is caught up during
660  * live_check as the new WAL records could be generated.
661  *
662  * We intentionally skip checking the WALs for invalidated slots as the
663  * corresponding WALs could have been removed for such slots.
664  *
665  * The temporary slots are explicitly ignored while checking because such
666  * slots cannot exist after the upgrade. During the upgrade, clusters are
667  * started and stopped several times causing any temporary slots to be
668  * removed.
669  */
670  res = executeQueryOrDie(conn, "SELECT slot_name, plugin, two_phase, failover, "
671  "%s as caught_up, invalidation_reason IS NOT NULL as invalid "
672  "FROM pg_catalog.pg_replication_slots "
673  "WHERE slot_type = 'logical' AND "
674  "database = current_database() AND "
675  "temporary IS FALSE;",
676  live_check ? "FALSE" :
677  "(CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
678  "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
679  "END)");
680 
681  num_slots = PQntuples(res);
682 
683  if (num_slots)
684  {
685  int i_slotname;
686  int i_plugin;
687  int i_twophase;
688  int i_failover;
689  int i_caught_up;
690  int i_invalid;
691 
692  slotinfos = (LogicalSlotInfo *) pg_malloc(sizeof(LogicalSlotInfo) * num_slots);
693 
694  i_slotname = PQfnumber(res, "slot_name");
695  i_plugin = PQfnumber(res, "plugin");
696  i_twophase = PQfnumber(res, "two_phase");
697  i_failover = PQfnumber(res, "failover");
698  i_caught_up = PQfnumber(res, "caught_up");
699  i_invalid = PQfnumber(res, "invalid");
700 
701  for (int slotnum = 0; slotnum < num_slots; slotnum++)
702  {
703  LogicalSlotInfo *curr = &slotinfos[slotnum];
704 
705  curr->slotname = pg_strdup(PQgetvalue(res, slotnum, i_slotname));
706  curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin));
707  curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0);
708  curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0);
709  curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0);
710  curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0);
711  }
712  }
713 
714  PQclear(res);
715  PQfinish(conn);
716 
717  dbinfo->slot_arr.slots = slotinfos;
718  dbinfo->slot_arr.nslots = num_slots;
719 }
static struct LogicalRepInfo * dbinfo
uint32 major_version
Definition: pg_upgrade.h:293

References LogicalSlotInfo::caught_up, conn, connectToServer(), dbinfo, executeQueryOrDie(), LogicalSlotInfo::failover, GET_MAJOR_VERSION, LogicalSlotInfo::invalid, ClusterInfo::major_version, old_cluster, pg_malloc(), pg_strdup(), LogicalSlotInfo::plugin, PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), res, LogicalSlotInfo::slotname, and LogicalSlotInfo::two_phase.

Referenced by get_db_rel_and_slot_infos().

◆ get_rel_infos()

static void get_rel_infos ( ClusterInfo cluster,
DbInfo dbinfo 
)
static

Definition at line 445 of file info.c.

446 {
448  dbinfo->db_name);
449  PGresult *res;
450  RelInfo *relinfos;
451  int ntups;
452  int relnum;
453  int num_rels = 0;
454  char *nspname = NULL;
455  char *relname = NULL;
456  char *tablespace = NULL;
457  int i_spclocation,
458  i_nspname,
459  i_relname,
460  i_reloid,
461  i_indtable,
462  i_toastheap,
463  i_relfilenumber,
464  i_reltablespace;
465  char query[QUERY_ALLOC];
466  char *last_namespace = NULL,
467  *last_tablespace = NULL;
468 
469  query[0] = '\0'; /* initialize query string to empty */
470 
471  /*
472  * Create a CTE that collects OIDs of regular user tables and matviews,
473  * but excluding toast tables and indexes. We assume that relations with
474  * OIDs >= FirstNormalObjectId belong to the user. (That's probably
475  * redundant with the namespace-name exclusions, but let's be safe.)
476  *
477  * pg_largeobject contains user data that does not appear in pg_dump
478  * output, so we have to copy that system table. It's easiest to do that
479  * by treating it as a user table.
480  */
481  snprintf(query + strlen(query), sizeof(query) - strlen(query),
482  "WITH regular_heap (reloid, indtable, toastheap) AS ( "
483  " SELECT c.oid, 0::oid, 0::oid "
484  " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
485  " ON c.relnamespace = n.oid "
486  " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
487  CppAsString2(RELKIND_MATVIEW) ") AND "
488  /* exclude possible orphaned temp tables */
489  " ((n.nspname !~ '^pg_temp_' AND "
490  " n.nspname !~ '^pg_toast_temp_' AND "
491  " n.nspname NOT IN ('pg_catalog', 'information_schema', "
492  " 'binary_upgrade', 'pg_toast') AND "
493  " c.oid >= %u::pg_catalog.oid) OR "
494  " (n.nspname = 'pg_catalog' AND "
495  " relname IN ('pg_largeobject') ))), ",
497 
498  /*
499  * Add a CTE that collects OIDs of toast tables belonging to the tables
500  * selected by the regular_heap CTE. (We have to do this separately
501  * because the namespace-name rules above don't work for toast tables.)
502  */
503  snprintf(query + strlen(query), sizeof(query) - strlen(query),
504  " toast_heap (reloid, indtable, toastheap) AS ( "
505  " SELECT c.reltoastrelid, 0::oid, c.oid "
506  " FROM regular_heap JOIN pg_catalog.pg_class c "
507  " ON regular_heap.reloid = c.oid "
508  " WHERE c.reltoastrelid != 0), ");
509 
510  /*
511  * Add a CTE that collects OIDs of all valid indexes on the previously
512  * selected tables. We can ignore invalid indexes since pg_dump does.
513  * Testing indisready is necessary in 9.2, and harmless in earlier/later
514  * versions.
515  */
516  snprintf(query + strlen(query), sizeof(query) - strlen(query),
517  " all_index (reloid, indtable, toastheap) AS ( "
518  " SELECT indexrelid, indrelid, 0::oid "
519  " FROM pg_catalog.pg_index "
520  " WHERE indisvalid AND indisready "
521  " AND indrelid IN "
522  " (SELECT reloid FROM regular_heap "
523  " UNION ALL "
524  " SELECT reloid FROM toast_heap)) ");
525 
526  /*
527  * And now we can write the query that retrieves the data we want for each
528  * heap and index relation. Make sure result is sorted by OID.
529  */
530  snprintf(query + strlen(query), sizeof(query) - strlen(query),
531  "SELECT all_rels.*, n.nspname, c.relname, "
532  " c.relfilenode, c.reltablespace, "
533  " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
534  "FROM (SELECT * FROM regular_heap "
535  " UNION ALL "
536  " SELECT * FROM toast_heap "
537  " UNION ALL "
538  " SELECT * FROM all_index) all_rels "
539  " JOIN pg_catalog.pg_class c "
540  " ON all_rels.reloid = c.oid "
541  " JOIN pg_catalog.pg_namespace n "
542  " ON c.relnamespace = n.oid "
543  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
544  " ON c.reltablespace = t.oid "
545  "ORDER BY 1;");
546 
547  res = executeQueryOrDie(conn, "%s", query);
548 
549  ntups = PQntuples(res);
550 
551  relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
552 
553  i_reloid = PQfnumber(res, "reloid");
554  i_indtable = PQfnumber(res, "indtable");
555  i_toastheap = PQfnumber(res, "toastheap");
556  i_nspname = PQfnumber(res, "nspname");
557  i_relname = PQfnumber(res, "relname");
558  i_relfilenumber = PQfnumber(res, "relfilenode");
559  i_reltablespace = PQfnumber(res, "reltablespace");
560  i_spclocation = PQfnumber(res, "spclocation");
561 
562  for (relnum = 0; relnum < ntups; relnum++)
563  {
564  RelInfo *curr = &relinfos[num_rels++];
565 
566  curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
567  curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
568  curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
569 
570  nspname = PQgetvalue(res, relnum, i_nspname);
571  curr->nsp_alloc = false;
572 
573  /*
574  * Many of the namespace and tablespace strings are identical, so we
575  * try to reuse the allocated string pointers where possible to reduce
576  * memory consumption.
577  */
578  /* Can we reuse the previous string allocation? */
579  if (last_namespace && strcmp(nspname, last_namespace) == 0)
580  curr->nspname = last_namespace;
581  else
582  {
583  last_namespace = curr->nspname = pg_strdup(nspname);
584  curr->nsp_alloc = true;
585  }
586 
587  relname = PQgetvalue(res, relnum, i_relname);
588  curr->relname = pg_strdup(relname);
589 
590  curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
591  curr->tblsp_alloc = false;
592 
593  /* Is the tablespace oid non-default? */
594  if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
595  {
596  /*
597  * The tablespace location might be "", meaning the cluster
598  * default location, i.e. pg_default or pg_global.
599  */
600  tablespace = PQgetvalue(res, relnum, i_spclocation);
601 
602  /* Can we reuse the previous string allocation? */
603  if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
604  curr->tablespace = last_tablespace;
605  else
606  {
607  last_tablespace = curr->tablespace = pg_strdup(tablespace);
608  curr->tblsp_alloc = true;
609  }
610  }
611  else
612  /* A zero reltablespace oid indicates the database tablespace. */
613  curr->tablespace = dbinfo->db_tablespace;
614  }
615  PQclear(res);
616 
617  PQfinish(conn);
618 
619  dbinfo->rel_arr.rels = relinfos;
620  dbinfo->rel_arr.nrels = num_rels;
621 }
#define CppAsString2(x)
Definition: c.h:327
NameData relname
Definition: pg_class.h:38
static char * tablespace
Definition: pgbench.c:216
Oid toastheap
Definition: pg_upgrade.h:140
Oid indtable
Definition: pg_upgrade.h:139
#define FirstNormalObjectId
Definition: transam.h:197

References atooid, cluster(), conn, connectToServer(), CppAsString2, dbinfo, executeQueryOrDie(), FirstNormalObjectId, RelInfo::indtable, RelInfo::nsp_alloc, RelInfo::nspname, pg_malloc(), pg_strdup(), PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), QUERY_ALLOC, RelInfo::relfilenumber, RelInfo::relname, relname, RelInfo::reloid, res, snprintf, RelInfo::tablespace, tablespace, RelInfo::tblsp_alloc, and RelInfo::toastheap.

Referenced by get_db_rel_and_slot_infos().

◆ get_subscription_count()

void get_subscription_count ( ClusterInfo cluster)

Definition at line 748 of file info.c.

749 {
750  PGconn *conn;
751  PGresult *res;
752 
753  conn = connectToServer(cluster, "template1");
754  res = executeQueryOrDie(conn, "SELECT count(*) "
755  "FROM pg_catalog.pg_subscription");
756  cluster->nsubs = atoi(PQgetvalue(res, 0, 0));
757 
758  PQclear(res);
759  PQfinish(conn);
760 }

References cluster(), conn, connectToServer(), executeQueryOrDie(), PQclear(), PQfinish(), PQgetvalue(), and res.

Referenced by check_and_dump_old_cluster().

◆ get_template0_info()

static void get_template0_info ( ClusterInfo cluster)
static

Definition at line 314 of file info.c.

315 {
316  PGconn *conn = connectToServer(cluster, "template1");
318  PGresult *dbres;
319  int i_datencoding;
320  int i_datlocprovider;
321  int i_datcollate;
322  int i_datctype;
323  int i_datlocale;
324 
325  if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
326  dbres = executeQueryOrDie(conn,
327  "SELECT encoding, datlocprovider, "
328  " datcollate, datctype, datlocale "
329  "FROM pg_catalog.pg_database "
330  "WHERE datname='template0'");
331  else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
332  dbres = executeQueryOrDie(conn,
333  "SELECT encoding, datlocprovider, "
334  " datcollate, datctype, daticulocale AS datlocale "
335  "FROM pg_catalog.pg_database "
336  "WHERE datname='template0'");
337  else
338  dbres = executeQueryOrDie(conn,
339  "SELECT encoding, 'c' AS datlocprovider, "
340  " datcollate, datctype, NULL AS datlocale "
341  "FROM pg_catalog.pg_database "
342  "WHERE datname='template0'");
343 
344 
345  if (PQntuples(dbres) != 1)
346  pg_fatal("template0 not found");
347 
348  locale = pg_malloc(sizeof(DbLocaleInfo));
349 
350  i_datencoding = PQfnumber(dbres, "encoding");
351  i_datlocprovider = PQfnumber(dbres, "datlocprovider");
352  i_datcollate = PQfnumber(dbres, "datcollate");
353  i_datctype = PQfnumber(dbres, "datctype");
354  i_datlocale = PQfnumber(dbres, "datlocale");
355 
356  locale->db_encoding = atoi(PQgetvalue(dbres, 0, i_datencoding));
357  locale->db_collprovider = PQgetvalue(dbres, 0, i_datlocprovider)[0];
358  locale->db_collate = pg_strdup(PQgetvalue(dbres, 0, i_datcollate));
359  locale->db_ctype = pg_strdup(PQgetvalue(dbres, 0, i_datctype));
360  if (PQgetisnull(dbres, 0, i_datlocale))
361  locale->db_locale = NULL;
362  else
363  locale->db_locale = pg_strdup(PQgetvalue(dbres, 0, i_datlocale));
364 
365  cluster->template0 = locale;
366 
367  PQclear(dbres);
368  PQfinish(conn);
369 }
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
static char * locale
Definition: initdb.c:140

References cluster(), conn, connectToServer(), executeQueryOrDie(), GET_MAJOR_VERSION, locale, 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 797 of file info.c.

798 {
799  int dbnum;
800 
801  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
802  {
803  DbInfo *pDbInfo = &db_arr->dbs[dbnum];
804 
805  pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
806  print_rel_infos(&pDbInfo->rel_arr);
807  print_slot_infos(&pDbInfo->slot_arr);
808  }
809 }
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
Definition: info.c:826
static void print_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:813

References DbInfo::db_name, DbInfoArr::dbs, DbInfoArr::ndbs, pg_log(), PG_VERBOSE, print_rel_infos(), print_slot_infos(), DbInfo::rel_arr, and DbInfo::slot_arr.

Referenced by get_db_rel_and_slot_infos().

◆ print_rel_infos()

static void print_rel_infos ( RelInfoArr rel_arr)
static

Definition at line 813 of file info.c.

814 {
815  int relnum;
816 
817  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
818  pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
819  rel_arr->rels[relnum].nspname,
820  rel_arr->rels[relnum].relname,
821  rel_arr->rels[relnum].reloid,
822  rel_arr->rels[relnum].tablespace);
823 }

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

827 {
828  /* Quick return if there are no logical slots. */
829  if (slot_arr->nslots == 0)
830  return;
831 
832  pg_log(PG_VERBOSE, "Logical replication slots within the database:");
833 
834  for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
835  {
836  LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
837 
838  pg_log(PG_VERBOSE, "slot_name: \"%s\", plugin: \"%s\", two_phase: %s",
839  slot_info->slotname,
840  slot_info->plugin,
841  slot_info->two_phase ? "true" : "false");
842  }
843 }
LogicalSlotInfo * slots
Definition: pg_upgrade.h:169

References LogicalSlotInfoArr::nslots, pg_log(), PG_VERBOSE, LogicalSlotInfo::plugin, LogicalSlotInfo::slotname, LogicalSlotInfoArr::slots, and LogicalSlotInfo::two_phase.

Referenced by print_db_infos().

◆ report_unmatched_relation()

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

Definition at line 211 of file info.c.

212 {
213  Oid reloid = rel->reloid; /* we might change rel below */
214  char reldesc[1000];
215  int i;
216 
217  snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
218  rel->nspname, rel->relname);
219  if (rel->indtable)
220  {
221  for (i = 0; i < db->rel_arr.nrels; i++)
222  {
223  const RelInfo *hrel = &db->rel_arr.rels[i];
224 
225  if (hrel->reloid == rel->indtable)
226  {
227  snprintf(reldesc + strlen(reldesc),
228  sizeof(reldesc) - strlen(reldesc),
229  _(" which is an index on \"%s.%s\""),
230  hrel->nspname, hrel->relname);
231  /* Shift attention to index's table for toast check */
232  rel = hrel;
233  break;
234  }
235  }
236  if (i >= db->rel_arr.nrels)
237  snprintf(reldesc + strlen(reldesc),
238  sizeof(reldesc) - strlen(reldesc),
239  _(" which is an index on OID %u"), rel->indtable);
240  }
241  if (rel->toastheap)
242  {
243  for (i = 0; i < db->rel_arr.nrels; i++)
244  {
245  const RelInfo *brel = &db->rel_arr.rels[i];
246 
247  if (brel->reloid == rel->toastheap)
248  {
249  snprintf(reldesc + strlen(reldesc),
250  sizeof(reldesc) - strlen(reldesc),
251  _(" which is the TOAST table for \"%s.%s\""),
252  brel->nspname, brel->relname);
253  break;
254  }
255  }
256  if (i >= db->rel_arr.nrels)
257  snprintf(reldesc + strlen(reldesc),
258  sizeof(reldesc) - strlen(reldesc),
259  _(" which is the TOAST table for OID %u"), rel->toastheap);
260  }
261 
262  if (is_new_db)
263  pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s",
264  reloid, db->db_name, reldesc);
265  else
266  pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s",
267  reloid, db->db_name, reldesc);
268 }
#define _(x)
Definition: elog.c:90
int i
Definition: isn.c:73
unsigned int Oid
Definition: postgres_ext.h:31

References _, DbInfo::db_name, 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().