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