1 /***********************************************************************************************************************************
2 PostgreSQL Interface
3 ***********************************************************************************************************************************/
4 #ifndef POSTGRES_INTERFACE_H
5 #define POSTGRES_INTERFACE_H
6 
7 #include <stdint.h>
8 #include <sys/types.h>
9 
10 #include "common/debug.h"
11 #include "common/type/string.h"
12 #include "storage/storage.h"
13 
14 /***********************************************************************************************************************************
15 Defines for various Postgres paths and files
16 ***********************************************************************************************************************************/
17 #define PG_FILE_BACKUPLABEL                                         "backup_label"
18 #define PG_FILE_BACKUPLABELOLD                                      "backup_label.old"
19 #define PG_FILE_BACKUPMANIFEST                                      "backup_manifest"
20 #define PG_FILE_BACKUPMANIFEST_TMP                                  "backup_manifest.tmp"
21 #define PG_FILE_PGCONTROL                                           "pg_control"
22 #define PG_FILE_PGFILENODEMAP                                       "pg_filenode.map"
23 #define PG_FILE_PGINTERNALINIT                                      "pg_internal.init"
24 #define PG_FILE_PGVERSION                                           "PG_VERSION"
25     STRING_DECLARE(PG_FILE_PGVERSION_STR);
26 #define PG_FILE_POSTGRESQLAUTOCONF                                  "postgresql.auto.conf"
27     STRING_DECLARE(PG_FILE_POSTGRESQLAUTOCONF_STR);
28 #define PG_FILE_POSTGRESQLAUTOCONFTMP                               "postgresql.auto.conf.tmp"
29 #define PG_FILE_POSTMASTEROPTS                                      "postmaster.opts"
30 #define PG_FILE_POSTMASTERPID                                       "postmaster.pid"
31     STRING_DECLARE(PG_FILE_POSTMASTERPID_STR);
32 #define PG_FILE_RECOVERYCONF                                        "recovery.conf"
33     STRING_DECLARE(PG_FILE_RECOVERYCONF_STR);
34 #define PG_FILE_RECOVERYDONE                                        "recovery.done"
35     STRING_DECLARE(PG_FILE_RECOVERYDONE_STR);
36 #define PG_FILE_RECOVERYSIGNAL                                      "recovery.signal"
37     STRING_DECLARE(PG_FILE_RECOVERYSIGNAL_STR);
38 #define PG_FILE_STANDBYSIGNAL                                       "standby.signal"
39     STRING_DECLARE(PG_FILE_STANDBYSIGNAL_STR);
40 #define PG_FILE_TABLESPACEMAP                                       "tablespace_map"
41 
42 #define PG_PATH_ARCHIVE_STATUS                                      "archive_status"
43 #define PG_PATH_BASE                                                "base"
44 #define PG_PATH_GLOBAL                                              "global"
45     STRING_DECLARE(PG_PATH_GLOBAL_STR);
46 #define PG_PATH_PGMULTIXACT                                         "pg_multixact"
47 #define PG_PATH_PGDYNSHMEM                                          "pg_dynshmem"
48 #define PG_PATH_PGNOTIFY                                            "pg_notify"
49 #define PG_PATH_PGREPLSLOT                                          "pg_replslot"
50 #define PG_PATH_PGSERIAL                                            "pg_serial"
51 #define PG_PATH_PGSNAPSHOTS                                         "pg_snapshots"
52 #define PG_PATH_PGSTATTMP                                           "pg_stat_tmp"
53 #define PG_PATH_PGSUBTRANS                                          "pg_subtrans"
54 #define PG_PATH_PGTBLSPC                                            "pg_tblspc"
55 
56 #define PG_PREFIX_PGSQLTMP                                          "pgsql_tmp"
57 
58 #define PG_NAME_WAL                                                 "wal"
59     STRING_DECLARE(PG_NAME_WAL_STR);
60 #define PG_NAME_XLOG                                                "xlog"
61     STRING_DECLARE(PG_NAME_XLOG_STR);
62 
63 /***********************************************************************************************************************************
64 Define default page size
65 
66 Page size can only be changed at compile time and is not known to be well-tested, so only the default page size is supported.
67 ***********************************************************************************************************************************/
68 #define PG_PAGE_SIZE_DEFAULT                                        ((unsigned int)(8 * 1024))
69 
70 /***********************************************************************************************************************************
71 Define the minimum oid that can be used for a user object
72 
73 Everything below this number should have been created at initdb time.
74 ***********************************************************************************************************************************/
75 #define PG_USER_OBJECT_MIN_ID                                       16384
76 
77 /***********************************************************************************************************************************
78 Define default segment size and pages per segment
79 
80 Segment size can only be changed at compile time and is not known to be well-tested, so only the default segment size is supported.
81 ***********************************************************************************************************************************/
82 #define PG_SEGMENT_SIZE_DEFAULT                                     ((unsigned int)(1 * 1024 * 1024 * 1024))
83 #define PG_SEGMENT_PAGE_DEFAULT                                     (PG_SEGMENT_SIZE_DEFAULT / PG_PAGE_SIZE_DEFAULT)
84 
85 /***********************************************************************************************************************************
86 WAL header size. It doesn't seem worth tracking the exact size of the WAL header across versions of PostgreSQL so just set it to
87 something far larger needed but <= the minimum read size on just about any system.
88 ***********************************************************************************************************************************/
89 #define PG_WAL_HEADER_SIZE                                          ((unsigned int)(512))
90 
91 /***********************************************************************************************************************************
92 Define default wal segment size
93 
94 Before PostgreSQL 11 WAL segment size could only be changed at compile time and is not known to be well-tested, so only the default
95 WAL segment size is supported for versions below 11.
96 ***********************************************************************************************************************************/
97 #define PG_WAL_SEGMENT_SIZE_DEFAULT                                 ((unsigned int)(16 * 1024 * 1024))
98 
99 /***********************************************************************************************************************************
100 PostgreSQL Control File Info
101 ***********************************************************************************************************************************/
102 typedef struct PgControl
103 {
104     unsigned int version;
105     uint64_t systemId;
106     unsigned int catalogVersion;
107 
108     unsigned int pageSize;
109     unsigned int walSegmentSize;
110 
111     bool pageChecksum;
112 } PgControl;
113 
114 /***********************************************************************************************************************************
115 PostgreSQL WAL Info
116 ***********************************************************************************************************************************/
117 typedef struct PgWal
118 {
119     unsigned int version;
120     unsigned int size;
121     uint64_t systemId;
122 } PgWal;
123 
124 /***********************************************************************************************************************************
125 Functions
126 ***********************************************************************************************************************************/
127 // Get info from pg_control
128 PgControl pgControlFromFile(const Storage *storage);
129 
130 // Get the control version for a PostgreSQL version
131 uint32_t pgControlVersion(unsigned int pgVersion);
132 
133 // Convert version string to version number and vice versa
134 unsigned int pgVersionFromStr(const String *version);
135 String *pgVersionToStr(unsigned int version);
136 
137 // Get info from WAL header
138 PgWal pgWalFromFile(const String *walFile, const Storage *storage);
139 PgWal pgWalFromBuffer(const Buffer *walBuffer);
140 
141 // Get the tablespace identifier used to distinguish versions in a tablespace directory, e.g. PG_9.0_201008051
142 String *pgTablespaceId(unsigned int pgVersion, unsigned int pgCatalogVersion);
143 
144 // Convert a string to an lsn and vice versa
145 uint64_t pgLsnFromStr(const String *lsn);
146 String *pgLsnToStr(uint64_t lsn);
147 
148 // Convert a timeline and lsn to a wal segment and vice versa
149 String *pgLsnToWalSegment(uint32_t timeline, uint64_t lsn, unsigned int walSegmentSize);
150 uint64_t pgLsnFromWalSegment(const String *walSegment, unsigned int walSegmentSize);
151 
152 // Convert a timeline and lsn range to a list of wal segments
153 StringList *pgLsnRangeToWalSegmentList(
154     unsigned int pgVersion, uint32_t timeline, uint64_t lsnStart, uint64_t lsnStop, unsigned int walSegmentSize);
155 
156 // Get name used for lsn in functions (this was changed in PostgreSQL 10 for consistency since lots of names were changing)
157 const String *pgLsnName(unsigned int pgVersion);
158 
159 // Calculate the checksum for a page. Page cannot be const because the page header is temporarily modified during processing.
160 uint16_t pgPageChecksum(unsigned char *page, uint32_t blockNo);
161 
162 const String *pgWalName(unsigned int pgVersion);
163 
164 // Get wal path (this was changed in PostgreSQL 10 to avoid including "log" in the name)
165 const String *pgWalPath(unsigned int pgVersion);
166 
167 // Get transaction commit log path (this was changed in PostgreSQL 10 to avoid including "log" in the name)
168 const String *pgXactPath(unsigned int pgVersion);
169 
170 /***********************************************************************************************************************************
171 Macros for function logging
172 ***********************************************************************************************************************************/
173 String *pgControlToLog(const PgControl *pgControl);
174 String *pgWalToLog(const PgWal *pgWal);
175 
176 #define FUNCTION_LOG_PG_CONTROL_TYPE                                                                                               \
177     PgControl
178 #define FUNCTION_LOG_PG_CONTROL_FORMAT(value, buffer, bufferSize)                                                                  \
179     FUNCTION_LOG_STRING_OBJECT_FORMAT(&value, pgControlToLog, buffer, bufferSize)
180 
181 #define FUNCTION_LOG_PG_WAL_TYPE                                                                                                   \
182     PgWal
183 #define FUNCTION_LOG_PG_WAL_FORMAT(value, buffer, bufferSize)                                                                      \
184     FUNCTION_LOG_STRING_OBJECT_FORMAT(&value, pgWalToLog, buffer, bufferSize)
185 
186 #endif
187