Search Postgresql Archives

Re: Largest & Smallest Functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





čt 8. 11. 2018 v 7:11 odesílatel Andrew Gierth <andrew@xxxxxxxxxxxxxxxxxxxx> napsal:
>>>>> "Ken" == Ken Tanzer <ken.tanzer@xxxxxxxxx> 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

It is work with custom functions, not with buildin least, greatest

Regards

Pavel


 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)


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux