pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: bert@brothom.nl
To: pgsql-bugs@postgresql.org
Subject: BUG #6761: unexpected behaviour of 'now'::timestamp
Date: Wed, 25 Jul 2012 12:53:01 +0000
Message-ID: <E1Su15J-0001Oj-Ja@wrigleys.postgresql.org> (raw)

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 := '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 := 'now';
  l_ts := 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




view thread (4+ messages)  latest in thread

Message-ID: <E1Su15J-0001Oj-Ja@wrigleys.postgresql.org>
Permalink:  ../E1Su15J-0001Oj-Ja@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/E1Su15J-0001Oj-Ja@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bert@brothom.nl
  Subject: Re: BUG #6761: unexpected behaviour of 'now'::timestamp
  In-Reply-To: <E1Su15J-0001Oj-Ja@wrigleys.postgresql.org>
  References: <E1Su15J-0001Oj-Ja@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox