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-2023, 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 "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 */
46  if (!crc_ok)
47  ereport(ERROR,
48  (errmsg("calculated CRC checksum does not match value stored in file")));
49 
51  nulls[0] = false;
52 
54  nulls[1] = false;
55 
57  nulls[2] = false;
58 
60  nulls[3] = false;
61 
62  htup = heap_form_tuple(tupdesc, values, nulls);
63 
65 }
66 
67 Datum
69 {
70  Datum values[18];
71  bool nulls[18];
72  TupleDesc tupdesc;
73  HeapTuple htup;
75  XLogSegNo segno;
76  char xlogfilename[MAXFNAMELEN];
77  bool crc_ok;
78 
79  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
80  elog(ERROR, "return type must be a row type");
81 
82  /* Read the control file. */
84  if (!crc_ok)
85  ereport(ERROR,
86  (errmsg("calculated CRC checksum does not match value stored in file")));
87 
88  /*
89  * Calculate name of the WAL file containing the latest checkpoint's REDO
90  * start point.
91  */
94  segno, wal_segment_size);
95 
96  /* Populate the values and null arrays */
98  nulls[0] = false;
99 
101  nulls[1] = false;
102 
103  values[2] = CStringGetTextDatum(xlogfilename);
104  nulls[2] = false;
105 
107  nulls[3] = false;
108 
110  nulls[4] = false;
111 
113  nulls[5] = false;
114 
115  values[6] = CStringGetTextDatum(psprintf("%u:%u",
118  nulls[6] = false;
119 
121  nulls[7] = false;
122 
124  nulls[8] = false;
125 
127  nulls[9] = false;
128 
130  nulls[10] = false;
131 
133  nulls[11] = false;
134 
136  nulls[12] = false;
137 
139  nulls[13] = false;
140 
142  nulls[14] = false;
143 
145  nulls[15] = false;
146 
148  nulls[16] = false;
149 
151  nulls[17] = false;
152 
153  htup = heap_form_tuple(tupdesc, values, nulls);
154 
156 }
157 
158 Datum
160 {
161  Datum values[5];
162  bool nulls[5];
163  TupleDesc tupdesc;
164  HeapTuple htup;
166  bool crc_ok;
167 
168  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
169  elog(ERROR, "return type must be a row type");
170 
171  /* read the control file */
172  ControlFile = get_controlfile(DataDir, &crc_ok);
173  if (!crc_ok)
174  ereport(ERROR,
175  (errmsg("calculated CRC checksum does not match value stored in file")));
176 
178  nulls[0] = false;
179 
181  nulls[1] = false;
182 
184  nulls[2] = false;
185 
187  nulls[3] = false;
188 
190  nulls[4] = false;
191 
192  htup = heap_form_tuple(tupdesc, values, nulls);
193 
195 }
196 
197 Datum
199 {
200  Datum values[11];
201  bool nulls[11];
202  TupleDesc tupdesc;
203  HeapTuple htup;
205  bool crc_ok;
206 
207  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
208  elog(ERROR, "return type must be a row type");
209 
210  /* read the control file */
211  ControlFile = get_controlfile(DataDir, &crc_ok);
212  if (!crc_ok)
213  ereport(ERROR,
214  (errmsg("calculated CRC checksum does not match value stored in file")));
215 
217  nulls[0] = false;
218 
220  nulls[1] = false;
221 
223  nulls[2] = false;
224 
226  nulls[3] = false;
227 
229  nulls[4] = false;
230 
232  nulls[5] = false;
233 
235  nulls[6] = false;
236 
238  nulls[7] = false;
239 
241  nulls[8] = false;
242 
244  nulls[9] = false;
245 
247  nulls[10] = false;
248 
249  htup = heap_form_tuple(tupdesc, values, nulls);
250 
252 }
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1740
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:94
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1790
#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:66
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1108
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:123
uint32 xlog_seg_size
Definition: pg_control.h:209
XLogRecPtr backupStartPoint
Definition: pg_control.h:168
bool backupEndRequired
Definition: pg_control.h:170
uint32 nameDataLen
Definition: pg_control.h:211
CheckPoint checkPointCopy
Definition: pg_control.h:133
XLogRecPtr backupEndPoint
Definition: pg_control.h:169
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:166
uint32 data_checksum_version
Definition: pg_control.h:220
uint32 indexMaxKeys
Definition: pg_control.h:212
uint32 relseg_size
Definition: pg_control.h:206
pg_time_t time
Definition: pg_control.h:130
XLogRecPtr checkPoint
Definition: pg_control.h:131
uint64 system_identifier
Definition: pg_control.h:108
uint32 catalog_version_no
Definition: pg_control.h:124
uint32 xlog_blcksz
Definition: pg_control.h:208
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:167
uint32 loblksize
Definition: pg_control.h:215
uint32 toast_max_chunk_size
Definition: pg_control.h:214
#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:146
static ControlFileData * ControlFile
Definition: xlog.c:570
#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