LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - controldata.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 297 440 67.5 %
Date: 2025-04-01 16:15:31 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  controldata.c
       3             :  *
       4             :  *  controldata functions
       5             :  *
       6             :  *  Copyright (c) 2010-2025, PostgreSQL Global Development Group
       7             :  *  src/bin/pg_upgrade/controldata.c
       8             :  */
       9             : 
      10             : #include "postgres_fe.h"
      11             : 
      12             : #include <ctype.h>
      13             : #include <limits.h>               /* for CHAR_MIN */
      14             : 
      15             : #include "common/string.h"
      16             : #include "pg_upgrade.h"
      17             : 
      18             : 
      19             : /*
      20             :  * get_control_data()
      21             :  *
      22             :  * gets pg_control information in "ctrl". Assumes that bindir and
      23             :  * datadir are valid absolute paths to postgresql bin and pgdata
      24             :  * directories respectively *and* pg_resetwal is version compatible
      25             :  * with datadir. The main purpose of this function is to get pg_control
      26             :  * data in a version independent manner.
      27             :  *
      28             :  * The approach taken here is to invoke pg_resetwal with -n option
      29             :  * and then pipe its output. With little string parsing we get the
      30             :  * pg_control data.  pg_resetwal cannot be run while the server is running
      31             :  * so we use pg_controldata;  pg_controldata doesn't provide all the fields
      32             :  * we need to actually perform the upgrade, but it provides enough for
      33             :  * check mode.  We do not implement pg_resetwal -n because it is hard to
      34             :  * return valid xid data for a running server.
      35             :  */
      36             : void
      37          60 : get_control_data(ClusterInfo *cluster)
      38             : {
      39             :     char        cmd[MAXPGPATH];
      40             :     char        bufin[MAX_STRING];
      41             :     FILE       *output;
      42             :     char       *p;
      43          60 :     bool        got_tli = false;
      44          60 :     bool        got_log_id = false;
      45          60 :     bool        got_log_seg = false;
      46          60 :     bool        got_xid = false;
      47          60 :     bool        got_oid = false;
      48          60 :     bool        got_multi = false;
      49          60 :     bool        got_oldestmulti = false;
      50          60 :     bool        got_oldestxid = false;
      51          60 :     bool        got_mxoff = false;
      52          60 :     bool        got_nextxlogfile = false;
      53          60 :     bool        got_float8_pass_by_value = false;
      54          60 :     bool        got_align = false;
      55          60 :     bool        got_blocksz = false;
      56          60 :     bool        got_largesz = false;
      57          60 :     bool        got_walsz = false;
      58          60 :     bool        got_walseg = false;
      59          60 :     bool        got_ident = false;
      60          60 :     bool        got_index = false;
      61          60 :     bool        got_toast = false;
      62          60 :     bool        got_large_object = false;
      63          60 :     bool        got_date_is_int = false;
      64          60 :     bool        got_data_checksum_version = false;
      65          60 :     bool        got_cluster_state = false;
      66          60 :     bool        got_default_char_signedness = false;
      67          60 :     char       *lc_collate = NULL;
      68          60 :     char       *lc_ctype = NULL;
      69          60 :     char       *lc_monetary = NULL;
      70          60 :     char       *lc_numeric = NULL;
      71          60 :     char       *lc_time = NULL;
      72          60 :     char       *lang = NULL;
      73          60 :     char       *language = NULL;
      74          60 :     char       *lc_all = NULL;
      75          60 :     char       *lc_messages = NULL;
      76          60 :     uint32      tli = 0;
      77          60 :     uint32      logid = 0;
      78          60 :     uint32      segno = 0;
      79             :     char       *resetwal_bin;
      80             :     int         rc;
      81          60 :     bool        live_check = (cluster == &old_cluster && user_opts.live_check);
      82             : 
      83             :     /*
      84             :      * Because we test the pg_resetwal output as strings, it has to be in
      85             :      * English.  Copied from pg_regress.c.
      86             :      */
      87          60 :     if (getenv("LC_COLLATE"))
      88           0 :         lc_collate = pg_strdup(getenv("LC_COLLATE"));
      89          60 :     if (getenv("LC_CTYPE"))
      90           0 :         lc_ctype = pg_strdup(getenv("LC_CTYPE"));
      91          60 :     if (getenv("LC_MONETARY"))
      92           0 :         lc_monetary = pg_strdup(getenv("LC_MONETARY"));
      93          60 :     if (getenv("LC_NUMERIC"))
      94           0 :         lc_numeric = pg_strdup(getenv("LC_NUMERIC"));
      95          60 :     if (getenv("LC_TIME"))
      96           0 :         lc_time = pg_strdup(getenv("LC_TIME"));
      97          60 :     if (getenv("LANG"))
      98          60 :         lang = pg_strdup(getenv("LANG"));
      99          60 :     if (getenv("LANGUAGE"))
     100           0 :         language = pg_strdup(getenv("LANGUAGE"));
     101          60 :     if (getenv("LC_ALL"))
     102           0 :         lc_all = pg_strdup(getenv("LC_ALL"));
     103          60 :     if (getenv("LC_MESSAGES"))
     104          60 :         lc_messages = pg_strdup(getenv("LC_MESSAGES"));
     105             : 
     106          60 :     unsetenv("LC_COLLATE");
     107          60 :     unsetenv("LC_CTYPE");
     108          60 :     unsetenv("LC_MONETARY");
     109          60 :     unsetenv("LC_NUMERIC");
     110          60 :     unsetenv("LC_TIME");
     111             : #ifndef WIN32
     112          60 :     unsetenv("LANG");
     113             : #else
     114             :     /* On Windows the default locale may not be English, so force it */
     115             :     setenv("LANG", "en", 1);
     116             : #endif
     117          60 :     unsetenv("LANGUAGE");
     118          60 :     unsetenv("LC_ALL");
     119          60 :     setenv("LC_MESSAGES", "C", 1);
     120             : 
     121             :     /*
     122             :      * Check for clean shutdown
     123             :      */
     124          60 :     if (!live_check || cluster == &new_cluster)
     125             :     {
     126             :         /* only pg_controldata outputs the cluster state */
     127          60 :         snprintf(cmd, sizeof(cmd), "\"%s/pg_controldata\" \"%s\"",
     128             :                  cluster->bindir, cluster->pgdata);
     129          60 :         fflush(NULL);
     130             : 
     131          60 :         if ((output = popen(cmd, "r")) == NULL)
     132           0 :             pg_fatal("could not get control data using %s: %m", cmd);
     133             : 
     134             :         /* we have the result of cmd in "output". so parse it line by line now */
     135        3120 :         while (fgets(bufin, sizeof(bufin), output))
     136             :         {
     137        3060 :             if ((p = strstr(bufin, "Database cluster state:")) != NULL)
     138             :             {
     139          60 :                 p = strchr(p, ':');
     140             : 
     141          60 :                 if (p == NULL || strlen(p) <= 1)
     142           0 :                     pg_fatal("%d: database cluster state problem", __LINE__);
     143             : 
     144          60 :                 p++;            /* remove ':' char */
     145             : 
     146             :                 /*
     147             :                  * We checked earlier for a postmaster lock file, and if we
     148             :                  * found one, we tried to start/stop the server to replay the
     149             :                  * WAL.  However, pg_ctl -m immediate doesn't leave a lock
     150             :                  * file, but does require WAL replay, so we check here that
     151             :                  * the server was shut down cleanly, from the controldata
     152             :                  * perspective.
     153             :                  */
     154             :                 /* Remove trailing newline and leading spaces */
     155          60 :                 (void) pg_strip_crlf(p);
     156         960 :                 while (*p == ' ')
     157         900 :                     p++;
     158          60 :                 if (strcmp(p, "shut down in recovery") == 0)
     159             :                 {
     160           0 :                     if (cluster == &old_cluster)
     161           0 :                         pg_fatal("The source cluster was shut down while in recovery mode.  To upgrade, use \"rsync\" as documented or shut it down as a primary.");
     162             :                     else
     163           0 :                         pg_fatal("The target cluster was shut down while in recovery mode.  To upgrade, use \"rsync\" as documented or shut it down as a primary.");
     164             :                 }
     165          60 :                 else if (strcmp(p, "shut down") != 0)
     166             :                 {
     167           0 :                     if (cluster == &old_cluster)
     168           0 :                         pg_fatal("The source cluster was not shut down cleanly, state reported as: \"%s\"", p);
     169             :                     else
     170           0 :                         pg_fatal("The target cluster was not shut down cleanly, state reported as: \"%s\"", p);
     171             :                 }
     172          60 :                 got_cluster_state = true;
     173             :             }
     174             :         }
     175             : 
     176          60 :         rc = pclose(output);
     177          60 :         if (rc != 0)
     178           0 :             pg_fatal("could not get control data using %s: %s",
     179             :                      cmd, wait_result_to_str(rc));
     180             : 
     181          60 :         if (!got_cluster_state)
     182             :         {
     183           0 :             if (cluster == &old_cluster)
     184           0 :                 pg_fatal("The source cluster lacks cluster state information:");
     185             :             else
     186           0 :                 pg_fatal("The target cluster lacks cluster state information:");
     187             :         }
     188             :     }
     189             : 
     190             :     /* pg_resetxlog has been renamed to pg_resetwal in version 10 */
     191          60 :     if (GET_MAJOR_VERSION(cluster->bin_version) <= 906)
     192           0 :         resetwal_bin = "pg_resetxlog\" -n";
     193             :     else
     194          60 :         resetwal_bin = "pg_resetwal\" -n";
     195          60 :     snprintf(cmd, sizeof(cmd), "\"%s/%s \"%s\"",
     196             :              cluster->bindir,
     197             :              live_check ? "pg_controldata\"" : resetwal_bin,
     198             :              cluster->pgdata);
     199          60 :     fflush(NULL);
     200             : 
     201          60 :     if ((output = popen(cmd, "r")) == NULL)
     202           0 :         pg_fatal("could not get control data using %s: %m", cmd);
     203             : 
     204             :     /* Only in <= 9.2 */
     205          60 :     if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
     206             :     {
     207           0 :         cluster->controldata.data_checksum_version = 0;
     208           0 :         got_data_checksum_version = true;
     209             :     }
     210             : 
     211             :     /* we have the result of cmd in "output". so parse it line by line now */
     212        2220 :     while (fgets(bufin, sizeof(bufin), output))
     213             :     {
     214             :         /* In verbose mode, log each line */
     215        2160 :         pg_strip_crlf(bufin);
     216        2160 :         pg_log(PG_VERBOSE, "%s", bufin);
     217             : 
     218        2160 :         if ((p = strstr(bufin, "pg_control version number:")) != NULL)
     219             :         {
     220          60 :             p = strchr(p, ':');
     221             : 
     222          60 :             if (p == NULL || strlen(p) <= 1)
     223           0 :                 pg_fatal("%d: pg_resetwal problem", __LINE__);
     224             : 
     225          60 :             p++;                /* remove ':' char */
     226          60 :             cluster->controldata.ctrl_ver = str2uint(p);
     227             :         }
     228        2100 :         else if ((p = strstr(bufin, "Catalog version number:")) != NULL)
     229             :         {
     230          60 :             p = strchr(p, ':');
     231             : 
     232          60 :             if (p == NULL || strlen(p) <= 1)
     233           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     234             : 
     235          60 :             p++;                /* remove ':' char */
     236          60 :             cluster->controldata.cat_ver = str2uint(p);
     237             :         }
     238        2040 :         else if ((p = strstr(bufin, "Latest checkpoint's TimeLineID:")) != NULL)
     239             :         {
     240          60 :             p = strchr(p, ':');
     241             : 
     242          60 :             if (p == NULL || strlen(p) <= 1)
     243           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     244             : 
     245          60 :             p++;                /* remove ':' char */
     246          60 :             tli = str2uint(p);
     247          60 :             got_tli = true;
     248             :         }
     249        1980 :         else if ((p = strstr(bufin, "First log file ID after reset:")) != NULL)
     250             :         {
     251           0 :             p = strchr(p, ':');
     252             : 
     253           0 :             if (p == NULL || strlen(p) <= 1)
     254           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     255             : 
     256           0 :             p++;                /* remove ':' char */
     257           0 :             logid = str2uint(p);
     258           0 :             got_log_id = true;
     259             :         }
     260        1980 :         else if ((p = strstr(bufin, "First log file segment after reset:")) != NULL)
     261             :         {
     262           0 :             p = strchr(p, ':');
     263             : 
     264           0 :             if (p == NULL || strlen(p) <= 1)
     265           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     266             : 
     267           0 :             p++;                /* remove ':' char */
     268           0 :             segno = str2uint(p);
     269           0 :             got_log_seg = true;
     270             :         }
     271        1980 :         else if ((p = strstr(bufin, "Latest checkpoint's NextXID:")) != NULL)
     272             :         {
     273          60 :             p = strchr(p, ':');
     274             : 
     275          60 :             if (p == NULL || strlen(p) <= 1)
     276           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     277             : 
     278          60 :             p++;                /* remove ':' char */
     279          60 :             cluster->controldata.chkpnt_nxtepoch = str2uint(p);
     280             : 
     281             :             /*
     282             :              * Delimiter changed from '/' to ':' in 9.6.  We don't test for
     283             :              * the catalog version of the change because the catalog version
     284             :              * is pulled from pg_controldata too, and it isn't worth adding an
     285             :              * order dependency for this --- we just check the string.
     286             :              */
     287          60 :             if (strchr(p, '/') != NULL)
     288           0 :                 p = strchr(p, '/');
     289          60 :             else if (GET_MAJOR_VERSION(cluster->major_version) >= 906)
     290          60 :                 p = strchr(p, ':');
     291             :             else
     292           0 :                 p = NULL;
     293             : 
     294          60 :             if (p == NULL || strlen(p) <= 1)
     295           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     296             : 
     297          60 :             p++;                /* remove '/' or ':' char */
     298          60 :             cluster->controldata.chkpnt_nxtxid = str2uint(p);
     299          60 :             got_xid = true;
     300             :         }
     301        1920 :         else if ((p = strstr(bufin, "Latest checkpoint's NextOID:")) != NULL)
     302             :         {
     303          60 :             p = strchr(p, ':');
     304             : 
     305          60 :             if (p == NULL || strlen(p) <= 1)
     306           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     307             : 
     308          60 :             p++;                /* remove ':' char */
     309          60 :             cluster->controldata.chkpnt_nxtoid = str2uint(p);
     310          60 :             got_oid = true;
     311             :         }
     312        1860 :         else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL)
     313             :         {
     314          60 :             p = strchr(p, ':');
     315             : 
     316          60 :             if (p == NULL || strlen(p) <= 1)
     317           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     318             : 
     319          60 :             p++;                /* remove ':' char */
     320          60 :             cluster->controldata.chkpnt_nxtmulti = str2uint(p);
     321          60 :             got_multi = true;
     322             :         }
     323        1800 :         else if ((p = strstr(bufin, "Latest checkpoint's oldestXID:")) != NULL)
     324             :         {
     325          60 :             p = strchr(p, ':');
     326             : 
     327          60 :             if (p == NULL || strlen(p) <= 1)
     328           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     329             : 
     330          60 :             p++;                /* remove ':' char */
     331          60 :             cluster->controldata.chkpnt_oldstxid = str2uint(p);
     332          60 :             got_oldestxid = true;
     333             :         }
     334        1740 :         else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL)
     335             :         {
     336          60 :             p = strchr(p, ':');
     337             : 
     338          60 :             if (p == NULL || strlen(p) <= 1)
     339           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     340             : 
     341          60 :             p++;                /* remove ':' char */
     342          60 :             cluster->controldata.chkpnt_oldstMulti = str2uint(p);
     343          60 :             got_oldestmulti = true;
     344             :         }
     345        1680 :         else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL)
     346             :         {
     347          60 :             p = strchr(p, ':');
     348             : 
     349          60 :             if (p == NULL || strlen(p) <= 1)
     350           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     351             : 
     352          60 :             p++;                /* remove ':' char */
     353          60 :             cluster->controldata.chkpnt_nxtmxoff = str2uint(p);
     354          60 :             got_mxoff = true;
     355             :         }
     356        1620 :         else if ((p = strstr(bufin, "First log segment after reset:")) != NULL)
     357             :         {
     358             :             /* Skip the colon and any whitespace after it */
     359          60 :             p = strchr(p, ':');
     360          60 :             if (p == NULL || strlen(p) <= 1)
     361           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     362          60 :             p = strpbrk(p, "01234567890ABCDEF");
     363          60 :             if (p == NULL || strlen(p) <= 1)
     364           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     365             : 
     366             :             /* Make sure it looks like a valid WAL file name */
     367          60 :             if (strspn(p, "0123456789ABCDEF") != 24)
     368           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     369             : 
     370          60 :             strlcpy(cluster->controldata.nextxlogfile, p, 25);
     371          60 :             got_nextxlogfile = true;
     372             :         }
     373        1560 :         else if ((p = strstr(bufin, "Float8 argument passing:")) != NULL)
     374             :         {
     375          60 :             p = strchr(p, ':');
     376             : 
     377          60 :             if (p == NULL || strlen(p) <= 1)
     378           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     379             : 
     380          60 :             p++;                /* remove ':' char */
     381             :             /* used later for contrib check */
     382          60 :             cluster->controldata.float8_pass_by_value = strstr(p, "by value") != NULL;
     383          60 :             got_float8_pass_by_value = true;
     384             :         }
     385        1500 :         else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL)
     386             :         {
     387          60 :             p = strchr(p, ':');
     388             : 
     389          60 :             if (p == NULL || strlen(p) <= 1)
     390           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     391             : 
     392          60 :             p++;                /* remove ':' char */
     393          60 :             cluster->controldata.align = str2uint(p);
     394          60 :             got_align = true;
     395             :         }
     396        1440 :         else if ((p = strstr(bufin, "Database block size:")) != NULL)
     397             :         {
     398          60 :             p = strchr(p, ':');
     399             : 
     400          60 :             if (p == NULL || strlen(p) <= 1)
     401           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     402             : 
     403          60 :             p++;                /* remove ':' char */
     404          60 :             cluster->controldata.blocksz = str2uint(p);
     405          60 :             got_blocksz = true;
     406             :         }
     407        1380 :         else if ((p = strstr(bufin, "Blocks per segment of large relation:")) != NULL)
     408             :         {
     409          60 :             p = strchr(p, ':');
     410             : 
     411          60 :             if (p == NULL || strlen(p) <= 1)
     412           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     413             : 
     414          60 :             p++;                /* remove ':' char */
     415          60 :             cluster->controldata.largesz = str2uint(p);
     416          60 :             got_largesz = true;
     417             :         }
     418        1320 :         else if ((p = strstr(bufin, "WAL block size:")) != NULL)
     419             :         {
     420          60 :             p = strchr(p, ':');
     421             : 
     422          60 :             if (p == NULL || strlen(p) <= 1)
     423           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     424             : 
     425          60 :             p++;                /* remove ':' char */
     426          60 :             cluster->controldata.walsz = str2uint(p);
     427          60 :             got_walsz = true;
     428             :         }
     429        1260 :         else if ((p = strstr(bufin, "Bytes per WAL segment:")) != NULL)
     430             :         {
     431          60 :             p = strchr(p, ':');
     432             : 
     433          60 :             if (p == NULL || strlen(p) <= 1)
     434           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     435             : 
     436          60 :             p++;                /* remove ':' char */
     437          60 :             cluster->controldata.walseg = str2uint(p);
     438          60 :             got_walseg = true;
     439             :         }
     440        1200 :         else if ((p = strstr(bufin, "Maximum length of identifiers:")) != NULL)
     441             :         {
     442          60 :             p = strchr(p, ':');
     443             : 
     444          60 :             if (p == NULL || strlen(p) <= 1)
     445           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     446             : 
     447          60 :             p++;                /* remove ':' char */
     448          60 :             cluster->controldata.ident = str2uint(p);
     449          60 :             got_ident = true;
     450             :         }
     451        1140 :         else if ((p = strstr(bufin, "Maximum columns in an index:")) != NULL)
     452             :         {
     453          60 :             p = strchr(p, ':');
     454             : 
     455          60 :             if (p == NULL || strlen(p) <= 1)
     456           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     457             : 
     458          60 :             p++;                /* remove ':' char */
     459          60 :             cluster->controldata.index = str2uint(p);
     460          60 :             got_index = true;
     461             :         }
     462        1080 :         else if ((p = strstr(bufin, "Maximum size of a TOAST chunk:")) != NULL)
     463             :         {
     464          60 :             p = strchr(p, ':');
     465             : 
     466          60 :             if (p == NULL || strlen(p) <= 1)
     467           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     468             : 
     469          60 :             p++;                /* remove ':' char */
     470          60 :             cluster->controldata.toast = str2uint(p);
     471          60 :             got_toast = true;
     472             :         }
     473        1020 :         else if ((p = strstr(bufin, "Size of a large-object chunk:")) != NULL)
     474             :         {
     475          60 :             p = strchr(p, ':');
     476             : 
     477          60 :             if (p == NULL || strlen(p) <= 1)
     478           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     479             : 
     480          60 :             p++;                /* remove ':' char */
     481          60 :             cluster->controldata.large_object = str2uint(p);
     482          60 :             got_large_object = true;
     483             :         }
     484         960 :         else if ((p = strstr(bufin, "Date/time type storage:")) != NULL)
     485             :         {
     486          60 :             p = strchr(p, ':');
     487             : 
     488          60 :             if (p == NULL || strlen(p) <= 1)
     489           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     490             : 
     491          60 :             p++;                /* remove ':' char */
     492          60 :             cluster->controldata.date_is_int = strstr(p, "64-bit integers") != NULL;
     493          60 :             got_date_is_int = true;
     494             :         }
     495         900 :         else if ((p = strstr(bufin, "checksum")) != NULL)
     496             :         {
     497          60 :             p = strchr(p, ':');
     498             : 
     499          60 :             if (p == NULL || strlen(p) <= 1)
     500           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     501             : 
     502          60 :             p++;                /* remove ':' char */
     503          60 :             cluster->controldata.data_checksum_version = str2uint(p);
     504          60 :             got_data_checksum_version = true;
     505             :         }
     506         840 :         else if ((p = strstr(bufin, "Default char data signedness:")) != NULL)
     507             :         {
     508          60 :             p = strchr(p, ':');
     509             : 
     510          60 :             if (p == NULL || strlen(p) <= 1)
     511           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     512             : 
     513             :             /* Skip the colon and any whitespace after it */
     514          60 :             p++;
     515         600 :             while (isspace((unsigned char) *p))
     516         540 :                 p++;
     517             : 
     518             :             /* The value should be either 'signed' or 'unsigned' */
     519          60 :             if (strcmp(p, "signed") != 0 && strcmp(p, "unsigned") != 0)
     520           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     521             : 
     522          60 :             cluster->controldata.default_char_signedness = strcmp(p, "signed") == 0;
     523          60 :             got_default_char_signedness = true;
     524             :         }
     525             :     }
     526             : 
     527          60 :     rc = pclose(output);
     528          60 :     if (rc != 0)
     529           0 :         pg_fatal("could not get control data using %s: %s",
     530             :                  cmd, wait_result_to_str(rc));
     531             : 
     532             :     /*
     533             :      * Restore environment variables.  Note all but LANG and LC_MESSAGES were
     534             :      * unset above.
     535             :      */
     536          60 :     if (lc_collate)
     537           0 :         setenv("LC_COLLATE", lc_collate, 1);
     538          60 :     if (lc_ctype)
     539           0 :         setenv("LC_CTYPE", lc_ctype, 1);
     540          60 :     if (lc_monetary)
     541           0 :         setenv("LC_MONETARY", lc_monetary, 1);
     542          60 :     if (lc_numeric)
     543           0 :         setenv("LC_NUMERIC", lc_numeric, 1);
     544          60 :     if (lc_time)
     545           0 :         setenv("LC_TIME", lc_time, 1);
     546          60 :     if (lang)
     547          60 :         setenv("LANG", lang, 1);
     548             :     else
     549           0 :         unsetenv("LANG");
     550          60 :     if (language)
     551           0 :         setenv("LANGUAGE", language, 1);
     552          60 :     if (lc_all)
     553           0 :         setenv("LC_ALL", lc_all, 1);
     554          60 :     if (lc_messages)
     555          60 :         setenv("LC_MESSAGES", lc_messages, 1);
     556             :     else
     557           0 :         unsetenv("LC_MESSAGES");
     558             : 
     559          60 :     pg_free(lc_collate);
     560          60 :     pg_free(lc_ctype);
     561          60 :     pg_free(lc_monetary);
     562          60 :     pg_free(lc_numeric);
     563          60 :     pg_free(lc_time);
     564          60 :     pg_free(lang);
     565          60 :     pg_free(language);
     566          60 :     pg_free(lc_all);
     567          60 :     pg_free(lc_messages);
     568             : 
     569             :     /*
     570             :      * Before 9.3, pg_resetwal reported the xlogid and segno of the first log
     571             :      * file after reset as separate lines. Starting with 9.3, it reports the
     572             :      * WAL file name. If the old cluster is older than 9.3, we construct the
     573             :      * WAL file name from the xlogid and segno.
     574             :      */
     575          60 :     if (GET_MAJOR_VERSION(cluster->major_version) <= 902)
     576             :     {
     577           0 :         if (got_tli && got_log_id && got_log_seg)
     578             :         {
     579           0 :             snprintf(cluster->controldata.nextxlogfile, 25, "%08X%08X%08X",
     580             :                      tli, logid, segno);
     581           0 :             got_nextxlogfile = true;
     582             :         }
     583             :     }
     584             : 
     585             :     /*
     586             :      * Pre-v18 database clusters don't have the default char signedness
     587             :      * information. We use the char signedness of the platform where
     588             :      * pg_upgrade was built.
     589             :      */
     590          60 :     if (cluster->controldata.cat_ver < DEFAULT_CHAR_SIGNEDNESS_CAT_VER)
     591             :     {
     592             :         Assert(!got_default_char_signedness);
     593             : #if CHAR_MIN != 0
     594           0 :         cluster->controldata.default_char_signedness = true;
     595             : #else
     596             :         cluster->controldata.default_char_signedness = false;
     597             : #endif
     598             :     }
     599             : 
     600             :     /* verify that we got all the mandatory pg_control data */
     601          60 :     if (!got_xid || !got_oid ||
     602          60 :         !got_multi || !got_oldestxid ||
     603          60 :         (!got_oldestmulti &&
     604           0 :          cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) ||
     605          60 :         !got_mxoff || (!live_check && !got_nextxlogfile) ||
     606          60 :         !got_float8_pass_by_value || !got_align || !got_blocksz ||
     607          60 :         !got_largesz || !got_walsz || !got_walseg || !got_ident ||
     608          60 :         !got_index || !got_toast ||
     609          60 :         (!got_large_object &&
     610           0 :          cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER) ||
     611          60 :         !got_date_is_int || !got_data_checksum_version ||
     612          60 :         (!got_default_char_signedness &&
     613           0 :          cluster->controldata.cat_ver >= DEFAULT_CHAR_SIGNEDNESS_CAT_VER))
     614             :     {
     615           0 :         if (cluster == &old_cluster)
     616           0 :             pg_log(PG_REPORT,
     617             :                    "The source cluster lacks some required control information:");
     618             :         else
     619           0 :             pg_log(PG_REPORT,
     620             :                    "The target cluster lacks some required control information:");
     621             : 
     622           0 :         if (!got_xid)
     623           0 :             pg_log(PG_REPORT, "  checkpoint next XID");
     624             : 
     625           0 :         if (!got_oid)
     626           0 :             pg_log(PG_REPORT, "  latest checkpoint next OID");
     627             : 
     628           0 :         if (!got_multi)
     629           0 :             pg_log(PG_REPORT, "  latest checkpoint next MultiXactId");
     630             : 
     631           0 :         if (!got_oldestmulti &&
     632           0 :             cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER)
     633           0 :             pg_log(PG_REPORT, "  latest checkpoint oldest MultiXactId");
     634             : 
     635           0 :         if (!got_oldestxid)
     636           0 :             pg_log(PG_REPORT, "  latest checkpoint oldestXID");
     637             : 
     638           0 :         if (!got_mxoff)
     639           0 :             pg_log(PG_REPORT, "  latest checkpoint next MultiXactOffset");
     640             : 
     641           0 :         if (!live_check && !got_nextxlogfile)
     642           0 :             pg_log(PG_REPORT, "  first WAL segment after reset");
     643             : 
     644           0 :         if (!got_float8_pass_by_value)
     645           0 :             pg_log(PG_REPORT, "  float8 argument passing method");
     646             : 
     647           0 :         if (!got_align)
     648           0 :             pg_log(PG_REPORT, "  maximum alignment");
     649             : 
     650           0 :         if (!got_blocksz)
     651           0 :             pg_log(PG_REPORT, "  block size");
     652             : 
     653           0 :         if (!got_largesz)
     654           0 :             pg_log(PG_REPORT, "  large relation segment size");
     655             : 
     656           0 :         if (!got_walsz)
     657           0 :             pg_log(PG_REPORT, "  WAL block size");
     658             : 
     659           0 :         if (!got_walseg)
     660           0 :             pg_log(PG_REPORT, "  WAL segment size");
     661             : 
     662           0 :         if (!got_ident)
     663           0 :             pg_log(PG_REPORT, "  maximum identifier length");
     664             : 
     665           0 :         if (!got_index)
     666           0 :             pg_log(PG_REPORT, "  maximum number of indexed columns");
     667             : 
     668           0 :         if (!got_toast)
     669           0 :             pg_log(PG_REPORT, "  maximum TOAST chunk size");
     670             : 
     671           0 :         if (!got_large_object &&
     672           0 :             cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER)
     673           0 :             pg_log(PG_REPORT, "  large-object chunk size");
     674             : 
     675           0 :         if (!got_date_is_int)
     676           0 :             pg_log(PG_REPORT, "  dates/times are integers?");
     677             : 
     678             :         /* value added in Postgres 9.3 */
     679           0 :         if (!got_data_checksum_version)
     680           0 :             pg_log(PG_REPORT, "  data checksum version");
     681             : 
     682             :         /* value added in Postgres 18 */
     683           0 :         if (!got_default_char_signedness)
     684           0 :             pg_log(PG_REPORT, "  default char signedness");
     685             : 
     686           0 :         pg_fatal("Cannot continue without required control information, terminating");
     687             :     }
     688          60 : }
     689             : 
     690             : 
     691             : /*
     692             :  * check_control_data()
     693             :  *
     694             :  * check to make sure the control data settings are compatible
     695             :  */
     696             : void
     697          30 : check_control_data(ControlData *oldctrl,
     698             :                    ControlData *newctrl)
     699             : {
     700          30 :     if (oldctrl->align == 0 || oldctrl->align != newctrl->align)
     701           0 :         pg_fatal("old and new pg_controldata alignments are invalid or do not match.\n"
     702             :                  "Likely one cluster is a 32-bit install, the other 64-bit");
     703             : 
     704          30 :     if (oldctrl->blocksz == 0 || oldctrl->blocksz != newctrl->blocksz)
     705           0 :         pg_fatal("old and new pg_controldata block sizes are invalid or do not match");
     706             : 
     707          30 :     if (oldctrl->largesz == 0 || oldctrl->largesz != newctrl->largesz)
     708           0 :         pg_fatal("old and new pg_controldata maximum relation segment sizes are invalid or do not match");
     709             : 
     710          30 :     if (oldctrl->walsz == 0 || oldctrl->walsz != newctrl->walsz)
     711           0 :         pg_fatal("old and new pg_controldata WAL block sizes are invalid or do not match");
     712             : 
     713          30 :     if (oldctrl->walseg == 0 || oldctrl->walseg != newctrl->walseg)
     714           0 :         pg_fatal("old and new pg_controldata WAL segment sizes are invalid or do not match");
     715             : 
     716          30 :     if (oldctrl->ident == 0 || oldctrl->ident != newctrl->ident)
     717           0 :         pg_fatal("old and new pg_controldata maximum identifier lengths are invalid or do not match");
     718             : 
     719          30 :     if (oldctrl->index == 0 || oldctrl->index != newctrl->index)
     720           0 :         pg_fatal("old and new pg_controldata maximum indexed columns are invalid or do not match");
     721             : 
     722          30 :     if (oldctrl->toast == 0 || oldctrl->toast != newctrl->toast)
     723           0 :         pg_fatal("old and new pg_controldata maximum TOAST chunk sizes are invalid or do not match");
     724             : 
     725             :     /* large_object added in 9.5, so it might not exist in the old cluster */
     726          30 :     if (oldctrl->large_object != 0 &&
     727          30 :         oldctrl->large_object != newctrl->large_object)
     728           0 :         pg_fatal("old and new pg_controldata large-object chunk sizes are invalid or do not match");
     729             : 
     730          30 :     if (oldctrl->date_is_int != newctrl->date_is_int)
     731           0 :         pg_fatal("old and new pg_controldata date/time storage types do not match");
     732             : 
     733             :     /*
     734             :      * float8_pass_by_value does not need to match, but is used in
     735             :      * check_for_isn_and_int8_passing_mismatch().
     736             :      */
     737             : 
     738             :     /*
     739             :      * We might eventually allow upgrades from checksum to no-checksum
     740             :      * clusters.
     741             :      */
     742          30 :     if (oldctrl->data_checksum_version == 0 &&
     743           0 :         newctrl->data_checksum_version != 0)
     744           0 :         pg_fatal("old cluster does not use data checksums but the new one does");
     745          30 :     else if (oldctrl->data_checksum_version != 0 &&
     746          30 :              newctrl->data_checksum_version == 0)
     747           0 :         pg_fatal("old cluster uses data checksums but the new one does not");
     748          30 :     else if (oldctrl->data_checksum_version != newctrl->data_checksum_version)
     749           0 :         pg_fatal("old and new cluster pg_controldata checksum versions do not match");
     750          30 : }
     751             : 
     752             : 
     753             : void
     754           4 : disable_old_cluster(transferMode transfer_mode)
     755             : {
     756             :     char        old_path[MAXPGPATH],
     757             :                 new_path[MAXPGPATH];
     758             : 
     759             :     /* rename pg_control so old server cannot be accidentally started */
     760           4 :     prep_status("Adding \".old\" suffix to old global/pg_control");
     761             : 
     762           4 :     snprintf(old_path, sizeof(old_path), "%s/global/pg_control", old_cluster.pgdata);
     763           4 :     snprintf(new_path, sizeof(new_path), "%s/global/pg_control.old", old_cluster.pgdata);
     764           4 :     if (pg_mv_file(old_path, new_path) != 0)
     765           0 :         pg_fatal("could not rename file \"%s\" to \"%s\": %m",
     766             :                  old_path, new_path);
     767           4 :     check_ok();
     768             : 
     769           4 :     if (transfer_mode == TRANSFER_MODE_LINK)
     770           2 :         pg_log(PG_REPORT, "\n"
     771             :                "If you want to start the old cluster, you will need to remove\n"
     772             :                "the \".old\" suffix from %s/global/pg_control.old.\n"
     773             :                "Because \"link\" mode was used, the old cluster cannot be safely\n"
     774             :                "started once the new cluster has been started.",
     775             :                old_cluster.pgdata);
     776           2 :     else if (transfer_mode == TRANSFER_MODE_SWAP)
     777           2 :         pg_log(PG_REPORT, "\n"
     778             :                "Because \"swap\" mode was used, the old cluster can no longer be\n"
     779             :                "safely started.");
     780             :     else
     781           0 :         pg_fatal("unrecognized transfer mode");
     782           4 : }

Generated by: LCOV version 1.14