forked from duckdb/duckdb-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_catalog.cpp
More file actions
181 lines (160 loc) · 6.43 KB
/
postgres_catalog.cpp
File metadata and controls
181 lines (160 loc) · 6.43 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
#include "storage/postgres_catalog.hpp"
#include "storage/postgres_schema_entry.hpp"
#include "storage/postgres_transaction.hpp"
#include "postgres_connection.hpp"
#include "duckdb/storage/database_size.hpp"
#include "duckdb/parser/parsed_data/drop_info.hpp"
#include "duckdb/parser/parsed_data/create_schema_info.hpp"
#include "duckdb/main/attached_database.hpp"
#include "duckdb/main/secret/secret_manager.hpp"
namespace duckdb {
PostgresCatalog::PostgresCatalog(AttachedDatabase &db_p, string connection_string_p, string attach_path_p,
AccessMode access_mode, string schema_to_load, PostgresIsolationLevel isolation_level)
: Catalog(db_p), connection_string(std::move(connection_string_p)), attach_path(std::move(attach_path_p)),
access_mode(access_mode), isolation_level(isolation_level), schemas(*this, schema_to_load),
connection_pool(*this), default_schema(schema_to_load) {
if (default_schema.empty()) {
default_schema = "public";
}
Value connection_limit;
auto &db_instance = db_p.GetDatabase();
if (db_instance.TryGetCurrentSetting("pg_connection_limit", connection_limit)) {
connection_pool.SetMaximumConnections(UBigIntValue::Get(connection_limit));
}
auto connection = connection_pool.GetConnection();
this->version = connection.GetConnection().GetPostgresVersion();
}
string EscapeConnectionString(const string &input) {
string result = "'";
for (auto c : input) {
if (c == '\\') {
result += "\\\\";
} else if (c == '\'') {
result += "\\'";
} else {
result += c;
}
}
result += "'";
return result;
}
string AddConnectionOption(const KeyValueSecret &kv_secret, const string &name) {
Value input_val = kv_secret.TryGetValue(name);
if (input_val.IsNull()) {
// not provided
return string();
}
string result;
result += name;
result += "=";
result += EscapeConnectionString(input_val.ToString());
result += " ";
return result;
}
unique_ptr<SecretEntry> GetSecret(ClientContext &context, const string &secret_name) {
auto &secret_manager = SecretManager::Get(context);
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context);
// FIXME: this should be adjusted once the `GetSecretByName` API supports this use case
auto secret_entry = secret_manager.GetSecretByName(transaction, secret_name, "memory");
if (secret_entry) {
return secret_entry;
}
secret_entry = secret_manager.GetSecretByName(transaction, secret_name, "local_file");
if (secret_entry) {
return secret_entry;
}
return nullptr;
}
string PostgresCatalog::GetConnectionString(ClientContext &context, const string &attach_path, string secret_name) {
// if no secret is specified we default to the unnamed postgres secret, if it exists
string connection_string = attach_path;
bool explicit_secret = !secret_name.empty();
if (!explicit_secret) {
// look up settings from the default unnamed postgres secret if none is provided
secret_name = "__default_postgres";
}
auto secret_entry = GetSecret(context, secret_name);
if (secret_entry) {
// secret found - read data
const auto &kv_secret = dynamic_cast<const KeyValueSecret &>(*secret_entry->secret);
string new_connection_info;
new_connection_info += AddConnectionOption(kv_secret, "user");
new_connection_info += AddConnectionOption(kv_secret, "password");
new_connection_info += AddConnectionOption(kv_secret, "host");
new_connection_info += AddConnectionOption(kv_secret, "port");
new_connection_info += AddConnectionOption(kv_secret, "dbname");
connection_string = new_connection_info + connection_string;
} else if (explicit_secret) {
// secret not found and one was explicitly provided - throw an error
throw BinderException("Secret with name \"%s\" not found", secret_name);
}
return connection_string;
}
PostgresCatalog::~PostgresCatalog() = default;
void PostgresCatalog::Initialize(bool load_builtin) {
}
optional_ptr<CatalogEntry> PostgresCatalog::CreateSchema(CatalogTransaction transaction, CreateSchemaInfo &info) {
auto &postgres_transaction = PostgresTransaction::Get(transaction.GetContext(), *this);
auto entry = schemas.GetEntry(transaction.GetContext(), info.schema);
if (entry) {
switch (info.on_conflict) {
case OnCreateConflict::REPLACE_ON_CONFLICT: {
DropInfo try_drop;
try_drop.type = CatalogType::SCHEMA_ENTRY;
try_drop.name = info.schema;
try_drop.if_not_found = OnEntryNotFound::RETURN_NULL;
try_drop.cascade = false;
schemas.DropEntry(transaction.GetContext(), try_drop);
break;
}
case OnCreateConflict::IGNORE_ON_CONFLICT:
return entry;
case OnCreateConflict::ERROR_ON_CONFLICT:
default:
throw BinderException("Failed to create schema \"%s\": schema already exists", info.schema);
}
}
return schemas.CreateSchema(transaction.GetContext(), info);
}
void PostgresCatalog::DropSchema(ClientContext &context, DropInfo &info) {
return schemas.DropEntry(context, info);
}
void PostgresCatalog::ScanSchemas(ClientContext &context, std::function<void(SchemaCatalogEntry &)> callback) {
schemas.Scan(context, [&](CatalogEntry &schema) { callback(schema.Cast<PostgresSchemaEntry>()); });
}
optional_ptr<SchemaCatalogEntry> PostgresCatalog::LookupSchema(CatalogTransaction transaction,
const EntryLookupInfo &schema_lookup,
OnEntryNotFound if_not_found) {
auto schema_name = schema_lookup.GetEntryName();
auto &postgres_transaction = PostgresTransaction::Get(transaction.GetContext(), *this);
if (schema_name == "pg_temp") {
schema_name = postgres_transaction.GetTemporarySchema();
}
auto entry = schemas.GetEntry(transaction.GetContext(), schema_name);
if (!entry && if_not_found != OnEntryNotFound::RETURN_NULL) {
throw BinderException("Schema with name \"%s\" not found", schema_name);
}
return reinterpret_cast<SchemaCatalogEntry *>(entry.get());
}
bool PostgresCatalog::InMemory() {
return false;
}
string PostgresCatalog::GetDBPath() {
return attach_path;
}
DatabaseSize PostgresCatalog::GetDatabaseSize(ClientContext &context) {
auto &postgres_transaction = PostgresTransaction::Get(context, *this);
auto result = postgres_transaction.Query("SELECT pg_database_size(current_database());");
DatabaseSize size;
size.free_blocks = 0;
size.total_blocks = 0;
size.used_blocks = 0;
size.wal_size = 0;
size.block_size = 0;
size.bytes = result->GetInt64(0, 0);
return size;
}
void PostgresCatalog::ClearCache() {
schemas.ClearEntries();
}
} // namespace duckdb