Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id EB153475DFD for ; Thu, 16 May 2002 13:58:32 -0400 (EDT) Received: from ler-freebie.iadfw.net (ler-freebie.iadfw.net [206.66.13.221]) by postgresql.org (Postfix) with ESMTP id C5D38475CEA for ; Thu, 16 May 2002 13:51:04 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by ler-freebie.iadfw.net (8.12.2/8.12.2) with ESMTP id g4GHp3v2017100; Thu, 16 May 2002 12:51:03 -0500 (CDT) (envelope-from ler@lerctr.org) Subject: Re: trigger question From: Larry Rosenman To: josh@agliodbs.com Cc: pgsql-sql@postgresql.org In-Reply-To: <200205161049.23148.josh@agliodbs.com> References: <1021570694.95676.7.camel@ler-freebie.iadfw.net> <200205161049.23148.josh@agliodbs.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Mailer: Ximian Evolution 1.0.5 Date: 16 May 2002 12:51:03 -0500 Message-Id: <1021571463.95676.11.camel@ler-freebie.iadfw.net> Mime-Version: 1.0 X-Archive-Number: 200205/165 X-Sequence-Number: 7801 On Thu, 2002-05-16 at 12:49, Josh Berkus wrote: > Larry, > > > Is there a way in PL/pgSQL to have a trigger look at the fields in a > > %ROWTYPE variable to look for changes? > > > > I'm looking to be able to log the fields that are different between old > > and new in a trigger, but don't want to have to list each field (It > > changes, occasionally, but it changes). I'd also like to add a > > timestamp and flag field when I store the record in the log table. > > > > Am I just SOL, or is there a way to do this? (7.2.1, if it matters). > > I'm pretty sure there is a way to do what you want, and it's probably simpler > than you think. > > However, I need you to be more explicit, with examples. I can't quite figure > out what is is you're trying to do. > given the following table: CREATE TABLE "networks" ( "netblock" cidr, "router" integer, "interface" character varying(256), "dest_ip" inet, "mis_token" character(16), "assigned_date" date, "assigned_by" character varying(256), "justification_now" integer, "justification_1yr" integer, "cust_asn" integer, "comments" character varying(2048), "other_reference" character varying(256), "parent_asn" integer, "status" integer NOT NULL, "purpose" integer, "last_update_by" character varying(256), "last_update_at" timestamp with time zone, "customer_reference" integer, "natblock" cidr ); I want to log the changes to it in a trigger. Occasionally we add fields to it, so I'd like the trigger to know about them automatically. I need to log old/new for just the fields that change (and the netblock, which is the primary key). I realize I can build up the insert, but that seems to be the hard way. > -Josh Berkus -- Larry Rosenman http://www.lerctr.org/~ler Phone: +1 972-414-9812 E-Mail: ler@lerctr.org US Mail: 1905 Steamboat Springs Drive, Garland, TX 75044-6749