Re: Defer a functional index calculation?

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Defer a functional index calculation?
Date: 2009-03-23 11:46:09
Message-ID: 20090323114608.GM32672@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Mar 20, 2009 at 05:37:33PM -0700, Randall Lucas wrote:
> I added a functional index.
>
> create table example (id serial primary key, stuff text, parent_id int);
> create index example_root_idx on example (get_root_id(id));
>
> (get_root_id(id) pulls an example row and recurses onto parent_id until it
> hits a root)

I don't think you can do that; are you lying in the function's
definition that it's "immutable"? As far as I know, and a quick check
of the manual[1] seems to confirm it, all functions used in functional
indexes must be immutable and the execution of an immutable function
can't depend on any data in the database. If you're creating this
function as "immutable" then this is lying to the database and it's
guaranteed to go wrong at some point (as you seem to be finding out).

What about having some trigger to "cache" the entry's root "parent_id"
in another column?

--
Sam http://samason.me.uk/

[1] http://www.postgresql.org/docs/current/static/sql-createindex.html

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2009-03-23 12:19:44 Re: text column constraint, newbie question
Previous Message Dmitri Girski 2009-03-23 11:00:16 LISTEN/NOTIFY problem