LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - info.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 69.5 % 321 223
Test Date: 2026-07-16 11:17:59 Functions: 76.5 % 17 13
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 51.5 % 132 68

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