mirror of
https://github.com/postgres/postgres.git
synced 2025-11-24 00:23:06 +03:00
Transaction control in PL procedures
In each of the supplied procedural languages (PL/pgSQL, PL/Perl, PL/Python, PL/Tcl), add language-specific commit and rollback functions/commands to control transactions in procedures in that language. Add similar underlying functions to SPI. Some additional cleanup so that transaction commit or abort doesn't blow away data structures still used by the procedure call. Add execution context tracking to CALL and DO statements so that transaction control commands can only be issued in top-level procedure and block calls, not function calls or other procedure or block calls. - SPI Add a new function SPI_connect_ext() that is like SPI_connect() but allows passing option flags. The only option flag right now is SPI_OPT_NONATOMIC. A nonatomic SPI connection can execute transaction control commands, otherwise it's not allowed. This is meant to be passed down from CALL and DO statements which themselves know in which context they are called. A nonatomic SPI connection uses different memory management. A normal SPI connection allocates its memory in TopTransactionContext. For nonatomic connections we use PortalContext instead. As the comment in SPI_connect_ext() (previously SPI_connect()) indicates, one could potentially use PortalContext in all cases, but it seems safest to leave the existing uses alone, because this stuff is complicated enough already. SPI also gets new functions SPI_start_transaction(), SPI_commit(), and SPI_rollback(), which can be used by PLs to implement their transaction control logic. - portalmem.c Some adjustments were made in the code that cleans up portals at transaction abort. The portal code could already handle a command *committing* a transaction and continuing (e.g., VACUUM), but it was not quite prepared for a command *aborting* a transaction and continuing. In AtAbort_Portals(), remove the code that marks an active portal as failed. As the comment there already predicted, this doesn't work if the running command wants to keep running after transaction abort. And it's actually not necessary, because pquery.c is careful to run all portal code in a PG_TRY block and explicitly runs MarkPortalFailed() if there is an exception. So the code in AtAbort_Portals() is never used anyway. In AtAbort_Portals() and AtCleanup_Portals(), we need to be careful not to clean up active portals too much. This mirrors similar code in PreCommit_Portals(). - PL/Perl Gets new functions spi_commit() and spi_rollback() - PL/pgSQL Gets new commands COMMIT and ROLLBACK. Update the PL/SQL porting example in the documentation to reflect that transactions are now possible in procedures. - PL/Python Gets new functions plpy.commit and plpy.rollback. - PL/Tcl Gets new commands commit and rollback. Reviewed-by: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
This commit is contained in:
@@ -742,11 +742,8 @@ PreCommit_Portals(bool isPrepare)
|
||||
/*
|
||||
* Abort processing for portals.
|
||||
*
|
||||
* At this point we reset "active" status and run the cleanup hook if
|
||||
* present, but we can't release the portal's memory until the cleanup call.
|
||||
*
|
||||
* The reason we need to reset active is so that we can replace the unnamed
|
||||
* portal, else we'll fail to execute ROLLBACK when it arrives.
|
||||
* At this point we run the cleanup hook if present, but we can't release the
|
||||
* portal's memory until the cleanup call.
|
||||
*/
|
||||
void
|
||||
AtAbort_Portals(void)
|
||||
@@ -760,17 +757,6 @@ AtAbort_Portals(void)
|
||||
{
|
||||
Portal portal = hentry->portal;
|
||||
|
||||
/*
|
||||
* See similar code in AtSubAbort_Portals(). This would fire if code
|
||||
* orchestrating multiple top-level transactions within a portal, such
|
||||
* as VACUUM, caught errors and continued under the same portal with a
|
||||
* fresh transaction. No part of core PostgreSQL functions that way.
|
||||
* XXX Such code would wish the portal to remain ACTIVE, as in
|
||||
* PreCommit_Portals().
|
||||
*/
|
||||
if (portal->status == PORTAL_ACTIVE)
|
||||
MarkPortalFailed(portal);
|
||||
|
||||
/*
|
||||
* Do nothing else to cursors held over from a previous transaction.
|
||||
*/
|
||||
@@ -810,9 +796,10 @@ AtAbort_Portals(void)
|
||||
* Although we can't delete the portal data structure proper, we can
|
||||
* release any memory in subsidiary contexts, such as executor state.
|
||||
* The cleanup hook was the last thing that might have needed data
|
||||
* there.
|
||||
* there. But leave active portals alone.
|
||||
*/
|
||||
MemoryContextDeleteChildren(portal->portalContext);
|
||||
if (portal->status != PORTAL_ACTIVE)
|
||||
MemoryContextDeleteChildren(portal->portalContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -832,6 +819,13 @@ AtCleanup_Portals(void)
|
||||
{
|
||||
Portal portal = hentry->portal;
|
||||
|
||||
/*
|
||||
* Do not touch active portals --- this can only happen in the case of
|
||||
* a multi-transaction command.
|
||||
*/
|
||||
if (portal->status == PORTAL_ACTIVE)
|
||||
continue;
|
||||
|
||||
/* Do nothing to cursors held over from a previous transaction */
|
||||
if (portal->createSubid == InvalidSubTransactionId)
|
||||
{
|
||||
@@ -1161,3 +1155,22 @@ ThereAreNoReadyPortals(void)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
ThereArePinnedPortals(void)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
PortalHashEnt *hentry;
|
||||
|
||||
hash_seq_init(&status, PortalHashTable);
|
||||
|
||||
while ((hentry = (PortalHashEnt *) hash_seq_search(&status)) != NULL)
|
||||
{
|
||||
Portal portal = hentry->portal;
|
||||
|
||||
if (portal->portalPinned)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user