66.1. Transactions and Identifiers#
Transactions can be created explicitly usingBEGIN
orSTART TRANSACTION
and ended usingCOMMIT
orROLLBACK
. SQL statements outside of explicit transactions automatically use single-statement transactions.
Every transaction is identified by a uniqueVirtualTransactionId
(also calledvirtualXID
orvxid
), which is comprised of a backend's process number (orprocNumber
) and a sequentially-assigned number local to each backend, known aslocalXID
. For example, the virtual transaction ID4/12532
has aprocNumber
of4
and alocalXID
of12532
.
Non-virtualTransactionId
s (orxid
), e.g.,278394
, are assigned sequentially to transactions from a global counter used by all databases within thePostgreSQL cluster. This assignment happens when a transaction first writes to the database. This means lower-numbered xids started writing before higher-numbered xids. Note that the order in which transactions perform their first database write might be different from the order in which the transactions started, particularly if the transaction started with statements that only performed database reads.
The internal transaction ID typexid
is 32 bits wide andwraps around every 4 billion transactions. A 32-bit epoch is incremented during each wraparound. There is also a 64-bit typexid8
which includes this epoch and therefore does not wrap around during the life of an installation; it can be converted to xid by casting. The functions inTable 9.82 returnxid8
values. Xids are used as the basis forPostgreSQL'sMVCC concurrency mechanism and streaming replication.
When a top-level transaction with a (non-virtual) xid commits, it is marked as committed in thepg_xact
directory. Additional information is recorded in thepg_commit_ts
directory iftrack_commit_timestamp is enabled.
In addition tovxid
andxid
, prepared transactions are also assigned Global Transaction Identifiers (GID). GIDs are string literals up to 200 bytes long, which must be unique amongst other currently prepared transactions. The mapping of GID to xid is shown inpg_prepared_xacts
.