Re: Short-circuit boolean evaluation - Mailing list pgsql-general

From Jasen Betts
Subject Re: Short-circuit boolean evaluation
Date
Msg-id ipjjjv$a9t$1@reversiblemaps.ath.cx
Whole thread Raw
In response to Short-circuit boolean evaluation  (Jon Smark <jon.smark@yahoo.com>)
List pgsql-general
On 2011-04-30, Jon Smark <jon.smark@yahoo.com> wrote:
> Hi,
>
> Does Postgresql perform short-circuit boolean evaluation both in SQL
> and PL/pgSQL functions?

sometimes.

the planner will rearrange what you write,

for this reason it is very likely that

>  SELECT count(*) FROM foobar WHERE foobar.id = $1 AND do_stuff (foobar.name);

will perform as well as

   SELECT count(*) FROM foobar WHERE do_stuff (foobar.name) and foobar.id = $1;

because the planner will rewrite this 'bad version' to execute the
same as the good version.

If you have an index on foobar.id or on do_stuff(foobar.name) or on
both it might be used to speed up the query.

You can give the planner a hint as to how expensive each function is
when you define the function.

in general the planner will take care of it for you

if you want to control when the function gets called with a boolean
test consider using case.

--
⚂⚃ 100% natural

pgsql-general by date:

Previous
From: Jasen Betts
Date:
Subject: Re: Postgresql, PSN hack and table limits
Next
From: Stefan Keller
Date:
Subject: Re: Values larger than 1/3 of a buffer page cannot be indexed (hstore)