Re: Getting current transaction id

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Paesold <mpaesold(at)gmx(dot)at>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Getting current transaction id
Date: 2002-09-25 17:12:03
Message-ID: 11493.1032973923@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers pgsql-sql

Michael Paesold <mpaesold(at)gmx(dot)at> writes:
> I just read it's possible to get the MVCC last version numbers. Is it also
> possible to get the current transaction id?

Well, there's the brute force way: insert a tuple in some table and look
at its xmin. Offhand I don't think we provide a SQL function to read
current transaction id, though it'd surely be a trivial addition.

> Would it be possible to check
> later if that transaction has been commited? This would be nice for a distributed
> application to enforce an "exactly once" semantics for transactions (even if
> there are network related errors while the server sends ack for commiting a
> transaction).

Again, it's not an exported operation, though you could add a SQL function
that called TransactionIdDidCommit().

> And if it's possible, how long would that information be valid, i.e. when do
> transaction id's get reused?

That would be the tricky part. The ID would be reused after 4 billion
transactions, which is long enough that you probably don't care ... but
the segment of the transaction log that has the associated commit bit
will be recycled as soon as the server has no internal use for it
anymore, which could be as early as the next database-wide VACUUM.
If you tried to call TransactionIdDidCommit() after that, you'd get the
infamous "can't open pg_clog/nnnn" error.

> If it's not working I will have to implement my own transactions table.

That's what I'd recommend. Transaction IDs are internal to the database
and are not designed for users to rely on.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2002-09-25 17:17:47 Re: Administrator issue
Previous Message Michelle Konzack 2002-09-25 16:47:26 Problem mit PostgreSQL

Browse pgsql-hackers by date

  From Date Subject
Next Message faove 2002-09-25 17:18:41 Gana con Es-Fcil!
Previous Message Neil Conway 2002-09-25 17:07:19 Re: making use of large TLB pages

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew Perrin 2002-09-25 17:44:57 SQL formatter?
Previous Message Boulgakov Andrei 2002-09-25 16:18:34 Is it possible to use lo_write in SQL?