Skip to content

Commit d99f48f

Browse files
committed
First attempt at fixing issue 761
1 parent cbc28da commit d99f48f

File tree

6 files changed

+68
-1
lines changed

6 files changed

+68
-1
lines changed

include/pgduckdb/pgduckdb_xact.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ void AutocommitSingleStatementQueries();
2323
void MarkStatementNotTopLevel();
2424
void SetStatementTopLevel(bool top_level);
2525
bool IsStatementTopLevel();
26+
std::string FindAndResetPendingError();
2627
} // namespace pgduckdb

src/pgduckdb_node.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "pgduckdb/pgduckdb_planner.hpp"
77
#include "pgduckdb/pgduckdb_types.hpp"
8+
#include "pgduckdb/pgduckdb_xact.hpp"
89
#include "pgduckdb/vendor/pg_explain.hpp"
910

1011
extern "C" {
@@ -327,6 +328,12 @@ void
327328
Duckdb_EndCustomScan_Cpp(CustomScanState *node) {
328329
DuckdbScanState *duckdb_scan_state = (DuckdbScanState *)node;
329330
CleanupDuckdbScanState(duckdb_scan_state);
331+
332+
auto err = pgduckdb::FindAndResetPendingError();
333+
if (!err.empty() && QueryCancelHoldoffCount == 0) {
334+
throw duckdb::Exception(duckdb::ExceptionType::EXECUTOR, err);
335+
}
336+
330337
/*
331338
* BUG: In rare error casess it's possible that we call this when we are
332339
* currently accepting interupts, in those cases we should not resume them

src/pgduckdb_xact.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@
99
#include "pgduckdb/pg/transactions.hpp"
1010
#include "pgduckdb/utility/cpp_wrapper.hpp"
1111

12+
extern "C" {
13+
extern bool ExitOnAnyError;
14+
}
15+
1216
namespace pgduckdb {
1317

1418
static CommandId next_expected_command_id = FirstCommandId;
1519
static bool top_level_statement = true;
20+
static std::string pending_error;
21+
22+
std::string
23+
FindAndResetPendingError() {
24+
auto prev = pending_error;
25+
pending_error = "";
26+
return prev;
27+
}
1628

1729
namespace pg {
1830

@@ -308,14 +320,19 @@ DuckdbSubXactCallback_Cpp(SubXactEvent event) {
308320
if (!DuckDBManager::IsInitialized()) {
309321
return;
310322
}
323+
311324
auto connection = DuckDBManager::GetConnectionUnsafe();
312325
auto &context = *connection->context;
313326
if (!context.transaction.HasActiveTransaction()) {
314327
return;
315328
}
316329

317330
if (event == SUBXACT_EVENT_START_SUB) {
318-
throw duckdb::NotImplementedException("SAVEPOINT is not supported in DuckDB");
331+
if (ExitOnAnyError) {
332+
pending_error = "SAVEPOINT is not supported in DuckDB";
333+
} else {
334+
throw duckdb::NotImplementedException("SAVEPOINT is not supported in DuckDB");
335+
}
319336
}
320337
}
321338

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SET duckdb.force_execution = TRUE;
2+
DO $$
3+
DECLARE
4+
objtype text;
5+
BEGIN
6+
FOR objtype IN VALUES ('toast table'), ('index column'), ('sequence column'),
7+
('toast table column'), ('view column'), ('materialized view column')
8+
LOOP
9+
BEGIN
10+
PERFORM pg_get_object_address(objtype, '{one}', '{}');
11+
EXCEPTION WHEN invalid_parameter_value THEN
12+
RAISE WARNING 'error for %: %', objtype, sqlerrm;
13+
END;
14+
END LOOP;
15+
END;
16+
$$;
17+
WARNING: error for toast table: unsupported object type "toast table"
18+
WARNING: error for index column: unsupported object type "index column"
19+
WARNING: error for sequence column: unsupported object type "sequence column"
20+
WARNING: error for toast table column: unsupported object type "toast table column"
21+
WARNING: error for view column: unsupported object type "view column"
22+
WARNING: error for materialized view column: unsupported object type "materialized view column"
23+
ERROR: (PGDuckDB/Duckdb_EndCustomScan_Cpp) Executor Error: SAVEPOINT is not supported in DuckDB
24+
CONTEXT: PL/pgSQL function inline_code_block line 5 at FOR over SELECT rows

test/regression/schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ test: function
3232
test: issue_410
3333
test: issue_730
3434
test: issue_748
35+
test: issue_761
3536
test: timestamp_with_interval
3637
test: approx_count_distinct
3738
test: time_bucket

test/regression/sql/issue_761.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
SET duckdb.force_execution = TRUE;
2+
3+
DO $$
4+
DECLARE
5+
objtype text;
6+
BEGIN
7+
FOR objtype IN VALUES ('toast table'), ('index column'), ('sequence column'),
8+
('toast table column'), ('view column'), ('materialized view column')
9+
LOOP
10+
BEGIN
11+
PERFORM pg_get_object_address(objtype, '{one}', '{}');
12+
EXCEPTION WHEN invalid_parameter_value THEN
13+
RAISE WARNING 'error for %: %', objtype, sqlerrm;
14+
END;
15+
END LOOP;
16+
END;
17+
$$;

0 commit comments

Comments
 (0)