postgresql - postgres index with date in where clause -
i have large table several million rows in postgresql 9.1. 1 of columns timestamp time zone.
frequently used query looking data using clause 'column > (now()::date - 11)' last ten days.
i want build index work last months data, limit scan. partial index.
so far have not figured out how use actual last month, started hardcoding '2015-12-01' start date index.
create index q on test (i) > '2015-01-01'; this worked fine, index created. unfortunately, not used, treats '2015-01-01' ::timestamp, while query ::date. index not used , square one.
next tried modify index compare column date, match. here hit immutable wall.
as to_date or cast date mutable functions, dependent on local timezone, index creation fails.
if have test table this:
create table test (i timestamptz); and try create index
create index q on test (i) > to_date('2015-01-01','yyyy-dd-mm'); then fails
error: functions in index predicate must marked immutable this understandable. now, when try specific timezone
create index q on test (i) > to_date('2015-01-01','yyyy-dd-mm') @ time zone 'utc'; it still fails
error: functions in index predicate must marked immutable this don't understand anymore. has timezone defined. else immutable?
i tried creating immutable function myself:
create function datacube=# create or replace function immutable_date(timestamptz) returns date $$ select ($1::date @ time zone 'utc')::date; $$ language sql immutable; but using function in index:
create index q on test (i) > immutable_date('2015-01-01'); fails same error:
error: functions in index predicate must marked immutable i @ loss here. maybe has locales, not timezones? or else makes mutable?
and - maybe there another, simpler way, limit index last month or 2 of data? table partitioning in postgres require rebuilding entire database, , far have not found else.
Comments
Post a Comment