Re: Atomicity of UPDATE, interchanging values in unique column

From: Rod Taylor <rbt(at)rbt(dot)ca>
To: daniel alvarez <d-alvarez(at)gmx(dot)de>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Atomicity of UPDATE, interchanging values in unique column
Date: 2003-03-08 21:33:22
Message-ID: 1047159202.28251.196.camel@jester
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


> UPDATE sometable SET unique_col =
> CASE WHEN unique_col = firstvalue THEN secondvalue
> ELSE firstvalue
> END
> WHERE unique_col = firstvalue
> OR unique_col = secondvalue

(See last comment)

> BEGIN;
> SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
>
> UPDATE sometable SET unique_col = firstvalue WHERE unique_col = secondvalue;
> UPDATE sometable SET unique_col = secondvalue WHERE unique_col = firstvalue;
>
> COMMIT;

This one will always fail unless you DEFER unique constraints --
something we don't support with PostgreSQL, but some others do.

> How can I interchange two values in a unique column? Am I missing something
> really
> obvious (like a swap statement)? Is there any reason besides performance for
> not
> making index accesses fully ACID-compliant? Doesn't MVCC require this
> anyway?

The first is what you want. PostgreSQL needs some work in the
evaluation of unique indexes to properly support it.

Namely, when it sees a conflict when inserting into the index, it needs
to record the fact, and revisit the conflict at the end of the command.
Lots of work...

--
Rod Taylor <rbt(at)rbt(dot)ca>

PGP Key: http://www.rbt.ca/rbtpub.asc

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message daniel alvarez 2003-03-08 21:48:46 Re: Atomicity of UPDATE, interchanging values in unique column
Previous Message daniel alvarez 2003-03-08 21:17:13 Atomicity of UPDATE, interchanging values in unique column