pg_current_xact_id_if_assigned()

A function returning the current transaction ID, if assigned

pg_current_xact_id_if_assigned() is a system function returning the current transaction ID, if one is assigned.

pg_current_xact_id_if_assigned() was added in PostgreSQL 13.

Usage

pg_current_xact_id_if_assigned () → xid8

pg_current_xact_id_if_assigned() returns the current transaction ID as xid8. Note that a new transaction ID will be not be assigned until a non-read query is made in the current transaction.

pg_current_xact_id_if_assigned() replaces txid_current_if_assigned().

Change history

Examples

Basic usage example for pg_current_xact_id_if_assigned() - a transaction will not have a transaction ID until it makes a change to the database:

postgres=# BEGIN;
BEGIN

postgres=*# SELECT pg_current_xact_id_if_assigned();
 pg_current_xact_id_if_assigned 
--------------------------------
                               
(1 row)

postgres=*# SELECT 1;
 ?column? 
----------
        1
(1 row)

postgres=*# SELECT pg_current_xact_id_if_assigned();
 pg_current_xact_id_if_assigned 
--------------------------------
                               
(1 row)

postgres=*# INSERT INTO foo VALUES (1); 
INSERT 0 1

postgres=*# SELECT pg_current_xact_id_if_assigned();
 pg_current_xact_id_if_assigned 
--------------------------------
                            764
(1 row)

Categories

System function, Transactions

See also

pg_current_xact_id(), pg_xact_status()