mirror of
https://github.com/postgres/postgres.git
synced 2025-06-27 23:21:58 +03:00
Transaction chaining
Add command variants COMMIT AND CHAIN and ROLLBACK AND CHAIN, which start new transactions with the same transaction characteristics as the just finished one, per SQL standard. Support for transaction chaining in PL/pgSQL is also added. This functionality is especially useful when running COMMIT in a loop in PL/pgSQL. Reviewed-by: Fabien COELHO <coelho@cri.ensmp.fr> Discussion: https://www.postgresql.org/message-id/flat/28536681-324b-10dc-ade8-ab46f7645a5a@2ndquadrant.com
This commit is contained in:
@ -217,8 +217,8 @@ SPI_start_transaction(void)
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
}
|
||||
|
||||
void
|
||||
SPI_commit(void)
|
||||
static void
|
||||
_SPI_commit(bool chain)
|
||||
{
|
||||
MemoryContext oldcontext = CurrentMemoryContext;
|
||||
|
||||
@ -250,14 +250,36 @@ SPI_commit(void)
|
||||
while (ActiveSnapshotSet())
|
||||
PopActiveSnapshot();
|
||||
|
||||
if (chain)
|
||||
SaveTransactionCharacteristics();
|
||||
|
||||
CommitTransactionCommand();
|
||||
|
||||
if (chain)
|
||||
{
|
||||
StartTransactionCommand();
|
||||
RestoreTransactionCharacteristics();
|
||||
}
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
|
||||
_SPI_current->internal_xact = false;
|
||||
}
|
||||
|
||||
void
|
||||
SPI_rollback(void)
|
||||
SPI_commit(void)
|
||||
{
|
||||
_SPI_commit(false);
|
||||
}
|
||||
|
||||
void
|
||||
SPI_commit_and_chain(void)
|
||||
{
|
||||
_SPI_commit(true);
|
||||
}
|
||||
|
||||
static void
|
||||
_SPI_rollback(bool chain)
|
||||
{
|
||||
MemoryContext oldcontext = CurrentMemoryContext;
|
||||
|
||||
@ -274,12 +296,34 @@ SPI_rollback(void)
|
||||
|
||||
_SPI_current->internal_xact = true;
|
||||
|
||||
if (chain)
|
||||
SaveTransactionCharacteristics();
|
||||
|
||||
AbortCurrentTransaction();
|
||||
|
||||
if (chain)
|
||||
{
|
||||
StartTransactionCommand();
|
||||
RestoreTransactionCharacteristics();
|
||||
}
|
||||
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
|
||||
_SPI_current->internal_xact = false;
|
||||
}
|
||||
|
||||
void
|
||||
SPI_rollback(void)
|
||||
{
|
||||
_SPI_rollback(false);
|
||||
}
|
||||
|
||||
void
|
||||
SPI_rollback_and_chain(void)
|
||||
{
|
||||
_SPI_rollback(true);
|
||||
}
|
||||
|
||||
/*
|
||||
* Clean up SPI state. Called on transaction end (of non-SPI-internal
|
||||
* transactions) and when returning to the main loop on error.
|
||||
|
Reference in New Issue
Block a user