Received: from localhost (unknown [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id 0C8DB2E015A for ; Tue, 25 Dec 2007 20:20:59 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 50730-07 for ; Tue, 25 Dec 2007 20:20:51 -0400 (AST) Received: from web31813.mail.mud.yahoo.com (web31813.mail.mud.yahoo.com [68.142.207.76]) by postgresql.org (Postfix) with SMTP id 007E92E004A for ; Tue, 25 Dec 2007 20:20:54 -0400 (AST) Received: (qmail 56328 invoked by uid 60001); 26 Dec 2007 00:20:53 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:Date:From:Subject:To:MIME-Version:Content-Type:Message-ID; b=6sWMhEKjMQVEJHPmD0kuNsDklxG+t2eE/7Bk3iFhyNndNlKmw3WvWg5XBf8VtfcIkOJzJDusEJgrzkonDZcU/UVsitLV7DApWcGrjaMjvIznajAjJbMfXPZMJax3Wgj6o5+9Q1Dv4bOs5kjV9Qskc2I0dezo4BXMX4ncx6xjVfY=; X-YMail-OSG: q6cfJ0IVM1n91sQtnxZ9SUMk3qauHwXYwl0d68NZ1pZK01upBaBMowrpMmHATrlF4smyIRlgiA-- Received: from [75.84.89.248] by web31813.mail.mud.yahoo.com via HTTP; Tue, 25 Dec 2007 16:20:53 PST X-Mailer: YahooMailWebService/0.7.162 Date: Tue, 25 Dec 2007 16:20:53 -0800 (PST) From: Richard Broersma Jr Subject: Advice for generalizing trigger functions To: SQL Postgresql List MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Message-ID: <781842.52422.qm@web31813.mail.mud.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/132 X-Sequence-Number: 29824 I've created quite a few functions that log modifications to various history tables. (the history table has the same name as the base table but is prefixed by the 'History.' schema.) The only difference between functions I can find is the table name. Is there any way to generalize these myriad of functions into one? Below is a sample of a typical logging trigger function. Regards, Richard Broersma Jr. CREATE OR REPLACE FUNCTION "project"."log_managers_ops"() RETURNS trigger AS $BODY$ BEGIN IF TG_OP IN ( 'UPDATE', 'DELETE' ) THEN UPDATE History.Managers AS M SET endts = now() WHERE M.manager_id = OLD.manager_id AND now() BETWEEN M.startts AND M.endts; end IF; IF TG_OP IN ( 'UPDATE', 'INSERT' ) THEN INSERT INTO History.Managers VALUES ( now()::timestamptz, 'INFINITY'::timestamptz, NEW.*); RETURN NEW; END IF; RETURN OLD; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER;