F.13. csn — Commit Sequence Number Extension#

F.13. csn — Commit Sequence Number Extension

F.13. csn — Commit Sequence Number Extension #

Version: 1.0

F.13.1. Overview #

The csn extension provides functions for retrieving the Commit Sequence Number (CSN) associated with transactions and snapshots. A CSN is a monotonically increasing identifier assigned at transaction commit time, enabling a total ordering of committed transactions.

This extension is intended for distributions that implement CSN-based MVCC. It is primarily useful for diagnostics, debugging, and visibility analysis.

F.13.2. Rationale #

Traditional XID based MVCC relies on transaction IDs (XIDs) and snapshots to determine visibility. CSN-based MVCC enhances this model by assigning a global commit order, simplifying visibility checks and reducing contention in highly concurrent environments.

The csn extension exposes this functionality to SQL, allowing users to:

  • Inspect commit ordering of transactions.

  • Analyze snapshot visibility boundaries.

  • Debug concurrency behavior.

  • Perform research on CSN-based MVCC systems.

F.13.3. Installation #

To install the extension, ensure that it has been compiled and installed into extension directory. Then enable it in the desired database:

CREATE EXTENSION csn;

To remove the extension:

DROP EXTENSION csn;

F.13.4. Quickstart #

Retrieve the CSN of the current transaction:

SELECT pg_csn(pg_current_xact_id());

Retrieve the CSN associated with the current snapshot:

SELECT pg_snapshot_csn(pg_current_snapshot());

Legacy-compatible interfaces:

SELECT txid_csn(txid_current());
SELECT txid_snapshot_csn(txid_current_snapshot());

F.13.5. Functions #

FunctionReturn TypeDescription
pg_csn(xid xid8)bigintReturns the CSN of a transaction.
pg_snapshot_csn(snapshot pg_snapshot)bigintReturns the CSN corresponding to a snapshot.
txid_csn(xid bigint)bigintCompatibility wrapper for pg_csn.
txid_snapshot_csn(snapshot txid_snapshot)bigintCompatibility wrapper for pg_snapshot_csn.
csnlog_upperbound_stats()recordReturns upperbound cache hit counters.

Modern applications should prefer the pg_* functions.

F.13.6. Usage #

F.13.6.1. Example: Retrieve CSN of the Current Transaction #

SELECT pg_csn(pg_current_xact_id());

F.13.6.2. Example: Retrieve CSN of a Snapshot #

SELECT pg_snapshot_csn(pg_current_snapshot());

F.13.6.3. Example: Determine Commit Order #

BEGIN;
INSERT INTO test VALUES (1);
COMMIT;

SELECT pg_csn(pg_current_xact_id());

F.13.6.4. Legacy Compatibility #

SELECT txid_csn(txid_current());
SELECT txid_csn(txid_current() - 4);
SELECT txid_snapshot_csn(txid_current_snapshot());

F.13.7. Upperbound Cache #

When a transaction's visibility is checked, the CSN of the committing transaction is normally read from the pg_csnlog SLRU. To avoid that lookup on the hot path, CSN-based MVCC keeps an in-memory upperbound cache: a fixed array of slots, one per 1024-XID CSNLOG page, each holding the largest CSN committed on that page. If the slot value is already below the reader's snapshot CSN, the transaction is known to be visible and the SLRU read is skipped.

The cache is controlled by the csnlog_upperbound_enable GUC and only has effect when csn_enable is on as well.

F.13.7.1. csnlog_upperbound_stats() #

Returns a single row with two counters describing cache activity:

ColumnTypeDescription
ub_fetchesbigintChecks that tried the upperbound cache.
ub_hitsbigintChecks that hit the upperbound cache (avoided SLRU lookup)

By construction ub_hits <= ub_fetches. Both columns are NULL when the cache is disabled (csn_enable or csnlog_upperbound_enable is off).

SELECT * FROM csnlog_upperbound_stats();
 ub_fetches | ub_hits
------------+---------
        512 |     480

F.13.8. Sample Output #

 pg_csn
--------
      42

Snapshot example:

 pg_snapshot_csn
-----------------
              42

Special values:

 pg_csn
--------
       3

This corresponds to CSN_FROZEN.

 pg_csn
--------
       0

This corresponds to CSN_INPROGRESS.

ERROR:  could not access status of transaction 131938
DETAIL:  Could not open file "pg_csnlog/000000000000004": No such file or directory.

The transaction's number is too far ahead, an error.

F.13.9. Returned Values #

All functions return a Commit Sequence Number (CSN) of type bigint. The result depends on the transaction's state and age.

Returned ValueDescription
Normal CSN (>= 4)Indicates a successfully committed transaction.
CSN_FROZENReturned for bootstrap, frozen, or very old committed transactions.
CSN_ABORTEDIndicates that the transaction was aborted.
CSN_INPROGRESSIndicates that no such transaction exists or it is currently in progress.
CSN_COMMITTINGIndicates that transaction is commiting now.

For transactions older than TransactionXmin, the CSN may no longer be stored in pg_csnlog. In such cases, the status is derived from pg_xact, and committed transactions return CSN_FROZEN.

TransactionXmin is the oldest xmin among all active backends, it is the system-wide MVCC horizon ensuring safe truncation of global metadata such as pg_subtrans and pg_csnlog.

If the transaction's number is too far ahead, an error may occur. No such transaction exists, and no CSNLOG pages have been reserved for this number.

ERROR:  could not access status of transaction 131938
DETAIL:  Could not open file "pg_csnlog/000000000000004": No such file or directory.

F.13.10. Numeric Representation of CSN Values #

CSNs are represented as unsigned 64-bit integers.

ConstantNumeric ValueDescription
CSN_INPROGRESS0No such transaction exists or is currently in progress.
CSN_ABORTED1Transaction was aborted.
CSN_COMMITTING2Transaction is committing (internal use).
CSN_FROZEN3Frozen or very old committed transaction.
CSN_FIRST_NORMAL4First valid CSN assigned to a committed transaction.
CSN_MAX_NORMAL2^63 − 1Maximum valid CSN in normal operation.

F.13.11. Limitations #

  • The extension requires a build that supports CSN-based MVCC.

  • It is not supported without CSN infrastructure.

  • Arithmetic operations on xid8 are not supported in SQL.

  • Internal CSN states such as CSN_INPROGRESS are not exposed to users.

  • Results depend on the availability of CSN logs (pg_csnlog).

F.13.12. Compatibility #

Modern interfaces:

  • pg_csn(xid8)

  • pg_snapshot_csn(pg_snapshot)

Legacy compatibility interfaces:

  • txid_csn(bigint)

  • txid_snapshot_csn(txid_snapshot)