X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 346723A4081 for ; Fri, 26 Nov 2004 23:55:52 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 84472-09 for ; Fri, 26 Nov 2004 23:55:46 +0000 (GMT) Received: from mail.delegated.net (nitro.ezprovider.net [66.199.148.100]) by svr1.postgresql.org (Postfix) with SMTP id 1D5713A3BA2 for ; Fri, 26 Nov 2004 23:55:47 +0000 (GMT) Received: (qmail 21723 invoked from network); 26 Nov 2004 23:55:47 -0000 Received: from h103.61.181.208.cablerocket.net (HELO ?192.168.0.181?) (208.181.61.103) by nitro.ezprovider.net with SMTP; 26 Nov 2004 23:55:47 -0000 Message-ID: <41A7C2C4.7010305@delegated.net> Date: Fri, 26 Nov 2004 15:56:52 -0800 From: Jonathan Knopp User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.3) Gecko/20040910 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Way to stop recursion? References: <41A79A2A.6070606@delegated.net> <20041126213448.GB2312@phlogiston.dyndns.org> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/273 X-Sequence-Number: 19591 Pierre-Frédéric Caillaud wrote: > - Change the name of your table to "hidden_table" > > - Create a view which is a duplicate of your table : > CREATE VIEW visible_table AS SELECT * FROM hidden_table; > > -> Your application now accesses its data without realizing it goes > through a view. > > Now create a rule on this view, to make it update the real > hidden_table. As the rule does not apply to hidden_table, it won't > recurse. > > Other solution (this similar to what Tom Lane proposed I think) : > > Create a field common_id in your table, with > - an insert trigger which puts a SERIAL default value if there is no > parent, or copies the parent's value if there is one > - an update trigger to copy the new parent's common_id whenever a child > changes parent (if this ever occurs in your design) > > Now create another table linking common_id to the 'common' value. > > Create a view which joins the two, which emulates your current behaviour. > Create an ON UPDATE rule to the view which just changes one row in the > link table. > > If you do a lot of selects, solution #1 will be faster, if you do a lot > of updates, #2 will win... The "hidden table" method should work just fine. Ingenius idea, thank you! > Just out of curiosity, what is this for ? The actual application has companies instead of parents, employees instead of children, then emails as children of employees and/or companies, and folders as parents of companies and employees. The "common" field (in all 4 layers) are a pair of permissions flags. May I humbly suggest two possible todo's for postgreSQL: a simple flag to suppress recursion (easier/more powerful way of doing the above), and/or more direct access to query rewriting. Seems right now rules require you to rewrite queries while partially blind to them. Being able to rewrite queries in statement triggers similar to what can be done with row triggers would be very nice too.