LCOV - code coverage report
Current view: top level - src/backend/utils/misc - pg_controldata.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 94.7 % 151 143
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 16.7 % 48 8

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pg_controldata.c
       4                 :             :  *
       5                 :             :  * Routines to expose the contents of the control data file via
       6                 :             :  * a set of SQL functions.
       7                 :             :  *
       8                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       9                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      10                 :             :  *
      11                 :             :  * IDENTIFICATION
      12                 :             :  *        src/backend/utils/misc/pg_controldata.c
      13                 :             :  *-------------------------------------------------------------------------
      14                 :             :  */
      15                 :             : 
      16                 :             : #include "postgres.h"
      17                 :             : 
      18                 :             : #include "access/htup_details.h"
      19                 :             : #include "access/transam.h"
      20                 :             : #include "access/xlog.h"
      21                 :             : #include "access/xlog_internal.h"
      22                 :             : #include "catalog/pg_control.h"
      23                 :             : #include "common/controldata_utils.h"
      24                 :             : #include "funcapi.h"
      25                 :             : #include "miscadmin.h"
      26                 :             : #include "storage/lwlock.h"
      27                 :             : #include "utils/builtins.h"
      28                 :             : #include "utils/pg_lsn.h"
      29                 :             : #include "utils/timestamp.h"
      30                 :             : 
      31                 :             : Datum
      32                 :           1 : pg_control_system(PG_FUNCTION_ARGS)
      33                 :             : {
      34                 :           1 :         Datum           values[4];
      35                 :           1 :         bool            nulls[4];
      36                 :           1 :         TupleDesc       tupdesc;
      37                 :           1 :         HeapTuple       htup;
      38                 :           1 :         ControlFileData *ControlFile;
      39                 :           1 :         bool            crc_ok;
      40                 :             : 
      41         [ +  - ]:           1 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
      42   [ #  #  #  # ]:           0 :                 elog(ERROR, "return type must be a row type");
      43                 :             : 
      44                 :             :         /* read the control file */
      45                 :           1 :         LWLockAcquire(ControlFileLock, LW_SHARED);
      46                 :           1 :         ControlFile = get_controlfile(DataDir, &crc_ok);
      47                 :           1 :         LWLockRelease(ControlFileLock);
      48         [ +  - ]:           1 :         if (!crc_ok)
      49   [ #  #  #  # ]:           0 :                 ereport(ERROR,
      50                 :             :                                 (errmsg("calculated CRC checksum does not match value stored in file")));
      51                 :             : 
      52                 :           1 :         values[0] = Int32GetDatum(ControlFile->pg_control_version);
      53                 :           1 :         nulls[0] = false;
      54                 :             : 
      55                 :           1 :         values[1] = Int32GetDatum(ControlFile->catalog_version_no);
      56                 :           1 :         nulls[1] = false;
      57                 :             : 
      58                 :           1 :         values[2] = Int64GetDatum(ControlFile->system_identifier);
      59                 :           1 :         nulls[2] = false;
      60                 :             : 
      61                 :           1 :         values[3] = TimestampTzGetDatum(time_t_to_timestamptz(ControlFile->time));
      62                 :           1 :         nulls[3] = false;
      63                 :             : 
      64                 :           1 :         htup = heap_form_tuple(tupdesc, values, nulls);
      65                 :             : 
      66                 :           2 :         PG_RETURN_DATUM(HeapTupleGetDatum(htup));
      67                 :           1 : }
      68                 :             : 
      69                 :             : Datum
      70                 :           1 : pg_control_checkpoint(PG_FUNCTION_ARGS)
      71                 :             : {
      72                 :           1 :         Datum           values[18];
      73                 :           1 :         bool            nulls[18];
      74                 :           1 :         TupleDesc       tupdesc;
      75                 :           1 :         HeapTuple       htup;
      76                 :           1 :         ControlFileData *ControlFile;
      77                 :           1 :         XLogSegNo       segno;
      78                 :           1 :         char            xlogfilename[MAXFNAMELEN];
      79                 :           1 :         bool            crc_ok;
      80                 :             : 
      81         [ +  - ]:           1 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
      82   [ #  #  #  # ]:           0 :                 elog(ERROR, "return type must be a row type");
      83                 :             : 
      84                 :             :         /* Read the control file. */
      85                 :           1 :         LWLockAcquire(ControlFileLock, LW_SHARED);
      86                 :           1 :         ControlFile = get_controlfile(DataDir, &crc_ok);
      87                 :           1 :         LWLockRelease(ControlFileLock);
      88         [ +  - ]:           1 :         if (!crc_ok)
      89   [ #  #  #  # ]:           0 :                 ereport(ERROR,
      90                 :             :                                 (errmsg("calculated CRC checksum does not match value stored in file")));
      91                 :             : 
      92                 :             :         /*
      93                 :             :          * Calculate name of the WAL file containing the latest checkpoint's REDO
      94                 :             :          * start point.
      95                 :             :          */
      96                 :           1 :         XLByteToSeg(ControlFile->checkPointCopy.redo, segno, wal_segment_size);
      97                 :           2 :         XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
      98                 :           1 :                                  segno, wal_segment_size);
      99                 :             : 
     100                 :             :         /* Populate the values and null arrays */
     101                 :           1 :         values[0] = LSNGetDatum(ControlFile->checkPoint);
     102                 :           1 :         nulls[0] = false;
     103                 :             : 
     104                 :           1 :         values[1] = LSNGetDatum(ControlFile->checkPointCopy.redo);
     105                 :           1 :         nulls[1] = false;
     106                 :             : 
     107                 :           1 :         values[2] = CStringGetTextDatum(xlogfilename);
     108                 :           1 :         nulls[2] = false;
     109                 :             : 
     110                 :           1 :         values[3] = Int32GetDatum(ControlFile->checkPointCopy.ThisTimeLineID);
     111                 :           1 :         nulls[3] = false;
     112                 :             : 
     113                 :           1 :         values[4] = Int32GetDatum(ControlFile->checkPointCopy.PrevTimeLineID);
     114                 :           1 :         nulls[4] = false;
     115                 :             : 
     116                 :           1 :         values[5] = BoolGetDatum(ControlFile->checkPointCopy.fullPageWrites);
     117                 :           1 :         nulls[5] = false;
     118                 :             : 
     119                 :           1 :         values[6] = CStringGetTextDatum(psprintf("%u:%u",
     120                 :             :                                                                                          EpochFromFullTransactionId(ControlFile->checkPointCopy.nextXid),
     121                 :             :                                                                                          XidFromFullTransactionId(ControlFile->checkPointCopy.nextXid)));
     122                 :           1 :         nulls[6] = false;
     123                 :             : 
     124                 :           1 :         values[7] = ObjectIdGetDatum(ControlFile->checkPointCopy.nextOid);
     125                 :           1 :         nulls[7] = false;
     126                 :             : 
     127                 :           1 :         values[8] = TransactionIdGetDatum(ControlFile->checkPointCopy.nextMulti);
     128                 :           1 :         nulls[8] = false;
     129                 :             : 
     130                 :           1 :         values[9] = TransactionIdGetDatum(ControlFile->checkPointCopy.nextMultiOffset);
     131                 :           1 :         nulls[9] = false;
     132                 :             : 
     133                 :           1 :         values[10] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestXid);
     134                 :           1 :         nulls[10] = false;
     135                 :             : 
     136                 :           1 :         values[11] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestXidDB);
     137                 :           1 :         nulls[11] = false;
     138                 :             : 
     139                 :           1 :         values[12] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestActiveXid);
     140                 :           1 :         nulls[12] = false;
     141                 :             : 
     142                 :           1 :         values[13] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestMulti);
     143                 :           1 :         nulls[13] = false;
     144                 :             : 
     145                 :           1 :         values[14] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestMultiDB);
     146                 :           1 :         nulls[14] = false;
     147                 :             : 
     148                 :           1 :         values[15] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestCommitTsXid);
     149                 :           1 :         nulls[15] = false;
     150                 :             : 
     151                 :           1 :         values[16] = TransactionIdGetDatum(ControlFile->checkPointCopy.newestCommitTsXid);
     152                 :           1 :         nulls[16] = false;
     153                 :             : 
     154                 :           1 :         values[17] = TimestampTzGetDatum(time_t_to_timestamptz(ControlFile->checkPointCopy.time));
     155                 :           1 :         nulls[17] = false;
     156                 :             : 
     157                 :           1 :         htup = heap_form_tuple(tupdesc, values, nulls);
     158                 :             : 
     159                 :           2 :         PG_RETURN_DATUM(HeapTupleGetDatum(htup));
     160                 :           1 : }
     161                 :             : 
     162                 :             : Datum
     163                 :           1 : pg_control_recovery(PG_FUNCTION_ARGS)
     164                 :             : {
     165                 :           1 :         Datum           values[5];
     166                 :           1 :         bool            nulls[5];
     167                 :           1 :         TupleDesc       tupdesc;
     168                 :           1 :         HeapTuple       htup;
     169                 :           1 :         ControlFileData *ControlFile;
     170                 :           1 :         bool            crc_ok;
     171                 :             : 
     172         [ +  - ]:           1 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     173   [ #  #  #  # ]:           0 :                 elog(ERROR, "return type must be a row type");
     174                 :             : 
     175                 :             :         /* read the control file */
     176                 :           1 :         LWLockAcquire(ControlFileLock, LW_SHARED);
     177                 :           1 :         ControlFile = get_controlfile(DataDir, &crc_ok);
     178                 :           1 :         LWLockRelease(ControlFileLock);
     179         [ +  - ]:           1 :         if (!crc_ok)
     180   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     181                 :             :                                 (errmsg("calculated CRC checksum does not match value stored in file")));
     182                 :             : 
     183                 :           1 :         values[0] = LSNGetDatum(ControlFile->minRecoveryPoint);
     184                 :           1 :         nulls[0] = false;
     185                 :             : 
     186                 :           1 :         values[1] = Int32GetDatum(ControlFile->minRecoveryPointTLI);
     187                 :           1 :         nulls[1] = false;
     188                 :             : 
     189                 :           1 :         values[2] = LSNGetDatum(ControlFile->backupStartPoint);
     190                 :           1 :         nulls[2] = false;
     191                 :             : 
     192                 :           1 :         values[3] = LSNGetDatum(ControlFile->backupEndPoint);
     193                 :           1 :         nulls[3] = false;
     194                 :             : 
     195                 :           1 :         values[4] = BoolGetDatum(ControlFile->backupEndRequired);
     196                 :           1 :         nulls[4] = false;
     197                 :             : 
     198                 :           1 :         htup = heap_form_tuple(tupdesc, values, nulls);
     199                 :             : 
     200                 :           2 :         PG_RETURN_DATUM(HeapTupleGetDatum(htup));
     201                 :           1 : }
     202                 :             : 
     203                 :             : Datum
     204                 :           1 : pg_control_init(PG_FUNCTION_ARGS)
     205                 :             : {
     206                 :           1 :         Datum           values[12];
     207                 :           1 :         bool            nulls[12];
     208                 :           1 :         TupleDesc       tupdesc;
     209                 :           1 :         HeapTuple       htup;
     210                 :           1 :         ControlFileData *ControlFile;
     211                 :           1 :         bool            crc_ok;
     212                 :             : 
     213         [ +  - ]:           1 :         if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
     214   [ #  #  #  # ]:           0 :                 elog(ERROR, "return type must be a row type");
     215                 :             : 
     216                 :             :         /* read the control file */
     217                 :           1 :         LWLockAcquire(ControlFileLock, LW_SHARED);
     218                 :           1 :         ControlFile = get_controlfile(DataDir, &crc_ok);
     219                 :           1 :         LWLockRelease(ControlFileLock);
     220         [ +  - ]:           1 :         if (!crc_ok)
     221   [ #  #  #  # ]:           0 :                 ereport(ERROR,
     222                 :             :                                 (errmsg("calculated CRC checksum does not match value stored in file")));
     223                 :             : 
     224                 :           1 :         values[0] = Int32GetDatum(ControlFile->maxAlign);
     225                 :           1 :         nulls[0] = false;
     226                 :             : 
     227                 :           1 :         values[1] = Int32GetDatum(ControlFile->blcksz);
     228                 :           1 :         nulls[1] = false;
     229                 :             : 
     230                 :           1 :         values[2] = Int32GetDatum(ControlFile->relseg_size);
     231                 :           1 :         nulls[2] = false;
     232                 :             : 
     233                 :           1 :         values[3] = Int32GetDatum(ControlFile->xlog_blcksz);
     234                 :           1 :         nulls[3] = false;
     235                 :             : 
     236                 :           1 :         values[4] = Int32GetDatum(ControlFile->xlog_seg_size);
     237                 :           1 :         nulls[4] = false;
     238                 :             : 
     239                 :           1 :         values[5] = Int32GetDatum(ControlFile->nameDataLen);
     240                 :           1 :         nulls[5] = false;
     241                 :             : 
     242                 :           1 :         values[6] = Int32GetDatum(ControlFile->indexMaxKeys);
     243                 :           1 :         nulls[6] = false;
     244                 :             : 
     245                 :           1 :         values[7] = Int32GetDatum(ControlFile->toast_max_chunk_size);
     246                 :           1 :         nulls[7] = false;
     247                 :             : 
     248                 :           1 :         values[8] = Int32GetDatum(ControlFile->loblksize);
     249                 :           1 :         nulls[8] = false;
     250                 :             : 
     251                 :           1 :         values[9] = BoolGetDatum(ControlFile->float8ByVal);
     252                 :           1 :         nulls[9] = false;
     253                 :             : 
     254                 :           1 :         values[10] = Int32GetDatum(ControlFile->data_checksum_version);
     255                 :           1 :         nulls[10] = false;
     256                 :             : 
     257                 :           1 :         values[11] = BoolGetDatum(ControlFile->default_char_signedness);
     258                 :           1 :         nulls[11] = false;
     259                 :             : 
     260                 :           1 :         htup = heap_form_tuple(tupdesc, values, nulls);
     261                 :             : 
     262                 :           2 :         PG_RETURN_DATUM(HeapTupleGetDatum(htup));
     263                 :           1 : }
        

Generated by: LCOV version 2.3.2-1