Skip to content

Commit 375710f

Browse files
authored
Merge pull request #191 from carlopi/patches
Apply duckdb's rebind.patch
2 parents 7c3a601 + 974dd46 commit 375710f

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

src/include/postgres_scanner.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class PostgresClearCacheFunction : public TableFunction {
8383
PostgresClearCacheFunction();
8484

8585
static void ClearCacheOnSetting(ClientContext &context, SetScope scope, Value &parameter);
86+
static void ClearPostgresCaches(ClientContext &context);
8687
};
8788

8889
class PostgresQueryFunction : public TableFunction {

src/include/storage/postgres_catalog.hpp

+14
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ class PostgresCatalog : public Catalog {
7272

7373
void ClearCache();
7474

75+
//! Whether or not this catalog should search a specific type with the standard priority
76+
CatalogLookupBehavior CatalogTypeLookupRule(CatalogType type) const override {
77+
switch (type) {
78+
case CatalogType::INDEX_ENTRY:
79+
case CatalogType::TABLE_ENTRY:
80+
case CatalogType::TYPE_ENTRY:
81+
case CatalogType::VIEW_ENTRY:
82+
return CatalogLookupBehavior::STANDARD;
83+
default:
84+
// unsupported type (e.g. scalar functions, aggregates, ...)
85+
return CatalogLookupBehavior::NEVER_LOOKUP;
86+
}
87+
}
88+
7589
private:
7690
void DropSchema(ClientContext &context, DropInfo &info) override;
7791

src/postgres_extension.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,44 @@
1212
#include "duckdb/main/attached_database.hpp"
1313
#include "storage/postgres_catalog.hpp"
1414
#include "storage/postgres_optimizer.hpp"
15+
#include "duckdb/planner/extension_callback.hpp"
16+
#include "duckdb/main/client_context.hpp"
17+
#include "duckdb/main/client_context_state.hpp"
18+
#include "duckdb/main/connection_manager.hpp"
19+
#include "duckdb/common/error_data.hpp"
1520

1621
using namespace duckdb;
1722

23+
class PostgresExtensionState : public ClientContextState {
24+
public:
25+
bool CanRequestRebind() override {
26+
return true;
27+
}
28+
RebindQueryInfo OnPlanningError(ClientContext &context, SQLStatement &statement, ErrorData &error) override {
29+
if (error.Type() != ExceptionType::BINDER) {
30+
return RebindQueryInfo::DO_NOT_REBIND;
31+
}
32+
auto &extra_info = error.ExtraInfo();
33+
auto entry = extra_info.find("error_subtype");
34+
if (entry == extra_info.end()) {
35+
return RebindQueryInfo::DO_NOT_REBIND;
36+
}
37+
if (entry->second != "COLUMN_NOT_FOUND") {
38+
return RebindQueryInfo::DO_NOT_REBIND;
39+
}
40+
// clear caches and rebind
41+
PostgresClearCacheFunction::ClearPostgresCaches(context);
42+
return RebindQueryInfo::ATTEMPT_TO_REBIND;
43+
}
44+
};
45+
46+
class PostgresExtensionCallback : public ExtensionCallback {
47+
public:
48+
void OnConnectionOpened(ClientContext &context) override {
49+
context.registered_state.insert(make_pair("postgres_extension", make_shared<PostgresExtensionState>()));
50+
}
51+
};
52+
1853
static void SetPostgresConnectionLimit(ClientContext &context, SetScope scope, Value &parameter) {
1954
if (scope == SetScope::LOCAL) {
2055
throw InvalidInputException("pg_connection_limit can only be set globally");
@@ -78,6 +113,11 @@ static void LoadInternal(DatabaseInstance &db) {
78113
OptimizerExtension postgres_optimizer;
79114
postgres_optimizer.optimize_function = PostgresOptimizer::Optimize;
80115
config.optimizer_extensions.push_back(std::move(postgres_optimizer));
116+
117+
config.extension_callbacks.push_back(make_uniq<PostgresExtensionCallback>());
118+
for(auto &connection : ConnectionManager::Get(db).GetConnectionList()) {
119+
connection->registered_state.insert(make_pair("postgres_extension", make_shared<PostgresExtensionState>()));
120+
}
81121
}
82122

83123
void PostgresScannerExtension::Load(DuckDB &db) {

src/storage/postgres_clear_cache.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static unique_ptr<FunctionData> ClearCacheBind(ClientContext &context, TableFunc
2121
return std::move(result);
2222
}
2323

24-
static void ClearPostgresCaches(ClientContext &context) {
24+
void PostgresClearCacheFunction::ClearPostgresCaches(ClientContext &context) {
2525
auto databases = DatabaseManager::Get(context).GetDatabases(context);
2626
for (auto &db_ref : databases) {
2727
auto &db = db_ref.get();
@@ -38,12 +38,12 @@ static void ClearCacheFunction(ClientContext &context, TableFunctionInput &data_
3838
if (data.finished) {
3939
return;
4040
}
41-
ClearPostgresCaches(context);
41+
PostgresClearCacheFunction::ClearPostgresCaches(context);
4242
data.finished = true;
4343
}
4444

4545
void PostgresClearCacheFunction::ClearCacheOnSetting(ClientContext &context, SetScope scope, Value &parameter) {
46-
ClearPostgresCaches(context);
46+
PostgresClearCacheFunction::ClearPostgresCaches(context);
4747
}
4848

4949
PostgresClearCacheFunction::PostgresClearCacheFunction()

0 commit comments

Comments
 (0)