LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - info.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 224 322 69.6 %
Date: 2026-02-05 02:19:56 Functions: 13 17 76.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.16