16.1. Overview#

16.1. Overview

16.1. Overview

Autonomous transactions in PL/pgSQL enable the execution of SQL statements within a function to operate within a separate transaction controlled by an autonomous session. This can be beneficial for implementing complex business logic and robust error handling.

The autonomous session operates independently and runs in a separate process. It communicates with the main session through a shared memory queue using the standard frontend/backend protocol. This architecture provides transaction isolation from the main session, ensuring that the actions within an autonomous transaction do not interfere with the main transaction.

Autonomous transactions offer the flexibility to implement intricate logic and error handling scenarios that may not be achievable in a regular PL/pgSQL function. However, it is crucial to exercise caution when using autonomous transactions to avoid potential overhead and concurrency issues.

An autonomous transaction operates independently and is initiated by a calling program. Any commit or rollback of SQL commands within the autonomous transaction has no impact on the commit or rollback state of any transaction in the calling program. Similarly, any commit or rollback action in the calling program does not affect the commit or rollback status of SQL commands within the autonomous transaction.

The following kinds of PL/pgSQL routines can incorporate autonomous transactions:

  • Independent procedures and functions

  • Anonymous blocks

  • Trigger functions

Key aspects and limitations associated with autonomous transactions include:

  • Each autonomous transaction occupies a connection slot throughout its run. In specific scenarios, this might require adjusting the max_connections setting in the postgresql.conf configuration file.

  • In most aspects, an autonomous transaction acts as though it's a completely distinct session. However, it's vital to recognize that configuration parameters (set using the SET command) are an exception to this rule. Autonomous transactions adopt the existing parameter settings and can pass on settings they commit to the encompassing transaction.

  • Autonomous transactions can be nested.

  • Executing queries in parallel is not permissible inside autonomous transactions.