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