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

From: "Pavlo Golub" <pavlo(dot)golub(at)cybertec(dot)at>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: [PATCH] Add pg_current_vxact_id() function to expose virtual transaction IDs
Date: 2025-12-08 12:09:58
Message-ID: em5af9cba4-8cd3-4c9a-a593-c985d1c373ee@18a0083a.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v1-0001-add-pg_current_vxact_id-function.patch application/octet-stream 7.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2025-12-08 12:10:53 Re: Missing empty transaction optimization in pgoutput plugin
Previous Message shveta malik 2025-12-08 11:57:27 Re: Skipping schema changes in publication