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 1lYWHJ-0005Y9-Iv for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Apr 2021 15:57:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lYWHH-0006zu-K6 for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Apr 2021 15:57:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lYWHH-0006zn-DB for pgsql-hackers@lists.postgresql.org; Mon, 19 Apr 2021 15:57:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lYWHC-0004l5-4D for pgsql-hackers@postgresql.org; Mon, 19 Apr 2021 15:57:34 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 13JFvP3Y226164; Mon, 19 Apr 2021 11:57:25 -0400 From: Tom Lane To: Peter Eisentraut cc: David Steele , Pavel Stehule , Thomas Munro , Petr Fedorov , pgsql-hackers Subject: Re: Since '2001-09-09 01:46:40'::timestamp microseconds are lost when extracting epoch In-reply-to: <75db3efe-8023-58ef-9ee4-8a6a3101c7ca@2ndquadrant.com> References: <1551357.1599435992@sss.pgh.pa.us> <097a4242-0748-98e3-06ab-ded1d366cd76@2ndquadrant.com> <1893468.1599658711@sss.pgh.pa.us> <38fc499a-ae52-efb6-aad6-82f812d6782d@2ndquadrant.com> <20200917055327.GU2873@paquier.xyz> <20200930071543.GL1996@paquier.xyz> <2fb99632-9302-6509-78ce-3aaed3fd7f40@2ndquadrant.com> <3ff4be02-1e2b-564f-9b53-3dc561da8218@pgmasters.net> <3013402.1615829712@sss.pgh.pa.us> <8fae8673-0d5b-12d9-7342-daed0d590594@2ndquadrant.com> <901bcfb8-36c8-27b2-b148-042392992eeb@2ndquadrant.com> <4158031.1616184417@sss.pgh.pa.us> <75db3efe-8023-58ef-9ee4-8a6a3101c7ca@2ndquadrant.com> Comments: In-reply-to Peter Eisentraut message dated "Thu, 01 Apr 2021 20:49:45 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <226162.1618847845.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 19 Apr 2021 11:57:25 -0400 Message-ID: <226163.1618847845@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Peter Eisentraut writes: > The extract(julian from timestamp) is still a bit in the slow mode, but = > as I previously stated, it's not documented and gives the wrong result, = > so it's not clear whether it should be fixed and what it should do. I = > think I'll register that part as an open item in any case, to see what = > we should do about that. I looked into this issue. It's not quite true that the behavior is entirely undocumented: Appendix B (datetime.sgml) says In the Julian Date system, each day has a sequential number, starting from JD 0 (which is sometimes called the Julian Da= te). JD 0 corresponds to 1 January 4713 BC in the Julian calendar, or 24 November 4714 BC in the Gregorian calendar. Julian Date counting is most often used by astronomers for labeling their nightly observatio= ns, and therefore a date runs from noon UTC to the next noon UTC, rather th= an from midnight to midnight: JD 0 designates the 24 hours from noon UTC o= n 24 November 4714 BC to noon UTC on 25 November 4714 BC. Although PostgreSQL supports Julian Date not= ation for input and output of dates (and also uses Julian dates for some internal datetime calculations), it does not observe the nicety of having dates run from noon to noon. PostgreSQL treats a = Julian Date as running from midnight to midnight. That last bit requires clarification: we treat a Julian date as running from *local* midnight to local midnight (ie in the active timezone, not UTC midnight). So far as I can see, the behavior of extract(julian) is consistent with that definition: regression=3D# show timezone; TimeZone = ------------------ America/New_York (1 row) regression=3D# select date_part('julian', '2021-04-19 00:00:01-04'::timest= amptz); date_part = ------------------- 2459324.000011574 (1 row) regression=3D# select date_part('julian', '2021-04-19 23:59:00-04'::timest= amptz); date_part = -------------------- 2459324.9993055556 (1 row) regression=3D# select date_part('julian', '2021-04-19'::date); date_part = ----------- 2459324 (1 row) I don't see that to_char's J mode differs from this, either. So I don't think there's any code change required (unless you are still worried about speed). What we do need is documentation fixes: * clarify the above bit about local vs UTC midnight * document the existence of the julian field for date_part/extract * fix this bit in the to_char docs to agree with reality, ie s/UTC/local time/: J Julian Day (integer days since November 24, 4714 BC at midn= ight UTC) Perhaps it'd be worth documenting that you can get the standard astronomical definition of Julian date by transposing to time zone UTC-12 before converting. But I think trying to change PG's behavior at this point would be a bad idea. (We could also consider back-patching these doc fixes.) regards, tom lane