Thread: Transaction ID in Triggers
Hello All,
Is there a way to find out the current transaction id from within a Trigger? I'm interested in implementing a better audit trail mechanism and this would help me greatly.
Thanks in advance!
David Saracini <dsaracini@yahoo.com> writes: > Is there a way to find out the current transaction id from within a Trigger? I'm interested in implementing a better audittrail mechanism and this would help me greatly. If you have your hands on a row that was inserted or updated by the current transaction, you could look at its xmin. Another possibility (in recent releases) is to take the low-order 32 bits of txid_current(). regards, tom lane
On Fri, Aug 14, 2009 at 9:08 PM, Tom Lane<tgl@sss.pgh.pa.us> wrote: > David Saracini <dsaracini@yahoo.com> writes: >> Is there a way to find out the current transaction id from within a Trigger? I'm interested in implementing a betteraudit trail mechanism and this would help me greatly. > > If you have your hands on a row that was inserted or updated by the > current transaction, you could look at its xmin. Another possibility > (in recent releases) is to take the low-order 32 bits of txid_current(). For an auditing mechanism you probably just want to use the whole of txid_current() anyways. -- greg http://mit.edu/~gsstark/resume.pdf
Hi Tom,
Thank you for the quick reply. Unfortunately, I have a couple of more questions. :)
So, if I understand it... then, txid_current returns the xmax in the high-order and the xmin in the low-order. Correct? and the xmax is the transaction that deleted the tuple and the xmin stores the last transaction that (inserted or modified). Correct?
Thanks - and Regards,
David
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Saracini <dsaracini@yahoo.com>
Cc: pgsql novice forum <pgsql-novice@postgresql.org>
Sent: Friday, August 14, 2009 1:08:16 PM
Subject: Re: [NOVICE] Transaction ID in Triggers
David Saracini <dsaracini@yahoo.com> writes:
> Is there a way to find out the current transaction id from within a Trigger? I'm interested in implementing a better audit trail mechanism and this would help me greatly.
If you have your hands on a row that was inserted or updated by the
current transaction, you could look at its xmin. Another possibility
(in recent releases) is to take the low-order 32 bits of txid_current().
regards, tom lane
--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
David Saracini <dsaracini@yahoo.com> writes: > So, if I understand it... then, txid_current returns the xmax in the > high-order and the xmin in the low-order. Correct? No. txid_current returns a monotonically increasing version of the transaction counter --- its high order bits are an "epoch" counter that increases whenever the 32-bit xid wraps around. regards, tom lane
Boy... I had the screwed up! :) Thanks for explaining.
Regards,
David
From: Tom Lane <tgl@sss.pgh.pa.us>
To: David Saracini <dsaracini@yahoo.com>
Cc: pgsql novice forum <pgsql-novice@postgresql.org>
Sent: Friday, August 14, 2009 1:38:20 PM
Subject: Re: [NOVICE] Transaction ID in Triggers
David Saracini <dsaracini@yahoo.com> writes:
> So, if I understand it... then, txid_current returns the xmax in the
> high-order and the xmin in the low-order. Correct?
No. txid_current returns a monotonically increasing version of the
transaction counter --- its high order bits are an "epoch" counter
that increases whenever the 32-bit xid wraps around.
regards, tom lane