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