Re: Index optimization ?

From: Bo Lorentsen <bl(at)netgroup(dot)dk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Index optimization ?
Date: 2005-01-16 16:30:22
Message-ID: 41EA969E.2090207@netgroup.dk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Glaesemann wrote:

> I don't believe it has necessarily anything to do with the return
> type, but rather the return value. An index only works if you know
> what the value is, and the return value for a volatile function is not
> guaranteed to be the same for given parameters. Here's a contrived
> (and untestsd) example, but one I think makes it clear:
>
> CREATE FUNCTION plus_random ( INTEGER )
> RETURNS INTEGER
> LANGUAGE SQL AS '
> SELECT round( $1 + random() * 100 );
> ';
>
> One could conceivably attempt to make a functional index using
> plus_random(), but the result it gives every time is indeterminant.
> How would you be able to usefully search for values in an index that
> is based on this function? Would it make sense do to do so?

What you say is that PG can't see the difference between this
"plus_random" and the "currval", right.

But if I have a select (a quite strange one), like this :

SELECT * FROM test_table WHERE id = plus_random( test_col );

I don't understand the problem. The function always return an integer as
specified in the function decl. so why not use the PK index for search,
instead of using seq scan ? The value is totally unpredictable but it is
still an integer and the pk index is still useful regarding performance !

I know there is something I don't understand, so I just have to ask :-)

> Does this help? (And if I'm completely off base, someone please let me
> know :)

No this time I think missed the point, I understand the volatility of
functions, so the planer know what to expect from the function,
regarding side effect, but I still don't understand why this influences
the choice of valid indexes.

/BL

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jasper Potts 2005-01-16 16:32:21 Bypass administrative permissions restriction on windows?
Previous Message Michael Glaesemann 2005-01-16 15:49:00 Re: Index optimization ?