Re: creating index with a cast statement in the predicate

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mark Steben <mark(dot)steben(at)drivedominion(dot)com>
Cc: pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: Re: creating index with a cast statement in the predicate
Date: 2017-07-31 18:12:13
Message-ID: 7027.1501524733@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Mark Steben <mark(dot)steben(at)drivedominion(dot)com> writes:
> I would like to create an index on a column named value: defined as
> varchar(255)
> Values in this column can be many, including both dates and non-dates.
> The developers run a query that first uses a regular expression to filter
> non-dates out
> then, ANDS it with the following
> *and cast(value as date) >= date_trunc('MONTH', current_date) - interval '
> 2 Month '*
> When I attempt to create an index using this statement in the
> predicate, I get the error that states that functions in an index predicate
> must be marked as Immutable.

I think it's unhappy about the varchar-to-date cast. That is not
considered immutable because the behavior of date_in() depends on
the setting of DateStyle.

regards, tom lane

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Dhandapani Shanmugam 2017-08-01 12:12:33 native connector to Postgres
Previous Message Mark Steben 2017-07-31 17:33:43 creating index with a cast statement in the predicate