Age Owner Branch data TLA 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
3920 mail@joeconway.com 32 :CBC 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 : :
1438 michael@paquier.xyz 41 [ - + ]: 16 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1438 michael@paquier.xyz 42 [ # # ]:UBC 0 : elog(ERROR, "return type must be a row type");
43 : :
44 : : /* read the control file */
1139 tmunro@postgresql.or 45 :CBC 16 : LWLockAcquire(ControlFileLock, LW_SHARED);
2798 peter@eisentraut.org 46 : 16 : ControlFile = get_controlfile(DataDir, &crc_ok);
1139 tmunro@postgresql.or 47 : 16 : LWLockRelease(ControlFileLock);
3713 peter_e@gmx.net 48 [ - + ]: 16 : if (!crc_ok)
3777 peter_e@gmx.net 49 [ # # ]:UBC 0 : ereport(ERROR,
50 : : (errcode(ERRCODE_DATA_CORRUPTED),
51 : : errmsg("calculated CRC checksum does not match value stored in file")));
52 : :
3920 mail@joeconway.com 53 :CBC 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 : 17 : 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 : :
1438 michael@paquier.xyz 82 [ - + ]: 17 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1438 michael@paquier.xyz 83 [ # # ]:UBC 0 : elog(ERROR, "return type must be a row type");
84 : :
85 : : /* Read the control file. */
1139 tmunro@postgresql.or 86 :CBC 17 : LWLockAcquire(ControlFileLock, LW_SHARED);
2798 peter@eisentraut.org 87 : 17 : ControlFile = get_controlfile(DataDir, &crc_ok);
1139 tmunro@postgresql.or 88 : 17 : LWLockRelease(ControlFileLock);
3713 peter_e@gmx.net 89 [ - + ]: 17 : if (!crc_ok)
3777 peter_e@gmx.net 90 [ # # ]:UBC 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 : : */
3357 andres@anarazel.de 98 :CBC 17 : XLByteToSeg(ControlFile->checkPointCopy.redo, segno, wal_segment_size);
99 : 17 : XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
100 : : segno, wal_segment_size);
101 : :
102 : : /* Populate the values and null arrays */
3920 mail@joeconway.com 103 : 17 : values[0] = LSNGetDatum(ControlFile->checkPoint);
104 : 17 : nulls[0] = false;
105 : :
3308 simon@2ndQuadrant.co 106 : 17 : values[1] = LSNGetDatum(ControlFile->checkPointCopy.redo);
3920 mail@joeconway.com 107 : 17 : nulls[1] = false;
108 : :
3308 simon@2ndQuadrant.co 109 : 17 : values[2] = CStringGetTextDatum(xlogfilename);
3920 mail@joeconway.com 110 : 17 : nulls[2] = false;
111 : :
3308 simon@2ndQuadrant.co 112 : 17 : values[3] = Int32GetDatum(ControlFile->checkPointCopy.ThisTimeLineID);
3920 mail@joeconway.com 113 : 17 : nulls[3] = false;
114 : :
3308 simon@2ndQuadrant.co 115 : 17 : values[4] = Int32GetDatum(ControlFile->checkPointCopy.PrevTimeLineID);
3920 mail@joeconway.com 116 : 17 : nulls[4] = false;
117 : :
3308 simon@2ndQuadrant.co 118 : 17 : values[5] = BoolGetDatum(ControlFile->checkPointCopy.fullPageWrites);
3920 mail@joeconway.com 119 : 17 : nulls[5] = false;
120 : :
131 msawada@postgresql.o 121 : 17 : values[6] = BoolGetDatum(ControlFile->checkPointCopy.logicalDecodingEnabled);
3308 simon@2ndQuadrant.co 122 : 17 : nulls[6] = false;
123 : :
131 msawada@postgresql.o 124 : 17 : values[7] = CStringGetTextDatum(psprintf("%u:%u",
125 : : EpochFromFullTransactionId(ControlFile->checkPointCopy.nextXid),
126 : : XidFromFullTransactionId(ControlFile->checkPointCopy.nextXid)));
3920 mail@joeconway.com 127 : 17 : nulls[7] = false;
128 : :
79 michael@paquier.xyz 129 :GNC 17 : values[8] = ObjectId8GetDatum(ControlFile->checkPointCopy.nextOid);
3920 mail@joeconway.com 130 :CBC 17 : nulls[8] = false;
131 : :
131 msawada@postgresql.o 132 : 17 : values[9] = TransactionIdGetDatum(ControlFile->checkPointCopy.nextMulti);
3920 mail@joeconway.com 133 : 17 : nulls[9] = false;
134 : :
93 msawada@postgresql.o 135 : 17 : values[10] = Int64GetDatum(ControlFile->checkPointCopy.nextMultiOffset);
3920 mail@joeconway.com 136 : 17 : nulls[10] = false;
137 : :
131 msawada@postgresql.o 138 : 17 : values[11] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestXid);
3920 mail@joeconway.com 139 : 17 : nulls[11] = false;
140 : :
131 msawada@postgresql.o 141 : 17 : values[12] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestXidDB);
3920 mail@joeconway.com 142 : 17 : nulls[12] = false;
143 : :
131 msawada@postgresql.o 144 : 17 : values[13] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestActiveXid);
3920 mail@joeconway.com 145 : 17 : nulls[13] = false;
146 : :
131 msawada@postgresql.o 147 : 17 : values[14] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestMulti);
3920 mail@joeconway.com 148 : 17 : nulls[14] = false;
149 : :
131 msawada@postgresql.o 150 : 17 : values[15] = ObjectIdGetDatum(ControlFile->checkPointCopy.oldestMultiDB);
3920 mail@joeconway.com 151 : 17 : nulls[15] = false;
152 : :
131 msawada@postgresql.o 153 : 17 : values[16] = TransactionIdGetDatum(ControlFile->checkPointCopy.oldestCommitTsXid);
3920 mail@joeconway.com 154 : 17 : nulls[16] = false;
155 : :
131 msawada@postgresql.o 156 : 17 : values[17] = TransactionIdGetDatum(ControlFile->checkPointCopy.newestCommitTsXid);
3308 simon@2ndQuadrant.co 157 : 17 : nulls[17] = false;
158 : :
102 dgustafsson@postgres 159 :GNC 17 : values[18] = Int32GetDatum(ControlFile->checkPointCopy.dataChecksumState);
131 msawada@postgresql.o 160 :CBC 17 : nulls[18] = false;
161 : :
102 dgustafsson@postgres 162 :GNC 17 : values[19] = TimestampTzGetDatum(time_t_to_timestamptz(ControlFile->checkPointCopy.time));
163 : 17 : nulls[19] = false;
164 : :
3920 mail@joeconway.com 165 :CBC 17 : htup = heap_form_tuple(tupdesc, values, nulls);
166 : :
167 : 17 : 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 : :
1438 michael@paquier.xyz 180 [ - + ]: 4 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1438 michael@paquier.xyz 181 [ # # ]:UBC 0 : elog(ERROR, "return type must be a row type");
182 : :
183 : : /* read the control file */
1139 tmunro@postgresql.or 184 :CBC 4 : LWLockAcquire(ControlFileLock, LW_SHARED);
2798 peter@eisentraut.org 185 : 4 : ControlFile = get_controlfile(DataDir, &crc_ok);
1139 tmunro@postgresql.or 186 : 4 : LWLockRelease(ControlFileLock);
3713 peter_e@gmx.net 187 [ - + ]: 4 : if (!crc_ok)
3777 peter_e@gmx.net 188 [ # # ]:UBC 0 : ereport(ERROR,
189 : : (errcode(ERRCODE_DATA_CORRUPTED),
190 : : errmsg("calculated CRC checksum does not match value stored in file")));
191 : :
3920 mail@joeconway.com 192 :CBC 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 : :
1438 michael@paquier.xyz 222 [ - + ]: 11 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1438 michael@paquier.xyz 223 [ # # ]:UBC 0 : elog(ERROR, "return type must be a row type");
224 : :
225 : : /* read the control file */
1139 tmunro@postgresql.or 226 :CBC 11 : LWLockAcquire(ControlFileLock, LW_SHARED);
2798 peter@eisentraut.org 227 : 11 : ControlFile = get_controlfile(DataDir, &crc_ok);
1139 tmunro@postgresql.or 228 : 11 : LWLockRelease(ControlFileLock);
3713 peter_e@gmx.net 229 [ - + ]: 11 : if (!crc_ok)
3777 peter_e@gmx.net 230 [ # # ]:UBC 0 : ereport(ERROR,
231 : : (errcode(ERRCODE_DATA_CORRUPTED),
232 : : errmsg("calculated CRC checksum does not match value stored in file")));
233 : :
3920 mail@joeconway.com 234 :CBC 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 : :
2564 peter@eisentraut.org 261 : 11 : values[9] = BoolGetDatum(ControlFile->float8ByVal);
3920 mail@joeconway.com 262 : 11 : nulls[9] = false;
263 : :
102 dgustafsson@postgres 264 : 11 : values[10] = Int32GetDatum(ControlFile->data_checksum_version_init);
3920 mail@joeconway.com 265 : 11 : nulls[10] = false;
266 : :
645 msawada@postgresql.o 267 : 11 : values[11] = BoolGetDatum(ControlFile->default_char_signedness);
268 : 11 : nulls[11] = false;
269 : :
3920 mail@joeconway.com 270 : 11 : htup = heap_form_tuple(tupdesc, values, nulls);
271 : :
272 : 11 : PG_RETURN_DATUM(HeapTupleGetDatum(htup));
273 : : }
|