Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 5D7C51337B68 for ; Thu, 16 Dec 2010 16:48:19 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 38882-05 for ; Thu, 16 Dec 2010 20:48:12 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-gw0-f49.google.com (mail-gw0-f49.google.com [74.125.83.49]) by mail.postgresql.org (Postfix) with ESMTP id DFE9C133655C for ; Thu, 16 Dec 2010 16:48:11 -0400 (AST) Received: by gwj20 with SMTP id 20so2401575gwj.22 for ; Thu, 16 Dec 2010 12:48:12 -0800 (PST) Received: by 10.100.209.5 with SMTP id h5mr85426ang.238.1292532159344; Thu, 16 Dec 2010 12:42:39 -0800 (PST) Received: from [192.168.0.11] (75-139-158-182.dhcp.gwnt.ga.charter.com [75.139.158.182]) by mx.google.com with ESMTPS id c7sm3260332ana.17.2010.12.16.12.42.37 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 16 Dec 2010 12:42:38 -0800 (PST) From: Chris Ruprecht Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Create/update trigger auto-populate fields. Date: Thu, 16 Dec 2010 15:42:37 -0500 Message-Id: <0E78342B-BB79-4DB4-B7CA-004911A95F8D@ruprecht.org> To: pgsql-sql@postgresql.org Mime-Version: 1.0 (Apple Message framework v1082) X-Mailer: Apple Mail (2.1082) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.6 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201012/84 X-Sequence-Number: 34920 Hey guys, To start, I need to mention that I did read chapter 39-3 on triggers and = do understand how they work, in the current (9.0) manuals (OK, maybe the = manuals are RC1, but the content is the same as in 8.4... :). I have a simple table, person: pe_id | bigint | not null default = nextval(('"pe_seq"'::text)::regclass) fname | character varying(100) | not null minit | character varying(3) |=20 lname | character varying(100) | not null us_id | bigint | default 0 cre_ts | timestamp with time zone | not null default now() When a record gets inserted/updated, I want to auto-update the us_id and = cre_ts fields. In my trigger function, I do: CREATE or REPLACE FUNCTION person_stamp() RETURNS trigger=20 AS $person_stamp$=20 BEGIN -- Remember who changed the record and when=20 NEW.cre_ts :=3D current_timestamp;=20 NEW.us_id :=3D TG_ARG[0];=20 RETURN NEW; END; $person_stamp$ LANGUAGE plpgsql; My real DB user(s) (webserver, one dbuser for everybody) makes the DB = connection. Application users are not DB users as they are not created = with 'createuser'. They are application users that have an entry in an = application table. That is the user ID, that I need to use to = auto-populate the us_id field, current_user will give me values like = 'root', 'webserver' or 'postgres' or such, that is not what I need. In the trigger definition, I can hard code something like CREATE TRIGGER person_stamp BEFORE INSERT OR UPDATE ON person FOR EACH ROW EXECUTE PROCEDURE person_stamp( 1 ); and use TG_ARGV[0] to assign that value to NEW.us_id, but that always = assigns us_id =3D 1;, which is not what I want either. How to I make the trigger pass the us_id to the person_stamp function, = where us_id can be any value in appuser.us_id, other than insert into = person (..., us_id) values (..., MyAppUserId ) or update person set ..., = us_id =3D MyAppUserId ...? Thanks, Chris