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_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 *dbinfo)
 
static void print_rel_infos (RelInfoArr *rel_arr)
 
FileNameMapgen_db_file_maps (DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
 
void print_maps (FileNameMap *maps, int n_maps, const char *db_name)
 
void get_db_and_rel_infos (ClusterInfo *cluster)
 

Function Documentation

◆ create_rel_filename_map()

static void create_rel_filename_map ( const char *  old_data,
const char *  new_data,
const DbInfo old_db,
const DbInfo new_db,
const RelInfo old_rel,
const RelInfo new_rel,
FileNameMap map 
)
static

Definition at line 165 of file info.c.

References DbInfo::db_oid, new_cluster, FileNameMap::new_db_oid, FileNameMap::new_relfilenode, FileNameMap::new_tablespace, FileNameMap::new_tablespace_suffix, RelInfo::nspname, FileNameMap::nspname, old_cluster, FileNameMap::old_db_oid, FileNameMap::old_relfilenode, FileNameMap::old_tablespace, FileNameMap::old_tablespace_suffix, RelInfo::relfilenode, RelInfo::relname, FileNameMap::relname, RelInfo::tablespace, and ClusterInfo::tablespace_suffix.

Referenced by gen_db_file_maps().

169 {
170  /* In case old/new tablespaces don't match, do them separately. */
171  if (strlen(old_rel->tablespace) == 0)
172  {
173  /*
174  * relation belongs to the default tablespace, hence relfiles should
175  * exist in the data directories.
176  */
177  map->old_tablespace = old_data;
178  map->old_tablespace_suffix = "/base";
179  }
180  else
181  {
182  /* relation belongs to a tablespace, so use the tablespace location */
183  map->old_tablespace = old_rel->tablespace;
185  }
186 
187  /* Do the same for new tablespaces */
188  if (strlen(new_rel->tablespace) == 0)
189  {
190  map->new_tablespace = new_data;
191  map->new_tablespace_suffix = "/base";
192  }
193  else
194  {
195  map->new_tablespace = new_rel->tablespace;
197  }
198 
199  map->old_db_oid = old_db->db_oid;
200  map->new_db_oid = new_db->db_oid;
201 
202  /*
203  * old_relfilenode might differ from pg_class.oid (and hence
204  * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
205  */
206  map->old_relfilenode = old_rel->relfilenode;
207 
208  /* new_relfilenode will match old and new pg_class.oid */
209  map->new_relfilenode = new_rel->relfilenode;
210 
211  /* used only for logging and error reporting, old/new are identical */
212  map->nspname = old_rel->nspname;
213  map->relname = old_rel->relname;
214 }
Oid old_relfilenode
Definition: pg_upgrade.h:168
char * relname
Definition: pg_upgrade.h:136
Oid new_db_oid
Definition: pg_upgrade.h:162
char * nspname
Definition: pg_upgrade.h:135
Oid db_oid
Definition: pg_upgrade.h:180
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
const char * old_tablespace
Definition: pg_upgrade.h:157
Oid new_relfilenode
Definition: pg_upgrade.h:169
Oid relfilenode
Definition: pg_upgrade.h:138
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
const char * old_tablespace_suffix
Definition: pg_upgrade.h:159
const char * tablespace_suffix
Definition: pg_upgrade.h:272
Oid old_db_oid
Definition: pg_upgrade.h:161
char * nspname
Definition: pg_upgrade.h:171
char * tablespace
Definition: pg_upgrade.h:141
const char * new_tablespace
Definition: pg_upgrade.h:158
const char * new_tablespace_suffix
Definition: pg_upgrade.h:160
char * relname
Definition: pg_upgrade.h:172

◆ free_db_and_rel_infos()

static void free_db_and_rel_infos ( DbInfoArr db_arr)
static

Definition at line 591 of file info.c.

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

Referenced by get_db_and_rel_infos().

592 {
593  int dbnum;
594 
595  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
596  {
597  free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
598  pg_free(db_arr->dbs[dbnum].db_name);
599  }
600  pg_free(db_arr->dbs);
601  db_arr->dbs = NULL;
602  db_arr->ndbs = 0;
603 }
static void free_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:607
RelInfoArr rel_arr
Definition: pg_upgrade.h:187
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * db_name
Definition: pg_upgrade.h:181
DbInfo * dbs
Definition: pg_upgrade.h:192

◆ free_rel_infos()

static void free_rel_infos ( RelInfoArr rel_arr)
static

Definition at line 607 of file info.c.

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

608 {
609  int relnum;
610 
611  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
612  {
613  if (rel_arr->rels[relnum].nsp_alloc)
614  pg_free(rel_arr->rels[relnum].nspname);
615  pg_free(rel_arr->rels[relnum].relname);
616  if (rel_arr->rels[relnum].tblsp_alloc)
617  pg_free(rel_arr->rels[relnum].tablespace);
618  }
619  pg_free(rel_arr->rels);
620  rel_arr->nrels = 0;
621 }
char * relname
Definition: pg_upgrade.h:136
bool tblsp_alloc
Definition: pg_upgrade.h:143
char * nspname
Definition: pg_upgrade.h:135
RelInfo * rels
Definition: pg_upgrade.h:148
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * tablespace
Definition: pg_upgrade.h:141
bool nsp_alloc
Definition: pg_upgrade.h:142

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

References create_rel_filename_map(), DbInfo::db_name, GET_MAJOR_VERSION, ClusterInfo::major_version, maps, RelInfoArr::nrels, RelInfo::nspname, old_cluster, 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().

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

◆ get_db_and_rel_infos()

void get_db_and_rel_infos ( ClusterInfo cluster)

Definition at line 309 of file info.c.

References ClusterInfo::dbarr, DbInfoArr::dbs, free_db_and_rel_infos(), get_db_infos(), get_rel_infos(), log_opts, DbInfoArr::ndbs, 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().

310 {
311  int dbnum;
312 
313  if (cluster->dbarr.dbs != NULL)
314  free_db_and_rel_infos(&cluster->dbarr);
315 
316  get_db_infos(cluster);
317 
318  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
319  get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
320 
321  if (cluster == &old_cluster)
322  pg_log(PG_VERBOSE, "\nsource databases:\n");
323  else
324  pg_log(PG_VERBOSE, "\ntarget databases:\n");
325 
326  if (log_opts.verbose)
327  print_db_infos(&cluster->dbarr);
328 }
static void get_db_infos(ClusterInfo *cluster)
Definition: info.c:338
LogOpts log_opts
Definition: util.c:17
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
Definition: info.c:407
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
static void print_db_infos(DbInfoArr *dbinfo)
Definition: info.c:625
bool verbose
Definition: pg_upgrade.h:282
DbInfoArr dbarr
Definition: pg_upgrade.h:260
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition: info.c:591
DbInfo * dbs
Definition: pg_upgrade.h:192

◆ get_db_infos()

static void get_db_infos ( ClusterInfo cluster)
static

Definition at line 338 of file info.c.

References atooid, conn, connectToServer(), DbInfo::db_collate, DbInfo::db_ctype, DbInfo::db_encoding, DbInfo::db_name, DbInfo::db_oid, ClusterInfo::dbarr, DbInfoArr::dbs, executeQueryOrDie(), GET_MAJOR_VERSION, ClusterInfo::major_version, DbInfoArr::ndbs, pg_malloc(), pg_strdup(), PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), QUERY_ALLOC, and snprintf.

Referenced by get_db_and_rel_infos().

339 {
340  PGconn *conn = connectToServer(cluster, "template1");
341  PGresult *res;
342  int ntups;
343  int tupnum;
344  DbInfo *dbinfos;
345  int i_datname,
346  i_oid,
347  i_encoding,
348  i_datcollate,
349  i_datctype,
350  i_spclocation;
351  char query[QUERY_ALLOC];
352 
353  snprintf(query, sizeof(query),
354  "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, "
355  "%s AS spclocation "
356  "FROM pg_catalog.pg_database d "
357  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
358  " ON d.dattablespace = t.oid "
359  "WHERE d.datallowconn = true "
360  /* we don't preserve pg_database.oid so we sort by name */
361  "ORDER BY 2",
362  /* 9.2 removed the spclocation column */
363  (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
364  "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
365 
366  res = executeQueryOrDie(conn, "%s", query);
367 
368  i_oid = PQfnumber(res, "oid");
369  i_datname = PQfnumber(res, "datname");
370  i_encoding = PQfnumber(res, "encoding");
371  i_datcollate = PQfnumber(res, "datcollate");
372  i_datctype = PQfnumber(res, "datctype");
373  i_spclocation = PQfnumber(res, "spclocation");
374 
375  ntups = PQntuples(res);
376  dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
377 
378  for (tupnum = 0; tupnum < ntups; tupnum++)
379  {
380  dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
381  dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
382  dbinfos[tupnum].db_encoding = atoi(PQgetvalue(res, tupnum, i_encoding));
383  dbinfos[tupnum].db_collate = pg_strdup(PQgetvalue(res, tupnum, i_datcollate));
384  dbinfos[tupnum].db_ctype = pg_strdup(PQgetvalue(res, tupnum, i_datctype));
385  snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
386  PQgetvalue(res, tupnum, i_spclocation));
387  }
388  PQclear(res);
389 
390  PQfinish(conn);
391 
392  cluster->dbarr.dbs = dbinfos;
393  cluster->dbarr.ndbs = ntups;
394 }
uint32 major_version
Definition: pg_upgrade.h:269
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
Oid db_oid
Definition: pg_upgrade.h:180
char * db_ctype
Definition: pg_upgrade.h:185
PGconn * conn
Definition: streamutil.c:54
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define atooid(x)
Definition: postgres_ext.h:42
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:27
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694
DbInfoArr dbarr
Definition: pg_upgrade.h:260
int db_encoding
Definition: pg_upgrade.h:186
char * db_name
Definition: pg_upgrade.h:181
char * db_collate
Definition: pg_upgrade.h:184
DbInfo * dbs
Definition: pg_upgrade.h:192
#define snprintf
Definition: port.h:192
#define QUERY_ALLOC
Definition: pg_upgrade.h:19

◆ get_rel_infos()

static void get_rel_infos ( ClusterInfo cluster,
DbInfo dbinfo 
)
static

Definition at line 407 of file info.c.

References atooid, conn, connectToServer(), CppAsString2, DbInfo::db_name, DbInfo::db_tablespace, executeQueryOrDie(), FirstNormalObjectId, GET_MAJOR_VERSION, RelInfo::indtable, ClusterInfo::major_version, RelInfoArr::nrels, RelInfo::nsp_alloc, RelInfo::nspname, pg_malloc(), pg_strdup(), PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), QUERY_ALLOC, DbInfo::rel_arr, RelInfo::relfilenode, relname, RelInfo::relname, RelInfo::reloid, RelInfoArr::rels, snprintf, RelInfo::tablespace, tablespace, RelInfo::tblsp_alloc, and RelInfo::toastheap.

Referenced by get_db_and_rel_infos().

408 {
409  PGconn *conn = connectToServer(cluster,
410  dbinfo->db_name);
411  PGresult *res;
412  RelInfo *relinfos;
413  int ntups;
414  int relnum;
415  int num_rels = 0;
416  char *nspname = NULL;
417  char *relname = NULL;
418  char *tablespace = NULL;
419  int i_spclocation,
420  i_nspname,
421  i_relname,
422  i_reloid,
423  i_indtable,
424  i_toastheap,
425  i_relfilenode,
426  i_reltablespace;
427  char query[QUERY_ALLOC];
428  char *last_namespace = NULL,
429  *last_tablespace = NULL;
430 
431  query[0] = '\0'; /* initialize query string to empty */
432 
433  /*
434  * Create a CTE that collects OIDs of regular user tables, including
435  * matviews and sequences, but excluding toast tables and indexes. We
436  * assume that relations with OIDs >= FirstNormalObjectId belong to the
437  * user. (That's probably redundant with the namespace-name exclusions,
438  * but let's be safe.)
439  *
440  * pg_largeobject contains user data that does not appear in pg_dump
441  * output, so we have to copy that system table. It's easiest to do that
442  * by treating it as a user table.
443  */
444  snprintf(query + strlen(query), sizeof(query) - strlen(query),
445  "WITH regular_heap (reloid, indtable, toastheap) AS ( "
446  " SELECT c.oid, 0::oid, 0::oid "
447  " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
448  " ON c.relnamespace = n.oid "
449  " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
450  CppAsString2(RELKIND_MATVIEW) ") AND "
451  /* exclude possible orphaned temp tables */
452  " ((n.nspname !~ '^pg_temp_' AND "
453  " n.nspname !~ '^pg_toast_temp_' AND "
454  " n.nspname NOT IN ('pg_catalog', 'information_schema', "
455  " 'binary_upgrade', 'pg_toast') AND "
456  " c.oid >= %u::pg_catalog.oid) OR "
457  " (n.nspname = 'pg_catalog' AND "
458  " relname IN ('pg_largeobject') ))), ",
460 
461  /*
462  * Add a CTE that collects OIDs of toast tables belonging to the tables
463  * selected by the regular_heap CTE. (We have to do this separately
464  * because the namespace-name rules above don't work for toast tables.)
465  */
466  snprintf(query + strlen(query), sizeof(query) - strlen(query),
467  " toast_heap (reloid, indtable, toastheap) AS ( "
468  " SELECT c.reltoastrelid, 0::oid, c.oid "
469  " FROM regular_heap JOIN pg_catalog.pg_class c "
470  " ON regular_heap.reloid = c.oid "
471  " WHERE c.reltoastrelid != 0), ");
472 
473  /*
474  * Add a CTE that collects OIDs of all valid indexes on the previously
475  * selected tables. We can ignore invalid indexes since pg_dump does.
476  * Testing indisready is necessary in 9.2, and harmless in earlier/later
477  * versions.
478  */
479  snprintf(query + strlen(query), sizeof(query) - strlen(query),
480  " all_index (reloid, indtable, toastheap) AS ( "
481  " SELECT indexrelid, indrelid, 0::oid "
482  " FROM pg_catalog.pg_index "
483  " WHERE indisvalid AND indisready "
484  " AND indrelid IN "
485  " (SELECT reloid FROM regular_heap "
486  " UNION ALL "
487  " SELECT reloid FROM toast_heap)) ");
488 
489  /*
490  * And now we can write the query that retrieves the data we want for each
491  * heap and index relation. Make sure result is sorted by OID.
492  */
493  snprintf(query + strlen(query), sizeof(query) - strlen(query),
494  "SELECT all_rels.*, n.nspname, c.relname, "
495  " c.relfilenode, c.reltablespace, %s "
496  "FROM (SELECT * FROM regular_heap "
497  " UNION ALL "
498  " SELECT * FROM toast_heap "
499  " UNION ALL "
500  " SELECT * FROM all_index) all_rels "
501  " JOIN pg_catalog.pg_class c "
502  " ON all_rels.reloid = c.oid "
503  " JOIN pg_catalog.pg_namespace n "
504  " ON c.relnamespace = n.oid "
505  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
506  " ON c.reltablespace = t.oid "
507  "ORDER BY 1;",
508  /* 9.2 removed the pg_tablespace.spclocation column */
509  (GET_MAJOR_VERSION(cluster->major_version) >= 902) ?
510  "pg_catalog.pg_tablespace_location(t.oid) AS spclocation" :
511  "t.spclocation");
512 
513  res = executeQueryOrDie(conn, "%s", query);
514 
515  ntups = PQntuples(res);
516 
517  relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
518 
519  i_reloid = PQfnumber(res, "reloid");
520  i_indtable = PQfnumber(res, "indtable");
521  i_toastheap = PQfnumber(res, "toastheap");
522  i_nspname = PQfnumber(res, "nspname");
523  i_relname = PQfnumber(res, "relname");
524  i_relfilenode = PQfnumber(res, "relfilenode");
525  i_reltablespace = PQfnumber(res, "reltablespace");
526  i_spclocation = PQfnumber(res, "spclocation");
527 
528  for (relnum = 0; relnum < ntups; relnum++)
529  {
530  RelInfo *curr = &relinfos[num_rels++];
531 
532  curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
533  curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
534  curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
535 
536  nspname = PQgetvalue(res, relnum, i_nspname);
537  curr->nsp_alloc = false;
538 
539  /*
540  * Many of the namespace and tablespace strings are identical, so we
541  * try to reuse the allocated string pointers where possible to reduce
542  * memory consumption.
543  */
544  /* Can we reuse the previous string allocation? */
545  if (last_namespace && strcmp(nspname, last_namespace) == 0)
546  curr->nspname = last_namespace;
547  else
548  {
549  last_namespace = curr->nspname = pg_strdup(nspname);
550  curr->nsp_alloc = true;
551  }
552 
553  relname = PQgetvalue(res, relnum, i_relname);
554  curr->relname = pg_strdup(relname);
555 
556  curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
557  curr->tblsp_alloc = false;
558 
559  /* Is the tablespace oid non-default? */
560  if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
561  {
562  /*
563  * The tablespace location might be "", meaning the cluster
564  * default location, i.e. pg_default or pg_global.
565  */
566  tablespace = PQgetvalue(res, relnum, i_spclocation);
567 
568  /* Can we reuse the previous string allocation? */
569  if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
570  curr->tablespace = last_tablespace;
571  else
572  {
573  last_tablespace = curr->tablespace = pg_strdup(tablespace);
574  curr->tblsp_alloc = true;
575  }
576  }
577  else
578  /* A zero reltablespace oid indicates the database tablespace. */
579  curr->tablespace = dbinfo->db_tablespace;
580  }
581  PQclear(res);
582 
583  PQfinish(conn);
584 
585  dbinfo->rel_arr.rels = relinfos;
586  dbinfo->rel_arr.nrels = num_rels;
587 }
uint32 major_version
Definition: pg_upgrade.h:269
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
char * relname
Definition: pg_upgrade.h:136
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4125
bool tblsp_alloc
Definition: pg_upgrade.h:143
NameData relname
Definition: pg_class.h:35
Oid reloid
Definition: pg_upgrade.h:137
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
char * nspname
Definition: pg_upgrade.h:135
#define FirstNormalObjectId
Definition: transam.h:141
char db_tablespace[MAXPGPATH]
Definition: pg_upgrade.h:182
PGconn * conn
Definition: streamutil.c:54
RelInfo * rels
Definition: pg_upgrade.h:148
Oid relfilenode
Definition: pg_upgrade.h:138
char * tablespace
Definition: pgbench.c:189
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define atooid(x)
Definition: postgres_ext.h:42
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
Definition: server.c:27
Oid indtable
Definition: pg_upgrade.h:139
#define CppAsString2(x)
Definition: c.h:224
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:2877
void PQclear(PGresult *res)
Definition: fe-exec.c:694
RelInfoArr rel_arr
Definition: pg_upgrade.h:187
char * tablespace
Definition: pg_upgrade.h:141
bool nsp_alloc
Definition: pg_upgrade.h:142
char * db_name
Definition: pg_upgrade.h:181
#define snprintf
Definition: port.h:192
#define QUERY_ALLOC
Definition: pg_upgrade.h:19
Oid toastheap
Definition: pg_upgrade.h:140

◆ print_db_infos()

static void print_db_infos ( DbInfoArr dbinfo)
static

Definition at line 625 of file info.c.

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

Referenced by get_db_and_rel_infos().

626 {
627  int dbnum;
628 
629  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
630  {
631  pg_log(PG_VERBOSE, "Database: %s\n", db_arr->dbs[dbnum].db_name);
632  print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
633  pg_log(PG_VERBOSE, "\n\n");
634  }
635 }
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
static void print_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:639

◆ print_maps()

void print_maps ( FileNameMap maps,
int  n_maps,
const char *  db_name 
)

Definition at line 283 of file info.c.

References log_opts, pg_log(), PG_VERBOSE, relname, and LogOpts::verbose.

Referenced by transfer_all_new_dbs().

284 {
285  if (log_opts.verbose)
286  {
287  int mapnum;
288 
289  pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
290 
291  for (mapnum = 0; mapnum < n_maps; mapnum++)
292  pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
293  maps[mapnum].nspname, maps[mapnum].relname,
294  maps[mapnum].old_relfilenode,
295  maps[mapnum].new_relfilenode);
296 
297  pg_log(PG_VERBOSE, "\n\n");
298  }
299 }
LogOpts log_opts
Definition: util.c:17
NameData relname
Definition: pg_class.h:35
bool verbose
Definition: pg_upgrade.h:282
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2

◆ print_rel_infos()

static void print_rel_infos ( RelInfoArr rel_arr)
static

Definition at line 639 of file info.c.

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

Referenced by print_db_infos().

640 {
641  int relnum;
642 
643  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
644  pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
645  rel_arr->rels[relnum].nspname,
646  rel_arr->rels[relnum].relname,
647  rel_arr->rels[relnum].reloid,
648  rel_arr->rels[relnum].tablespace);
649 }
char * relname
Definition: pg_upgrade.h:136
Oid reloid
Definition: pg_upgrade.h:137
char * nspname
Definition: pg_upgrade.h:135
RelInfo * rels
Definition: pg_upgrade.h:148
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
char * tablespace
Definition: pg_upgrade.h:141

◆ report_unmatched_relation()

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

Definition at line 222 of file info.c.

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

223 {
224  Oid reloid = rel->reloid; /* we might change rel below */
225  char reldesc[1000];
226  int i;
227 
228  snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
229  rel->nspname, rel->relname);
230  if (rel->indtable)
231  {
232  for (i = 0; i < db->rel_arr.nrels; i++)
233  {
234  const RelInfo *hrel = &db->rel_arr.rels[i];
235 
236  if (hrel->reloid == rel->indtable)
237  {
238  snprintf(reldesc + strlen(reldesc),
239  sizeof(reldesc) - strlen(reldesc),
240  _(" which is an index on \"%s.%s\""),
241  hrel->nspname, hrel->relname);
242  /* Shift attention to index's table for toast check */
243  rel = hrel;
244  break;
245  }
246  }
247  if (i >= db->rel_arr.nrels)
248  snprintf(reldesc + strlen(reldesc),
249  sizeof(reldesc) - strlen(reldesc),
250  _(" which is an index on OID %u"), rel->indtable);
251  }
252  if (rel->toastheap)
253  {
254  for (i = 0; i < db->rel_arr.nrels; i++)
255  {
256  const RelInfo *brel = &db->rel_arr.rels[i];
257 
258  if (brel->reloid == rel->toastheap)
259  {
260  snprintf(reldesc + strlen(reldesc),
261  sizeof(reldesc) - strlen(reldesc),
262  _(" which is the TOAST table for \"%s.%s\""),
263  brel->nspname, brel->relname);
264  break;
265  }
266  }
267  if (i >= db->rel_arr.nrels)
268  snprintf(reldesc + strlen(reldesc),
269  sizeof(reldesc) - strlen(reldesc),
270  _(" which is the TOAST table for OID %u"), rel->toastheap);
271  }
272 
273  if (is_new_db)
274  pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s\n",
275  reloid, db->db_name, reldesc);
276  else
277  pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s\n",
278  reloid, db->db_name, reldesc);
279 }
char * relname
Definition: pg_upgrade.h:136
Oid reloid
Definition: pg_upgrade.h:137
unsigned int Oid
Definition: postgres_ext.h:31
char * nspname
Definition: pg_upgrade.h:135
RelInfo * rels
Definition: pg_upgrade.h:148
Oid indtable
Definition: pg_upgrade.h:139
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
RelInfoArr rel_arr
Definition: pg_upgrade.h:187
int i
char * db_name
Definition: pg_upgrade.h:181
#define snprintf
Definition: port.h:192
#define _(x)
Definition: elog.c:87
Oid toastheap
Definition: pg_upgrade.h:140