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"
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
33 {
34  Datum values[4];
35  bool nulls[4];
36  TupleDesc tupdesc;
37  HeapTuple htup;
39  bool crc_ok;
40 
41  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
42  elog(ERROR, "return type must be a row type");
43 
44  /* read the control file */
45  LWLockAcquire(ControlFileLock, LW_SHARED);
47  LWLockRelease(ControlFileLock);
48  if (!crc_ok)
49  ereport(ERROR,
50  (errmsg("calculated CRC checksum does not match value stored in file")));
51 
53  nulls[0] = false;
54 
56  nulls[1] = false;
57 
59  nulls[2] = false;
60 
62  nulls[3] = false;
63 
64  htup = heap_form_tuple(tupdesc, values, nulls);
65 
67 }
68 
69 Datum
71 {
72  Datum values[18];
73  bool nulls[18];
74  TupleDesc tupdesc;
75  HeapTuple htup;
77  XLogSegNo segno;
78  char xlogfilename[MAXFNAMELEN];
79  bool crc_ok;
80 
81  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
82  elog(ERROR, "return type must be a row type");
83 
84  /* Read the control file. */
85  LWLockAcquire(ControlFileLock, LW_SHARED);
87  LWLockRelease(ControlFileLock);
88  if (!crc_ok)
89  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  */
98  segno, wal_segment_size);
99 
100  /* Populate the values and null arrays */
102  nulls[0] = false;
103 
105  nulls[1] = false;
106 
107  values[2] = CStringGetTextDatum(xlogfilename);
108  nulls[2] = false;
109 
111  nulls[3] = false;
112 
114  nulls[4] = false;
115 
117  nulls[5] = false;
118 
119  values[6] = CStringGetTextDatum(psprintf("%u:%u",
122  nulls[6] = false;
123 
125  nulls[7] = false;
126 
128  nulls[8] = false;
129 
131  nulls[9] = false;
132 
134  nulls[10] = false;
135 
137  nulls[11] = false;
138 
140  nulls[12] = false;
141 
143  nulls[13] = false;
144 
146  nulls[14] = false;
147 
149  nulls[15] = false;
150 
152  nulls[16] = false;
153 
155  nulls[17] = false;
156 
157  htup = heap_form_tuple(tupdesc, values, nulls);
158 
160 }
161 
162 Datum
164 {
165  Datum values[5];
166  bool nulls[5];
167  TupleDesc tupdesc;
168  HeapTuple htup;
170  bool crc_ok;
171 
172  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
173  elog(ERROR, "return type must be a row type");
174 
175  /* read the control file */
176  LWLockAcquire(ControlFileLock, LW_SHARED);
177  ControlFile = get_controlfile(DataDir, &crc_ok);
178  LWLockRelease(ControlFileLock);
179  if (!crc_ok)
180  ereport(ERROR,
181  (errmsg("calculated CRC checksum does not match value stored in file")));
182 
184  nulls[0] = false;
185 
187  nulls[1] = false;
188 
190  nulls[2] = false;
191 
193  nulls[3] = false;
194 
196  nulls[4] = false;
197 
198  htup = heap_form_tuple(tupdesc, values, nulls);
199 
201 }
202 
203 Datum
205 {
206  Datum values[11];
207  bool nulls[11];
208  TupleDesc tupdesc;
209  HeapTuple htup;
211  bool crc_ok;
212 
213  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
214  elog(ERROR, "return type must be a row type");
215 
216  /* read the control file */
217  LWLockAcquire(ControlFileLock, LW_SHARED);
218  ControlFile = get_controlfile(DataDir, &crc_ok);
219  LWLockRelease(ControlFileLock);
220  if (!crc_ok)
221  ereport(ERROR,
222  (errmsg("calculated CRC checksum does not match value stored in file")));
223 
225  nulls[0] = false;
226 
228  nulls[1] = false;
229 
231  nulls[2] = false;
232 
234  nulls[3] = false;
235 
237  nulls[4] = false;
238 
240  nulls[5] = false;
241 
243  nulls[6] = false;
244 
246  nulls[7] = false;
247 
249  nulls[8] = false;
250 
252  nulls[9] = false;
253 
255  nulls[10] = false;
256 
257  htup = heap_form_tuple(tupdesc, values, nulls);
258 
260 }
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1811
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:152
#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:1070
#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:68
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
@ LW_SHARED
Definition: lwlock.h:115
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:143
static ControlFileData * ControlFile
Definition: xlog.c:569
#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