LCOV - differential code coverage report
Current view: top level - src/bin/pg_controldata - pg_controldata.c (source / functions) Coverage Total Hit UBC GBC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 90.3 % 144 130 14 4 5 121 3
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 4 4 1 3
Baseline: 16@8cea358b128 Branches: 75.9 % 58 44 14 2 42
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (180,240] days: 100.0 % 5 5 5
(240..) days: 89.9 % 139 125 14 4 121
Function coverage date bins:
(240..) days: 100.0 % 4 4 1 3
Branch coverage date bins:
(240..) days: 75.9 % 58 44 14 2 42

 Age         Owner                    Branch data    TLA  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
 7907 peter_e@gmx.net            33                 :CBC           1 : usage(const char *progname)
                                 34                 :                : {
 7571                            35                 :              1 :     printf(_("%s displays control information of a PostgreSQL database cluster.\n\n"), progname);
 4318                            36                 :              1 :     printf(_("Usage:\n"));
 3133                            37                 :              1 :     printf(_("  %s [OPTION] [DATADIR]\n"), progname);
 4318                            38                 :              1 :     printf(_("\nOptions:\n"));
 2159                            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"));
 7143 neilc@samurai.com          42                 :              1 :     printf(_("\nIf no data directory (DATADIR) is specified, "
                                 43                 :                :              "the environment variable PGDATA\nis used.\n\n"));
 1507 peter@eisentraut.org       44                 :              1 :     printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
                                 45                 :              1 :     printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
 7907 peter_e@gmx.net            46                 :              1 : }
                                 47                 :                : 
                                 48                 :                : 
                                 49                 :                : static const char *
 8433 tgl@sss.pgh.pa.us          50                 :             30 : dbState(DBState state)
                                 51                 :                : {
                                 52   [ +  +  +  -  :             30 :     switch (state)
                                        -  -  +  - ]
                                 53                 :                :     {
 8207 bruce@momjian.us           54                 :              1 :         case DB_STARTUP:
 7895 peter_e@gmx.net            55                 :              1 :             return _("starting up");
 8433 tgl@sss.pgh.pa.us          56                 :             26 :         case DB_SHUTDOWNED:
 7895 peter_e@gmx.net            57                 :             26 :             return _("shut down");
 5064 rhaas@postgresql.org       58                 :              2 :         case DB_SHUTDOWNED_IN_RECOVERY:
                                 59                 :              2 :             return _("shut down in recovery");
 8433 tgl@sss.pgh.pa.us          60                 :UBC           0 :         case DB_SHUTDOWNING:
 7895 peter_e@gmx.net            61                 :              0 :             return _("shutting down");
 6460 tgl@sss.pgh.pa.us          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");
 8433 tgl@sss.pgh.pa.us          66                 :GBC           1 :         case DB_IN_PRODUCTION:
 7895 peter_e@gmx.net            67                 :              1 :             return _("in production");
                                 68                 :                :     }
 7907 peter_e@gmx.net            69                 :UBC           0 :     return _("unrecognized status code");
                                 70                 :                : }
                                 71                 :                : 
                                 72                 :                : static const char *
 5100 heikki.linnakangas@i       73                 :CBC          30 : wal_level_str(WalLevel wal_level)
                                 74                 :                : {
                                 75   [ +  +  +  - ]:             30 :     switch (wal_level)
                                 76                 :                :     {
                                 77                 :             11 :         case WAL_LEVEL_MINIMAL:
                                 78                 :             11 :             return "minimal";
 2967 peter_e@gmx.net            79                 :             13 :         case WAL_LEVEL_REPLICA:
                                 80                 :             13 :             return "replica";
 3778 rhaas@postgresql.org       81                 :GBC           6 :         case WAL_LEVEL_LOGICAL:
                                 82                 :              6 :             return "logical";
                                 83                 :                :     }
 5100 heikki.linnakangas@i       84                 :UBC           0 :     return _("unrecognized wal_level");
                                 85                 :                : }
                                 86                 :                : 
                                 87                 :                : 
                                 88                 :                : int
 8256 peter_e@gmx.net            89                 :CBC          53 : 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;
 3460 heikki.linnakangas@i       98                 :             53 :     char       *DataDir = NULL;
                                 99                 :                :     time_t      time_tmp;
                                100                 :                :     char        pgctime_str[128];
                                101                 :                :     char        ckpttime_str[128];
                                102                 :                :     char        mock_auth_nonce_str[MOCK_AUTH_NONCE_LEN * 2 + 1];
 6237 neilc@samurai.com         103                 :             53 :     const char *strftime_fmt = "%c";
                                104                 :                :     const char *progname;
                                105                 :                :     char        xlogfilename[MAXFNAMELEN];
                                106                 :                :     int         c;
                                107                 :                :     int         i;
                                108                 :                :     int         WalSegSz;
                                109                 :                : 
 1840 peter@eisentraut.org      110                 :             53 :     pg_logging_init(argv[0]);
 5603 peter_e@gmx.net           111                 :             53 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_controldata"));
 7681 bruce@momjian.us          112                 :             53 :     progname = get_progname(argv[0]);
                                113                 :                : 
 7907 peter_e@gmx.net           114         [ +  + ]:             53 :     if (argc > 1)
                                115                 :                :     {
                                116   [ +  +  -  + ]:             52 :         if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
                                117                 :                :         {
                                118                 :              1 :             usage(progname);
                                119                 :              1 :             exit(0);
                                120                 :                :         }
                                121   [ +  +  +  + ]:             51 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
                                122                 :                :         {
                                123                 :             19 :             puts("pg_controldata (PostgreSQL) " PG_VERSION);
                                124                 :             19 :             exit(0);
                                125                 :                :         }
                                126                 :                :     }
                                127                 :                : 
 2213                           128         [ +  + ]:             33 :     while ((c = getopt_long(argc, argv, "D:", long_options, NULL)) != -1)
                                129                 :                :     {
 3460 heikki.linnakangas@i      130         [ -  + ]:              1 :         switch (c)
                                131                 :                :         {
 3460 heikki.linnakangas@i      132                 :UBC           0 :             case 'D':
                                133                 :              0 :                 DataDir = optarg;
                                134                 :              0 :                 break;
                                135                 :                : 
 3460 heikki.linnakangas@i      136                 :CBC           1 :             default:
                                137                 :                :                 /* getopt_long already emitted a complaint */
  737 tgl@sss.pgh.pa.us         138                 :              1 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3460 heikki.linnakangas@i      139                 :              1 :                 exit(1);
                                140                 :                :         }
                                141                 :                :     }
                                142                 :                : 
                                143         [ +  - ]:             32 :     if (DataDir == NULL)
                                144                 :                :     {
                                145         [ +  + ]:             32 :         if (optind < argc)
                                146                 :             31 :             DataDir = argv[optind++];
                                147                 :                :         else
                                148                 :              1 :             DataDir = getenv("PGDATA");
                                149                 :                :     }
                                150                 :                : 
                                151                 :                :     /* Complain if any arguments remain */
                                152         [ -  + ]:             32 :     if (optind < argc)
                                153                 :                :     {
 1840 peter@eisentraut.org      154                 :UBC           0 :         pg_log_error("too many command-line arguments (first is \"%s\")",
                                155                 :                :                      argv[optind]);
  737 tgl@sss.pgh.pa.us         156                 :              0 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 3460 heikki.linnakangas@i      157                 :              0 :         exit(1);
                                158                 :                :     }
                                159                 :                : 
 8424 bruce@momjian.us          160         [ +  + ]:CBC          32 :     if (DataDir == NULL)
                                161                 :                :     {
 1840 peter@eisentraut.org      162                 :              1 :         pg_log_error("no data directory specified");
  737 tgl@sss.pgh.pa.us         163                 :              1 :         pg_log_error_hint("Try \"%s --help\" for more information.", progname);
 8451 bruce@momjian.us          164                 :              1 :         exit(1);
                                165                 :                :     }
                                166                 :                : 
                                167                 :                :     /* get a copy of the control file */
 1840 peter@eisentraut.org      168                 :             31 :     ControlFile = get_controlfile(DataDir, &crc_ok);
 2755 peter_e@gmx.net           169         [ +  + ]:             30 :     if (!crc_ok)
                                170                 :                :     {
  230 peter@eisentraut.org      171                 :GNC           1 :         pg_log_warning("calculated CRC checksum does not match value stored in control file");
                                172                 :              1 :         pg_log_warning_detail("Either the control file is corrupt, or it has a different layout than this program "
                                173                 :                :                               "is expecting.  The results below are untrustworthy.");
                                174                 :                :     }
                                175                 :                : 
                                176                 :                :     /* set wal segment size */
 2399 andres@anarazel.de        177                 :CBC          30 :     WalSegSz = ControlFile->xlog_seg_size;
                                178                 :                : 
                                179   [ +  +  +  -  :             30 :     if (!IsValidWalSegSize(WalSegSz))
                                        +  -  -  + ]
                                180                 :                :     {
  230 peter@eisentraut.org      181                 :GNC           1 :         pg_log_warning(ngettext("invalid WAL segment size in control file (%d byte)",
                                182                 :                :                                 "invalid WAL segment size in control file (%d bytes)",
                                183                 :                :                                 WalSegSz),
                                184                 :                :                        WalSegSz);
                                185                 :              1 :         pg_log_warning_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
                                186                 :              1 :         pg_log_warning_detail("The file is corrupt and the results below are untrustworthy.");
                                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                 :                :      */
 2962 mail@joeconway.com        198                 :CBC          30 :     time_tmp = (time_t) ControlFile->time;
 7910 tgl@sss.pgh.pa.us         199                 :             30 :     strftime(pgctime_str, sizeof(pgctime_str), strftime_fmt,
 5901                           200                 :             30 :              localtime(&time_tmp));
 2962 mail@joeconway.com        201                 :             30 :     time_tmp = (time_t) ControlFile->checkPointCopy.time;
 7910 tgl@sss.pgh.pa.us         202                 :             30 :     strftime(ckpttime_str, sizeof(ckpttime_str), strftime_fmt,
 5901                           203                 :             30 :              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                 :                :      */
 2216 peter_e@gmx.net           212         [ +  + ]:             30 :     if (WalSegSz != 0)
                                213                 :                :     {
                                214                 :                :         XLogSegNo   segno;
                                215                 :                : 
                                216                 :             29 :         XLByteToSeg(ControlFile->checkPointCopy.redo, segno, WalSegSz);
                                217                 :             29 :         XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
                                218                 :                :                      segno, WalSegSz);
                                219                 :                :     }
                                220                 :                :     else
                                221                 :              1 :         strcpy(xlogfilename, _("???"));
                                222                 :                : 
 2595 heikki.linnakangas@i      223         [ +  + ]:            990 :     for (i = 0; i < MOCK_AUTH_NONCE_LEN; i++)
                                224                 :            960 :         snprintf(&mock_auth_nonce_str[i * 2], 3, "%02x",
                                225                 :            960 :                  (unsigned char) ControlFile->mock_authentication_nonce[i]);
                                226                 :                : 
 6460 tgl@sss.pgh.pa.us         227                 :             30 :     printf(_("pg_control version number:            %u\n"),
                                228                 :                :            ControlFile->pg_control_version);
                                229                 :             30 :     printf(_("Catalog version number:               %u\n"),
                                230                 :                :            ControlFile->catalog_version_no);
 1774 peter@eisentraut.org      231                 :             30 :     printf(_("Database system identifier:           %llu\n"),
                                232                 :                :            (unsigned long long) ControlFile->system_identifier);
 6460 tgl@sss.pgh.pa.us         233                 :             30 :     printf(_("Database cluster state:               %s\n"),
                                234                 :                :            dbState(ControlFile->state));
                                235                 :             30 :     printf(_("pg_control last modified:             %s\n"),
                                236                 :                :            pgctime_str);
 7907 peter_e@gmx.net           237                 :             30 :     printf(_("Latest checkpoint location:           %X/%X\n"),
                                238                 :                :            LSN_FORMAT_ARGS(ControlFile->checkPoint));
                                239                 :             30 :     printf(_("Latest checkpoint's REDO location:    %X/%X\n"),
                                240                 :                :            LSN_FORMAT_ARGS(ControlFile->checkPointCopy.redo));
 4292 heikki.linnakangas@i      241                 :             30 :     printf(_("Latest checkpoint's REDO WAL file:    %s\n"),
                                242                 :                :            xlogfilename);
 6460 tgl@sss.pgh.pa.us         243                 :             30 :     printf(_("Latest checkpoint's TimeLineID:       %u\n"),
                                244                 :                :            ControlFile->checkPointCopy.ThisTimeLineID);
 4080 heikki.linnakangas@i      245                 :             30 :     printf(_("Latest checkpoint's PrevTimeLineID:   %u\n"),
                                246                 :                :            ControlFile->checkPointCopy.PrevTimeLineID);
 4463 simon@2ndQuadrant.co      247         [ +  + ]:             30 :     printf(_("Latest checkpoint's full_page_writes: %s\n"),
                                248                 :                :            ControlFile->checkPointCopy.fullPageWrites ? _("on") : _("off"));
 2984 mail@joeconway.com        249                 :             30 :     printf(_("Latest checkpoint's NextXID:          %u:%u\n"),
                                250                 :                :            EpochFromFullTransactionId(ControlFile->checkPointCopy.nextXid),
                                251                 :                :            XidFromFullTransactionId(ControlFile->checkPointCopy.nextXid));
 6460 tgl@sss.pgh.pa.us         252                 :             30 :     printf(_("Latest checkpoint's NextOID:          %u\n"),
                                253                 :                :            ControlFile->checkPointCopy.nextOid);
                                254                 :             30 :     printf(_("Latest checkpoint's NextMultiXactId:  %u\n"),
                                255                 :                :            ControlFile->checkPointCopy.nextMulti);
                                256                 :             30 :     printf(_("Latest checkpoint's NextMultiOffset:  %u\n"),
                                257                 :                :            ControlFile->checkPointCopy.nextMultiOffset);
 5340                           258                 :             30 :     printf(_("Latest checkpoint's oldestXID:        %u\n"),
                                259                 :                :            ControlFile->checkPointCopy.oldestXid);
                                260                 :             30 :     printf(_("Latest checkpoint's oldestXID's DB:   %u\n"),
                                261                 :                :            ControlFile->checkPointCopy.oldestXidDB);
 5214 heikki.linnakangas@i      262                 :             30 :     printf(_("Latest checkpoint's oldestActiveXID:  %u\n"),
                                263                 :                :            ControlFile->checkPointCopy.oldestActiveXid);
 4098 alvherre@alvh.no-ip.      264                 :             30 :     printf(_("Latest checkpoint's oldestMultiXid:   %u\n"),
                                265                 :                :            ControlFile->checkPointCopy.oldestMulti);
 4099                           266                 :             30 :     printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
                                267                 :                :            ControlFile->checkPointCopy.oldestMultiDB);
 3030 mail@joeconway.com        268                 :             30 :     printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
                                269                 :                :            ControlFile->checkPointCopy.oldestCommitTsXid);
                                270                 :             30 :     printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
                                271                 :                :            ControlFile->checkPointCopy.newestCommitTsXid);
 6460 tgl@sss.pgh.pa.us         272                 :             30 :     printf(_("Time of latest checkpoint:            %s\n"),
                                273                 :                :            ckpttime_str);
 4080 heikki.linnakangas@i      274                 :             30 :     printf(_("Fake LSN counter for unlogged rels:   %X/%X\n"),
                                275                 :                :            LSN_FORMAT_ARGS(ControlFile->unloggedLSN));
 4047 peter_e@gmx.net           276                 :             30 :     printf(_("Minimum recovery ending location:     %X/%X\n"),
                                277                 :                :            LSN_FORMAT_ARGS(ControlFile->minRecoveryPoint));
 4149 heikki.linnakangas@i      278                 :             30 :     printf(_("Min recovery ending loc's timeline:   %u\n"),
                                279                 :                :            ControlFile->minRecoveryPointTLI);
 5214                           280                 :             30 :     printf(_("Backup start location:                %X/%X\n"),
                                281                 :                :            LSN_FORMAT_ARGS(ControlFile->backupStartPoint));
 4463 simon@2ndQuadrant.co      282                 :             30 :     printf(_("Backup end location:                  %X/%X\n"),
                                283                 :                :            LSN_FORMAT_ARGS(ControlFile->backupEndPoint));
 4624 heikki.linnakangas@i      284         [ -  + ]:             30 :     printf(_("End-of-backup record required:        %s\n"),
                                285                 :                :            ControlFile->backupEndRequired ? _("yes") : _("no"));
 3155 mail@joeconway.com        286                 :             30 :     printf(_("wal_level setting:                    %s\n"),
                                287                 :                :            wal_level_str(ControlFile->wal_level));
                                288         [ +  + ]:             30 :     printf(_("wal_log_hints setting:                %s\n"),
                                289                 :                :            ControlFile->wal_log_hints ? _("on") : _("off"));
                                290                 :             30 :     printf(_("max_connections setting:              %d\n"),
                                291                 :                :            ControlFile->MaxConnections);
                                292                 :             30 :     printf(_("max_worker_processes setting:         %d\n"),
                                293                 :                :            ControlFile->max_worker_processes);
 1888 michael@paquier.xyz       294                 :             30 :     printf(_("max_wal_senders setting:              %d\n"),
                                295                 :                :            ControlFile->max_wal_senders);
 3155 mail@joeconway.com        296                 :             30 :     printf(_("max_prepared_xacts setting:           %d\n"),
                                297                 :                :            ControlFile->max_prepared_xacts);
                                298                 :             30 :     printf(_("max_locks_per_xact setting:           %d\n"),
                                299                 :                :            ControlFile->max_locks_per_xact);
                                300         [ -  + ]:             30 :     printf(_("track_commit_timestamp setting:       %s\n"),
                                301                 :                :            ControlFile->track_commit_timestamp ? _("on") : _("off"));
 6460 tgl@sss.pgh.pa.us         302                 :             30 :     printf(_("Maximum data alignment:               %u\n"),
                                303                 :                :            ControlFile->maxAlign);
                                304                 :                :     /* we don't print floatFormat since can't say much useful about it */
                                305                 :             30 :     printf(_("Database block size:                  %u\n"),
                                306                 :                :            ControlFile->blcksz);
                                307                 :             30 :     printf(_("Blocks per segment of large relation: %u\n"),
                                308                 :                :            ControlFile->relseg_size);
                                309                 :             30 :     printf(_("WAL block size:                       %u\n"),
                                310                 :                :            ControlFile->xlog_blcksz);
                                311                 :             30 :     printf(_("Bytes per WAL segment:                %u\n"),
                                312                 :                :            ControlFile->xlog_seg_size);
                                313                 :             30 :     printf(_("Maximum length of identifiers:        %u\n"),
                                314                 :                :            ControlFile->nameDataLen);
                                315                 :             30 :     printf(_("Maximum columns in an index:          %u\n"),
                                316                 :                :            ControlFile->indexMaxKeys);
 6221                           317                 :             30 :     printf(_("Maximum size of a TOAST chunk:        %u\n"),
                                318                 :                :            ControlFile->toast_max_chunk_size);
 3601                           319                 :             30 :     printf(_("Size of a large-object chunk:         %u\n"),
                                320                 :                :            ControlFile->loblksize);
                                321                 :                :     /* This is no longer configurable, but users may still expect to see it: */
 7907 peter_e@gmx.net           322                 :             30 :     printf(_("Date/time type storage:               %s\n"),
                                323                 :                :            _("64-bit integers"));
 5837 tgl@sss.pgh.pa.us         324         [ +  + ]:             30 :     printf(_("Float8 argument passing:              %s\n"),
                                325                 :                :            (ControlFile->float8ByVal ? _("by value") : _("by reference")));
 4002 simon@2ndQuadrant.co      326                 :             30 :     printf(_("Data page checksum version:           %u\n"),
                                327                 :                :            ControlFile->data_checksum_version);
 2595 heikki.linnakangas@i      328                 :             30 :     printf(_("Mock authentication nonce:            %s\n"),
                                329                 :                :            mock_auth_nonce_str);
 7907 peter_e@gmx.net           330                 :             30 :     return 0;
                                331                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622