pg.ddx.io pgsql-bugs@postgresql.org mailing list archivehelp / color / mirror / Atom feed
BUG #10856: Delete trigger corrupts foreign key integrity 3+ messages / 3 participants [nested] [flat]
* BUG #10856: Delete trigger corrupts foreign key integrity @ 2014-07-04 06:55 m.fritz@wisutec.de 2014-07-05 10:36 ` Re: BUG #10856: Delete trigger corrupts foreign key integrity Greg Stark <stark@mit.edu> 0 siblings, 1 reply; 3+ messages in thread From: m.fritz@wisutec.de @ 2014-07-04 06:55 UTC (permalink / raw) To: pgsql-bugs The following bug has been logged on the website: Bug reference: 10856 Logged by: Mathias Fritz Email address: m.fritz@wisutec.de PostgreSQL version: 9.3.4 Operating system: Windows 7 x64 Description: Affected version: --------------------------------- "PostgreSQL 9.3.4, compiled by Visual C++ build 1600, 64-bit" Problem description: --------------------------------- A "before delete" trigger on a child table returning "NULL" prevents records from deletion, which violates against the FOREIGN KEY constraint. Child records are still present, master record is deleted but FOREIGN KEY is still "VALID". Although this is a logical bug of the trigger (to return NULL in delete operation), there should be never orphaned records. Also the FK validity check seems to be ignoring the orphaned child records. Steps to reproduce: --------------------------------- drop table if exists child; drop table if exists master; -- Sample master table create table master(id_m integer, name character varying(100), constraint pk_master_id_m primary key(id_m)); -- Sample child table with cascading delete FK create table child(id_c integer, id_m integer, name character varying(100), constraint pk_child_id_c primary key(id_c), constraint fk_child_id_m foreign key(id_m) references master(id_m) on delete cascade); CREATE OR REPLACE FUNCTION trgfn_child() RETURNS trigger AS $BODY$ DECLARE tmpID_C integer; tmpID_M integer; BEGIN -- For TG_OP='DELETE' NEW is null, so trigger prevents further processing of row RETURN NEW; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; CREATE TRIGGER tr_child BEFORE INSERT OR UPDATE OR DELETE ON child FOR EACH ROW EXECUTE PROCEDURE trgfn_child(); insert into master values (1, 'test 1'); insert into master values (2, 'test 2'); insert into child values (1, 1, 'child 1-1'); insert into child values (2, 1, 'child 1-2'); insert into child values (3, 2, 'child 2-1'); insert into child values (4, 2, 'child 2-2'); -- Start deleting master delete from master where id_m = 2; -- 1st bug: Cascading didn't work, there are still children, but master is deleted! select * from master where id_m = 2; /* 0 rows, good */ select * from child where id_m = 2; /* 2 rows still present !*/ -- 2nd bug: Master is gone, child present, but FK still valid alter table child validate constraint fk_child_id_m; -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #10856: Delete trigger corrupts foreign key integrity 2014-07-04 06:55 BUG #10856: Delete trigger corrupts foreign key integrity m.fritz@wisutec.de @ 2014-07-05 10:36 ` Greg Stark <stark@mit.edu> 2014-07-05 14:46 ` Re: BUG #10856: Delete trigger corrupts foreign key integrity Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 1 reply; 3+ messages in thread From: Greg Stark @ 2014-07-05 10:36 UTC (permalink / raw) To: m.fritz@wisutec.de; +Cc: pgsql-bugs On 5 Jul 2014 06:58, <m.fritz@wisutec.de> wrote: > > A "before delete" trigger on a child table returning "NULL" prevents records > from deletion, which violates against the FOREIGN KEY constraint. Child > records are still present, master record is deleted but FOREIGN KEY is still > "VALID". Yup, don't do that. I bellringer there are warnings to this effect in the documentation. PostgreSQL's referential integrity is handled by triggers so it's possible to escape their effects with other triggers. We could implement them internally without using triggers but there are other advantages to using triggers and so far the feeling is that the development effort is better spent elsewhere. ^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #10856: Delete trigger corrupts foreign key integrity 2014-07-04 06:55 BUG #10856: Delete trigger corrupts foreign key integrity m.fritz@wisutec.de 2014-07-05 10:36 ` Re: BUG #10856: Delete trigger corrupts foreign key integrity Greg Stark <stark@mit.edu> @ 2014-07-05 14:46 ` Tom Lane <tgl@sss.pgh.pa.us> 0 siblings, 0 replies; 3+ messages in thread From: Tom Lane @ 2014-07-05 14:46 UTC (permalink / raw) To: Greg Stark <stark@mit.edu>; +Cc: m.fritz@wisutec.de; pgsql-bugs Greg Stark <stark@mit.edu> writes: > On 5 Jul 2014 06:58, <m.fritz@wisutec.de> wrote: >> A "before delete" trigger on a child table returning "NULL" prevents records >> from deletion, which violates against the FOREIGN KEY constraint. > Yup, don't do that. I bellringer there are warnings to this effect in the > documentation. > We could implement them internally without using triggers but there are > other advantages to using triggers and so far the feeling is that the > development effort is better spent elsewhere. More to the point, if FK actions did not fire triggers, that would be a significant loss of functionality. There are plenty of applications wherein you'd like to have that, for example for logging purposes. So our choice is that user-written triggers are fired and it's up to you that they not break FK changes. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2014-07-05 14:46 UTC | newest] Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2014-07-04 06:55 BUG #10856: Delete trigger corrupts foreign key integrity m.fritz@wisutec.de 2014-07-05 10:36 ` Greg Stark <stark@mit.edu> 2014-07-05 14:46 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions for how to clone and mirror all data and code used for this inbox