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