473,490 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1843
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
9956
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
1535
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
3299
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
1219
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
336
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
7330
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
6228
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
2
2099
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
2920
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
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
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,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.