pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feeddate_trunc not immutable
5+ messages / 4 participants
[nested] [flat]
* date_trunc not immutable
@ 2018-12-15 23:26 Ravi Krishna <srkrishna@fastmail.com>
0 siblings, 2 replies; 5+ messages in thread
From: Ravi Krishna @ 2018-12-15 23:26 UTC (permalink / raw)
To: pgsql-general <pgsql-general@lists.postgresql.org>
Version: PG 10.6 on AWS Linux.
I am trying to create an index on function date_trunc('month',timestamp)
PG is complaining that the function must be marked as IMMUTABLE. So I
assume that date_trunc is not marked as immutable.
Definition of immutable from PG documentation
====================================
All functions and operators used in an index definition must be
"immutable", that is, their results must depend only on their arguments
and never on any outside influence (such as the contents of another
table or the current time). This restriction ensures that the behavior
of the index is well-defined. To use a user-defined function in an index
expression or WHERE clause, remember to mark the function immutable when
you create it.===================================
What am I missing? date_trunc will always return the same value for a
given value. Not sure how I can mark a PG function as immutable.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: date_trunc not immutable
@ 2018-12-15 23:51 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Ravi Krishna <srkrishna@fastmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Adrian Klaver @ 2018-12-15 23:51 UTC (permalink / raw)
To: Ravi Krishna <srkrishna@fastmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>
On 12/15/18 3:26 PM, Ravi Krishna wrote:
> Version: PG 10.6 on AWS Linux.
>
> I am trying to create an index on function date_trunc('month',timestamp)
>
> PG is complaining that the function must be marked as IMMUTABLE. So I
> assume that date_trunc is not marked as immutable.
>
> Definition of immutable from PG documentation
> ====================================
>
> All functions and operators used in an index definition must be
> "immutable", that is, their results must depend only on their arguments
> and never on any outside influence (such as the contents of another
> table or the current time). This restriction ensures that the behavior
> of the index is well-defined. To use a user-defined function in an index
> expression or WHERE clause, remember to mark the function immutable when
> you create it.
> ===================================
> What am I missing? date_trunc will always return the same value for a
> given value. Not sure how I can mark a PG function as immutable.
No it won't:
show timezone;
TimeZone
------------
US/Pacific
select date_trunc('hour', now());
date_trunc
------------------------
2018-12-15 15:00:00-08
set timezone='UTC';
select date_trunc('hour', now());
date_trunc
------------------------
2018-12-15 23:00:00+00
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: date_trunc not immutable
@ 2018-12-16 00:27 Tom Lane <tgl@sss.pgh.pa.us>
parent: Ravi Krishna <srkrishna@fastmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Tom Lane @ 2018-12-16 00:27 UTC (permalink / raw)
To: Ravi Krishna <srkrishna@fastmail.com>; +Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Ravi Krishna <srkrishna@fastmail.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
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: date_trunc not immutable
@ 2018-12-16 00:37 Vitaly Burovoy <vitaly.burovoy@gmail.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 1 reply; 5+ messages in thread
From: Vitaly Burovoy @ 2018-12-16 00:37 UTC (permalink / raw)
To: Ravi Krishna <srkrishna@fastmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general <pgsql-general@lists.postgresql.org>
On 2018-12-15, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
> On 12/15/18 3:26 PM, Ravi Krishna wrote:
>> Version: PG 10.6 on AWS Linux.
>>
>> I am trying to create an index on function date_trunc('month',timestamp)
>>
>> PG is complaining that the function must be marked as IMMUTABLE. So I
>> assume that date_trunc is not marked as immutable.
>>
>> Definition of immutable from PG documentation
>> ====================================
>>
>> All functions and operators used in an index definition must be
>> "immutable", that is, their results must depend only on their arguments
>> and never on any outside influence (such as the contents of another
>> table or the current time). This restriction ensures that the behavior
>> of the index is well-defined. To use a user-defined function in an index
>> expression or WHERE clause, remember to mark the function immutable when
>> you create it.
>> ===================================
>> What am I missing? date_trunc will always return the same value for a
>> given value. Not sure how I can mark a PG function as immutable.
>
> No it won't:
>
> show timezone;
> TimeZone
> ------------
> US/Pacific
>
> select date_trunc('hour', now());
> date_trunc
> ------------------------
> 2018-12-15 15:00:00-08
>
> set timezone='UTC';
>
> select date_trunc('hour', now());
>
> date_trunc
> ------------------------
> 2018-12-15 23:00:00+00
>
Ravi, the date_trunc('month',timestamp) is already immutable (at least in PG11):
postgres=# \df+ date_trunc
Schema | Name | Result data type |
Argument data types | Volatility | ...
------------+------------+-----------------------------+-----------------------------------+------------+-...
pg_catalog | date_trunc | timestamp without time zone | text,
timestamp without time zone | immutable | ...
For the "date_trunc(text, timestampTZ) see Adrian's response, why it
does not always return the same values for the same input.
--
Best regards,
Vitaly Burovoy
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: date_trunc not immutable
@ 2018-12-16 02:08 Ravi Krishna <srkrishna@fastmail.com>
parent: Vitaly Burovoy <vitaly.burovoy@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Ravi Krishna @ 2018-12-16 02:08 UTC (permalink / raw)
To: Vitaly Burovoy <vitaly.burovoy@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general <pgsql-general@lists.postgresql.org>
Thanks all. I forgot the TZ part.
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2018-12-16 02:08 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-15 23:26 date_trunc not immutable Ravi Krishna <srkrishna@fastmail.com>
2018-12-15 23:51 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-12-16 00:37 ` Vitaly Burovoy <vitaly.burovoy@gmail.com>
2018-12-16 02:08 ` Ravi Krishna <srkrishna@fastmail.com>
2018-12-16 00:27 ` Tom Lane <tgl@sss.pgh.pa.us>
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