LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - info.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 70.2 % 322 226 2 94 7 19 200 1 4
Current Date: 2025-09-06 07:49:51 +0900 Functions: 76.5 % 17 13 4 3 10 1
Baseline: lcov-20250906-005545-baseline Branches: 52.3 % 130 68 6 56 4 10 54
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 97.0 % 67 65 2 19 46
(360..) days: 63.1 % 255 161 94 7 154
Function coverage date bins:
(30,360] days: 100.0 % 5 5 2 3
(360..) days: 66.7 % 12 8 4 1 7
Branch coverage date bins:
(30,360] days: 71.4 % 28 20 6 2 10 10
(360..) days: 47.1 % 102 48 54 4 44

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

Generated by: LCOV version 2.4-beta