| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/transaction/meta_transaction.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/common/common.hpp" |
| 12 | #include "duckdb/common/atomic.hpp" |
| 13 | #include "duckdb/main/valid_checker.hpp" |
| 14 | #include "duckdb/common/types/timestamp.hpp" |
| 15 | #include "duckdb/common/unordered_map.hpp" |
| 16 | #include "duckdb/common/optional_ptr.hpp" |
| 17 | |
| 18 | namespace duckdb { |
| 19 | class AttachedDatabase; |
| 20 | class ClientContext; |
| 21 | class Transaction; |
| 22 | |
| 23 | //! The MetaTransaction manages multiple transactions for different attached databases |
| 24 | class MetaTransaction { |
| 25 | public: |
| 26 | DUCKDB_API MetaTransaction(ClientContext &context, timestamp_t start_timestamp, idx_t catalog_version); |
| 27 | |
| 28 | ClientContext &context; |
| 29 | //! The timestamp when the transaction started |
| 30 | timestamp_t start_timestamp; |
| 31 | //! The catalog version when the transaction was started |
| 32 | idx_t catalog_version; |
| 33 | //! The validity checker of the transaction |
| 34 | ValidChecker transaction_validity; |
| 35 | //! Whether or not any transaction have made modifications |
| 36 | bool read_only; |
| 37 | //! The active query number |
| 38 | transaction_t active_query; |
| 39 | |
| 40 | public: |
| 41 | DUCKDB_API static MetaTransaction &Get(ClientContext &context); |
| 42 | timestamp_t GetCurrentTransactionStartTimestamp() { |
| 43 | return start_timestamp; |
| 44 | } |
| 45 | |
| 46 | Transaction &GetTransaction(AttachedDatabase &db); |
| 47 | |
| 48 | string Commit(); |
| 49 | void Rollback(); |
| 50 | |
| 51 | idx_t GetActiveQuery(); |
| 52 | void SetActiveQuery(transaction_t query_number); |
| 53 | |
| 54 | void ModifyDatabase(AttachedDatabase &db); |
| 55 | optional_ptr<AttachedDatabase> ModifiedDatabase() { |
| 56 | return modified_database; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | //! The set of active transactions for each database |
| 61 | unordered_map<AttachedDatabase *, Transaction *> transactions; |
| 62 | //! The set of transactions in order of when they were started |
| 63 | vector<optional_ptr<AttachedDatabase>> all_transactions; |
| 64 | //! The database we are modifying - we can only modify one database per transaction |
| 65 | optional_ptr<AttachedDatabase> modified_database; |
| 66 | }; |
| 67 | |
| 68 | } // namespace duckdb |
| 69 |