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)
 
static void get_db_subscription_count (DbInfo *dbinfo)
 
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)
 
int count_old_cluster_subscriptions (void)
 

Function Documentation

◆ count_old_cluster_logical_slots()

int count_old_cluster_logical_slots ( void  )

Definition at line 740 of file info.c.

741 {
742  int slot_count = 0;
743 
744  for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
745  slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots;
746 
747  return slot_count;
748 }
ClusterInfo old_cluster
Definition: pg_upgrade.c:71
DbInfoArr dbarr
Definition: pg_upgrade.h:286
DbInfo * dbs
Definition: pg_upgrade.h:217
LogicalSlotInfoArr slot_arr
Definition: pg_upgrade.h:199

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().

◆ count_old_cluster_subscriptions()

int count_old_cluster_subscriptions ( void  )

Definition at line 789 of file info.c.

790 {
791  int nsubs = 0;
792 
793  for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
794  nsubs += old_cluster.dbarr.dbs[dbnum].nsubs;
795 
796  return nsubs;
797 }
int nsubs
Definition: pg_upgrade.h:200

References ClusterInfo::dbarr, DbInfoArr::dbs, DbInfoArr::ndbs, DbInfo::nsubs, and old_cluster.

Referenced by check_new_cluster_subscription_configuration().

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

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

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

801 {
802  int dbnum;
803 
804  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
805  {
806  free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
807  pg_free(db_arr->dbs[dbnum].db_name);
808  }
809  pg_free(db_arr->dbs);
810  db_arr->dbs = NULL;
811  db_arr->ndbs = 0;
812 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static void free_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:816
char * db_name
Definition: pg_upgrade.h:195
RelInfoArr rel_arr
Definition: pg_upgrade.h:198

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

817 {
818  int relnum;
819 
820  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
821  {
822  if (rel_arr->rels[relnum].nsp_alloc)
823  pg_free(rel_arr->rels[relnum].nspname);
824  pg_free(rel_arr->rels[relnum].relname);
825  if (rel_arr->rels[relnum].tblsp_alloc)
826  pg_free(rel_arr->rels[relnum].tablespace);
827  }
828  pg_free(rel_arr->rels);
829  rel_arr->nrels = 0;
830 }
RelInfo * rels
Definition: pg_upgrade.h:149
bool tblsp_alloc
Definition: pg_upgrade.h:144
bool nsp_alloc
Definition: pg_upgrade.h:143

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

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

388 {
389  PGconn *conn = connectToServer(cluster, "template1");
390  PGresult *res;
391  int ntups;
392  int tupnum;
393  DbInfo *dbinfos;
394  int i_datname,
395  i_oid,
396  i_spclocation;
397  char query[QUERY_ALLOC];
398 
399  snprintf(query, sizeof(query),
400  "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
401  if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
402  snprintf(query + strlen(query), sizeof(query) - strlen(query),
403  "datlocprovider, datlocale, ");
404  else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
405  snprintf(query + strlen(query), sizeof(query) - strlen(query),
406  "datlocprovider, daticulocale AS datlocale, ");
407  else
408  snprintf(query + strlen(query), sizeof(query) - strlen(query),
409  "'c' AS datlocprovider, NULL AS datlocale, ");
410  snprintf(query + strlen(query), sizeof(query) - strlen(query),
411  "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
412  "FROM pg_catalog.pg_database d "
413  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
414  " ON d.dattablespace = t.oid "
415  "WHERE d.datallowconn = true "
416  "ORDER BY 1");
417 
418  res = executeQueryOrDie(conn, "%s", query);
419 
420  i_oid = PQfnumber(res, "oid");
421  i_datname = PQfnumber(res, "datname");
422  i_spclocation = PQfnumber(res, "spclocation");
423 
424  ntups = PQntuples(res);
425  dbinfos = (DbInfo *) pg_malloc0(sizeof(DbInfo) * ntups);
426 
427  for (tupnum = 0; tupnum < ntups; tupnum++)
428  {
429  dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
430  dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
431  snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
432  PQgetvalue(res, tupnum, i_spclocation));
433  }
434  PQclear(res);
435 
436  PQfinish(conn);
437 
438  cluster->dbarr.dbs = dbinfos;
439  cluster->dbarr.ndbs = ntups;
440 }
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4875
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 280 of file info.c.

281 {
282  int dbnum;
283 
284  if (cluster->dbarr.dbs != NULL)
286 
289 
290  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
291  {
292  DbInfo *pDbInfo = &cluster->dbarr.dbs[dbnum];
293 
294  get_rel_infos(cluster, pDbInfo);
295 
296  /*
297  * Retrieve the logical replication slots infos and the subscriptions
298  * count for the old cluster.
299  */
300  if (cluster == &old_cluster)
301  {
302  get_old_cluster_logical_slot_infos(pDbInfo, live_check);
303  get_db_subscription_count(pDbInfo);
304  }
305  }
306 
307  if (cluster == &old_cluster)
308  pg_log(PG_VERBOSE, "\nsource databases:");
309  else
310  pg_log(PG_VERBOSE, "\ntarget databases:");
311 
312  if (log_opts.verbose)
313  print_db_infos(&cluster->dbarr);
314 }
static void get_template0_info(ClusterInfo *cluster)
Definition: info.c:322
static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check)
Definition: info.c:648
static void get_db_subscription_count(DbInfo *dbinfo)
Definition: info.c:760
static void get_db_infos(ClusterInfo *cluster)
Definition: info.c:387
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition: info.c:800
static void print_db_infos(DbInfoArr *db_arr)
Definition: info.c:834
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
Definition: info.c:453
LogOpts log_opts
Definition: util.c:17
@ PG_VERBOSE
Definition: pg_upgrade.h:268
bool verbose
Definition: pg_upgrade.h:308

References cluster(), free_db_and_rel_infos(), get_db_infos(), get_db_subscription_count(), 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_db_subscription_count()

static void get_db_subscription_count ( DbInfo dbinfo)
static

Definition at line 760 of file info.c.

761 {
762  PGconn *conn;
763  PGresult *res;
764 
765  /* Subscriptions can be migrated since PG17. */
767  return;
768 
769  conn = connectToServer(&old_cluster, dbinfo->db_name);
770  res = executeQueryOrDie(conn, "SELECT count(*) "
771  "FROM pg_catalog.pg_subscription WHERE subdbid = %u",
772  dbinfo->db_oid);
773  dbinfo->nsubs = atoi(PQgetvalue(res, 0, 0));
774 
775  PQclear(res);
776  PQfinish(conn);
777 }
static struct LogicalRepInfo * dbinfo
uint32 major_version
Definition: pg_upgrade.h:295

References conn, connectToServer(), dbinfo, executeQueryOrDie(), GET_MAJOR_VERSION, ClusterInfo::major_version, old_cluster, PQclear(), PQfinish(), PQgetvalue(), and res.

Referenced by get_db_rel_and_slot_infos().

◆ get_old_cluster_logical_slot_infos()

static void get_old_cluster_logical_slot_infos ( DbInfo dbinfo,
bool  live_check 
)
static

Definition at line 648 of file info.c.

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

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

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

static void get_template0_info ( ClusterInfo cluster)
static

Definition at line 322 of file info.c.

323 {
324  PGconn *conn = connectToServer(cluster, "template1");
326  PGresult *dbres;
327  int i_datencoding;
328  int i_datlocprovider;
329  int i_datcollate;
330  int i_datctype;
331  int i_datlocale;
332 
333  if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
334  dbres = executeQueryOrDie(conn,
335  "SELECT encoding, datlocprovider, "
336  " datcollate, datctype, datlocale "
337  "FROM pg_catalog.pg_database "
338  "WHERE datname='template0'");
339  else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
340  dbres = executeQueryOrDie(conn,
341  "SELECT encoding, datlocprovider, "
342  " datcollate, datctype, daticulocale AS datlocale "
343  "FROM pg_catalog.pg_database "
344  "WHERE datname='template0'");
345  else
346  dbres = executeQueryOrDie(conn,
347  "SELECT encoding, 'c' AS datlocprovider, "
348  " datcollate, datctype, NULL AS datlocale "
349  "FROM pg_catalog.pg_database "
350  "WHERE datname='template0'");
351 
352 
353  if (PQntuples(dbres) != 1)
354  pg_fatal("template0 not found");
355 
356  locale = pg_malloc(sizeof(DbLocaleInfo));
357 
358  i_datencoding = PQfnumber(dbres, "encoding");
359  i_datlocprovider = PQfnumber(dbres, "datlocprovider");
360  i_datcollate = PQfnumber(dbres, "datcollate");
361  i_datctype = PQfnumber(dbres, "datctype");
362  i_datlocale = PQfnumber(dbres, "datlocale");
363 
364  locale->db_encoding = atoi(PQgetvalue(dbres, 0, i_datencoding));
365  locale->db_collprovider = PQgetvalue(dbres, 0, i_datlocprovider)[0];
366  locale->db_collate = pg_strdup(PQgetvalue(dbres, 0, i_datcollate));
367  locale->db_ctype = pg_strdup(PQgetvalue(dbres, 0, i_datctype));
368  if (PQgetisnull(dbres, 0, i_datlocale))
369  locale->db_locale = NULL;
370  else
371  locale->db_locale = pg_strdup(PQgetvalue(dbres, 0, i_datlocale));
372 
373  cluster->template0 = locale;
374 
375  PQclear(dbres);
376  PQfinish(conn);
377 }
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 834 of file info.c.

835 {
836  int dbnum;
837 
838  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
839  {
840  DbInfo *pDbInfo = &db_arr->dbs[dbnum];
841 
842  pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
843  print_rel_infos(&pDbInfo->rel_arr);
844  print_slot_infos(&pDbInfo->slot_arr);
845  }
846 }
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
Definition: info.c:863
static void print_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:850

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

851 {
852  int relnum;
853 
854  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
855  pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
856  rel_arr->rels[relnum].nspname,
857  rel_arr->rels[relnum].relname,
858  rel_arr->rels[relnum].reloid,
859  rel_arr->rels[relnum].tablespace);
860 }

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

864 {
865  /* Quick return if there are no logical slots. */
866  if (slot_arr->nslots == 0)
867  return;
868 
869  pg_log(PG_VERBOSE, "Logical replication slots within the database:");
870 
871  for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
872  {
873  LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
874 
875  pg_log(PG_VERBOSE, "slot_name: \"%s\", plugin: \"%s\", two_phase: %s",
876  slot_info->slotname,
877  slot_info->plugin,
878  slot_info->two_phase ? "true" : "false");
879  }
880 }
LogicalSlotInfo * slots
Definition: pg_upgrade.h:170

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

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