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 2BF19528BE for ; Tue, 19 Jul 2005 06:16:12 -0300 (ADT) 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 76045-05 for ; Tue, 19 Jul 2005 09:16:03 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 3616B52806 for ; Tue, 19 Jul 2005 06:16:00 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id E034540DAD2; Tue, 19 Jul 2005 10:15:54 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 0240315EE6; Tue, 19 Jul 2005 10:12:02 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 03747-01; Tue, 19 Jul 2005 10:11:57 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id C249515EDA; Tue, 19 Jul 2005 10:11:57 +0100 (BST) Message-ID: <42DCC3DD.5000806@archonet.com> Date: Tue, 19 Jul 2005 10:11:57 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Andrei Bintintan Cc: pgsql-sql@postgresql.org Subject: Re: Create trigger for auto update function References: <00e001c58b93$ad30b130$0b00a8c0@forge> In-Reply-To: <00e001c58b93$ad30b130$0b00a8c0@forge> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.159 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO, UPPERCASE_25_50 X-Spam-Level: X-Archive-Number: 200507/159 X-Sequence-Number: 22137 Andrei Bintintan wrote: > > Now, I want to write a trigger function that automatically updates the pass_md5 with the md5 function of the pass. > > I tried this: > > CREATE FUNCTION update_pass(integer) RETURNS integer AS $$ > UPDATE hoy SET pass_md5=md5(pass) WHERE id=$1; > SELECT 1; > $$ LANGUAGE SQL; > > and > > CREATE TRIGGER triger_users_pass_md5 > AFTER INSERT OR UPDATE > ON hoy > EXECUTE PROCEDURE update_pass(integer); The simplest way to do this is with a BEFORE trigger, and just modifying the NEW pseudo-record. CREATE OR REPLACE FUNCTION maintain_pass_md5() RETURNS TRIGGER AS ' BEGIN NEW.pass_md5 = md5(NEW.pass); RETURN NEW; END ' LANGUAGE plpgsql; CREATE TRIGGER hoy_maintain_pass_md5 BEFORE INSERT OR UPDATE ON hoy FOR EACH ROW EXECUTE PROCEDURE maintain_pass_md5(); Note that the function is defined to return type TRIGGER and that we return NEW. If we returned NULL, the row would be skipped by the current update statement. This means only one actual on-disk update takes place, and as far as everyone is concerned pass_md5 automagically updates itself. If the md5() function was actually an operation that would take a long time, it might be worth checking whether pass has been changed: IF NEW.pass IS DISTINCT FROM OLD.pass THEN ... END IF However, if you do this then you have to test TG_OP to see whether you are inserting or updating - insert ops don't have OLD defined. HTH -- Richard Huxton Archonet Ltd