LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - controldata.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 286 436 65.6 %
Date: 2025-02-22 07:14:56 Functions: 2 3 66.7 %
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          40 : get_control_data(ClusterInfo *cluster)
      38             : {
      39             :     char        cmd[MAXPGPATH];
      40             :     char        bufin[MAX_STRING];
      41             :     FILE       *output;
      42             :     char       *p;
      43          40 :     bool        got_tli = false;
      44          40 :     bool        got_log_id = false;
      45          40 :     bool        got_log_seg = false;
      46          40 :     bool        got_xid = false;
      47          40 :     bool        got_oid = false;
      48          40 :     bool        got_multi = false;
      49          40 :     bool        got_oldestmulti = false;
      50          40 :     bool        got_oldestxid = false;
      51          40 :     bool        got_mxoff = false;
      52          40 :     bool        got_nextxlogfile = false;
      53          40 :     bool        got_float8_pass_by_value = false;
      54          40 :     bool        got_align = false;
      55          40 :     bool        got_blocksz = false;
      56          40 :     bool        got_largesz = false;
      57          40 :     bool        got_walsz = false;
      58          40 :     bool        got_walseg = false;
      59          40 :     bool        got_ident = false;
      60          40 :     bool        got_index = false;
      61          40 :     bool        got_toast = false;
      62          40 :     bool        got_large_object = false;
      63          40 :     bool        got_date_is_int = false;
      64          40 :     bool        got_data_checksum_version = false;
      65          40 :     bool        got_cluster_state = false;
      66          40 :     bool        got_default_char_signedness = false;
      67          40 :     char       *lc_collate = NULL;
      68          40 :     char       *lc_ctype = NULL;
      69          40 :     char       *lc_monetary = NULL;
      70          40 :     char       *lc_numeric = NULL;
      71          40 :     char       *lc_time = NULL;
      72          40 :     char       *lang = NULL;
      73          40 :     char       *language = NULL;
      74          40 :     char       *lc_all = NULL;
      75          40 :     char       *lc_messages = NULL;
      76          40 :     uint32      tli = 0;
      77          40 :     uint32      logid = 0;
      78          40 :     uint32      segno = 0;
      79             :     char       *resetwal_bin;
      80             :     int         rc;
      81          40 :     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          40 :     if (getenv("LC_COLLATE"))
      88           0 :         lc_collate = pg_strdup(getenv("LC_COLLATE"));
      89          40 :     if (getenv("LC_CTYPE"))
      90           0 :         lc_ctype = pg_strdup(getenv("LC_CTYPE"));
      91          40 :     if (getenv("LC_MONETARY"))
      92           0 :         lc_monetary = pg_strdup(getenv("LC_MONETARY"));
      93          40 :     if (getenv("LC_NUMERIC"))
      94           0 :         lc_numeric = pg_strdup(getenv("LC_NUMERIC"));
      95          40 :     if (getenv("LC_TIME"))
      96           0 :         lc_time = pg_strdup(getenv("LC_TIME"));
      97          40 :     if (getenv("LANG"))
      98          40 :         lang = pg_strdup(getenv("LANG"));
      99          40 :     if (getenv("LANGUAGE"))
     100           0 :         language = pg_strdup(getenv("LANGUAGE"));
     101          40 :     if (getenv("LC_ALL"))
     102           0 :         lc_all = pg_strdup(getenv("LC_ALL"));
     103          40 :     if (getenv("LC_MESSAGES"))
     104          40 :         lc_messages = pg_strdup(getenv("LC_MESSAGES"));
     105             : 
     106          40 :     unsetenv("LC_COLLATE");
     107          40 :     unsetenv("LC_CTYPE");
     108          40 :     unsetenv("LC_MONETARY");
     109          40 :     unsetenv("LC_NUMERIC");
     110          40 :     unsetenv("LC_TIME");
     111             : #ifndef WIN32
     112          40 :     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          40 :     unsetenv("LANGUAGE");
     118          40 :     unsetenv("LC_ALL");
     119          40 :     setenv("LC_MESSAGES", "C", 1);
     120             : 
     121             :     /*
     122             :      * Check for clean shutdown
     123             :      */
     124          40 :     if (!live_check || cluster == &new_cluster)
     125             :     {
     126             :         /* only pg_controldata outputs the cluster state */
     127          40 :         snprintf(cmd, sizeof(cmd), "\"%s/pg_controldata\" \"%s\"",
     128             :                  cluster->bindir, cluster->pgdata);
     129          40 :         fflush(NULL);
     130             : 
     131          40 :         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        2080 :         while (fgets(bufin, sizeof(bufin), output))
     136             :         {
     137        2040 :             if ((p = strstr(bufin, "Database cluster state:")) != NULL)
     138             :             {
     139          40 :                 p = strchr(p, ':');
     140             : 
     141          40 :                 if (p == NULL || strlen(p) <= 1)
     142           0 :                     pg_fatal("%d: database cluster state problem", __LINE__);
     143             : 
     144          40 :                 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          40 :                 (void) pg_strip_crlf(p);
     156         640 :                 while (*p == ' ')
     157         600 :                     p++;
     158          40 :                 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          40 :                 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          40 :                 got_cluster_state = true;
     173             :             }
     174             :         }
     175             : 
     176          40 :         rc = pclose(output);
     177          40 :         if (rc != 0)
     178           0 :             pg_fatal("could not get control data using %s: %s",
     179             :                      cmd, wait_result_to_str(rc));
     180             : 
     181          40 :         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          40 :     if (GET_MAJOR_VERSION(cluster->bin_version) <= 906)
     192           0 :         resetwal_bin = "pg_resetxlog\" -n";
     193             :     else
     194          40 :         resetwal_bin = "pg_resetwal\" -n";
     195          40 :     snprintf(cmd, sizeof(cmd), "\"%s/%s \"%s\"",
     196             :              cluster->bindir,
     197             :              live_check ? "pg_controldata\"" : resetwal_bin,
     198             :              cluster->pgdata);
     199          40 :     fflush(NULL);
     200             : 
     201          40 :     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          40 :     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        1480 :     while (fgets(bufin, sizeof(bufin), output))
     213             :     {
     214             :         /* In verbose mode, log each line */
     215        1440 :         pg_strip_crlf(bufin);
     216        1440 :         pg_log(PG_VERBOSE, "%s", bufin);
     217             : 
     218        1440 :         if ((p = strstr(bufin, "pg_control version number:")) != NULL)
     219             :         {
     220          40 :             p = strchr(p, ':');
     221             : 
     222          40 :             if (p == NULL || strlen(p) <= 1)
     223           0 :                 pg_fatal("%d: pg_resetwal problem", __LINE__);
     224             : 
     225          40 :             p++;                /* remove ':' char */
     226          40 :             cluster->controldata.ctrl_ver = str2uint(p);
     227             :         }
     228        1400 :         else if ((p = strstr(bufin, "Catalog version number:")) != NULL)
     229             :         {
     230          40 :             p = strchr(p, ':');
     231             : 
     232          40 :             if (p == NULL || strlen(p) <= 1)
     233           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     234             : 
     235          40 :             p++;                /* remove ':' char */
     236          40 :             cluster->controldata.cat_ver = str2uint(p);
     237             :         }
     238        1360 :         else if ((p = strstr(bufin, "Latest checkpoint's TimeLineID:")) != NULL)
     239             :         {
     240          40 :             p = strchr(p, ':');
     241             : 
     242          40 :             if (p == NULL || strlen(p) <= 1)
     243           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     244             : 
     245          40 :             p++;                /* remove ':' char */
     246          40 :             tli = str2uint(p);
     247          40 :             got_tli = true;
     248             :         }
     249        1320 :         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        1320 :         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        1320 :         else if ((p = strstr(bufin, "Latest checkpoint's NextXID:")) != NULL)
     272             :         {
     273          40 :             p = strchr(p, ':');
     274             : 
     275          40 :             if (p == NULL || strlen(p) <= 1)
     276           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     277             : 
     278          40 :             p++;                /* remove ':' char */
     279          40 :             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          40 :             if (strchr(p, '/') != NULL)
     288           0 :                 p = strchr(p, '/');
     289          40 :             else if (GET_MAJOR_VERSION(cluster->major_version) >= 906)
     290          40 :                 p = strchr(p, ':');
     291             :             else
     292           0 :                 p = NULL;
     293             : 
     294          40 :             if (p == NULL || strlen(p) <= 1)
     295           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     296             : 
     297          40 :             p++;                /* remove '/' or ':' char */
     298          40 :             cluster->controldata.chkpnt_nxtxid = str2uint(p);
     299          40 :             got_xid = true;
     300             :         }
     301        1280 :         else if ((p = strstr(bufin, "Latest checkpoint's NextOID:")) != NULL)
     302             :         {
     303          40 :             p = strchr(p, ':');
     304             : 
     305          40 :             if (p == NULL || strlen(p) <= 1)
     306           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     307             : 
     308          40 :             p++;                /* remove ':' char */
     309          40 :             cluster->controldata.chkpnt_nxtoid = str2uint(p);
     310          40 :             got_oid = true;
     311             :         }
     312        1240 :         else if ((p = strstr(bufin, "Latest checkpoint's NextMultiXactId:")) != NULL)
     313             :         {
     314          40 :             p = strchr(p, ':');
     315             : 
     316          40 :             if (p == NULL || strlen(p) <= 1)
     317           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     318             : 
     319          40 :             p++;                /* remove ':' char */
     320          40 :             cluster->controldata.chkpnt_nxtmulti = str2uint(p);
     321          40 :             got_multi = true;
     322             :         }
     323        1200 :         else if ((p = strstr(bufin, "Latest checkpoint's oldestXID:")) != NULL)
     324             :         {
     325          40 :             p = strchr(p, ':');
     326             : 
     327          40 :             if (p == NULL || strlen(p) <= 1)
     328           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     329             : 
     330          40 :             p++;                /* remove ':' char */
     331          40 :             cluster->controldata.chkpnt_oldstxid = str2uint(p);
     332          40 :             got_oldestxid = true;
     333             :         }
     334        1160 :         else if ((p = strstr(bufin, "Latest checkpoint's oldestMultiXid:")) != NULL)
     335             :         {
     336          40 :             p = strchr(p, ':');
     337             : 
     338          40 :             if (p == NULL || strlen(p) <= 1)
     339           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     340             : 
     341          40 :             p++;                /* remove ':' char */
     342          40 :             cluster->controldata.chkpnt_oldstMulti = str2uint(p);
     343          40 :             got_oldestmulti = true;
     344             :         }
     345        1120 :         else if ((p = strstr(bufin, "Latest checkpoint's NextMultiOffset:")) != NULL)
     346             :         {
     347          40 :             p = strchr(p, ':');
     348             : 
     349          40 :             if (p == NULL || strlen(p) <= 1)
     350           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     351             : 
     352          40 :             p++;                /* remove ':' char */
     353          40 :             cluster->controldata.chkpnt_nxtmxoff = str2uint(p);
     354          40 :             got_mxoff = true;
     355             :         }
     356        1080 :         else if ((p = strstr(bufin, "First log segment after reset:")) != NULL)
     357             :         {
     358             :             /* Skip the colon and any whitespace after it */
     359          40 :             p = strchr(p, ':');
     360          40 :             if (p == NULL || strlen(p) <= 1)
     361           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     362          40 :             p = strpbrk(p, "01234567890ABCDEF");
     363          40 :             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          40 :             if (strspn(p, "0123456789ABCDEF") != 24)
     368           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     369             : 
     370          40 :             strlcpy(cluster->controldata.nextxlogfile, p, 25);
     371          40 :             got_nextxlogfile = true;
     372             :         }
     373        1040 :         else if ((p = strstr(bufin, "Float8 argument passing:")) != NULL)
     374             :         {
     375          40 :             p = strchr(p, ':');
     376             : 
     377          40 :             if (p == NULL || strlen(p) <= 1)
     378           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     379             : 
     380          40 :             p++;                /* remove ':' char */
     381             :             /* used later for contrib check */
     382          40 :             cluster->controldata.float8_pass_by_value = strstr(p, "by value") != NULL;
     383          40 :             got_float8_pass_by_value = true;
     384             :         }
     385        1000 :         else if ((p = strstr(bufin, "Maximum data alignment:")) != NULL)
     386             :         {
     387          40 :             p = strchr(p, ':');
     388             : 
     389          40 :             if (p == NULL || strlen(p) <= 1)
     390           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     391             : 
     392          40 :             p++;                /* remove ':' char */
     393          40 :             cluster->controldata.align = str2uint(p);
     394          40 :             got_align = true;
     395             :         }
     396         960 :         else if ((p = strstr(bufin, "Database block size:")) != NULL)
     397             :         {
     398          40 :             p = strchr(p, ':');
     399             : 
     400          40 :             if (p == NULL || strlen(p) <= 1)
     401           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     402             : 
     403          40 :             p++;                /* remove ':' char */
     404          40 :             cluster->controldata.blocksz = str2uint(p);
     405          40 :             got_blocksz = true;
     406             :         }
     407         920 :         else if ((p = strstr(bufin, "Blocks per segment of large relation:")) != NULL)
     408             :         {
     409          40 :             p = strchr(p, ':');
     410             : 
     411          40 :             if (p == NULL || strlen(p) <= 1)
     412           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     413             : 
     414          40 :             p++;                /* remove ':' char */
     415          40 :             cluster->controldata.largesz = str2uint(p);
     416          40 :             got_largesz = true;
     417             :         }
     418         880 :         else if ((p = strstr(bufin, "WAL block size:")) != NULL)
     419             :         {
     420          40 :             p = strchr(p, ':');
     421             : 
     422          40 :             if (p == NULL || strlen(p) <= 1)
     423           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     424             : 
     425          40 :             p++;                /* remove ':' char */
     426          40 :             cluster->controldata.walsz = str2uint(p);
     427          40 :             got_walsz = true;
     428             :         }
     429         840 :         else if ((p = strstr(bufin, "Bytes per WAL segment:")) != NULL)
     430             :         {
     431          40 :             p = strchr(p, ':');
     432             : 
     433          40 :             if (p == NULL || strlen(p) <= 1)
     434           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     435             : 
     436          40 :             p++;                /* remove ':' char */
     437          40 :             cluster->controldata.walseg = str2uint(p);
     438          40 :             got_walseg = true;
     439             :         }
     440         800 :         else if ((p = strstr(bufin, "Maximum length of identifiers:")) != NULL)
     441             :         {
     442          40 :             p = strchr(p, ':');
     443             : 
     444          40 :             if (p == NULL || strlen(p) <= 1)
     445           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     446             : 
     447          40 :             p++;                /* remove ':' char */
     448          40 :             cluster->controldata.ident = str2uint(p);
     449          40 :             got_ident = true;
     450             :         }
     451         760 :         else if ((p = strstr(bufin, "Maximum columns in an index:")) != NULL)
     452             :         {
     453          40 :             p = strchr(p, ':');
     454             : 
     455          40 :             if (p == NULL || strlen(p) <= 1)
     456           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     457             : 
     458          40 :             p++;                /* remove ':' char */
     459          40 :             cluster->controldata.index = str2uint(p);
     460          40 :             got_index = true;
     461             :         }
     462         720 :         else if ((p = strstr(bufin, "Maximum size of a TOAST chunk:")) != NULL)
     463             :         {
     464          40 :             p = strchr(p, ':');
     465             : 
     466          40 :             if (p == NULL || strlen(p) <= 1)
     467           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     468             : 
     469          40 :             p++;                /* remove ':' char */
     470          40 :             cluster->controldata.toast = str2uint(p);
     471          40 :             got_toast = true;
     472             :         }
     473         680 :         else if ((p = strstr(bufin, "Size of a large-object chunk:")) != NULL)
     474             :         {
     475          40 :             p = strchr(p, ':');
     476             : 
     477          40 :             if (p == NULL || strlen(p) <= 1)
     478           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     479             : 
     480          40 :             p++;                /* remove ':' char */
     481          40 :             cluster->controldata.large_object = str2uint(p);
     482          40 :             got_large_object = true;
     483             :         }
     484         640 :         else if ((p = strstr(bufin, "Date/time type storage:")) != NULL)
     485             :         {
     486          40 :             p = strchr(p, ':');
     487             : 
     488          40 :             if (p == NULL || strlen(p) <= 1)
     489           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     490             : 
     491          40 :             p++;                /* remove ':' char */
     492          40 :             cluster->controldata.date_is_int = strstr(p, "64-bit integers") != NULL;
     493          40 :             got_date_is_int = true;
     494             :         }
     495         600 :         else if ((p = strstr(bufin, "checksum")) != NULL)
     496             :         {
     497          40 :             p = strchr(p, ':');
     498             : 
     499          40 :             if (p == NULL || strlen(p) <= 1)
     500           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     501             : 
     502          40 :             p++;                /* remove ':' char */
     503          40 :             cluster->controldata.data_checksum_version = str2uint(p);
     504          40 :             got_data_checksum_version = true;
     505             :         }
     506         560 :         else if ((p = strstr(bufin, "Default char data signedness:")) != NULL)
     507             :         {
     508          40 :             p = strchr(p, ':');
     509             : 
     510          40 :             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          40 :             p++;
     515         400 :             while (isspace((unsigned char) *p))
     516         360 :                 p++;
     517             : 
     518             :             /* The value should be either 'signed' or 'unsigned' */
     519          40 :             if (strcmp(p, "signed") != 0 && strcmp(p, "unsigned") != 0)
     520           0 :                 pg_fatal("%d: controldata retrieval problem", __LINE__);
     521             : 
     522          40 :             cluster->controldata.default_char_signedness = strcmp(p, "signed") == 0;
     523          40 :             got_default_char_signedness = true;
     524             :         }
     525             :     }
     526             : 
     527          40 :     rc = pclose(output);
     528          40 :     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          40 :     if (lc_collate)
     537           0 :         setenv("LC_COLLATE", lc_collate, 1);
     538          40 :     if (lc_ctype)
     539           0 :         setenv("LC_CTYPE", lc_ctype, 1);
     540          40 :     if (lc_monetary)
     541           0 :         setenv("LC_MONETARY", lc_monetary, 1);
     542          40 :     if (lc_numeric)
     543           0 :         setenv("LC_NUMERIC", lc_numeric, 1);
     544          40 :     if (lc_time)
     545           0 :         setenv("LC_TIME", lc_time, 1);
     546          40 :     if (lang)
     547          40 :         setenv("LANG", lang, 1);
     548             :     else
     549           0 :         unsetenv("LANG");
     550          40 :     if (language)
     551           0 :         setenv("LANGUAGE", language, 1);
     552          40 :     if (lc_all)
     553           0 :         setenv("LC_ALL", lc_all, 1);
     554          40 :     if (lc_messages)
     555          40 :         setenv("LC_MESSAGES", lc_messages, 1);
     556             :     else
     557           0 :         unsetenv("LC_MESSAGES");
     558             : 
     559          40 :     pg_free(lc_collate);
     560          40 :     pg_free(lc_ctype);
     561          40 :     pg_free(lc_monetary);
     562          40 :     pg_free(lc_numeric);
     563          40 :     pg_free(lc_time);
     564          40 :     pg_free(lang);
     565          40 :     pg_free(language);
     566          40 :     pg_free(lc_all);
     567          40 :     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          40 :     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          40 :     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          40 :     if (!got_xid || !got_oid ||
     602          40 :         !got_multi || !got_oldestxid ||
     603          40 :         (!got_oldestmulti &&
     604           0 :          cluster->controldata.cat_ver >= MULTIXACT_FORMATCHANGE_CAT_VER) ||
     605          40 :         !got_mxoff || (!live_check && !got_nextxlogfile) ||
     606          40 :         !got_float8_pass_by_value || !got_align || !got_blocksz ||
     607          40 :         !got_largesz || !got_walsz || !got_walseg || !got_ident ||
     608          40 :         !got_index || !got_toast ||
     609          40 :         (!got_large_object &&
     610           0 :          cluster->controldata.ctrl_ver >= LARGE_OBJECT_SIZE_PG_CONTROL_VER) ||
     611          40 :         !got_date_is_int || !got_data_checksum_version ||
     612          40 :         (!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          40 : }
     689             : 
     690             : 
     691             : /*
     692             :  * check_control_data()
     693             :  *
     694             :  * check to make sure the control data settings are compatible
     695             :  */
     696             : void
     697          20 : check_control_data(ControlData *oldctrl,
     698             :                    ControlData *newctrl)
     699             : {
     700          20 :     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          20 :     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          20 :     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          20 :     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          20 :     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          20 :     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          20 :     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          20 :     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          20 :     if (oldctrl->large_object != 0 &&
     727          20 :         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          20 :     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          20 :     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          20 :     else if (oldctrl->data_checksum_version != 0 &&
     746          20 :              newctrl->data_checksum_version == 0)
     747           0 :         pg_fatal("old cluster uses data checksums but the new one does not");
     748          20 :     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          20 : }
     751             : 
     752             : 
     753             : void
     754           0 : disable_old_cluster(void)
     755             : {
     756             :     char        old_path[MAXPGPATH],
     757             :                 new_path[MAXPGPATH];
     758             : 
     759             :     /* rename pg_control so old server cannot be accidentally started */
     760           0 :     prep_status("Adding \".old\" suffix to old global/pg_control");
     761             : 
     762           0 :     snprintf(old_path, sizeof(old_path), "%s/global/pg_control", old_cluster.pgdata);
     763           0 :     snprintf(new_path, sizeof(new_path), "%s/global/pg_control.old", old_cluster.pgdata);
     764           0 :     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           0 :     check_ok();
     768             : 
     769           0 :     pg_log(PG_REPORT, "\n"
     770             :            "If you want to start the old cluster, you will need to remove\n"
     771             :            "the \".old\" suffix from %s/global/pg_control.old.\n"
     772             :            "Because \"link\" mode was used, the old cluster cannot be safely\n"
     773             :            "started once the new cluster has been started.",
     774             :            old_cluster.pgdata);
     775           0 : }

Generated by: LCOV version 1.14