Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id C81E82E0040 for ; Mon, 25 Feb 2008 21:35:58 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 99089-03 for ; Mon, 25 Feb 2008 21:35:48 -0400 (AST) X-Greylist: delayed 00:09:09.494414 by SQLgrey-1.7.5 Received: from server.firetrust.net (mailserver.firetrust.com [209.213.221.13]) by postgresql.org (Postfix) with ESMTP id E20CB2E003D for ; Mon, 25 Feb 2008 21:35:55 -0400 (AST) Received: from localhost.localdomain (root@[192.168.1.247]) (authenticated bits=0) by server.firetrust.net (8.14.2/8.14.2) with ESMTP id m1Q1QUZE013617 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Tue, 26 Feb 2008 14:26:43 +1300 Date: Tue, 26 Feb 2008 14:26:29 +1300 From: Steve Holdoway To: pgsql-admin@postgresql.org Subject: Re: example of insert timestamp Message-Id: <20080226142629.1bc12aa7.steve.holdoway@firetrust.com> In-Reply-To: <200802251821.19453.david.bear@asu.edu> References: <200802251821.19453.david.bear@asu.edu> Organization: Firetrust Ltd X-Mailer: Sylpheed 2.4.8 (GTK+ 2.12.7; i486-pc-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; protocol="application/pgp-signature"; micalg="PGP-SHA1"; boundary="Signature=_Tue__26_Feb_2008_14_26_29_+1300_5fENYs2vAXYvm1op" X-MailWasher-Server-Scanned: Checked by MailWasher server v2.2.3 (www.Firetrust.com) X-MailWasher-Server-Status: Clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/327 X-Sequence-Number: 28401 --Signature=_Tue__26_Feb_2008_14_26_29_+1300_5fENYs2vAXYvm1op Content-Type: text/plain; charset=US-ASCII Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, 25 Feb 2008 18:21:19 -0700 David Bear wrote: > I've been reading about triggers on insert and found the page at=20 > http://www.postgresql.org/docs/8.1/interactive/triggers.html with some sa= mple=20 > code in the comments. >=20 > I'm thinking what I want can't really be this involved. >=20 > I want to have a table with a timestamp field that automatically gets the= =20 > value of now() on insert. The timestamp will never be updated. I assume I= =20 > need to create a trigger to do this. If there is an easier way, please=20 > advise. >=20 > Otherwise, is there any sample code that would should be how to do this? = The=20 > sample on the page above looks like overkill. >=20 > --=20 >=20 > David Bear > College of Public Programs/ASU > 411 N Central, Phoenix, AZ 85004 >=20 > ---------------------------(end of broadcast)--------------------------- > TIP 3: Have you checked our extensive FAQ? >=20 > http://www.postgresql.org/docs/faq Here's a quick example which sets column dateadded to now on insert... can'= t get much simpler! create or replace function surbl.AddUrl() returns TRIGGER AS $$ BEGIN -- Must have an url to add IF ( NEW.url IS NULL ) THEN RETURN NULL; END IF; NEW.dateadded :=3D now(); RETURN NEW; END; $$ language 'plpgsql'; drop trigger addurl_trig ON surbl.rawdata; create trigger addurl_trig BEFORE INSERT ON surbl.rawdata FOR EACH ROW EXECUTE PROCEDURE surbl.AddUrl(); --Signature=_Tue__26_Feb_2008_14_26_29_+1300_5fENYs2vAXYvm1op Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFHw2rFDN07rEXM5z0RArMiAJ0fgD9hXLMucEl93XfoeWOdk1iNzACfTaU7 k62nImJfm1g8mBbBjkI6Ing= =whVK -----END PGP SIGNATURE----- --Signature=_Tue__26_Feb_2008_14_26_29_+1300_5fENYs2vAXYvm1op--