Age Owner Branch data TLA Line data Source code
1 : : /*
2 : : * pg_controldata
3 : : *
4 : : * reads the data from the control file located at $PGDATA/XLOG_CONTROL_FILE.
5 : : *
6 : : * copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
7 : : * license: BSD
8 : : *
9 : : * src/bin/pg_controldata/pg_controldata.c
10 : : */
11 : :
12 : : /*
13 : : * We have to use postgres.h not postgres_fe.h here, because there's so much
14 : : * backend-only stuff in the XLOG include files we need. But we need a
15 : : * frontend-ish environment otherwise. Hence this ugly hack.
16 : : */
17 : : #define FRONTEND 1
18 : :
19 : : #include "postgres.h"
20 : :
21 : : #include <time.h>
22 : :
23 : : #include "access/transam.h"
24 : : #include "access/xlog.h"
25 : : #include "access/xlog_internal.h"
26 : : #include "catalog/pg_control.h"
27 : : #include "common/controldata_utils.h"
28 : : #include "common/logging.h"
29 : : #include "getopt_long.h"
30 : : #include "pg_getopt.h"
31 : :
32 : : static void
8417 peter_e@gmx.net 33 :CBC 1 : usage(const char *progname)
34 : : {
8081 35 : 1 : printf(_("%s displays control information of a PostgreSQL database cluster.\n\n"), progname);
4828 36 : 1 : printf(_("Usage:\n"));
3643 37 : 1 : printf(_(" %s [OPTION] [DATADIR]\n"), progname);
4828 38 : 1 : printf(_("\nOptions:\n"));
2669 39 : 1 : printf(_(" [-D, --pgdata=]DATADIR data directory\n"));
40 : 1 : printf(_(" -V, --version output version information, then exit\n"));
41 : 1 : printf(_(" -?, --help show this help, then exit\n"));
7653 neilc@samurai.com 42 : 1 : printf(_("\nIf no data directory (DATADIR) is specified, "
43 : : "the environment variable PGDATA\nis used.\n\n"));
2017 peter@eisentraut.org 44 : 1 : printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
45 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
8417 peter_e@gmx.net 46 : 1 : }
47 : :
48 : :
49 : : static const char *
8943 tgl@sss.pgh.pa.us 50 : 50 : dbState(DBState state)
51 : : {
52 [ + + + - : 50 : switch (state)
- - + - ]
53 : : {
8717 bruce@momjian.us 54 : 1 : case DB_STARTUP:
8405 peter_e@gmx.net 55 : 1 : return _("starting up");
8943 tgl@sss.pgh.pa.us 56 : 46 : case DB_SHUTDOWNED:
8405 peter_e@gmx.net 57 : 46 : return _("shut down");
5574 rhaas@postgresql.org 58 : 2 : case DB_SHUTDOWNED_IN_RECOVERY:
59 : 2 : return _("shut down in recovery");
8943 tgl@sss.pgh.pa.us 60 :UBC 0 : case DB_SHUTDOWNING:
8405 peter_e@gmx.net 61 : 0 : return _("shutting down");
6970 tgl@sss.pgh.pa.us 62 : 0 : case DB_IN_CRASH_RECOVERY:
63 : 0 : return _("in crash recovery");
64 : 0 : case DB_IN_ARCHIVE_RECOVERY:
65 : 0 : return _("in archive recovery");
8943 tgl@sss.pgh.pa.us 66 :CBC 1 : case DB_IN_PRODUCTION:
8405 peter_e@gmx.net 67 : 1 : return _("in production");
68 : : }
8417 peter_e@gmx.net 69 :UBC 0 : return _("unrecognized status code");
70 : : }
71 : :
72 : : static const char *
5610 heikki.linnakangas@i 73 :CBC 50 : wal_level_str(WalLevel wal_level)
74 : : {
75 [ + + + - ]: 50 : switch (wal_level)
76 : : {
77 : 14 : case WAL_LEVEL_MINIMAL:
78 : 14 : return "minimal";
3477 peter_e@gmx.net 79 : 29 : case WAL_LEVEL_REPLICA:
80 : 29 : return "replica";
4288 rhaas@postgresql.org 81 : 7 : case WAL_LEVEL_LOGICAL:
82 : 7 : return "logical";
83 : : }
477 peter@eisentraut.org 84 :UBC 0 : return _("unrecognized \"wal_level\"");
85 : : }
86 : :
87 : :
88 : : int
8766 peter_e@gmx.net 89 :CBC 89 : main(int argc, char *argv[])
90 : : {
91 : : static struct option long_options[] = {
92 : : {"pgdata", required_argument, NULL, 'D'},
93 : : {NULL, 0, NULL, 0}
94 : : };
95 : :
96 : : ControlFileData *ControlFile;
97 : : bool crc_ok;
3970 heikki.linnakangas@i 98 : 89 : char *DataDir = NULL;
99 : : time_t time_tmp;
100 : : struct tm *tm_tmp;
101 : : char pgctime_str[128];
102 : : char ckpttime_str[128];
103 : : char mock_auth_nonce_str[MOCK_AUTH_NONCE_LEN * 2 + 1];
6747 neilc@samurai.com 104 : 89 : const char *strftime_fmt = "%c";
105 : : const char *progname;
106 : : char xlogfilename[MAXFNAMELEN];
107 : : int c;
108 : : int i;
109 : : int WalSegSz;
110 : :
2350 peter@eisentraut.org 111 : 89 : pg_logging_init(argv[0]);
6113 peter_e@gmx.net 112 : 89 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_controldata"));
8191 bruce@momjian.us 113 : 89 : progname = get_progname(argv[0]);
114 : :
8417 peter_e@gmx.net 115 [ + + ]: 89 : if (argc > 1)
116 : : {
117 [ + + - + ]: 88 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
118 : : {
119 : 1 : usage(progname);
120 : 1 : exit(0);
121 : : }
122 [ + + + + ]: 87 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
123 : : {
124 : 35 : puts("pg_controldata (PostgreSQL) " PG_VERSION);
125 : 35 : exit(0);
126 : : }
127 : : }
128 : :
2723 129 [ + + ]: 53 : while ((c = getopt_long(argc, argv, "D:", long_options, NULL)) != -1)
130 : : {
3970 heikki.linnakangas@i 131 [ - + ]: 1 : switch (c)
132 : : {
3970 heikki.linnakangas@i 133 :UBC 0 : case 'D':
134 : 0 : DataDir = optarg;
135 : 0 : break;
136 : :
3970 heikki.linnakangas@i 137 :CBC 1 : default:
138 : : /* getopt_long already emitted a complaint */
1247 tgl@sss.pgh.pa.us 139 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
3970 heikki.linnakangas@i 140 : 1 : exit(1);
141 : : }
142 : : }
143 : :
144 [ + - ]: 52 : if (DataDir == NULL)
145 : : {
146 [ + + ]: 52 : if (optind < argc)
147 : 51 : DataDir = argv[optind++];
148 : : else
149 : 1 : DataDir = getenv("PGDATA");
150 : : }
151 : :
152 : : /* Complain if any arguments remain */
153 [ - + ]: 52 : if (optind < argc)
154 : : {
2350 peter@eisentraut.org 155 :UBC 0 : pg_log_error("too many command-line arguments (first is \"%s\")",
156 : : argv[optind]);
1247 tgl@sss.pgh.pa.us 157 : 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
3970 heikki.linnakangas@i 158 : 0 : exit(1);
159 : : }
160 : :
8934 bruce@momjian.us 161 [ + + ]:CBC 52 : if (DataDir == NULL)
162 : : {
2350 peter@eisentraut.org 163 : 1 : pg_log_error("no data directory specified");
1247 tgl@sss.pgh.pa.us 164 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
8961 bruce@momjian.us 165 : 1 : exit(1);
166 : : }
167 : :
168 : : /* get a copy of the control file */
2350 peter@eisentraut.org 169 : 51 : ControlFile = get_controlfile(DataDir, &crc_ok);
3265 peter_e@gmx.net 170 [ + + ]: 50 : if (!crc_ok)
171 : : {
740 peter@eisentraut.org 172 : 1 : pg_log_warning("calculated CRC checksum does not match value stored in control file");
173 : 1 : pg_log_warning_detail("Either the control file is corrupt, or it has a different layout than this program "
174 : : "is expecting. The results below are untrustworthy.");
175 : : }
176 : :
177 : : /* set wal segment size */
2909 andres@anarazel.de 178 : 50 : WalSegSz = ControlFile->xlog_seg_size;
179 : :
180 [ + + + - : 50 : if (!IsValidWalSegSize(WalSegSz))
+ - - + ]
181 : : {
740 peter@eisentraut.org 182 : 1 : pg_log_warning(ngettext("invalid WAL segment size in control file (%d byte)",
183 : : "invalid WAL segment size in control file (%d bytes)",
184 : : WalSegSz),
185 : : WalSegSz);
186 : 1 : pg_log_warning_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
187 : 1 : pg_log_warning_detail("The file is corrupt and the results below are untrustworthy.");
188 : : }
189 : :
190 : : /*
191 : : * This slightly-chintzy coding will work as long as the control file
192 : : * timestamps are within the range of time_t; that should be the case in
193 : : * all foreseeable circumstances, so we don't bother importing the
194 : : * backend's timezone library into pg_controldata.
195 : : *
196 : : * Use variable for format to suppress overly-anal-retentive gcc warning
197 : : * about %c
198 : : */
3472 mail@joeconway.com 199 : 50 : time_tmp = (time_t) ControlFile->time;
213 akorotkov@postgresql 200 : 50 : tm_tmp = localtime(&time_tmp);
201 : :
202 [ + - ]: 50 : if (tm_tmp != NULL)
203 : 50 : strftime(pgctime_str, sizeof(pgctime_str), strftime_fmt, tm_tmp);
204 : : else
213 akorotkov@postgresql 205 :UBC 0 : snprintf(pgctime_str, sizeof(pgctime_str), _("???"));
206 : :
3472 mail@joeconway.com 207 :CBC 50 : time_tmp = (time_t) ControlFile->checkPointCopy.time;
213 akorotkov@postgresql 208 : 50 : tm_tmp = localtime(&time_tmp);
209 : :
210 [ + - ]: 50 : if (tm_tmp != NULL)
211 : 50 : strftime(ckpttime_str, sizeof(ckpttime_str), strftime_fmt, tm_tmp);
212 : : else
213 akorotkov@postgresql 213 :UBC 0 : snprintf(ckpttime_str, sizeof(ckpttime_str), _("???"));
214 : :
215 : : /*
216 : : * Calculate name of the WAL file containing the latest checkpoint's REDO
217 : : * start point.
218 : : *
219 : : * A corrupted control file could report a WAL segment size of 0 or
220 : : * negative value, and to guard against division by zero, we need to treat
221 : : * that specially.
222 : : */
213 akorotkov@postgresql 223 [ + + ]:CBC 50 : if (WalSegSz > 0)
224 : : {
225 : : XLogSegNo segno;
226 : :
2726 peter_e@gmx.net 227 : 49 : XLByteToSeg(ControlFile->checkPointCopy.redo, segno, WalSegSz);
228 : 49 : XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
229 : : segno, WalSegSz);
230 : : }
231 : : else
232 : 1 : strcpy(xlogfilename, _("???"));
233 : :
3105 heikki.linnakangas@i 234 [ + + ]: 1650 : for (i = 0; i < MOCK_AUTH_NONCE_LEN; i++)
235 : 1600 : snprintf(&mock_auth_nonce_str[i * 2], 3, "%02x",
236 : 1600 : (unsigned char) ControlFile->mock_authentication_nonce[i]);
237 : :
6970 tgl@sss.pgh.pa.us 238 : 50 : printf(_("pg_control version number: %u\n"),
239 : : ControlFile->pg_control_version);
240 : 50 : printf(_("Catalog version number: %u\n"),
241 : : ControlFile->catalog_version_no);
161 peter@eisentraut.org 242 : 50 : printf(_("Database system identifier: %" PRIu64 "\n"),
243 : : ControlFile->system_identifier);
6970 tgl@sss.pgh.pa.us 244 : 50 : printf(_("Database cluster state: %s\n"),
245 : : dbState(ControlFile->state));
246 : 50 : printf(_("pg_control last modified: %s\n"),
247 : : pgctime_str);
61 alvherre@kurilemu.de 248 :GNC 50 : printf(_("Latest checkpoint location: %X/%08X\n"),
249 : : LSN_FORMAT_ARGS(ControlFile->checkPoint));
250 : 50 : printf(_("Latest checkpoint's REDO location: %X/%08X\n"),
251 : : LSN_FORMAT_ARGS(ControlFile->checkPointCopy.redo));
4802 heikki.linnakangas@i 252 :CBC 50 : printf(_("Latest checkpoint's REDO WAL file: %s\n"),
253 : : xlogfilename);
6970 tgl@sss.pgh.pa.us 254 : 50 : printf(_("Latest checkpoint's TimeLineID: %u\n"),
255 : : ControlFile->checkPointCopy.ThisTimeLineID);
4590 heikki.linnakangas@i 256 : 50 : printf(_("Latest checkpoint's PrevTimeLineID: %u\n"),
257 : : ControlFile->checkPointCopy.PrevTimeLineID);
4973 simon@2ndQuadrant.co 258 [ + + ]: 50 : printf(_("Latest checkpoint's full_page_writes: %s\n"),
259 : : ControlFile->checkPointCopy.fullPageWrites ? _("on") : _("off"));
3494 mail@joeconway.com 260 : 50 : printf(_("Latest checkpoint's NextXID: %u:%u\n"),
261 : : EpochFromFullTransactionId(ControlFile->checkPointCopy.nextXid),
262 : : XidFromFullTransactionId(ControlFile->checkPointCopy.nextXid));
6970 tgl@sss.pgh.pa.us 263 : 50 : printf(_("Latest checkpoint's NextOID: %u\n"),
264 : : ControlFile->checkPointCopy.nextOid);
265 : 50 : printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
266 : : ControlFile->checkPointCopy.nextMulti);
267 : 50 : printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
268 : : ControlFile->checkPointCopy.nextMultiOffset);
5850 269 : 50 : printf(_("Latest checkpoint's oldestXID: %u\n"),
270 : : ControlFile->checkPointCopy.oldestXid);
271 : 50 : printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
272 : : ControlFile->checkPointCopy.oldestXidDB);
5724 heikki.linnakangas@i 273 : 50 : printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
274 : : ControlFile->checkPointCopy.oldestActiveXid);
4608 alvherre@alvh.no-ip. 275 : 50 : printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
276 : : ControlFile->checkPointCopy.oldestMulti);
4609 277 : 50 : printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
278 : : ControlFile->checkPointCopy.oldestMultiDB);
3540 mail@joeconway.com 279 : 50 : printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
280 : : ControlFile->checkPointCopy.oldestCommitTsXid);
281 : 50 : printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
282 : : ControlFile->checkPointCopy.newestCommitTsXid);
6970 tgl@sss.pgh.pa.us 283 : 50 : printf(_("Time of latest checkpoint: %s\n"),
284 : : ckpttime_str);
61 alvherre@kurilemu.de 285 :GNC 50 : printf(_("Fake LSN counter for unlogged rels: %X/%08X\n"),
286 : : LSN_FORMAT_ARGS(ControlFile->unloggedLSN));
287 : 50 : printf(_("Minimum recovery ending location: %X/%08X\n"),
288 : : LSN_FORMAT_ARGS(ControlFile->minRecoveryPoint));
4659 heikki.linnakangas@i 289 :CBC 50 : printf(_("Min recovery ending loc's timeline: %u\n"),
290 : : ControlFile->minRecoveryPointTLI);
61 alvherre@kurilemu.de 291 :GNC 50 : printf(_("Backup start location: %X/%08X\n"),
292 : : LSN_FORMAT_ARGS(ControlFile->backupStartPoint));
293 : 50 : printf(_("Backup end location: %X/%08X\n"),
294 : : LSN_FORMAT_ARGS(ControlFile->backupEndPoint));
5134 heikki.linnakangas@i 295 [ - + ]:CBC 50 : printf(_("End-of-backup record required: %s\n"),
296 : : ControlFile->backupEndRequired ? _("yes") : _("no"));
3665 mail@joeconway.com 297 : 50 : printf(_("wal_level setting: %s\n"),
298 : : wal_level_str(ControlFile->wal_level));
299 [ + + ]: 50 : printf(_("wal_log_hints setting: %s\n"),
300 : : ControlFile->wal_log_hints ? _("on") : _("off"));
301 : 50 : printf(_("max_connections setting: %d\n"),
302 : : ControlFile->MaxConnections);
303 : 50 : printf(_("max_worker_processes setting: %d\n"),
304 : : ControlFile->max_worker_processes);
2398 michael@paquier.xyz 305 : 50 : printf(_("max_wal_senders setting: %d\n"),
306 : : ControlFile->max_wal_senders);
3665 mail@joeconway.com 307 : 50 : printf(_("max_prepared_xacts setting: %d\n"),
308 : : ControlFile->max_prepared_xacts);
309 : 50 : printf(_("max_locks_per_xact setting: %d\n"),
310 : : ControlFile->max_locks_per_xact);
311 [ - + ]: 50 : printf(_("track_commit_timestamp setting: %s\n"),
312 : : ControlFile->track_commit_timestamp ? _("on") : _("off"));
6970 tgl@sss.pgh.pa.us 313 : 50 : printf(_("Maximum data alignment: %u\n"),
314 : : ControlFile->maxAlign);
315 : : /* we don't print floatFormat since can't say much useful about it */
316 : 50 : printf(_("Database block size: %u\n"),
317 : : ControlFile->blcksz);
318 : 50 : printf(_("Blocks per segment of large relation: %u\n"),
319 : : ControlFile->relseg_size);
320 : 50 : printf(_("WAL block size: %u\n"),
321 : : ControlFile->xlog_blcksz);
322 : 50 : printf(_("Bytes per WAL segment: %u\n"),
323 : : ControlFile->xlog_seg_size);
324 : 50 : printf(_("Maximum length of identifiers: %u\n"),
325 : : ControlFile->nameDataLen);
326 : 50 : printf(_("Maximum columns in an index: %u\n"),
327 : : ControlFile->indexMaxKeys);
6731 328 : 50 : printf(_("Maximum size of a TOAST chunk: %u\n"),
329 : : ControlFile->toast_max_chunk_size);
4111 330 : 50 : printf(_("Size of a large-object chunk: %u\n"),
331 : : ControlFile->loblksize);
332 : : /* This is no longer configurable, but users may still expect to see it: */
8417 peter_e@gmx.net 333 : 50 : printf(_("Date/time type storage: %s\n"),
334 : : _("64-bit integers"));
6347 tgl@sss.pgh.pa.us 335 [ + + ]: 50 : printf(_("Float8 argument passing: %s\n"),
336 : : (ControlFile->float8ByVal ? _("by value") : _("by reference")));
4512 simon@2ndQuadrant.co 337 : 50 : printf(_("Data page checksum version: %u\n"),
338 : : ControlFile->data_checksum_version);
197 msawada@postgresql.o 339 [ + + ]: 50 : printf(_("Default char data signedness: %s\n"),
340 : : (ControlFile->default_char_signedness ? _("signed") : _("unsigned")));
3105 heikki.linnakangas@i 341 : 50 : printf(_("Mock authentication nonce: %s\n"),
342 : : mock_auth_nonce_str);
8417 peter_e@gmx.net 343 : 50 : return 0;
344 : : }
|