pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Wells Oliver <wells.oliver@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Argument casting hierarchy?
Date: Tue, 02 Jul 2019 04:26:40 +0100
Message-ID: <8736jpqgne.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAOC+FBVZzMgF5u91E8sFUPht_8s+Xy5PVxWQ1qncgZLCkM4sMQ@mail.gmail.com>
References: <CAOC+FBVZzMgF5u91E8sFUPht_8s+Xy5PVxWQ1qncgZLCkM4sMQ@mail.gmail.com>

>>>>> "Wells" == Wells Oliver <wells.oliver@gmail.com> 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)





view thread (5+ messages)

Message-ID: <8736jpqgne.fsf@news-spur.riddles.org.uk>
Permalink:  ../8736jpqgne.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/8736jpqgne.fsf@news-spur.riddles.org.uk

 · 

reply

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, wells.oliver@gmail.com
  Subject: Re: Argument casting hierarchy?
  In-Reply-To: <8736jpqgne.fsf@news-spur.riddles.org.uk>
  References: <CAOC+FBVZzMgF5u91E8sFUPht_8s+Xy5PVxWQ1qncgZLCkM4sMQ@mail.gmail.com> <8736jpqgne.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