LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - info.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 145 227 63.9 %
Date: 2022-12-05 10:11:06 Functions: 7 10 70.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  info.c
       3             :  *
       4             :  *  information support functions
       5             :  *
       6             :  *  Copyright (c) 2010-2022, 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 *db_arr);
      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        2518 :     while (old_relnum < old_db->rel_arr.nrels ||
      60          12 :            new_relnum < new_db->rel_arr.nrels)
      61             :     {
      62        5012 :         RelInfo    *old_rel = (old_relnum < old_db->rel_arr.nrels) ?
      63        2506 :         &old_db->rel_arr.rels[old_relnum] : NULL;
      64        5012 :         RelInfo    *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
      65        2506 :         &new_db->rel_arr.rels[new_relnum] : NULL;
      66             : 
      67             :         /* handle running off one array before the other */
      68        2506 :         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        2506 :         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        2506 :         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        2506 :         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        2506 :         if (strcmp(old_rel->nspname, new_rel->nspname) != 0 ||
     123        2506 :             strcmp(old_rel->relname, new_rel->relname) != 0)
     124             :         {
     125           0 :             pg_log(PG_WARNING, "Relation names for OID %u in database \"%s\" do not match: "
     126             :                    "old name \"%s.%s\", new name \"%s.%s\"",
     127             :                    old_rel->reloid, old_db->db_name,
     128             :                    old_rel->nspname, old_rel->relname,
     129             :                    new_rel->nspname, new_rel->relname);
     130           0 :             all_matched = false;
     131           0 :             old_relnum++;
     132           0 :             new_relnum++;
     133           0 :             continue;
     134             :         }
     135             : 
     136             :         /* OK, create a mapping entry */
     137        2506 :         create_rel_filename_map(old_pgdata, new_pgdata, old_db, new_db,
     138        2506 :                                 old_rel, new_rel, maps + num_maps);
     139        2506 :         num_maps++;
     140        2506 :         old_relnum++;
     141        2506 :         new_relnum++;
     142             :     }
     143             : 
     144          12 :     if (!all_matched)
     145           0 :         pg_fatal("Failed to match up old and new tables in database \"%s\"",
     146             :                  old_db->db_name);
     147             : 
     148          12 :     *nmaps = num_maps;
     149          12 :     return maps;
     150             : }
     151             : 
     152             : 
     153             : /*
     154             :  * create_rel_filename_map()
     155             :  *
     156             :  * fills a file node map structure and returns it in "map".
     157             :  */
     158             : static void
     159        2506 : create_rel_filename_map(const char *old_data, const char *new_data,
     160             :                         const DbInfo *old_db, const DbInfo *new_db,
     161             :                         const RelInfo *old_rel, const RelInfo *new_rel,
     162             :                         FileNameMap *map)
     163             : {
     164             :     /* In case old/new tablespaces don't match, do them separately. */
     165        2506 :     if (strlen(old_rel->tablespace) == 0)
     166             :     {
     167             :         /*
     168             :          * relation belongs to the default tablespace, hence relfiles should
     169             :          * exist in the data directories.
     170             :          */
     171        2506 :         map->old_tablespace = old_data;
     172        2506 :         map->old_tablespace_suffix = "/base";
     173             :     }
     174             :     else
     175             :     {
     176             :         /* relation belongs to a tablespace, so use the tablespace location */
     177           0 :         map->old_tablespace = old_rel->tablespace;
     178           0 :         map->old_tablespace_suffix = old_cluster.tablespace_suffix;
     179             :     }
     180             : 
     181             :     /* Do the same for new tablespaces */
     182        2506 :     if (strlen(new_rel->tablespace) == 0)
     183             :     {
     184        2506 :         map->new_tablespace = new_data;
     185        2506 :         map->new_tablespace_suffix = "/base";
     186             :     }
     187             :     else
     188             :     {
     189           0 :         map->new_tablespace = new_rel->tablespace;
     190           0 :         map->new_tablespace_suffix = new_cluster.tablespace_suffix;
     191             :     }
     192             : 
     193             :     /* DB oid and relfilenumbers are preserved between old and new cluster */
     194        2506 :     map->db_oid = old_db->db_oid;
     195        2506 :     map->relfilenumber = old_rel->relfilenumber;
     196             : 
     197             :     /* used only for logging and error reporting, old/new are identical */
     198        2506 :     map->nspname = old_rel->nspname;
     199        2506 :     map->relname = old_rel->relname;
     200        2506 : }
     201             : 
     202             : 
     203             : /*
     204             :  * Complain about a relation we couldn't match to the other database,
     205             :  * identifying it as best we can.
     206             :  */
     207             : static void
     208           0 : report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
     209             : {
     210           0 :     Oid         reloid = rel->reloid;    /* we might change rel below */
     211             :     char        reldesc[1000];
     212             :     int         i;
     213             : 
     214           0 :     snprintf(reldesc, sizeof(reldesc), "\"%s.%s\"",
     215             :              rel->nspname, rel->relname);
     216           0 :     if (rel->indtable)
     217             :     {
     218           0 :         for (i = 0; i < db->rel_arr.nrels; i++)
     219             :         {
     220           0 :             const RelInfo *hrel = &db->rel_arr.rels[i];
     221             : 
     222           0 :             if (hrel->reloid == rel->indtable)
     223             :             {
     224           0 :                 snprintf(reldesc + strlen(reldesc),
     225           0 :                          sizeof(reldesc) - strlen(reldesc),
     226           0 :                          _(" which is an index on \"%s.%s\""),
     227             :                          hrel->nspname, hrel->relname);
     228             :                 /* Shift attention to index's table for toast check */
     229           0 :                 rel = hrel;
     230           0 :                 break;
     231             :             }
     232             :         }
     233           0 :         if (i >= db->rel_arr.nrels)
     234           0 :             snprintf(reldesc + strlen(reldesc),
     235           0 :                      sizeof(reldesc) - strlen(reldesc),
     236           0 :                      _(" which is an index on OID %u"), rel->indtable);
     237             :     }
     238           0 :     if (rel->toastheap)
     239             :     {
     240           0 :         for (i = 0; i < db->rel_arr.nrels; i++)
     241             :         {
     242           0 :             const RelInfo *brel = &db->rel_arr.rels[i];
     243             : 
     244           0 :             if (brel->reloid == rel->toastheap)
     245             :             {
     246           0 :                 snprintf(reldesc + strlen(reldesc),
     247           0 :                          sizeof(reldesc) - strlen(reldesc),
     248           0 :                          _(" which is the TOAST table for \"%s.%s\""),
     249             :                          brel->nspname, brel->relname);
     250           0 :                 break;
     251             :             }
     252             :         }
     253           0 :         if (i >= db->rel_arr.nrels)
     254           0 :             snprintf(reldesc + strlen(reldesc),
     255           0 :                      sizeof(reldesc) - strlen(reldesc),
     256           0 :                      _(" which is the TOAST table for OID %u"), rel->toastheap);
     257             :     }
     258             : 
     259           0 :     if (is_new_db)
     260           0 :         pg_log(PG_WARNING, "No match found in old cluster for new relation with OID %u in database \"%s\": %s",
     261             :                reloid, db->db_name, reldesc);
     262             :     else
     263           0 :         pg_log(PG_WARNING, "No match found in new cluster for old relation with OID %u in database \"%s\": %s",
     264             :                reloid, db->db_name, reldesc);
     265           0 : }
     266             : 
     267             : /*
     268             :  * get_db_and_rel_infos()
     269             :  *
     270             :  * higher level routine to generate dbinfos for the database running
     271             :  * on the given "port". Assumes that server is already running.
     272             :  */
     273             : void
     274          10 : get_db_and_rel_infos(ClusterInfo *cluster)
     275             : {
     276             :     int         dbnum;
     277             : 
     278          10 :     if (cluster->dbarr.dbs != NULL)
     279           2 :         free_db_and_rel_infos(&cluster->dbarr);
     280             : 
     281          10 :     get_db_infos(cluster);
     282             : 
     283          54 :     for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
     284          44 :         get_rel_infos(cluster, &cluster->dbarr.dbs[dbnum]);
     285             : 
     286          10 :     if (cluster == &old_cluster)
     287           4 :         pg_log(PG_VERBOSE, "\nsource databases:");
     288             :     else
     289           6 :         pg_log(PG_VERBOSE, "\ntarget databases:");
     290             : 
     291          10 :     if (log_opts.verbose)
     292           0 :         print_db_infos(&cluster->dbarr);
     293          10 : }
     294             : 
     295             : 
     296             : /*
     297             :  * get_db_infos()
     298             :  *
     299             :  * Scans pg_database system catalog and populates all user
     300             :  * databases.
     301             :  */
     302             : static void
     303          10 : get_db_infos(ClusterInfo *cluster)
     304             : {
     305          10 :     PGconn     *conn = connectToServer(cluster, "template1");
     306             :     PGresult   *res;
     307             :     int         ntups;
     308             :     int         tupnum;
     309             :     DbInfo     *dbinfos;
     310             :     int         i_datname,
     311             :                 i_oid,
     312             :                 i_encoding,
     313             :                 i_datcollate,
     314             :                 i_datctype,
     315             :                 i_datlocprovider,
     316             :                 i_daticulocale,
     317             :                 i_spclocation;
     318             :     char        query[QUERY_ALLOC];
     319             : 
     320          10 :     snprintf(query, sizeof(query),
     321             :              "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
     322          10 :     if (GET_MAJOR_VERSION(cluster->major_version) < 1500)
     323           0 :         snprintf(query + strlen(query), sizeof(query) - strlen(query),
     324             :                  "'c' AS datlocprovider, NULL AS daticulocale, ");
     325             :     else
     326          10 :         snprintf(query + strlen(query), sizeof(query) - strlen(query),
     327             :                  "datlocprovider, daticulocale, ");
     328          10 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     329             :              "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
     330             :              "FROM pg_catalog.pg_database d "
     331             :              " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
     332             :              " ON d.dattablespace = t.oid "
     333             :              "WHERE d.datallowconn = true "
     334             :              "ORDER BY 1");
     335             : 
     336          10 :     res = executeQueryOrDie(conn, "%s", query);
     337             : 
     338          10 :     i_oid = PQfnumber(res, "oid");
     339          10 :     i_datname = PQfnumber(res, "datname");
     340          10 :     i_encoding = PQfnumber(res, "encoding");
     341          10 :     i_datcollate = PQfnumber(res, "datcollate");
     342          10 :     i_datctype = PQfnumber(res, "datctype");
     343          10 :     i_datlocprovider = PQfnumber(res, "datlocprovider");
     344          10 :     i_daticulocale = PQfnumber(res, "daticulocale");
     345          10 :     i_spclocation = PQfnumber(res, "spclocation");
     346             : 
     347          10 :     ntups = PQntuples(res);
     348          10 :     dbinfos = (DbInfo *) pg_malloc(sizeof(DbInfo) * ntups);
     349             : 
     350          54 :     for (tupnum = 0; tupnum < ntups; tupnum++)
     351             :     {
     352          44 :         dbinfos[tupnum].db_oid = atooid(PQgetvalue(res, tupnum, i_oid));
     353          44 :         dbinfos[tupnum].db_name = pg_strdup(PQgetvalue(res, tupnum, i_datname));
     354          44 :         dbinfos[tupnum].db_encoding = atoi(PQgetvalue(res, tupnum, i_encoding));
     355          44 :         dbinfos[tupnum].db_collate = pg_strdup(PQgetvalue(res, tupnum, i_datcollate));
     356          44 :         dbinfos[tupnum].db_ctype = pg_strdup(PQgetvalue(res, tupnum, i_datctype));
     357          44 :         dbinfos[tupnum].db_collprovider = PQgetvalue(res, tupnum, i_datlocprovider)[0];
     358          44 :         if (PQgetisnull(res, tupnum, i_daticulocale))
     359          44 :             dbinfos[tupnum].db_iculocale = NULL;
     360             :         else
     361           0 :             dbinfos[tupnum].db_iculocale = pg_strdup(PQgetvalue(res, tupnum, i_daticulocale));
     362          44 :         snprintf(dbinfos[tupnum].db_tablespace, sizeof(dbinfos[tupnum].db_tablespace), "%s",
     363             :                  PQgetvalue(res, tupnum, i_spclocation));
     364             :     }
     365          10 :     PQclear(res);
     366             : 
     367          10 :     PQfinish(conn);
     368             : 
     369          10 :     cluster->dbarr.dbs = dbinfos;
     370          10 :     cluster->dbarr.ndbs = ntups;
     371          10 : }
     372             : 
     373             : 
     374             : /*
     375             :  * get_rel_infos()
     376             :  *
     377             :  * gets the relinfos for all the user tables and indexes of the database
     378             :  * referred to by "dbinfo".
     379             :  *
     380             :  * Note: the resulting RelInfo array is assumed to be sorted by OID.
     381             :  * This allows later processing to match up old and new databases efficiently.
     382             :  */
     383             : static void
     384          44 : get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
     385             : {
     386          44 :     PGconn     *conn = connectToServer(cluster,
     387          44 :                                        dbinfo->db_name);
     388             :     PGresult   *res;
     389             :     RelInfo    *relinfos;
     390             :     int         ntups;
     391             :     int         relnum;
     392          44 :     int         num_rels = 0;
     393          44 :     char       *nspname = NULL;
     394          44 :     char       *relname = NULL;
     395          44 :     char       *tablespace = NULL;
     396             :     int         i_spclocation,
     397             :                 i_nspname,
     398             :                 i_relname,
     399             :                 i_reloid,
     400             :                 i_indtable,
     401             :                 i_toastheap,
     402             :                 i_relfilenumber,
     403             :                 i_reltablespace;
     404             :     char        query[QUERY_ALLOC];
     405          44 :     char       *last_namespace = NULL,
     406          44 :                *last_tablespace = NULL;
     407             : 
     408          44 :     query[0] = '\0';            /* initialize query string to empty */
     409             : 
     410             :     /*
     411             :      * Create a CTE that collects OIDs of regular user tables, including
     412             :      * matviews and sequences, but excluding toast tables and indexes.  We
     413             :      * assume that relations with OIDs >= FirstNormalObjectId belong to the
     414             :      * user.  (That's probably redundant with the namespace-name exclusions,
     415             :      * but let's be safe.)
     416             :      *
     417             :      * pg_largeobject contains user data that does not appear in pg_dump
     418             :      * output, so we have to copy that system table.  It's easiest to do that
     419             :      * by treating it as a user table.
     420             :      */
     421          44 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     422             :              "WITH regular_heap (reloid, indtable, toastheap) AS ( "
     423             :              "  SELECT c.oid, 0::oid, 0::oid "
     424             :              "  FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
     425             :              "         ON c.relnamespace = n.oid "
     426             :              "  WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
     427             :              CppAsString2(RELKIND_MATVIEW) ") AND "
     428             :     /* exclude possible orphaned temp tables */
     429             :              "    ((n.nspname !~ '^pg_temp_' AND "
     430             :              "      n.nspname !~ '^pg_toast_temp_' AND "
     431             :              "      n.nspname NOT IN ('pg_catalog', 'information_schema', "
     432             :              "                        'binary_upgrade', 'pg_toast') AND "
     433             :              "      c.oid >= %u::pg_catalog.oid) OR "
     434             :              "     (n.nspname = 'pg_catalog' AND "
     435             :              "      relname IN ('pg_largeobject') ))), ",
     436             :              FirstNormalObjectId);
     437             : 
     438             :     /*
     439             :      * Add a CTE that collects OIDs of toast tables belonging to the tables
     440             :      * selected by the regular_heap CTE.  (We have to do this separately
     441             :      * because the namespace-name rules above don't work for toast tables.)
     442             :      */
     443          44 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     444             :              "  toast_heap (reloid, indtable, toastheap) AS ( "
     445             :              "  SELECT c.reltoastrelid, 0::oid, c.oid "
     446             :              "  FROM regular_heap JOIN pg_catalog.pg_class c "
     447             :              "      ON regular_heap.reloid = c.oid "
     448             :              "  WHERE c.reltoastrelid != 0), ");
     449             : 
     450             :     /*
     451             :      * Add a CTE that collects OIDs of all valid indexes on the previously
     452             :      * selected tables.  We can ignore invalid indexes since pg_dump does.
     453             :      * Testing indisready is necessary in 9.2, and harmless in earlier/later
     454             :      * versions.
     455             :      */
     456          44 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     457             :              "  all_index (reloid, indtable, toastheap) AS ( "
     458             :              "  SELECT indexrelid, indrelid, 0::oid "
     459             :              "  FROM pg_catalog.pg_index "
     460             :              "  WHERE indisvalid AND indisready "
     461             :              "    AND indrelid IN "
     462             :              "        (SELECT reloid FROM regular_heap "
     463             :              "         UNION ALL "
     464             :              "         SELECT reloid FROM toast_heap)) ");
     465             : 
     466             :     /*
     467             :      * And now we can write the query that retrieves the data we want for each
     468             :      * heap and index relation.  Make sure result is sorted by OID.
     469             :      */
     470          44 :     snprintf(query + strlen(query), sizeof(query) - strlen(query),
     471             :              "SELECT all_rels.*, n.nspname, c.relname, "
     472             :              "  c.relfilenode, c.reltablespace, "
     473             :              "  pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
     474             :              "FROM (SELECT * FROM regular_heap "
     475             :              "      UNION ALL "
     476             :              "      SELECT * FROM toast_heap "
     477             :              "      UNION ALL "
     478             :              "      SELECT * FROM all_index) all_rels "
     479             :              "  JOIN pg_catalog.pg_class c "
     480             :              "      ON all_rels.reloid = c.oid "
     481             :              "  JOIN pg_catalog.pg_namespace n "
     482             :              "     ON c.relnamespace = n.oid "
     483             :              "  LEFT OUTER JOIN pg_catalog.pg_tablespace t "
     484             :              "     ON c.reltablespace = t.oid "
     485             :              "ORDER BY 1;");
     486             : 
     487          44 :     res = executeQueryOrDie(conn, "%s", query);
     488             : 
     489          44 :     ntups = PQntuples(res);
     490             : 
     491          44 :     relinfos = (RelInfo *) pg_malloc(sizeof(RelInfo) * ntups);
     492             : 
     493          44 :     i_reloid = PQfnumber(res, "reloid");
     494          44 :     i_indtable = PQfnumber(res, "indtable");
     495          44 :     i_toastheap = PQfnumber(res, "toastheap");
     496          44 :     i_nspname = PQfnumber(res, "nspname");
     497          44 :     i_relname = PQfnumber(res, "relname");
     498          44 :     i_relfilenumber = PQfnumber(res, "relfilenode");
     499          44 :     i_reltablespace = PQfnumber(res, "reltablespace");
     500          44 :     i_spclocation = PQfnumber(res, "spclocation");
     501             : 
     502        7578 :     for (relnum = 0; relnum < ntups; relnum++)
     503             :     {
     504        7534 :         RelInfo    *curr = &relinfos[num_rels++];
     505             : 
     506        7534 :         curr->reloid = atooid(PQgetvalue(res, relnum, i_reloid));
     507        7534 :         curr->indtable = atooid(PQgetvalue(res, relnum, i_indtable));
     508        7534 :         curr->toastheap = atooid(PQgetvalue(res, relnum, i_toastheap));
     509             : 
     510        7534 :         nspname = PQgetvalue(res, relnum, i_nspname);
     511        7534 :         curr->nsp_alloc = false;
     512             : 
     513             :         /*
     514             :          * Many of the namespace and tablespace strings are identical, so we
     515             :          * try to reuse the allocated string pointers where possible to reduce
     516             :          * memory consumption.
     517             :          */
     518             :         /* Can we reuse the previous string allocation? */
     519        7534 :         if (last_namespace && strcmp(nspname, last_namespace) == 0)
     520        4616 :             curr->nspname = last_namespace;
     521             :         else
     522             :         {
     523        2918 :             last_namespace = curr->nspname = pg_strdup(nspname);
     524        2918 :             curr->nsp_alloc = true;
     525             :         }
     526             : 
     527        7534 :         relname = PQgetvalue(res, relnum, i_relname);
     528        7534 :         curr->relname = pg_strdup(relname);
     529             : 
     530        7534 :         curr->relfilenumber = atooid(PQgetvalue(res, relnum, i_relfilenumber));
     531        7534 :         curr->tblsp_alloc = false;
     532             : 
     533             :         /* Is the tablespace oid non-default? */
     534        7534 :         if (atooid(PQgetvalue(res, relnum, i_reltablespace)) != 0)
     535             :         {
     536             :             /*
     537             :              * The tablespace location might be "", meaning the cluster
     538             :              * default location, i.e. pg_default or pg_global.
     539             :              */
     540           0 :             tablespace = PQgetvalue(res, relnum, i_spclocation);
     541             : 
     542             :             /* Can we reuse the previous string allocation? */
     543           0 :             if (last_tablespace && strcmp(tablespace, last_tablespace) == 0)
     544           0 :                 curr->tablespace = last_tablespace;
     545             :             else
     546             :             {
     547           0 :                 last_tablespace = curr->tablespace = pg_strdup(tablespace);
     548           0 :                 curr->tblsp_alloc = true;
     549             :             }
     550             :         }
     551             :         else
     552             :             /* A zero reltablespace oid indicates the database tablespace. */
     553        7534 :             curr->tablespace = dbinfo->db_tablespace;
     554             :     }
     555          44 :     PQclear(res);
     556             : 
     557          44 :     PQfinish(conn);
     558             : 
     559          44 :     dbinfo->rel_arr.rels = relinfos;
     560          44 :     dbinfo->rel_arr.nrels = num_rels;
     561          44 : }
     562             : 
     563             : 
     564             : static void
     565           2 : free_db_and_rel_infos(DbInfoArr *db_arr)
     566             : {
     567             :     int         dbnum;
     568             : 
     569           6 :     for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
     570             :     {
     571           4 :         free_rel_infos(&db_arr->dbs[dbnum].rel_arr);
     572           4 :         pg_free(db_arr->dbs[dbnum].db_name);
     573             :     }
     574           2 :     pg_free(db_arr->dbs);
     575           2 :     db_arr->dbs = NULL;
     576           2 :     db_arr->ndbs = 0;
     577           2 : }
     578             : 
     579             : 
     580             : static void
     581           4 : free_rel_infos(RelInfoArr *rel_arr)
     582             : {
     583             :     int         relnum;
     584             : 
     585          12 :     for (relnum = 0; relnum < rel_arr->nrels; relnum++)
     586             :     {
     587           8 :         if (rel_arr->rels[relnum].nsp_alloc)
     588           4 :             pg_free(rel_arr->rels[relnum].nspname);
     589           8 :         pg_free(rel_arr->rels[relnum].relname);
     590           8 :         if (rel_arr->rels[relnum].tblsp_alloc)
     591           0 :             pg_free(rel_arr->rels[relnum].tablespace);
     592             :     }
     593           4 :     pg_free(rel_arr->rels);
     594           4 :     rel_arr->nrels = 0;
     595           4 : }
     596             : 
     597             : 
     598             : static void
     599           0 : print_db_infos(DbInfoArr *db_arr)
     600             : {
     601             :     int         dbnum;
     602             : 
     603           0 :     for (dbnum = 0; dbnum < db_arr->ndbs; dbnum++)
     604             :     {
     605           0 :         pg_log(PG_VERBOSE, "Database: %s", db_arr->dbs[dbnum].db_name);
     606           0 :         print_rel_infos(&db_arr->dbs[dbnum].rel_arr);
     607             :     }
     608           0 : }
     609             : 
     610             : 
     611             : static void
     612           0 : print_rel_infos(RelInfoArr *rel_arr)
     613             : {
     614             :     int         relnum;
     615             : 
     616           0 :     for (relnum = 0; relnum < rel_arr->nrels; relnum++)
     617           0 :         pg_log(PG_VERBOSE, "relname: %s.%s: reloid: %u reltblspace: %s",
     618           0 :                rel_arr->rels[relnum].nspname,
     619           0 :                rel_arr->rels[relnum].relname,
     620           0 :                rel_arr->rels[relnum].reloid,
     621           0 :                rel_arr->rels[relnum].tablespace);
     622           0 : }

Generated by: LCOV version 1.14