Denis Zaitsev <[EMAIL PROTECTED]> 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

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
    (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

Reply via email to