Skip to content

Commit

Permalink
Call GetActiveSnapshot when creating TX (#246)
Browse files Browse the repository at this point in the history
Co-authored-by: Jelte Fennema-Nio <[email protected]>
  • Loading branch information
Y-- and JelteF authored Oct 4, 2024
1 parent aa3c27c commit 01f3050
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 23 deletions.
11 changes: 1 addition & 10 deletions include/pgduckdb/catalog/pgduckdb_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,9 @@ extern "C" {

namespace duckdb {

class PostgresStorageExtensionInfo : public StorageExtensionInfo {
public:
PostgresStorageExtensionInfo(Snapshot snapshot) : snapshot(snapshot) {
}

public:
Snapshot snapshot;
};

class PostgresStorageExtension : public StorageExtension {
public:
PostgresStorageExtension(Snapshot snapshot);
PostgresStorageExtension();
};

} // namespace duckdb
3 changes: 1 addition & 2 deletions include/pgduckdb/catalog/pgduckdb_transaction_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace duckdb {

class PostgresTransactionManager : public TransactionManager {
public:
PostgresTransactionManager(AttachedDatabase &db_p, PostgresCatalog &catalog, Snapshot snapshot);
PostgresTransactionManager(AttachedDatabase &db_p, PostgresCatalog &catalog);

Transaction &StartTransaction(ClientContext &context) override;
ErrorData CommitTransaction(ClientContext &context, Transaction &transaction) override;
Expand All @@ -19,7 +19,6 @@ class PostgresTransactionManager : public TransactionManager {

private:
PostgresCatalog &catalog;
Snapshot snapshot;
mutex transaction_lock;
reference_map_t<Transaction, unique_ptr<Transaction>> transactions;
};
Expand Down
8 changes: 2 additions & 6 deletions src/catalog/pgduckdb_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ namespace duckdb {

static unique_ptr<TransactionManager>
CreateTransactionManager(StorageExtensionInfo *storage_info, AttachedDatabase &db, Catalog &catalog) {
auto &pg_storage_info = *(reinterpret_cast<PostgresStorageExtensionInfo *>(storage_info));
auto snapshot = pg_storage_info.snapshot;

return make_uniq<PostgresTransactionManager>(db, catalog.Cast<PostgresCatalog>(), snapshot);
return make_uniq<PostgresTransactionManager>(db, catalog.Cast<PostgresCatalog>());
}

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

} // namespace duckdb
8 changes: 4 additions & 4 deletions src/catalog/pgduckdb_transaction_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "pgduckdb/catalog/pgduckdb_transaction_manager.hpp"
#include "duckdb/main/attached_database.hpp"
#include "pgduckdb/pgduckdb_process_lock.hpp"

namespace duckdb {

PostgresTransactionManager::PostgresTransactionManager(AttachedDatabase &db_p, PostgresCatalog &catalog,
Snapshot snapshot)
: TransactionManager(db_p), catalog(catalog), snapshot(snapshot) {
PostgresTransactionManager::PostgresTransactionManager(AttachedDatabase &db_p, PostgresCatalog &catalog)
: TransactionManager(db_p), catalog(catalog) {
}

Transaction &
PostgresTransactionManager::StartTransaction(ClientContext &context) {
auto transaction = make_uniq<PostgresTransaction>(*this, context, catalog, snapshot);
auto transaction = make_uniq<PostgresTransaction>(*this, context, catalog, GetActiveSnapshot());
auto &result = *transaction;
lock_guard<mutex> l(transaction_lock);
transactions[result] = std::move(transaction);
Expand Down
2 changes: 1 addition & 1 deletion src/pgduckdb_duckdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ DuckDBManager::DuckDBManager() {

database = duckdb::make_uniq<duckdb::DuckDB>(nullptr, &config);
duckdb::DBConfig::GetConfig(*database->instance).storage_extensions["pgduckdb"] =
duckdb::make_uniq<duckdb::PostgresStorageExtension>(GetActiveSnapshot());
duckdb::make_uniq<duckdb::PostgresStorageExtension>();
duckdb::ExtensionInstallInfo extension_install_info;
database->instance->SetExtensionLoaded("pgduckdb", extension_install_info);

Expand Down
12 changes: 12 additions & 0 deletions test/regression/expected/transaction_errors.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE foo AS SELECT 'bar' AS t;
BEGIN; SET duckdb.execution = true; SELECT t::integer AS t1 FROM foo; ROLLBACK;
ERROR: (PGDuckDB/ExecuteQuery) Conversion Error: Could not convert string 'bar' to INT32
LINE 1: SELECT (t)::integer AS t1 FROM public.foo
^
SET duckdb.execution = true;
SELECT 1 FROM foo;
?column?
----------
1
(1 row)

1 change: 1 addition & 0 deletions test/regression/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ test: create_table_as
test: standard_conforming_strings
test: query_filter
test: altered_tables
test: transaction_errors
4 changes: 4 additions & 0 deletions test/regression/sql/transaction_errors.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE foo AS SELECT 'bar' AS t;
BEGIN; SET duckdb.execution = true; SELECT t::integer AS t1 FROM foo; ROLLBACK;
SET duckdb.execution = true;
SELECT 1 FROM foo;

0 comments on commit 01f3050

Please sign in to comment.