PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
info.c File Reference
#include "postgres_fe.h"
#include "pg_upgrade.h"
#include "access/transam.h"
#include "catalog/pg_class.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

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

171 {
172  /* In case old/new tablespaces don't match, do them separately. */
173  if (strlen(old_rel->tablespace) == 0)
174  {
175  /*
176  * relation belongs to the default tablespace, hence relfiles should
177  * exist in the data directories.
178  */
179  map->old_tablespace = old_data;
180  map->old_tablespace_suffix = "/base";
181  }
182  else
183  {
184  /* relation belongs to a tablespace, so use the tablespace location */
185  map->old_tablespace = old_rel->tablespace;
187  }
188 
189  /* Do the same for new tablespaces */
190  if (strlen(new_rel->tablespace) == 0)
191  {
192  map->new_tablespace = new_data;
193  map->new_tablespace_suffix = "/base";
194  }
195  else
196  {
197  map->new_tablespace = new_rel->tablespace;
199  }
200 
201  map->old_db_oid = old_db->db_oid;
202  map->new_db_oid = new_db->db_oid;
203 
204  /*
205  * old_relfilenode might differ from pg_class.oid (and hence
206  * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
207  */
208  map->old_relfilenode = old_rel->relfilenode;
209 
210  /* new_relfilenode will match old and new pg_class.oid */
211  map->new_relfilenode = new_rel->relfilenode;
212 
213  /* used only for logging and error reporing, old/new are identical */
214  map->nspname = old_rel->nspname;
215  map->relname = old_rel->relname;
216 }
Oid old_relfilenode
Definition: pg_upgrade.h:172
char * relname
Definition: pg_upgrade.h:140
Oid new_db_oid
Definition: pg_upgrade.h:166
char * nspname
Definition: pg_upgrade.h:139
Oid db_oid
Definition: pg_upgrade.h:184
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
const char * old_tablespace
Definition: pg_upgrade.h:161
Oid new_relfilenode
Definition: pg_upgrade.h:173
Oid relfilenode
Definition: pg_upgrade.h:142
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
const char * old_tablespace_suffix
Definition: pg_upgrade.h:163
const char * tablespace_suffix
Definition: pg_upgrade.h:275
Oid old_db_oid
Definition: pg_upgrade.h:165
char * nspname
Definition: pg_upgrade.h:175
char * tablespace
Definition: pg_upgrade.h:145
const char * new_tablespace
Definition: pg_upgrade.h:162
const char * new_tablespace_suffix
Definition: pg_upgrade.h:164
char * relname
Definition: pg_upgrade.h:176
static void free_db_and_rel_infos ( DbInfoArr db_arr)
static

Definition at line 592 of file info.c.

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

Referenced by get_db_and_rel_infos().

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

Definition at line 608 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().

609 {
610  int relnum;
611 
612  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
613  {
614  if (rel_arr->rels[relnum].nsp_alloc)
615  pg_free(rel_arr->rels[relnum].nspname);
616  pg_free(rel_arr->rels[relnum].relname);
617  if (rel_arr->rels[relnum].tblsp_alloc)
618  pg_free(rel_arr->rels[relnum].tablespace);
619  }
620  pg_free(rel_arr->rels);
621  rel_arr->nrels = 0;
622 }
char * relname
Definition: pg_upgrade.h:140
bool tblsp_alloc
Definition: pg_upgrade.h:147
char * nspname
Definition: pg_upgrade.h:139
RelInfo * rels
Definition: pg_upgrade.h:152
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * tablespace
Definition: pg_upgrade.h:145
bool nsp_alloc
Definition: pg_upgrade.h:146
FileNameMap* gen_db_file_maps ( DbInfo old_db,
DbInfo new_db,
int *  nmaps,
const char *  old_pgdata,
const char *  new_pgdata 
)

Definition at line 41 of file info.c.

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

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

Definition at line 311 of file info.c.

References CLUSTER_NAME, ClusterInfo::dbarr, DbInfoArr::dbs, free_db_and_rel_infos(), get_db_infos(), get_rel_infos(), log_opts, DbInfoArr::ndbs, NULL, pg_log(), PG_VERBOSE, print_db_infos(), and LogOpts::verbose.

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

312 {
313  int dbnum;
314 
315  if (cluster->dbarr.dbs != NULL)
316  free_db_and_rel_infos(&cluster->dbarr);
317 
318  get_db_infos(cluster);
319 
320  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
321  get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
322 
323  pg_log(PG_VERBOSE, "\n%s databases:\n", CLUSTER_NAME(cluster));
324  if (log_opts.verbose)
325  print_db_infos(&cluster->dbarr);
326 }
static void get_db_infos(ClusterInfo *cluster)
Definition: info.c:336
LogOpts log_opts
Definition: util.c:18
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
Definition: info.c:405
#define CLUSTER_NAME(cluster)
Definition: pg_upgrade.h:97
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
static void print_db_infos(DbInfoArr *dbinfo)
Definition: info.c:626
bool verbose
Definition: pg_upgrade.h:285
DbInfoArr dbarr
Definition: pg_upgrade.h:263
#define NULL
Definition: c.h:229
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition: info.c:592
DbInfo * dbs
Definition: pg_upgrade.h:196
static void get_db_infos ( ClusterInfo cluster)
static

Definition at line 336 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().

337 {
338  PGconn *conn = connectToServer(cluster, "template1");
339  PGresult *res;
340  int ntups;
341  int tupnum;
342  DbInfo *dbinfos;
343  int i_datname,
344  i_oid,
345  i_encoding,
346  i_datcollate,
347  i_datctype,
348  i_spclocation;
349  char query[QUERY_ALLOC];
350 
351  snprintf(query, sizeof(query),
352  "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, "
353  "%s AS spclocation "
354  "FROM pg_catalog.pg_database d "
355  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
356  " ON d.dattablespace = t.oid "
357  "WHERE d.datallowconn = true "
358  /* we don't preserve pg_database.oid so we sort by name */
359  "ORDER BY 2",
360  /* 9.2 removed the spclocation column */
361  (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
362  "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
363 
364  res = executeQueryOrDie(conn, "%s", query);
365 
366  i_oid = PQfnumber(res, "oid");
367  i_datname = PQfnumber(res, "datname");
368  i_encoding = PQfnumber(res, "encoding");
369  i_datcollate = PQfnumber(res, "datcollate");
370  i_datctype = PQfnumber(res, "datctype");
371  i_spclocation = PQfnumber(res, "spclocation");
372 
373  ntups = PQntuples(res);
374  dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
375 
376  for (tupnum = 0; tupnum < ntups; tupnum++)
377  {
378  dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
379  dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
380  dbinfos[tupnum].db_encoding = atoi(PQgetvalue(res, tupnum, i_encoding));
381  dbinfos[tupnum].db_collate = pg_strdup(PQgetvalue(res, tupnum, i_datcollate));
382  dbinfos[tupnum].db_ctype = pg_strdup(PQgetvalue(res, tupnum, i_datctype));
383  snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
384  PQgetvalue(res, tupnum, i_spclocation));
385  }
386  PQclear(res);
387 
388  PQfinish(conn);
389 
390  cluster->dbarr.dbs = dbinfos;
391  cluster->dbarr.ndbs = ntups;
392 }
uint32 major_version
Definition: pg_upgrade.h:272
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
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:29
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3521
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
#define QUERY_ALLOC
Definition: logging.c:26
Oid db_oid
Definition: pg_upgrade.h:184
char * db_ctype
Definition: pg_upgrade.h:189
PGconn * conn
Definition: streamutil.c:42
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:2781
void PQclear(PGresult *res)
Definition: fe-exec.c:650
DbInfoArr dbarr
Definition: pg_upgrade.h:263
int db_encoding
Definition: pg_upgrade.h:190
char * db_name
Definition: pg_upgrade.h:185
char * db_collate
Definition: pg_upgrade.h:188
DbInfo * dbs
Definition: pg_upgrade.h:196
static void get_rel_infos ( ClusterInfo cluster,
DbInfo dbinfo 
)
static

Definition at line 405 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, NULL, old_cluster, pg_malloc(), pg_strdup(), PQclear(), PQfinish(), PQfnumber(), PQgetvalue(), PQntuples(), QUERY_ALLOC, DbInfo::rel_arr, RelInfo::relfilenode, RELKIND_MATVIEW, RELKIND_RELATION, RelInfo::relname, RelInfo::reloid, RelInfoArr::rels, snprintf(), RelInfo::tablespace, tablespace, RelInfo::tblsp_alloc, and RelInfo::toastheap.

Referenced by get_db_and_rel_infos().

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

Definition at line 626 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().

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

Definition at line 285 of file info.c.

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

Referenced by transfer_all_new_dbs().

286 {
287  if (log_opts.verbose)
288  {
289  int mapnum;
290 
291  pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
292 
293  for (mapnum = 0; mapnum < n_maps; mapnum++)
294  pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
295  maps[mapnum].nspname, maps[mapnum].relname,
296  maps[mapnum].old_relfilenode,
297  maps[mapnum].new_relfilenode);
298 
299  pg_log(PG_VERBOSE, "\n\n");
300  }
301 }
LogOpts log_opts
Definition: util.c:18
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
bool verbose
Definition: pg_upgrade.h:285
static void print_rel_infos ( RelInfoArr rel_arr)
static

Definition at line 640 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().

641 {
642  int relnum;
643 
644  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
645  pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
646  rel_arr->rels[relnum].nspname,
647  rel_arr->rels[relnum].relname,
648  rel_arr->rels[relnum].reloid,
649  rel_arr->rels[relnum].tablespace);
650 }
char * relname
Definition: pg_upgrade.h:140
Oid reloid
Definition: pg_upgrade.h:141
char * nspname
Definition: pg_upgrade.h:139
RelInfo * rels
Definition: pg_upgrade.h:152
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
char * tablespace
Definition: pg_upgrade.h:145
static void report_unmatched_relation ( const RelInfo rel,
const DbInfo db,
bool  is_new_db 
)
static

Definition at line 224 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().

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