LCOV - code coverage report
Current view: top level - src/bin/pg_controldata - pg_controldata.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 88.3 % 154 136
Test Date: 2026-07-03 19:57:34 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 74.2 % 66 49

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

Generated by: LCOV version 2.0-1