Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***********************************************************************************************************************************
Database Client
 
Implements the required PostgreSQL queries and commands. Notice that there is no general purpose query function -- all queries are
expected to be embedded in this object.
***********************************************************************************************************************************/
#ifndef DB_DB_H
#define DB_DB_H
 
#include "common/type/object.h"
#include "postgres/client.h"
#include "postgres/interface.h"
#include "protocol/client.h"
#include "storage/storage.h"
 
/***********************************************************************************************************************************
Object type
***********************************************************************************************************************************/
typedef struct Db Db;
 
/***********************************************************************************************************************************
Constructors
***********************************************************************************************************************************/
FN_EXTERN Db *dbNew(PgClient *client, ProtocolClient *remoteClient, const Storage *storage, const String *applicationName);
 
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
typedef struct DbPub
{
    MemContext *memContext;                                         // Mem context
    PgControl pgControl;                                            // Control info
    const String *archiveMode;                                      // The archive_mode reported by the database
    const String *archiveCommand;                                   // The archive_command reported by the database
    const String *pgDataPath;                                       // Data directory reported by the database
    bool standby;                                                   // Is the cluster a standby?
    unsigned int pgVersion;                                         // Version as reported by the database
    TimeMSec checkpointTimeout;                                     // The checkpoint timeout reported by the database
    TimeMSec dbTimeout;                                             // Database timeout for statements/queries from PG client
} DbPub;
 
// Archive mode loaded from the archive_mode GUC
FN_INLINE_ALWAYS const String *
dbArchiveMode(const Db *const this)
{
    return THIS_PUB(Db)->archiveMode;
}
 
// Archive command loaded from the archive_command GUC
FN_INLINE_ALWAYS const String *
dbArchiveCommand(const Db *const this)
{
    return THIS_PUB(Db)->archiveCommand;
}
 
// Control data
FN_INLINE_ALWAYS PgControl
dbPgControl(const Db *const this)
{
    return THIS_PUB(Db)->pgControl;
}
 
// Data path loaded from the data_directory GUC
FN_INLINE_ALWAYS const String *
dbPgDataPath(const Db *const this)
{
    return THIS_PUB(Db)->pgDataPath;
}
 
// Is the cluster a standby?
FN_INLINE_ALWAYS bool
dbIsStandby(const Db *const this)
{
    return THIS_PUB(Db)->standby;
}
 
// Version loaded from the server_version_num GUC
FN_INLINE_ALWAYS unsigned int
dbPgVersion(const Db *const this)
{
    return THIS_PUB(Db)->pgVersion;
}
 
// Checkpoint timeout loaded from the checkpoint_timeout GUC
FN_INLINE_ALWAYS TimeMSec
dbCheckpointTimeout(const Db *const this)
{
    return THIS_PUB(Db)->checkpointTimeout;
}
 
// Database timeout from main/remote process
FN_INLINE_ALWAYS TimeMSec
dbDbTimeout(const Db *const this)
{
    return THIS_PUB(Db)->dbTimeout;
}
 
/***********************************************************************************************************************************
Functions
***********************************************************************************************************************************/
// Open the db connection
FN_EXTERN void dbOpen(Db *this);
 
// Start backup and return starting lsn and wal segment name
typedef struct DbBackupStartResult
{
    String *lsn;
    String *walSegmentName;
    String *walSegmentCheck;                                        // Segment used to check archiving, may be NULL
} DbBackupStartResult;
 
FN_EXTERN DbBackupStartResult dbBackupStart(Db *this, bool startFast, bool archiveCheck);
 
// Stop backup and return starting lsn, wal segment name, backup label, and tablespace map
typedef struct DbBackupStopResult
{
    String *lsn;
    String *walSegmentName;
    String *backupLabel;
    String *tablespaceMap;
} DbBackupStopResult;
 
FN_EXTERN DbBackupStopResult dbBackupStop(Db *this);
 
// Get list of databases in the cluster: select oid, datname, datlastsysoid from pg_database
FN_EXTERN Pack *dbList(Db *this);
 
// Waits for replay on the standby to equal the target LSN
FN_EXTERN void dbReplayWait(Db *this, const String *targetLsn, uint32_t targetTimeline, TimeMSec timeout);
 
// Check that the cluster is alive and correctly configured during the backup
FN_EXTERN void dbPing(Db *this, bool force);
 
// Epoch time on the PostgreSQL host in ms
FN_EXTERN TimeMSec dbTimeMSec(Db *this);
 
// Get list of tablespaces in the cluster: select oid, datname, datlastsysoid from pg_database
FN_EXTERN Pack *dbTablespaceList(Db *this);
 
// Switch the WAL segment and return the segment that should have been archived
FN_EXTERN String *dbWalSwitch(Db *this);
 
// Move to a new parent mem context
FN_INLINE_ALWAYS Db *
dbMove(Db *const this, MemContext *const parentNew)
{
    return objMove(this, parentNew);
}
 
/***********************************************************************************************************************************
Destructor
***********************************************************************************************************************************/
FN_INLINE_ALWAYS void
dbFree(Db *const this)
{
    objFree(this);
}
 
/***********************************************************************************************************************************
Macros for function logging
***********************************************************************************************************************************/
FN_EXTERN void dbToLog(const Db *this, StringStatic *debugLog);
 
#define FUNCTION_LOG_DB_TYPE                                                                                                       \
    Db *
#define FUNCTION_LOG_DB_FORMAT(value, buffer, bufferSize)                                                                          \
    FUNCTION_LOG_OBJECT_FORMAT(value, dbToLog, buffer, bufferSize)
 
#endif