X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 076DC329E12 for ; Tue, 5 Oct 2004 17:19:33 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 66182-01 for ; Tue, 5 Oct 2004 16:19:23 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 4A347329E01 for ; Tue, 5 Oct 2004 17:19:22 +0100 (BST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id i95GJCma012298 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 5 Oct 2004 10:19:14 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id i95GJCHr055696; Tue, 5 Oct 2004 10:19:12 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id i95GJCbu055695; Tue, 5 Oct 2004 10:19:12 -0600 (MDT) (envelope-from mfuhr) Date: Tue, 5 Oct 2004 10:19:12 -0600 From: Michael Fuhr To: Silke Trissl Cc: pgsql-sql@postgresql.org Subject: Re: Using timestamp in function Message-ID: <20041005161912.GA55627@winnie.fuhr.org> References: <4162BFCF.9090501@informatik.hu-berlin.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4162BFCF.9090501@informatik.hu-berlin.de> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/31 X-Sequence-Number: 19134 On Tue, Oct 05, 2004 at 05:37:51PM +0200, Silke Trissl wrote: > CREATE or replace FUNCTION mylog_test(integer, varchar) RETURNS > timestamp AS ' > DECLARE > n ALIAS FOR $1; > logtxt ALIAS FOR $2; > curtime timestamp; > BEGIN > curtime := ''now''; > --INSERT INTO logger VALUES ( nextval(''seq_log''), curtime, > substr(logtxt,0,200)); > RAISE NOTICE ''TIME: %'',curtime; > RETURN curtime; > END; > ' LANGUAGE plpgsql; > > I expected, that the variable curtime gets a new time value, each time > the function is called (at least that is what I understood from the > documentation). This works fine, if I test it with > SELECT mylog_test(5, 'test'); > But as soon as I call the funtion from another function (which I need) > the variable curtime does not change anymore. "Functions and trigger procedures are always executed within a transaction established by an outer query...." [1] "It is important to know that CURRENT_TIMESTAMP and related functions return the start time of the current transaction; their values do not change during the transaction....timeofday() returns the wall-clock time and does advance during transactions." [2] [1] http://www.postgresql.org/docs/7.4/static/plpgsql-structure.html [2] http://www.postgresql.org/docs/7.4/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT -- Michael Fuhr http://www.fuhr.org/~mfuhr/