LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - info.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 144 234 61.5 %
Date: 2019-11-15 22:06:47 Functions: 8 11 72.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  info.c
       3             :  *
       4             :  *  information support functions
       5             :  *
       6             :  *  Copyright (c) 2010-2019, PostgreSQL Global Development Group
       7             :  *  src/bin/pg_upgrade/info.c
       8             :  */
       9             : 
      10             : #include "postgres_fe.h"
      11             : 
      12             : #include "access/transam.h"
      13             : #include "catalog/pg_class_d.h"
      14             : #include "pg_upgrade.h"
      15             : 
      16             : static void create_rel_filename_map(const char *old_data, const char *new_data,
      17             :                                     const DbInfo *old_db, const DbInfo *new_db,
      18             :                                     const RelInfo *old_rel, const RelInfo *new_rel,
      19             :                                     FileNameMap *map);
      20             : static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db,
      21             :                                       bool is_new_db);
      22             : static void free_db_and_rel_infos(DbInfoArr *db_arr);
      23             : static void get_db_infos(ClusterInfo *cluster);
      24             : static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo);
      25             : static void free_rel_infos(RelInfoArr *rel_arr);
      26             : static void print_db_infos(DbInfoArr *dbinfo);
      27             : static void print_rel_infos(RelInfoArr *rel_arr);
      28             : 
      29             : 
      30             : /*
      31             :  * gen_db_file_maps()
      32             :  *
      33             :  * generates a database mapping from "old_db" to "new_db".
      34             :  *
      35             :  * Returns a malloc'ed array of mappings.  The length of the array
      36             :  * is returned into *nmaps.
      37             :  */
      38             : FileNameMap *
      39          12 : gen_db_file_maps(DbInfo *old_db, DbInfo *new_db,
      40             :                  int *nmaps,
      41             :                  const char *old_pgdata, const char *new_pgdata)
      42             : {
      43             :     FileNameMap *maps;
      44             :     int         old_relnum,
      45             :                 new_relnum;
      46          12 :     int         num_maps = 0;
      47          12 :     bool        all_matched = true;
      48             : 
      49             :     /* There will certainly not be more mappings than there are old rels */
      50          12 :     maps = (FileNameMap *) pg_malloc(sizeof(FileNameMap) *
      51          12 :                                      old_db->rel_arr.nrels);
      52             : 
      53             :     /*
      54             :      * Each of the RelInfo arrays should be sorted by OID.  Scan through them
      55             :      * and match them up.  If we fail to match everything, we'll abort, but
      56             :      * first print as much info as we can about mismatches.
      57             :      */
      58          12 :     old_relnum = new_relnum = 0;
      59        2204 :     while (old_relnum < old_db->rel_arr.nrels ||
      60          12 :            new_relnum < new_db->rel_arr.nrels)
      61             :     {
      62        4336 :         RelInfo    *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
      63        2168 :         &old_db->rel_arr.rels[old_relnum] : NULL;
      64        4336 :         RelInfo    *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
      65        2168 :         &new_db->rel_arr.rels[new_relnum] : NULL;
      66             : 
      67             :         /* handle running off one array before the other */
      68        2168 :         if (!new_rel)
      69             :         {
      70             :             /*
      71             :              * old_rel is unmatched.  This should never happen, because we
      72             :              * force new rels to have TOAST tables if the old one did.
      73             :              */
      74           0 :             report_unmatched_relation(old_rel, old_db, false);
      75           0 :             all_matched = false;
      76           0 :             old_relnum++;
      77           0 :             continue;
      78             :         }
      79        2168 :         if (!old_rel)
      80             :         {
      81             :             /*
      82             :              * new_rel is unmatched.  This shouldn't really happen either, but
      83             :              * if it's a TOAST table, we can ignore it and continue
      84             :              * processing, assuming that the new server made a TOAST table
      85             :              * that wasn't needed.
      86             :              */
      87           0 :             if (strcmp(new_rel->nspname, "pg_toast") != 0)
      88             :             {
      89           0 :                 report_unmatched_relation(new_rel, new_db, true);
      90           0 :                 all_matched = false;
      91             :             }
      92           0 :             new_relnum++;
      93           0 :             continue;
      94             :         }
      95             : 
      96             :         /* check for mismatched OID */
      97        2168 :         if (old_rel->reloid < new_rel->reloid)
      98             :         {
      99             :             /* old_rel is unmatched, see comment above */
     100           0 :             report_unmatched_relation(old_rel, old_db, false);
     101           0 :             all_matched = false;
     102           0 :             old_relnum++;
     103           0 :             continue;
     104             :         }
     105        2168 :         else if (old_rel->reloid > new_rel->reloid)
     106             :         {
     107             :             /* new_rel is unmatched, see comment above */
     108           0 :             if (strcmp(new_rel->nspname, "pg_toast") != 0)
     109             :             {
     110           0 :                 report_unmatched_relation(new_rel, new_db, true);
     111           0 :                 all_matched = false;
     112             :             }
     113           0 :             new_relnum++;
     114           0 :             continue;
     115             :         }
     116             : 
     117             :         /*
     118             :          * Verify that rels of same OID have same name.  The namespace name
     119             :          * should always match, but the relname might not match for TOAST
     120             :          * tables (and, therefore, their indexes).
     121             :          *
     122             :          * TOAST table names initially match the heap pg_class oid, but
     123             :          * pre-9.0 they can change during certain commands such as CLUSTER, so
     124             :          * don't insist on a match if old cluster is < 9.0.
     125             :          */
     126        4336 :         if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
     127        2168 :             (strcmp(old_rel->relname, new_rel->relname) != 0 &&
     128           0 :              (GET_MAJOR_VERSION(old_cluster.major_version) >= 900 ||
     129           0 :               strcmp(old_rel->nspname, "pg_toast") != 0)))
     130             :         {
     131           0 :             pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: "
     132             :                    "old name \"%s.%s\", new name \"%s.%s\"\n",
     133             :                    old_rel->reloid, old_db->db_name,
     134             :                    old_rel->nspname, old_rel->relname,
     135             :                    new_rel->nspname, new_rel->relname);
     136           0 :             all_matched = false;
     137           0 :             old_relnum++;
     138           0 :             new_relnum++;
     139           0 :             continue;
     140             :         }
     141             : 
     142             :         /* OK, create a mapping entry */
     143        2168 :         create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
     144        2168 :                                 old_rel, new_rel, maps + num_maps);
     145        2168 :         num_maps++;
     146        2168 :         old_relnum++;
     147        2168 :         new_relnum++;
     148             :     }
     149             : 
     150          12 :     if (!all_matched)
     151           0 :         pg_fatal("Failed to match up old and new tables in database \"%s\"\n",
     152             :                  old_db->db_name);
     153             : 
     154          12 :     *nmaps = num_maps;
     155          12 :     return maps;
     156             : }
     157             : 
     158             : 
     159             : /*
     160             :  * create_rel_filename_map()
     161             :  *
     162             :  * fills a file node map structure and returns it in "map".
     163             :  */
     164             : static void
     165        2168 : 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        2168 :     if (strlen(old_rel->tablespace) == 0)
     172             :     {
     173             :         /*
     174             :          * relation belongs to the default tablespace, hence relfiles should
     175             :          * exist in the data directories.
     176             :          */
     177        2168 :         map->old_tablespace = old_data;
     178        2168 :         map->old_tablespace_suffix = "/base";
     179             :     }
     180             :     else
     181             :     {
     182             :         /* relation belongs to a tablespace, so use the tablespace location */
     183           0 :         map->old_tablespace = old_rel->tablespace;
     184           0 :         map->old_tablespace_suffix = old_cluster.tablespace_suffix;
     185             :     }
     186             : 
     187             :     /* Do the same for new tablespaces */
     188        2168 :     if (strlen(new_rel->tablespace) == 0)
     189             :     {
     190        2168 :         map->new_tablespace = new_data;
     191        2168 :         map->new_tablespace_suffix = "/base";
     192             :     }
     193             :     else
     194             :     {
     195           0 :         map->new_tablespace = new_rel->tablespace;
     196           0 :         map->new_tablespace_suffix = new_cluster.tablespace_suffix;
     197             :     }
     198             : 
     199        2168 :     map->old_db_oid = old_db->db_oid;
     200        2168 :     map->new_db_oid = new_db->db_oid;
     201             : 
     202             :     /*
     203             :      * old_relfilenode might differ from pg_class.oid (and hence
     204             :      * new_relfilenode) because of CLUSTER, REINDEX, or VACUUM FULL.
     205             :      */
     206        2168 :     map->old_relfilenode = old_rel->relfilenode;
     207             : 
     208             :     /* new_relfilenode will match old and new pg_class.oid */
     209        2168 :     map->new_relfilenode = new_rel->relfilenode;
     210             : 
     211             :     /* used only for logging and error reporting, old/new are identical */
     212        2168 :     map->nspname = old_rel->nspname;
     213        2168 :     map->relname = old_rel->relname;
     214        2168 : }
     215             : 
     216             : 
     217             : /*
     218             :  * Complain about a relation we couldn't match to the other database,
     219             :  * identifying it as best we can.
     220             :  */
     221             : static void
     222           0 : report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
     223             : {
     224           0 :     Oid         reloid = rel->reloid;    /* we might change rel below */
     225             :     char        reldesc[1000];
     226             :     int         i;
     227             : 
     228           0 :     snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
     229             :              rel->nspname, rel->relname);
     230           0 :     if (rel->indtable)
     231             :     {
     232           0 :         for (i = 0; i < db->rel_arr.nrels; i++)
     233             :         {
     234           0 :             const RelInfo *hrel = &db->rel_arr.rels[i];
     235             : 
     236           0 :             if (hrel->reloid == rel->indtable)
     237             :             {
     238           0 :                 snprintf(reldesc + strlen(reldesc),
     239           0 :                          sizeof(reldesc) - strlen(reldesc),
     240           0 :                          _(" which is an index on \"%s.%s\""),
     241             :                          hrel->nspname, hrel->relname);
     242             :                 /* Shift attention to index's table for toast check */
     243           0 :                 rel = hrel;
     244           0 :                 break;
     245             :             }
     246             :         }
     247           0 :         if (i >= db->rel_arr.nrels)
     248           0 :             snprintf(reldesc + strlen(reldesc),
     249           0 :                      sizeof(reldesc) - strlen(reldesc),
     250           0 :                      _(" which is an index on OID %u"), rel->indtable);
     251             :     }
     252           0 :     if (rel->toastheap)
     253             :     {
     254           0 :         for (i = 0; i < db->rel_arr.nrels; i++)
     255             :         {
     256           0 :             const RelInfo *brel = &db->rel_arr.rels[i];
     257             : 
     258           0 :             if (brel->reloid == rel->toastheap)
     259             :             {
     260           0 :                 snprintf(reldesc + strlen(reldesc),
     261           0 :                          sizeof(reldesc) - strlen(reldesc),
     262           0 :                          _(" which is the TOAST table for \"%s.%s\""),
     263             :                          brel->nspname, brel->relname);
     264           0 :                 break;
     265             :             }
     266             :         }
     267           0 :         if (i >= db->rel_arr.nrels)
     268           0 :             snprintf(reldesc + strlen(reldesc),
     269           0 :                      sizeof(reldesc) - strlen(reldesc),
     270           0 :                      _(" which is the TOAST table for OID %u"), rel->toastheap);
     271             :     }
     272             : 
     273           0 :     if (is_new_db)
     274           0 :         pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s\n",
     275             :                reloid, db->db_name, reldesc);
     276             :     else
     277           0 :         pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s\n",
     278             :                reloid, db->db_name, reldesc);
     279           0 : }
     280             : 
     281             : 
     282             : void
     283          12 : print_maps(FileNameMap *maps, int n_maps, const char *db_name)
     284             : {
     285          12 :     if (log_opts.verbose)
     286             :     {
     287             :         int         mapnum;
     288             : 
     289           0 :         pg_log(PG_VERBOSE, "mappings for database \"%s\":\n", db_name);
     290             : 
     291           0 :         for (mapnum = 0; mapnum < n_maps; mapnum++)
     292           0 :             pg_log(PG_VERBOSE, "%s.%s: %u to %u\n",
     293           0 :                    maps[mapnum].nspname, maps[mapnum].relname,
     294           0 :                    maps[mapnum].old_relfilenode,
     295           0 :                    maps[mapnum].new_relfilenode);
     296             : 
     297           0 :         pg_log(PG_VERBOSE, "\n\n");
     298             :     }
     299          12 : }
     300             : 
     301             : 
     302             : /*
     303             :  * get_db_and_rel_infos()
     304             :  *
     305             :  * higher level routine to generate dbinfos for the database running
     306             :  * on the given "port". Assumes that server is already running.
     307             :  */
     308             : void
     309           6 : get_db_and_rel_infos(ClusterInfo *cluster)
     310             : {
     311             :     int         dbnum;
     312             : 
     313           6 :     if (cluster->dbarr.dbs != NULL)
     314           2 :         free_db_and_rel_infos(&cluster->dbarr);
     315             : 
     316           6 :     get_db_infos(cluster);
     317             : 
     318          34 :     for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
     319          28 :         get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
     320             : 
     321           6 :     if (cluster == &old_cluster)
     322           2 :         pg_log(PG_VERBOSE, "\nsource databases:\n");
     323             :     else
     324           4 :         pg_log(PG_VERBOSE, "\ntarget databases:\n");
     325             : 
     326           6 :     if (log_opts.verbose)
     327           0 :         print_db_infos(&cluster->dbarr);
     328           6 : }
     329             : 
     330             : 
     331             : /*
     332             :  * get_db_infos()
     333             :  *
     334             :  * Scans pg_database system catalog and populates all user
     335             :  * databases.
     336             :  */
     337             : static void
     338           6 : get_db_infos(ClusterInfo *cluster)
     339             : {
     340           6 :     PGconn     *conn = connectToServer(cluster, "template1");
     341             :     PGresult   *res;
     342             :     int         ntups;
     343             :     int         tupnum;
     344             :     DbInfo     *dbinfos;
     345             :     int         i_datname,
     346             :                 i_oid,
     347             :                 i_encoding,
     348             :                 i_datcollate,
     349             :                 i_datctype,
     350             :                 i_spclocation;
     351             :     char        query[QUERY_ALLOC];
     352             : 
     353           6 :     snprintf(query, sizeof(query),
     354             :              "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, "
     355             :              "%s AS spclocation "
     356             :              "FROM pg_catalog.pg_database d "
     357             :              " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
     358             :              " ON d.dattablespace = t.oid "
     359             :              "WHERE d.datallowconn = true "
     360             :     /* we don't preserve pg_database.oid so we sort by name */
     361             :              "ORDER BY 2",
     362             :     /* 9.2 removed the spclocation column */
     363           6 :              (GET_MAJOR_VERSION(cluster->major_version) <= 901) ?
     364             :              "t.spclocation" : "pg_catalog.pg_tablespace_location(t.oid)");
     365             : 
     366           6 :     res = executeQueryOrDie(conn, "%s", query);
     367             : 
     368           6 :     i_oid = PQfnumber(res, "oid");
     369           6 :     i_datname = PQfnumber(res, "datname");
     370           6 :     i_encoding = PQfnumber(res, "encoding");
     371           6 :     i_datcollate = PQfnumber(res, "datcollate");
     372           6 :     i_datctype = PQfnumber(res, "datctype");
     373           6 :     i_spclocation = PQfnumber(res, "spclocation");
     374             : 
     375           6 :     ntups = PQntuples(res);
     376           6 :     dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
     377             : 
     378          34 :     for (tupnum = 0; tupnum < ntups; tupnum++)
     379             :     {
     380          28 :         dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
     381          28 :         dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
     382          28 :         dbinfos[tupnum].db_encoding = atoi(PQgetvalue(res, tupnum, i_encoding));
     383          28 :         dbinfos[tupnum].db_collate = pg_strdup(PQgetvalue(res, tupnum, i_datcollate));
     384          28 :         dbinfos[tupnum].db_ctype = pg_strdup(PQgetvalue(res, tupnum, i_datctype));
     385          28 :         snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
     386             :                  PQgetvalue(res, tupnum, i_spclocation));
     387             :     }
     388           6 :     PQclear(res);
     389             : 
     390           6 :     PQfinish(conn);
     391             : 
     392           6 :     cluster->dbarr.dbs = dbinfos;
     393           6 :     cluster->dbarr.ndbs = ntups;
     394           6 : }
     395             : 
     396             : 
     397             : /*
     398             :  * get_rel_infos()
     399             :  *
     400             :  * gets the relinfos for all the user tables and indexes of the database
     401             :  * referred to by "dbinfo".
     402             :  *
     403             :  * Note: the resulting RelInfo array is assumed to be sorted by OID.
     404             :  * This allows later processing to match up old and new databases efficiently.
     405             :  */
     406             : static void
     407          28 : get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
     408             : {
     409          28 :     PGconn     *conn = connectToServer(cluster,
     410          28 :                                        dbinfo->db_name);
     411             :     PGresult   *res;
     412             :     RelInfo    *relinfos;
     413             :     int         ntups;
     414             :     int         relnum;
     415          28 :     int         num_rels = 0;
     416          28 :     char       *nspname = NULL;
     417          28 :     char       *relname = NULL;
     418          28 :     char       *tablespace = NULL;
     419             :     int         i_spclocation,
     420             :                 i_nspname,
     421             :                 i_relname,
     422             :                 i_reloid,
     423             :                 i_indtable,
     424             :                 i_toastheap,
     425             :                 i_relfilenode,
     426             :                 i_reltablespace;
     427             :     char        query[QUERY_ALLOC];
     428          28 :     char       *last_namespace = NULL,
     429          28 :                *last_tablespace = NULL;
     430             : 
     431          28 :     query[0] = '\0';            /* initialize query string to empty */
     432             : 
     433             :     /*
     434             :      * Create a CTE that collects OIDs of regular user tables, including
     435             :      * matviews and sequences, but excluding toast tables and indexes.  We
     436             :      * assume that relations with OIDs >= FirstNormalObjectId belong to the
     437             :      * user.  (That's probably redundant with the namespace-name exclusions,
     438             :      * but let's be safe.)
     439             :      *
     440             :      * pg_largeobject contains user data that does not appear in pg_dump
     441             :      * output, so we have to copy that system table.  It's easiest to do that
     442             :      * by treating it as a user table.
     443             :      */
     444          28 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     445             :              "WITH regular_heap (reloid, indtable, toastheap) AS ( "
     446             :              "  SELECT c.oid, 0::oid, 0::oid "
     447             :              "  FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
     448             :              "         ON c.relnamespace = n.oid "
     449             :              "  WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
     450             :              CppAsString2(RELKIND_MATVIEW) ") AND "
     451             :     /* exclude possible orphaned temp tables */
     452             :              "    ((n.nspname !~ '^pg_temp_' AND "
     453             :              "      n.nspname !~ '^pg_toast_temp_' AND "
     454             :              "      n.nspname NOT IN ('pg_catalog', 'information_schema', "
     455             :              "                        'binary_upgrade', 'pg_toast') AND "
     456             :              "      c.oid >= %u::pg_catalog.oid) OR "
     457             :              "     (n.nspname = 'pg_catalog' AND "
     458             :              "      relname IN ('pg_largeobject') ))), ",
     459             :              FirstNormalObjectId);
     460             : 
     461             :     /*
     462             :      * Add a CTE that collects OIDs of toast tables belonging to the tables
     463             :      * selected by the regular_heap CTE.  (We have to do this separately
     464             :      * because the namespace-name rules above don't work for toast tables.)
     465             :      */
     466          28 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     467             :              "  toast_heap (reloid, indtable, toastheap) AS ( "
     468             :              "  SELECT c.reltoastrelid, 0::oid, c.oid "
     469             :              "  FROM regular_heap JOIN pg_catalog.pg_class c "
     470             :              "      ON regular_heap.reloid = c.oid "
     471             :              "  WHERE c.reltoastrelid != 0), ");
     472             : 
     473             :     /*
     474             :      * Add a CTE that collects OIDs of all valid indexes on the previously
     475             :      * selected tables.  We can ignore invalid indexes since pg_dump does.
     476             :      * Testing indisready is necessary in 9.2, and harmless in earlier/later
     477             :      * versions.
     478             :      */
     479          28 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     480             :              "  all_index (reloid, indtable, toastheap) AS ( "
     481             :              "  SELECT indexrelid, indrelid, 0::oid "
     482             :              "  FROM pg_catalog.pg_index "
     483             :              "  WHERE indisvalid AND indisready "
     484             :              "    AND indrelid IN "
     485             :              "        (SELECT reloid FROM regular_heap "
     486             :              "         UNION ALL "
     487             :              "         SELECT reloid FROM toast_heap)) ");
     488             : 
     489             :     /*
     490             :      * And now we can write the query that retrieves the data we want for each
     491             :      * heap and index relation.  Make sure result is sorted by OID.
     492             :      */
     493          28 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     494             :              "SELECT all_rels.*, n.nspname, c.relname, "
     495             :              "  c.relfilenode, c.reltablespace, %s "
     496             :              "FROM (SELECT * FROM regular_heap "
     497             :              "      UNION ALL "
     498             :              "      SELECT * FROM toast_heap "
     499             :              "      UNION ALL "
     500             :              "      SELECT * FROM all_index) all_rels "
     501             :              "  JOIN pg_catalog.pg_class c "
     502             :              "      ON all_rels.reloid = c.oid "
     503             :              "  JOIN pg_catalog.pg_namespace n "
     504             :              "     ON c.relnamespace = n.oid "
     505             :              "  LEFT OUTER JOIN pg_catalog.pg_tablespace t "
     506             :              "     ON c.reltablespace = t.oid "
     507             :              "ORDER BY 1;",
     508             :     /* 9.2 removed the pg_tablespace.spclocation column */
     509          28 :              (GET_MAJOR_VERSION(cluster->major_version) >= 902) ?
     510             :              "pg_catalog.pg_tablespace_location(t.oid) AS spclocation" :
     511             :              "t.spclocation");
     512             : 
     513          28 :     res = executeQueryOrDie(conn, "%s", query);
     514             : 
     515          28 :     ntups = PQntuples(res);
     516             : 
     517          28 :     relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
     518             : 
     519          28 :     i_reloid = PQfnumber(res, "reloid");
     520          28 :     i_indtable = PQfnumber(res, "indtable");
     521          28 :     i_toastheap = PQfnumber(res, "toastheap");
     522          28 :     i_nspname = PQfnumber(res, "nspname");
     523          28 :     i_relname = PQfnumber(res, "relname");
     524          28 :     i_relfilenode = PQfnumber(res, "relfilenode");
     525          28 :     i_reltablespace = PQfnumber(res, "reltablespace");
     526          28 :     i_spclocation = PQfnumber(res, "spclocation");
     527             : 
     528        4372 :     for (relnum = 0; relnum < ntups; relnum++)
     529             :     {
     530        4344 :         RelInfo    *curr = &relinfos[num_rels++];
     531             : 
     532        4344 :         curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
     533        4344 :         curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
     534        4344 :         curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
     535             : 
     536        4344 :         nspname = PQgetvalue(res, relnum, i_nspname);
     537        4344 :         curr->nsp_alloc = false;
     538             : 
     539             :         /*
     540             :          * Many of the namespace and tablespace strings are identical, so we
     541             :          * try to reuse the allocated string pointers where possible to reduce
     542             :          * memory consumption.
     543             :          */
     544             :         /* Can we reuse the previous string allocation? */
     545        4344 :         if (last_namespace && strcmp(nspname, last_namespace) == 0)
     546        2664 :             curr->nspname = last_namespace;
     547             :         else
     548             :         {
     549        1680 :             last_namespace = curr->nspname = pg_strdup(nspname);
     550        1680 :             curr->nsp_alloc = true;
     551             :         }
     552             : 
     553        4344 :         relname = PQgetvalue(res, relnum, i_relname);
     554        4344 :         curr->relname = pg_strdup(relname);
     555             : 
     556        4344 :         curr->relfilenode = atooid(PQgetvalue(res, relnum, i_relfilenode));
     557        4344 :         curr->tblsp_alloc = false;
     558             : 
     559             :         /* Is the tablespace oid non-default? */
     560        4344 :         if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
     561             :         {
     562             :             /*
     563             :              * The tablespace location might be "", meaning the cluster
     564             :              * default location, i.e. pg_default or pg_global.
     565             :              */
     566           0 :             tablespace = PQgetvalue(res, relnum, i_spclocation);
     567             : 
     568             :             /* Can we reuse the previous string allocation? */
     569           0 :             if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
     570           0 :                 curr->tablespace = last_tablespace;
     571             :             else
     572             :             {
     573           0 :                 last_tablespace = curr->tablespace = pg_strdup(tablespace);
     574           0 :                 curr->tblsp_alloc = true;
     575             :             }
     576             :         }
     577             :         else
     578             :             /* A zero reltablespace oid indicates the database tablespace. */
     579        4344 :             curr->tablespace = dbinfo->db_tablespace;
     580             :     }
     581          28 :     PQclear(res);
     582             : 
     583          28 :     PQfinish(conn);
     584             : 
     585          28 :     dbinfo->rel_arr.rels = relinfos;
     586          28 :     dbinfo->rel_arr.nrels = num_rels;
     587          28 : }
     588             : 
     589             : 
     590             : static void
     591           2 : free_db_and_rel_infos(DbInfoArr *db_arr)
     592             : {
     593             :     int         dbnum;
     594             : 
     595           6 :     for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
     596             :     {
     597           4 :         free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
     598           4 :         pg_free(db_arr->dbs[dbnum].db_name);
     599             :     }
     600           2 :     pg_free(db_arr->dbs);
     601           2 :     db_arr->dbs = NULL;
     602           2 :     db_arr->ndbs = 0;
     603           2 : }
     604             : 
     605             : 
     606             : static void
     607           4 : free_rel_infos(RelInfoArr *rel_arr)
     608             : {
     609             :     int         relnum;
     610             : 
     611          12 :     for (relnum = 0; relnum < rel_arr->nrels; relnum++)
     612             :     {
     613           8 :         if (rel_arr->rels[relnum].nsp_alloc)
     614           4 :             pg_free(rel_arr->rels[relnum].nspname);
     615           8 :         pg_free(rel_arr->rels[relnum].relname);
     616           8 :         if (rel_arr->rels[relnum].tblsp_alloc)
     617           0 :             pg_free(rel_arr->rels[relnum].tablespace);
     618             :     }
     619           4 :     pg_free(rel_arr->rels);
     620           4 :     rel_arr->nrels = 0;
     621           4 : }
     622             : 
     623             : 
     624             : static void
     625           0 : print_db_infos(DbInfoArr *db_arr)
     626             : {
     627             :     int         dbnum;
     628             : 
     629           0 :     for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
     630             :     {
     631           0 :         pg_log(PG_VERBOSE, "Database: %s\n", db_arr->dbs[dbnum].db_name);
     632           0 :         print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
     633           0 :         pg_log(PG_VERBOSE, "\n\n");
     634             :     }
     635           0 : }
     636             : 
     637             : 
     638             : static void
     639           0 : print_rel_infos(RelInfoArr *rel_arr)
     640             : {
     641             :     int         relnum;
     642             : 
     643           0 :     for (relnum = 0; relnum < rel_arr->nrels; relnum++)
     644           0 :         pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s\n",
     645           0 :                rel_arr->rels[relnum].nspname,
     646           0 :                rel_arr->rels[relnum].relname,
     647           0 :                rel_arr->rels[relnum].reloid,
     648           0 :                rel_arr->rels[relnum].tablespace);
     649           0 : }

Generated by: LCOV version 1.13