X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id 513A630E7F8 for ; Thu, 12 Jun 2003 12:00:36 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 55960-03 for ; Thu, 12 Jun 2003 09:00:20 -0300 (ADT) Received: from romana (unknown [207.215.68.169]) by svr1.postgresql.org (Postfix) with ESMTP id 69B7F30E7B9 for ; Thu, 12 Jun 2003 09:00:18 -0300 (ADT) Received: from wotan ([192.168.0.2]) by romana with esmtp (Exim 4.20) id 19QQkc-00043q-AQ; Thu, 12 Jun 2003 05:00:18 -0700 Received: from hajhouse by wotan with local (Exim 3.36 #1 (Debian)) id 19QQlF-00037t-00; Thu, 12 Jun 2003 05:00:57 -0700 Date: Thu, 12 Jun 2003 05:00:57 -0700 From: Henry House To: Anagha Joshi Cc: pgsql-admin@postgresql.org Subject: Re: Timestamp of insertion of the row. Message-ID: <20030612120056.GA11922@houseag.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.4i X-Archive-Number: 200306/126 X-Sequence-Number: 9117 On Wed, Jun 11, 2003 at 02:42:42PM +0530, Anagha Joshi wrote: > Hi, > Is there any way to know data & time when the row is inserted into a > table? Yes. Easy answer: use a column of type 'timestamp default now()'. Whenever row is inserted with the value for that columns unspecified, it will take on the current timestamp. Harder answer: write a function to update the timestamp columns and run before update or insert as a trigger on the table in question. Here is working example: CREATE TABLE "example" ( "id" integer DEFAULT nextval('"example_id_seq"'::text) NOT NULL, "descr" text, "mod" timestamp with time zone DEFAULT now(), "last_user" text, Constraint "example_pkey" Primary Key ("id") ); CREATE OR REPLACE FUNCTION "update_example_timestamp" () RETURNS opaque AS ' BEGIN -- Remember who last changed the row and when NEW.mod := ''now''; NEW.last_user := current_user; RETURN NEW; END; ' LANGUAGE 'plpgsql'; CREATE TRIGGER "example_on_update_set_timestamp" BEFORE INSERT OR UPDATE ON "example" FOR EACH ROW EXECUTE PROCEDURE "update_example_timestamp" (); This also logs the last user to modify the row. This system provides only rudimentary accountability; a more rigorous solution would be to log all inserts and updates to a row in another table example_log with columns for example id, timestamp, and user. PS. On most public lists, HTML e-mail is considered improper. It will also cause people who filter HTML e-mail as spam to likely not see your messages. I recommend sending plain text e-mail only to public mailing lists. -- Henry House The attached file is a digital signature. See for information. My OpenPGP key: .