Re: Coalesce bug ? - Mailing list pgsql-general

From Adrian Klaver
Subject Re: Coalesce bug ?
Date
Msg-id 50D47FA7.60900@gmail.com
Whole thread Raw
In response to Coalesce bug ?  ("jg" <jg@rilk.com>)
Responses Re: Coalesce bug ?  ("David Johnston" <polobo@yahoo.com>)
List pgsql-general
On 12/21/2012 07:03 AM, jg wrote:
> Hi,
>
> In PostgreSQL 9.2, I have the following behavior, and I found it strange.
>
> ps3 is executed or "never executed" ? !!!
>
> JG
>
> [postgres@]test=# create or replace function ps3(a int) returns int as $$ BEGIN
> RAISE WARNING 'Call ps3(%)=%',$1,$1; RETURN $1::int;
> END; $$ LANGUAGE plpgsql STRICT IMMUTABLE;
> CREATE FUNCTION
> Temps : 22,632 ms
> [postgres@]test=# select coalesce( (select ps3(1)), (SELECT ps3(2)) );
> WARNING:  Call ps3(1)=1
> WARNING:  Call ps3(2)=2
>   coalesce
> ----------
>          1
> (1 ligne)
>
> Temps : 0,692 ms
> [postgres@]test=# select coalesce( ps3(1), ps3(2) );
> WARNING:  Call ps3(1)=1
>   coalesce
> ----------
>          1
> (1 ligne)
>
> Temps : 0,441 ms

Well I believe there is no bug.

In the first case the nested parentheses mean the SELECT statements are
run first and the COALESCE is run on the return results.

In the second case COALESCE is working as advertised. Working left to
right it finds the first non NULL argument and stops.


--
Adrian Klaver
adrian.klaver@gmail.com


pgsql-general by date:

Previous
From: "jg"
Date:
Subject: Re: Coalesce bug ?
Next
From: Merlin Moncure
Date:
Subject: Re: Heavy Function Optimisation