Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScopedPostgresResource #188

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions include/pgduckdb/common/scoped_postgres_resource.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <functional>
#include <type_traits>
#include <utility>

namespace pgduckdb {

template <class T>
class ScopedPostgresResource {
public:
using resource_destructor_t = std::function<void(T)>;

explicit ScopedPostgresResource(T resource, resource_destructor_t &&destructor)
: resource(resource), destructor(std::move(destructor)) {
}
~ScopedPostgresResource() {
destructor(resource);
}

private:
T resource;
resource_destructor_t destructor;
};

// Specialization for pointer types
template <class T>
class ScopedPostgresResource<T *> {
public:
using resource_destructor_t = std::function<void(T *)>;

explicit ScopedPostgresResource(T *resource, resource_destructor_t &&destructor)
: resource(resource), destructor(std::move(destructor)) {
}

~ScopedPostgresResource() {
destructor(resource);
}

T *
operator->() const {
return resource;
}

operator void *() const {
return static_cast<void *>(resource);
}

private:
T *resource;
resource_destructor_t destructor;
};

} // namespace pgduckdb
7 changes: 4 additions & 3 deletions src/pgduckdb_metadata_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extern "C" {
#include "utils/syscache.h"
}

#include "pgduckdb/common/scoped_postgres_resource.hpp"
#include "pgduckdb/vendor/pg_list.hpp"

namespace pgduckdb {
Expand Down Expand Up @@ -88,7 +89,9 @@ BuildDuckdbOnlyFunctions() {
const char *function_names[] = {"read_parquet", "read_csv", "iceberg_scan"};

for (int i = 0; i < lengthof(function_names); i++) {
CatCList *catlist = SearchSysCacheList1(PROCNAMEARGSNSP, CStringGetDatum(function_names[i]));
auto catlist =
ScopedPostgresResource<CatCList *>(SearchSysCacheList1(PROCNAMEARGSNSP, CStringGetDatum(function_names[i])),
[](CatCList *clist) { ReleaseSysCacheList(clist); });

for (int j = 0; j < catlist->n_members; j++) {
HeapTuple tuple = &catlist->members[j]->tuple;
Expand All @@ -102,8 +105,6 @@ BuildDuckdbOnlyFunctions() {
cache.duckdb_only_functions = lappend_oid(cache.duckdb_only_functions, function->oid);
MemoryContextSwitchTo(oldcontext);
}

ReleaseSysCacheList(catlist);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/pgduckdb_planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extern "C" {
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/pgduckdb_node.hpp"
#include "pgduckdb/common/scoped_postgres_resource.hpp"
#include "pgduckdb/pgduckdb_planner.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
Expand Down Expand Up @@ -111,10 +112,10 @@ CreatePlan(Query *query, ParamListInfo bound_params) {
return nullptr;
}

HeapTuple tp;
Form_pg_type typtup;

tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(postgresColumnOid));
auto tp = pgduckdb::ScopedPostgresResource<HeapTuple>(
SearchSysCache1(TYPEOID, ObjectIdGetDatum(postgresColumnOid)), ReleaseSysCache);
if (!HeapTupleIsValid(tp)) {
elog(WARNING, "(PGDuckDB/CreatePlan) Cache lookup failed for type %u", postgresColumnOid);
return nullptr;
Expand All @@ -127,8 +128,6 @@ CreatePlan(Query *query, ParamListInfo bound_params) {
duckdb_node->custom_scan_tlist =
lappend(duckdb_node->custom_scan_tlist,
makeTargetEntry((Expr *)var, i + 1, (char *)pstrdup(prepared_query->GetNames()[i].c_str()), false));

ReleaseSysCache(tp);
}

duckdb_node->custom_private = list_make1(query);
Expand Down
5 changes: 2 additions & 3 deletions src/scan/postgres_index_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {
#include "pgduckdb/scan/index_scan_utils.hpp"
#include "pgduckdb/scan/postgres_index_scan.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/common/scoped_postgres_resource.hpp"
#include "pgduckdb/vendor/pg_list.hpp"

namespace pgduckdb {
Expand Down Expand Up @@ -91,7 +92,7 @@ PostgresIndexScanFunction::PostgresIndexScanBind(duckdb::ClientContext &context,

RangeTblEntry *rte = planner_rt_fetch(path->parent->relid, planner_info);

auto rel = RelationIdGetRelation(rte->relid);
auto rel = ScopedPostgresResource<Relation>(RelationIdGetRelation(rte->relid), RelationClose);
auto relation_descr = RelationGetDescr(rel);

if (!relation_descr) {
Expand All @@ -111,8 +112,6 @@ PostgresIndexScanFunction::PostgresIndexScanBind(duckdb::ClientContext &context,
duck_type.ToString().c_str());
}

RelationClose(rel);

return duckdb::make_uniq<PostgresIndexScanFunctionData>(cardinality, path, planner_info, rte->relid, snapshot);
}

Expand Down
5 changes: 2 additions & 3 deletions src/scan/postgres_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
}

#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/common/scoped_postgres_resource.hpp"
#include "pgduckdb/pgduckdb_types.hpp"

namespace pgduckdb {
Expand Down Expand Up @@ -186,7 +187,7 @@ PostgresReplacementScan(duckdb::ClientContext &context, duckdb::ReplacementScanI
}

// Check if the Relation is a VIEW
auto tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
auto tuple = ScopedPostgresResource<HeapTuple>(SearchSysCache1(RELOID, ObjectIdGetDatum(relid)), ReleaseSysCache);
if (!HeapTupleIsValid(tuple)) {
elog(WARNING, "(PGDuckDB/PostgresReplacementScan) Cache lookup failed for relation %u", relid);
return nullptr;
Expand All @@ -196,10 +197,8 @@ PostgresReplacementScan(duckdb::ClientContext &context, duckdb::ReplacementScanI

// Check if the relation is a view
if (relForm->relkind == RELKIND_VIEW) {
ReleaseSysCache(tuple);
return ReplaceView(relid);
}
ReleaseSysCache(tuple);

RelOptInfo *node = nullptr;
Path *node_path = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/scan/postgres_seq_scan.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "duckdb.hpp"

#include "pgduckdb/scan/postgres_seq_scan.hpp"
#include "pgduckdb/common/scoped_postgres_resource.hpp"
#include "pgduckdb/pgduckdb_types.hpp"

namespace pgduckdb {
Expand Down Expand Up @@ -72,12 +73,12 @@ PostgresSeqScanFunction::PostgresSeqScanBind(duckdb::ClientContext &context, duc
auto relid = input.named_parameters["relid"].GetValue<uint32_t>();
auto snapshot = (reinterpret_cast<Snapshot>(input.named_parameters["snapshot"].GetPointer()));

auto rel = RelationIdGetRelation(relid);
auto rel = ScopedPostgresResource<Relation>(RelationIdGetRelation(relid), RelationClose);

auto relation_descr = RelationGetDescr(rel);

if (!relation_descr) {
elog(WARNING, "(PGDuckDB/PostgresSeqScanBind) Failed to get tuple descriptor for relation with OID %u", relid);
RelationClose(rel);
return nullptr;
}

Expand All @@ -92,7 +93,6 @@ PostgresSeqScanFunction::PostgresSeqScanBind(duckdb::ClientContext &context, duc
duck_type.ToString().c_str());
}

RelationClose(rel);
return duckdb::make_uniq<PostgresSeqScanFunctionData>(cardinality, relid, snapshot);
}

Expand Down