Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hi9RQ-0004L1-SU for pgsql-general@arkaria.postgresql.org; Tue, 02 Jul 2019 03:26:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hi9RO-0006gK-AN for pgsql-general@arkaria.postgresql.org; Tue, 02 Jul 2019 03:26:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hi9RO-0006gD-0x for pgsql-general@lists.postgresql.org; Tue, 02 Jul 2019 03:26:46 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hi9RL-0006o8-Ix for pgsql-general@lists.postgresql.org; Tue, 02 Jul 2019 03:26:45 +0000 Received: from [192.168.127.1] (port=33432 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 1hi9RJ-000CtJ-4e; Tue, 02 Jul 2019 03:26:41 +0000 Received: from [127.0.0.1] (port=37872 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1hi9RI-0005LQ-G0; Tue, 02 Jul 2019 03:26:40 +0000 From: Andrew Gierth To: Wells Oliver Cc: pgsql-general@lists.postgresql.org Subject: Re: Argument casting hierarchy? In-Reply-To: (Wells Oliver's message of "Mon, 1 Jul 2019 12:18:33 -0700") Message-ID: <8736jpqgne.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Tue, 02 Jul 2019 04:26:40 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Wells" == Wells Oliver writes: Wells> Hi guys, hoping you can help clarify what the 'hierarchy' of Wells> casts might be in function arguments. In terms of which casts will be selected in some given context, there isn't exactly a "hierarchy", but some types are designated as "preferred" types in their category. Of the builtin types, the preferred ones are boolean, text, oid, double precision, inet, timestamptz, interval and varbit. (See pg_type.typispreferred and pg_type.typcategory.) If there are several ways to implicitly cast function arguments to match signatures of known functions, then the result is ambiguous (and hence an error) _unless_ there is one (and only one) combination of casts to preferred types in matching categories. So in your example, if you had a variant of the function (a float8, b float8) then smallint inputs would call that variant, in preference to the bigint or real ones, because float8 (aka double precision) is a preferred type in category N (number types). You have to be a bit careful with this because it sometimes leads to unexpected casts or loss of precision. If your function will be casting the input values to "numeric", for example, then it is better to have just one function with numeric type args, rather than risk getting casts like numeric -> float8 -> numeric. Another commonly seen example is the mis-casting of "date" type to timestamptz (as the preferred type) where timestamp without tz was intended or semantically required. -- Andrew (irc:RhodiumToad)