#include <userver/storages/postgres/transaction.hpp>
PostgreSQL transaction.
RAII wrapper for running transactions on PostgreSQL connections. Should be retrieved by calling storages::postgres::Cluster::Begin().
Non-copyable.
If the transaction is not explicitly finished (either committed or rolled back) it will roll itself back in the destructor.
Definition at line 144 of file transaction.hpp.
Shortcut transaction options constants | |
static constexpr TransactionOptions | RW {} |
Read-write read committed transaction. | |
static constexpr TransactionOptions | RO {TransactionOptions::kReadOnly} |
Read-only read committed transaction. | |
static constexpr TransactionOptions | Deferrable {TransactionOptions::Deferrable()} |
Read-only serializable deferrable transaction. | |
static constexpr std::size_t | kDefaultRowsInChunk = 1024 |
Transaction (Transaction &&) noexcept | |
Transaction & | operator= (Transaction &&) noexcept |
Transaction (const Transaction &)=delete | |
Transaction & | operator= (const Transaction &)=delete |
Query execution | |
Execute statement with arbitrary parameters. Suspends coroutine for execution.
std::size_t InsertInTransaction(storages::postgres::Transaction& transaction, std::string_view key) {
auto res = transaction.Execute("INSERT INTO keys VALUES ($1)", key);
return res.RowsAffected();
}
| |
template<typename... Args> | |
ResultSet | Execute (const Query &query, const Args &... args) |
template<typename... Args> | |
ResultSet | Execute (OptionalCommandControl statement_cmd_ctl, const Query &query, const Args &... args) |
ResultSet | Execute (const Query &query, const ParameterStore &store) |
ResultSet | Execute (OptionalCommandControl statement_cmd_ctl, const Query &query, const ParameterStore &store) |
template<typename Container> | |
void | ExecuteBulk (const Query &query, const Container &args, std::size_t chunk_rows=kDefaultRowsInChunk) |
template<typename Container> | |
void | ExecuteBulk (OptionalCommandControl statement_cmd_ctl, const Query &query, const Container &args, std::size_t chunk_rows=kDefaultRowsInChunk) |
template<typename Container> | |
void | ExecuteDecomposeBulk (const Query &query, const Container &args, std::size_t chunk_rows=kDefaultRowsInChunk) |
template<typename Container> | |
void | ExecuteDecomposeBulk (OptionalCommandControl statement_cmd_ctl, const Query &query, const Container &args, std::size_t chunk_rows=kDefaultRowsInChunk) |
template<typename... Args> | |
Portal | MakePortal (const Query &query, const Args &... args) |
Create a portal for fetching results of a statement with arbitrary parameters. | |
template<typename... Args> | |
Portal | MakePortal (OptionalCommandControl statement_cmd_ctl, const Query &query, const Args &... args) |
Create a portal for fetching results of a statement with arbitrary parameters and per-statement command control. | |
Portal | MakePortal (const Query &query, const ParameterStore &store) |
Create a portal for fetching results of a statement with stored parameters. | |
Portal | MakePortal (OptionalCommandControl statement_cmd_ctl, const Query &query, const ParameterStore &store) |
Create a portal for fetching results of a statement with stored parameters and per-statement command control. | |
void | SetParameter (const std::string ¶m_name, const std::string &value) |
void | Commit () |
void | Rollback () |
OptionalCommandControl | GetConnTransactionCommandControlDebug () const |
Used in tests. | |
TimeoutDuration | GetConnStatementTimeoutDebug () const |
void storages::postgres::Transaction::Commit | ( | ) |
Commit the transaction Suspends coroutine until command complete. After Commit or Rollback is called, the transaction is not usable any more.
|
inline |
Definition at line 187 of file transaction.hpp.
|
inline |
Execute statement with stored parameters.
Suspends coroutine for execution.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information.Definition at line 219 of file transaction.hpp.
|
inline |
Execute statement with arbitrary parameters and per-statement command control.
Suspends coroutine for execution.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information.Definition at line 203 of file transaction.hpp.
ResultSet storages::postgres::Transaction::Execute | ( | OptionalCommandControl | statement_cmd_ctl, |
const Query & | query, | ||
const ParameterStore & | store ) |
Execute statement with stored parameters and per-statement command control.
Suspends coroutine for execution.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information.void storages::postgres::Transaction::ExecuteBulk | ( | const Query & | query, |
const Container & | args, | ||
std::size_t | chunk_rows = kDefaultRowsInChunk ) |
Execute statement that uses an array of arguments splitting that array in chunks and executing the statement with a chunk of arguments.
Useful for statements that unnest their arguments to avoid the need to increase timeouts due to data amount growth.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information. Definition at line 378 of file transaction.hpp.
void storages::postgres::Transaction::ExecuteBulk | ( | OptionalCommandControl | statement_cmd_ctl, |
const Query & | query, | ||
const Container & | args, | ||
std::size_t | chunk_rows = kDefaultRowsInChunk ) |
Execute statement that uses an array of arguments splitting that array in chunks and executing the statement with a chunk of arguments.
Useful for statements that unnest their arguments to avoid the need to increase timeouts due to data amount growth.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information. Definition at line 386 of file transaction.hpp.
void storages::postgres::Transaction::ExecuteDecomposeBulk | ( | const Query & | query, |
const Container & | args, | ||
std::size_t | chunk_rows = kDefaultRowsInChunk ) |
Execute statement that uses an array of arguments transforming that array into N arrays of corresponding fields and executing the statement with a chunk of each of these arrays values. Basically, a column-wise ExecuteBulk.
Useful for statements that unnest their arguments to avoid the need to increase timeouts due to data amount growth, but providing an explicit mapping from Container::value_type
to PG type is infeasible for some reason (otherwise, use ExecuteBulk).
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information.Definition at line 399 of file transaction.hpp.
void storages::postgres::Transaction::ExecuteDecomposeBulk | ( | OptionalCommandControl | statement_cmd_ctl, |
const Query & | query, | ||
const Container & | args, | ||
std::size_t | chunk_rows = kDefaultRowsInChunk ) |
Execute statement that uses an array of arguments transforming that array into N arrays of corresponding fields and executing the statement with a chunk of each of these arrays values. Basically, a column-wise ExecuteBulk.
Useful for statements that unnest their arguments to avoid the need to increase timeouts due to data amount growth, but providing an explicit mapping from Container::value_type
to PG type is infeasible for some reason (otherwise, use ExecuteBulk).
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information.Definition at line 406 of file transaction.hpp.
|
inline |
Create a portal for fetching results of a statement with arbitrary parameters.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information. Definition at line 308 of file transaction.hpp.
|
inline |
Create a portal for fetching results of a statement with stored parameters.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information. Definition at line 329 of file transaction.hpp.
|
inline |
Create a portal for fetching results of a statement with arbitrary parameters and per-statement command control.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information. Definition at line 318 of file transaction.hpp.
Portal storages::postgres::Transaction::MakePortal | ( | OptionalCommandControl | statement_cmd_ctl, |
const Query & | query, | ||
const ParameterStore & | store ) |
Create a portal for fetching results of a statement with stored parameters and per-statement command control.
.sql
file and generate a header file with Query from it. See External SQL/YQL files for more information. void storages::postgres::Transaction::Rollback | ( | ) |
Rollback the transaction Suspends coroutine until command complete. After Commit or Rollback is called, the transaction is not usable any more.
void storages::postgres::Transaction::SetParameter | ( | const std::string & | param_name, |
const std::string & | value ) |
Set a connection parameter https://www.postgresql.org/docs/current/sql-set.html The parameter is set for this transaction only
|
staticconstexpr |
Read-only serializable deferrable transaction.
Definition at line 153 of file transaction.hpp.
|
staticconstexpr |
Definition at line 156 of file transaction.hpp.
|
staticconstexpr |
Read-only read committed transaction.
Definition at line 151 of file transaction.hpp.
|
staticconstexpr |
Read-write read committed transaction.
Definition at line 149 of file transaction.hpp.