dbTalk Databases Forums  

[BUGS] RI within PLPGSQL

mailing.database.pgsql-bugs mailing.database.pgsql-bugs


Discuss [BUGS] RI within PLPGSQL in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
cnliou
 
Posts: n/a

Default [BUGS] RI within PLPGSQL - 12-12-2003 , 04:45 AM






Hi!

It looks to me a referential integrity problem (only?)
within PLPGSQL. Plesase see the test result below.

Thank you!
CN
=======
CREATE TABLE test1(c1 INTEGER PRIMARY KEY) WITHOUT OIDS;

CREATE TABLE test2
( c1 INTEGER,
c2 INTEGER,
PRIMARY KEY (c1,c2),
CONSTRAINT ctest2 FOREIGN KEY (c1) REFERENCES test1 (c1)
ON UPDATE CASCADE ON DELETE CASCADE
) WITHOUT OIDS;

CREATE OR REPLACE FUNCTION f1(int) RETURNS VOID AS '
BEGIN
DELETE FROM test1 WHERE c1= $1;

INSERT INTO test1 VALUES($1);
INSERT INTO test2 VALUES($1,2);
RETURN;
END' LANGUAGE 'plpgsql' STABLE;

CREATE OR REPLACE FUNCTION f2(int) RETURNS VOID AS '
BEGIN
DELETE FROM test2 WHERE c1= $1;
DELETE FROM test1 WHERE c1= $1;

INSERT INTO test1 VALUES($1);
INSERT INTO test2 VALUES($1,2);
RETURN;
END' LANGUAGE 'plpgsql' STABLE;

db1=# select f1(1);
f1
----

(1 row)

db1=# select * from test1; select * from test2;
c1
----
1
(1 row)

c1 | c2
----+----
1 | 2
(1 row)

db1=# select f1(1);
ERROR: duplicate key violates unique constraint
"test2_pkey"
CONTEXT: PL/pgSQL function "f1" line 5 at SQL statement
db1=# select * from test1; select * from test2;
c1
----
1
(1 row)

c1 | c2
----+----
1 | 2
(1 row)

db1=# select f2(1);
f2
----

(1 row)

db1=# select * from test1; select * from test2;
c1
----
1
(1 row)

c1 | c2
----+----
(0 rows)

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)

Reply With Quote
  #2  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: [BUGS] RI within PLPGSQL - 12-12-2003 , 10:50 AM







On Fri, 12 Dec 2003, cnliou wrote:

Quote:
It looks to me a referential integrity problem (only?)
within PLPGSQL. Plesase see the test result below.
There have been discussions in the past about when cascade events
should occur. The code currently does what I believe was last
agreed upon, although its behavior is fairly wierd for deferred
constraints and functions. Right now the cascade happens at the
end of the full statement (in this case the call to the function)
which is why you get a key constraint error in the second call to f1
and why the later inserted row is removed in f2.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)


Reply With Quote
  #3  
Old   
cnliou
 
Posts: n/a

Default Re: [BUGS] RI within PLPGSQL - 12-12-2003 , 10:22 PM



Thank you very much for your explanation!

ˇ° Includeˇm"Stephan Szabo" <sszabo (AT) megazone (DOT) bigpanda.com>ˇn
wrote:
Quote:
There have been discussions in the past about when cascade
events
should occur. The code currently does what I believe was
last
agreed upon, although its behavior is fairly wierd for
deferred
constraints and functions. Right now the cascade happens
at the
end of the full statement (in this case the call to the
function)
which is why you get a key constraint error in the second
call to f1
and why the later inserted row is removed in f2.
It sounds to me that the only solution to my case is
executing

DELETE FROM referenced_table

and

INSERT INTO referencing_table

in seperate transactions. Please correct me if I am wrong.

I also feel it might be a good idea to include an example
like the one in my previous message in the documentation so
that this question hopefully will not be asked repeatedly.

Regards,

CN

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #4  
Old   
Stephan Szabo
 
Posts: n/a

Default Re: [BUGS] RI within PLPGSQL - 12-13-2003 , 12:09 AM



On Sat, 13 Dec 2003, cnliou wrote:

Quote:
Thank you very much for your explanation!

=A1=B0 Include=A1m"Stephan Szabo" <sszabo (AT) megazone (DOT) bigpanda.com>=A1n
wrote:
There have been discussions in the past about when cascade
events
should occur. The code currently does what I believe was
last
agreed upon, although its behavior is fairly wierd for
deferred
constraints and functions. Right now the cascade happens
at the
end of the full statement (in this case the call to the
function)
which is why you get a key constraint error in the second
call to f1
and why the later inserted row is removed in f2.

It sounds to me that the only solution to my case is
executing

DELETE FROM referenced_table

and

INSERT INTO referencing_table

in seperate transactions. Please correct me if I am wrong.
I think they only need to be in separate outer statements for non-deferred
triggers. It's just that the full set of triggered actions for the
function count as part of the one statement that calls it.

So, from psql, sending separate statements
DELETE FROM ... ;
INSERT INTO ... ;
should work, but a function body
'DELETE FROM ...;
INSERT INTO ...;'
counts as one statement and so the delete action happens after the insert.

Quote:
I also feel it might be a good idea to include an example
like the one in my previous message in the documentation so
that this question hopefully will not be asked repeatedly.
I think the behavior of this hasn't entirely solidified yet. It's still
possible that it'll change as although we came to a behavior set, if it
can be shown to break the spec's requirements, it'll be changed.

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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.