X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 3EB68D1B500 for ; Wed, 17 Sep 2003 11:21:25 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 88406-06 for ; Wed, 17 Sep 2003 08:20:57 -0300 (ADT) Received: from anchor-post-33.mail.demon.net (anchor-post-33.mail.demon.net [194.217.242.91]) by svr1.postgresql.org (Postfix) with ESMTP id 0F7E5D1B4EF for ; Wed, 17 Sep 2003 08:20:54 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-33.mail.demon.net with esmtp (Exim 3.35 #1) id 19zaMi-000G4m-0X for pgsql-sql@postgresql.org; Wed, 17 Sep 2003 12:20:56 +0100 Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 3F69017919 for ; Wed, 17 Sep 2003 12:20:55 +0100 (BST) Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 9AF451649D for ; Wed, 17 Sep 2003 12:20:54 +0100 (BST) From: Richard Huxton To: pgsql-sql@postgresql.org Subject: Trigger order problems Date: Wed, 17 Sep 2003 12:20:54 +0100 User-Agent: KMail/1.5 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200309171220.54020.dev@archonet.com> X-Virus-Scanned: by AMaViS snapshot-20020531 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/153 X-Sequence-Number: 15131 TIA people Three related tables: trans_core - transaction details (trans_id, trans_owner, trans_date, trans_amount) trans_src - funding source (core_id, src_id, src_amount) src_summary - summary of funding-source totals (summary_date, summary_src, summary_amount) The total trans_amount must always equal the sum of the linked "src_amount"s and these funding-sources are totalled to the summary table. The problem: I want to update the summary table whenever trans_src is modified, but to do so I need to get information from trans_core (trans_owner, trans_date). Now that's not a problem with UPDATE or INSERT since the corresponding trans_core must exist. The problem is with DELETE. If I have the following sequence: 1. DELETE row from trans_core 2. Cascades to several DELETEs on trans_src 3. BEFORE DELETE trigger is called for each of these 4. Summary table is decremented using details from trans_src and trans_core Of course, the problem is that by step 4 there isn't a row in trans_core to refer to... Options I have considered: 1. Duplicate required fields from trans_core in trans_src (yuck - in the real tables there are several fields I'd need) 2. Wipe and recalculate all relevant summary info every time trans_core is modified and make sure that we touch trans_core every time trans_src gets updated. 3. Replace src_summary with a view. Can't do this, since some of the information is historical for which we don't have any transactions to back up the summary. I'm going with #2 at the moment, but it seems wasteful to recalculate more than I need to. Anyone got any smart ideas? -- Richard Huxton Archonet Ltd