You can inspect the system column xmin
to identify newly inserted row versions. That includes new row versions written by UPDATE
. Deleted rows that were deleted in the same transaction are always invisible either way, so not included in the result.
Postgres 13 or newer
The function pg_current_xact_id
returns the current transaction ID as xid8
, a dedicated 8-byte data type representing unique transaction IDs.
For storage and historical reasons, xmin
in the tuple header only stores xid
, a 4-byte data type sliced down from the full transaction ID that wraps around every ~ 4 billion transactions. (There is a whole machinery in place to avoid actual wraparounds in the DB.) The manual:
Unlike xid
values, xid8
values increase strictly monotonically and cannot be reused in the lifetime of a database cluster.
There is also pg_current_xact_id_if_assigned()
, which does not burn a transaction ID if non has been assigned, yet (if the transaction has been read-only so far). That's better for the purpose, as there cannot be any rows from a read-only transaction anyway.
Either way, to make it work, we have to add an explicit cast:
SELECT * FROM foo_table
WHERE xmin = pg_current_xact_id_if_assigned()::xid;
Here is the thread discussing the patch to add xid8
:
Older versions
There is the similar function txid_current()
, which returns bigint
. It is deprecated now, but still supported as of pg 16. It builds on xid
, but quoting the manual for pg 12:
... extended with an "epoch" counter so it will not wrap around during the life of an installation.
There is a thread on pgsql-hackers with Tom Lane discussing it:
I derived the safe WHERE
condition from it:
SELECT * FROM foo_table
WHERE xmin = (txid_current() % (2^32)::bigint)::text::xid;
Disclaimer
However, this does not work with subtransactions started with SAVEPOINT
(or some other way, plpython can use subtransactions, too). Those spawn separate xids and there is currently (as of pg 12) no way to get the full list of xids belonging to the top transaction returned by txid_current()
. In my search for a solution I found this closely related thread on pgsql-hackers:
I don't currently see a reliable solution working around that limitation.
Related: