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 1mdiEF-0003y4-MZ for pgsql-general@arkaria.postgresql.org; Fri, 22 Oct 2021 00:16:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mdiED-0003hA-Rf for pgsql-general@arkaria.postgresql.org; Fri, 22 Oct 2021 00:16:09 +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 1mdiEC-0003h0-SB for pgsql-general@lists.postgresql.org; Fri, 22 Oct 2021 00:16:09 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mdiE5-0001VQ-48 for pgsql-general@lists.postgresql.org; Fri, 22 Oct 2021 00:16:08 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id C405C5C02A5; Thu, 21 Oct 2021 20:15:59 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 21 Oct 2021 20:15:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= message-id:date:mime-version:subject:to:cc:references:from :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=c 6r14fJFpsEvw0nAHZnKpyyvn5sKAURXO6U7QVftGJM=; b=EKi+wlPwYX7ctIsrA iQeO2HQRc4b9uLdk6v/CCGHRIltjbpYvqmVI4/fKnHNSNnRylCec5hvcwG8mUaKJ aRxQcqFXSQgrnurRyhFMd6BiYZkqQSHfIL/yHn1hkheP797tF7ZcN73TJjVo2YZP esTlisAehacjVFH+X1qhCGx9YZZW4L8D6NVaOvwBC6427E3EuFMyGbcQX/hbherX ec9EL0dWnpKghf3FjNW1FjtlLjR2yuIIRFAzEK8yEaQMti69sP4CTax8I+CbHpMj cwsF8ShzkjcoDpjQ6Xzp5gGN3QilN1GklmIqKoUT9crqTPm+LSpbTMJODUQZORNq Uh22A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=c6r14fJFpsEvw0nAHZnKpyyvn5sKAURXO6U7QVftG JM=; b=IxaID6MMkvHWpLaESoN/UuA7cNTC0QV46o4/paioz8ao7ESjITpBE8fDK AyQ9wSPkP1okAmTXXxedkBTXBDc84FWGQuax2CL/SKvm7p6ZyBK6GEqRJFUxc2MO cEloTGLfvN3IyDhlTJXKAqw8kJ2vQMWZ6ASkMElXlagjWV4krS26pHTQgaxSMnpf o5bWiT6n+e8/GZQA0LqMUA0/80KAcOvfafxZk49GHiTkq3SQs1t6zluMIVdAHHC+ 9l3L3udtX5AqXCP/aAX2oZpK2kGLQI6J1EpEXgI1kczstXZq/x93/YEJfYZOQ0kW ZmtcwnF4pLbYHQoCgVnFmkQExnoiw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvtddrvddvjedgvdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeelkeegjefhkeetheefudevteehiedvudehkeduleet fedvffeuhedtvdegtdeffeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgne cuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhi rghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 21 Oct 2021 20:15:59 -0400 (EDT) Message-ID: <4f7a9fe8-ea27-e4a3-34ac-f05fcfb7f5c3@aklaver.com> Date: Thu, 21 Oct 2021 17:15:58 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: Looking for a doc section that presents the overload selection rules Content-Language: en-US To: Bryn Llewellyn , "David G. Johnston" Cc: pgsql-general list References: <4F35D95E-4974-44FC-AA4B-3F1E94580B82@yugabyte.com> From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/21/21 15:45, Bryn Llewellyn wrote: >> /Adrian Klaver wrote:/ >> >>> /Bryn wrote:/ >>> > 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. This was explicitly dealt with in the Postgres 8.3 release: https://www.postgresql.org/docs/8.3/release-8-3.html E.24.2.1. General Non-character data types are no longer automatically cast to TEXT (Peter, Tom) > > 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 That is because: https://www.postgresql.org/docs/14/functions-formatting.html to_char ( timestamp, text ) → text to_char ( timestamp with time zone, text ) → text Converts time stamp to string according to the given format. to_char(timestamp '2002-04-20 17:31:12.66', 'HH12:MI:SS') → 05:31:12 to_char() expects a timestamp and per my previous post the preferred cast for a date to a timestamp is to timestamptz. > 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 = 'Europe/Helsinki'; > execute s; > > set timezone = '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()"?* > -- Adrian Klaver adrian.klaver@aklaver.com