X-Original-To: pgsql-novice-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 823A03CBEEB for ; Fri, 17 Dec 2004 06:52:26 +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 95814-10 for ; Fri, 17 Dec 2004 06:52:15 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 1CF923CBEEA for ; Fri, 17 Dec 2004 06:52:15 +0000 (GMT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id iBH6qANv013536 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 16 Dec 2004 23:52:12 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id iBH6q9Zo032827; Thu, 16 Dec 2004 23:52:10 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id iBH6q9aT032826; Thu, 16 Dec 2004 23:52:09 -0700 (MST) (envelope-from mfuhr) Date: Thu, 16 Dec 2004 23:52:09 -0700 From: Michael Fuhr To: Keith Worthington Cc: PostgreSQL Novice Subject: Re: FUNCTION, TRIGGER and best practices Message-ID: <20041217065209.GA32514@winnie.fuhr.org> References: <20041217040520.M87972@narrowpathinc.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20041217040520.M87972@narrowpathinc.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.002 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200412/137 X-Sequence-Number: 11714 On Thu, Dec 16, 2004 at 11:05:20PM -0500, Keith Worthington wrote: > Can triggers and functions have the same name? The "Trigger Procedures" section of the "PL/pgSQL - SQL Procedural Language" chapter in the documentation shows a trigger having the same name as the function it calls. And you could always try it. > Is this a good practice? I'd suggest using whatever names seem the most self-documenting for a particular application. Maybe somebody else will present arguments advocating one style or another; I don't have strong opinions either way. Multiple triggers on a table will fire alphabetically by trigger name, so firing order might affect how you name the triggers. > Where should the function and trigger be stored? > In the same schema as the table they are connected to? If objects (functions, types, etc.) are dedicated to tables in a particular schema then I usually create them in that schema to keep everything together. That can be handy for doing backups with "pg_dump --schema schemaname". > I will now be attempting to expand this function to move data from two source > tables to four or more target tables. (Different source columns go to > different tables.) Is it possible to do a transaction/commit/rollback inside > a function that is driven by a trigger so that if any portion of the transfer > fails it is all rolled back? See the last paragraph of the "Structure of PL/pgSQL" section in the "PL/pgSQL - SQL Procedural Language" chapter of the documentation, as well as the "Errors and Messages" section in the same chapter. Functions can't execute COMMIT or ROLLBACK, but they can raise an exception to abort the current transaction. In PostgreSQL 8.0 functions can trap errors, effectively allowing you to have a subtransaction inside your function. See "Trapping Errors" in the "Control Structures" documentation for details. -- Michael Fuhr http://www.fuhr.org/~mfuhr/