| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 2 | // DuckDB |
| 3 | // |
| 4 | // duckdb/transaction/duck_transaction_manager.hpp |
| 5 | // |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "duckdb/transaction/transaction_manager.hpp" |
| 12 | |
| 13 | namespace duckdb { |
| 14 | class DuckTransaction; |
| 15 | |
| 16 | //! The Transaction Manager is responsible for creating and managing |
| 17 | //! transactions |
| 18 | class DuckTransactionManager : public TransactionManager { |
| 19 | friend struct CheckpointLock; |
| 20 | |
| 21 | public: |
| 22 | explicit DuckTransactionManager(AttachedDatabase &db); |
| 23 | ~DuckTransactionManager(); |
| 24 | |
| 25 | public: |
| 26 | static DuckTransactionManager &Get(AttachedDatabase &db); |
| 27 | |
| 28 | //! Start a new transaction |
| 29 | Transaction *StartTransaction(ClientContext &context) override; |
| 30 | //! Commit the given transaction |
| 31 | string CommitTransaction(ClientContext &context, Transaction *transaction) override; |
| 32 | //! Rollback the given transaction |
| 33 | void RollbackTransaction(Transaction *transaction) override; |
| 34 | |
| 35 | void Checkpoint(ClientContext &context, bool force = false) override; |
| 36 | |
| 37 | transaction_t LowestActiveId() { |
| 38 | return lowest_active_id; |
| 39 | } |
| 40 | transaction_t LowestActiveStart() { |
| 41 | return lowest_active_start; |
| 42 | } |
| 43 | |
| 44 | bool IsDuckTransactionManager() override { |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | bool CanCheckpoint(optional_ptr<DuckTransaction> current = nullptr); |
| 50 | //! Remove the given transaction from the list of active transactions |
| 51 | void RemoveTransaction(DuckTransaction &transaction) noexcept; |
| 52 | void LockClients(vector<ClientLockWrapper> &client_locks, ClientContext &context); |
| 53 | |
| 54 | private: |
| 55 | //! The current start timestamp used by transactions |
| 56 | transaction_t current_start_timestamp; |
| 57 | //! The current transaction ID used by transactions |
| 58 | transaction_t current_transaction_id; |
| 59 | //! The lowest active transaction id |
| 60 | atomic<transaction_t> lowest_active_id; |
| 61 | //! The lowest active transaction timestamp |
| 62 | atomic<transaction_t> lowest_active_start; |
| 63 | //! Set of currently running transactions |
| 64 | vector<unique_ptr<DuckTransaction>> active_transactions; |
| 65 | //! Set of recently committed transactions |
| 66 | vector<unique_ptr<DuckTransaction>> recently_committed_transactions; |
| 67 | //! Transactions awaiting GC |
| 68 | vector<unique_ptr<DuckTransaction>> old_transactions; |
| 69 | //! The lock used for transaction operations |
| 70 | mutex transaction_lock; |
| 71 | |
| 72 | bool thread_is_checkpointing; |
| 73 | }; |
| 74 | |
| 75 | } // namespace duckdb |
| 76 |