Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Tishj committed Aug 1, 2024
1 parent 92d9de4 commit 8ad3b4f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
31 changes: 1 addition & 30 deletions src/catalog/pgduckdb_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -46,30 +47,6 @@ optional_ptr<CatalogEntry> 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<SchemaCatalogEntry> 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);
Expand All @@ -80,12 +57,6 @@ optional_ptr<SchemaCatalogEntry> 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<PostgresSchema>(*this, create_schema, snapshot);
Expand Down
10 changes: 7 additions & 3 deletions src/catalog/pgduckdb_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,21 @@ optional_ptr<CatalogEntry> PostgresSchema::CreateType(CatalogTransaction transac
throw duckdb::NotImplementedException("CreateType not supported yet");
}

optional_ptr<CatalogEntry> PostgresSchema::GetEntry(CatalogTransaction transaction, CatalogType type, const string &name) {
optional_ptr<CatalogEntry> 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
Expand Down
6 changes: 6 additions & 0 deletions src/catalog/pgduckdb_storage.cpp
Original file line number Diff line number Diff line change
@@ -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<duckdb::TransactionManager> CreateTransactionManager(duckdb::StorageExtensionInfo *storage_info, duckdb::AttachedDatabase &db, duckdb::Catalog &catalog) {
return make_uniq<duckdb::DuckTransactionManager>(db);
}

PostgresStorageExtension::PostgresStorageExtension(Snapshot snapshot) {
attach = PostgresCatalog::Attach;
create_transaction_manager = CreateTransactionManager;
storage_info = duckdb::make_uniq<PostgresStorageExtensionInfo>(snapshot);
}

Expand Down
10 changes: 6 additions & 4 deletions src/pgduckdb_duckdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -70,7 +71,6 @@ duckdb::unique_ptr<duckdb::DuckDB>
DuckdbOpenDatabase() {
duckdb::DBConfig config;
config.SetOptionByName("extension_directory", duckdbGetExtensionDirectory());
config.storage_extensions["pgduckdb"] = duckdb::make_uniq<PostgresStorageExtension>(GetActiveSnapshot());
return duckdb::make_uniq<duckdb::DuckDB>(nullptr, &config);
}

Expand All @@ -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<PostgresStorageExtension>(GetActiveSnapshot());

auto connection = duckdb::make_uniq<duckdb::Connection>(*db);

Expand All @@ -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);
Expand Down

0 comments on commit 8ad3b4f

Please sign in to comment.