Обсуждение: [ADMIN] creating index with a cast statement in the predicate

Поиск
Список
Период
Сортировка

[ADMIN] creating index with a cast statement in the predicate

От
Mark Steben
Дата:
Good afternoon

I am currently running postgres 9.4.8.

    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.
Our internal functions date_trunc (all 3) and date are all already defined as immutable.
I can create an index with the regular expression as the predicate but not the cast statement comparison.

   Here is the regular expression index I constructed:
  "primets_custattr_small_regexp" btree (value) WHERE value::text ~ '([0-9][0-9][0-9][0-9])-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])|([0-9][0-9][0-9][0-9])-(0[469]|11])-(0[1-9]|[12][0-9]|30)|([0-9][0-9][0248][048]|[0-9][0-9][13579][26])-(02)-(0[1-9]|1[0-9]|2[0-9])|([0-9][0-9][0248][1235679]|[0-9][0-9][13579][01345789])-(02)-(0[1-9]|1[0-9]|2[0-8])'::text

I would appreciate any insight here.  Thank you

 


--
Mark Steben
 Database Administrator
@utoRevenue | Autobase 
  CRM division of Dominion Dealer Solutions 
95D Ashley Ave.
West Springfield, MA 01089

t: 413.327-3045
f: 413.383-9567

www.fb.com/DominionDealerSolutions
www.twitter.com/DominionDealer
 www.drivedominion.com





Re: [ADMIN] creating index with a cast statement in the predicate

От
Tom Lane
Дата:
Mark Steben <mark.steben@drivedominion.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