pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Denis Zaitsev <zzz@anda.ru>
Cc: pgsql-sql@postgresql.org
Subject: Re: Very strange 'now' behaviour in nested triggers.
Date: Sat, 26 Jul 2003 10:31:44 -0400
Message-ID: <9412.1059229904@sss.pgh.pa.us> (raw)
In-Reply-To: <20030726193947.B1636@natasha.ward.six>
References: <20030726193947.B1636@natasha.ward.six>

Denis Zaitsev <zzz@anda.ru> writes:
> create table xxx (
>     s text,
>     t timestamp
>         default 'now'
> );

That's a dangerous way to define the default --- 'now' is taken as a
literal of type timestamp, which means it will be reduced to a timestamp
constant as soon as a statement that requires the default is planned.
You lose in plpgsql because of plan caching, but you'd also lose if you
tried to PREPARE the insert command.  Example:

regression=# insert into xxx values('a');
INSERT 154541 1
regression=# insert into xxx values('b');
INSERT 154542 1
regression=# prepare s(text) as insert into xxx values($1);
PREPARE
regression=# execute s('q1');
EXECUTE
regression=# execute s('q2');
EXECUTE
regression=# select * from xxx;
 s  |             t
----+----------------------------
 a  | 2003-07-26 10:18:51.364913
 b  | 2003-07-26 10:18:53.519648
 q1 | 2003-07-26 10:19:21.795415
 q2 | 2003-07-26 10:19:21.795415
(4 rows)

The default would work the way you want with almost any other way of
doing it.  For instance

	default now()
	default current_timestamp
	default localtimestamp
	default 'now'::text

Given that you want timestamp without time zone, I'd probably use
"default localtimestamp".

			regards, tom lane



view thread (17+ messages)  latest in thread

Message-ID: <9412.1059229904@sss.pgh.pa.us>
Permalink:  ../9412.1059229904@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/9412.1059229904@sss.pgh.pa.us

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, zzz@anda.ru
  Subject: Re: Very strange 'now' behaviour in nested triggers.
  In-Reply-To: <9412.1059229904@sss.pgh.pa.us>

* 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