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