Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdgoF-0008OG-Tw for pgsql-general@arkaria.postgresql.org; Thu, 21 Oct 2021 22:45:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mdgoD-0003hp-NW for pgsql-general@arkaria.postgresql.org; Thu, 21 Oct 2021 22:45:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdgoD-0003he-A4 for pgsql-general@lists.postgresql.org; Thu, 21 Oct 2021 22:45:13 +0000 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mdgo9-0000kZ-Hj for pgsql-general@lists.postgresql.org; Thu, 21 Oct 2021 22:45:12 +0000 Received: by mail-pg1-x536.google.com with SMTP id g184so1597284pgc.6 for ; Thu, 21 Oct 2021 15:45:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=+OCWUj4sZeDDANpq5dIx6VU4xkE+tQtiMGJGAjjdxI8=; b=DRQGStNqKVAGfaJiiiBZ/uZYDY3lC7aPtMPZyflRzOjWNHWANMMDE0L/1SCYKD5pWu hA7+iWL9N1f8SidJ8Mw47LeRNaXbo8SdDbAVhn0bTTUGqA6egiEyRmWeGa0C6ezog5+j d5Me8E3OTVStYZh7ZPokviiGp2O5EzABDA4I0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=+OCWUj4sZeDDANpq5dIx6VU4xkE+tQtiMGJGAjjdxI8=; b=iZ7s3qxdrHGuqLv3pNYv7R2rHHiuuxTo7iq5AgWSOxu/UQkZzwCft6GrEfCAFBp5a4 uKD+IDLoSZmViY8P0z06Vm/ti2fKw5I5TpfzXSQ/1TU2DEH51eDwv3g99mCTYipXOSYg br3A6xtkVbF1ET2nixxbxOZCy1SQ2iFr+KMVgay6fDrd5/CEn9BHHqVIe8mLPuvnAMTR 4iGy36A6kDyYjyHeL6Wcw15TCVrJaE/yU4CJUR2a2P8bxT2oZDhYkCZcxKtLiZ2sRkVA 58i4CwjSCqrt3JoSoV9aiy0MWkaNGaLWbUeKHlA4vdZXRfKQe4DopcV/jhF729/BRXge kU9g== X-Gm-Message-State: AOAM531f5g+RGJ5+5lVp/nSCfcCBb/JO9MkTwBTqbnTY/y4wVntclh75 Sul6qu8serLLzwkXl1IGW0a3iQ== X-Google-Smtp-Source: ABdhPJwIgBAOz8+xDVCIXKzZcUewmZ8DaKtmvzxVv5YwDJM3hkQc4PpSEbxyFznI1ll0cJAvZXwdjA== X-Received: by 2002:a63:fb18:: with SMTP id o24mr6532753pgh.8.1634856307309; Thu, 21 Oct 2021 15:45:07 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:888b:65fa:5c90:4cd2]) by smtp.gmail.com with ESMTPSA id lr16sm4559970pjb.29.2021.10.21.15.45.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Oct 2021 15:45:06 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_0CA2A8C3-2D7B-49B3-A5C7-8659EAE8C1B3" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Looking for a doc section that presents the overload selection rules Date: Thu, 21 Oct 2021 15:45:05 -0700 In-Reply-To: Cc: pgsql-general list To: Adrian Klaver , "David G. Johnston" References: <4F35D95E-4974-44FC-AA4B-3F1E94580B82@yugabyte.com> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_0CA2A8C3-2D7B-49B3-A5C7-8659EAE8C1B3 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Adrian Klaver wrote: >=20 >> Bryn wrote: >>=20 >> I fear that I'm about to embarrass myself again. So I'll just ask for = forgiveness in advance. Here's a simple test to get started. (All tests = are done in a session where I set the timezone to 'UTC'.) >>=20 >> drop function if exists f(text) cascade; >> drop function if exists f(timestamp) cascade; >> drop function if exists f(timestamptz) cascade; >>=20 >> create function f(t in text) >> returns text >> language plpgsql >> as $body$ >> begin >> return 'plain "text" overload: '||t; >> end; >> $body$; >>=20 >> select f('2021-03-15'::date); >>=20 >> This causes the 42883 error, "function f(date) does not exist". I = might've expected the system to have done an implicit conversion to = "text" because this conversion is supported, thus: >>=20 >> select f(('2021-03-15'::date)::text); >>=20 >> This succeeds with this result: >>=20 >> plain "text" overload: 2021-03-15 >>=20 >> There's clearly a rule at work here. For some reason, the implicit = conversion from "date" to "text" is not considered to be acceptable. >=20 > =46rom 10.3 > " > Look for the best match. >=20 > Discard candidate functions for which the input types do not match and = cannot be converted (using an implicit conversion) to match. unknown = literals are assumed to be convertible to anything for this purpose. If = only one candidate remains, use it; else continue to the next step. > " >=20 > See cast query below. >=20 > For some reason, the implicit conversion from "date" to "timestamptz" = _is_ considered to be preferable to the implicit conversion from "date" = to plain "timestamp". >=20 > 52.62. pg_type =E2=80=94 = https://www.postgresql.org/docs/current/catalog-pg-type.html >=20 > select oid from pg_type where typname =3D 'date'; >=20 > oid > ------ > 1082 >=20 > 52.10. pg_cast =E2=80=94 = https://www.postgresql.org/docs/current/catalog-pg-cast.html >=20 > select * from pg_cast where castsource =3D 1082; >=20 > oid | castsource | casttarget | castfunc | castcontext | castmethod > = -------+------------+------------+----------+-------------+------------ > 11421 | 1082 | 1114 | 2024 | i | f > 11422 | 1082 | 1184 | 1174 | i | f >=20 > Note castcontext of 'i'(implicit) and only to timestamp types per = below. >=20 > select typname, typispreferred from pg_type where oid in (1114, 1184); >=20 > typname | typispreferred > -------------+---------------- > timestamp | f > timestamptz | t >=20 > typispreferred has timestmaptz as preferred cast. >=20 >> I started with "38.6. Function Overloading", followed the link to = "Chapter 10. Type Conversion" and started with "10.3. Functions". I read = "If no exact match is found, see if the function call appears to be a = special type conversion request=E2=80=A6" as far as "Note that the = =E2=80=9Cbest match=E2=80=9D rules are identical for operator and = function type resolution." So I went to "10.2. Operators" and searched = in the page for "timestamp". No hits. Where, in the PG doc, can I find a = statement of the rules that allow me to predict the outcome of my tests? Thank you very much, Adrian. This is just what I needed. "10.3. Functions" doesn't mention "pg_type". And "52.62. pg_type" = doesn't mention "pg_cast". So it's no wonder that I couldn't find what = you showed me here. (At least, that's my excuse.) Thanks, too, to David Johnston for your reply. Yes, I see now that the = "10.1. Overview" page that starts the "Type Conversion" chapter does = have lots of inflexions of the verb "prefer". And close to one of these = there's a link to "Table 52.63" on the "52.62. pg_type" page. But I = failed to spot that. You said "implicit casting to text is bad". Yes, all implicit casting = is, at best, potentially confusing for human code readers. I aim = religiously to avoid this and always aim to use an explicit typecast = instead. And this brings me to what started me on this path today. "\df to_char" = shows that while it has overloads for both plain "timestamp" and = "timestamptz" date-time inputs, it has no "date" overload. Here's a = contrived test: deallocate all; prepare s as with c as ( select '2021-06-15'::date as d, 'dd-Mon-yyyy TZH:TZM' as fmt) select rpad(current_setting('timezone'), 20) as "timezone", to_char(d, fmt) as "implicit cast to = timestamptz", to_char(d::timestamptz, fmt) as "explicit cast to = timestamptz", to_char(d::timestamp, fmt) as "explicit cast to plain = timestamp" from c; \t on set timezone =3D 'Europe/Helsinki'; execute s; set timezone =3D 'America/Los_Angeles'; execute s; \t off It gives the result that I'd expect: Europe/Helsinki | 15-Jun-2021 +03:00 | 15-Jun-2021 = +03:00 | 15-Jun-2021 +00:00 America/Los_Angeles | 15-Jun-2021 -07:00 | 15-Jun-2021 = -07:00 | 15-Jun-2021 +00:00 And, given that nobody would include "TZH:TZM" in the template for = rendering a date (except in this contrived test), then all three text = renderings in this test would be identical. However, it seems to me that the proper practice must be not to rely on = intellectual analysis and the implicit cast. Rather, you must say that = "date" is more like plain "timestamp" than it's like "timestamptz" (in = that it knows nothing about timezones), and to write the explicit cast = to plain "timestamp". But this leads to nastily cluttered code. Why is there no "date" overload of "to_char()"? --Apple-Mail=_0CA2A8C3-2D7B-49B3-A5C7-8659EAE8C1B3 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
Adrian = Klaver wrote:

Bryn wrote:

I fear that I'm = about to embarrass myself again. So I'll just ask for forgiveness in = advance. Here's a simple test to get started. (All tests are done = in a session where I set the timezone to 'UTC'.)

drop function if exists f(text)       =  cascade;
drop function if exists f(timestamp)   = cascade;
drop function if exists f(timestamptz) = cascade;

create function f(t in text)
  returns text
  language plpgsql
as $body$
begin
  return = 'plain "text" overload: '||t;
end;
$body$;

select = f('2021-03-15'::date);

This = causes the 42883 error, "function f(date) does not exist". I might've = expected the system to have done an implicit conversion to "text" = because this conversion is supported, thus:

select f(('2021-03-15'::date)::text);

This succeeds with this result:

plain "text" = overload: 2021-03-15

There's = clearly a rule at work here. For some reason, the implicit conversion = from "date" to "text" is not considered to be acceptable.

=46rom 10.3
"Look for the best match.

Discard = candidate functions for which the input types do not match and cannot be = converted (using an implicit conversion) to match. unknown literals = are assumed to be convertible to anything for this purpose. If only one = candidate remains, use it; else continue to the next step.
"

See cast query below.

For some reason, the implicit conversion from = "date" to "timestamptz" _is_ considered to be preferable to the implicit = conversion from "date" to plain "timestamp".

52.62. pg_type =E2=80=94 https://www.postgresql.org/docs/current/catalog-pg-type.html

select oid = from pg_type where typname =3D 'date';







 Europe/Helsinki  =     | 15-Jun-2021 +03:00         =   | 15-Jun-2021 +03:00         =   | 15-Jun-2021 +00:00
 America/Los_Angeles  | 15-Jun-2021 = -07:00           | 15-Jun-2021 = -07:00           | 15-Jun-2021 = +00:00

And, given that nobody would include "TZH:TZM" in the = template for rendering a date (except in this contrived test), then all = three text renderings in this test would be identical.

However, it seems to me that the proper = practice must be not to rely on intellectual analysis and the implicit = cast. Rather, you must say that "date" is more like plain "timestamp" = than it's like "timestamptz" (in that it knows nothing about timezones), = and to write the explicit cast to plain "timestamp". But this leads to = nastily cluttered code.

Why is there no "date" overload of = "to_char()"?

= --Apple-Mail=_0CA2A8C3-2D7B-49B3-A5C7-8659EAE8C1B3--