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-2018, 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/xlog_internal.h"
20 #include "access/xlog.h"
21 #include "catalog/pg_control.h"
22 #include "catalog/pg_type.h"
24 #include "funcapi.h"
25 #include "miscadmin.h"
26 #include "utils/builtins.h"
27 #include "utils/pg_lsn.h"
28 #include "utils/timestamp.h"
29 
30 Datum
32 {
33  Datum values[4];
34  bool nulls[4];
35  TupleDesc tupdesc;
36  HeapTuple htup;
38  bool crc_ok;
39 
40  /*
41  * Construct a tuple descriptor for the result row. This must match this
42  * function's pg_proc entry!
43  */
44  tupdesc = CreateTemplateTupleDesc(4, false);
45  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "pg_control_version",
46  INT4OID, -1, 0);
47  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "catalog_version_no",
48  INT4OID, -1, 0);
49  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "system_identifier",
50  INT8OID, -1, 0);
51  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "pg_control_last_modified",
52  TIMESTAMPTZOID, -1, 0);
53  tupdesc = BlessTupleDesc(tupdesc);
54 
55  /* read the control file */
56  ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
57  if (!crc_ok)
58  ereport(ERROR,
59  (errmsg("calculated CRC checksum does not match value stored in file")));
60 
61  values[0] = Int32GetDatum(ControlFile->pg_control_version);
62  nulls[0] = false;
63 
64  values[1] = Int32GetDatum(ControlFile->catalog_version_no);
65  nulls[1] = false;
66 
67  values[2] = Int64GetDatum(ControlFile->system_identifier);
68  nulls[2] = false;
69 
70  values[3] = TimestampTzGetDatum(time_t_to_timestamptz(ControlFile->time));
71  nulls[3] = false;
72 
73  htup = heap_form_tuple(tupdesc, values, nulls);
74 
76 }
77 
78 Datum
80 {
81  Datum values[19];
82  bool nulls[19];
83  TupleDesc tupdesc;
84  HeapTuple htup;
86  XLogSegNo segno;
87  char xlogfilename[MAXFNAMELEN];
88  bool crc_ok;
89 
90  /*
91  * Construct a tuple descriptor for the result row. This must match this
92  * function's pg_proc entry!
93  */
94  tupdesc = CreateTemplateTupleDesc(18, false);
95  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "checkpoint_lsn",
96  LSNOID, -1, 0);
97  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "redo_lsn",
98  LSNOID, -1, 0);
99  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "redo_wal_file",
100  TEXTOID, -1, 0);
101  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "timeline_id",
102  INT4OID, -1, 0);
103  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "prev_timeline_id",
104  INT4OID, -1, 0);
105  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "full_page_writes",
106  BOOLOID, -1, 0);
107  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "next_xid",
108  TEXTOID, -1, 0);
109  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "next_oid",
110  OIDOID, -1, 0);
111  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "next_multixact_id",
112  XIDOID, -1, 0);
113  TupleDescInitEntry(tupdesc, (AttrNumber) 10, "next_multi_offset",
114  XIDOID, -1, 0);
115  TupleDescInitEntry(tupdesc, (AttrNumber) 11, "oldest_xid",
116  XIDOID, -1, 0);
117  TupleDescInitEntry(tupdesc, (AttrNumber) 12, "oldest_xid_dbid",
118  OIDOID, -1, 0);
119  TupleDescInitEntry(tupdesc, (AttrNumber) 13, "oldest_active_xid",
120  XIDOID, -1, 0);
121  TupleDescInitEntry(tupdesc, (AttrNumber) 14, "oldest_multi_xid",
122  XIDOID, -1, 0);
123  TupleDescInitEntry(tupdesc, (AttrNumber) 15, "oldest_multi_dbid",
124  OIDOID, -1, 0);
125  TupleDescInitEntry(tupdesc, (AttrNumber) 16, "oldest_commit_ts_xid",
126  XIDOID, -1, 0);
127  TupleDescInitEntry(tupdesc, (AttrNumber) 17, "newest_commit_ts_xid",
128  XIDOID, -1, 0);
129  TupleDescInitEntry(tupdesc, (AttrNumber) 18, "checkpoint_time",
130  TIMESTAMPTZOID, -1, 0);
131  tupdesc = BlessTupleDesc(tupdesc);
132 
133  /* Read the control file. */
134  ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
135  if (!crc_ok)
136  ereport(ERROR,
137  (errmsg("calculated CRC checksum does not match value stored in file")));
138 
139  /*
140  * Calculate name of the WAL file containing the latest checkpoint's REDO
141  * start point.
142  */
143  XLByteToSeg(ControlFile->checkPointCopy.redo, segno, wal_segment_size);
144  XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
145  segno, wal_segment_size);
146 
147  /* Populate the values and null arrays */
148  values[0] = LSNGetDatum(ControlFile->checkPoint);
149  nulls[0] = false;
150 
151  values[1] = LSNGetDatum(ControlFile->checkPointCopy.redo);
152  nulls[1] = false;
153 
154  values[2] = CStringGetTextDatum(xlogfilename);
155  nulls[2] = false;
156 
157  values[3] = Int32GetDatum(ControlFile->checkPointCopy.ThisTimeLineID);
158  nulls[3] = false;
159 
160  values[4] = Int32GetDatum(ControlFile->checkPointCopy.PrevTimeLineID);
161  nulls[4] = false;
162 
163  values[5] = BoolGetDatum(ControlFile->checkPointCopy.fullPageWrites);
164  nulls[5] = false;
165 
166  values[6] = CStringGetTextDatum(psprintf("%u:%u",
167  ControlFile->checkPointCopy.nextXidEpoch,
168  ControlFile->checkPointCopy.nextXid));
169  nulls[6] = false;
170 
171  values[7] = ObjectIdGetDatum(ControlFile->checkPointCopy.nextOid);
172  nulls[7] = false;
173 
174  values[8] = TransactionIdGetDatum(ControlFile->checkPointCopy.nextMulti);
175  nulls[8] = false;
176 
177  values[9] = TransactionIdGetDatum(ControlFile->checkPointCopy.nextMultiOffset);
178  nulls[9] = false;
179 
180  values[10] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestXid);
181  nulls[10] = false;
182 
183  values[11] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestXidDB);
184  nulls[11] = false;
185 
186  values[12] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestActiveXid);
187  nulls[12] = false;
188 
189  values[13] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestMulti);
190  nulls[13] = false;
191 
192  values[14] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestMultiDB);
193  nulls[14] = false;
194 
195  values[15] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestCommitTsXid);
196  nulls[15] = false;
197 
198  values[16] = TransactionIdGetDatum(ControlFile->checkPointCopy.newestCommitTsXid);
199  nulls[16] = false;
200 
201  values[17] = TimestampTzGetDatum(
203  nulls[17] = false;
204 
205  htup = heap_form_tuple(tupdesc, values, nulls);
206 
208 }
209 
210 Datum
212 {
213  Datum values[5];
214  bool nulls[5];
215  TupleDesc tupdesc;
216  HeapTuple htup;
218  bool crc_ok;
219 
220  /*
221  * Construct a tuple descriptor for the result row. This must match this
222  * function's pg_proc entry!
223  */
224  tupdesc = CreateTemplateTupleDesc(5, false);
225  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "min_recovery_end_lsn",
226  LSNOID, -1, 0);
227  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "min_recovery_end_timeline",
228  INT4OID, -1, 0);
229  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "backup_start_lsn",
230  LSNOID, -1, 0);
231  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "backup_end_lsn",
232  LSNOID, -1, 0);
233  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "end_of_backup_record_required",
234  BOOLOID, -1, 0);
235  tupdesc = BlessTupleDesc(tupdesc);
236 
237  /* read the control file */
238  ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
239  if (!crc_ok)
240  ereport(ERROR,
241  (errmsg("calculated CRC checksum does not match value stored in file")));
242 
243  values[0] = LSNGetDatum(ControlFile->minRecoveryPoint);
244  nulls[0] = false;
245 
246  values[1] = Int32GetDatum(ControlFile->minRecoveryPointTLI);
247  nulls[1] = false;
248 
249  values[2] = LSNGetDatum(ControlFile->backupStartPoint);
250  nulls[2] = false;
251 
252  values[3] = LSNGetDatum(ControlFile->backupEndPoint);
253  nulls[3] = false;
254 
255  values[4] = BoolGetDatum(ControlFile->backupEndRequired);
256  nulls[4] = false;
257 
258  htup = heap_form_tuple(tupdesc, values, nulls);
259 
261 }
262 
263 Datum
265 {
266  Datum values[12];
267  bool nulls[12];
268  TupleDesc tupdesc;
269  HeapTuple htup;
271  bool crc_ok;
272 
273  /*
274  * Construct a tuple descriptor for the result row. This must match this
275  * function's pg_proc entry!
276  */
277  tupdesc = CreateTemplateTupleDesc(12, false);
278  TupleDescInitEntry(tupdesc, (AttrNumber) 1, "max_data_alignment",
279  INT4OID, -1, 0);
280  TupleDescInitEntry(tupdesc, (AttrNumber) 2, "database_block_size",
281  INT4OID, -1, 0);
282  TupleDescInitEntry(tupdesc, (AttrNumber) 3, "blocks_per_segment",
283  INT4OID, -1, 0);
284  TupleDescInitEntry(tupdesc, (AttrNumber) 4, "wal_block_size",
285  INT4OID, -1, 0);
286  TupleDescInitEntry(tupdesc, (AttrNumber) 5, "bytes_per_wal_segment",
287  INT4OID, -1, 0);
288  TupleDescInitEntry(tupdesc, (AttrNumber) 6, "max_identifier_length",
289  INT4OID, -1, 0);
290  TupleDescInitEntry(tupdesc, (AttrNumber) 7, "max_index_columns",
291  INT4OID, -1, 0);
292  TupleDescInitEntry(tupdesc, (AttrNumber) 8, "max_toast_chunk_size",
293  INT4OID, -1, 0);
294  TupleDescInitEntry(tupdesc, (AttrNumber) 9, "large_object_chunk_size",
295  INT4OID, -1, 0);
296  TupleDescInitEntry(tupdesc, (AttrNumber) 10, "float4_pass_by_value",
297  BOOLOID, -1, 0);
298  TupleDescInitEntry(tupdesc, (AttrNumber) 11, "float8_pass_by_value",
299  BOOLOID, -1, 0);
300  TupleDescInitEntry(tupdesc, (AttrNumber) 12, "data_page_checksum_version",
301  INT4OID, -1, 0);
302  tupdesc = BlessTupleDesc(tupdesc);
303 
304  /* read the control file */
305  ControlFile = get_controlfile(DataDir, NULL, &crc_ok);
306  if (!crc_ok)
307  ereport(ERROR,
308  (errmsg("calculated CRC checksum does not match value stored in file")));
309 
310  values[0] = Int32GetDatum(ControlFile->maxAlign);
311  nulls[0] = false;
312 
313  values[1] = Int32GetDatum(ControlFile->blcksz);
314  nulls[1] = false;
315 
316  values[2] = Int32GetDatum(ControlFile->relseg_size);
317  nulls[2] = false;
318 
319  values[3] = Int32GetDatum(ControlFile->xlog_blcksz);
320  nulls[3] = false;
321 
322  values[4] = Int32GetDatum(ControlFile->xlog_seg_size);
323  nulls[4] = false;
324 
325  values[5] = Int32GetDatum(ControlFile->nameDataLen);
326  nulls[5] = false;
327 
328  values[6] = Int32GetDatum(ControlFile->indexMaxKeys);
329  nulls[6] = false;
330 
331  values[7] = Int32GetDatum(ControlFile->toast_max_chunk_size);
332  nulls[7] = false;
333 
334  values[8] = Int32GetDatum(ControlFile->loblksize);
335  nulls[8] = false;
336 
337  values[9] = BoolGetDatum(ControlFile->float4ByVal);
338  nulls[9] = false;
339 
340  values[10] = BoolGetDatum(ControlFile->float8ByVal);
341  nulls[10] = false;
342 
343  values[11] = Int32GetDatum(ControlFile->data_checksum_version);
344  nulls[11] = false;
345 
346  htup = heap_form_tuple(tupdesc, values, nulls);
347 
349 }
Datum pg_control_recovery(PG_FUNCTION_ARGS)
int wal_segment_size
Definition: xlog.c:113
pg_time_t time
Definition: pg_control.h:128
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:167
TransactionId oldestActiveXid
Definition: pg_control.h:63
ControlFileData * get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
uint32 nameDataLen
Definition: pg_control.h:210
MultiXactId oldestMulti
Definition: pg_control.h:49
TimeLineID PrevTimeLineID
Definition: pg_control.h:39
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
uint32 pg_control_version
Definition: pg_control.h:121
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
CheckPoint checkPointCopy
Definition: pg_control.h:131
uint32 xlog_blcksz
Definition: pg_control.h:207
TransactionId oldestXid
Definition: pg_control.h:47
TransactionId nextXid
Definition: pg_control.h:43
pg_time_t time
Definition: pg_control.h:51
bool backupEndRequired
Definition: pg_control.h:170
MultiXactOffset nextMultiOffset
Definition: pg_control.h:46
TransactionId oldestCommitTsXid
Definition: pg_control.h:52
uint32 xlog_seg_size
Definition: pg_control.h:208
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
uint64 system_identifier
Definition: pg_control.h:106
Datum pg_control_checkpoint(PG_FUNCTION_ARGS)
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
Datum pg_control_init(PG_FUNCTION_ARGS)
uint32 nextXidEpoch
Definition: pg_control.h:42
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1109
uint32 data_checksum_version
Definition: pg_control.h:221
uint64 XLogSegNo
Definition: xlogdefs.h:34
uint32 loblksize
Definition: pg_control.h:214
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1877
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:600
#define ereport(elevel, rest)
Definition: elog.h:122
uint32 indexMaxKeys
Definition: pg_control.h:211
#define MAXFNAMELEN
TransactionId newestCommitTsXid
Definition: pg_control.h:54
#define TransactionIdGetDatum(X)
Definition: postgres.h:506
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:318
Oid oldestMultiDB
Definition: pg_control.h:50
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1670
uint32 toast_max_chunk_size
Definition: pg_control.h:213
static ControlFileData * ControlFile
Definition: xlog.c:715
#define BoolGetDatum(X)
Definition: postgres.h:387
Oid nextOid
Definition: pg_control.h:44
XLogRecPtr backupEndPoint
Definition: pg_control.h:169
bool fullPageWrites
Definition: pg_control.h:41
Datum pg_control_system(PG_FUNCTION_ARGS)
Oid oldestXidDB
Definition: pg_control.h:48
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
MultiXactId nextMulti
Definition: pg_control.h:45
uint32 catalog_version_no
Definition: pg_control.h:122
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:231
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:45
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define Int32GetDatum(X)
Definition: postgres.h:464
TimeLineID ThisTimeLineID
Definition: pg_control.h:38
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:95
char * DataDir
Definition: globals.c:61
#define PG_FUNCTION_ARGS
Definition: fmgr.h:163
int16 AttrNumber
Definition: attnum.h:21
XLogRecPtr backupStartPoint
Definition: pg_control.h:168
uint32 relseg_size
Definition: pg_control.h:205
XLogRecPtr checkPoint
Definition: pg_control.h:129
XLogRecPtr redo
Definition: pg_control.h:36
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:166
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)