pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Gerald Gutierrez <pozix@home.com>
To: pgsql-sql@postgresql.org
Subject: Are SQL commands "atomic" ?
Date: Wed, 06 Jun 2001 23:10:35 -0700
Message-ID: <5.1.0.14.0.20010606231028.02b925d0@mail.rchmd1.bc.wave.home.com> (raw)


I'm using 7.1.1 right now, and have the following table:

  id |   s
----+-------
   1 | alpha
   2 | beta
   3 | gamma
   4 | delta
(4 rows)

I'd like to switch the id of "beta" to 3 and the id of "gamma" to 2 ("flip" 
them). Since id is the PK, it must remain unique and so I can't just set 
the two lines using two UPDATEs.

My solution is:

UPDATE t1 SET id=id#1 WHERE id=2 OR id=3; -- # is the XOR operator

where 2#1=3 and 3#1=2. One statement will change both values as I want. But 
when I run the statement, the server replies with:

ERROR:  Cannot insert a duplicate key into unique index t1_pkey

If the statement is "atomic", then if the statement succeeds, the IDs will 
be unique and the error is incorrect. Does this imply that SQL statements 
are not actually atomic?




view thread (4+ messages)  latest in thread

Message-ID: <5.1.0.14.0.20010606231028.02b925d0@mail.rchmd1.bc.wave.home.com>
Permalink:  ../5.1.0.14.0.20010606231028.02b925d0@mail.rchmd1.bc.wave.home.com/
Also on:    postgresql.org/message-id/5.1.0.14.0.20010606231028.02b925d0@mail.rchmd1.bc.wave.home.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: pozix@home.com
  Subject: Re: Are SQL commands "atomic" ?
  In-Reply-To: <5.1.0.14.0.20010606231028.02b925d0@mail.rchmd1.bc.wave.home.com>
  References: <5.1.0.14.0.20010606231028.02b925d0@mail.rchmd1.bc.wave.home.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox