pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christopher Browne <cbbrowne@acm.org>
To: pgsql-admin@postgresql.org
Subject: Re: Partial indexes ... any good theoretical discussion?
Date: Fri, 03 Oct 2003 13:38:16 -0400
Message-ID: <m3brsyfchj.fsf@wolfe.cbbrowne.com> (raw)
References: <d701af91955b194af069c0b945f35c16@news.teranews.com>

In an attempt to throw the authorities off his trail, Jeff Boes <jboes@nexcerpt.com> transmitted:
> I've only just now noticed that CREATE INDEX accepts a 'WHERE'
> clause. This is used to create something called a "partial index".
> Hmm, ever being one who sees the world as made of nails when first
> given a hammer ...
>
> One of our tables, with a couple hundred thousand rows) has a
> date-column index. We expire things out of the table every day (the
> vast majority, but not exclusively, stuff that's a couple days
> old). We're frequently running queries against the table, looking
> for "everything since this time yesterday"; we hardly ever look back
> more than 24 hours.
>
> If I created the index as something like:
>
>     CREATE INDEX ix_foo ON foo(the_date)
>       WHERE the_date >= now() - interval '24 hours';
>
> what might I expect as the impact?  Do index values older than 24
> hours drop out? Or must I "refresh" the index from time to time (in
> our application, probably a couple dozen times a day)?

That won't work, unfortunately.

[somedatabase]=# create index partial on transaction_log(trans_on) where
trans_on > now() - '5 days'::interval;
ERROR:  functions in index predicate must be marked IMMUTABLE
[somedatabase]=#

You can't have now() (which is certainly *not* immutable) as part of
the index.

A better idea would be to have a set of several tables, one for each
day, UNION ALL them together to generate a view to support queries,
and use a sequence to control which table is inserted to on any given
day, alongside some rules for insert/deletes.
-- 
(format nil "~S@~S" "aa454" "freenet.carleton.ca")
http://cbbrowne.com/info/linuxxian.html
"Waving away a cloud of smoke, I  look up, and am blinded by a bright,
white light.  It's God. No,  not Richard Stallman, or  Linus Torvalds,
but God. In a booming voice, He  says: "THIS IS A SIGN. USE LINUX, THE
FREE Unix SYSTEM FOR THE 386." -- Matt Welsh



view thread (4+ messages)  latest in thread

Message-ID: <m3brsyfchj.fsf@wolfe.cbbrowne.com>
Permalink:  ../m3brsyfchj.fsf@wolfe.cbbrowne.com/
Also on:    postgresql.org/message-id/m3brsyfchj.fsf@wolfe.cbbrowne.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: cbbrowne@acm.org
  Subject: Re: Partial indexes ... any good theoretical discussion?
  In-Reply-To: <m3brsyfchj.fsf@wolfe.cbbrowne.com>
  References: <d701af91955b194af069c0b945f35c16@news.teranews.com> <m3brsyfchj.fsf@wolfe.cbbrowne.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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