LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - info.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 66.2 % 308 204 19 1 84 1 56 147 3 9
Current Date: 2024-04-14 14:21:10 Functions: 75.0 % 16 12 3 1 7 5 1
Baseline: 16@8cea358b128 Branches: 47.4 % 116 55 15 46 17 38
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 70.0 % 10 7 3 1 6
(60,120] days: 94.1 % 17 16 1 16
(120,180] days: 72.3 % 47 34 13 34
(180,240] days: 0.0 % 1 0 1
(240..) days: 63.1 % 233 147 1 1 84 147
Function coverage date bins:
(60,120] days: 100.0 % 2 2 2
(120,180] days: 75.0 % 4 3 1 3
(240..) days: 70.0 % 10 7 2 1 2 5
Branch coverage date bins:
[..60] days: 40.0 % 10 4 6 4
(60,120] days: 66.7 % 6 4 2 4
(120,180] days: 56.2 % 16 9 7 9
(240..) days: 45.2 % 84 38 46 38

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

Generated by: LCOV version 2.1-beta2-3-g6141622