|
| 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 | + |
0 commit comments