Branch data Line data Source code
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-2026, 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 "common/controldata_utils.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
32 : 16 : pg_control_system(PG_FUNCTION_ARGS)
33 : : {
34 : : Datum values[4];
35 : : bool nulls[4];
36 : : TupleDesc tupdesc;
37 : : HeapTuple htup;
38 : : ControlFileData *ControlFile;
39 : : bool crc_ok;
40 : :
41 [ - + ]: 16 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
42 [ # # ]: 0 : elog(ERROR, "return type must be a row type");
43 : :
44 : : /* read the control file */
45 : 16 : LWLockAcquire(ControlFileLock, LW_SHARED);
46 : 16 : ControlFile = get_controlfile(DataDir, &crc_ok);
47 : 16 : LWLockRelease(ControlFileLock);
48 [ - + ]: 16 : if (!crc_ok)
49 [ # # ]: 0 : ereport(ERROR,
50 : : (errcode(ERRCODE_DATA_CORRUPTED),
51 : : errmsg("calculated CRC checksum does not match value stored in file")));
52 : :
53 : 16 : values[0] = Int32GetDatum(ControlFile->pg_control_version);
54 : 16 : nulls[0] = false;
55 : :
56 : 16 : values[1] = Int32GetDatum(ControlFile->catalog_version_no);
57 : 16 : nulls[1] = false;
58 : :
59 : 16 : values[2] = Int64GetDatum(ControlFile->system_identifier);
60 : 16 : nulls[2] = false;
61 : :
62 : 16 : values[3] = TimestampTzGetDatum(time_t_to_timestamptz(ControlFile->time));
63 : 16 : nulls[3] = false;
64 : :
65 : 16 : htup = heap_form_tuple(tupdesc, values, nulls);
66 : :
67 : 16 : PG_RETURN_DATUM(HeapTupleGetDatum(htup));
68 : : }
69 : :
70 : : Datum
71 : 18 : pg_control_checkpoint(PG_FUNCTION_ARGS)
72 : : {
73 : : Datum values[20];
74 : : bool nulls[20];
75 : : TupleDesc tupdesc;
76 : : HeapTuple htup;
77 : : ControlFileData *ControlFile;
78 : : XLogSegNo segno;
79 : : char xlogfilename[MAXFNAMELEN];
80 : : bool crc_ok;
81 : :
82 [ - + ]: 18 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
83 [ # # ]: 0 : elog(ERROR, "return type must be a row type");
84 : :
85 : : /* Read the control file. */
86 : 18 : LWLockAcquire(ControlFileLock, LW_SHARED);
87 : 18 : ControlFile = get_controlfile(DataDir, &crc_ok);
88 : 18 : LWLockRelease(ControlFileLock);
89 [ - + ]: 18 : if (!crc_ok)
90 [ # # ]: 0 : ereport(ERROR,
91 : : (errcode(ERRCODE_DATA_CORRUPTED),
92 : : errmsg("calculated CRC checksum does not match value stored in file")));
93 : :
94 : : /*
95 : : * Calculate name of the WAL file containing the latest checkpoint's REDO
96 : : * start point.
97 : : */
98 : 18 : XLByteToSeg(ControlFile->checkPointCopy.redo, segno, wal_segment_size);
99 : 18 : XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
100 : : segno, wal_segment_size);
101 : :
102 : : /* Populate the values and null arrays */
103 : 18 : values[0] = LSNGetDatum(ControlFile->checkPoint);
104 : 18 : nulls[0] = false;
105 : :
106 : 18 : values[1] = LSNGetDatum(ControlFile->checkPointCopy.redo);
107 : 18 : nulls[1] = false;
108 : :
109 : 18 : values[2] = CStringGetTextDatum(xlogfilename);
110 : 18 : nulls[2] = false;
111 : :
112 : 18 : values[3] = Int32GetDatum(ControlFile->checkPointCopy.ThisTimeLineID);
113 : 18 : nulls[3] = false;
114 : :
115 : 18 : values[4] = Int32GetDatum(ControlFile->checkPointCopy.PrevTimeLineID);
116 : 18 : nulls[4] = false;
117 : :
118 : 18 : values[5] = BoolGetDatum(ControlFile->checkPointCopy.fullPageWrites);
119 : 18 : nulls[5] = false;
120 : :
121 : 18 : values[6] = BoolGetDatum(ControlFile->checkPointCopy.logicalDecodingEnabled);
122 : 18 : nulls[6] = false;
123 : :
124 : 18 : values[7] = CStringGetTextDatum(psprintf("%u:%u",
125 : : EpochFromFullTransactionId(ControlFile->checkPointCopy.nextXid),
126 : : XidFromFullTransactionId(ControlFile->checkPointCopy.nextXid)));
127 : 18 : nulls[7] = false;
128 : :
129 : 18 : values[8] = ObjectId8GetDatum(ControlFile->checkPointCopy.nextOid);
130 : 18 : nulls[8] = false;
131 : :
132 : 18 : values[9] = TransactionIdGetDatum(ControlFile->checkPointCopy.nextMulti);
133 : 18 : nulls[9] = false;
134 : :
135 : 18 : values[10] = Int64GetDatum(ControlFile->checkPointCopy.nextMultiOffset);
136 : 18 : nulls[10] = false;
137 : :
138 : 18 : values[11] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestXid);
139 : 18 : nulls[11] = false;
140 : :
141 : 18 : values[12] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestXidDB);
142 : 18 : nulls[12] = false;
143 : :
144 : 18 : values[13] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestActiveXid);
145 : 18 : nulls[13] = false;
146 : :
147 : 18 : values[14] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestMulti);
148 : 18 : nulls[14] = false;
149 : :
150 : 18 : values[15] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestMultiDB);
151 : 18 : nulls[15] = false;
152 : :
153 : 18 : values[16] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestCommitTsXid);
154 : 18 : nulls[16] = false;
155 : :
156 : 18 : values[17] = TransactionIdGetDatum(ControlFile->checkPointCopy.newestCommitTsXid);
157 : 18 : nulls[17] = false;
158 : :
159 : 18 : values[18] = Int32GetDatum(ControlFile->checkPointCopy.dataChecksumState);
160 : 18 : nulls[18] = false;
161 : :
162 : 18 : values[19] = TimestampTzGetDatum(time_t_to_timestamptz(ControlFile->checkPointCopy.time));
163 : 18 : nulls[19] = false;
164 : :
165 : 18 : htup = heap_form_tuple(tupdesc, values, nulls);
166 : :
167 : 18 : PG_RETURN_DATUM(HeapTupleGetDatum(htup));
168 : : }
169 : :
170 : : Datum
171 : 4 : pg_control_recovery(PG_FUNCTION_ARGS)
172 : : {
173 : : Datum values[5];
174 : : bool nulls[5];
175 : : TupleDesc tupdesc;
176 : : HeapTuple htup;
177 : : ControlFileData *ControlFile;
178 : : bool crc_ok;
179 : :
180 [ - + ]: 4 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
181 [ # # ]: 0 : elog(ERROR, "return type must be a row type");
182 : :
183 : : /* read the control file */
184 : 4 : LWLockAcquire(ControlFileLock, LW_SHARED);
185 : 4 : ControlFile = get_controlfile(DataDir, &crc_ok);
186 : 4 : LWLockRelease(ControlFileLock);
187 [ - + ]: 4 : if (!crc_ok)
188 [ # # ]: 0 : ereport(ERROR,
189 : : (errcode(ERRCODE_DATA_CORRUPTED),
190 : : errmsg("calculated CRC checksum does not match value stored in file")));
191 : :
192 : 4 : values[0] = LSNGetDatum(ControlFile->minRecoveryPoint);
193 : 4 : nulls[0] = false;
194 : :
195 : 4 : values[1] = Int32GetDatum(ControlFile->minRecoveryPointTLI);
196 : 4 : nulls[1] = false;
197 : :
198 : 4 : values[2] = LSNGetDatum(ControlFile->backupStartPoint);
199 : 4 : nulls[2] = false;
200 : :
201 : 4 : values[3] = LSNGetDatum(ControlFile->backupEndPoint);
202 : 4 : nulls[3] = false;
203 : :
204 : 4 : values[4] = BoolGetDatum(ControlFile->backupEndRequired);
205 : 4 : nulls[4] = false;
206 : :
207 : 4 : htup = heap_form_tuple(tupdesc, values, nulls);
208 : :
209 : 4 : PG_RETURN_DATUM(HeapTupleGetDatum(htup));
210 : : }
211 : :
212 : : Datum
213 : 11 : pg_control_init(PG_FUNCTION_ARGS)
214 : : {
215 : : Datum values[12];
216 : : bool nulls[12];
217 : : TupleDesc tupdesc;
218 : : HeapTuple htup;
219 : : ControlFileData *ControlFile;
220 : : bool crc_ok;
221 : :
222 [ - + ]: 11 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
223 [ # # ]: 0 : elog(ERROR, "return type must be a row type");
224 : :
225 : : /* read the control file */
226 : 11 : LWLockAcquire(ControlFileLock, LW_SHARED);
227 : 11 : ControlFile = get_controlfile(DataDir, &crc_ok);
228 : 11 : LWLockRelease(ControlFileLock);
229 [ - + ]: 11 : if (!crc_ok)
230 [ # # ]: 0 : ereport(ERROR,
231 : : (errcode(ERRCODE_DATA_CORRUPTED),
232 : : errmsg("calculated CRC checksum does not match value stored in file")));
233 : :
234 : 11 : values[0] = Int32GetDatum(ControlFile->maxAlign);
235 : 11 : nulls[0] = false;
236 : :
237 : 11 : values[1] = Int32GetDatum(ControlFile->blcksz);
238 : 11 : nulls[1] = false;
239 : :
240 : 11 : values[2] = Int32GetDatum(ControlFile->relseg_size);
241 : 11 : nulls[2] = false;
242 : :
243 : 11 : values[3] = Int32GetDatum(ControlFile->xlog_blcksz);
244 : 11 : nulls[3] = false;
245 : :
246 : 11 : values[4] = Int32GetDatum(ControlFile->xlog_seg_size);
247 : 11 : nulls[4] = false;
248 : :
249 : 11 : values[5] = Int32GetDatum(ControlFile->nameDataLen);
250 : 11 : nulls[5] = false;
251 : :
252 : 11 : values[6] = Int32GetDatum(ControlFile->indexMaxKeys);
253 : 11 : nulls[6] = false;
254 : :
255 : 11 : values[7] = Int32GetDatum(ControlFile->toast_max_chunk_size);
256 : 11 : nulls[7] = false;
257 : :
258 : 11 : values[8] = Int32GetDatum(ControlFile->loblksize);
259 : 11 : nulls[8] = false;
260 : :
261 : 11 : values[9] = BoolGetDatum(ControlFile->float8ByVal);
262 : 11 : nulls[9] = false;
263 : :
264 : 11 : values[10] = Int32GetDatum(ControlFile->data_checksum_version_init);
265 : 11 : nulls[10] = false;
266 : :
267 : 11 : values[11] = BoolGetDatum(ControlFile->default_char_signedness);
268 : 11 : nulls[11] = false;
269 : :
270 : 11 : htup = heap_form_tuple(tupdesc, values, nulls);
271 : :
272 : 11 : PG_RETURN_DATUM(HeapTupleGetDatum(htup));
273 : : }
|