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 "getopt_long.h"
30 #include "pg_getopt.h"
31 
32 static void
33 usage(const char *progname)
34 {
35  printf(_("%s displays control information of a PostgreSQL database cluster.\n\n"), progname);
36  printf(_("Usage:\n"));
37  printf(_(" %s [OPTION] [DATADIR]\n"), progname);
38  printf(_("\nOptions:\n"));
39  printf(_(" [-D, --pgdata=]DATADIR data directory\n"));
40  printf(_(" -V, --version output version information, then exit\n"));
41  printf(_(" -?, --help show this help, then exit\n"));
42  printf(_("\nIf no data directory (DATADIR) is specified, "
43  "the environment variable PGDATA\nis used.\n\n"));
44  printf(_("Report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
45  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
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  /* getopt_long already emitted a complaint */
138  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
139  exit(1);
140  }
141  }
142 
143  if (DataDir == NULL)
144  {
145  if (optind < argc)
146  DataDir = argv[optind++];
147  else
148  DataDir = getenv("PGDATA");
149  }
150 
151  /* Complain if any arguments remain */
152  if (optind < argc)
153  {
154  pg_log_error("too many command-line arguments (first is \"%s\")",
155  argv[optind]);
156  pg_log_error_hint("Try \"%s --help\" for more information.", progname);
157  exit(1);
158  }
159 
160  if (DataDir == NULL)
161  {
162  pg_log_error("no data directory specified");
163  pg_log_error_hint("Try \"%s --help\" for more information.", 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  {
171  pg_log_warning("calculated CRC checksum does not match value stored in control file");
172  pg_log_warning_detail("Either the control file is corrupt, or it has a different layout than this program "
173  "is expecting. The results below are untrustworthy.");
174  }
175 
176  /* set wal segment size */
178 
180  {
181  pg_log_warning(ngettext("invalid WAL segment size in control file (%d byte)",
182  "invalid WAL segment size in control file (%d bytes)",
183  WalSegSz),
184  WalSegSz);
185  pg_log_warning_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
186  pg_log_warning_detail("The file is corrupt and the results below are untrustworthy.");
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 
218  segno, WalSegSz);
219  }
220  else
221  strcpy(xlogfilename, _("???"));
222 
223  for (i = 0; i < MOCK_AUTH_NONCE_LEN; i++)
224  snprintf(&mock_auth_nonce_str[i * 2], 3, "%02x",
225  (unsigned char) ControlFile->mock_authentication_nonce[i]);
226 
227  printf(_("pg_control version number: %u\n"),
229  printf(_("Catalog version number: %u\n"),
231  printf(_("Database system identifier: %llu\n"),
232  (unsigned long long) ControlFile->system_identifier);
233  printf(_("Database cluster state: %s\n"),
235  printf(_("pg_control last modified: %s\n"),
236  pgctime_str);
237  printf(_("Latest checkpoint location: %X/%X\n"),
239  printf(_("Latest checkpoint's REDO location: %X/%X\n"),
241  printf(_("Latest checkpoint's REDO WAL file: %s\n"),
242  xlogfilename);
243  printf(_("Latest checkpoint's TimeLineID: %u\n"),
245  printf(_("Latest checkpoint's PrevTimeLineID: %u\n"),
247  printf(_("Latest checkpoint's full_page_writes: %s\n"),
248  ControlFile->checkPointCopy.fullPageWrites ? _("on") : _("off"));
249  printf(_("Latest checkpoint's NextXID: %u:%u\n"),
252  printf(_("Latest checkpoint's NextOID: %u\n"),
254  printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
256  printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
258  printf(_("Latest checkpoint's oldestXID: %u\n"),
260  printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
262  printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
264  printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
266  printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
268  printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
270  printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
272  printf(_("Time of latest checkpoint: %s\n"),
273  ckpttime_str);
274  printf(_("Fake LSN counter for unlogged rels: %X/%X\n"),
276  printf(_("Minimum recovery ending location: %X/%X\n"),
278  printf(_("Min recovery ending loc's timeline: %u\n"),
280  printf(_("Backup start location: %X/%X\n"),
282  printf(_("Backup end location: %X/%X\n"),
284  printf(_("End-of-backup record required: %s\n"),
285  ControlFile->backupEndRequired ? _("yes") : _("no"));
286  printf(_("wal_level setting: %s\n"),
288  printf(_("wal_log_hints setting: %s\n"),
289  ControlFile->wal_log_hints ? _("on") : _("off"));
290  printf(_("max_connections setting: %d\n"),
292  printf(_("max_worker_processes setting: %d\n"),
294  printf(_("max_wal_senders setting: %d\n"),
296  printf(_("max_prepared_xacts setting: %d\n"),
298  printf(_("max_locks_per_xact setting: %d\n"),
300  printf(_("track_commit_timestamp setting: %s\n"),
301  ControlFile->track_commit_timestamp ? _("on") : _("off"));
302  printf(_("Maximum data alignment: %u\n"),
304  /* we don't print floatFormat since can't say much useful about it */
305  printf(_("Database block size: %u\n"),
307  printf(_("Blocks per segment of large relation: %u\n"),
309  printf(_("WAL block size: %u\n"),
311  printf(_("Bytes per WAL segment: %u\n"),
313  printf(_("Maximum length of identifiers: %u\n"),
315  printf(_("Maximum columns in an index: %u\n"),
317  printf(_("Maximum size of a TOAST chunk: %u\n"),
319  printf(_("Size of a large-object chunk: %u\n"),
321  /* This is no longer configurable, but users may still expect to see it: */
322  printf(_("Date/time type storage: %s\n"),
323  _("64-bit integers"));
324  printf(_("Float8 argument passing: %s\n"),
325  (ControlFile->float8ByVal ? _("by value") : _("by reference")));
326  printf(_("Data page checksum version: %u\n"),
328  printf(_("Mock authentication nonce: %s\n"),
329  mock_auth_nonce_str);
330  return 0;
331 }
static void usage(const char *progname)
int main(int argc, char *argv[])
static const char * dbState(DBState state)
static const char * wal_level_str(WalLevel wal_level)
#define ngettext(s, p, n)
Definition: c.h:1172
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1205
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
#define _(x)
Definition: elog.c:90
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define required_argument
Definition: getopt_long.h:25
char * DataDir
Definition: globals.c:70
int i
Definition: isn.c:72
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_error_hint(...)
Definition: logging.h:112
#define pg_log_warning_detail(...)
Definition: logging.h:118
const char * progname
Definition: main.c:43
#define MOCK_AUTH_NONCE_LEN
Definition: pg_control.h:28
DBState
Definition: pg_control.h:90
@ DB_IN_PRODUCTION
Definition: pg_control.h:97
@ DB_STARTUP
Definition: pg_control.h:91
@ DB_SHUTDOWNING
Definition: pg_control.h:94
@ DB_IN_ARCHIVE_RECOVERY
Definition: pg_control.h:96
@ DB_SHUTDOWNED_IN_RECOVERY
Definition: pg_control.h:93
@ DB_SHUTDOWNED
Definition: pg_control.h:92
@ DB_IN_CRASH_RECOVERY
Definition: pg_control.h:95
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
#define pg_log_warning(...)
Definition: pgfnames.c:24
const char * get_progname(const char *argv0)
Definition: path.c:575
#define snprintf
Definition: port.h:238
#define printf(...)
Definition: port.h:244
char * c
int WalSegSz
Definition: streamutil.c:32
Oid oldestMultiDB
Definition: pg_control.h:51
MultiXactId oldestMulti
Definition: pg_control.h:50
MultiXactOffset nextMultiOffset
Definition: pg_control.h:47
TransactionId newestCommitTsXid
Definition: pg_control.h:55
TransactionId oldestXid
Definition: pg_control.h:48
TimeLineID PrevTimeLineID
Definition: pg_control.h:40
TimeLineID ThisTimeLineID
Definition: pg_control.h:39
Oid nextOid
Definition: pg_control.h:45
TransactionId oldestActiveXid
Definition: pg_control.h:64
bool fullPageWrites
Definition: pg_control.h:42
MultiXactId nextMulti
Definition: pg_control.h:46
FullTransactionId nextXid
Definition: pg_control.h:44
TransactionId oldestCommitTsXid
Definition: pg_control.h:53
pg_time_t time
Definition: pg_control.h:52
XLogRecPtr redo
Definition: pg_control.h:37
Oid oldestXidDB
Definition: pg_control.h:49
char mock_authentication_nonce[MOCK_AUTH_NONCE_LEN]
Definition: pg_control.h:229
int max_worker_processes
Definition: pg_control.h:181
uint32 pg_control_version
Definition: pg_control.h:125
uint32 xlog_seg_size
Definition: pg_control.h:211
XLogRecPtr backupStartPoint
Definition: pg_control.h:170
bool track_commit_timestamp
Definition: pg_control.h:185
bool backupEndRequired
Definition: pg_control.h:172
int max_locks_per_xact
Definition: pg_control.h:184
uint32 nameDataLen
Definition: pg_control.h:213
CheckPoint checkPointCopy
Definition: pg_control.h:135
XLogRecPtr backupEndPoint
Definition: pg_control.h:171
XLogRecPtr minRecoveryPoint
Definition: pg_control.h:168
uint32 data_checksum_version
Definition: pg_control.h:222
XLogRecPtr unloggedLSN
Definition: pg_control.h:137
uint32 indexMaxKeys
Definition: pg_control.h:214
uint32 relseg_size
Definition: pg_control.h:208
pg_time_t time
Definition: pg_control.h:132
XLogRecPtr checkPoint
Definition: pg_control.h:133
uint64 system_identifier
Definition: pg_control.h:110
uint32 catalog_version_no
Definition: pg_control.h:126
int max_prepared_xacts
Definition: pg_control.h:183
uint32 xlog_blcksz
Definition: pg_control.h:210
TimeLineID minRecoveryPointTLI
Definition: pg_control.h:169
uint32 loblksize
Definition: pg_control.h:217
uint32 toast_max_chunk_size
Definition: pg_control.h:216
Definition: regguts.h:323
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define XidFromFullTransactionId(x)
Definition: transam.h:48
int wal_level
Definition: xlog.c:131
static ControlFileData * ControlFile
Definition: xlog.c:574
WalLevel
Definition: xlog.h:73
@ WAL_LEVEL_REPLICA
Definition: xlog.h:75
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76
@ WAL_LEVEL_MINIMAL
Definition: xlog.h:74
#define IsValidWalSegSize(size)
Definition: xlog_internal.h:96
#define MAXFNAMELEN
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
uint64 XLogSegNo
Definition: xlogdefs.h:48