X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 4F7C6310A98 for ; Mon, 30 Jun 2003 21:45:20 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 93012-02 for ; Mon, 30 Jun 2003 18:45:09 -0300 (ADT) Received: from dbdevel.sunwave.net (h64-141-16-15.sunwave.net [64.141.16.15]) by svr1.postgresql.org (Postfix) with ESMTP id 8E77F310A95 for ; Mon, 30 Jun 2003 18:45:09 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by dbdevel.sunwave.net (Postfix) with ESMTP id 6EA6B178008 for ; Mon, 30 Jun 2003 17:45:08 -0400 (EDT) From: Michael A Nachbaur To: pgsql-sql@postgresql.org Subject: Need help creating a BEFORE DELETE trigger Date: Mon, 30 Jun 2003 14:45:06 -0700 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: <200306301445.06935.mike@nachbaur.com> X-Archive-Number: 200306/347 X-Sequence-Number: 14105 Hello everyone, I'm creating two database tables that will be used to cache the results of a search. Basically, when a search is initiated, an entry will be created in the "Search" table that represents the search, and a single entry will be created in a child table "SearchResults" for every result returned. A foreign key relationship will be associated between the two. CREATE TABLE Search ( id SERIAL , accountid INTEGER REFERENCES account(id) ON DELETE CASCADE NOT NULL , sessionnum CHAR(32) UNIQUE NOT NULL , created TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL , sqlcode TEXT ); CREATE TABLE SearchResults_Customer ( id SERIAL , searchid INTEGER REFERENCES search(id) ON DELETE CASCADE NOT NULL , customerid INTEGER REFERENCES customer(id) ON DELETE CASCADE -- All the results go in fields here ); Now, when any record is deleted in the SearchResults table (via an ON DELETE CASCADE, or other trigger), I'd like the entire search set to be deleted since the search is now invalid. Therefore, if a single record in the SearchResults table is deleted, I want it to instead delete the associated record in the Search table; this'll cause a CASCADE into the SearchResults table, toasting my entire result set. The problem I'm looking at is: could this cause a recursion problem, where the cascading deletion will try to cause the whole thing to cascade again? How can I set this up so I can kill an entire tree of data if any one of it's members dies? Thanks in advance. -- /* Michael A. Nachbaur * http://nachbaur.com/pgpkey.asc */ "I don't know, " said the voice on the PA, "apathetic bloody planet, I've no sympathy at all. "