LCOV - code coverage report
Current view: top level - src/bin/pg_controldata - pg_controldata.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 123 141 87.2 %
Date: 2020-01-25 22:06:23 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * pg_controldata
       3             :  *
       4             :  * reads the data from $PGDATA/global/pg_control
       5             :  *
       6             :  * copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
       7             :  * license: BSD
       8             :  *
       9             :  * src/bin/pg_controldata/pg_controldata.c
      10             :  */
      11             : 
      12             : /*
      13             :  * We have to use postgres.h not postgres_fe.h here, because there's so much
      14             :  * backend-only stuff in the XLOG include files we need.  But we need a
      15             :  * frontend-ish environment otherwise.  Hence this ugly hack.
      16             :  */
      17             : #define FRONTEND 1
      18             : 
      19             : #include "postgres.h"
      20             : 
      21             : #include <time.h>
      22             : 
      23             : #include "access/transam.h"
      24             : #include "access/xlog.h"
      25             : #include "access/xlog_internal.h"
      26             : #include "catalog/pg_control.h"
      27             : #include "common/controldata_utils.h"
      28             : #include "common/logging.h"
      29             : #include "getopt_long.h"
      30             : #include "pg_getopt.h"
      31             : 
      32             : static void
      33           2 : usage(const char *progname)
      34             : {
      35           2 :     printf(_("%s displays control information of a PostgreSQL database cluster.\n\n"), progname);
      36           2 :     printf(_("Usage:\n"));
      37           2 :     printf(_("  %s [OPTION] [DATADIR]\n"), progname);
      38           2 :     printf(_("\nOptions:\n"));
      39           2 :     printf(_(" [-D, --pgdata=]DATADIR  data directory\n"));
      40           2 :     printf(_("  -V, --version          output version information, then exit\n"));
      41           2 :     printf(_("  -?, --help             show this help, then exit\n"));
      42           2 :     printf(_("\nIf no data directory (DATADIR) is specified, "
      43             :              "the environment variable PGDATA\nis used.\n\n"));
      44           2 :     printf(_("Report bugs to <pgsql-bugs@lists.postgresql.org>.\n"));
      45           2 : }
      46             : 
      47             : 
      48             : static const char *
      49          20 : dbState(DBState state)
      50             : {
      51          20 :     switch (state)
      52             :     {
      53           2 :         case DB_STARTUP:
      54           2 :             return _("starting up");
      55          16 :         case DB_SHUTDOWNED:
      56          16 :             return _("shut down");
      57           2 :         case DB_SHUTDOWNED_IN_RECOVERY:
      58           2 :             return _("shut down in recovery");
      59           0 :         case DB_SHUTDOWNING:
      60           0 :             return _("shutting down");
      61           0 :         case DB_IN_CRASH_RECOVERY:
      62           0 :             return _("in crash recovery");
      63           0 :         case DB_IN_ARCHIVE_RECOVERY:
      64           0 :             return _("in archive recovery");
      65           0 :         case DB_IN_PRODUCTION:
      66           0 :             return _("in production");
      67             :     }
      68           0 :     return _("unrecognized status code");
      69             : }
      70             : 
      71             : static const char *
      72          20 : wal_level_str(WalLevel wal_level)
      73             : {
      74          20 :     switch (wal_level)
      75             :     {
      76           2 :         case WAL_LEVEL_MINIMAL:
      77           2 :             return "minimal";
      78          18 :         case WAL_LEVEL_REPLICA:
      79          18 :             return "replica";
      80           0 :         case WAL_LEVEL_LOGICAL:
      81           0 :             return "logical";
      82             :     }
      83           0 :     return _("unrecognized wal_level");
      84             : }
      85             : 
      86             : 
      87             : int
      88          30 : main(int argc, char *argv[])
      89             : {
      90             :     static struct option long_options[] = {
      91             :         {"pgdata", required_argument, NULL, 'D'},
      92             :         {NULL, 0, NULL, 0}
      93             :     };
      94             : 
      95             :     ControlFileData *ControlFile;
      96             :     bool        crc_ok;
      97          30 :     char       *DataDir = NULL;
      98             :     time_t      time_tmp;
      99             :     char        pgctime_str[128];
     100             :     char        ckpttime_str[128];
     101             :     char        mock_auth_nonce_str[MOCK_AUTH_NONCE_LEN * 2 + 1];
     102          30 :     const char *strftime_fmt = "%c";
     103             :     const char *progname;
     104             :     char        xlogfilename[MAXFNAMELEN];
     105             :     int         c;
     106             :     int         i;
     107             :     int         WalSegSz;
     108             : 
     109          30 :     pg_logging_init(argv[0]);
     110          30 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_controldata"));
     111          30 :     progname = get_progname(argv[0]);
     112             : 
     113          30 :     if (argc > 1)
     114             :     {
     115          28 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
     116             :         {
     117           2 :             usage(progname);
     118           2 :             exit(0);
     119             :         }
     120          26 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     121             :         {
     122           2 :             puts("pg_controldata (PostgreSQL) " PG_VERSION);
     123           2 :             exit(0);
     124             :         }
     125             :     }
     126             : 
     127          26 :     while ((c = getopt_long(argc, argv, "D:", long_options, NULL)) != -1)
     128             :     {
     129           2 :         switch (c)
     130             :         {
     131           0 :             case 'D':
     132           0 :                 DataDir = optarg;
     133           0 :                 break;
     134             : 
     135           2 :             default:
     136           2 :                 fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     137           2 :                 exit(1);
     138             :         }
     139             :     }
     140             : 
     141          24 :     if (DataDir == NULL)
     142             :     {
     143          24 :         if (optind < argc)
     144          22 :             DataDir = argv[optind++];
     145             :         else
     146           2 :             DataDir = getenv("PGDATA");
     147             :     }
     148             : 
     149             :     /* Complain if any arguments remain */
     150          24 :     if (optind < argc)
     151             :     {
     152           0 :         pg_log_error("too many command-line arguments (first is \"%s\")",
     153             :                      argv[optind]);
     154           0 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
     155             :                 progname);
     156           0 :         exit(1);
     157             :     }
     158             : 
     159          24 :     if (DataDir == NULL)
     160             :     {
     161           2 :         pg_log_error("no data directory specified");
     162           2 :         fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
     163           2 :         exit(1);
     164             :     }
     165             : 
     166             :     /* get a copy of the control file */
     167          22 :     ControlFile = get_controlfile(DataDir, &crc_ok);
     168          20 :     if (!crc_ok)
     169           2 :         printf(_("WARNING: Calculated CRC checksum does not match value stored in file.\n"
     170             :                  "Either the file is corrupt, or it has a different layout than this program\n"
     171             :                  "is expecting.  The results below are untrustworthy.\n\n"));
     172             : 
     173             :     /* set wal segment size */
     174          20 :     WalSegSz = ControlFile->xlog_seg_size;
     175             : 
     176          20 :     if (!IsValidWalSegSize(WalSegSz))
     177             :     {
     178           2 :         printf(_("WARNING: invalid WAL segment size\n"));
     179           2 :         printf(ngettext("The WAL segment size stored in the file, %d byte, is not a power of two\n"
     180             :                         "between 1 MB and 1 GB.  The file is corrupt and the results below are\n"
     181             :                         "untrustworthy.\n\n",
     182             :                         "The WAL segment size stored in the file, %d bytes, is not a power of two\n"
     183             :                         "between 1 MB and 1 GB.  The file is corrupt and the results below are\n"
     184             :                         "untrustworthy.\n\n",
     185             :                         WalSegSz),
     186             :                WalSegSz);
     187             :     }
     188             : 
     189             :     /*
     190             :      * This slightly-chintzy coding will work as long as the control file
     191             :      * timestamps are within the range of time_t; that should be the case in
     192             :      * all foreseeable circumstances, so we don't bother importing the
     193             :      * backend's timezone library into pg_controldata.
     194             :      *
     195             :      * Use variable for format to suppress overly-anal-retentive gcc warning
     196             :      * about %c
     197             :      */
     198          20 :     time_tmp = (time_t) ControlFile->time;
     199          20 :     strftime(pgctime_str, sizeof(pgctime_str), strftime_fmt,
     200          20 :              localtime(&time_tmp));
     201          20 :     time_tmp = (time_t) ControlFile->checkPointCopy.time;
     202          20 :     strftime(ckpttime_str, sizeof(ckpttime_str), strftime_fmt,
     203          20 :              localtime(&time_tmp));
     204             : 
     205             :     /*
     206             :      * Calculate name of the WAL file containing the latest checkpoint's REDO
     207             :      * start point.
     208             :      *
     209             :      * A corrupted control file could report a WAL segment size of 0, and to
     210             :      * guard against division by zero, we need to treat that specially.
     211             :      */
     212          20 :     if (WalSegSz != 0)
     213             :     {
     214             :         XLogSegNo   segno;
     215             : 
     216          18 :         XLByteToSeg(ControlFile->checkPointCopy.redo, segno, WalSegSz);
     217          18 :         XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
     218             :                      segno, WalSegSz);
     219             :     }
     220             :     else
     221           2 :         strcpy(xlogfilename, _("???"));
     222             : 
     223         660 :     for (i = 0; i < MOCK_AUTH_NONCE_LEN; i++)
     224         640 :         snprintf(&mock_auth_nonce_str[i * 2], 3, "%02x",
     225         640 :                  (unsigned char) ControlFile->mock_authentication_nonce[i]);
     226             : 
     227          20 :     printf(_("pg_control version number:            %u\n"),
     228             :            ControlFile->pg_control_version);
     229          20 :     printf(_("Catalog version number:               %u\n"),
     230             :            ControlFile->catalog_version_no);
     231          20 :     printf(_("Database system identifier:           %llu\n"),
     232             :            (unsigned long long) ControlFile->system_identifier);
     233          20 :     printf(_("Database cluster state:               %s\n"),
     234             :            dbState(ControlFile->state));
     235          20 :     printf(_("pg_control last modified:             %s\n"),
     236             :            pgctime_str);
     237          20 :     printf(_("Latest checkpoint location:           %X/%X\n"),
     238             :            (uint32) (ControlFile->checkPoint >> 32),
     239             :            (uint32) ControlFile->checkPoint);
     240          20 :     printf(_("Latest checkpoint's REDO location:    %X/%X\n"),
     241             :            (uint32) (ControlFile->checkPointCopy.redo >> 32),
     242             :            (uint32) ControlFile->checkPointCopy.redo);
     243          20 :     printf(_("Latest checkpoint's REDO WAL file:    %s\n"),
     244             :            xlogfilename);
     245          20 :     printf(_("Latest checkpoint's TimeLineID:       %u\n"),
     246             :            ControlFile->checkPointCopy.ThisTimeLineID);
     247          20 :     printf(_("Latest checkpoint's PrevTimeLineID:   %u\n"),
     248             :            ControlFile->checkPointCopy.PrevTimeLineID);
     249          20 :     printf(_("Latest checkpoint's full_page_writes: %s\n"),
     250             :            ControlFile->checkPointCopy.fullPageWrites ? _("on") : _("off"));
     251          20 :     printf(_("Latest checkpoint's NextXID:          %u:%u\n"),
     252             :            EpochFromFullTransactionId(ControlFile->checkPointCopy.nextFullXid),
     253             :            XidFromFullTransactionId(ControlFile->checkPointCopy.nextFullXid));
     254          20 :     printf(_("Latest checkpoint's NextOID:          %u\n"),
     255             :            ControlFile->checkPointCopy.nextOid);
     256          20 :     printf(_("Latest checkpoint's NextMultiXactId:  %u\n"),
     257             :            ControlFile->checkPointCopy.nextMulti);
     258          20 :     printf(_("Latest checkpoint's NextMultiOffset:  %u\n"),
     259             :            ControlFile->checkPointCopy.nextMultiOffset);
     260          20 :     printf(_("Latest checkpoint's oldestXID:        %u\n"),
     261             :            ControlFile->checkPointCopy.oldestXid);
     262          20 :     printf(_("Latest checkpoint's oldestXID's DB:   %u\n"),
     263             :            ControlFile->checkPointCopy.oldestXidDB);
     264          20 :     printf(_("Latest checkpoint's oldestActiveXID:  %u\n"),
     265             :            ControlFile->checkPointCopy.oldestActiveXid);
     266          20 :     printf(_("Latest checkpoint's oldestMultiXid:   %u\n"),
     267             :            ControlFile->checkPointCopy.oldestMulti);
     268          20 :     printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
     269             :            ControlFile->checkPointCopy.oldestMultiDB);
     270          20 :     printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
     271             :            ControlFile->checkPointCopy.oldestCommitTsXid);
     272          20 :     printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
     273             :            ControlFile->checkPointCopy.newestCommitTsXid);
     274          20 :     printf(_("Time of latest checkpoint:            %s\n"),
     275             :            ckpttime_str);
     276          20 :     printf(_("Fake LSN counter for unlogged rels:   %X/%X\n"),
     277             :            (uint32) (ControlFile->unloggedLSN >> 32),
     278             :            (uint32) ControlFile->unloggedLSN);
     279          20 :     printf(_("Minimum recovery ending location:     %X/%X\n"),
     280             :            (uint32) (ControlFile->minRecoveryPoint >> 32),
     281             :            (uint32) ControlFile->minRecoveryPoint);
     282          20 :     printf(_("Min recovery ending loc's timeline:   %u\n"),
     283             :            ControlFile->minRecoveryPointTLI);
     284          20 :     printf(_("Backup start location:                %X/%X\n"),
     285             :            (uint32) (ControlFile->backupStartPoint >> 32),
     286             :            (uint32) ControlFile->backupStartPoint);
     287          20 :     printf(_("Backup end location:                  %X/%X\n"),
     288             :            (uint32) (ControlFile->backupEndPoint >> 32),
     289             :            (uint32) ControlFile->backupEndPoint);
     290          20 :     printf(_("End-of-backup record required:        %s\n"),
     291             :            ControlFile->backupEndRequired ? _("yes") : _("no"));
     292          20 :     printf(_("wal_level setting:                    %s\n"),
     293             :            wal_level_str(ControlFile->wal_level));
     294          20 :     printf(_("wal_log_hints setting:                %s\n"),
     295             :            ControlFile->wal_log_hints ? _("on") : _("off"));
     296          20 :     printf(_("max_connections setting:              %d\n"),
     297             :            ControlFile->MaxConnections);
     298          20 :     printf(_("max_worker_processes setting:         %d\n"),
     299             :            ControlFile->max_worker_processes);
     300          20 :     printf(_("max_wal_senders setting:              %d\n"),
     301             :            ControlFile->max_wal_senders);
     302          20 :     printf(_("max_prepared_xacts setting:           %d\n"),
     303             :            ControlFile->max_prepared_xacts);
     304          20 :     printf(_("max_locks_per_xact setting:           %d\n"),
     305             :            ControlFile->max_locks_per_xact);
     306          20 :     printf(_("track_commit_timestamp setting:       %s\n"),
     307             :            ControlFile->track_commit_timestamp ? _("on") : _("off"));
     308          20 :     printf(_("Maximum data alignment:               %u\n"),
     309             :            ControlFile->maxAlign);
     310             :     /* we don't print floatFormat since can't say much useful about it */
     311          20 :     printf(_("Database block size:                  %u\n"),
     312             :            ControlFile->blcksz);
     313          20 :     printf(_("Blocks per segment of large relation: %u\n"),
     314             :            ControlFile->relseg_size);
     315          20 :     printf(_("WAL block size:                       %u\n"),
     316             :            ControlFile->xlog_blcksz);
     317          20 :     printf(_("Bytes per WAL segment:                %u\n"),
     318             :            ControlFile->xlog_seg_size);
     319          20 :     printf(_("Maximum length of identifiers:        %u\n"),
     320             :            ControlFile->nameDataLen);
     321          20 :     printf(_("Maximum columns in an index:          %u\n"),
     322             :            ControlFile->indexMaxKeys);
     323          20 :     printf(_("Maximum size of a TOAST chunk:        %u\n"),
     324             :            ControlFile->toast_max_chunk_size);
     325          20 :     printf(_("Size of a large-object chunk:         %u\n"),
     326             :            ControlFile->loblksize);
     327             :     /* This is no longer configurable, but users may still expect to see it: */
     328          20 :     printf(_("Date/time type storage:               %s\n"),
     329             :            _("64-bit integers"));
     330          20 :     printf(_("Float8 argument passing:              %s\n"),
     331             :            (ControlFile->float8ByVal ? _("by value") : _("by reference")));
     332          20 :     printf(_("Data page checksum version:           %u\n"),
     333             :            ControlFile->data_checksum_version);
     334          20 :     printf(_("Mock authentication nonce:            %s\n"),
     335             :            mock_auth_nonce_str);
     336          20 :     return 0;
     337             : }

Generated by: LCOV version 1.13