Re: How to check whether the row was modified by this transaction before?

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Vlad Arkhipov <arhipov(at)dc(dot)baikal(dot)ru>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: How to check whether the row was modified by this transaction before?
Date: 2012-12-06 17:53:33
Message-ID: CA+Tgmob3KLxzksRjWFOvWVQP1L1yX5xqHUj8N+SBfzZTW60VHw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Dec 6, 2012 at 3:58 AM, Vlad Arkhipov <arhipov(at)dc(dot)baikal(dot)ru> wrote:
> In a BEFORE UPDATE trigger I need to know whether the row was previously
> modified by this transaction. Is it safe to use xmin and txid_current() for
> this purpose (xmin is 32-bit txid type but txid_current() returns 64-bit
> bigint).
>
> CREATE FUNCTION test_trigger()
> RETURNS TRIGGER AS $$
> BEGIN
> IF OLD.xmin = txid_current() THEN
> -- Do something.
> ELSE
> -- Do something else.
> END IF;
> END;
> $$ LANGUAGE plpgsql;

txid_current() will return a different value from xmin after the XID
space has wrapped around at least once; also, you might need to
consider subtransactions.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2012-12-06 17:53:44 Re: Fix for pg_upgrade status display
Previous Message Robert Haas 2012-12-06 17:52:41 Re: why can't plpgsql return a row-expression?