LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - relfilenumber.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 57 89 64.0 %
Date: 2024-11-21 08:14:44 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  relfilenumber.c
       3             :  *
       4             :  *  relfilenumber functions
       5             :  *
       6             :  *  Copyright (c) 2010-2024, PostgreSQL Global Development Group
       7             :  *  src/bin/pg_upgrade/relfilenumber.c
       8             :  */
       9             : 
      10             : #include "postgres_fe.h"
      11             : 
      12             : #include <sys/stat.h>
      13             : 
      14             : #include "pg_upgrade.h"
      15             : 
      16             : static void transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace);
      17             : static void transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit);
      18             : 
      19             : 
      20             : /*
      21             :  * transfer_all_new_tablespaces()
      22             :  *
      23             :  * Responsible for upgrading all database. invokes routines to generate mappings and then
      24             :  * physically link the databases.
      25             :  */
      26             : void
      27           6 : transfer_all_new_tablespaces(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
      28             :                              char *old_pgdata, char *new_pgdata)
      29             : {
      30           6 :     switch (user_opts.transfer_mode)
      31             :     {
      32           0 :         case TRANSFER_MODE_CLONE:
      33           0 :             prep_status_progress("Cloning user relation files");
      34           0 :             break;
      35           6 :         case TRANSFER_MODE_COPY:
      36           6 :             prep_status_progress("Copying user relation files");
      37           6 :             break;
      38           0 :         case TRANSFER_MODE_COPY_FILE_RANGE:
      39           0 :             prep_status_progress("Copying user relation files with copy_file_range");
      40           0 :             break;
      41           0 :         case TRANSFER_MODE_LINK:
      42           0 :             prep_status_progress("Linking user relation files");
      43           0 :             break;
      44             :     }
      45             : 
      46             :     /*
      47             :      * Transferring files by tablespace is tricky because a single database
      48             :      * can use multiple tablespaces.  For non-parallel mode, we just pass a
      49             :      * NULL tablespace path, which matches all tablespaces.  In parallel mode,
      50             :      * we pass the default tablespace and all user-created tablespaces and let
      51             :      * those operations happen in parallel.
      52             :      */
      53           6 :     if (user_opts.jobs <= 1)
      54           6 :         parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
      55             :                                       new_pgdata, NULL);
      56             :     else
      57             :     {
      58             :         int         tblnum;
      59             : 
      60             :         /* transfer default tablespace */
      61           0 :         parallel_transfer_all_new_dbs(old_db_arr, new_db_arr, old_pgdata,
      62             :                                       new_pgdata, old_pgdata);
      63             : 
      64           0 :         for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
      65           0 :             parallel_transfer_all_new_dbs(old_db_arr,
      66             :                                           new_db_arr,
      67             :                                           old_pgdata,
      68             :                                           new_pgdata,
      69           0 :                                           os_info.old_tablespaces[tblnum]);
      70             :         /* reap all children */
      71           0 :         while (reap_child(true) == true)
      72             :             ;
      73             :     }
      74             : 
      75           6 :     end_progress_output();
      76           6 :     check_ok();
      77           6 : }
      78             : 
      79             : 
      80             : /*
      81             :  * transfer_all_new_dbs()
      82             :  *
      83             :  * Responsible for upgrading all database. invokes routines to generate mappings and then
      84             :  * physically link the databases.
      85             :  */
      86             : void
      87           6 : transfer_all_new_dbs(DbInfoArr *old_db_arr, DbInfoArr *new_db_arr,
      88             :                      char *old_pgdata, char *new_pgdata, char *old_tablespace)
      89             : {
      90             :     int         old_dbnum,
      91             :                 new_dbnum;
      92             : 
      93             :     /* Scan the old cluster databases and transfer their files */
      94           6 :     for (old_dbnum = new_dbnum = 0;
      95          26 :          old_dbnum < old_db_arr->ndbs;
      96          20 :          old_dbnum++, new_dbnum++)
      97             :     {
      98          20 :         DbInfo     *old_db = &old_db_arr->dbs[old_dbnum],
      99          20 :                    *new_db = NULL;
     100             :         FileNameMap *mappings;
     101             :         int         n_maps;
     102             : 
     103             :         /*
     104             :          * Advance past any databases that exist in the new cluster but not in
     105             :          * the old, e.g. "postgres".  (The user might have removed the
     106             :          * 'postgres' database from the old cluster.)
     107             :          */
     108          20 :         for (; new_dbnum < new_db_arr->ndbs; new_dbnum++)
     109             :         {
     110          20 :             new_db = &new_db_arr->dbs[new_dbnum];
     111          20 :             if (strcmp(old_db->db_name, new_db->db_name) == 0)
     112          20 :                 break;
     113             :         }
     114             : 
     115          20 :         if (new_dbnum >= new_db_arr->ndbs)
     116           0 :             pg_fatal("old database \"%s\" not found in the new cluster",
     117             :                      old_db->db_name);
     118             : 
     119          20 :         mappings = gen_db_file_maps(old_db, new_db, &n_maps, old_pgdata,
     120             :                                     new_pgdata);
     121          20 :         if (n_maps)
     122             :         {
     123          20 :             transfer_single_new_db(mappings, n_maps, old_tablespace);
     124             :         }
     125             :         /* We allocate something even for n_maps == 0 */
     126          20 :         pg_free(mappings);
     127             :     }
     128           6 : }
     129             : 
     130             : /*
     131             :  * transfer_single_new_db()
     132             :  *
     133             :  * create links for mappings stored in "maps" array.
     134             :  */
     135             : static void
     136          20 : transfer_single_new_db(FileNameMap *maps, int size, char *old_tablespace)
     137             : {
     138             :     int         mapnum;
     139          20 :     bool        vm_must_add_frozenbit = false;
     140             : 
     141             :     /*
     142             :      * Do we need to rewrite visibilitymap?
     143             :      */
     144          20 :     if (old_cluster.controldata.cat_ver < VISIBILITY_MAP_FROZEN_BIT_CAT_VER &&
     145           0 :         new_cluster.controldata.cat_ver >= VISIBILITY_MAP_FROZEN_BIT_CAT_VER)
     146           0 :         vm_must_add_frozenbit = true;
     147             : 
     148        2740 :     for (mapnum = 0; mapnum < size; mapnum++)
     149             :     {
     150        2720 :         if (old_tablespace == NULL ||
     151           0 :             strcmp(maps[mapnum].old_tablespace, old_tablespace) == 0)
     152             :         {
     153             :             /* transfer primary file */
     154        2720 :             transfer_relfile(&maps[mapnum], "", vm_must_add_frozenbit);
     155             : 
     156             :             /*
     157             :              * Copy/link any fsm and vm files, if they exist
     158             :              */
     159        2720 :             transfer_relfile(&maps[mapnum], "_fsm", vm_must_add_frozenbit);
     160        2720 :             transfer_relfile(&maps[mapnum], "_vm", vm_must_add_frozenbit);
     161             :         }
     162             :     }
     163          20 : }
     164             : 
     165             : 
     166             : /*
     167             :  * transfer_relfile()
     168             :  *
     169             :  * Copy or link file from old cluster to new one.  If vm_must_add_frozenbit
     170             :  * is true, visibility map forks are converted and rewritten, even in link
     171             :  * mode.
     172             :  */
     173             : static void
     174        8160 : transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_frozenbit)
     175             : {
     176             :     char        old_file[MAXPGPATH];
     177             :     char        new_file[MAXPGPATH];
     178             :     int         segno;
     179             :     char        extent_suffix[65];
     180             :     struct stat statbuf;
     181             : 
     182             :     /*
     183             :      * Now copy/link any related segments as well. Remember, PG breaks large
     184             :      * files into 1GB segments, the first segment has no extension, subsequent
     185             :      * segments are named relfilenumber.1, relfilenumber.2, relfilenumber.3.
     186             :      */
     187        8160 :     for (segno = 0;; segno++)
     188             :     {
     189       11460 :         if (segno == 0)
     190        8160 :             extent_suffix[0] = '\0';
     191             :         else
     192        3300 :             snprintf(extent_suffix, sizeof(extent_suffix), ".%d", segno);
     193             : 
     194       11460 :         snprintf(old_file, sizeof(old_file), "%s%s/%u/%u%s%s",
     195             :                  map->old_tablespace,
     196             :                  map->old_tablespace_suffix,
     197             :                  map->db_oid,
     198             :                  map->relfilenumber,
     199             :                  type_suffix,
     200             :                  extent_suffix);
     201       11460 :         snprintf(new_file, sizeof(new_file), "%s%s/%u/%u%s%s",
     202             :                  map->new_tablespace,
     203             :                  map->new_tablespace_suffix,
     204             :                  map->db_oid,
     205             :                  map->relfilenumber,
     206             :                  type_suffix,
     207             :                  extent_suffix);
     208             : 
     209             :         /* Is it an extent, fsm, or vm file? */
     210       11460 :         if (type_suffix[0] != '\0' || segno != 0)
     211             :         {
     212             :             /* Did file open fail? */
     213        8740 :             if (stat(old_file, &statbuf) != 0)
     214             :             {
     215             :                 /* File does not exist?  That's OK, just return */
     216        8158 :                 if (errno == ENOENT)
     217        8158 :                     return;
     218             :                 else
     219           0 :                     pg_fatal("error while checking for file existence \"%s.%s\" (\"%s\" to \"%s\"): %m",
     220             :                              map->nspname, map->relname, old_file, new_file);
     221             :             }
     222             : 
     223             :             /* If file is empty, just return */
     224         582 :             if (statbuf.st_size == 0)
     225           2 :                 return;
     226             :         }
     227             : 
     228        3300 :         unlink(new_file);
     229             : 
     230             :         /* Copying files might take some time, so give feedback. */
     231        3300 :         pg_log(PG_STATUS, "%s", old_file);
     232             : 
     233        3300 :         if (vm_must_add_frozenbit && strcmp(type_suffix, "_vm") == 0)
     234             :         {
     235             :             /* Need to rewrite visibility map format */
     236           0 :             pg_log(PG_VERBOSE, "rewriting \"%s\" to \"%s\"",
     237             :                    old_file, new_file);
     238           0 :             rewriteVisibilityMap(old_file, new_file, map->nspname, map->relname);
     239             :         }
     240             :         else
     241        3300 :             switch (user_opts.transfer_mode)
     242             :             {
     243           0 :                 case TRANSFER_MODE_CLONE:
     244           0 :                     pg_log(PG_VERBOSE, "cloning \"%s\" to \"%s\"",
     245             :                            old_file, new_file);
     246           0 :                     cloneFile(old_file, new_file, map->nspname, map->relname);
     247           0 :                     break;
     248        3300 :                 case TRANSFER_MODE_COPY:
     249        3300 :                     pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\"",
     250             :                            old_file, new_file);
     251        3300 :                     copyFile(old_file, new_file, map->nspname, map->relname);
     252        3300 :                     break;
     253           0 :                 case TRANSFER_MODE_COPY_FILE_RANGE:
     254           0 :                     pg_log(PG_VERBOSE, "copying \"%s\" to \"%s\" with copy_file_range",
     255             :                            old_file, new_file);
     256           0 :                     copyFileByRange(old_file, new_file, map->nspname, map->relname);
     257           0 :                     break;
     258           0 :                 case TRANSFER_MODE_LINK:
     259           0 :                     pg_log(PG_VERBOSE, "linking \"%s\" to \"%s\"",
     260             :                            old_file, new_file);
     261           0 :                     linkFile(old_file, new_file, map->nspname, map->relname);
     262             :             }
     263        3300 :     }
     264             : }

Generated by: LCOV version 1.14