Re: immutable functions vs. join for lookups ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: weigelt(at)metux(dot)de
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: immutable functions vs. join for lookups ?
Date: 2005-04-15 21:12:47
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Enrico Weigelt <weigelt(at)metux(dot)de> writes:
> c) CREATE FUNCTION id2username(oid) RETURNS text
> SELECT username AS RESULT FROM users WHERE uid = $1';

This is simply dangerous. The function is *NOT* immutable (it is
stable though). When ... not if ... your application breaks because
you got the wrong answers, you'll get no sympathy from anyone.

The correct question to ask was "if I make a stable function like
this, is it likely to be faster than the join?". The answer is
"probably not; at best it will be equal to the join". The best the
planner is likely to be able to do with the function-based query
is equivalent to a nestloop with inner indexscan (assuming there is
an index on users.uid). If that's the best plan then the join case
should find it too ... but if you are selecting a lot of items rows
then it won't be the best plan.

regards, tom lane

In response to


Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2005-04-15 21:23:34 Re: pgbench Comparison of 7.4.7 to 8.0.2
Previous Message Thomas F.O'Connell 2005-04-15 21:02:29 pgbench Comparison of 7.4.7 to 8.0.2