Commit the given transaction.
446761 {
446762 auto &transaction = transaction_p.Cast<DuckTransaction>();
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
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
446783
446784
446785
446786
446787
446788
446789
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
446798
446799
446800
446801
446802
446803
446804 t_lock.unlock();
446805
446806 held_wal_lock = storage_manager.GetWALLock();
446807
446808
446809 if (!skip_wal_write_due_to_checkpoint) {
446810 error = transaction.WriteToWAL(context,
db, commit_state);
446811 }
446812
446813
446814 t_lock.lock();
446815 }
446816 if (!error.HasError() && checkpoint_decision.can_checkpoint) {
446817
446818
446820 if (should_write_to_wal && skip_wal_write_due_to_checkpoint && !checkpoint_decision.can_checkpoint) {
446821
446822
446823 D_ASSERT(held_wal_lock);
446824
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
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
446839 CommitInfo info;
446841
446842
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
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);
446868
446869
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
446878 skip_wal_write_due_to_checkpoint = false;
446879 lock.reset();
446880 }
446881
446882
446883
446884 bool store_transaction = undo_properties.has_updates || undo_properties.has_index_deletes ||
446885 undo_properties.has_catalog_changes || error.HasError();
446886
446887
446889 if (cleanup_info->ScheduleCleanup()) {
446892 }
446893
446894
446895
446896 t_lock.unlock();
446897
446898
446899 if (!skip_wal_write_due_to_checkpoint) {
446900 held_wal_lock.reset();
446901 }
446902
446903 CleanupTransactions();
446904
446905
446906
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
446912
446913 CheckpointOptions options;
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
446922
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
Definition duckdb.cpp:22799