Re: [SPAM] Current transaction ID?

Lists: pgsql-general
From: "Steve - DND" <postgres(at)digitalnothing(dot)com>
To: "postgres-general" <pgsql-general(at)postgresql(dot)org>
Subject: Current transaction ID?
Date: 2005-06-14 23:32:58
Message-ID: LDEHKBBOEMIJKHKBOFNFKEMMPBAA.postgres@digitalnothing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Simple question I'm sure, but how can I get the transaction ID that a query
is currently running in? I can find all kinds of documentation talking about
transaction ids, but nothing about how to get the current one.

Thanks,
Steve


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Steve - DND <postgres(at)digitalnothing(dot)com>
Cc: postgres-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-14 23:41:11
Message-ID: 200506142341.j5ENfBc14189@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Steve - DND wrote:
> Simple question I'm sure, but how can I get the transaction ID that a query
> is currently running in? I can find all kinds of documentation talking about
> transaction ids, but nothing about how to get the current one.

You can look at the xmin of a tuple you created or an xmax of a tuple
you deleted.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: "Steve - DND" <postgres(at)digitalnothing(dot)com>
To: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: "postgres-general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-14 23:46:29
Message-ID: LDEHKBBOEMIJKHKBOFNFCEMPPBAA.postgres@digitalnothing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> Steve - DND wrote:
> > Simple question I'm sure, but how can I get the transaction ID
> that a query
> > is currently running in? I can find all kinds of documentation
> talking about
> > transaction ids, but nothing about how to get the current one.
>
> You can look at the xmin of a tuple you created or an xmax of a tuple
> you deleted.

Let's pretend I don't know how to do that. :) Do you have a link to a page
in the manual that describes this process, or can you give me a quick
runthrough of what you're talking about?

Thanks,
Steve


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Steve - DND <postgres(at)digitalnothing(dot)com>
Cc: postgres-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-14 23:48:50
Message-ID: 200506142348.j5ENmoH19070@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Steve - DND wrote:
> > Steve - DND wrote:
> > > Simple question I'm sure, but how can I get the transaction ID
> > that a query
> > > is currently running in? I can find all kinds of documentation
> > talking about
> > > transaction ids, but nothing about how to get the current one.
> >
> > You can look at the xmin of a tuple you created or an xmax of a tuple
> > you deleted.
>
> Let's pretend I don't know how to do that. :) Do you have a link to a page
> in the manual that describes this process, or can you give me a quick
> runthrough of what you're talking about?

Every row has an invisible xmin/xmax columns that represent the xid of
the row inserted, updated, or deleted. Why do you need to know the xid
is a better question.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: "Steve - DND" <postgres(at)digitalnothing(dot)com>
To: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: "postgres-general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-14 23:58:50
Message-ID: LDEHKBBOEMIJKHKBOFNFGENAPBAA.postgres@digitalnothing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> > Let's pretend I don't know how to do that. :) Do you have a
> link to a page
> > in the manual that describes this process, or can you give me a quick
> > runthrough of what you're talking about?
>
> Every row has an invisible xmin/xmax columns that represent the xid of
> the row inserted, updated, or deleted. Why do you need to know the xid
> is a better question.

Hmm...not quite what I was hoping for. I'm looking to add the ability to my
auditing scripts to know what happened in a given transaction. Right now,
it's just table based, so while it still logs all of the changes, it does it
on a per table basis, not associating changes on two tables as being part of
the same action.

I can currently guess as to what was changed at one time by using the
current user that's recorded, and the time at which the changes occurred,
but it's not foolproof.

Steve


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Steve - DND <postgres(at)digitalnothing(dot)com>
Cc: postgres-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-15 00:03:41
Message-ID: 200506150003.j5F03fY12576@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Steve - DND wrote:
> > > Let's pretend I don't know how to do that. :) Do you have a
> > link to a page
> > > in the manual that describes this process, or can you give me a quick
> > > runthrough of what you're talking about?
> >
> > Every row has an invisible xmin/xmax columns that represent the xid of
> > the row inserted, updated, or deleted. Why do you need to know the xid
> > is a better question.
>
> Hmm...not quite what I was hoping for. I'm looking to add the ability to my
> auditing scripts to know what happened in a given transaction. Right now,
> it's just table based, so while it still logs all of the changes, it does it
> on a per table basis, not associating changes on two tables as being part of
> the same action.
>
> I can currently guess as to what was changed at one time by using the
> current user that's recorded, and the time at which the changes occurred,
> but it's not foolproof.

Well, you can take the xmin of a row and look for other rows with the
same xmin, either in the same table or in different tables.

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073


From: "Steve - DND" <postgres(at)digitalnothing(dot)com>
To: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: "postgres-general" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-15 00:10:45
Message-ID: LDEHKBBOEMIJKHKBOFNFGENBPBAA.postgres@digitalnothing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

>
> Well, you can take the xmin of a row and look for other rows with the
> same xmin, either in the same table or in different tables.

Do these columns appear inside the NEW/OLD variables for triggers, or from
some other method within a trigger?

Steve


From: Marek Lewczuk <newsy(at)lewczuk(dot)com>
To: Steve - DND <postgres(at)digitalnothing(dot)com>
Cc: postgres-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: [SPAM] Current transaction ID?
Date: 2005-06-15 05:59:49
Message-ID: 42AFC3D5.5010702@lewczuk.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Steve - DND napisał(a):
> Simple question I'm sure, but how can I get the transaction ID that a query
> is currently running in? I can find all kinds of documentation talking about
> transaction ids, but nothing about how to get the current one.
>
You can compile attached function - this will return current transaction id.

Attachment Content-Type Size
getcurrenttransactionid.c text/plain 192 bytes
getcurrenttransactionid.sql.in text/plain 170 bytes
Makefile text/plain 505 bytes
README.getcurrenttransactionid text/plain 7 bytes

From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Steve - DND <postgres(at)digitalnothing(dot)com>, postgres-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-15 13:51:00
Message-ID: 1118843460.3382.54.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Tue, 2005-06-14 at 19:03, Bruce Momjian wrote:
> Steve - DND wrote:
> > > > Let's pretend I don't know how to do that. :) Do you have a
> > > link to a page
> > > > in the manual that describes this process, or can you give me a quick
> > > > runthrough of what you're talking about?
> > >
> > > Every row has an invisible xmin/xmax columns that represent the xid of
> > > the row inserted, updated, or deleted. Why do you need to know the xid
> > > is a better question.
> >
> > Hmm...not quite what I was hoping for. I'm looking to add the ability to my
> > auditing scripts to know what happened in a given transaction. Right now,
> > it's just table based, so while it still logs all of the changes, it does it
> > on a per table basis, not associating changes on two tables as being part of
> > the same action.
> >
> > I can currently guess as to what was changed at one time by using the
> > current user that's recorded, and the time at which the changes occurred,
> > but it's not foolproof.
>
> Well, you can take the xmin of a row and look for other rows with the
> same xmin, either in the same table or in different tables.

A query to pull all the rows that match. id is the primary key.

select *,t1.xmin from test t1 join test t2 on (t1.xmin=t2.xmin and
t1.id>t2.id);


From: Alvaro Herrera <alvherre(at)surnet(dot)cl>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Steve - DND <postgres(at)digitalnothing(dot)com>, postgres-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Current transaction ID?
Date: 2005-06-15 16:58:25
Message-ID: 20050615165825.GR9860@surnet.cl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Tue, Jun 14, 2005 at 07:41:11PM -0400, Bruce Momjian wrote:
> Steve - DND wrote:
> > Simple question I'm sure, but how can I get the transaction ID that a query
> > is currently running in? I can find all kinds of documentation talking about
> > transaction ids, but nothing about how to get the current one.
>
> You can look at the xmin of a tuple you created or an xmax of a tuple
> you deleted.

Beware -- if you use savepoints, the Xid will change and you will think
it was a different transaction when in fact it was the same one.

You should also know that savepoints are used invisibly sometimes, for
example in EXCEPTION clauses in PL/pgSQL. (Though you return to the
original transaction when the EXCEPTION block finishes, I think.)

--
Alvaro Herrera (<alvherre[a]surnet.cl>)
"Los románticos son seres que mueren de deseos de vida"