aboutsummaryrefslogtreecommitdiff
path: root/contrib/pg_controldata/pg_controldata.c
blob: 93fe9a54c9b5cd3596d03df329e7996815302543 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * pg_controldata
 *
 * reads the data from $PGDATA/global/pg_control
 *
 * copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
 * licence: BSD
 *
 * $Header: /cvsroot/pgsql/contrib/pg_controldata/Attic/pg_controldata.c,v 1.3 2001/03/22 03:59:09 momjian Exp $
 */
#include "postgres.h"

#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "catalog/pg_control.h"


static const char *
dbState(DBState state)
{
	switch (state)
	{
			case DB_STARTUP:
			return "STARTUP";
		case DB_SHUTDOWNED:
			return "SHUTDOWNED";
		case DB_SHUTDOWNING:
			return "SHUTDOWNING";
		case DB_IN_RECOVERY:
			return "IN_RECOVERY";
		case DB_IN_PRODUCTION:
			return "IN_PRODUCTION";
	}
	return "unrecognized status code";
}


int
main()
{
	ControlFileData ControlFile;
	int			fd;
	char		ControlFilePath[MAXPGPATH];
	char	   *DataDir;
	crc64		crc;
	char		pgctime_str[32];
	char		ckpttime_str[32];

	DataDir = getenv("PGDATA");
	if (DataDir == NULL)
	{
		fprintf(stderr, "PGDATA is not defined\n");
		exit(1);
	}

	snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);

	if ((fd = open(ControlFilePath, O_RDONLY)) == -1)
	{
		perror("Failed to open $PGDATA/global/pg_control for reading");
		exit(2);
	}

	if (read(fd, &ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
	{
		perror("Failed to read $PGDATA/global/pg_control");
		exit(2);
	}
	close(fd);

	/* Check the CRC. */
	INIT_CRC64(crc);
	COMP_CRC64(crc,
			   (char *) &ControlFile + sizeof(crc64),
			   sizeof(ControlFileData) - sizeof(crc64));
	FIN_CRC64(crc);

	if (!EQ_CRC64(crc, ControlFile.crc))
		printf("WARNING: Calculated CRC checksum does not match value stored in file.\n"
			   "Either the file is corrupt, or it has a different layout than this program\n"
			   "is expecting.  The results below are untrustworthy.\n\n");

	strftime(pgctime_str, 32, "%c",
			 localtime(&(ControlFile.time)));
	strftime(ckpttime_str, 32, "%c",
			 localtime(&(ControlFile.checkPointCopy.time)));

	printf("pg_control version number:            %u\n"
		   "Catalog version number:               %u\n"
		   "Database state:                       %s\n"
		   "pg_control last modified:             %s\n"
		   "Current log file id:                  %u\n"
		   "Next log file segment:                %u\n"
		   "Latest checkpoint location:           %X/%X\n"
		   "Prior checkpoint location:            %X/%X\n"
		   "Latest checkpoint's REDO location:    %X/%X\n"
		   "Latest checkpoint's UNDO location:    %X/%X\n"
		   "Latest checkpoint's StartUpID:        %u\n"
		   "Latest checkpoint's NextXID:          %u\n"
		   "Latest checkpoint's NextOID:          %u\n"
		   "Time of latest checkpoint:            %s\n"
		   "Database block size:                  %u\n"
		   "Blocks per segment of large relation: %u\n"
		   "LC_COLLATE:                           %s\n"
		   "LC_CTYPE:                             %s\n",

		   ControlFile.pg_control_version,
		   ControlFile.catalog_version_no,
		   dbState(ControlFile.state),
		   pgctime_str,
		   ControlFile.logId,
		   ControlFile.logSeg,
		   ControlFile.checkPoint.xlogid,
		   ControlFile.checkPoint.xrecoff,
		   ControlFile.prevCheckPoint.xlogid,
		   ControlFile.prevCheckPoint.xrecoff,
		   ControlFile.checkPointCopy.redo.xlogid,
		   ControlFile.checkPointCopy.redo.xrecoff,
		   ControlFile.checkPointCopy.undo.xlogid,
		   ControlFile.checkPointCopy.undo.xrecoff,
		   ControlFile.checkPointCopy.ThisStartUpID,
		   ControlFile.checkPointCopy.nextXid,
		   ControlFile.checkPointCopy.nextOid,
		   ckpttime_str,
		   ControlFile.blcksz,
		   ControlFile.relseg_size,
		   ControlFile.lc_collate,
		   ControlFile.lc_ctype);

	return (0);
}