I want to have a timestamp array which tracks a series of events. The first event always happens when the row is created, so I want the array to be instantiated with an initial default value. I've tried these two things and they both result in now()
being executed at the time of the statement. Is it possible for me to achieve this with a simple default value, or do I need to write a hook?
# ALTER TABLE messages ADD COLUMN testarray TIMESTAMP[] DEFAULT '{NOW()}';
ALTER TABLE
# ALTER TABLE messages ADD COLUMN testarray2 TIMESTAMP[] DEFAULT '{"NOW()"}';
ALTER TABLE
freedom_development=# \d messages
Table "public.messages"
Column | Type | Modifiers
---------------+-------------------------------+-------------------------------------------------------------------------
id | integer | not null default nextval('messages_id_seq'::regclass)
testarray | timestamp without time zone[] | default '{"2017-12-30 21:00:09.622951"}'::timestamp without time zone[]
testarray2 | timestamp without time zone[] | default '{"2017-12-30 21:00:19.936001"}'::timestamp without time zone[]
TIMESTAMP
both with and without time zone are stored exactly the same on Postgres. The difference is thatWITH
means "with respect for zone/offset", where any offset or zone info passed with input is used to adjust the date-time into UTC before storing. TheWITHOUT
type ignores any passed zone/offset info, so for example noon in Montréal and noon in Kolkata are both stored as noon rather than adjusted to UTC.