11#include <access/xact.h>
18using command_id = ::CommandId;
28 ffi_guard{acquire ? ::GetCurrentTransactionId : ::GetCurrentTransactionIdIfAny}());
31 bool is_valid()
const {
return TransactionIdIsValid(id_); }
33 bool operator==(
const transaction_id &other)
const {
return TransactionIdEquals(id_, other.id_); }
34 bool operator>(
const transaction_id &other)
const {
return TransactionIdFollows(id_, other.id_); }
36 return TransactionIdFollowsOrEquals(id_, other.id_);
39 return TransactionIdPrecedes(id_, other.id_);
42 return TransactionIdPrecedesOrEquals(id_, other.id_);
45 bool did_abort()
const {
return is_valid() && TransactionIdDidAbort(id_); }
46 bool did_commit()
const {
return is_valid() && TransactionIdDidCommit(id_); }
70 : ctx(::CurrentMemoryContext), owner(::CurrentResourceOwner), should_commit(
commit),
71 uncaught(std::uncaught_exceptions()), finished(
false),
name(
"") {
72 ffi_guard{::BeginInternalSubTransaction}(
nullptr);
73 ::CurrentMemoryContext = ctx;
77 : ctx(::CurrentMemoryContext), owner(::CurrentResourceOwner), should_commit(
commit),
78 uncaught(std::uncaught_exceptions()), finished(
false),
name(
name) {
79 ffi_guard{::BeginInternalSubTransaction}(this->name.c_str());
80 ::CurrentMemoryContext = ctx;
92 ffi_guard{::ReleaseCurrentSubTransaction}();
101 ffi_guard{::RollbackAndReleaseCurrentSubTransaction}();
112 if (std::uncaught_exceptions() > uncaught || !should_commit) {
113 ::RollbackAndReleaseCurrentSubTransaction();
115 ::ReleaseCurrentSubTransaction();
118 "cppgres: finishing internal subtransaction failed");
123 void restore() noexcept {
124 ::CurrentMemoryContext = ctx;
125 ::CurrentResourceOwner = owner;
129 ::ResourceOwner owner;
137 transaction(
bool commit =
true) : should_commit(commit), released(
false) {
139 if (!::IsTransactionState()) {
140 ::SetCurrentStatementStartTimestamp();
141 ::StartTransactionCommand();
142 ::PushActiveSnapshot(::GetTransactionSnapshot());
150 ::PopActiveSnapshot();
152 ::CommitTransactionCommand();
154 ::AbortCurrentTransaction();
162 ::PopActiveSnapshot();
163 ::CommitTransactionCommand();
170 ::PopActiveSnapshot();
171 ::AbortCurrentTransaction();
Internal subtransaction guard.
Definition: xact.hpp:68
void rollback()
Roll the subtransaction back now.
Definition: xact.hpp:100
void commit()
Commit the subtransaction now.
Definition: xact.hpp:91