PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_controldata.c File Reference
#include "postgres.h"
#include <time.h>
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "catalog/pg_control.h"
#include "common/controldata_utils.h"
#include "common/logging.h"
#include "getopt_long.h"
#include "pg_getopt.h"
Include dependency graph for pg_controldata.c:

Go to the source code of this file.

Macros

#define FRONTEND   1
 

Functions

static void usage (const char *progname)
 
static const char * dbState (DBState state)
 
static const char * wal_level_str (WalLevel wal_level)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ FRONTEND

#define FRONTEND   1

Definition at line 17 of file pg_controldata.c.

Function Documentation

◆ dbState()

static const char * dbState ( DBState  state)
static

Definition at line 50 of file pg_controldata.c.

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");
67 return _("in production");
68 }
69 return _("unrecognized status code");
70}
#define _(x)
Definition: elog.c: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
Definition: regguts.h:323

References _, DB_IN_ARCHIVE_RECOVERY, DB_IN_CRASH_RECOVERY, DB_IN_PRODUCTION, DB_SHUTDOWNED, DB_SHUTDOWNED_IN_RECOVERY, DB_SHUTDOWNING, and DB_STARTUP.

Referenced by main().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 89 of file pg_controldata.c.

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 struct tm *tm_tmp;
101 char pgctime_str[128];
102 char ckpttime_str[128];
103 char mock_auth_nonce_str[MOCK_AUTH_NONCE_LEN * 2 + 1];
104 const char *strftime_fmt = "%c";
105 const char *progname;
106 char xlogfilename[MAXFNAMELEN];
107 int c;
108 int i;
109 int WalSegSz;
110
111 pg_logging_init(argv[0]);
112 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_controldata"));
113 progname = get_progname(argv[0]);
114
115 if (argc > 1)
116 {
117 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
118 {
120 exit(0);
121 }
122 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
123 {
124 puts("pg_controldata (PostgreSQL) " PG_VERSION);
125 exit(0);
126 }
127 }
128
129 while ((c = getopt_long(argc, argv, "D:", long_options, NULL)) != -1)
130 {
131 switch (c)
132 {
133 case 'D':
134 DataDir = optarg;
135 break;
136
137 default:
138 /* getopt_long already emitted a complaint */
139 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
140 exit(1);
141 }
142 }
143
144 if (DataDir == NULL)
145 {
146 if (optind < argc)
147 DataDir = argv[optind++];
148 else
149 DataDir = getenv("PGDATA");
150 }
151
152 /* Complain if any arguments remain */
153 if (optind < argc)
154 {
155 pg_log_error("too many command-line arguments (first is \"%s\")",
156 argv[optind]);
157 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
158 exit(1);
159 }
160
161 if (DataDir == NULL)
162 {
163 pg_log_error("no data directory specified");
164 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
165 exit(1);
166 }
167
168 /* get a copy of the control file */
170 if (!crc_ok)
171 {
172 pg_log_warning("calculated CRC checksum does not match value stored in control file");
173 pg_log_warning_detail("Either the control file is corrupt, or it has a different layout than this program "
174 "is expecting. The results below are untrustworthy.");
175 }
176
177 /* set wal segment size */
179
181 {
182 pg_log_warning(ngettext("invalid WAL segment size in control file (%d byte)",
183 "invalid WAL segment size in control file (%d bytes)",
184 WalSegSz),
185 WalSegSz);
186 pg_log_warning_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
187 pg_log_warning_detail("The file is corrupt and the results below are untrustworthy.");
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 tm_tmp = localtime(&time_tmp);
201
202 if (tm_tmp != NULL)
203 strftime(pgctime_str, sizeof(pgctime_str), strftime_fmt, tm_tmp);
204 else
205 snprintf(pgctime_str, sizeof(pgctime_str), _("???"));
206
207 time_tmp = (time_t) ControlFile->checkPointCopy.time;
208 tm_tmp = localtime(&time_tmp);
209
210 if (tm_tmp != NULL)
211 strftime(ckpttime_str, sizeof(ckpttime_str), strftime_fmt, tm_tmp);
212 else
213 snprintf(ckpttime_str, sizeof(ckpttime_str), _("???"));
214
215 /*
216 * Calculate name of the WAL file containing the latest checkpoint's REDO
217 * start point.
218 *
219 * A corrupted control file could report a WAL segment size of 0 or
220 * negative value, and to guard against division by zero, we need to treat
221 * that specially.
222 */
223 if (WalSegSz > 0)
224 {
225 XLogSegNo segno;
226
229 segno, WalSegSz);
230 }
231 else
232 strcpy(xlogfilename, _("???"));
233
234 for (i = 0; i < MOCK_AUTH_NONCE_LEN; i++)
235 snprintf(&mock_auth_nonce_str[i * 2], 3, "%02x",
236 (unsigned char) ControlFile->mock_authentication_nonce[i]);
237
238 printf(_("pg_control version number: %u\n"),
240 printf(_("Catalog version number: %u\n"),
242 printf(_("Database system identifier: %llu\n"),
243 (unsigned long long) ControlFile->system_identifier);
244 printf(_("Database cluster state: %s\n"),
246 printf(_("pg_control last modified: %s\n"),
247 pgctime_str);
248 printf(_("Latest checkpoint location: %X/%X\n"),
250 printf(_("Latest checkpoint's REDO location: %X/%X\n"),
252 printf(_("Latest checkpoint's REDO WAL file: %s\n"),
253 xlogfilename);
254 printf(_("Latest checkpoint's TimeLineID: %u\n"),
256 printf(_("Latest checkpoint's PrevTimeLineID: %u\n"),
258 printf(_("Latest checkpoint's full_page_writes: %s\n"),
259 ControlFile->checkPointCopy.fullPageWrites ? _("on") : _("off"));
260 printf(_("Latest checkpoint's NextXID: %u:%u\n"),
263 printf(_("Latest checkpoint's NextOID: %u\n"),
265 printf(_("Latest checkpoint's NextMultiXactId: %u\n"),
267 printf(_("Latest checkpoint's NextMultiOffset: %u\n"),
269 printf(_("Latest checkpoint's oldestXID: %u\n"),
271 printf(_("Latest checkpoint's oldestXID's DB: %u\n"),
273 printf(_("Latest checkpoint's oldestActiveXID: %u\n"),
275 printf(_("Latest checkpoint's oldestMultiXid: %u\n"),
277 printf(_("Latest checkpoint's oldestMulti's DB: %u\n"),
279 printf(_("Latest checkpoint's oldestCommitTsXid:%u\n"),
281 printf(_("Latest checkpoint's newestCommitTsXid:%u\n"),
283 printf(_("Time of latest checkpoint: %s\n"),
284 ckpttime_str);
285 printf(_("Fake LSN counter for unlogged rels: %X/%X\n"),
287 printf(_("Minimum recovery ending location: %X/%X\n"),
289 printf(_("Min recovery ending loc's timeline: %u\n"),
291 printf(_("Backup start location: %X/%X\n"),
293 printf(_("Backup end location: %X/%X\n"),
295 printf(_("End-of-backup record required: %s\n"),
296 ControlFile->backupEndRequired ? _("yes") : _("no"));
297 printf(_("wal_level setting: %s\n"),
299 printf(_("wal_log_hints setting: %s\n"),
300 ControlFile->wal_log_hints ? _("on") : _("off"));
301 printf(_("max_connections setting: %d\n"),
303 printf(_("max_worker_processes setting: %d\n"),
305 printf(_("max_wal_senders setting: %d\n"),
307 printf(_("max_prepared_xacts setting: %d\n"),
309 printf(_("max_locks_per_xact setting: %d\n"),
311 printf(_("track_commit_timestamp setting: %s\n"),
312 ControlFile->track_commit_timestamp ? _("on") : _("off"));
313 printf(_("Maximum data alignment: %u\n"),
315 /* we don't print floatFormat since can't say much useful about it */
316 printf(_("Database block size: %u\n"),
318 printf(_("Blocks per segment of large relation: %u\n"),
320 printf(_("WAL block size: %u\n"),
322 printf(_("Bytes per WAL segment: %u\n"),
324 printf(_("Maximum length of identifiers: %u\n"),
326 printf(_("Maximum columns in an index: %u\n"),
328 printf(_("Maximum size of a TOAST chunk: %u\n"),
330 printf(_("Size of a large-object chunk: %u\n"),
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(_("Float8 argument passing: %s\n"),
336 (ControlFile->float8ByVal ? _("by value") : _("by reference")));
337 printf(_("Data page checksum version: %u\n"),
339 printf(_("Default char data signedness: %s\n"),
340 (ControlFile->default_char_signedness ? _("signed") : _("unsigned")));
341 printf(_("Mock authentication nonce: %s\n"),
342 mock_auth_nonce_str);
343 return 0;
344}
static void usage(const char *progname)
static const char * dbState(DBState state)
static const char * wal_level_str(WalLevel wal_level)
#define ngettext(s, p, n)
Definition: c.h:1152
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1185
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
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:26
char * DataDir
Definition: globals.c:70
int i
Definition: isn.c:72
static struct pg_tm tm
Definition: localtime.c:104
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:44
#define MOCK_AUTH_NONCE_LEN
Definition: pg_control.h:28
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define snprintf
Definition: port.h:239
const char * get_progname(const char *argv0)
Definition: path.c:652
#define printf(...)
Definition: port.h:245
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:235
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
bool default_char_signedness
Definition: pg_control.h:228
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
#define EpochFromFullTransactionId(x)
Definition: transam.h:47
#define XidFromFullTransactionId(x)
Definition: transam.h:48
static ControlFileData * ControlFile
Definition: xlog.c:574
#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

References _, ControlFileData::backupEndPoint, ControlFileData::backupEndRequired, ControlFileData::backupStartPoint, ControlFileData::blcksz, ControlFileData::catalog_version_no, ControlFileData::checkPoint, ControlFileData::checkPointCopy, ControlFile, ControlFileData::data_checksum_version, DataDir, dbState(), ControlFileData::default_char_signedness, EpochFromFullTransactionId, ControlFileData::float8ByVal, CheckPoint::fullPageWrites, get_controlfile(), get_progname(), getopt_long(), i, ControlFileData::indexMaxKeys, IsValidWalSegSize, ControlFileData::loblksize, LSN_FORMAT_ARGS, ControlFileData::max_locks_per_xact, ControlFileData::max_prepared_xacts, ControlFileData::max_wal_senders, ControlFileData::max_worker_processes, ControlFileData::maxAlign, ControlFileData::MaxConnections, MAXFNAMELEN, ControlFileData::minRecoveryPoint, ControlFileData::minRecoveryPointTLI, MOCK_AUTH_NONCE_LEN, ControlFileData::mock_authentication_nonce, ControlFileData::nameDataLen, CheckPoint::newestCommitTsXid, CheckPoint::nextMulti, CheckPoint::nextMultiOffset, CheckPoint::nextOid, CheckPoint::nextXid, ngettext, CheckPoint::oldestActiveXid, CheckPoint::oldestCommitTsXid, CheckPoint::oldestMulti, CheckPoint::oldestMultiDB, CheckPoint::oldestXid, CheckPoint::oldestXidDB, optarg, optind, ControlFileData::pg_control_version, pg_log_error, pg_log_error_hint, pg_log_warning, pg_log_warning_detail, pg_logging_init(), PG_TEXTDOMAIN, CheckPoint::PrevTimeLineID, printf, progname, CheckPoint::redo, ControlFileData::relseg_size, required_argument, set_pglocale_pgservice(), snprintf, ControlFileData::state, ControlFileData::system_identifier, CheckPoint::ThisTimeLineID, CheckPoint::time, ControlFileData::time, tm, ControlFileData::toast_max_chunk_size, ControlFileData::track_commit_timestamp, ControlFileData::unloggedLSN, usage(), ControlFileData::wal_level, wal_level_str(), ControlFileData::wal_log_hints, WalSegSz, XidFromFullTransactionId, XLByteToSeg, ControlFileData::xlog_blcksz, ControlFileData::xlog_seg_size, and XLogFileName().

◆ usage()

static void usage ( const char *  progname)
static

Definition at line 33 of file pg_controldata.c.

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}

References _, printf, and progname.

Referenced by main().

◆ wal_level_str()

static const char * wal_level_str ( WalLevel  wal_level)
static

Definition at line 73 of file pg_controldata.c.

74{
75 switch (wal_level)
76 {
78 return "minimal";
80 return "replica";
82 return "logical";
83 }
84 return _("unrecognized \"wal_level\"");
85}
int wal_level
Definition: xlog.c:131
@ WAL_LEVEL_REPLICA
Definition: xlog.h:75
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76
@ WAL_LEVEL_MINIMAL
Definition: xlog.h:74

References _, wal_level, WAL_LEVEL_LOGICAL, WAL_LEVEL_MINIMAL, and WAL_LEVEL_REPLICA.

Referenced by get_wal_level_string(), main(), and xlog_desc().