RI within PLPGSQL

From: "cnliou" <cnliou(at)so-net(dot)net(dot)tw>
To: "" <pgsql-bugs(at)postgresql(dot)org>
Subject: RI within PLPGSQL
Date: 2003-12-12 10:35:09
Message-ID: 1071225309.92707.cnliou@so-net.net.tw
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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)

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message internet email storage service 2003-12-12 10:57:56 Report
Previous Message Seum-Lim Gan 2003-12-12 07:07:01 Re: dyntest.pgc not working in 7.4 ?