PostgreSQL Source Code  git master
pg_controldata.c
Go to the documentation of this file.
1 /*
2  * pg_controldata
3  *
4  * reads the data from $PGDATA/global/pg_control
5  *
6  * copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
7  * licence: 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/xlog.h"
24 #include "access/xlog_internal.h"
25 #include "catalog/pg_control.h"
27 #include "pg_getopt.h"
28 #include "getopt_long.h"
29 
30 
31 static void
32 usage(const char *progname)
33 {
34  printf(_("%s displays control information of a PostgreSQL database cluster.\n\n"), progname);
35  printf(_("Usage:\n"));
36  printf(_(" %s [OPTION] [DATADIR]\n"), progname);
37  printf(_("\nOptions:\n"));
38  printf(_(" [-D, --pgdata=]DATADIR data directory\n"));
39  printf(_(" -V, --version output version information, then exit\n"));
40  printf(_(" -?, --help show this help, then exit\n"));
41  printf(_("\nIf no data directory (DATADIR) is specified, "
42  "the environment variable PGDATA\nis used.\n\n"));
43  printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
44 }
45 
46 
47 static const char *
49 {
50  switch (state)
51  {
52  case DB_STARTUP:
53  return _("starting up");
54  case DB_SHUTDOWNED:
55  return _("shut down");
57  return _("shut down in recovery");
58  case DB_SHUTDOWNING:
59  return _("shutting down");
61  return _("in crash recovery");
63  return _("in archive recovery");
64  case DB_IN_PRODUCTION:
65  return _("in production");
66  }
67  return _("unrecognized status code");
68 }
69 
70 static const char *
72 {
73  switch (wal_level)
74  {
75  case WAL_LEVEL_MINIMAL:
76  return "minimal";
77  case WAL_LEVEL_REPLICA:
78  return "replica";
79  case WAL_LEVEL_LOGICAL:
80  return "logical";
81  }
82  return _("unrecognized wal_level");
83 }
84 
85 
86 int
87 main(int argc, char *argv[])
88 {
89  static struct option long_options[] = {
90  {"pgdata", required_argument, NULL, 'D'},
91  {NULL, 0, NULL, 0}
92  };
93 
95  bool crc_ok;
96  char *DataDir = NULL;
97  time_t time_tmp;
98  char pgctime_str[128];
99  char ckpttime_str[128];
100  char sysident_str[32];
101  char mock_auth_nonce_str[MOCK_AUTH_NONCE_LEN * 2 + 1];
102  const char *strftime_fmt = "%c";
103  const char *progname;
104  char xlogfilename[MAXFNAMELEN];
105  int c;
106  int i;
107  int WalSegSz;
108 
109  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_controldata"));
110 
111  progname = get_progname(argv[0]);
112 
113  if (argc > 1)
114  {
115  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
116  {
117  usage(progname);
118  exit(0);
119  }
120  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
121  {
122  puts("pg_controldata (PostgreSQL) " PG_VERSION);
123  exit(0);
124  }
125  }
126 
127  while ((c = getopt_long(argc, argv, "D:", long_options, NULL)) != -1)
128  {
129  switch (c)
130  {
131  case 'D':
132  DataDir = optarg;
133  break;
134 
135  default:
136  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
137  exit(1);
138  }
139  }
140 
141  if (DataDir == NULL)
142  {
143  if (optind < argc)
144  DataDir = argv[optind++];
145  else
146  DataDir = getenv("PGDATA");
147  }
148 
149  /* Complain if any arguments remain */
150  if (optind < argc)
151  {
152  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
153  progname, argv[optind]);
154  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
155  progname);
156  exit(1);
157  }
158 
159  if (DataDir == NULL)
160  {
161  fprintf(stderr, _("%s: no data directory specified\n"), progname);
162  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
163  exit(1);
164  }
165 
166  /* get a copy of the control file */
167  ControlFile = get_controlfile(DataDir, progname, &crc_ok);
168  if (!crc_ok)
169  printf(_("WARNING: Calculated CRC checksum does not match value stored in file.\n"
170  "Either the file is corrupt, or it has a different layout than this program\n"
171  "is expecting. The results below are untrustworthy.\n\n"));
172 
173  /* set wal segment size */
174  WalSegSz = ControlFile->xlog_seg_size;
175 
176  if (!IsValidWalSegSize(WalSegSz))
177  {
178  printf(_("WARNING: invalid WAL segment size\n"));
179  printf(ngettext("The WAL segment size stored in the file, %d byte, is not a power of two\n"
180  "between 1 MB and 1 GB. The file is corrupt and the results below are\n"
181  "untrustworthy.\n\n",
182  "The WAL segment size stored in the file, %d bytes, is not a power of two\n"
183  "between 1 MB and 1 GB. The file is corrupt and the results below are\n"
184  "untrustworthy.\n\n",
185  WalSegSz),
186  WalSegSz);
187  }
188 
189  /*
190  * This slightly-chintzy coding will work as long as the control file
191  * timestamps are within the range of time_t; that should be the case in
192  * all foreseeable circumstances, so we don't bother importing the
193  * backend's timezone library into pg_controldata.
194  *
195  * Use variable for format to suppress overly-anal-retentive gcc warning
196  * about %c
197  */
198  time_tmp = (time_t) ControlFile->time;
199  strftime(pgctime_str, sizeof(pgctime_str), strftime_fmt,
200  localtime(&time_tmp));
201  time_tmp = (time_t) ControlFile->checkPointCopy.time;
202  strftime(ckpttime_str, sizeof(ckpttime_str), strftime_fmt,
203  localtime(&time_tmp));
204 
205  /*
206  * Calculate name of the WAL file containing the latest checkpoint's REDO
207  * start point.
208  *
209  * A corrupted control file could report a WAL segment size of 0, and to
210  * guard against division by zero, we need to treat that specially.
211  */
212  if (WalSegSz != 0)
213  {
214  XLogSegNo segno;
215 
216  XLByteToSeg(ControlFile->checkPointCopy.redo, segno, WalSegSz);
217  XLogFileName(xlogfilename, ControlFile->checkPointCopy.ThisTimeLineID,
218  segno, WalSegSz);
219  }
220  else
221  strcpy(xlogfilename, _("???"));
222 
223  /*
224  * Format system_identifier and mock_authentication_nonce separately to
225  * keep platform-dependent format code out of the translatable message
226  * string.
227  */
228  snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
229  ControlFile->system_identifier);
230  for (i = 0; i < MOCK_AUTH_NONCE_LEN; i++)
231  snprintf(&mock_auth_nonce_str[i * 2], 3, "%02x",
232  (unsigned char) ControlFile->mock_authentication_nonce[i]);
233 
234  printf(_("pg_control version number: %u\n"),
235  ControlFile->pg_control_version);
236  printf(_("Catalog version number: %u\n"),
237  ControlFile->catalog_version_no);
238  printf(_("Database system identifier: %s\n"),
239  sysident_str);
240  printf(_("Database cluster state: %s\n"),
241  dbState(ControlFile->state));
242  printf(_("pg_control last modified: %s\n"),
243  pgctime_str);
244  printf(_("Latest checkpoint location: %X/%X\n"),
245  (uint32) (ControlFile->checkPoint >> 32),
246  (uint32) ControlFile->checkPoint);
247  printf(_("Latest checkpoint's REDO location: %X/%X\n"),
248  (uint32) (ControlFile->checkPointCopy.redo >> 32),
249  (uint32) ControlFile->checkPointCopy.redo);
250  printf(_("Latest checkpoint's REDO WAL file: %s\n"),
251  xlogfilename);
252  printf(_("Latest checkpoint's TimeLineID: %u\n"),
253  ControlFile->checkPointCopy.ThisTimeLineID);
254  printf(_("Latest checkpoint's PrevTimeLineID: %u\n"),
255  ControlFile->checkPointCopy.PrevTimeLineID);
256  printf(_("Latest checkpoint's full_page_writes: %s\n"),
257  ControlFile->checkPointCopy.fullPageWrites ? _("on") : _("off"));
258  printf(_("Latest checkpoint's NextXID: %u:%u\n"),
259  ControlFile->checkPointCopy.nextXidEpoch,
260  ControlFile->checkPointCopy.nextXid);
261  printf(_("Latest checkpoint's NextOID: %u\n"),
262  ControlFile->checkPointCopy.nextOid);
263  printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
264  ControlFile->checkPointCopy.nextMulti);
265  printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
266  ControlFile->checkPointCopy.nextMultiOffset);
267  printf(_("Latest checkpoint's oldestXID: %u\n"),
268  ControlFile->checkPointCopy.oldestXid);
269  printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
270  ControlFile->checkPointCopy.oldestXidDB);
271  printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
272  ControlFile->checkPointCopy.oldestActiveXid);
273  printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
274  ControlFile->checkPointCopy.oldestMulti);
275  printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
276  ControlFile->checkPointCopy.oldestMultiDB);
277  printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
278  ControlFile->checkPointCopy.oldestCommitTsXid);
279  printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
280  ControlFile->checkPointCopy.newestCommitTsXid);
281  printf(_("Time of latest checkpoint: %s\n"),
282  ckpttime_str);
283  printf(_("Fake LSN counter for unlogged rels: %X/%X\n"),
284  (uint32) (ControlFile->unloggedLSN >> 32),
285  (uint32) ControlFile->unloggedLSN);
286  printf(_("Minimum recovery ending location: %X/%X\n"),
287  (uint32) (ControlFile->minRecoveryPoint >> 32),
288  (uint32) ControlFile->minRecoveryPoint);
289  printf(_("Min recovery ending loc's timeline: %u\n"),
290  ControlFile->minRecoveryPointTLI);
291  printf(_("Backup start location: %X/%X\n"),
292  (uint32) (ControlFile->backupStartPoint >> 32),
293  (uint32) ControlFile->backupStartPoint);
294  printf(_("Backup end location: %X/%X\n"),
295  (uint32) (ControlFile->backupEndPoint >> 32),
296  (uint32) ControlFile->backupEndPoint);
297  printf(_("End-of-backup record required: %s\n"),
298  ControlFile->backupEndRequired ? _("yes") : _("no"));
299  printf(_("wal_level setting: %s\n"),
300  wal_level_str(ControlFile->wal_level));
301  printf(_("wal_log_hints setting: %s\n"),
302  ControlFile->wal_log_hints ? _("on") : _("off"));
303  printf(_("max_connections setting: %d\n"),
304  ControlFile->MaxConnections);
305  printf(_("max_worker_processes setting: %d\n"),
306  ControlFile->max_worker_processes);
307  printf(_("max_prepared_xacts setting: %d\n"),
308  ControlFile->max_prepared_xacts);
309  printf(_("max_locks_per_xact setting: %d\n"),
310  ControlFile->max_locks_per_xact);
311  printf(_("track_commit_timestamp setting: %s\n"),
312  ControlFile->track_commit_timestamp ? _("on") : _("off"));
313  printf(_("Maximum data alignment: %u\n"),
314  ControlFile->maxAlign);
315  /* we don't print floatFormat since can't say much useful about it */
316  printf(_("Database block size: %u\n"),
317  ControlFile->blcksz);
318  printf(_("Blocks per segment of large relation: %u\n"),
319  ControlFile->relseg_size);
320  printf(_("WAL block size: %u\n"),
321  ControlFile->xlog_blcksz);
322  printf(_("Bytes per WAL segment: %u\n"),
323  ControlFile->xlog_seg_size);
324  printf(_("Maximum length of identifiers: %u\n"),
325  ControlFile->nameDataLen);
326  printf(_("Maximum columns in an index: %u\n"),
327  ControlFile->indexMaxKeys);
328  printf(_("Maximum size of a TOAST chunk: %u\n"),
329  ControlFile->toast_max_chunk_size);
330  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: */
333  printf(_("Date/time type storage: %s\n"),
334  _("64-bit integers"));
335  printf(_("Float4 argument passing: %s\n"),
336  (ControlFile->float4ByVal ? _("by value") : _("by reference")));
337  printf(_("Float8 argument passing: %s\n"),
338  (ControlFile->float8ByVal ? _("by value") : _("by reference")));
339  printf(_("Data page checksum version: %u\n"),
340  ControlFile->data_checksum_version);
341  printf(_("Mock authentication nonce: %s\n"),
342  mock_auth_nonce_str);
343  return 0;
344 }
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:97
int max_locks_per_xact
Definition: pg_control.h:181
static const char * wal_level_str(WalLevel wal_level)
int max_prepared_xacts
Definition: pg_control.h:180
pg_time_t time
Definition: pg_control.h:128
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:167
const char * get_progname(const char *argv0)
Definition: path.c:453
int max_worker_processes
Definition: pg_control.h:179
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
TransactionId oldestActiveXid
Definition: pg_control.h:63
int wal_level
Definition: xlog.c:104
ControlFileData * get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
static void usage(const char *progname)
uint32 nameDataLen
Definition: pg_control.h:210
MultiXactId oldestMulti
Definition: pg_control.h:49
TimeLineID PrevTimeLineID
Definition: pg_control.h:39
uint32 pg_control_version
Definition: pg_control.h:121
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
int main(int argc, char *argv[])
CheckPoint checkPointCopy
Definition: pg_control.h:131
uint32 xlog_blcksz
Definition: pg_control.h:207
TransactionId oldestXid
Definition: pg_control.h:47
const char * progname
Definition: pg_standby.c:37
TransactionId nextXid
Definition: pg_control.h:43
pg_time_t time
Definition: pg_control.h:51
#define MOCK_AUTH_NONCE_LEN
Definition: pg_control.h:27
bool backupEndRequired
Definition: pg_control.h:170
MultiXactOffset nextMultiOffset
Definition: pg_control.h:46
#define required_argument
Definition: getopt_long.h:25
TransactionId oldestCommitTsXid
Definition: pg_control.h:52
uint32 xlog_seg_size
Definition: pg_control.h:208
int optind
Definition: getopt.c:51
uint64 system_identifier
Definition: pg_control.h:106
uint32 nextXidEpoch
Definition: pg_control.h:42
uint32 data_checksum_version
Definition: pg_control.h:221
char * c
XLogRecPtr unloggedLSN
Definition: pg_control.h:133
uint64 XLogSegNo
Definition: xlogdefs.h:34
unsigned int uint32
Definition: c.h:325
uint32 loblksize
Definition: pg_control.h:214
uint32 indexMaxKeys
Definition: pg_control.h:211
DBState
Definition: pg_control.h:85
#define MAXFNAMELEN
#define ngettext(s, p, n)
Definition: c.h:1022
TransactionId newestCommitTsXid
Definition: pg_control.h:54
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1054
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:228
Oid oldestMultiDB
Definition: pg_control.h:50
uint32 toast_max_chunk_size
Definition: pg_control.h:213
static ControlFileData * ControlFile
Definition: xlog.c:715
Oid nextOid
Definition: pg_control.h:44
XLogRecPtr backupEndPoint
Definition: pg_control.h:169
bool fullPageWrites
Definition: pg_control.h:41
bool track_commit_timestamp
Definition: pg_control.h:182
Definition: regguts.h:298
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
int WalSegSz
Definition: pg_standby.c:39
TimeLineID ThisTimeLineID
Definition: pg_control.h:38
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * optarg
Definition: getopt.c:53
int i
char * DataDir
Definition: globals.c:63
#define _(x)
Definition: elog.c:84
#define UINT64_FORMAT
Definition: c.h:368
static const char * dbState(DBState state)
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
WalLevel
Definition: xlog.h:125
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)