Re: date_trunc not immutable

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ravi Krishna <srkrishna(at)fastmail(dot)com>
Cc: "pgsql-general" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: date_trunc not immutable
Date: 2018-12-16 00:27:03
Message-ID: 7869.1544920023@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ravi Krishna <srkrishna(at)fastmail(dot)com> writes:
> I am trying to create an index on function date_trunc('month',timestamp)
> PG is complaining that the function must be marked as IMMUTABLE.

The timestamptz version of it is not immutable, because its effects depend
on the timezone setting:

regression=# set timezone = 'America/New_York';
SET
regression=# select date_trunc('month', now());
date_trunc
------------------------
2018-12-01 00:00:00-05
(1 row)

regression=# set timezone = 'Europe/Paris';
SET
regression=# select date_trunc('month', now());
date_trunc
------------------------
2018-12-01 00:00:00+01
(1 row)

If you want immutability, you need to be working with timestamp-without-tz
or date input, so that timezone isn't a factor.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vitaly Burovoy 2018-12-16 00:37:02 Re: date_trunc not immutable
Previous Message Tom Lane 2018-12-16 00:22:21 Re: simple query on why a merge join plan got selected