Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b25e60d

Browse files
committed
I had a need to read such things as the backend locale and the catalog
version number from the current database, and couldn't find any existing program to do that. linda:~$ pg_controldata Log file id: 0 Log file segment: 5 Last modified: Wed Feb 7 19:35:47 2001 Database block size: 8192 Blocks per segment of large relation: 131072 Catalog version number: 200101061 LC_COLLATE: en_GB LC_CTYPE: en_GB Log archive directory: Oliver Elphick Oliver.Elphick@lfix.co.uk
1 parent e74ce0a commit b25e60d

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed

contrib/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ oid2name -
8585
maps numeric files to table names
8686
by B Palmer <bpalmer@crimelabs.net>
8787

88+
pg_controldata -
89+
Dump internal database site structures
90+
by Oliver Elphick <olly@lfix.co.uk>
91+
8892
pg_dumplo -
8993
Dump large objects
9094
by Karel Zak <zakkr@zf.jcu.cz>

contrib/pg_controldata/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# $Header: /cvsroot/pgsql/contrib/pg_controldata/Attic/Makefile,v 1.1 2001/02/23 20:38:35 momjian Exp $
3+
#
4+
5+
subdir = contrib/pg_controldata
6+
top_builddir = ../..
7+
include $(top_builddir)/src/Makefile.global
8+
9+
override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
10+
11+
OBJS = pg_controldata.o
12+
13+
all: pg_controldata
14+
15+
pg_controldata: $(OBJS) $(libpq_builddir)/libpq.a
16+
$(CC) $(CFLAGS) $(OBJS) $(libpq) $(LDFLAGS) $(LIBS) -o $@
17+
18+
install: all installdirs
19+
$(INSTALL_PROGRAM) pg_controldata$(X) $(bindir)
20+
$(INSTALL_DATA) README.pg_controldata $(docdir)/contrib
21+
22+
installdirs:
23+
$(mkinstalldirs) $(bindir) $(docdir)/contrib
24+
25+
uninstall:
26+
rm -f $(bindir)/pg_controldata$(X) $(docdir)/contrib/README.pg_controldata
27+
28+
clean distclean maintainer-clean:
29+
rm -f pg_controldata$(X) $(OBJS)
30+
31+
depend dep:
32+
$(CC) -MM -MG $(CFLAGS) *.c > depend
33+
34+
ifeq (depend,$(wildcard depend))
35+
include depend
36+
endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
I had a need to read such things as the backend locale and the catalog
2+
version number from the current database, and couldn't find any existing
3+
program to do that.
4+
5+
The attached utility produces this output:
6+
7+
linda:~$ pg_controldata
8+
Log file id: 0
9+
Log file segment: 5
10+
Last modified: Wed Feb 7 19:35:47 2001
11+
Database block size: 8192
12+
Blocks per segment of large relation: 131072
13+
Catalog version number: 200101061
14+
LC_COLLATE: en_GB
15+
LC_CTYPE: en_GB
16+
Log archive directory:
17+
18+
--
19+
Oliver Elphick <olly@lfix.co.uk>

contrib/pg_controldata/pg_controldata

11.5 KB
Binary file not shown.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/* pg_controldata
2+
*
3+
* reads the data from $PGDATA/global/pg_control
4+
*
5+
* copyright (c) Oliver Elphick <olly@lfix.co.uk>, 2001;
6+
* licence: BSD
7+
*
8+
*/
9+
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <unistd.h>
13+
#include <time.h>
14+
#include <sys/types.h>
15+
#include <sys/stat.h>
16+
#include <fcntl.h>
17+
18+
19+
typedef unsigned int uint32;
20+
21+
#include "config.h"
22+
#include "access/xlogdefs.h"
23+
24+
/*
25+
* #include "access/xlog.h"
26+
* #include "c.h"
27+
*/
28+
29+
/* The following definitions are extracted from access/xlog.h and its
30+
* recursive includes. There is too much initialisation needed if
31+
* they are included direct. Perhaps someone more knowledgeable can
32+
* fix that.
33+
*/
34+
typedef struct crc64
35+
{
36+
uint32 crc1;
37+
uint32 crc2;
38+
} crc64;
39+
40+
#define LOCALE_NAME_BUFLEN 128
41+
42+
typedef enum DBState
43+
{
44+
DB_STARTUP = 0,
45+
DB_SHUTDOWNED,
46+
DB_SHUTDOWNING,
47+
DB_IN_RECOVERY,
48+
DB_IN_PRODUCTION
49+
} DBState;
50+
51+
52+
typedef struct ControlFileData
53+
{
54+
crc64 crc;
55+
uint32 logId; /* current log file id */
56+
uint32 logSeg; /* current log file segment (1-based) */
57+
struct
58+
XLogRecPtr checkPoint; /* last check point record ptr */
59+
time_t time; /* time stamp of last modification */
60+
DBState state; /* see enum above */
61+
62+
/*
63+
* this data is used to make sure that configuration of this DB is
64+
* compatible with the backend executable
65+
*/
66+
uint32 blcksz; /* block size for this DB */
67+
uint32 relseg_size; /* blocks per segment of large relation */
68+
uint32 catalog_version_no; /* internal version number */
69+
/* active locales --- "C" if compiled without USE_LOCALE: */
70+
char lc_collate[LOCALE_NAME_BUFLEN];
71+
char lc_ctype[LOCALE_NAME_BUFLEN];
72+
73+
/*
74+
* important directory locations
75+
*/
76+
char archdir[MAXPGPATH]; /* where to move offline log files */
77+
} ControlFileData;
78+
79+
int main() {
80+
ControlFileData ControlFile;
81+
int fd;
82+
char ControlFilePath[MAXPGPATH];
83+
char *DataDir;
84+
char tmdt[32];
85+
86+
DataDir = getenv("PGDATA");
87+
if ( DataDir == NULL ) {
88+
fprintf(stderr,"PGDATA is not defined\n");
89+
exit(1);
90+
}
91+
92+
snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
93+
94+
if ((fd = open(ControlFilePath, O_RDONLY)) == -1) {
95+
perror("Failed to open $PGDATA/global/pg_control for reading");
96+
exit(2);
97+
}
98+
99+
read(fd, &ControlFile, sizeof(ControlFileData));
100+
strftime(tmdt, 32, "%c", localtime(&(ControlFile.time)));
101+
102+
printf("Log file id: %u\n"
103+
"Log file segment: %u\n"
104+
"Last modified: %s\n"
105+
"Database block size: %u\n"
106+
"Blocks per segment of large relation: %u\n"
107+
"Catalog version number: %u\n"
108+
"LC_COLLATE: %s\n"
109+
"LC_CTYPE: %s\n"
110+
"Log archive directory: %s\n",
111+
ControlFile.logId,
112+
ControlFile.logSeg,
113+
tmdt,
114+
ControlFile.blcksz,
115+
ControlFile.relseg_size,
116+
ControlFile.catalog_version_no,
117+
ControlFile.lc_collate,
118+
ControlFile.lc_ctype,
119+
ControlFile.archdir);
120+
121+
return (0);
122+
}
123+
8.17 KB
Binary file not shown.

0 commit comments

Comments
 (0)