X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id CB93CD1B506 for ; Fri, 3 Oct 2003 17:50:29 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 03365-02 for ; Fri, 3 Oct 2003 14:49:44 -0300 (ADT) Received: from news.hub.org (unknown [64.117.224.194]) by svr1.postgresql.org (Postfix) with ESMTP id 08886D1B4E3 for ; Fri, 3 Oct 2003 14:49:43 -0300 (ADT) Received: from news.hub.org (host-64-117-224-194.altec1.com [64.117.224.194] (may be forged)) by news.hub.org (8.12.9/8.12.9) with ESMTP id h93HndQh014410 for ; Fri, 3 Oct 2003 17:49:39 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h93HiUxa013758 for pgsql-admin@postgresql.org; Fri, 3 Oct 2003 17:44:30 GMT From: Christopher Browne X-Newsgroups: comp.databases.postgresql.admin Subject: Re: Partial indexes ... any good theoretical discussion? Date: Fri, 03 Oct 2003 13:38:16 -0400 Organization: cbbrowne Computing Inc Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@news.hub.org X-message-flag: Outlook is rather hackable, isn't it? X-Home-Page: http://www.cbbrowne.com/info/ X-Affero: http://svcs.affero.net/rm.php?r=cbbrowne User-Agent: Gnus/5.1003 (Gnus v5.10.3) XEmacs/21.4 (Reasonable Discussion, linux) Cancel-Lock: sha1:5Ac332Jq9q5EYyiGK5YprgfrfcU= To: pgsql-admin@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200310/50 X-Sequence-Number: 10611 In an attempt to throw the authorities off his trail, Jeff Boes 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