Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKdX2-0007vd-Ge for pgsql-general@arkaria.postgresql.org; Thu, 08 Nov 2018 06:11:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gKdX0-0004hR-AZ for pgsql-general@arkaria.postgresql.org; Thu, 08 Nov 2018 06:11:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKdWs-0003z1-Q6 for pgsql-general@lists.postgresql.org; Thu, 08 Nov 2018 06:10:58 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gKdWo-0006hQ-Hq for pgsql-general@postgresql.org; Thu, 08 Nov 2018 06:10:56 +0000 Received: from [192.168.127.1] (port=30648 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gKdWl-0007c6-A8; Thu, 08 Nov 2018 06:10:51 +0000 Received: from [127.0.0.1] (port=43802 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gKdWk-00022o-Kk; Thu, 08 Nov 2018 06:10:50 +0000 From: Andrew Gierth To: Ken Tanzer Cc: PG-General Mailing List Subject: Re: Largest & Smallest Functions In-Reply-To: (Ken Tanzer's message of "Wed, 7 Nov 2018 13:37:09 -0800") Message-ID: <87a7mkyuo7.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Thu, 08 Nov 2018 06:10:50 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Ken" == Ken Tanzer 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)