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

Revert "Fix memory leaks in custom executor (#251)" #255

Merged
merged 1 commit into from
Oct 7, 2024
Merged
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
30 changes: 6 additions & 24 deletions include/pgduckdb/pgduckdb_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ extern "C" {
}

#include "duckdb/common/exception.hpp"
#include "duckdb/common/error_data.hpp"

#include <vector>
#include <string>
Expand All @@ -31,6 +30,7 @@ template <typename T, typename FuncType, typename... FuncArgs>
T
PostgresFunctionGuard(FuncType postgres_function, FuncArgs... args) {
T return_value;
bool error = false;
MemoryContext ctx = CurrentMemoryContext;
ErrorData *edata = nullptr;
// clang-format off
Expand All @@ -43,10 +43,11 @@ PostgresFunctionGuard(FuncType postgres_function, FuncArgs... args) {
MemoryContextSwitchTo(ctx);
edata = CopyErrorData();
FlushErrorState();
error = true;
}
PG_END_TRY();
// clang-format on
if (edata) {
if (error) {
throw duckdb::Exception(duckdb::ExceptionType::EXECUTOR, edata->message);
}
return return_value;
Expand All @@ -55,6 +56,7 @@ PostgresFunctionGuard(FuncType postgres_function, FuncArgs... args) {
template <typename FuncType, typename... FuncArgs>
void
PostgresFunctionGuard(FuncType postgres_function, FuncArgs... args) {
bool error = false;
MemoryContext ctx = CurrentMemoryContext;
ErrorData *edata = nullptr;
// clang-format off
Expand All @@ -67,33 +69,13 @@ PostgresFunctionGuard(FuncType postgres_function, FuncArgs... args) {
MemoryContextSwitchTo(ctx);
edata = CopyErrorData();
FlushErrorState();
error = true;
}
PG_END_TRY();
// clang-format on
if (edata) {
if (error) {
throw duckdb::Exception(duckdb::ExceptionType::EXECUTOR, edata->message);
}
}

template <typename FuncType, typename... FuncArgs>
const char*
DuckDBFunctionGuard(FuncType duckdb_function, FuncArgs... args) {
try {
duckdb_function(args...);
} catch (duckdb::Exception &ex) {
duckdb::ErrorData edata(ex.what());
return pstrdup(edata.Message().c_str());
} catch (std::exception &ex) {
const auto msg = ex.what();
if (msg[0] == '{') {
duckdb::ErrorData edata(ex.what());
return pstrdup(edata.Message().c_str());
} else {
return pstrdup(ex.what());
}
}

return nullptr;
}

} // namespace pgduckdb
55 changes: 15 additions & 40 deletions src/pgduckdb_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ extern "C" {
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_planner.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

/* global variables */
CustomScanMethods duckdb_scan_scan_methods;
Expand All @@ -37,21 +36,9 @@ typedef struct DuckdbScanState {

static void
CleanupDuckdbScanState(DuckdbScanState *state) {
MemoryContextReset(state->css.ss.ps.ps_ExprContext->ecxt_per_tuple_memory);
ExecClearTuple(state->css.ss.ss_ScanTupleSlot);

state->query_results.reset();
state->current_data_chunk.reset();

if (state->prepared_statement) {
delete state->prepared_statement;
state->prepared_statement = nullptr;
}

if (state->duckdb_connection) {
delete state->duckdb_connection;
state->duckdb_connection = nullptr;
}
delete state->prepared_statement;
delete state->duckdb_connection;
}

/* static callbacks */
Expand Down Expand Up @@ -105,28 +92,25 @@ ExecuteQuery(DuckdbScanState *state) {
ParamExternData tmp_workspace;

/* give hook a chance in case parameter is dynamic */
if (pg_params->paramFetch != NULL) {
if (pg_params->paramFetch != NULL)
pg_param = pg_params->paramFetch(pg_params, i + 1, false, &tmp_workspace);
} else {
else
pg_param = &pg_params->params[i];
}

if (pg_param->isnull) {
duckdb_params.push_back(duckdb::Value());
} else if (OidIsValid(pg_param->ptype)) {
duckdb_params.push_back(pgduckdb::ConvertPostgresParameterToDuckValue(pg_param->value, pg_param->ptype));
} else {
std::ostringstream oss;
oss << "parameter " << i << " has an invalid type (" << pg_param->ptype << ") during query execution";
throw duckdb::Exception(duckdb::ExceptionType::EXECUTOR, oss.str().c_str());
if (!OidIsValid(pg_param->ptype)) {
elog(ERROR, "parameter with invalid type during execution");
}
duckdb_params.push_back(pgduckdb::ConvertPostgresParameterToDuckValue(pg_param->value, pg_param->ptype));
}
}

auto pending = prepared.PendingQuery(duckdb_params, true);
if (pending->HasError()) {
return pending->ThrowError();
elog(ERROR, "DuckDB execute returned an error: %s", pending->GetError().c_str());
}

duckdb::PendingExecutionResult execution_result;
do {
execution_result = pending->ExecuteTask();
Expand All @@ -137,16 +121,16 @@ ExecuteQuery(DuckdbScanState *state) {
// Wait for all tasks to terminate
executor.CancelTasks();
// Delete the scan state
CleanupDuckdbScanState(state);
// Process the interrupt on the Postgres side
ProcessInterrupts();
throw duckdb::Exception(duckdb::ExceptionType::EXECUTOR, "Query cancelled");
elog(ERROR, "Query cancelled");
}
} while (!duckdb::PendingQueryResult::IsResultReady(execution_result));

if (execution_result == duckdb::PendingExecutionResult::EXECUTION_ERROR) {
return pending->ThrowError();
CleanupDuckdbScanState(state);
elog(ERROR, "(PGDuckDB/ExecuteQuery) %s", pending->GetError().c_str());
}

query_results = pending->Execute();
state->column_count = query_results->ColumnCount();
state->is_executed = true;
Expand All @@ -160,11 +144,7 @@ Duckdb_ExecCustomScan(CustomScanState *node) {

bool already_executed = duckdb_scan_state->is_executed;
if (!already_executed) {
auto err_msg = pgduckdb::DuckDBFunctionGuard(ExecuteQuery, duckdb_scan_state);
if (err_msg) {
Duckdb_EndCustomScan(node);
elog(ERROR, "(PGDuckDB/ExecuteQuery) %s", err_msg);
}
ExecuteQuery(duckdb_scan_state);
}

if (duckdb_scan_state->fetch_next) {
Expand Down Expand Up @@ -224,12 +204,7 @@ Duckdb_ReScanCustomScan(CustomScanState *node) {
void
Duckdb_ExplainCustomScan(CustomScanState *node, List *ancestors, ExplainState *es) {
DuckdbScanState *duckdb_scan_state = (DuckdbScanState *)node;
auto err_msg = pgduckdb::DuckDBFunctionGuard(ExecuteQuery, duckdb_scan_state);
if (err_msg) {
Duckdb_EndCustomScan(node);
elog(ERROR, "(PGDuckDB/Duckdb_ExecCustomScan) %s", err_msg);
}

ExecuteQuery(duckdb_scan_state);
auto chunk = duckdb_scan_state->query_results->Fetch();
if (!chunk || chunk->size() == 0) {
return;
Expand Down