[PATCH] Add pg_current_vxact_id() function to expose virtual transaction IDs

Pavlo Golub <pavlo.golub@cybertec.at>

From: "Pavlo Golub" <pavlo.golub@cybertec.at>
To: pgsql-hackers@lists.postgresql.org
Date: 2025-12-08T12:09:58Z
Lists: pgsql-hackers

Attachments

Hi hackers,

I'd like to propose a new function pg_current_vxact_id() that returns 
the
current backend's virtual transaction ID (VXID).

Virtual transaction IDs are fundamental to PostgreSQL's transaction 
tracking,
appearing in pg_locks.virtualtransaction, log output via %v placeholder, 
and
internal transaction management. However, there's currently no direct 
SQL
function to retrieve the current VXID, forcing applications to query 
pg_locks
or parse log files to obtain this information.

The patch adds pg_current_vxact_id() which returns the VXID as text in 
the
format "procNumber/lxid" (e.g., "3/42"), matching the format used 
throughout
PostgreSQL for consistency.

Use cases:
1. Application transaction tracking and correlation with logs
2. Monitoring read-only transactions (which never get regular XIDs)
3. Debugging transaction behavior without querying pg_locks
4. Building monitoring tools that need consistent transaction identity

The function follows the same pattern as pg_current_xact_id() and
pg_current_xact_id_if_assigned(), providing a clean API for a commonly
needed piece of information.

Changes:
- Added function in xid8funcs.c (alongside related transaction ID 
functions)
- OID 5101 (verified available with unused_oids script)
- Comprehensive regression tests in xid.sql
- Documentation in func-info.sgml and xact.sgml
- Format kept in sync with existing VXID representations in elog.c and 
lockfuncs.c

The v1 patch is attached. Tests pass cleanly with "meson test 
regress/regress".

Best regards,
Pavlo Golub