Re: LEAST and GREATEST functions? - Mailing list pgsql-sql

From Tom Lane
Subject Re: LEAST and GREATEST functions?
Date
Msg-id 25189.1057127804@sss.pgh.pa.us
Whole thread Raw
In response to Re: LEAST and GREATEST functions?  (Ang Chin Han <angch@bytecraft.com.my>)
Responses Re: LEAST and GREATEST functions?  (Ang Chin Han <angch@bytecraft.com.my>)
List pgsql-sql
Ang Chin Han <angch@bytecraft.com.my> writes:
> I'd say we need to have LEAST and GREATEST at least somewhere in contrib 
> (as functions) if not core, to make transition from other RDBMS to 
> postgresql easier.
> A brief test shows that we would incur quite a performance penalty (I 
> compared COALESCE with coalesce_sql_function) if it isn't hardwiring.

In 7.4 I think that tradeoff will change significantly.  SQL functions
are polymorphic thanks to Joe Conway, and they're inline-able thanks
to me ;-), so there's really no difference between writing the strictly
SQL-compliant

SELECT CASE WHEN a>b THEN a ELSE b END FROM foo;

and writing

create function greatest(anyelement, anyelement) returns anyelement as
'select case when $1>$2 then $1 else $2 end' language sql;

SELECT greatest(a,b) FROM foo;

You do have to create several greatest() functions for different numbers
of arguments, but not one for each datatype you want to handle.

I have not seen enough requests for a native LEAST/GREATEST
implementation to make me think we need to do more than this...
certainly I'd rather spend development effort on general facilities
like polymorphism and inlining than on creating one-use facilities
like built-in LEAST/GREATEST.
        regards, tom lane


pgsql-sql by date:

Previous
From: "Jonathan Man"
Date:
Subject: SQL
Next
From: Ang Chin Han
Date:
Subject: Re: LEAST and GREATEST functions?