date_trunc on date is immutable? - Mailing list pgsql-general

From Kian Wright
Subject date_trunc on date is immutable?
Date
Msg-id e88f31fb0912241536u48d65a41j14072b4c034d3266@mail.gmail.com
Whole thread Raw
Responses Re: date_trunc on date is immutable?  (Scott Marlowe <scott.marlowe@gmail.com>)
List pgsql-general
I'm trying to create an index on the month and year of a date field (in 8.3), and I'm getting the "functions in index expression must be marked IMMUTABLE" error message.

I thought dates were immutable, and didn't think that DATE_TRUNC did anything to change that.  These all fail:
  create index enrollments_app_recvd_month_idx on enrollments ( date_trunc('month', appl_recvd_date) );
  create index enrollments_app_recvd_month_idx on enrollments ( (date_trunc('month', appl_recvd_date) at time zone 'pst') );
  create index enrollments_app_recvd_month_idx on enrollments ( to_char(appl_recvd_date, 'YYYYMM') );
  create index enrollments_app_recvd_month_idx on enrollments ( (to_char(extract(year from appl_recvd_date), '0000') || to_char(extract( month from appl_recvd_date), '00')) );

After much experimentation, I finally was able to get this to work:
  create index enrollments_app_recvd_month_idx on enrollments ( (cast(extract(year from appl_recvd_date) as text) || cast(extract(month from appl_recvd_date) as text)) );

I am guessing to_char is mutable because the format string could use a locale specific character, and PG doesn't bother to check the format string when determining whether a function call is immutable. But I'm lost on why date_trunc is mutable, especially after applying a specific time zone. Am I missing something here?

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: How to add month.year column validation
Next
From: Scott Marlowe
Date:
Subject: Re: date_trunc on date is immutable?