-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathpgduckdb_duckdb.cpp
More file actions
422 lines (353 loc) · 14.4 KB
/
pgduckdb_duckdb.cpp
File metadata and controls
422 lines (353 loc) · 14.4 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include <filesystem>
#include "duckdb.hpp"
#include "duckdb/common/exception.hpp"
#include "duckdb/parser/parsed_data/create_table_function_info.hpp"
#include "pgduckdb/catalog/pgduckdb_storage.hpp"
#include "pgduckdb/pg/guc.hpp"
#include "pgduckdb/pg/permissions.hpp"
#include "pgduckdb/pg/string_utils.hpp"
#include "pgduckdb/pg/transactions.hpp"
#include "pgduckdb/pgduckdb_background_worker.hpp"
#include "pgduckdb/pgduckdb_fdw.hpp"
#include "pgduckdb/pgduckdb_guc.hpp"
#include "pgduckdb/pgduckdb_metadata_cache.hpp"
#include "pgduckdb/pgduckdb_extensions.hpp"
#include "pgduckdb/pgduckdb_secrets_helper.hpp"
#include "pgduckdb/pgduckdb_unsupported_type_optimizer.hpp"
#include "pgduckdb/pgduckdb_userdata_cache.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/pgduckdb_xact.hpp"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"
#include "pgduckdb/utility/signal_guard.hpp"
#include "pgduckdb/vendor/pg_list.hpp"
extern "C" {
#include "postgres.h"
#include "catalog/namespace.h"
#include "common/file_perm.h"
#include "lib/stringinfo.h"
#include "miscadmin.h" // superuser
#include "nodes/value.h" // strVal
#include "utils/fmgrprotos.h" // pg_sequence_last_value
#include "utils/lsyscache.h" // get_relname_relid
}
namespace pgduckdb {
const char *
GetSessionHint() {
if (!IsEmptyString(duckdb_motherduck_session_hint)) {
return duckdb_motherduck_session_hint;
}
return PossiblyReuseBgwSessionHint();
}
namespace ddb {
bool
DidWrites() {
if (!DuckDBManager::IsInitialized()) {
return false;
}
auto connection = DuckDBManager::GetConnectionUnsafe();
auto &context = *connection->context;
return DidWrites(context);
}
bool
DidWrites(duckdb::ClientContext &context) {
if (!context.transaction.HasActiveTransaction()) {
return false;
}
return context.ActiveTransaction().ModifiedDatabase() != nullptr;
}
} // namespace ddb
DuckDBManager DuckDBManager::manager_instance;
template <typename T>
std::string
ToString(T value) {
return std::to_string(value);
}
template <>
std::string
ToString(char *value) {
return std::string(value);
}
#define SET_DUCKDB_OPTION(ddb_option_name) \
config.options.ddb_option_name = duckdb_##ddb_option_name; \
elog(DEBUG2, "[PGDuckDB] Set DuckDB option: '" #ddb_option_name "'=%s", ToString(duckdb_##ddb_option_name).c_str());
void
DuckDBManager::Initialize() {
elog(DEBUG2, "(PGDuckDB/DuckDBManager) Creating DuckDB instance");
// Block signals before initializing DuckDB to ensure signal is handled by the Postgres main thread only
pgduckdb::ThreadSignalBlockGuard guard;
// Make sure directories provided in config exists
std::filesystem::create_directories(duckdb_temporary_directory);
std::filesystem::create_directories(duckdb_extension_directory);
duckdb::DBConfig config;
std::string user_agent = "pg_duckdb";
if (!IsEmptyString(duckdb_custom_user_agent)) {
user_agent += ", ";
user_agent += duckdb_custom_user_agent;
}
const char *application_name = pg::GetConfigOption("application_name", true);
if (!IsEmptyString(application_name)) {
user_agent += ", ";
user_agent += application_name;
}
config.SetOptionByName("custom_user_agent", user_agent);
config.SetOptionByName("default_null_order", "postgres");
SET_DUCKDB_OPTION(allow_unsigned_extensions);
SET_DUCKDB_OPTION(allow_community_extensions);
SET_DUCKDB_OPTION(autoinstall_known_extensions);
SET_DUCKDB_OPTION(autoload_known_extensions);
SET_DUCKDB_OPTION(temporary_directory);
SET_DUCKDB_OPTION(extension_directory);
if (duckdb_maximum_memory > 0) {
// Convert the memory limit from MB (as set by Postgres GUC_UNIT_MB, which is actually MiB; see
// memory_unit_conversion_table in guc.c) to a string with the "MiB" suffix, as required by DuckDB's memory
// parser. This ensures the value is interpreted correctly by DuckDB.
std::string memory_limit = std::to_string(duckdb_maximum_memory) + "MiB";
config.options.maximum_memory = duckdb::DBConfig::ParseMemoryLimit(memory_limit);
elog(DEBUG2, "[PGDuckDB] Set DuckDB option: 'maximum_memory'=%dMB", duckdb_maximum_memory);
}
if (duckdb_max_temp_directory_size != NULL && strlen(duckdb_max_temp_directory_size) != 0) {
config.SetOptionByName("max_temp_directory_size", duckdb_max_temp_directory_size);
elog(DEBUG2, "[PGDuckDB] Set DuckDB option: 'max_temp_directory_size'=%s", duckdb_max_temp_directory_size);
}
if (duckdb_maximum_threads > -1) {
SET_DUCKDB_OPTION(maximum_threads);
}
std::string connection_string;
/*
* If MotherDuck is enabled, use it to connect to DuckDB. That way DuckDB
* its default database will be set to the default MotherDuck database.
*/
if (pgduckdb::IsMotherDuckEnabled()) {
/*
* Disable web login for MotherDuck. Having support for web login could
* be nice for demos, but because the received token is not shared
* between backends you will get browser windows popped up. Sharing the
* token across backends could be made to work but the implementing it
* is not trivial, so for now we simply disable the web login.
*/
setenv("motherduck_disable_web_login", "1", 1);
std::ostringstream oss;
oss << "md:";
// Default database
auto default_database = FindMotherDuckDefaultDatabase();
AppendEscapedUri(oss, default_database);
oss << "?";
// Session hint
auto session_hint = GetSessionHint();
if (!IsEmptyString(session_hint)) {
oss << "session_hint=";
AppendEscapedUri(oss, session_hint);
oss << "&";
}
connection_string = oss.str();
// Token
auto token = FindMotherDuckToken();
if (token != nullptr && !AreStringEqual(token, "::FROM_ENV::")) {
setenv("motherduck_token", token, 1);
}
}
std::string pg_time_zone(pg::GetConfigOption("TimeZone"));
database = new duckdb::DuckDB(connection_string, &config);
auto &dbconfig = duckdb::DBConfig::GetConfig(*database->instance);
dbconfig.storage_extensions["pgduckdb"] = duckdb::make_uniq<PostgresStorageExtension>();
// Register the unsupported type optimizer to run after all other optimizations
dbconfig.optimizer_extensions.push_back(UnsupportedTypeOptimizer::GetOptimizerExtension());
auto &extension_manager = database->instance->GetExtensionManager();
auto extension_active_load = extension_manager.BeginLoad("pgduckdb");
D_ASSERT(extension_active_load);
duckdb::ExtensionInstallInfo extension_install_info;
extension_active_load->FinishLoad(extension_install_info);
connection = duckdb::make_uniq<duckdb::Connection>(*database);
auto &context = *connection->context;
auto &db_manager = duckdb::DatabaseManager::Get(context);
default_dbname = db_manager.GetDefaultDatabase(context);
pgduckdb::DuckDBQueryOrThrow(context, "SET TimeZone =" + duckdb::KeywordHelper::WriteQuoted(pg_time_zone));
pgduckdb::DuckDBQueryOrThrow(context, "SET default_collation =" +
duckdb::KeywordHelper::WriteQuoted(duckdb_default_collation));
pgduckdb::DuckDBQueryOrThrow(context, "ATTACH DATABASE 'pgduckdb' (TYPE pgduckdb)");
pgduckdb::DuckDBQueryOrThrow(context, "ATTACH DATABASE ':memory:' AS pg_temp;");
if (pgduckdb::IsMotherDuckEnabled()) {
auto timeout = FindMotherDuckBackgroundCatalogRefreshInactivityTimeout();
if (timeout != nullptr) {
auto quoted_timeout = duckdb::KeywordHelper::WriteQuoted(timeout);
pgduckdb::DuckDBQueryOrThrow(context, "SET motherduck_background_catalog_refresh_inactivity_timeout=" +
quoted_timeout);
}
}
if (duckdb_autoinstall_known_extensions) {
InstallExtensions(context);
}
LoadExtensions(context);
/* Set allowed_directories and enable_external_access AFTER loading extensions
* (extensions need filesystem access to install/load). Set allowed_directories
* BEFORE disabling external access (DuckDB rejects changes to
* allowed_directories when external access is disabled). */
if (!IsEmptyString(duckdb_allowed_directories)) {
auto dirs = duckdb::StringUtil::Split(duckdb_allowed_directories, ',');
auto list_str = "[" +
duckdb::StringUtil::Join(dirs, dirs.size(), ", ",
[](const std::string &dir) {
auto trimmed = dir;
duckdb::StringUtil::Trim(trimmed);
return duckdb::KeywordHelper::WriteQuoted(trimmed);
}) +
"]";
pgduckdb::DuckDBQueryOrThrow(context, "SET allowed_directories=" + list_str);
}
if (!duckdb_enable_external_access) {
pgduckdb::DuckDBQueryOrThrow(context, "SET enable_external_access=false");
}
}
void
DuckDBManager::Reset() {
manager_instance.connection = nullptr;
delete manager_instance.database;
manager_instance.database = nullptr;
UnclaimBgwSessionHint();
}
int64
GetSeqLastValue(const char *seq_name) {
Oid duckdb_namespace = get_namespace_oid("duckdb", false);
Oid table_seq_oid = get_relname_relid(seq_name, duckdb_namespace);
return PostgresFunctionGuard(DirectFunctionCall1Coll, pg_sequence_last_value, InvalidOid, table_seq_oid);
}
void
DuckDBManager::LoadSecrets(duckdb::ClientContext &context) {
auto queries = InvokeCPPFunc(pg::ListDuckDBCreateSecretQueries);
foreach_ptr(char, query, queries) {
DuckDBQueryOrThrow(context, query);
}
}
void
DuckDBManager::DropSecrets(duckdb::ClientContext &context) {
auto secrets =
pgduckdb::DuckDBQueryOrThrow(context, "SELECT name FROM duckdb_secrets() WHERE name LIKE 'pgduckdb_secret_%';");
while (auto chunk = secrets->Fetch()) {
for (size_t i = 0, s = chunk->size(); i < s; ++i) {
auto drop_secret_cmd = duckdb::StringUtil::Format("DROP SECRET %s;", chunk->GetValue(0, i).ToString());
pgduckdb::DuckDBQueryOrThrow(context, drop_secret_cmd);
}
}
}
void
DuckDBManager::LoadExtensions(duckdb::ClientContext &context) {
auto duckdb_extensions = ReadDuckdbExtensions();
for (auto &extension : duckdb_extensions) {
if (extension.autoload) {
DuckDBQueryOrThrow(context, ddb::LoadExtensionQuery(extension.name));
}
}
}
void
DuckDBManager::InstallExtensions(duckdb::ClientContext &context) {
auto duckdb_extensions = ReadDuckdbExtensions();
for (auto &extension : duckdb_extensions) {
DuckDBQueryOrThrow(context, ddb::InstallExtensionQuery(extension.name, extension.repository));
}
}
static std::string
DisabledFileSystems() {
if (pgduckdb::pg::AllowRawFileAccess()) {
return duckdb_disabled_filesystems;
}
if (IsEmptyString(duckdb_disabled_filesystems)) {
return "LocalFileSystem";
}
/* Ensure LocalFileSystem is added only when it's absent from duckdb_disabled_filesystems. */
std::vector<std::string> fs_list = duckdb::StringUtil::Split(duckdb_disabled_filesystems, ',');
for (auto &fs : fs_list) {
std::string trimmed_fs = fs;
duckdb::StringUtil::Trim(trimmed_fs);
if (duckdb::StringUtil::CIEquals(trimmed_fs, "LocalFileSystem")) {
return duckdb_disabled_filesystems;
}
}
return "LocalFileSystem," + std::string(duckdb_disabled_filesystems);
}
void
DuckDBManager::RefreshConnectionState(duckdb::ClientContext &context) {
std::string disabled_filesystems = DisabledFileSystems();
if (disabled_filesystems != "") {
/*
* DuckDB does not allow us to disable this setting on the
* database after the DuckDB connection is created for a non
* superuser, any further connections will inherit this
* restriction. This means that once a non-superuser used a
* DuckDB connection in a Postgres backend, any other
* connection will inherit these same filesystem restrictions.
* This shouldn't be a problem in practice.
*/
pgduckdb::DuckDBQueryOrThrow(context, "SET disabled_filesystems=" +
duckdb::KeywordHelper::WriteQuoted(disabled_filesystems));
}
if (strlen(duckdb_azure_transport_option_type) > 0) {
pgduckdb::DuckDBQueryOrThrow(context,
"SET azure_transport_option_type=" +
duckdb::KeywordHelper::WriteQuoted(duckdb_azure_transport_option_type));
}
const auto extensions_table_last_seq = GetSeqLastValue("extensions_table_seq");
if (IsExtensionsSeqLessThan(extensions_table_last_seq)) {
LoadExtensions(context);
UpdateExtensionsSeq(extensions_table_last_seq);
}
if (!secrets_valid) {
DropSecrets(context);
LoadSecrets(context);
secrets_valid = true;
}
}
/*
* Creates a new connection to the global DuckDB instance. This should only be
* used in some rare cases, where a temporary new connection is needed instead
* of the global cached connection that is returned by GetConnection.
*/
duckdb::unique_ptr<duckdb::Connection>
DuckDBManager::CreateConnection() {
pgduckdb::RequireDuckdbExecution();
auto &instance = Get();
auto connection = duckdb::make_uniq<duckdb::Connection>(*instance.database);
auto &context = *connection->context;
instance.RefreshConnectionState(context);
return connection;
}
/* Returns the cached connection to the global DuckDB instance. */
duckdb::Connection *
DuckDBManager::GetConnection(bool force_transaction) {
pgduckdb::RequireDuckdbExecution();
auto &instance = Get();
auto &context = *instance.connection->context;
if (!context.transaction.HasActiveTransaction()) {
if (IsSubTransaction()) {
throw duckdb::NotImplementedException("SAVEPOINT and subtransactions are not supported in DuckDB");
}
if (force_transaction || pg::IsInTransactionBlock()) {
/*
* We only want to open a new DuckDB transaction if we're already
* in a Postgres transaction block. Always opening a transaction
* incurs a significant performance penalty for single statement
* queries on MotherDuck. This is because a second round-trip is
* needed to send the COMMIT to MotherDuck when Postgres its
* transaction finishes. So we only want to do this when actually
* necessary.
*/
instance.connection->BeginTransaction();
}
}
instance.RefreshConnectionState(context);
return instance.connection.get();
}
/*
* Returns the cached connection to the global DuckDB instance, but does not do
* any checks required to correctly initialize the DuckDB transaction nor
* refreshes the secrets/extensions/etc. Only use this in rare cases where you
* know for sure that the connection is already initialized correctly for the
* current query, and you just want a pointer to it.
*/
duckdb::Connection *
DuckDBManager::GetConnectionUnsafe() {
auto &instance = Get();
return instance.connection.get();
}
} // namespace pgduckdb