Re: replace null with 0 in subselect ? - Mailing list pgsql-sql

From Tom Lane
Subject Re: replace null with 0 in subselect ?
Date
Msg-id 29430.1034824217@sss.pgh.pa.us
Whole thread Raw
In response to Re: replace null with 0 in subselect ?  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
List pgsql-sql
Stephan Szabo <sszabo@megazone23.bigpanda.com> writes:
> Coalesce((select max(pos)+1 from tab2), 0)
> should work.

Small comment: it's probably noticeably faster to do(select Coalesce(max(pos), 0) +1 from tab2)

I think that the former will result in two evaluations of the sub-select
in the typical case, because COALESCE(foo, bar) is only a macro forCASE WHEN foo IS NOT NULL THEN foo ELSE bar END
and that computes foo twice when foo isn't null.

My version isn't perfectly efficient either, because it will run two
copies of the MAX() calculation inside the sub-select; but that's still
better than two sub-selects.

Sometime we should reimplement COALESCE as a primitive instead of a
macro for a CASE expression.
        regards, tom lane


pgsql-sql by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: getting the current date
Next
From: Ludwig Lim
Date:
Subject: Re: getting the current date