From 8ad3b4f21929498146b3adaf6e0eeb2d3eff4f80 Mon Sep 17 00:00:00 2001 From: Tishj Date: Thu, 1 Aug 2024 20:19:03 +0200 Subject: [PATCH] wip --- src/catalog/pgduckdb_catalog.cpp | 31 +------------------------------ src/catalog/pgduckdb_schema.cpp | 10 +++++++--- src/catalog/pgduckdb_storage.cpp | 6 ++++++ src/pgduckdb_duckdb.cpp | 10 ++++++---- 4 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/catalog/pgduckdb_catalog.cpp b/src/catalog/pgduckdb_catalog.cpp index 761682a9..4b21e55b 100644 --- a/src/catalog/pgduckdb_catalog.cpp +++ b/src/catalog/pgduckdb_catalog.cpp @@ -4,6 +4,7 @@ extern "C" { #include "postgres.h" +#include "utils/fmgroids.h" #include "fmgr.h" #include "catalog/pg_namespace.h" #include "utils/syscache.h" @@ -46,30 +47,6 @@ optional_ptr PostgresCatalog::CreateSchema(CatalogTransaction tran throw duckdb::NotImplementedException("CreateSchema not supported yet"); } -static Oid LookupSchema(const string &schema_name, Snapshot snapshot) { - auto rel = table_open(NamespaceRelationId, AccessShareLock); - - ScanKeyData key; - ScanKeyInit(&key, - Anum_pg_namespace_nspname, - BTEqualStrategyNumber, - NAMEOID, - CStringGetDatum(schema_name.c_str())); - - auto scan = systable_beginscan(rel, NamespaceNameIndexId, true, snapshot, 1, &key); - - auto tuple = systable_getnext(scan); - Oid nspoid = InvalidOid; - if (HeapTupleIsValid(tuple)) { - nspoid = ((Form_pg_namespace) GETSTRUCT(tuple))->oid; - } - - systable_endscan(scan); - table_close(rel, AccessShareLock); - - return nspoid; -} - optional_ptr PostgresCatalog::GetSchema(CatalogTransaction transaction, const string &schema_name, OnEntryNotFound if_not_found, QueryErrorContext error_context) { if (schema_name == DEFAULT_SCHEMA) { return GetSchema(transaction, "public", if_not_found, error_context); @@ -80,12 +57,6 @@ optional_ptr PostgresCatalog::GetSchema(CatalogTransaction t return it->second.get(); } - auto oid = LookupSchema(schema_name, snapshot); - if (!OidIsValid(oid)) { - // Schema could not be found - return nullptr; - } - CreateSchemaInfo create_schema; create_schema.schema = schema_name; schemas[schema_name] = duckdb::make_uniq(*this, create_schema, snapshot); diff --git a/src/catalog/pgduckdb_schema.cpp b/src/catalog/pgduckdb_schema.cpp index b073ed0e..38b6d9ea 100644 --- a/src/catalog/pgduckdb_schema.cpp +++ b/src/catalog/pgduckdb_schema.cpp @@ -66,17 +66,21 @@ optional_ptr PostgresSchema::CreateType(CatalogTransaction transac throw duckdb::NotImplementedException("CreateType not supported yet"); } -optional_ptr PostgresSchema::GetEntry(CatalogTransaction transaction, CatalogType type, const string &name) { +optional_ptr PostgresSchema::GetEntry(CatalogTransaction transaction, CatalogType type, const string &entry_name) { if (type != CatalogType::TABLE_ENTRY) { throw duckdb::NotImplementedException("GetEntry (type: %s) not supported yet", duckdb::EnumUtil::ToString(type)); } - auto it = tables.find(name); + auto it = tables.find(entry_name); if (it != tables.end()) { return it->second.get(); } - RangeVar *table_range_var = makeRangeVarFromNameList(stringToQualifiedNameList(name.c_str(), NULL)); + List *name_list = NIL; + name_list = lappend(name_list, makeString(pstrdup(name.c_str()))); + name_list = lappend(name_list, makeString(pstrdup(entry_name.c_str()))); + + RangeVar *table_range_var = makeRangeVarFromNameList(name_list); Oid rel_oid = RangeVarGetRelid(table_range_var, AccessShareLock, true); if (rel_oid == InvalidOid) { // Table could not be found diff --git a/src/catalog/pgduckdb_storage.cpp b/src/catalog/pgduckdb_storage.cpp index 410d7795..32b0c9a5 100644 --- a/src/catalog/pgduckdb_storage.cpp +++ b/src/catalog/pgduckdb_storage.cpp @@ -1,10 +1,16 @@ #include "pgduckdb/catalog/pgduckdb_storage.hpp" #include "pgduckdb/catalog/pgduckdb_catalog.hpp" +#include "duckdb/transaction/duck_transaction_manager.hpp" namespace pgduckdb { +static duckdb::unique_ptr CreateTransactionManager(duckdb::StorageExtensionInfo *storage_info, duckdb::AttachedDatabase &db, duckdb::Catalog &catalog) { + return make_uniq(db); +} + PostgresStorageExtension::PostgresStorageExtension(Snapshot snapshot) { attach = PostgresCatalog::Attach; + create_transaction_manager = CreateTransactionManager; storage_info = duckdb::make_uniq(snapshot); } diff --git a/src/pgduckdb_duckdb.cpp b/src/pgduckdb_duckdb.cpp index 99286bc1..af9652d8 100644 --- a/src/pgduckdb_duckdb.cpp +++ b/src/pgduckdb_duckdb.cpp @@ -3,6 +3,7 @@ #include "duckdb/main/extension_util.hpp" #include "duckdb/main/client_data.hpp" #include "duckdb/catalog/catalog_search_path.hpp" +#include "duckdb/main/extension_install_info.hpp" #include "pgduckdb/pgduckdb_options.hpp" #include "pgduckdb/pgduckdb_duckdb.hpp" @@ -70,7 +71,6 @@ duckdb::unique_ptr DuckdbOpenDatabase() { duckdb::DBConfig config; config.SetOptionByName("extension_directory", duckdbGetExtensionDirectory()); - config.storage_extensions["pgduckdb"] = duckdb::make_uniq(GetActiveSnapshot()); return duckdb::make_uniq(nullptr, &config); } @@ -85,6 +85,7 @@ DuckdbCreateConnection(List *rtables, PlannerInfo *plannerInfo, List *neededColu // neededColumns, query)); auto &config = duckdb::DBConfig::GetConfig(*db->instance); + config.storage_extensions["pgduckdb"] = duckdb::make_uniq(GetActiveSnapshot()); auto connection = duckdb::make_uniq(*db); @@ -98,10 +99,11 @@ DuckdbCreateConnection(List *rtables, PlannerInfo *plannerInfo, List *neededColu duckdb::CreateTableFunctionInfo index_scan_info(index_scan_fun); auto &catalog = duckdb::Catalog::GetSystemCatalog(context); + duckdb::ExtensionInstallInfo extension_install_info; + db->instance->SetExtensionLoaded("pgduckdb", extension_install_info); + context.Query("ATTACH DATABASE 'pgduckdb' (TYPE pgduckdb)", false); + context.Query("USE pgduckdb", false); context.transaction.BeginTransaction(); - - // Make sure the custom postgres catalog is used - client_data.catalog_search_path->Set({duckdb::CatalogSearchEntry("", "pgduckdb")}, duckdb::CatalogSetPathType::SET_SCHEMA); auto &instance = *db->instance; duckdb::ExtensionUtil::RegisterType(instance, "UnsupportedPostgresType", duckdb::LogicalTypeId::VARCHAR); catalog.CreateTableFunction(context, &seq_scan_info);