dbTalk Databases Forums  

How can I defer cascading deletes?

comp.databases.postgresql comp.databases.postgresql


Discuss How can I defer cascading deletes? in the comp.databases.postgresql forum.



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

Default How can I defer cascading deletes? - 03-03-2010 , 01:23 PM






I'm trying to combind a cascading delete and a constraint deferal.
The hope is to have a cascading delete to clean up data, but only
after a commit. I can't get it to work. For example in the following
code:

create temp table primarytest (col2 integer primary key);
create temp table defertest( col2 integer references primarytest on
update cascade on delete cascade deferrable initially deferred);
insert into primarytest values(5);
insert into defertest values (5);
select * from defertest;
begin;
set constraints all deferred;
delete from primarytest;
insert into primarytest values(5);
commit;
select * from defertest;

The row in defertest is deleted even though the value (5) is in
primarytest at the end of the session. I was hoping all explicit
session transactions would be exceuted prior to nay constraints, but
apparently that's not the case.

Is there any way to do what I want without changing to an update?

Thanks
Paul

Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: How can I defer cascading deletes? - 03-04-2010 , 03:21 AM






Paul wrote:
Quote:
I'm trying to combind a cascading delete and a constraint deferal.
The hope is to have a cascading delete to clean up data, but only
after a commit. I can't get it to work. For example in the following
code:

create temp table primarytest (col2 integer primary key);
create temp table defertest( col2 integer references primarytest on
update cascade on delete cascade deferrable initially deferred);
insert into primarytest values(5);
insert into defertest values (5);
select * from defertest;
begin;
set constraints all deferred;
delete from primarytest;
insert into primarytest values(5);
commit;
select * from defertest;

The row in defertest is deleted even though the value (5) is in
primarytest at the end of the session. I was hoping all explicit
session transactions would be exceuted prior to nay constraints, but
apparently that's not the case.

Is there any way to do what I want without changing to an update?
As you have found out, ON DELETE CASCADE will delete the dependent
rows as soon as the DELETE statement is executed. There is no way
to change that.

If you need to avoid an UPDATE (your example screams for it, but
I guess that you have your reasons), you could use the (default) clause
ON DELETE NO ACTION. This will *not* delete the dependent rows,
but the check can be deferred to commit time.

If you want both ("deferred delete of dependent rows"), I guess you'd
have to write it yourself, by using ON DELETE NO ACTION and explicitly
deleting the offending dependent rows before COMMIT.

Yours,
Laurenz Albe

Reply With Quote
  #3  
Old   
Mladen Gogala
 
Posts: n/a

Default Re: How can I defer cascading deletes? - 03-04-2010 , 07:27 AM



On Thu, 04 Mar 2010 10:21:47 +0100, Laurenz Albe wrote:


Quote:
If you want both ("deferred delete of dependent rows"), I guess you'd
have to write it yourself, by using ON DELETE NO ACTION and explicitly
deleting the offending dependent rows before COMMIT.
Postgres has foreign keys implemented using triggers, which means that
this is really not much more expensive than the normal "ON DELETE
CASCADE" thing, unless the trigger is messed up and programmed badly.



--
http://mgogala.freehostia.com

Reply With Quote
  #4  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: How can I defer cascading deletes? - 03-04-2010 , 09:37 AM



Mladen Gogala wrote:
Quote:
If you want both ("deferred delete of dependent rows"), I guess you'd
have to write it yourself, by using ON DELETE NO ACTION and explicitly
deleting the offending dependent rows before COMMIT.

Postgres has foreign keys implemented using triggers, which means that
this is really not much more expensive than the normal "ON DELETE
CASCADE" thing, unless the trigger is messed up and programmed badly.
That won't help, because (as you have found out) trigger execution
can also not be deferred.

Yours,
Laurenz Albe

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.