Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out the numeric ID of the current Postgres Transaction?

Tags:

postgresql

On psql I can type the following:

BEGIN;

what do I type to get the current id of the newly created transaction?

I am trying to put together a demo that explains how vaccum and MVVC work in postgres. For example select xmin, xmax, * from test; shows the xmin and xmax of each row from the point of view of the current transaction.

I understand the basic theory of it but want to put together an interactive exercise such that I can have two psql consoles open and then have a set of step by step instructions that show mvcc and vaccum work.

How do I get the current postgres transaction id?

like image 819
ams Avatar asked Dec 15 '13 15:12

ams


3 Answers

Postgres has added txid_current_if_assigned() in version 10.

like image 99
dpmott Avatar answered Nov 04 '22 09:11

dpmott


"System Information Functions", "Transaction IDs and Snapshots".

like image 28
Milen A. Radev Avatar answered Nov 04 '22 08:11

Milen A. Radev


You can get the transaction id from:

txid_current()

You can additionally get the in-progress transactions in the snaption it's seeing from:

txid_snapshot_xip(txid_current_snapshot())

A few more functions are detailed in the manual:

http://www.postgresql.org/docs/current/static/functions-info.html#FUNCTIONS-TXID-SNAPSHOT

like image 32
Denis de Bernardy Avatar answered Nov 04 '22 09:11

Denis de Bernardy