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