LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - controldata.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 67.9 % 442 300
Test Date: 2026-04-16 12:16:20 Functions: 100.0 % 3 3
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.0-1