PostgreSQL Source Code  git master
pg_controldata.c
Go to the documentation of this file.
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-2024, 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 "catalog/pg_type.h"
25 #include "funcapi.h"
26 #include "miscadmin.h"
27 #include "storage/lwlock.h"
28 #include "utils/builtins.h"
29 #include "utils/pg_lsn.h"
30 #include "utils/timestamp.h"
31 
32 Datum
34 {
35  Datum values[4];
36  bool nulls[4];
37  TupleDesc tupdesc;
38  HeapTuple htup;
40  bool crc_ok;
41 
42  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
43  elog(ERROR, "return type must be a row type");
44 
45  /* read the control file */
46  LWLockAcquire(ControlFileLock, LW_SHARED);
48  LWLockRelease(ControlFileLock);
49  if (!crc_ok)
50  ereport(ERROR,
51  (errmsg("calculated CRC checksum does not match value stored in file")));
52 
54  nulls[0] = false;
55 
57  nulls[1] = false;
58 
60  nulls[2] = false;
61 
63  nulls[3] = false;
64 
65  htup = heap_form_tuple(tupdesc, values, nulls);
66 
68 }
69 
70 Datum
72 {
73  Datum values[18];
74  bool nulls[18];
75  TupleDesc tupdesc;
76  HeapTuple htup;
78  XLogSegNo segno;
79  char xlogfilename[MAXFNAMELEN];
80  bool crc_ok;
81 
82  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
83  elog(ERROR, "return type must be a row type");
84 
85  /* Read the control file. */
86  LWLockAcquire(ControlFileLock, LW_SHARED);
88  LWLockRelease(ControlFileLock);
89  if (!crc_ok)
90  ereport(ERROR,
91  (errmsg("calculated CRC checksum does not match value stored in file")));
92 
93  /*
94  * Calculate name of the WAL file containing the latest checkpoint's REDO
95  * start point.
96  */
99  segno, wal_segment_size);
100 
101  /* Populate the values and null arrays */
103  nulls[0] = false;
104 
106  nulls[1] = false;
107 
108  values[2] = CStringGetTextDatum(xlogfilename);
109  nulls[2] = false;
110 
112  nulls[3] = false;
113 
115  nulls[4] = false;
116 
118  nulls[5] = false;
119 
120  values[6] = CStringGetTextDatum(psprintf("%u:%u",
123  nulls[6] = false;
124 
126  nulls[7] = false;
127 
129  nulls[8] = false;
130 
132  nulls[9] = false;
133 
135  nulls[10] = false;
136 
138  nulls[11] = false;
139 
141  nulls[12] = false;
142 
144  nulls[13] = false;
145 
147  nulls[14] = false;
148 
150  nulls[15] = false;
151 
153  nulls[16] = false;
154 
156  nulls[17] = false;
157 
158  htup = heap_form_tuple(tupdesc, values, nulls);
159 
161 }
162 
163 Datum
165 {
166  Datum values[5];
167  bool nulls[5];
168  TupleDesc tupdesc;
169  HeapTuple htup;
171  bool crc_ok;
172 
173  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
174  elog(ERROR, "return type must be a row type");
175 
176  /* read the control file */
177  LWLockAcquire(ControlFileLock, LW_SHARED);
178  ControlFile = get_controlfile(DataDir, &crc_ok);
179  LWLockRelease(ControlFileLock);
180  if (!crc_ok)
181  ereport(ERROR,
182  (errmsg("calculated CRC checksum does not match value stored in file")));
183 
185  nulls[0] = false;
186 
188  nulls[1] = false;
189 
191  nulls[2] = false;
192 
194  nulls[3] = false;
195 
197  nulls[4] = false;
198 
199  htup = heap_form_tuple(tupdesc, values, nulls);
200 
202 }
203 
204 Datum
206 {
207  Datum values[11];
208  bool nulls[11];
209  TupleDesc tupdesc;
210  HeapTuple htup;
212  bool crc_ok;
213 
214  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
215  elog(ERROR, "return type must be a row type");
216 
217  /* read the control file */
218  LWLockAcquire(ControlFileLock, LW_SHARED);
219  ControlFile = get_controlfile(DataDir, &crc_ok);
220  LWLockRelease(ControlFileLock);
221  if (!crc_ok)
222  ereport(ERROR,
223  (errmsg("calculated CRC checksum does not match value stored in file")));
224 
226  nulls[0] = false;
227 
229  nulls[1] = false;
230 
232  nulls[2] = false;
233 
235  nulls[3] = false;
236 
238  nulls[4] = false;
239 
241  nulls[5] = false;
242 
244  nulls[6] = false;
245 
247  nulls[7] = false;
248 
250  nulls[8] = false;
251 
253  nulls[9] = false;
254 
256  nulls[10] = false;
257 
258  htup = heap_form_tuple(tupdesc, values, nulls);
259 
261 }
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1812
Datum pg_control_system(PG_FUNCTION_ARGS)
Datum pg_control_init(PG_FUNCTION_ARGS)
Datum pg_control_recovery(PG_FUNCTION_ARGS)
Datum pg_control_checkpoint(PG_FUNCTION_ARGS)
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define CStringGetTextDatum(s)
Definition: builtins.h:97
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
int errmsg(const char *fmt,...)
Definition: elog.c:1073
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
char * DataDir
Definition: globals.c:67
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1175
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1788
@ LW_SHARED
Definition: lwlock.h:117
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
static Datum TransactionIdGetDatum(TransactionId X)
Definition: postgres.h:272
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
Oid oldestMultiDB
Definition: pg_control.h:50
MultiXactId oldestMulti
Definition: pg_control.h:49
MultiXactOffset nextMultiOffset
Definition: pg_control.h:46
TransactionId newestCommitTsXid
Definition: pg_control.h:54
TransactionId oldestXid
Definition: pg_control.h:47
TimeLineID PrevTimeLineID
Definition: pg_control.h:40
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
Oid nextOid
Definition: pg_control.h:44
TransactionId oldestActiveXid
Definition: pg_control.h:63
bool fullPageWrites
Definition: pg_control.h:42
MultiXactId nextMulti
Definition: pg_control.h:45
FullTransactionId nextXid
Definition: pg_control.h:43
TransactionId oldestCommitTsXid
Definition: pg_control.h:52
pg_time_t time
Definition: pg_control.h:51
XLogRecPtr redo
Definition: pg_control.h:37
Oid oldestXidDB
Definition: pg_control.h:48
uint32 pg_control_version
Definition: pg_control.h:124
uint32 xlog_seg_size
Definition: pg_control.h:210
XLogRecPtr backupStartPoint
Definition: pg_control.h:169
bool backupEndRequired
Definition: pg_control.h:171
uint32 nameDataLen
Definition: pg_control.h:212
CheckPoint checkPointCopy
Definition: pg_control.h:134
XLogRecPtr backupEndPoint
Definition: pg_control.h:170
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:167
uint32 data_checksum_version
Definition: pg_control.h:221
uint32 indexMaxKeys
Definition: pg_control.h:213
uint32 relseg_size
Definition: pg_control.h:207
pg_time_t time
Definition: pg_control.h:131
XLogRecPtr checkPoint
Definition: pg_control.h:132
uint64 system_identifier
Definition: pg_control.h:109
uint32 catalog_version_no
Definition: pg_control.h:125
uint32 xlog_blcksz
Definition: pg_control.h:209
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:168
uint32 loblksize
Definition: pg_control.h:216
uint32 toast_max_chunk_size
Definition: pg_control.h:215
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
int wal_segment_size
Definition: xlog.c:148
static ControlFileData * ControlFile
Definition: xlog.c:581
#define MAXFNAMELEN
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
uint64 XLogSegNo
Definition: xlogdefs.h:48