Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 900449F9F46 for ; Wed, 13 Dec 2006 22:54:02 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 33703-03 for ; Wed, 13 Dec 2006 22:53:45 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 0CC049F9B05 for ; Wed, 13 Dec 2006 22:53:57 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from amanda.contactbda.com (ipn36372-e65122.cidr.lightship.net [216.204.66.226]) by svr4.postgresql.org (Postfix) with ESMTP id 0F12D5AF04F for ; Thu, 14 Dec 2006 02:53:52 +0000 (GMT) Received: from JIMSDELL2 (c-75-68-77-33.hsd1.nh.comcast.net [75.68.77.33]) (authenticated bits=0) by amanda.contactbda.com (8.13.8/8.13.8/Debian-2) with ESMTP id kBE2reA8011147; Wed, 13 Dec 2006 21:53:44 -0500 Message-Id: <200612140253.kBE2reA8011147@amanda.contactbda.com> From: "Jim Buttafuoco" To: "'William Scott Jordan'" , Subject: Re: Rule for multiple entries Date: Wed, 13 Dec 2006 21:53:36 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 In-Reply-To: <7.0.1.0.2.20061213180244.0844cff0@pandimensional.org> Thread-Index: AccfKCCimm2AKiPrQx+PLzIndikJDAAAsnYg X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/103 X-Sequence-Number: 27059 Use a trigger instead, the rule is only run once per insert/update/delete while the trigger is run for each row. -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] On Behalf Of William Scott Jordan Sent: Wednesday, December 13, 2006 9:05 PM To: pgsql-sql@postgresql.org Subject: [SQL] Rule for multiple entries Hi all! I have a rule in place that is supposed to adjust a value in one table based on how many rows are added or deleted to another table, but I'm not getting the results that I hoped for. If a single sql statement adds or deletes multiple entries, the rule still only adjusts the value by one, when I want it to adjust the value by the number of rows that were added or deleted. To recreate this problem, first set up the schema: CREATE TABLE counter ( counter_key int, counter_value int ) ; INSERT INTO counter (counter_key, counter_value) VALUES (1, 0) ; CREATE TABLE entries ( entry_key int ) ; CREATE RULE entries_insert AS ON INSERT TO entries DO UPDATE counter SET counter_value = counter_value + 1 WHERE counter_key = 1 ; CREATE RULE entries_delete AS ON DELETE TO entries DO UPDATE counter SET counter_value = counter_value - 1 WHERE counter_key = 1 ; Then enter some values: INSERT INTO entries (entry_key) VALUES (1) ; INSERT INTO entries (entry_key) VALUES (2) ; INSERT INTO entries (entry_key) VALUES (3) ; INSERT INTO entries (entry_key) VALUES (4) ; INSERT INTO entries (entry_key) VALUES (5) ; At this point the counter table should show a counter_value of 5, because there are 5 entries in the entries table. Now, if you delete 3 of those entries, the counter table should show a value of 2, but instead it only counts it as a single transaction. DELETE FROM entries WHERE entry_key > 2 ; Why doesn't the entries_delete rule hit for each row that's being adjusted. Or, more importantly, how can I adjust the rule so that it will count each row that's be added/deleted? Any suggestions would be appreciated. -Scott ---------------------------(end of broadcast)--------------------------- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match