Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 6E966BEAE5C for ; Wed, 25 Jul 2012 09:53:17 -0300 (ADT) Received: from mahout.postgresql.org ([98.129.198.123]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Su15X-00061c-0X for pgsql-bugs@postgresql.org; Wed, 25 Jul 2012 12:53:17 +0000 Received: from [204.145.124.246] (helo=wrigleys.postgresql.org) by mahout.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1Su15K-0003YJ-3A for pgsql-bugs@postgresql.org; Wed, 25 Jul 2012 12:53:02 +0000 Received: from www-data by wrigleys.postgresql.org with local (Exim 4.72) (envelope-from ) id 1Su15J-0001Oj-Ja for pgsql-bugs@postgresql.org; Wed, 25 Jul 2012 12:53:01 +0000 Date: Wed, 25 Jul 2012 12:53:01 +0000 Message-Id: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #6761: unexpected behaviour of 'now'::timestamp To: pgsql-bugs@postgresql.org From: bert@brothom.nl X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201207/191 X-Sequence-Number: 33667 The following bug has been logged on the website: Bug reference: 6761 Logged by: Bert Thomas Email address: bert@brothom.nl PostgreSQL version: 9.1.3 Operating system: Linux Description: = Hi, To reproduce what I mean, consider this function: CREATE FUNCTION testbug() RETURNS character varying LANGUAGE plpgsql AS $$declare l_ts timestamp(0); begin l_ts :=3D 'now'::timestamp(0); return l_ts::varchar; end $$; If a program invokes this function multiple times on a single connection, only the first time the correct date and time is produced. All other invocations return the exact same value as the first invocation. Changing the function to this fixes the problem: CREATE FUNCTION testbug() RETURNS character varying LANGUAGE plpgsql AS $$declare l_ts timestamp(0); l_nu varchar; begin l_nu :=3D 'now'; l_ts :=3D l_nu::timestamp(0); return l_ts::varchar; end $$; Appearently the expression is re-evaluated every time in this case, whilst in the first case it is only evaluated once as the constant 'now' could not change obviously. I'm not sure if this is a bug or not, but at least it is suprising behaviour. To me it looks like a bad form of optimization. Kind regards, Bert Thomas BroThom