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

Generated by: LCOV version 1.14