Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id A5FC19FA17C for ; Tue, 26 Jun 2007 18:37:42 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 80356-04 for ; Tue, 26 Jun 2007 18:37:40 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by postgresql.org (Postfix) with ESMTP id 7029A9FB5E0 for ; Tue, 26 Jun 2007 18:37:39 -0300 (ADT) Received: from list by ciao.gmane.org with local (Exim 4.43) id 1I3Iiw-0007BN-BE for pgsql-sql@postgresql.org; Tue, 26 Jun 2007 23:37:22 +0200 Received: from cc503261-a.eelde1.dr.home.nl ([212.204.130.190]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 26 Jun 2007 23:37:22 +0200 Received: from halfgaar by cc503261-a.eelde1.dr.home.nl with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 26 Jun 2007 23:37:22 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Wiebe Cazemier Subject: Re: Delete rules and functions Date: Tue, 26 Jun 2007 23:37:11 +0200 Lines: 62 Message-ID: References: <28442.1182891012@sss.pgh.pa.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: cc503261-a.eelde1.dr.home.nl User-Agent: KNode/0.10.4 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/230 X-Sequence-Number: 28720 On Tuesday 26 June 2007 22:50, Tom Lane wrote: > Please provide an example, because the rewriter is most certainly > applied to queries from functions. > > I suspect you are actually being burnt by some other effect, like a row > disappearing from the view as soon as its underlying data is deleted. Here is an example (and it's nothing more than an example...): -------------- CREATE TABLE cars ( id SERIAL PRIMARY KEY ); -------------- CREATE TABLE car_parts ( id SERIAL PRIMARY KEY, car_id INTEGER NOT NULL REFERENCES cars ON DELETE CASCADE, steering_wheel_id INTEGER NOT NULL REFERENCES steering_wheels ); -------------- CREATE RULE AS ON DELETE TO car_parts DO ALSO ( DELETE FROM steering_wheels WHERE id = OLD.steering_wheel_id; ); -------------- CREATE VIEW cars_view AS SELECT * FROM cars; -------------- CREATE FUNCTION cars_delete(p_old) RETURNS VOID AS $$ BEGIN DELETE FROM cars where id = p_old.id; END; $$ LANGUAGE plpgsql; -------------- CREATE RULE AS ON DELETE TO cars_view DO INSTEAD ( SELECT cars_delete(); ); -------------- Now, when I delete a row from the cars_view, the underlying record from car is deleted, which cascades to car_parts. The rule intended for removing the steering wheel doesn't do anything. And now that I wrote it, I can see that it's indeed because OLD no longer exists. I knew this was the case for rules, but I overlooked it, apparently... I had already converted it to use triggers, and I'll leave it that way.