1 /***********************************************************************************************************************************
2 Database Client
3 
4 Implements the required PostgreSQL queries and commands.  Notice that there is no general purpose query function -- all queries are
5 expected to be embedded in this object.
6 ***********************************************************************************************************************************/
7 #ifndef DB_DB_H
8 #define DB_DB_H
9 
10 #include "common/type/object.h"
11 #include "postgres/client.h"
12 #include "protocol/client.h"
13 
14 /***********************************************************************************************************************************
15 Object type
16 ***********************************************************************************************************************************/
17 typedef struct Db Db;
18 
19 /***********************************************************************************************************************************
20 Constructors
21 ***********************************************************************************************************************************/
22 Db *dbNew(PgClient *client, ProtocolClient *remoteClient, const String *applicationName);
23 
24 /***********************************************************************************************************************************
25 Getters/Setters
26 ***********************************************************************************************************************************/
27 typedef struct DbPub
28 {
29     MemContext *memContext;                                         // Mem context
30     const String *archiveMode;                                      // The archive_mode reported by the database
31     const String *archiveCommand;                                   // The archive_command reported by the database
32     const String *pgDataPath;                                       // Data directory reported by the database
33     unsigned int pgVersion;                                         // Version as reported by the database
34 } DbPub;
35 
36 // Archive mode loaded from the archive_mode GUC
37 __attribute__((always_inline)) static inline const String *
dbArchiveMode(const Db * const this)38 dbArchiveMode(const Db *const this)
39 {
40     return THIS_PUB(Db)->archiveMode;
41 }
42 
43 // Archive command loaded from the archive_command GUC
44 __attribute__((always_inline)) static inline const String *
dbArchiveCommand(const Db * const this)45 dbArchiveCommand(const Db *const this)
46 {
47     return THIS_PUB(Db)->archiveCommand;
48 }
49 
50 // Data path loaded from the data_directory GUC
51 __attribute__((always_inline)) static inline const String *
dbPgDataPath(const Db * const this)52 dbPgDataPath(const Db *const this)
53 {
54     return THIS_PUB(Db)->pgDataPath;
55 }
56 
57 // Version loaded from the server_version_num GUC
58 __attribute__((always_inline)) static inline unsigned int
dbPgVersion(const Db * const this)59 dbPgVersion(const Db *const this)
60 {
61     return THIS_PUB(Db)->pgVersion;
62 }
63 
64 /***********************************************************************************************************************************
65 Functions
66 ***********************************************************************************************************************************/
67 // Open the db connection
68 void dbOpen(Db *this);
69 
70 // Start backup and return starting lsn and wal segment name
71 typedef struct DbBackupStartResult
72 {
73     String *lsn;
74     String *walSegmentName;
75 } DbBackupStartResult;
76 
77 DbBackupStartResult dbBackupStart(Db *this, bool startFast, bool stopAuto);
78 
79 // Stop backup and return starting lsn, wal segment name, backup label, and tablspace map
80 typedef struct DbBackupStopResult
81 {
82     String *lsn;
83     String *walSegmentName;
84     String *backupLabel;
85     String *tablespaceMap;
86 } DbBackupStopResult;
87 
88 DbBackupStopResult dbBackupStop(Db *this);
89 
90 // Is this cluster a standby?
91 bool dbIsStandby(Db *this);
92 
93 // Get list of databases in the cluster: select oid, datname, datlastsysoid from pg_database
94 VariantList *dbList(Db *this);
95 
96 // Waits for replay on the standby to equal the target LSN
97 void dbReplayWait(Db *this, const String *targetLsn, TimeMSec timeout);
98 
99 // Epoch time on the PostgreSQL host in ms
100 TimeMSec dbTimeMSec(Db *this);
101 
102 // Get list of tablespaces in the cluster: select oid, datname, datlastsysoid from pg_database
103 VariantList *dbTablespaceList(Db *this);
104 
105 // Switch the WAL segment and return the segment that should have been archived
106 String *dbWalSwitch(Db *this);
107 void dbClose(Db *this);
108 
109 // Move to a new parent mem context
110 __attribute__((always_inline)) static inline Db *
dbMove(Db * const this,MemContext * const parentNew)111 dbMove(Db *const this, MemContext *const parentNew)
112 {
113     return objMove(this, parentNew);
114 }
115 
116 /***********************************************************************************************************************************
117 Destructor
118 ***********************************************************************************************************************************/
119 __attribute__((always_inline)) static inline void
dbFree(Db * const this)120 dbFree(Db *const this)
121 {
122     objFree(this);
123 }
124 
125 /***********************************************************************************************************************************
126 Macros for function logging
127 ***********************************************************************************************************************************/
128 String *dbToLog(const Db *this);
129 
130 #define FUNCTION_LOG_DB_TYPE                                                                                                       \
131     Db *
132 #define FUNCTION_LOG_DB_FORMAT(value, buffer, bufferSize)                                                                          \
133     FUNCTION_LOG_STRING_OBJECT_FORMAT(value, dbToLog, buffer, bufferSize)
134 
135 #endif
136