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

732 {
733  int slot_count = 0;
734 
735  for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
736  slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots;
737 
738  return slot_count;
739 }
ClusterInfo old_cluster
Definition: pg_upgrade.c:64
DbInfoArr dbarr
Definition: pg_upgrade.h:285
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 780 of file info.c.

781 {
782  int nsubs = 0;
783 
784  for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
785  nsubs += old_cluster.dbarr.dbs[dbnum].nsubs;
786 
787  return nsubs;
788 }
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:65
const char * tablespace_suffix
Definition: pg_upgrade.h:297
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 791 of file info.c.

792 {
793  int dbnum;
794 
795  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
796  {
797  free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
798  pg_free(db_arr->dbs[dbnum].db_name);
799  }
800  pg_free(db_arr->dbs);
801  db_arr->dbs = NULL;
802  db_arr->ndbs = 0;
803 }
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static void free_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:807
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 807 of file info.c.

808 {
809  int relnum;
810 
811  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
812  {
813  if (rel_arr->rels[relnum].nsp_alloc)
814  pg_free(rel_arr->rels[relnum].nspname);
815  pg_free(rel_arr->rels[relnum].relname);
816  if (rel_arr->rels[relnum].tblsp_alloc)
817  pg_free(rel_arr->rels[relnum].tablespace);
818  }
819  pg_free(rel_arr->rels);
820  rel_arr->nrels = 0;
821 }
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:271
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 381 of file info.c.

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

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:639
static void get_db_subscription_count(DbInfo *dbinfo)
Definition: info.c:751
static void get_db_infos(ClusterInfo *cluster)
Definition: info.c:381
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition: info.c:791
static void print_db_infos(DbInfoArr *db_arr)
Definition: info.c:825
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
Definition: info.c:444
LogOpts log_opts
Definition: util.c:17
@ PG_VERBOSE
Definition: pg_upgrade.h:267
bool verbose
Definition: pg_upgrade.h:307

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

752 {
753  PGconn *conn;
754  PGresult *res;
755 
756  /* Subscriptions can be migrated since PG17. */
758  return;
759 
761  res = executeQueryOrDie(conn, "SELECT count(*) "
762  "FROM pg_catalog.pg_subscription WHERE subdbid = %u",
763  dbinfo->db_oid);
764  dbinfo->nsubs = atoi(PQgetvalue(res, 0, 0));
765 
766  PQclear(res);
767  PQfinish(conn);
768 }
uint32 major_version
Definition: pg_upgrade.h:294

References conn, connectToServer(), DbInfo::db_name, DbInfo::db_oid, executeQueryOrDie(), GET_MAJOR_VERSION, ClusterInfo::major_version, DbInfo::nsubs, 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 639 of file info.c.

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

References LogicalSlotInfo::caught_up, conn, connectToServer(), DbInfo::db_name, executeQueryOrDie(), LogicalSlotInfo::failover, GET_MAJOR_VERSION, LogicalSlotInfo::invalid, ClusterInfo::major_version, LogicalSlotInfoArr::nslots, old_cluster, pg_malloc(), pg_strdup(), LogicalSlotInfo::plugin, PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), res, DbInfo::slot_arr, LogicalSlotInfo::slotname, LogicalSlotInfoArr::slots, 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 444 of file info.c.

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

826 {
827  int dbnum;
828 
829  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
830  {
831  DbInfo *pDbInfo = &db_arr->dbs[dbnum];
832 
833  pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
834  print_rel_infos(&pDbInfo->rel_arr);
835  print_slot_infos(&pDbInfo->slot_arr);
836  }
837 }
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
Definition: info.c:854
static void print_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:841

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

842 {
843  int relnum;
844 
845  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
846  pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
847  rel_arr->rels[relnum].nspname,
848  rel_arr->rels[relnum].relname,
849  rel_arr->rels[relnum].reloid,
850  rel_arr->rels[relnum].tablespace);
851 }

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

855 {
856  /* Quick return if there are no logical slots. */
857  if (slot_arr->nslots == 0)
858  return;
859 
860  pg_log(PG_VERBOSE, "Logical replication slots within the database:");
861 
862  for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
863  {
864  LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
865 
866  pg_log(PG_VERBOSE, "slot_name: \"%s\", plugin: \"%s\", two_phase: %s",
867  slot_info->slotname,
868  slot_info->plugin,
869  slot_info->two_phase ? "true" : "false");
870  }
871 }

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