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

Generated by: LCOV version 1.14