PostgreSQL Source Code  git master
info.c
Go to the documentation of this file.
1 /*
2  * info.c
3  *
4  * information support functions
5  *
6  * Copyright (c) 2010-2024, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/info.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include "access/transam.h"
13 #include "catalog/pg_class_d.h"
14 #include "pg_upgrade.h"
15 
16 static void create_rel_filename_map(const char *old_data, const char *new_data,
17  const DbInfo *old_db, const DbInfo *new_db,
18  const RelInfo *old_rel, const RelInfo *new_rel,
19  FileNameMap *map);
20 static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db,
21  bool is_new_db);
22 static void free_db_and_rel_infos(DbInfoArr *db_arr);
24 static void get_db_infos(ClusterInfo *cluster);
26 static void free_rel_infos(RelInfoArr *rel_arr);
27 static void print_db_infos(DbInfoArr *db_arr);
28 static void print_rel_infos(RelInfoArr *rel_arr);
29 static void print_slot_infos(LogicalSlotInfoArr *slot_arr);
31 
32 
33 /*
34  * gen_db_file_maps()
35  *
36  * generates a database mapping from "old_db" to "new_db".
37  *
38  * Returns a malloc'ed array of mappings. The length of the array
39  * is returned into *nmaps.
40  */
42 gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
43  int *nmaps,
44  const char *old_pgdata, const char *new_pgdata)
45 {
47  int old_relnum,
48  new_relnum;
49  int num_maps = 0;
50  bool all_matched = true;
51 
52  /* There will certainly not be more mappings than there are old rels */
53  maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
54  old_db->rel_arr.nrels);
55 
56  /*
57  * Each of the RelInfo arrays should be sorted by OID. Scan through them
58  * and match them up. If we fail to match everything, we'll abort, but
59  * first print as much info as we can about mismatches.
60  */
61  old_relnum = new_relnum = 0;
62  while (old_relnum < old_db->rel_arr.nrels ||
63  new_relnum < new_db->rel_arr.nrels)
64  {
65  RelInfo *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
66  &old_db->rel_arr.rels[old_relnum] : NULL;
67  RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
68  &new_db->rel_arr.rels[new_relnum] : NULL;
69 
70  /* handle running off one array before the other */
71  if (!new_rel)
72  {
73  /*
74  * old_rel is unmatched. This should never happen, because we
75  * force new rels to have TOAST tables if the old one did.
76  */
77  report_unmatched_relation(old_rel, old_db, false);
78  all_matched = false;
79  old_relnum++;
80  continue;
81  }
82  if (!old_rel)
83  {
84  /*
85  * new_rel is unmatched. This shouldn't really happen either, but
86  * if it's a TOAST table, we can ignore it and continue
87  * processing, assuming that the new server made a TOAST table
88  * that wasn't needed.
89  */
90  if (strcmp(new_rel->nspname, "pg_toast") != 0)
91  {
92  report_unmatched_relation(new_rel, new_db, true);
93  all_matched = false;
94  }
95  new_relnum++;
96  continue;
97  }
98 
99  /* check for mismatched OID */
100  if (old_rel->reloid < new_rel->reloid)
101  {
102  /* old_rel is unmatched, see comment above */
103  report_unmatched_relation(old_rel, old_db, false);
104  all_matched = false;
105  old_relnum++;
106  continue;
107  }
108  else if (old_rel->reloid > new_rel->reloid)
109  {
110  /* new_rel is unmatched, see comment above */
111  if (strcmp(new_rel->nspname, "pg_toast") != 0)
112  {
113  report_unmatched_relation(new_rel, new_db, true);
114  all_matched = false;
115  }
116  new_relnum++;
117  continue;
118  }
119 
120  /*
121  * Verify that rels of same OID have same name. The namespace name
122  * should always match, but the relname might not match for TOAST
123  * tables (and, therefore, their indexes).
124  */
125  if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
126  strcmp(old_rel->relname, new_rel->relname) != 0)
127  {
128  pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: "
129  "old name \"%s.%s\", new name \"%s.%s\"",
130  old_rel->reloid, old_db->db_name,
131  old_rel->nspname, old_rel->relname,
132  new_rel->nspname, new_rel->relname);
133  all_matched = false;
134  old_relnum++;
135  new_relnum++;
136  continue;
137  }
138 
139  /* OK, create a mapping entry */
140  create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
141  old_rel, new_rel, maps + num_maps);
142  num_maps++;
143  old_relnum++;
144  new_relnum++;
145  }
146 
147  if (!all_matched)
148  pg_fatal("Failed to match up old and new tables in database \"%s\"",
149  old_db->db_name);
150 
151  *nmaps = num_maps;
152  return maps;
153 }
154 
155 
156 /*
157  * create_rel_filename_map()
158  *
159  * fills a file node map structure and returns it in "map".
160  */
161 static void
162 create_rel_filename_map(const char *old_data, const char *new_data,
163  const DbInfo *old_db, const DbInfo *new_db,
164  const RelInfo *old_rel, const RelInfo *new_rel,
165  FileNameMap *map)
166 {
167  /* In case old/new tablespaces don't match, do them separately. */
168  if (strlen(old_rel->tablespace) == 0)
169  {
170  /*
171  * relation belongs to the default tablespace, hence relfiles should
172  * exist in the data directories.
173  */
174  map->old_tablespace = old_data;
175  map->old_tablespace_suffix = "/base";
176  }
177  else
178  {
179  /* relation belongs to a tablespace, so use the tablespace location */
180  map->old_tablespace = old_rel->tablespace;
182  }
183 
184  /* Do the same for new tablespaces */
185  if (strlen(new_rel->tablespace) == 0)
186  {
187  map->new_tablespace = new_data;
188  map->new_tablespace_suffix = "/base";
189  }
190  else
191  {
192  map->new_tablespace = new_rel->tablespace;
194  }
195 
196  /* DB oid and relfilenumbers are preserved between old and new cluster */
197  map->db_oid = old_db->db_oid;
198  map->relfilenumber = old_rel->relfilenumber;
199 
200  /* used only for logging and error reporting, old/new are identical */
201  map->nspname = old_rel->nspname;
202  map->relname = old_rel->relname;
203 }
204 
205 
206 /*
207  * Complain about a relation we couldn't match to the other database,
208  * identifying it as best we can.
209  */
210 static void
211 report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
212 {
213  Oid reloid = rel->reloid; /* we might change rel below */
214  char reldesc[1000];
215  int i;
216 
217  snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
218  rel->nspname, rel->relname);
219  if (rel->indtable)
220  {
221  for (i = 0; i < db->rel_arr.nrels; i++)
222  {
223  const RelInfo *hrel = &db->rel_arr.rels[i];
224 
225  if (hrel->reloid == rel->indtable)
226  {
227  snprintf(reldesc + strlen(reldesc),
228  sizeof(reldesc) - strlen(reldesc),
229  _(" which is an index on \"%s.%s\""),
230  hrel->nspname, hrel->relname);
231  /* Shift attention to index's table for toast check */
232  rel = hrel;
233  break;
234  }
235  }
236  if (i >= db->rel_arr.nrels)
237  snprintf(reldesc + strlen(reldesc),
238  sizeof(reldesc) - strlen(reldesc),
239  _(" which is an index on OID %u"), rel->indtable);
240  }
241  if (rel->toastheap)
242  {
243  for (i = 0; i < db->rel_arr.nrels; i++)
244  {
245  const RelInfo *brel = &db->rel_arr.rels[i];
246 
247  if (brel->reloid == rel->toastheap)
248  {
249  snprintf(reldesc + strlen(reldesc),
250  sizeof(reldesc) - strlen(reldesc),
251  _(" which is the TOAST table for \"%s.%s\""),
252  brel->nspname, brel->relname);
253  break;
254  }
255  }
256  if (i >= db->rel_arr.nrels)
257  snprintf(reldesc + strlen(reldesc),
258  sizeof(reldesc) - strlen(reldesc),
259  _(" which is the TOAST table for OID %u"), rel->toastheap);
260  }
261 
262  if (is_new_db)
263  pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s",
264  reloid, db->db_name, reldesc);
265  else
266  pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s",
267  reloid, db->db_name, reldesc);
268 }
269 
270 /*
271  * get_db_rel_and_slot_infos()
272  *
273  * higher level routine to generate dbinfos for the database running
274  * on the given "port". Assumes that server is already running.
275  */
276 void
278 {
279  int dbnum;
280 
281  if (cluster->dbarr.dbs != NULL)
283 
286 
287  for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
288  {
289  DbInfo *pDbInfo = &cluster->dbarr.dbs[dbnum];
290 
291  get_rel_infos(cluster, pDbInfo);
292 
293  if (cluster == &old_cluster)
295  }
296 
297  if (cluster == &old_cluster)
298  pg_log(PG_VERBOSE, "\nsource databases:");
299  else
300  pg_log(PG_VERBOSE, "\ntarget databases:");
301 
302  if (log_opts.verbose)
303  print_db_infos(&cluster->dbarr);
304 }
305 
306 
307 /*
308  * Get information about template0, which will be copied from the old cluster
309  * to the new cluster.
310  */
311 static void
313 {
314  PGconn *conn = connectToServer(cluster, "template1");
316  PGresult *dbres;
317  int i_datencoding;
318  int i_datlocprovider;
319  int i_datcollate;
320  int i_datctype;
321  int i_datlocale;
322 
323  if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
324  dbres = executeQueryOrDie(conn,
325  "SELECT encoding, datlocprovider, "
326  " datcollate, datctype, datlocale "
327  "FROM pg_catalog.pg_database "
328  "WHERE datname='template0'");
329  else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
330  dbres = executeQueryOrDie(conn,
331  "SELECT encoding, datlocprovider, "
332  " datcollate, datctype, daticulocale AS datlocale "
333  "FROM pg_catalog.pg_database "
334  "WHERE datname='template0'");
335  else
336  dbres = executeQueryOrDie(conn,
337  "SELECT encoding, 'c' AS datlocprovider, "
338  " datcollate, datctype, NULL AS datlocale "
339  "FROM pg_catalog.pg_database "
340  "WHERE datname='template0'");
341 
342 
343  if (PQntuples(dbres) != 1)
344  pg_fatal("template0 not found");
345 
346  locale = pg_malloc(sizeof(DbLocaleInfo));
347 
348  i_datencoding = PQfnumber(dbres, "encoding");
349  i_datlocprovider = PQfnumber(dbres, "datlocprovider");
350  i_datcollate = PQfnumber(dbres, "datcollate");
351  i_datctype = PQfnumber(dbres, "datctype");
352  i_datlocale = PQfnumber(dbres, "datlocale");
353 
354  locale->db_encoding = atoi(PQgetvalue(dbres, 0, i_datencoding));
355  locale->db_collprovider = PQgetvalue(dbres, 0, i_datlocprovider)[0];
356  locale->db_collate = pg_strdup(PQgetvalue(dbres, 0, i_datcollate));
357  locale->db_ctype = pg_strdup(PQgetvalue(dbres, 0, i_datctype));
358  if (PQgetisnull(dbres, 0, i_datlocale))
359  locale->db_locale = NULL;
360  else
361  locale->db_locale = pg_strdup(PQgetvalue(dbres, 0, i_datlocale));
362 
363  cluster->template0 = locale;
364 
365  PQclear(dbres);
366  PQfinish(conn);
367 }
368 
369 
370 /*
371  * get_db_infos()
372  *
373  * Scans pg_database system catalog and populates all user
374  * databases.
375  */
376 static void
378 {
379  PGconn *conn = connectToServer(cluster, "template1");
380  PGresult *res;
381  int ntups;
382  int tupnum;
383  DbInfo *dbinfos;
384  int i_datname,
385  i_oid,
386  i_spclocation;
387  char query[QUERY_ALLOC];
388 
389  snprintf(query, sizeof(query),
390  "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
391  if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
392  snprintf(query + strlen(query), sizeof(query) - strlen(query),
393  "datlocprovider, datlocale, ");
394  else if (GET_MAJOR_VERSION(cluster->major_version) >= 1500)
395  snprintf(query + strlen(query), sizeof(query) - strlen(query),
396  "datlocprovider, daticulocale AS datlocale, ");
397  else
398  snprintf(query + strlen(query), sizeof(query) - strlen(query),
399  "'c' AS datlocprovider, NULL AS datlocale, ");
400  snprintf(query + strlen(query), sizeof(query) - strlen(query),
401  "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
402  "FROM pg_catalog.pg_database d "
403  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
404  " ON d.dattablespace = t.oid "
405  "WHERE d.datallowconn = true "
406  "ORDER BY 1");
407 
408  res = executeQueryOrDie(conn, "%s", query);
409 
410  i_oid = PQfnumber(res, "oid");
411  i_datname = PQfnumber(res, "datname");
412  i_spclocation = PQfnumber(res, "spclocation");
413 
414  ntups = PQntuples(res);
415  dbinfos = (DbInfo *) pg_malloc0(sizeof(DbInfo) * ntups);
416 
417  for (tupnum = 0; tupnum < ntups; tupnum++)
418  {
419  dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
420  dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
421  snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
422  PQgetvalue(res, tupnum, i_spclocation));
423  }
424  PQclear(res);
425 
426  PQfinish(conn);
427 
428  cluster->dbarr.dbs = dbinfos;
429  cluster->dbarr.ndbs = ntups;
430 }
431 
432 
433 /*
434  * get_rel_infos()
435  *
436  * gets the relinfos for all the user tables and indexes of the database
437  * referred to by "dbinfo".
438  *
439  * Note: the resulting RelInfo array is assumed to be sorted by OID.
440  * This allows later processing to match up old and new databases efficiently.
441  */
442 static void
444 {
446  dbinfo->db_name);
447  PGresult *res;
448  RelInfo *relinfos;
449  int ntups;
450  int relnum;
451  int num_rels = 0;
452  char *nspname = NULL;
453  char *relname = NULL;
454  char *tablespace = NULL;
455  int i_spclocation,
456  i_nspname,
457  i_relname,
458  i_reloid,
459  i_indtable,
460  i_toastheap,
461  i_relfilenumber,
462  i_reltablespace;
463  char query[QUERY_ALLOC];
464  char *last_namespace = NULL,
465  *last_tablespace = NULL;
466 
467  query[0] = '\0'; /* initialize query string to empty */
468 
469  /*
470  * Create a CTE that collects OIDs of regular user tables and matviews,
471  * but excluding toast tables and indexes. We assume that relations with
472  * OIDs >= FirstNormalObjectId belong to the user. (That's probably
473  * redundant with the namespace-name exclusions, but let's be safe.)
474  *
475  * pg_largeobject contains user data that does not appear in pg_dump
476  * output, so we have to copy that system table. It's easiest to do that
477  * by treating it as a user table.
478  */
479  snprintf(query + strlen(query), sizeof(query) - strlen(query),
480  "WITH regular_heap (reloid, indtable, toastheap) AS ( "
481  " SELECT c.oid, 0::oid, 0::oid "
482  " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
483  " ON c.relnamespace = n.oid "
484  " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
485  CppAsString2(RELKIND_MATVIEW) ") AND "
486  /* exclude possible orphaned temp tables */
487  " ((n.nspname !~ '^pg_temp_' AND "
488  " n.nspname !~ '^pg_toast_temp_' AND "
489  " n.nspname NOT IN ('pg_catalog', 'information_schema', "
490  " 'binary_upgrade', 'pg_toast') AND "
491  " c.oid >= %u::pg_catalog.oid) OR "
492  " (n.nspname = 'pg_catalog' AND "
493  " relname IN ('pg_largeobject') ))), ",
495 
496  /*
497  * Add a CTE that collects OIDs of toast tables belonging to the tables
498  * selected by the regular_heap CTE. (We have to do this separately
499  * because the namespace-name rules above don't work for toast tables.)
500  */
501  snprintf(query + strlen(query), sizeof(query) - strlen(query),
502  " toast_heap (reloid, indtable, toastheap) AS ( "
503  " SELECT c.reltoastrelid, 0::oid, c.oid "
504  " FROM regular_heap JOIN pg_catalog.pg_class c "
505  " ON regular_heap.reloid = c.oid "
506  " WHERE c.reltoastrelid != 0), ");
507 
508  /*
509  * Add a CTE that collects OIDs of all valid indexes on the previously
510  * selected tables. We can ignore invalid indexes since pg_dump does.
511  * Testing indisready is necessary in 9.2, and harmless in earlier/later
512  * versions.
513  */
514  snprintf(query + strlen(query), sizeof(query) - strlen(query),
515  " all_index (reloid, indtable, toastheap) AS ( "
516  " SELECT indexrelid, indrelid, 0::oid "
517  " FROM pg_catalog.pg_index "
518  " WHERE indisvalid AND indisready "
519  " AND indrelid IN "
520  " (SELECT reloid FROM regular_heap "
521  " UNION ALL "
522  " SELECT reloid FROM toast_heap)) ");
523 
524  /*
525  * And now we can write the query that retrieves the data we want for each
526  * heap and index relation. Make sure result is sorted by OID.
527  */
528  snprintf(query + strlen(query), sizeof(query) - strlen(query),
529  "SELECT all_rels.*, n.nspname, c.relname, "
530  " c.relfilenode, c.reltablespace, "
531  " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
532  "FROM (SELECT * FROM regular_heap "
533  " UNION ALL "
534  " SELECT * FROM toast_heap "
535  " UNION ALL "
536  " SELECT * FROM all_index) all_rels "
537  " JOIN pg_catalog.pg_class c "
538  " ON all_rels.reloid = c.oid "
539  " JOIN pg_catalog.pg_namespace n "
540  " ON c.relnamespace = n.oid "
541  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
542  " ON c.reltablespace = t.oid "
543  "ORDER BY 1;");
544 
545  res = executeQueryOrDie(conn, "%s", query);
546 
547  ntups = PQntuples(res);
548 
549  relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
550 
551  i_reloid = PQfnumber(res, "reloid");
552  i_indtable = PQfnumber(res, "indtable");
553  i_toastheap = PQfnumber(res, "toastheap");
554  i_nspname = PQfnumber(res, "nspname");
555  i_relname = PQfnumber(res, "relname");
556  i_relfilenumber = PQfnumber(res, "relfilenode");
557  i_reltablespace = PQfnumber(res, "reltablespace");
558  i_spclocation = PQfnumber(res, "spclocation");
559 
560  for (relnum = 0; relnum < ntups; relnum++)
561  {
562  RelInfo *curr = &relinfos[num_rels++];
563 
564  curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
565  curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
566  curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
567 
568  nspname = PQgetvalue(res, relnum, i_nspname);
569  curr->nsp_alloc = false;
570 
571  /*
572  * Many of the namespace and tablespace strings are identical, so we
573  * try to reuse the allocated string pointers where possible to reduce
574  * memory consumption.
575  */
576  /* Can we reuse the previous string allocation? */
577  if (last_namespace && strcmp(nspname, last_namespace) == 0)
578  curr->nspname = last_namespace;
579  else
580  {
581  last_namespace = curr->nspname = pg_strdup(nspname);
582  curr->nsp_alloc = true;
583  }
584 
585  relname = PQgetvalue(res, relnum, i_relname);
586  curr->relname = pg_strdup(relname);
587 
588  curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
589  curr->tblsp_alloc = false;
590 
591  /* Is the tablespace oid non-default? */
592  if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
593  {
594  /*
595  * The tablespace location might be "", meaning the cluster
596  * default location, i.e. pg_default or pg_global.
597  */
598  tablespace = PQgetvalue(res, relnum, i_spclocation);
599 
600  /* Can we reuse the previous string allocation? */
601  if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
602  curr->tablespace = last_tablespace;
603  else
604  {
605  last_tablespace = curr->tablespace = pg_strdup(tablespace);
606  curr->tblsp_alloc = true;
607  }
608  }
609  else
610  /* A zero reltablespace oid indicates the database tablespace. */
611  curr->tablespace = dbinfo->db_tablespace;
612  }
613  PQclear(res);
614 
615  PQfinish(conn);
616 
617  dbinfo->rel_arr.rels = relinfos;
618  dbinfo->rel_arr.nrels = num_rels;
619 }
620 
621 /*
622  * get_old_cluster_logical_slot_infos()
623  *
624  * Gets the LogicalSlotInfos for all the logical replication slots of the
625  * database referred to by "dbinfo". The status of each logical slot is gotten
626  * here, but they are used at the checking phase. See
627  * check_old_cluster_for_valid_slots().
628  *
629  * Note: This function will not do anything if the old cluster is pre-PG17.
630  * This is because before that the logical slots are not saved at shutdown, so
631  * there is no guarantee that the latest confirmed_flush_lsn is saved to disk
632  * which can lead to data loss. It is still not guaranteed for manually created
633  * slots in PG17, so subsequent checks done in
634  * check_old_cluster_for_valid_slots() would raise a FATAL error if such slots
635  * are included.
636  */
637 static void
639 {
640  PGconn *conn;
641  PGresult *res;
642  LogicalSlotInfo *slotinfos = NULL;
643  int num_slots;
644 
645  /* Logical slots can be migrated since PG17. */
647  return;
648 
649  conn = connectToServer(&old_cluster, dbinfo->db_name);
650 
651  /*
652  * Fetch the logical replication slot information. The check whether the
653  * slot is considered caught up is done by an upgrade function. This
654  * regards the slot as caught up if we don't find any decodable changes.
655  * See binary_upgrade_logical_slot_has_caught_up().
656  *
657  * Note that we can't ensure whether the slot is caught up during
658  * live_check as the new WAL records could be generated.
659  *
660  * We intentionally skip checking the WALs for invalidated slots as the
661  * corresponding WALs could have been removed for such slots.
662  *
663  * The temporary slots are explicitly ignored while checking because such
664  * slots cannot exist after the upgrade. During the upgrade, clusters are
665  * started and stopped several times causing any temporary slots to be
666  * removed.
667  */
668  res = executeQueryOrDie(conn, "SELECT slot_name, plugin, two_phase, failover, "
669  "%s as caught_up, invalidation_reason IS NOT NULL as invalid "
670  "FROM pg_catalog.pg_replication_slots "
671  "WHERE slot_type = 'logical' AND "
672  "database = current_database() AND "
673  "temporary IS FALSE;",
674  user_opts.live_check ? "FALSE" :
675  "(CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
676  "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
677  "END)");
678 
679  num_slots = PQntuples(res);
680 
681  if (num_slots)
682  {
683  int i_slotname;
684  int i_plugin;
685  int i_twophase;
686  int i_failover;
687  int i_caught_up;
688  int i_invalid;
689 
690  slotinfos = (LogicalSlotInfo *) pg_malloc(sizeof(LogicalSlotInfo) * num_slots);
691 
692  i_slotname = PQfnumber(res, "slot_name");
693  i_plugin = PQfnumber(res, "plugin");
694  i_twophase = PQfnumber(res, "two_phase");
695  i_failover = PQfnumber(res, "failover");
696  i_caught_up = PQfnumber(res, "caught_up");
697  i_invalid = PQfnumber(res, "invalid");
698 
699  for (int slotnum = 0; slotnum < num_slots; slotnum++)
700  {
701  LogicalSlotInfo *curr = &slotinfos[slotnum];
702 
703  curr->slotname = pg_strdup(PQgetvalue(res, slotnum, i_slotname));
704  curr->plugin = pg_strdup(PQgetvalue(res, slotnum, i_plugin));
705  curr->two_phase = (strcmp(PQgetvalue(res, slotnum, i_twophase), "t") == 0);
706  curr->failover = (strcmp(PQgetvalue(res, slotnum, i_failover), "t") == 0);
707  curr->caught_up = (strcmp(PQgetvalue(res, slotnum, i_caught_up), "t") == 0);
708  curr->invalid = (strcmp(PQgetvalue(res, slotnum, i_invalid), "t") == 0);
709  }
710  }
711 
712  PQclear(res);
713  PQfinish(conn);
714 
715  dbinfo->slot_arr.slots = slotinfos;
716  dbinfo->slot_arr.nslots = num_slots;
717 }
718 
719 
720 /*
721  * count_old_cluster_logical_slots()
722  *
723  * Returns the number of logical replication slots for all databases.
724  *
725  * Note: this function always returns 0 if the old_cluster is PG16 and prior
726  * because we gather slot information only for cluster versions greater than or
727  * equal to PG17. See get_old_cluster_logical_slot_infos().
728  */
729 int
731 {
732  int slot_count = 0;
733 
734  for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
735  slot_count += old_cluster.dbarr.dbs[dbnum].slot_arr.nslots;
736 
737  return slot_count;
738 }
739 
740 /*
741  * get_subscription_count()
742  *
743  * Gets the number of subscriptions in the cluster.
744  */
745 void
747 {
748  PGconn *conn;
749  PGresult *res;
750 
751  conn = connectToServer(cluster, "template1");
752  res = executeQueryOrDie(conn, "SELECT count(*) "
753  "FROM pg_catalog.pg_subscription");
754  cluster->nsubs = atoi(PQgetvalue(res, 0, 0));
755 
756  PQclear(res);
757  PQfinish(conn);
758 }
759 
760 static void
762 {
763  int dbnum;
764 
765  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
766  {
767  free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
768  pg_free(db_arr->dbs[dbnum].db_name);
769  }
770  pg_free(db_arr->dbs);
771  db_arr->dbs = NULL;
772  db_arr->ndbs = 0;
773 }
774 
775 
776 static void
778 {
779  int relnum;
780 
781  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
782  {
783  if (rel_arr->rels[relnum].nsp_alloc)
784  pg_free(rel_arr->rels[relnum].nspname);
785  pg_free(rel_arr->rels[relnum].relname);
786  if (rel_arr->rels[relnum].tblsp_alloc)
787  pg_free(rel_arr->rels[relnum].tablespace);
788  }
789  pg_free(rel_arr->rels);
790  rel_arr->nrels = 0;
791 }
792 
793 
794 static void
796 {
797  int dbnum;
798 
799  for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
800  {
801  DbInfo *pDbInfo = &db_arr->dbs[dbnum];
802 
803  pg_log(PG_VERBOSE, "Database: \"%s\"", pDbInfo->db_name);
804  print_rel_infos(&pDbInfo->rel_arr);
805  print_slot_infos(&pDbInfo->slot_arr);
806  }
807 }
808 
809 
810 static void
812 {
813  int relnum;
814 
815  for (relnum = 0; relnum < rel_arr->nrels; relnum++)
816  pg_log(PG_VERBOSE, "relname: \"%s.%s\", reloid: %u, reltblspace: \"%s\"",
817  rel_arr->rels[relnum].nspname,
818  rel_arr->rels[relnum].relname,
819  rel_arr->rels[relnum].reloid,
820  rel_arr->rels[relnum].tablespace);
821 }
822 
823 static void
825 {
826  /* Quick return if there are no logical slots. */
827  if (slot_arr->nslots == 0)
828  return;
829 
830  pg_log(PG_VERBOSE, "Logical replication slots within the database:");
831 
832  for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
833  {
834  LogicalSlotInfo *slot_info = &slot_arr->slots[slotnum];
835 
836  pg_log(PG_VERBOSE, "slot_name: \"%s\", plugin: \"%s\", two_phase: %s",
837  slot_info->slotname,
838  slot_info->plugin,
839  slot_info->two_phase ? "true" : "false");
840  }
841 }
#define CppAsString2(x)
Definition: c.h:327
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
#define _(x)
Definition: elog.c:90
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4892
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3901
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
Definition: info.c:824
void get_subscription_count(ClusterInfo *cluster)
Definition: info.c:746
static void get_template0_info(ClusterInfo *cluster)
Definition: info.c:312
static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
Definition: info.c:211
static void get_db_infos(ClusterInfo *cluster)
Definition: info.c:377
static void free_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:777
static void print_rel_infos(RelInfoArr *rel_arr)
Definition: info.c:811
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:162
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
Definition: info.c:42
static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo)
Definition: info.c:638
static void free_db_and_rel_infos(DbInfoArr *db_arr)
Definition: info.c:761
static void print_db_infos(DbInfoArr *db_arr)
Definition: info.c:795
int count_old_cluster_logical_slots(void)
Definition: info.c:730
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
Definition: info.c:443
void get_db_rel_and_slot_infos(ClusterInfo *cluster)
Definition: info.c:277
static char * locale
Definition: initdb.c:140
int i
Definition: isn.c:73
#define pg_fatal(...)
NameData relname
Definition: pg_class.h:38
static struct LogicalRepInfo * dbinfo
ClusterInfo new_cluster
Definition: pg_upgrade.c:72
ClusterInfo old_cluster
Definition: pg_upgrade.c:71
#define QUERY_ALLOC
Definition: pg_upgrade.h:23
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
LogOpts log_opts
Definition: util.c:17
@ PG_WARNING
Definition: pg_upgrade.h:270
@ PG_VERBOSE
Definition: pg_upgrade.h:266
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
static char * tablespace
Definition: pgbench.c:216
#define snprintf
Definition: port.h:238
unsigned int Oid
Definition: postgres_ext.h:31
#define atooid(x)
Definition: postgres_ext.h:42
UserOpts user_opts
Definition: option.c:30
PGconn * conn
Definition: streamutil.c:55
DbInfoArr dbarr
Definition: pg_upgrade.h:284
uint32 major_version
Definition: pg_upgrade.h:293
const char * tablespace_suffix
Definition: pg_upgrade.h:296
DbInfo * dbs
Definition: pg_upgrade.h:215
LogicalSlotInfoArr slot_arr
Definition: pg_upgrade.h:198
char * db_name
Definition: pg_upgrade.h:194
RelInfoArr rel_arr
Definition: pg_upgrade.h:197
Oid db_oid
Definition: pg_upgrade.h:193
const char * new_tablespace
Definition: pg_upgrade.h:178
const char * old_tablespace_suffix
Definition: pg_upgrade.h:179
const char * old_tablespace
Definition: pg_upgrade.h:177
RelFileNumber relfilenumber
Definition: pg_upgrade.h:182
char * relname
Definition: pg_upgrade.h:185
char * nspname
Definition: pg_upgrade.h:184
const char * new_tablespace_suffix
Definition: pg_upgrade.h:180
bool verbose
Definition: pg_upgrade.h:307
LogicalSlotInfo * slots
Definition: pg_upgrade.h:169
RelInfo * rels
Definition: pg_upgrade.h:148
RelFileNumber relfilenumber
Definition: pg_upgrade.h:138
Oid toastheap
Definition: pg_upgrade.h:140
bool tblsp_alloc
Definition: pg_upgrade.h:143
Oid reloid
Definition: pg_upgrade.h:137
char * nspname
Definition: pg_upgrade.h:135
char * tablespace
Definition: pg_upgrade.h:141
Oid indtable
Definition: pg_upgrade.h:139
bool nsp_alloc
Definition: pg_upgrade.h:142
char * relname
Definition: pg_upgrade.h:136
bool live_check
Definition: pg_upgrade.h:324
#define FirstNormalObjectId
Definition: transam.h:197
static const pg_conv_map maps[]