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:170
char * relname
Definition: pg_upgrade.h:138
Oid new_db_oid
Definition: pg_upgrade.h:164
char * nspname
Definition: pg_upgrade.h:137
Oid db_oid
Definition: pg_upgrade.h:182
ClusterInfo new_cluster
Definition: pg_upgrade.c:56
const char * old_tablespace
Definition: pg_upgrade.h:159
Oid new_relfilenode
Definition: pg_upgrade.h:171
Oid relfilenode
Definition: pg_upgrade.h:140
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
const char * old_tablespace_suffix
Definition: pg_upgrade.h:161
const char * tablespace_suffix
Definition: pg_upgrade.h:273
Oid old_db_oid
Definition: pg_upgrade.h:163
char * nspname
Definition: pg_upgrade.h:173
char * tablespace
Definition: pg_upgrade.h:143
const char * new_tablespace
Definition: pg_upgrade.h:160
const char * new_tablespace_suffix
Definition: pg_upgrade.h:162
char * relname
Definition: pg_upgrade.h:174
static void free_db_and_rel_infos ( DbInfoArr db_arr)
static

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

597 {
598  int dbnum;
599 
600  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
601  {
602  free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
603  pg_free(db_arr->dbs[dbnum].db_name);
604  }
605  pg_free(db_arr->dbs);
606  db_arr->dbs = NULL;
607  db_arr->ndbs = 0;
608 }
static void free_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:612
#define NULL
Definition: c.h:229
RelInfoArr rel_arr
Definition: pg_upgrade.h:189
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * db_name
Definition: pg_upgrade.h:183
DbInfo * dbs
Definition: pg_upgrade.h:194
static void free_rel_infos ( RelInfoArr rel_arr)
static

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

613 {
614  int relnum;
615 
616  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
617  {
618  if (rel_arr->rels[relnum].nsp_alloc)
619  pg_free(rel_arr->rels[relnum].nspname);
620  pg_free(rel_arr->rels[relnum].relname);
621  if (rel_arr->rels[relnum].tblsp_alloc)
622  pg_free(rel_arr->rels[relnum].tablespace);
623  }
624  pg_free(rel_arr->rels);
625  rel_arr->nrels = 0;
626 }
char * relname
Definition: pg_upgrade.h:138
bool tblsp_alloc
Definition: pg_upgrade.h:145
char * nspname
Definition: pg_upgrade.h:137
RelInfo * rels
Definition: pg_upgrade.h:150
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * tablespace
Definition: pg_upgrade.h:143
bool nsp_alloc
Definition: pg_upgrade.h:144
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:270
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:138
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
Oid reloid
Definition: pg_upgrade.h:139
char * nspname
Definition: pg_upgrade.h:137
RelInfo * rels
Definition: pg_upgrade.h:150
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:189
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:183
void get_db_and_rel_infos ( ClusterInfo cluster)

Definition at line 311 of file info.c.

References ClusterInfo::dbarr, DbInfoArr::dbs, free_db_and_rel_infos(), get_db_infos(), get_rel_infos(), log_opts, DbInfoArr::ndbs, NULL, old_cluster, 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  if (cluster == &old_cluster)
324  pg_log(PG_VERBOSE, "\nsource databases:\n");
325  else
326  pg_log(PG_VERBOSE, "\ntarget databases:\n");
327 
328  if (log_opts.verbose)
329  print_db_infos(&cluster->dbarr);
330 }
static void get_db_infos(ClusterInfo *cluster)
Definition: info.c:340
LogOpts log_opts
Definition: util.c:18
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
Definition: info.c:409
ClusterInfo old_cluster
Definition: pg_upgrade.c:56
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
static void print_db_infos(DbInfoArr *dbinfo)
Definition: info.c:630
bool verbose
Definition: pg_upgrade.h:283
DbInfoArr dbarr
Definition: pg_upgrade.h:261
#define NULL
Definition: c.h:229
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition: info.c:596
DbInfo * dbs
Definition: pg_upgrade.h:194
static void get_db_infos ( ClusterInfo cluster)
static

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

341 {
342  PGconn *conn = connectToServer(cluster, "template1");
343  PGresult *res;
344  int ntups;
345  int tupnum;
346  DbInfo *dbinfos;
347  int i_datname,
348  i_oid,
349  i_encoding,
350  i_datcollate,
351  i_datctype,
352  i_spclocation;
353  char query[QUERY_ALLOC];
354 
355  snprintf(query, sizeof(query),
356  "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, "
357  "%s AS spclocation "
358  "FROM pg_catalog.pg_database d "
359  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
360  " ON d.dattablespace = t.oid "
361  "WHERE d.datallowconn = true "
362  /* we don't preserve pg_database.oid so we sort by name */
363  "ORDER BY 2",
364  /* 9.2 removed the spclocation column */
365  (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
366  "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
367 
368  res = executeQueryOrDie(conn, "%s", query);
369 
370  i_oid = PQfnumber(res, "oid");
371  i_datname = PQfnumber(res, "datname");
372  i_encoding = PQfnumber(res, "encoding");
373  i_datcollate = PQfnumber(res, "datcollate");
374  i_datctype = PQfnumber(res, "datctype");
375  i_spclocation = PQfnumber(res, "spclocation");
376 
377  ntups = PQntuples(res);
378  dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
379 
380  for (tupnum = 0; tupnum < ntups; tupnum++)
381  {
382  dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
383  dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
384  dbinfos[tupnum].db_encoding = atoi(PQgetvalue(res, tupnum, i_encoding));
385  dbinfos[tupnum].db_collate = pg_strdup(PQgetvalue(res, tupnum, i_datcollate));
386  dbinfos[tupnum].db_ctype = pg_strdup(PQgetvalue(res, tupnum, i_datctype));
387  snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
388  PQgetvalue(res, tupnum, i_spclocation));
389  }
390  PQclear(res);
391 
392  PQfinish(conn);
393 
394  cluster->dbarr.dbs = dbinfos;
395  cluster->dbarr.ndbs = ntups;
396 }
uint32 major_version
Definition: pg_upgrade.h:270
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:3630
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:182
char * db_ctype
Definition: pg_upgrade.h:187
PGconn * conn
Definition: streamutil.c:43
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:261
int db_encoding
Definition: pg_upgrade.h:188
char * db_name
Definition: pg_upgrade.h:183
char * db_collate
Definition: pg_upgrade.h:186
DbInfo * dbs
Definition: pg_upgrade.h:194
static void get_rel_infos ( ClusterInfo cluster,
DbInfo dbinfo 
)
static

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

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

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

631 {
632  int dbnum;
633 
634  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
635  {
636  pg_log(PG_VERBOSE, "Database: %s\n", db_arr->dbs[dbnum].db_name);
637  print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
638  pg_log(PG_VERBOSE, "\n\n");
639  }
640 }
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
static void print_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:644
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:283
static void print_rel_infos ( RelInfoArr rel_arr)
static

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

645 {
646  int relnum;
647 
648  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
649  pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
650  rel_arr->rels[relnum].nspname,
651  rel_arr->rels[relnum].relname,
652  rel_arr->rels[relnum].reloid,
653  rel_arr->rels[relnum].tablespace);
654 }
char * relname
Definition: pg_upgrade.h:138
Oid reloid
Definition: pg_upgrade.h:139
char * nspname
Definition: pg_upgrade.h:137
RelInfo * rels
Definition: pg_upgrade.h:150
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
char * tablespace
Definition: pg_upgrade.h:143
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:138
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Oid reloid
Definition: pg_upgrade.h:139
unsigned int Oid
Definition: postgres_ext.h:31
char * nspname
Definition: pg_upgrade.h:137
RelInfo * rels
Definition: pg_upgrade.h:150
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
Oid indtable
Definition: pg_upgrade.h:141
RelInfoArr rel_arr
Definition: pg_upgrade.h:189
int i
char * db_name
Definition: pg_upgrade.h:183
#define _(x)
Definition: elog.c:84
Oid toastheap
Definition: pg_upgrade.h:142