From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Ken Tanzer <ken.tanzer@gmail.com>
Cc: PG-General Mailing List <pgsql-general@postgresql.org>
Subject: Re: Largest & Smallest Functions
Date: Thu, 08 Nov 2018 06:10:50 +0000
Message-ID: <87a7mkyuo7.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAD3a31WVxY7ARuQ=c0t6dDfjskeHd1Ymv+fNyuz=xYaG4GCXnQ@mail.gmail.com>
References: <CAD3a31WVxY7ARuQ=c0t6dDfjskeHd1Ymv+fNyuz=xYaG4GCXnQ@mail.gmail.com>
>>>>> "Ken" == Ken Tanzer <ken.tanzer@gmail.com> writes:
Ken> Hi. Building on the [type]_larger and _smaller functions (and
Ken> lifting from the documentation), I put together a couple of
Ken> functions that will take any number of arguments:
Ken> CREATE FUNCTION largest(VARIADIC anyarray) RETURNS anyelement AS
Ken> $$ SELECT max($1[i]) FROM generate_subscripts($1, 1) g(i); $$
Ken> LANGUAGE SQL IMMUTABLE;
Ken> So far so good. I can do smallest(2,4,7), etc. But to take this a
Ken> convenient step further, sometimes I want the smallest or largest
Ken> from values already in an array. So I can create these functions:
But you don't need to create more functions, because you can do this:
select largest(variadic array[1,2,3]);
largest
---------
3
Ken> So here's my questions:
Ken> 1) Is there any way to collapse those four functions into two? (Or
Ken> is there a better way to go about this?)
See above
Ken> 2) Is there any particular reason functions like that aren't built
Ken> into Postgres? They seem like they would be useful. (Or maybe I
Ken> missed them?)
As already pointed out, greatest() and least() exist (though they were
added before VARIADIC was, so they don't use it)
Ken> 3) Bonus question--how come all the existing _larger and _smaller
Ken> functions are specific to each data type, as opposed to more
Ken> general smaller/larger functions?
Because it saves looking up the type comparison function and doing an
indirect call.
--
Andrew (irc:RhodiumToad)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: andrew@tao11.riddles.org.uk, ken.tanzer@gmail.com
Subject: Re: Largest & Smallest Functions
In-Reply-To: <87a7mkyuo7.fsf@news-spur.riddles.org.uk>
References: <CAD3a31WVxY7ARuQ=c0t6dDfjskeHd1Ymv+fNyuz=xYaG4GCXnQ@mail.gmail.com> <87a7mkyuo7.fsf@news-spur.riddles.org.uk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox