473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Updates not atomic with respect to indexes

Hi there, I am having a problem with the following code:

CREATE TABLE temp ( num integer UNIQUE );

INSERT INTO temp (num) VALUES (1);
INSERT INTO temp (num) VALUES (2);
INSERT INTO temp (num) VALUES (3);

UPDATE temp SET num = num+1;

If the update is really meant to be atomic then this should work, as the
column is still unique afterwards. However, I get a
ERROR: duplicate key violates unique constraint "temp_num_key"

Is this something I misunderstand about SQL & ACID in general, a known problem
in Posgresql, or bug which will be fixed sometime. Should I report this as a
bug?

Thanks for your help in advance!

Ben Young
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
8 1838
On Wed, 28 Apr 2004 09:33:34 +0100, Ben Young <be*@transversal.com>
wrote:
UPDATE temp SET num = num+1;
ERROR: duplicate key violates unique constraint "temp_num_key" Is this [...] a known problem in Posgresql,
Yes.
or bug which will be fixed sometime.


Don't know.

If you need a workaround for now, do it in two steps:

UPDATE temp SET num = -num;
UPDATE temp SET num = -num+1;

This assumes you have a range of numbers that is normally not used.

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #2
On Wed, 28 Apr 2004 09:33:34 +0100, Ben Young <be*@transversal.com>
wrote:
UPDATE temp SET num = num+1;
ERROR: duplicate key violates unique constraint "temp_num_key" Is this [...] a known problem in Posgresql,
Yes.
or bug which will be fixed sometime.


Don't know.

If you need a workaround for now, do it in two steps:

UPDATE temp SET num = -num;
UPDATE temp SET num = -num+1;

This assumes you have a range of numbers that is normally not used.

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #3
Ben Young said:
Hi there, I am having a problem with the following code:

CREATE TABLE temp ( num integer UNIQUE );

INSERT INTO temp (num) VALUES (1);
INSERT INTO temp (num) VALUES (2);
INSERT INTO temp (num) VALUES (3);

UPDATE temp SET num = num+1;

If the update is really meant to be atomic then this should work, as the
column is still unique afterwards. However, I get a
ERROR: duplicate key violates unique constraint "temp_num_key"

Is this something I misunderstand about SQL & ACID in general, a known
problem
in Posgresql, or bug which will be fixed sometime. Should I report this as
a
bug?


Although the command is atomic, the processing behind the scenes is not.
The problem occurs because record one's PK is updated to 2, but record 2
already has the PK value 2, so you get the PK unique constraint error
message.

One way round this is to create a procedure to select the records in
reverse order (select * from temp order by num DESC), and increment the PK
value in a loop.

Or update the PK values in two stages adding and then subtracting some
number > highest current PK value (inefficient/wasteful):

UPDATE temp SET num = num+10000000+1;
UPDATE temp SET num = num-10000000;

Hope that helps.

John Sidney-Woollett

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
Ben Young said:
Hi there, I am having a problem with the following code:

CREATE TABLE temp ( num integer UNIQUE );

INSERT INTO temp (num) VALUES (1);
INSERT INTO temp (num) VALUES (2);
INSERT INTO temp (num) VALUES (3);

UPDATE temp SET num = num+1;

If the update is really meant to be atomic then this should work, as the
column is still unique afterwards. However, I get a
ERROR: duplicate key violates unique constraint "temp_num_key"

Is this something I misunderstand about SQL & ACID in general, a known
problem
in Posgresql, or bug which will be fixed sometime. Should I report this as
a
bug?


Although the command is atomic, the processing behind the scenes is not.
The problem occurs because record one's PK is updated to 2, but record 2
already has the PK value 2, so you get the PK unique constraint error
message.

One way round this is to create a procedure to select the records in
reverse order (select * from temp order by num DESC), and increment the PK
value in a loop.

Or update the PK values in two stages adding and then subtracting some
number > highest current PK value (inefficient/wasteful):

UPDATE temp SET num = num+10000000+1;
UPDATE temp SET num = num-10000000;

Hope that helps.

John Sidney-Woollett

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5
Manfred Koizar <mk*****@aon.at> writes:
On Wed, 28 Apr 2004 09:33:34 +0100, Ben Young <be*@transversal.com>
wrote:
Is this [...] a known problem in Posgresql,

Yes.
or bug which will be fixed sometime.

Don't know.


I believe this is meant to be covered by this TODO item:

* Allow DEFERRABLE UNIQUE constraints

because the real issue is that the uniqueness check occurs immediately
rather than being deferred till end of statement or transaction.

Dunno when it will rise to the top of anyone's priority list...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #6
Manfred Koizar <mk*****@aon.at> writes:
On Wed, 28 Apr 2004 09:33:34 +0100, Ben Young <be*@transversal.com>
wrote:
Is this [...] a known problem in Posgresql,

Yes.
or bug which will be fixed sometime.

Don't know.


I believe this is meant to be covered by this TODO item:

* Allow DEFERRABLE UNIQUE constraints

because the real issue is that the uniqueness check occurs immediately
rather than being deferred till end of statement or transaction.

Dunno when it will rise to the top of anyone's priority list...

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #7
On Wednesday 28 April 2004 13:37, Tom Lane wrote:

I believe this is meant to be covered by this TODO item:

* Allow DEFERRABLE UNIQUE constraints

because the real issue is that the uniqueness check occurs immediately
rather than being deferred till end of statement or transaction.

Dunno when it will rise to the top of anyone's priority list...

regards, tom lane

Thanks for everyones responses. I guess for now I will just need to find a way
around it. Looking forward to deferrable unique contraints though!

Ben
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #8
On Wednesday 28 April 2004 13:37, Tom Lane wrote:

I believe this is meant to be covered by this TODO item:

* Allow DEFERRABLE UNIQUE constraints

because the real issue is that the uniqueness check occurs immediately
rather than being deferred till end of statement or transaction.

Dunno when it will rise to the top of anyone's priority list...

regards, tom lane

Thanks for everyones responses. I guess for now I will just need to find a way
around it. Looking forward to deferrable unique contraints though!

Ben
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
11
by: AJ | last post by:
here's the scenario.. I am running a DTS to collect the summarized info from Oracle database into SQL server. I then have a update job which updates my transactional table from the summarized...
42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
1
by: Duncan | last post by:
I'm looking for ideas on how to write SQL scripts for updates that are pushed out to clients for product updates. Obviously, We could just keep track of the changes on a pad or write a database...
0
by: Ben Young | last post by:
Hi there, I am having a problem with the following code: CREATE TABLE temp ( num integer UNIQUE ); INSERT INTO temp (num) VALUES (1); INSERT INTO temp (num) VALUES (2); INSERT INTO temp (num)...
28
by: robert | last post by:
In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict's etc. in it. Items are added, deleted, iteration over .keys() ... ). The threads are...
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
2
by: joe.kimbler | last post by:
What is the best way to handle updates in databases with each release of a software package? I used to work on an accounting package in FoxPro that had a "Data Dictionary" and as your code...
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.