dbTalk Databases Forums  

[BUGS] BUG #6632: "before delete" triggers that delete rows from the sametable can invalidate constraints

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


Discuss [BUGS] BUG #6632: "before delete" triggers that delete rows from the sametable can invalidate constraints in the mailing.database.pgsql-bugs forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
ignas@pow.lt
 
Posts: n/a

Default [BUGS] BUG #6632: "before delete" triggers that delete rows from the sametable can invalidate constraints - 05-07-2012 , 08:49 AM






The following bug has been logged on the website:

Bug reference: 6632
Logged by: Ignas Mikalajunas
Email address: ignas (AT) pow (DOT) lt
PostgreSQL version: 9.1.3
Operating system: Ubuntu 11.11
Description:

The snippet that reproduces the bug:
drop schema public cascade;
create schema public;

-- Setup
BEGIN;
CREATE TABLE apps (
id bigserial NOT NULL,
"type" varchar(32),
primary key (id)
);

CREATE TABLE content_items (
id bigserial NOT NULL,
"type" varchar(32),
app_id integer,

wall_post_id integer,

foreign key (app_id) references apps on delete cascade,
foreign key (wall_post_id) references content_items on delete set
null,
primary key (id)
);

CREATE OR REPLACE FUNCTION content_item_deleted_trigger() RETURNS trigger
AS $$
BEGIN
DELETE FROM
content_items
WHERE
content_items.id = OLD.wall_post_id;
RETURN OLD;
END
$$ LANGUAGE plpgsql;

-- the problem is in this trigger, if I make it an "AFTER" it works as it
should
CREATE TRIGGER content_item_deleted_trigger BEFORE DELETE ON
content_items
FOR EACH ROW
WHEN (OLD.wall_post_id is not null)
EXECUTE PROCEDURE content_item_deleted_trigger();

COMMIT;

-- End of schema setup

BEGIN;
INSERT INTO apps (type) VALUES ('basecamp');
INSERT INTO content_items (type, app_id, wall_post_id)
VALUES ('wall_post', NULL, NULL);
INSERT INTO content_items (type, app_id, wall_post_id)
VALUES ('basecamp_post', 1, 1);
COMMIT;

-- End of setup

BEGIN;
DELETE FROM apps WHERE apps.id = 1;
COMMIT;

-- This select still sees 1 item that refers to an app that does not exist
anymore

BEGIN;
SELECT count(*) from content_items;
SELECT count(*) from apps;
ROLLBACK;

I think being able to generate rows that do not pass constraints on a table
is a bug.


--
Sent via pgsql-bugs mailing list (pgsql-bugs (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

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 - 2013, Jelsoft Enterprises Ltd.