X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 872539DC9B8 for ; Mon, 19 Dec 2005 12:27:38 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 73916-06 for ; Mon, 19 Dec 2005 12:27:37 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 368C79DC9B6 for ; Mon, 19 Dec 2005 12:27:36 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id jBJGRUQq029899; Mon, 19 Dec 2005 11:27:30 -0500 (EST) To: Richard Huxton cc: Ken Winter , "'PostgreSQL pg-sql list'" Subject: Re: Rule causes baffling error In-reply-to: <43A6DA39.2080509@archonet.com> References: <009c01c604b3$1b007630$6603a8c0@kenxp> <43A6DA39.2080509@archonet.com> Comments: In-reply-to Richard Huxton message dated "Mon, 19 Dec 2005 16:05:13 +0000" Date: Mon, 19 Dec 2005 11:27:30 -0500 Message-ID: <29898.1135009650@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.017 required=5 tests=[AWL=0.017] X-Spam-Score: 0.017 X-Spam-Level: X-Archive-Number: 200512/166 X-Sequence-Number: 23708 Richard Huxton writes: > Ken Winter wrote: >> Can a trigger be written on a *view*? > There's nothing for them to fire against even if you could attach the > trigger. Currently we reject CREATE TRIGGER on a view, but it occurred to me the other day that that could be relaxed, at least for BEFORE triggers. The system could feed the trigger with the synthetic view row, and the trigger could update the view's underlying tables and then return NULL to suppress any actual "operation" on the view proper. To do this, instead of erroring out in the rewriter if a view has no DO INSTEAD rule, we would have to error out down in the guts of the executor if control got as far as trying to actually insert/update/delete a tuple in a view. The trickiest part of this is probably generating the "old" row for UPDATE and DELETE cases. I think you'd need to adjust the planner so that it would generate all the "old" view columns, rather than the current situation in which it generates just the "new" columns for an UPDATE, or no columns at all (only the CTID) for a DELETE. I don't see any fundamental reason why this couldn't be made to work though. Triggers would be better than rules for quite a few view-rewriting scenarios, mainly because you'd avoid all the gotchas with double evaluation and so on. So it seems like it might be worth doing. regards, tom lane