Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::DuckTransactionManager Class Reference
Inheritance diagram for duckdb::DuckTransactionManager:
Collaboration diagram for duckdb::DuckTransactionManager:

Classes

struct  CheckpointDecision

Public Member Functions

 DuckTransactionManager (AttachedDatabase &db)
Transaction & StartTransaction (ClientContext &context) override
 Start a new transaction.
ErrorData CommitTransaction (ClientContext &context, Transaction &transaction) override
 Commit the given transaction.
void RollbackTransaction (Transaction &transaction) override
 Rollback the given transaction.
void Checkpoint (ClientContext &context, bool force=false) override
transaction_t LowestActiveId () const
transaction_t LowestActiveStart () const
transaction_t GetLastCommit () const
transaction_t GetActiveCheckpoint () const
transaction_t GetNewCheckpointId ()
void ResetCheckpointId ()
bool IsDuckTransactionManager () override
unique_ptr< StorageLockKey > SharedCheckpointLock ()
 Obtains a shared lock to the checkpoint lock.
unique_ptr< StorageLockKey > TryGetCheckpointLock ()
 Try to obtain an exclusive checkpoint lock.
unique_ptr< StorageLockKey > TryUpgradeCheckpointLock (StorageLockKey &lock)
unique_ptr< StorageLockKey > SharedVacuumLock ()
unique_ptr< StorageLockKey > TryGetVacuumLock ()
DUCKDB_API idx_t GetCatalogVersion (Transaction &transaction)
 Returns the current version of the catalog (incremented whenever anything changes, not stored between restarts).
void PushCatalogEntry (Transaction &transaction_p, CatalogEntry &entry, data_ptr_t extra_data=nullptr, idx_t extra_data_size=0)
void PushAttach (Transaction &transaction_p, AttachedDatabase &db)
Public Member Functions inherited from duckdb::TransactionManager
 TransactionManager (AttachedDatabase &db)
AttachedDatabase & GetDB ()
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Static Public Member Functions

static DuckTransactionManager & Get (AttachedDatabase &db)
Static Public Member Functions inherited from duckdb::TransactionManager
static TransactionManager & Get (AttachedDatabase &db)

Protected Member Functions

virtual void OnCommitCheckpointDecision (const CheckpointDecision &decision, DuckTransaction &transaction)

Private Member Functions

transaction_t GetCommitTimestamp ()
 Generates a new commit timestamp.
unique_ptr< DuckCleanupInfo > RemoveTransaction (DuckTransaction &transaction) noexcept
 Remove the given transaction from the list of active transactions.
unique_ptr< DuckCleanupInfo > RemoveTransaction (DuckTransaction &transaction, bool store_transaction) noexcept
 Remove the given transaction from the list of active transactions.
CheckpointDecision CanCheckpoint (DuckTransaction &transaction, unique_ptr< StorageLockKey > &checkpoint_lock, const UndoBufferProperties &properties)
 Whether or not we can checkpoint.
CheckpointDecision GetCheckpointType (DuckTransaction &transaction, const UndoBufferProperties &undo_properties)
 Get the checkpoint type of an automatic checkpoint.
bool HasOtherTransactions (DuckTransaction &transaction)
void CleanupTransactions ()

Private Attributes

transaction_t current_start_timestamp
 The current start timestamp used by transactions.
transaction_t current_transaction_id
 The current transaction ID used by transactions.
atomic< transaction_t > lowest_active_id
 The lowest active transaction id.
atomic< transaction_t > lowest_active_start
 The lowest active transaction timestamp.
atomic< transaction_t > last_commit
 The last commit timestamp.
atomic< transaction_t > active_checkpoint
 The currently active checkpoint.
vector< unique_ptr< DuckTransaction > > active_transactions
 Set of currently running transactions.
vector< unique_ptr< DuckTransaction > > recently_committed_transactions
 Set of recently committed transactions.
mutex transaction_lock
 The lock used for transaction operations.
StorageLock checkpoint_lock
 The checkpoint lock.
StorageLock vacuum_lock
 The vacuum lock - necessary to start vacuum operations.
mutex start_transaction_lock
 Lock necessary to start transactions only - used by FORCE CHECKPOINT to prevent new transactions from starting.
atomic< idx_t > last_uncommitted_catalog_version = {TRANSACTION_ID_START}
idx_t last_committed_version = 0
mutex cleanup_lock
 Only one cleanup can be active at any time.
mutex cleanup_queue_lock
 Changes to the cleanup queue must be synchronized.
queue< unique_ptr< DuckCleanupInfo > > cleanup_queue

Additional Inherited Members

Protected Attributes inherited from duckdb::TransactionManager
AttachedDatabase & db
 The attached database.

Detailed Description

The Transaction Manager is responsible for creating and managing transactions

Constructor & Destructor Documentation

◆ DuckTransactionManager()

duckdb::DuckTransactionManager::DuckTransactionManager ( AttachedDatabase & db)
explicit
446493 : TransactionManager(db) {
446494 // start timestamp starts at two
446496 // transaction ID starts very high:
446497 // it should be much higher than the current start timestamp
446498 // if transaction_id < start_timestamp for any set of active transactions
446499 // uncommitted data could be read by
446500 current_transaction_id = TRANSACTION_ID_START;
446501 lowest_active_id = TRANSACTION_ID_START;
446502 lowest_active_start = MAX_TRANSACTION_ID;
446503 active_checkpoint = MAX_TRANSACTION_ID;
446504 if (!db.GetCatalog().IsDuckCatalog()) {
446505 // Specifically the StorageManager of the DuckCatalog is relied on, with `db.GetStorageManager`
446506 throw InternalException("DuckTransactionManager should only be created together with a DuckCatalog");
446507 }
446508}
atomic< transaction_t > lowest_active_id
The lowest active transaction id.
Definition duckdb.cpp:22832
transaction_t current_transaction_id
The current transaction ID used by transactions.
Definition duckdb.cpp:22830
atomic< transaction_t > active_checkpoint
The currently active checkpoint.
Definition duckdb.cpp:22838
transaction_t current_start_timestamp
The current start timestamp used by transactions.
Definition duckdb.cpp:22828
atomic< transaction_t > lowest_active_start
The lowest active transaction timestamp.
Definition duckdb.cpp:22834
AttachedDatabase & db
The attached database.
Definition duckdb.hpp:40609

◆ ~DuckTransactionManager()

duckdb::DuckTransactionManager::~DuckTransactionManager ( )
override
446510 {
446511}

Member Function Documentation

◆ Get()

DuckTransactionManager & duckdb::DuckTransactionManager::Get ( AttachedDatabase & db)
static
446513 {
446514 auto &transaction_manager = TransactionManager::Get(db);
446515 if (!transaction_manager.IsDuckTransactionManager()) {
446516 throw InternalException("Calling DuckTransactionManager::Get on non-DuckDB transaction manager");
446517 }
446518 return reinterpret_cast<DuckTransactionManager &>(transaction_manager);
446519}

◆ StartTransaction()

Transaction & duckdb::DuckTransactionManager::StartTransaction ( ClientContext & context)
overridevirtual

Start a new transaction.

Implements duckdb::TransactionManager.

446521 {
446522 // obtain the transaction lock during this function
446523 auto &meta_transaction = MetaTransaction::Get(context);
446524 unique_ptr<lock_guard<mutex>> start_lock;
446525 if (!meta_transaction.IsReadOnly()) {
446526 start_lock = make_uniq<lock_guard<mutex>>(start_transaction_lock);
446527 }
446528 lock_guard<mutex> lock(transaction_lock);
446529 if (current_start_timestamp >= TRANSACTION_ID_START) { // LCOV_EXCL_START
446530 throw InternalException("Cannot start more transactions, ran out of "
446531 "transaction identifiers!");
446532 } // LCOV_EXCL_STOP
446533
446534 // obtain the start time and transaction ID of this transaction
446535 transaction_t start_time = current_start_timestamp++;
446536 transaction_t transaction_id = current_transaction_id++;
446537 if (active_transactions.empty()) {
446538 lowest_active_start = start_time;
446539 lowest_active_id = transaction_id;
446540 }
446541
446542 // create the actual transaction
446543 auto transaction = make_uniq<DuckTransaction>(*this, context, start_time, transaction_id, last_committed_version);
446544 auto &transaction_ref = *transaction;
446545
446546 // store it in the set of active transactions
446547 active_transactions.push_back(std::move(transaction));
446548 return transaction_ref;
446549}
mutex start_transaction_lock
Lock necessary to start transactions only - used by FORCE CHECKPOINT to prevent new transactions from...
Definition duckdb.cpp:22850
mutex transaction_lock
The lock used for transaction operations.
Definition duckdb.cpp:22844
vector< unique_ptr< DuckTransaction > > active_transactions
Set of currently running transactions.
Definition duckdb.cpp:22840
idx_t transaction_t
Type used for transaction timestamps.
Definition duckdb.hpp:253

◆ CommitTransaction()

ErrorData duckdb::DuckTransactionManager::CommitTransaction ( ClientContext & context,
Transaction & transaction )
overridevirtual

Commit the given transaction.

Implements duckdb::TransactionManager.

446761 {
446762 auto &transaction = transaction_p.Cast<DuckTransaction>();
446763 unique_lock<mutex> t_lock(transaction_lock);
446764 if (!db.IsSystem() && !db.IsTemporary()) {
446765 if (transaction.ChangesMade()) {
446766 if (transaction.IsReadOnly()) {
446767 throw InternalException("Attempting to commit a transaction that is read-only but has made changes - "
446768 "this should not be possible");
446769 }
446770 }
446771 }
446772
446773 // check if we can checkpoint
446774 unique_ptr<StorageLockKey> lock;
446775 auto undo_properties = transaction.GetUndoProperties();
446776 auto checkpoint_decision = CanCheckpoint(transaction, lock, undo_properties);
446777 ErrorData error;
446778 unique_ptr<lock_guard<mutex>> held_wal_lock;
446779 unique_ptr<StorageCommitState> commit_state;
446780 bool skip_wal_write_due_to_checkpoint = false;
446781 if (checkpoint_decision.can_checkpoint) {
446782 // we can perform an automatic checkpoint
446783 // we have two options:
446784 // either we write to the WAL, in which case we can perform concurrent commits while running
446785 // OR we skip writing to the WAL, in which case we cannot perform concurrent commits
446786 // the reason for this is that if we don't write this transactions' changes to the WAL
446787 // any failure during checkpoint will cause this transactions' changes to be lost,
446788 // while later concurrent commits will not be
446789 // this can cause undefined state, as those commits were made assuming this one was already committed
446790 if (undo_properties.estimated_size >= Settings::Get<AutoCheckpointSkipWalThresholdSetting>(context)) {
446791 skip_wal_write_due_to_checkpoint = true;
446792 }
446793 }
446794 bool should_write_to_wal = transaction.ShouldWriteToWAL(db);
446795 if (should_write_to_wal) {
446796 auto &storage_manager = db.GetStorageManager().Cast<SingleFileStorageManager>();
446797 // if we are committing changes and we are not doing a "checkpoint instead of WAL write"
446798 // we need to write to the WAL to make the changes durable
446799 // since WAL writes can take a long time - we grab the WAL lock here and unlock the transaction lock
446800 // read-only transactions can bypass this branch and start/commit while the WAL write is happening
446801 // unlock the transaction lock while we write to the WAL
446802 // note: we can only drop the transaction lock if we are NOT checkpointing
446803 // if we are checkpointing, we have already made certain decisions (e.g. the CheckpointType)
446804 t_lock.unlock();
446805 // grab the WAL lock and hold it until the entire commit is finished
446806 held_wal_lock = storage_manager.GetWALLock();
446807
446808 // Commit the changes to the WAL.
446809 if (!skip_wal_write_due_to_checkpoint) {
446810 error = transaction.WriteToWAL(context, db, commit_state);
446811 }
446812
446813 // after we finish writing to the WAL we grab the transaction lock again
446814 t_lock.lock();
446815 }
446816 if (!error.HasError() && checkpoint_decision.can_checkpoint) {
446817 // now that we have the transaction lock again, new transactions can't start
446818 // figure out the checkpoint type now
446819 checkpoint_decision = GetCheckpointType(transaction, undo_properties);
446820 if (should_write_to_wal && skip_wal_write_due_to_checkpoint && !checkpoint_decision.can_checkpoint) {
446821 // we have not written to the WAL but we have now realized we can't checkpoint after all
446822 // in order to commit we need backpeddle and write to the WAL after all
446823 D_ASSERT(held_wal_lock);
446824 // unlock the transaction lock while we are writing to the WAL
446825 t_lock.unlock();
446826 error = transaction.WriteToWAL(context, db, commit_state);
446827 t_lock.lock();
446828 skip_wal_write_due_to_checkpoint = false;
446829 }
446830 }
446831 // in-memory databases don't have a WAL - we estimate how large their changeset is based on the undo properties
446832 if (!db.IsSystem()) {
446833 auto &storage_manager = db.GetStorageManager();
446834 if (storage_manager.InMemory() || db.GetRecoveryMode() == RecoveryMode::NO_WAL_WRITES) {
446835 storage_manager.AddWALSize(undo_properties.estimated_size);
446836 }
446837 }
446838 // obtain a commit id for the transaction
446839 CommitInfo info;
446840 info.commit_id = GetCommitTimestamp();
446841
446842 // commit the UndoBuffer of the transaction
446843 if (!error.HasError()) {
446844 if (HasOtherTransactions(transaction)) {
446845 info.active_transactions = ActiveTransactionState::OTHER_TRANSACTIONS;
446846 } else {
446847 info.active_transactions = ActiveTransactionState::NO_OTHER_TRANSACTIONS;
446848 }
446849 error = transaction.Commit(db, info, std::move(commit_state));
446850 }
446851
446852 if (error.HasError()) {
446853 DUCKDB_LOG(context, TransactionLogType, db, "Rollback (after failed commit)", info.commit_id);
446854
446855 // COMMIT not successful: ROLLBACK.
446856 checkpoint_decision = CheckpointDecision(error.Message());
446857 transaction.commit_id = 0;
446858
446859 auto rollback_error = transaction.Rollback();
446860 if (rollback_error.HasError()) {
446861 throw FatalException(
446862 "Failed to rollback transaction. Cannot continue operation.\nOriginal Error: %s\nRollback Error: %s",
446863 error.Message(), rollback_error.Message());
446864 }
446865 } else {
446866 DUCKDB_LOG(context, TransactionLogType, db, "Commit", info.commit_id);
446867 last_commit = info.commit_id;
446868
446869 // check if catalog changes were made
446870 if (transaction.catalog_version >= TRANSACTION_ID_START) {
446871 transaction.catalog_version = ++last_committed_version;
446872 }
446873 }
446874 OnCommitCheckpointDecision(checkpoint_decision, transaction);
446875
446876 if (!checkpoint_decision.can_checkpoint && lock) {
446877 // we won't checkpoint after all due to an error during commit: unlock the checkpoint lock again
446878 skip_wal_write_due_to_checkpoint = false;
446879 lock.reset();
446880 }
446881
446882 // commit successful: remove the transaction id from the list of active transactions
446883 // potentially resulting in garbage collection
446884 bool store_transaction = undo_properties.has_updates || undo_properties.has_index_deletes ||
446885 undo_properties.has_catalog_changes || error.HasError();
446886
446887 // Remove the transaction from the list of active transactions and gather cleanup information.
446888 auto cleanup_info = RemoveTransaction(transaction, store_transaction);
446889 if (cleanup_info->ScheduleCleanup()) {
446890 lock_guard<mutex> q_lock(cleanup_queue_lock);
446891 cleanup_queue.emplace(std::move(cleanup_info));
446892 }
446893
446894 // We do not need to hold the transaction lock during cleanup of transactions,
446895 // as they (1) have been removed, or (2) enter cleanup_info.
446896 t_lock.unlock();
446897 // if we have skipped the WAL write due to checkpoint, we keep the WAL lock while checkpointing
446898 // this prevents any concurrent transactions from happening during this time
446899 if (!skip_wal_write_due_to_checkpoint) {
446900 held_wal_lock.reset();
446901 }
446902
446903 CleanupTransactions();
446904
446905 // now perform a checkpoint if (1) we are able to checkpoint, and (2) the WAL has reached sufficient size to
446906 // checkpoint
446907 if (checkpoint_decision.can_checkpoint) {
446908 if (!lock || lock->GetType() != StorageLockType::EXCLUSIVE) {
446909 throw InternalException("Checkpointing requires an exclusive lock to be held");
446910 }
446911 // we can unlock the transaction lock while checkpointing
446912 // checkpoint the database to disk
446913 CheckpointOptions options;
446914 options.action = CheckpointAction::ALWAYS_CHECKPOINT;
446915 options.type = checkpoint_decision.type;
446916 options.wal_lock = held_wal_lock.get();
446917 auto &storage_manager = db.GetStorageManager();
446918 try {
446919 storage_manager.CreateCheckpoint(context, options);
446920 } catch (std::exception &ex) {
446921 // a checkpoint failure here should not result in the commit being turned into a rollback
446922 // .. UNLESS we have skipped writing to the WAL and there are concurrent transactions active
446923 if (skip_wal_write_due_to_checkpoint) {
446924 error.Merge(ErrorData(ex));
446925 }
446926 }
446927 }
446928
446929 return error;
446930}
queue< unique_ptr< DuckCleanupInfo > > cleanup_queue
Definition duckdb.cpp:22862
atomic< transaction_t > last_commit
The last commit timestamp.
Definition duckdb.cpp:22836
transaction_t GetCommitTimestamp()
Generates a new commit timestamp.
Definition duckdb.cpp:446738
mutex cleanup_queue_lock
Changes to the cleanup queue must be synchronized.
Definition duckdb.cpp:22858
CheckpointDecision GetCheckpointType(DuckTransaction &transaction, const UndoBufferProperties &undo_properties)
Get the checkpoint type of an automatic checkpoint.
Definition duckdb.cpp:446625
unique_ptr< DuckCleanupInfo > RemoveTransaction(DuckTransaction &transaction) noexcept
Remove the given transaction from the list of active transactions.
Definition duckdb.cpp:446958
CheckpointDecision CanCheckpoint(DuckTransaction &transaction, unique_ptr< StorageLockKey > &checkpoint_lock, const UndoBufferProperties &properties)
Whether or not we can checkpoint.
Definition duckdb.cpp:446597
@ ALWAYS_CHECKPOINT
Always checkpoint regardless of whether or not there is data in the WAL to flush.
Definition duckdb.hpp:23126
Here is the call graph for this function:

◆ RollbackTransaction()

void duckdb::DuckTransactionManager::RollbackTransaction ( Transaction & transaction)
overridevirtual

Rollback the given transaction.

Implements duckdb::TransactionManager.

446932 {
446933 auto &transaction = transaction_p.Cast<DuckTransaction>();
446934
446935 DUCKDB_LOG(db.GetDatabase(), TransactionLogType, db, "Rollback", transaction.transaction_id);
446936
446937 ErrorData error;
446938 {
446939 // Obtain the transaction lock and roll back.
446940 lock_guard<mutex> t_lock(transaction_lock);
446941 error = transaction.Rollback();
446942
446943 // Remove the transaction from the list of active transactions and gather cleanup information.
446944 auto cleanup_info = RemoveTransaction(transaction);
446945 if (cleanup_info->ScheduleCleanup()) {
446946 lock_guard<mutex> q_lock(cleanup_queue_lock);
446947 cleanup_queue.emplace(std::move(cleanup_info));
446948 }
446949 }
446950
446951 CleanupTransactions();
446952
446953 if (error.HasError()) {
446954 throw FatalException("Failed to rollback transaction. Cannot continue operation.\nError: %s", error.Message());
446955 }
446956}
Here is the call graph for this function:

◆ Checkpoint()

void duckdb::DuckTransactionManager::Checkpoint ( ClientContext & context,
bool force = false )
overridevirtual

Implements duckdb::TransactionManager.

446671 {
446672 auto &storage_manager = db.GetStorageManager();
446673 auto current = Transaction::TryGet(context, db);
446674 if (current) {
446675 if (force) {
446676 throw TransactionException(
446677 "Cannot FORCE CHECKPOINT: the current transaction has been started for this database");
446678 } else {
446679 auto &duck_transaction = current->Cast<DuckTransaction>();
446680 if (duck_transaction.ChangesMade()) {
446681 throw TransactionException("Cannot CHECKPOINT: the current transaction has transaction local changes");
446682 }
446683 }
446684 }
446685
446686 unique_ptr<StorageLockKey> lock;
446687 if (!force) {
446688 // not a force checkpoint
446689 // try to get the checkpoint lock
446690 lock = checkpoint_lock.TryGetExclusiveLock();
446691 if (!lock) {
446692 // we could not manage to get the lock - cancel
446693 throw TransactionException("Cannot CHECKPOINT: there are other write transactions active. Try using FORCE "
446694 "CHECKPOINT to wait until all active transactions are finished");
446695 }
446696
446697 } else {
446698 // force checkpoint - wait to get an exclusive lock
446699 // grab the start_transaction_lock to prevent new transactions from starting
446700 lock_guard<mutex> start_lock(start_transaction_lock);
446701 // wait until any active transactions are finished
446702 while (!lock) {
446703 if (context.interrupted) {
446704 throw InterruptException();
446705 }
446706 lock = checkpoint_lock.TryGetExclusiveLock();
446707 }
446708 }
446709 CheckpointOptions options;
446710 if (GetLastCommit() > LowestActiveStart()) {
446711 // we cannot do a full checkpoint if any transaction needs to read old data
446713 }
446714
446715 storage_manager.CreateCheckpoint(context, options);
446716}
StorageLock checkpoint_lock
The checkpoint lock.
Definition duckdb.cpp:22846
static DUCKDB_API optional_ptr< Transaction > TryGet(ClientContext &context, AttachedDatabase &db)
Returns the transaction for the given context if it has already been started.
Definition duckdb.cpp:447091
@ CONCURRENT_CHECKPOINT
Definition duckdb.hpp:23135

◆ LowestActiveId()

transaction_t duckdb::DuckTransactionManager::LowestActiveId ( ) const
inline
22764 {
22765 return lowest_active_id;
22766 }

◆ LowestActiveStart()

transaction_t duckdb::DuckTransactionManager::LowestActiveStart ( ) const
inline
22767 {
22768 return lowest_active_start;
22769 }

◆ GetLastCommit()

transaction_t duckdb::DuckTransactionManager::GetLastCommit ( ) const
inline
22770 {
22771 return last_commit;
22772 }

◆ GetActiveCheckpoint()

transaction_t duckdb::DuckTransactionManager::GetActiveCheckpoint ( ) const
inline
22773 {
22774 return active_checkpoint;
22775 }

◆ GetNewCheckpointId()

transaction_t duckdb::DuckTransactionManager::GetNewCheckpointId ( )
446563 {
446564 if (active_checkpoint != MAX_TRANSACTION_ID) {
446565 throw InternalException(
446566 "DuckTransactionManager::GetNewCheckpointId requested a new id but active_checkpoint was already set");
446567 }
446568 auto result = last_commit.load();
446569 active_checkpoint = result;
446570 return result;
446571}

◆ ResetCheckpointId()

void duckdb::DuckTransactionManager::ResetCheckpointId ( )
446573 {
446574 active_checkpoint = MAX_TRANSACTION_ID;
446575}

◆ IsDuckTransactionManager()

bool duckdb::DuckTransactionManager::IsDuckTransactionManager ( )
inlineoverridevirtual

Reimplemented from duckdb::TransactionManager.

22779 {
22780 return true;
22781 }

◆ SharedCheckpointLock()

unique_ptr< StorageLockKey > duckdb::DuckTransactionManager::SharedCheckpointLock ( )

Obtains a shared lock to the checkpoint lock.

446718 {
446719 return checkpoint_lock.GetSharedLock();
446720}

◆ TryGetCheckpointLock()

unique_ptr< StorageLockKey > duckdb::DuckTransactionManager::TryGetCheckpointLock ( )

Try to obtain an exclusive checkpoint lock.

446726 {
446727 return checkpoint_lock.TryGetExclusiveLock();
446728}

◆ TryUpgradeCheckpointLock()

unique_ptr< StorageLockKey > duckdb::DuckTransactionManager::TryUpgradeCheckpointLock ( StorageLockKey & lock)
446722 {
446723 return checkpoint_lock.TryUpgradeCheckpointLock(lock);
446724}

◆ SharedVacuumLock()

unique_ptr< StorageLockKey > duckdb::DuckTransactionManager::SharedVacuumLock ( )
446730 {
446731 return vacuum_lock.GetSharedLock();
446732}
StorageLock vacuum_lock
The vacuum lock - necessary to start vacuum operations.
Definition duckdb.cpp:22848

◆ TryGetVacuumLock()

unique_ptr< StorageLockKey > duckdb::DuckTransactionManager::TryGetVacuumLock ( )
446734 {
446735 return vacuum_lock.TryGetExclusiveLock();
446736}

◆ GetCatalogVersion()

idx_t duckdb::DuckTransactionManager::GetCatalogVersion ( Transaction & transaction)

Returns the current version of the catalog (incremented whenever anything changes, not stored between restarts).

447035 {
447036 auto &transaction = transaction_p.Cast<DuckTransaction>();
447037 return transaction.catalog_version;
447038}

◆ PushCatalogEntry()

void duckdb::DuckTransactionManager::PushCatalogEntry ( Transaction & transaction_p,
duckdb::CatalogEntry & entry,
duckdb::data_ptr_t extra_data = nullptr,
duckdb::idx_t extra_data_size = 0 )
447041 {
447042 auto &transaction = transaction_p.Cast<DuckTransaction>();
447043 if (!db.IsSystem() && !db.IsTemporary() && transaction.IsReadOnly()) {
447044 throw InternalException("Attempting to do catalog changes on a transaction that is read-only - "
447045 "this should not be possible");
447046 }
447047 transaction.catalog_version = ++last_uncommitted_catalog_version;
447048 transaction.PushCatalogEntry(entry, extra_data, extra_data_size);
447049}

◆ PushAttach()

void duckdb::DuckTransactionManager::PushAttach ( Transaction & transaction_p,
AttachedDatabase & db )
447051 {
447052 auto &transaction = transaction_p.Cast<DuckTransaction>();
447053 if (!db.IsSystem()) {
447054 throw InternalException("Can only ATTACH in the system catalog");
447055 }
447056 transaction.catalog_version = ++last_uncommitted_catalog_version;
447057 transaction.PushAttach(attached_db);
447058}

◆ GetCommitTimestamp()

transaction_t duckdb::DuckTransactionManager::GetCommitTimestamp ( )
private

Generates a new commit timestamp.

446738 {
446739 return current_start_timestamp++;
446740}
Here is the caller graph for this function:

◆ RemoveTransaction() [1/2]

unique_ptr< DuckCleanupInfo > duckdb::DuckTransactionManager::RemoveTransaction ( DuckTransaction & transaction)
privatenoexcept

Remove the given transaction from the list of active transactions.

446958 {
446959 return RemoveTransaction(transaction, transaction.ChangesMade());
446960}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveTransaction() [2/2]

unique_ptr< DuckCleanupInfo > duckdb::DuckTransactionManager::RemoveTransaction ( DuckTransaction & transaction,
bool store_transaction )
privatenoexcept

Remove the given transaction from the list of active transactions.

446963 {
446964 auto cleanup_info = make_uniq<DuckCleanupInfo>();
446965
446966 // Find the transaction in the active transactions,
446967 // as well as the lowest start time, transaction id, and active query.
446968 idx_t t_index = active_transactions.size();
446969 auto lowest_start_time = TRANSACTION_ID_START;
446970 auto lowest_transaction_id = MAX_TRANSACTION_ID;
446971 auto active_checkpoint_id = active_checkpoint.load();
446972 for (idx_t i = 0; i < active_transactions.size(); i++) {
446973 if (active_transactions[i].get() == &transaction) {
446974 t_index = i;
446975 continue;
446976 }
446977 lowest_start_time = MinValue(lowest_start_time, active_transactions[i]->start_time);
446978 lowest_transaction_id = MinValue(lowest_transaction_id, active_transactions[i]->transaction_id);
446979 }
446980 if (active_checkpoint_id != MAX_TRANSACTION_ID && active_checkpoint_id < lowest_start_time) {
446981 lowest_start_time = active_checkpoint_id;
446982 }
446983 lowest_active_start = lowest_start_time;
446984 lowest_active_id = lowest_transaction_id;
446985 D_ASSERT(t_index != active_transactions.size());
446986
446987 // Decide if we need to store the transaction, or if we can schedule it for cleanup.
446988 auto current_transaction = std::move(active_transactions[t_index]);
446989 if (store_transaction) {
446990 // If the transaction made any changes, we need to keep it around.
446991 if (transaction.commit_id != 0) {
446992 // The transaction was committed.
446993 // We add it to the list of recently committed transactions.
446994 recently_committed_transactions.push_back(std::move(current_transaction));
446995 } else {
446996 // The transaction was aborted.
446997 cleanup_info->transactions.push_back(std::move(current_transaction));
446998 }
446999 } else if (transaction.ChangesMade()) {
447000 // We do not need to store the transaction, directly schedule it for cleanup.
447001 current_transaction->awaiting_cleanup = true;
447002 cleanup_info->transactions.push_back(std::move(current_transaction));
447003 }
447004 cleanup_info->lowest_start_time = lowest_start_time;
447005
447006 // Remove the transaction from the list of active transactions.
447007 active_transactions.unsafe_erase_at(t_index);
447008
447009 // Traverse the recently_committed transactions to see if we can move any
447010 // to the list of transactions awaiting GC.
447011 idx_t i = 0;
447012 for (; i < recently_committed_transactions.size(); i++) {
447013 D_ASSERT(recently_committed_transactions[i]);
447014 if (recently_committed_transactions[i]->commit_id >= lowest_start_time) {
447015 // recently_committed_transactions is ordered on commit_id.
447016 // Thus, if the current commit_id is greater than
447017 // lowest_start_time, any subsequent commit IDs are also greater.
447018 break;
447019 }
447020
447021 recently_committed_transactions[i]->awaiting_cleanup = true;
447022 cleanup_info->transactions.push_back(std::move(recently_committed_transactions[i]));
447023 }
447024
447025 if (i > 0) {
447026 // We moved these transactions to the list of transactions awaiting GC.
447027 auto start = recently_committed_transactions.begin();
447028 auto end = recently_committed_transactions.begin() + static_cast<int64_t>(i);
447029 recently_committed_transactions.erase(start, end);
447030 }
447031
447032 return cleanup_info;
447033}
vector< unique_ptr< DuckTransaction > > recently_committed_transactions
Set of recently committed transactions.
Definition duckdb.cpp:22842
__host__ cudaTextureObject_t get() const noexcept
::int64_t int64_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ CanCheckpoint()

DuckTransactionManager::CheckpointDecision duckdb::DuckTransactionManager::CanCheckpoint ( DuckTransaction & transaction,
unique_ptr< StorageLockKey > & checkpoint_lock,
const UndoBufferProperties & properties )
private

Whether or not we can checkpoint.

446598 {
446599 if (db.IsSystem()) {
446600 return CheckpointDecision("system transaction");
446601 }
446602 if (transaction.IsReadOnly()) {
446603 return CheckpointDecision("transaction is read-only");
446604 }
446605 auto &storage_manager = db.GetStorageManager();
446606 if (!storage_manager.IsLoaded()) {
446607 return CheckpointDecision("cannot checkpoint while loading");
446608 }
446609 if (!transaction.AutomaticCheckpoint(db, undo_properties)) {
446610 return CheckpointDecision("no reason to automatically checkpoint");
446611 }
446612 if (Settings::Get<DebugSkipCheckpointOnCommitSetting>(db.GetDatabase())) {
446613 return CheckpointDecision("checkpointing on commit disabled through configuration");
446614 }
446615 // try to lock the checkpoint lock
446616 lock = transaction.TryGetCheckpointLock();
446617 if (!lock) {
446618 return CheckpointDecision("Failed to obtain checkpoint lock - another thread is writing/checkpointing or "
446619 "another read transaction relies on data that is not yet committed");
446620 }
446622}
@ FULL_CHECKPOINT
Definition duckdb.hpp:23132
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCheckpointType()

DuckTransactionManager::CheckpointDecision duckdb::DuckTransactionManager::GetCheckpointType ( DuckTransaction & transaction,
const UndoBufferProperties & undo_properties )
private

Get the checkpoint type of an automatic checkpoint.

446625 {
446626 auto &storage_manager = db.GetStorageManager();
446627 auto checkpoint_type = CheckpointType::FULL_CHECKPOINT;
446628 bool has_other_transactions = HasOtherTransactions(transaction);
446629 if (has_other_transactions) {
446630 if (undo_properties.has_updates || undo_properties.has_dropped_entries) {
446631 // if we have made updates/catalog changes in this transaction we cannot checkpoint
446632 // in the presence of other transactions
446633 string other_transactions;
446634 for (auto &active_transaction : active_transactions) {
446635 if (!RefersToSameObject(*active_transaction, transaction)) {
446636 if (!other_transactions.empty()) {
446637 other_transactions += ", ";
446638 }
446639 other_transactions += "[" + to_string(active_transaction->transaction_id) + "]";
446640 }
446641 }
446642 if (!other_transactions.empty()) {
446643 // there are other transactions!
446644 // these active transactions might need data from BEFORE this transaction
446645 // we might need to change our strategy here based on what changes THIS transaction has made
446646 if (undo_properties.has_dropped_entries) {
446647 // this transaction has changed the catalog - we cannot checkpoint
446648 return CheckpointDecision(
446649 "Transaction has dropped catalog entries and there are other transactions "
446650 "active\nActive transactions: " +
446651 other_transactions);
446652 }
446653 // this transaction has performed updates - we cannot checkpoint
446654 return CheckpointDecision(
446655 "Transaction has performed updates and there are other transactions active\nActive transactions: " +
446656 other_transactions);
446657 }
446658 }
446659 // otherwise - we need to do a concurrent checkpoint
446660 checkpoint_type = CheckpointType::CONCURRENT_CHECKPOINT;
446661 }
446662 if (storage_manager.InMemory() && !storage_manager.CompressionIsEnabled()) {
446663 if (checkpoint_type == CheckpointType::CONCURRENT_CHECKPOINT) {
446664 return CheckpointDecision("Cannot vacuum, and compression is disabled for in-memory table");
446665 }
446667 }
446668 return CheckpointDecision(checkpoint_type);
446669}
std::string to_string(const T &value)
Definition duckdb.cpp:19425
@ VACUUM_ONLY
Only run vacuum - this can be triggered for in-memory tables.
Definition duckdb.hpp:23137
bool RefersToSameObject(const reference< T > &a, const reference< T > &b)
Returns whether or not two reference wrappers refer to the same object.
Definition duckdb.hpp:2191
Here is the call graph for this function:
Here is the caller graph for this function:

◆ HasOtherTransactions()

bool duckdb::DuckTransactionManager::HasOtherTransactions ( DuckTransaction & transaction)
private
446587 {
446588 for (auto &active_transaction : active_transactions) {
446589 if (!RefersToSameObject(*active_transaction, transaction)) {
446590 return true;
446591 }
446592 }
446593 return false;
446594}

◆ CleanupTransactions()

void duckdb::DuckTransactionManager::CleanupTransactions ( )
private
446742 {
446743 lock_guard<mutex> c_lock(cleanup_lock);
446744 while (true) {
446745 unique_ptr<DuckCleanupInfo> top_cleanup_info;
446746 {
446747 lock_guard<mutex> q_lock(cleanup_queue_lock);
446748 if (cleanup_queue.empty()) {
446749 // all transactions have been cleaned up - done
446750 return;
446751 }
446752 top_cleanup_info = std::move(cleanup_queue.front());
446753 cleanup_queue.pop();
446754 }
446755 if (top_cleanup_info) {
446756 top_cleanup_info->Cleanup();
446757 }
446758 }
446759}
mutex cleanup_lock
Only one cleanup can be active at any time.
Definition duckdb.cpp:22856

◆ OnCommitCheckpointDecision()

virtual void duckdb::DuckTransactionManager::OnCommitCheckpointDecision ( const CheckpointDecision & decision,
DuckTransaction & transaction )
inlineprotectedvirtual
22865 {
22866 }

Member Data Documentation

◆ last_uncommitted_catalog_version

atomic<idx_t> duckdb::DuckTransactionManager::last_uncommitted_catalog_version = {TRANSACTION_ID_START}
private
22852{TRANSACTION_ID_START};

◆ cleanup_queue

queue<unique_ptr<DuckCleanupInfo> > duckdb::DuckTransactionManager::cleanup_queue
private

Cleanups have to happen in-order. E.g., if one transaction drops a table, and another creates a table, inverting the cleanup order can result in catalog errors.


The documentation for this class was generated from the following file: