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::DuckTransaction Class Reference
Inheritance diagram for duckdb::DuckTransaction:
Collaboration diagram for duckdb::DuckTransaction:

Classes

struct  ActiveTableLock

Public Member Functions

 DuckTransaction (DuckTransactionManager &manager, ClientContext &context, transaction_t start_time, transaction_t transaction_id, idx_t catalog_version)
LocalStorage & GetLocalStorage ()
void PushCatalogEntry (CatalogEntry &entry, data_ptr_t extra_data, idx_t extra_data_size)
void PushAttach (AttachedDatabase &db)
void SetModifications (DatabaseModificationType type) override
 Sets the database modifications that are planned to be performed in this transaction.
bool ShouldWriteToWAL (AttachedDatabase &db)
ErrorData WriteToWAL (ClientContext &context, AttachedDatabase &db, unique_ptr< StorageCommitState > &commit_state) noexcept
ErrorData Commit (AttachedDatabase &db, CommitInfo &commit_info, unique_ptr< StorageCommitState > commit_state) noexcept
bool AutomaticCheckpoint (AttachedDatabase &db, const UndoBufferProperties &properties)
 Returns whether or not a commit of this transaction should trigger an automatic checkpoint.
ErrorData Rollback ()
 Rollback.
void Cleanup (transaction_t lowest_active_transaction)
 Cleanup the undo buffer.
bool ChangesMade ()
UndoBufferProperties GetUndoProperties ()
void PushDelete (DataTable &table, RowVersionManager &info, idx_t vector_idx, row_t rows[], idx_t count, idx_t base_row)
void PushSequenceUsage (SequenceCatalogEntry &entry, const SequenceData &data)
void PushAppend (DataTable &table, idx_t row_start, idx_t row_count)
UndoBufferReference CreateUpdateInfo (idx_t type_size, DataTable &data_table, idx_t entries, idx_t row_group_start)
DuckTransactionManager & GetTransactionManager ()
bool IsDuckTransaction () const override
unique_ptr< StorageLockKey > TryGetCheckpointLock ()
shared_ptr< CheckpointLock > SharedLockTable (DataTableInfo &info)
 Get a shared lock on a table.
void ModifyTable (DataTable &tbl)
 Hold an owning reference of the table, needed to safely reference it inside the transaction commit/undo logic.
Public Member Functions inherited from duckdb::Transaction
DUCKDB_API Transaction (TransactionManager &manager, ClientContext &context)
DUCKDB_API bool IsReadOnly ()
 Whether or not the transaction has made any modifications to the database so far.
virtual DUCKDB_API void SetReadWrite ()
 Promotes the transaction to a read-write transaction.
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Static Public Member Functions

static DuckTransaction & Get (ClientContext &context, AttachedDatabase &db)
static DuckTransaction & Get (ClientContext &context, Catalog &catalog)
Static Public Member Functions inherited from duckdb::Transaction
static DUCKDB_API Transaction & Get (ClientContext &context, AttachedDatabase &db)
static DUCKDB_API Transaction & Get (ClientContext &context, Catalog &catalog)
static DUCKDB_API optional_ptr< Transaction > TryGet (ClientContext &context, AttachedDatabase &db)
 Returns the transaction for the given context if it has already been started.

Public Attributes

transaction_t start_time
 The start timestamp of this transaction.
transaction_t transaction_id
 The transaction id of this transaction.
transaction_t commit_id
 The commit id of this transaction, if it has successfully been committed.
atomic< idx_t > catalog_version
bool awaiting_cleanup
Public Attributes inherited from duckdb::Transaction
TransactionManager & manager
weak_ptr< ClientContext > context
atomic< transaction_t > active_query

Private Attributes

UndoBuffer undo_buffer
unique_ptr< LocalStorage > storage
 The set of uncommitted appends for the transaction.
unique_ptr< StorageLockKey > checkpoint_lock
 Lock that prevents checkpoints from starting.
unique_ptr< StorageLockKey > vacuum_lock
 Lock that prevents vacuums from starting.
mutex sequence_lock
 Lock for accessing sequence_usage.
reference_map_t< SequenceCatalogEntry, reference< SequenceValue > > sequence_usage
 Map of all sequences that were used during the transaction and the value they had in this transaction.
mutex modified_tables_lock
 Lock for modified_tables.
reference_map_t< DataTable, shared_ptr< DataTable > > modified_tables
 Tables that are modified by this transaction.
mutex active_locks_lock
 Lock for the active_locks map.
reference_map_t< DataTableInfo, unique_ptr< ActiveTableLock > > active_locks
 Active locks on tables.

Constructor & Destructor Documentation

◆ DuckTransaction()

duckdb::DuckTransaction::DuckTransaction ( DuckTransactionManager & manager,
ClientContext & context,
transaction_t start_time,
transaction_t transaction_id,
idx_t catalog_version )
446138 : Transaction(manager, context_p), start_time(start_time), transaction_id(transaction_id), commit_id(0),
446139 catalog_version(catalog_version_p), awaiting_cleanup(false), undo_buffer(*this, context_p),
446140 storage(make_uniq<LocalStorage>(context_p, *this)) {
446141}
bool awaiting_cleanup
Definition duckdb.cpp:10078
UndoBuffer undo_buffer
Definition duckdb.cpp:10130
transaction_t start_time
The start timestamp of this transaction.
Definition duckdb.cpp:10067
unique_ptr< LocalStorage > storage
The set of uncommitted appends for the transaction.
Definition duckdb.cpp:10132
transaction_t commit_id
The commit id of this transaction, if it has successfully been committed.
Definition duckdb.cpp:10071
transaction_t transaction_id
The transaction id of this transaction.
Definition duckdb.cpp:10069

◆ ~DuckTransaction()

duckdb::DuckTransaction::~DuckTransaction ( )
override
446143 {
446144}

Member Function Documentation

◆ Get() [1/2]

DuckTransaction & duckdb::DuckTransaction::Get ( ClientContext & context,
AttachedDatabase & db )
static
446146 {
446147 return DuckTransaction::Get(context, db.GetCatalog());
446148}
db

◆ Get() [2/2]

DuckTransaction & duckdb::DuckTransaction::Get ( ClientContext & context,
Catalog & catalog )
static
446150 {
446151 auto &transaction = Transaction::Get(context, catalog);
446152 if (!transaction.IsDuckTransaction()) {
446153 throw InternalException("DuckTransaction::Get called on non-DuckDB transaction");
446154 }
446155 return transaction.Cast<DuckTransaction>();
446156}

◆ GetLocalStorage()

LocalStorage & duckdb::DuckTransaction::GetLocalStorage ( )
446162 {
446163 return *storage;
446164}

◆ PushCatalogEntry()

void duckdb::DuckTransaction::PushCatalogEntry ( CatalogEntry & entry,
data_ptr_t extra_data,
idx_t extra_data_size )
446166 {
446167 idx_t alloc_size = sizeof(CatalogEntry *);
446168 if (extra_data_size > 0) {
446169 alloc_size += extra_data_size + sizeof(idx_t);
446170 }
446171
446172 auto undo_entry = undo_buffer.CreateEntry(UndoFlags::CATALOG_ENTRY, alloc_size);
446173 auto ptr = undo_entry.Ptr();
446174 // store the pointer to the catalog entry
446175 Store<CatalogEntry *>(&entry, ptr);
446176 if (extra_data_size > 0) {
446177 // copy the extra data behind the catalog entry pointer (if any)
446178 ptr += sizeof(CatalogEntry *);
446179 // first store the extra data size
446180 Store<idx_t>(extra_data_size, ptr);
446181 ptr += sizeof(idx_t);
446182 // then copy over the actual data
446183 memcpy(ptr, extra_data, extra_data_size);
446184 }
446185}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ PushAttach()

void duckdb::DuckTransaction::PushAttach ( AttachedDatabase & db)
446187 {
446188 auto undo_entry = undo_buffer.CreateEntry(UndoFlags::ATTACHED_DATABASE, sizeof(AttachedDatabase *));
446189 auto ptr = undo_entry.Ptr();
446190 // store the pointer to the database
446191 Store<CatalogEntry *>(&db, ptr);
446192}

◆ SetModifications()

void duckdb::DuckTransaction::SetModifications ( DatabaseModificationType type)
overridevirtual

Sets the database modifications that are planned to be performed in this transaction.

Reimplemented from duckdb::Transaction.

446402 {
446403 if (!checkpoint_lock) {
446404 bool require_write_lock = false;
446405 require_write_lock = require_write_lock || type.UpdateData();
446406 require_write_lock = require_write_lock || type.AlterTable();
446407 require_write_lock = require_write_lock || type.CreateCatalogEntry();
446408 require_write_lock = require_write_lock || type.DropCatalogEntry();
446409 require_write_lock = require_write_lock || type.Sequence();
446410 require_write_lock = require_write_lock || type.CreateIndex();
446411
446412 if (require_write_lock) {
446413 // obtain a shared checkpoint lock to prevent concurrent checkpoints while this transaction is running
446414 checkpoint_lock = GetTransactionManager().SharedCheckpointLock();
446415 }
446416 }
446417 if (!vacuum_lock) {
446418 bool require_vacuum_lock = false;
446419 require_vacuum_lock = require_vacuum_lock || type.InsertData();
446420 require_vacuum_lock = require_vacuum_lock || type.DeleteData();
446421
446422 if (require_vacuum_lock) {
446423 vacuum_lock = GetTransactionManager().SharedVacuumLock();
446424 }
446425 }
446426}
unique_ptr< StorageLockKey > vacuum_lock
Lock that prevents vacuums from starting.
Definition duckdb.cpp:10136
unique_ptr< StorageLockKey > checkpoint_lock
Lock that prevents checkpoints from starting.
Definition duckdb.cpp:10134

◆ ShouldWriteToWAL()

bool duckdb::DuckTransaction::ShouldWriteToWAL ( AttachedDatabase & db)
446300 {
446301 if (!ChangesMade()) {
446302 return false;
446303 }
446304 if (db.IsSystem()) {
446305 return false;
446306 }
446307 if (db.GetRecoveryMode() == RecoveryMode::NO_WAL_WRITES) {
446308 // WAL writes are explicitly disabled
446309 return false;
446310 }
446311 auto &storage_manager = db.GetStorageManager();
446312 if (!storage_manager.HasWAL()) {
446313 return false;
446314 }
446315 return true;
446316}

◆ WriteToWAL()

ErrorData duckdb::DuckTransaction::WriteToWAL ( ClientContext & context,
AttachedDatabase & db,
unique_ptr< StorageCommitState > & commit_state )
noexcept
446319 {
446320 ErrorData error_data;
446321 try {
446322 D_ASSERT(ShouldWriteToWAL(db));
446323 auto &storage_manager = db.GetStorageManager();
446324 auto wal = storage_manager.GetWAL();
446325 commit_state = storage_manager.GenStorageCommitState(*wal);
446326
446327 auto &profiler = *context.client_data->profiler;
446328
446329 auto commit_timer = profiler.StartTimer(MetricType::COMMIT_LOCAL_STORAGE_LATENCY);
446330 storage->Commit(commit_state.get());
446331
446332 auto wal_timer = profiler.StartTimer(MetricType::WRITE_TO_WAL_LATENCY);
446333 undo_buffer.WriteToWAL(*wal, commit_state.get());
446334 if (commit_state->HasRowGroupData()) {
446335 // if we have optimistically written any data AND we are writing to the WAL, we have written references to
446336 // optimistically written blocks
446337 // hence we need to ensure those optimistically written blocks are persisted
446338 storage_manager.GetBlockManager().FileSync();
446339 }
446340 } catch (std::exception &ex) {
446341 // Call RevertCommit() outside this try-catch as it itself may throw
446342 error_data = ErrorData(ex);
446343 }
446344
446345 if (commit_state && error_data.HasError()) {
446346 try {
446347 commit_state->RevertCommit();
446348 commit_state.reset();
446349 } catch (std::exception &) {
446350 // Ignore this error. If we fail to RevertCommit(), just return the original exception
446351 }
446352 }
446353
446354 return error_data;
446355}
bool HasError() const
Returns true, if this error data contains an exception, else false.
Definition duckdb.hpp:6527

◆ Commit()

ErrorData duckdb::DuckTransaction::Commit ( AttachedDatabase & db,
CommitInfo & commit_info,
unique_ptr< StorageCommitState > commit_state )
noexcept

Commit the current transaction with the given commit identifier. Returns an error message if the transaction commit failed, or an empty string if the commit was sucessful

446358 {
446359 this->commit_id = commit_info.commit_id;
446360 if (!ChangesMade()) {
446361 // no need to flush anything if we made no changes
446362 return ErrorData();
446363 }
446364 D_ASSERT(db.IsSystem() || db.IsTemporary() || !IsReadOnly());
446365
446366 UndoBuffer::IteratorState iterator_state;
446367 try {
446368 storage->Commit(commit_state.get());
446369 undo_buffer.Commit(iterator_state, commit_info);
446370 // if (DebugForceAbortCommit()) {
446371 // throw InvalidInputException("Force revert");
446372 // }
446373 if (commit_state) {
446374 // if we have written to the WAL - flush after the commit has been successful
446375 commit_state->FlushCommit();
446376 }
446377 return ErrorData();
446378 } catch (std::exception &ex) {
446379 undo_buffer.RevertCommit(iterator_state, this->transaction_id);
446380 if (commit_state) {
446381 // if we have written to the WAL - truncate the WAL on failure
446382 commit_state->RevertCommit();
446383 }
446384 return ErrorData(ex);
446385 }
446386}
DUCKDB_API bool IsReadOnly()
Whether or not the transaction has made any modifications to the database so far.
Definition duckdb.cpp:447429
Here is the call graph for this function:

◆ AutomaticCheckpoint()

bool duckdb::DuckTransaction::AutomaticCheckpoint ( AttachedDatabase & db,
const UndoBufferProperties & properties )

Returns whether or not a commit of this transaction should trigger an automatic checkpoint.

446285 {
446286 if (!ChangesMade()) {
446287 // read-only transactions cannot trigger an automated checkpoint
446288 return false;
446289 }
446290 if (db.IsReadOnly()) {
446291 // when attaching a database in read-only mode we cannot checkpoint
446292 // note that attaching a database in read-only mode does NOT mean we never make changes
446293 // WAL replay can make changes to the database - but only in the in-memory copy of the
446294 return false;
446295 }
446296 auto &storage_manager = db.GetStorageManager();
446297 return storage_manager.AutomaticCheckpoint(properties.estimated_size);
446298}
Here is the caller graph for this function:

◆ Rollback()

ErrorData duckdb::DuckTransaction::Rollback ( )

Rollback.

446388 {
446389 try {
446390 storage->Rollback();
446391 undo_buffer.Rollback();
446392 return ErrorData();
446393 } catch (std::exception &ex) {
446394 return ErrorData(ex);
446395 }
446396}

◆ Cleanup()

void duckdb::DuckTransaction::Cleanup ( transaction_t lowest_active_transaction)

Cleanup the undo buffer.

446398 {
446399 undo_buffer.Cleanup(lowest_active_transaction);
446400}

◆ ChangesMade()

bool duckdb::DuckTransaction::ChangesMade ( )
446275 {
446276 return undo_buffer.ChangesMade() || storage->ChangesMade();
446277}

◆ GetUndoProperties()

UndoBufferProperties duckdb::DuckTransaction::GetUndoProperties ( )
446279 {
446280 auto properties = undo_buffer.GetProperties();
446281 properties.estimated_size += storage->EstimatedSize();
446282 return properties;
446283}

◆ PushDelete()

void duckdb::DuckTransaction::PushDelete ( DataTable & table,
RowVersionManager & info,
idx_t vector_idx,
row_t rows[],
idx_t count,
idx_t base_row )
446195 {
446196 ModifyTable(table);
446197 bool is_consecutive = true;
446198 // check if the rows are consecutive
446199 for (idx_t i = 0; i < count; i++) {
446200 if (rows[i] != row_t(i)) {
446201 is_consecutive = false;
446202 break;
446203 }
446204 }
446205 idx_t alloc_size = sizeof(DeleteInfo);
446206 if (!is_consecutive) {
446207 // if rows are not consecutive we need to allocate row identifiers
446208 alloc_size += sizeof(uint16_t) * count;
446209 }
446210
446211 auto undo_entry = undo_buffer.CreateEntry(UndoFlags::DELETE_TUPLE, alloc_size);
446212 auto delete_info = reinterpret_cast<DeleteInfo *>(undo_entry.Ptr());
446213 delete_info->version_info = &info;
446214 delete_info->vector_idx = vector_idx;
446215 delete_info->table = &table;
446216 delete_info->count = count;
446217 delete_info->base_row = base_row;
446218 delete_info->is_consecutive = is_consecutive;
446219 if (!is_consecutive) {
446220 // if rows are not consecutive
446221 auto delete_rows = delete_info->GetRows();
446222 for (idx_t i = 0; i < count; i++) {
446223 delete_rows[i] = NumericCast<uint16_t>(rows[i]);
446224 }
446225 }
446226}
void ModifyTable(DataTable &tbl)
Hold an owning reference of the table, needed to safely reference it inside the transaction commit/un...
Definition duckdb.cpp:446264
int rows
::uint16_t uint16_t
int64_t row_t
The type used for row identifiers.
Definition duckdb.hpp:240

◆ PushSequenceUsage()

void duckdb::DuckTransaction::PushSequenceUsage ( SequenceCatalogEntry & entry,
const SequenceData & data )
446246 {
446247 lock_guard<mutex> l(sequence_lock);
446248 auto entry = sequence_usage.find(sequence);
446249 if (entry == sequence_usage.end()) {
446250 auto undo_entry = undo_buffer.CreateEntry(UndoFlags::SEQUENCE_VALUE, sizeof(SequenceValue));
446251 auto sequence_info = reinterpret_cast<SequenceValue *>(undo_entry.Ptr());
446252 sequence_info->entry = &sequence;
446253 sequence_info->usage_count = data.usage_count;
446254 sequence_info->counter = data.counter;
446255 sequence_usage.emplace(sequence, *sequence_info);
446256 } else {
446257 auto &sequence_info = entry->second.get();
446258 D_ASSERT(RefersToSameObject(*sequence_info.entry, sequence));
446259 sequence_info.usage_count = data.usage_count;
446260 sequence_info.counter = data.counter;
446261 }
446262}
reference_map_t< SequenceCatalogEntry, reference< SequenceValue > > sequence_usage
Map of all sequences that were used during the transaction and the value they had in this transaction...
Definition duckdb.cpp:10140
mutex sequence_lock
Lock for accessing sequence_usage.
Definition duckdb.cpp:10138
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

◆ PushAppend()

void duckdb::DuckTransaction::PushAppend ( DataTable & table,
idx_t row_start,
idx_t row_count )
446228 {
446229 ModifyTable(table);
446230 auto undo_entry = undo_buffer.CreateEntry(UndoFlags::INSERT_TUPLE, sizeof(AppendInfo));
446231 auto append_info = reinterpret_cast<AppendInfo *>(undo_entry.Ptr());
446232 append_info->table = &table;
446233 append_info->start_row = start_row;
446234 append_info->count = row_count;
446235}

◆ CreateUpdateInfo()

UndoBufferReference duckdb::DuckTransaction::CreateUpdateInfo ( idx_t type_size,
DataTable & data_table,
idx_t entries,
idx_t row_group_start )
446238 {
446239 idx_t alloc_size = UpdateInfo::GetAllocSize(type_size);
446240 auto undo_entry = undo_buffer.CreateEntry(UndoFlags::UPDATE_TUPLE, alloc_size);
446241 auto &update_info = UpdateInfo::Get(undo_entry);
446242 UpdateInfo::Initialize(update_info, data_table, transaction_id, row_group_start);
446243 return undo_entry;
446244}
static void Initialize(UpdateInfo &info, DataTable &data_table, transaction_t transaction_id, idx_t row_group_start)
Initialize an UpdateInfo struct that has been allocated using GetAllocSize (i.e. has extra space afte...
Definition duckdb.cpp:438946
static idx_t GetAllocSize(idx_t type_size)
Returns the total allocation size for an UpdateInfo entry, together with space for the tuple data.
Definition duckdb.cpp:438942

◆ GetTransactionManager()

DuckTransactionManager & duckdb::DuckTransaction::GetTransactionManager ( )
446158 {
446159 return manager.Cast<DuckTransactionManager>();
446160}

◆ IsDuckTransaction()

bool duckdb::DuckTransaction::IsDuckTransaction ( ) const
inlineoverridevirtual

Reimplemented from duckdb::Transaction.

10115 {
10116 return true;
10117 }

◆ TryGetCheckpointLock()

unique_ptr< StorageLockKey > duckdb::DuckTransaction::TryGetCheckpointLock ( )
446428 {
446429 if (!checkpoint_lock) {
446430 return GetTransactionManager().TryGetCheckpointLock();
446431 } else {
446432 return GetTransactionManager().TryUpgradeCheckpointLock(*checkpoint_lock);
446433 }
446434}

◆ SharedLockTable()

shared_ptr< CheckpointLock > duckdb::DuckTransaction::SharedLockTable ( DataTableInfo & info)

Get a shared lock on a table.

446436 {
446437 unique_lock<mutex> transaction_lock(active_locks_lock);
446438 auto entry = active_locks.find(info);
446439 if (entry == active_locks.end()) {
446440 entry = active_locks.insert(entry, make_pair(std::ref(info), make_uniq<ActiveTableLock>()));
446441 }
446442 auto &active_table_lock = *entry->second;
446443 transaction_lock.unlock(); // release transaction-level lock before acquiring table-level lock
446444 lock_guard<mutex> table_lock(active_table_lock.checkpoint_lock_mutex);
446445 auto checkpoint_lock = active_table_lock.checkpoint_lock.lock();
446446 // check if it is expired (or has never been acquired yet)
446447 if (checkpoint_lock) {
446448 // not expired - return it
446449 return checkpoint_lock;
446450 }
446451 // no existing lock - obtain it
446452 checkpoint_lock = make_shared_ptr<CheckpointLock>(info.GetSharedLock());
446453 // store it for future reference
446454 active_table_lock.checkpoint_lock = checkpoint_lock;
446455 return checkpoint_lock;
446456}
reference_map_t< DataTableInfo, unique_ptr< ActiveTableLock > > active_locks
Active locks on tables.
Definition duckdb.cpp:10152
mutex active_locks_lock
Lock for the active_locks map.
Definition duckdb.cpp:10146
Here is the caller graph for this function:

◆ ModifyTable()

void duckdb::DuckTransaction::ModifyTable ( DataTable & tbl)

Hold an owning reference of the table, needed to safely reference it inside the transaction commit/undo logic.

446264 {
446265 lock_guard<mutex> guard(modified_tables_lock);
446266 auto table_ref = reference<DataTable>(tbl);
446267 auto entry = modified_tables.find(table_ref);
446268 if (entry != modified_tables.end()) {
446269 // already exists
446270 return;
446271 }
446272 modified_tables.insert(make_pair(table_ref, tbl.shared_from_this()));
446273}
reference_map_t< DataTable, shared_ptr< DataTable > > modified_tables
Tables that are modified by this transaction.
Definition duckdb.cpp:10144
mutex modified_tables_lock
Lock for modified_tables.
Definition duckdb.cpp:10142
Here is the caller graph for this function:

Member Data Documentation

◆ awaiting_cleanup

bool duckdb::DuckTransaction::awaiting_cleanup

Transactions undergo Cleanup, after (1) removing them directly in RemoveTransaction, or (2) after they enter cleanup_queue. Some (after rollback) enter cleanup_queue, but do not require Cleanup.

◆ undo_buffer

UndoBuffer duckdb::DuckTransaction::undo_buffer
private

The undo buffer is used to store old versions of rows that are updated or deleted


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