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 1ma3Ka-0005ki-SC for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Oct 2021 21:59:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ma3KZ-0008Tc-HN for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Oct 2021 21:59: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 1ma3KZ-0008TU-Ac for pgsql-bugs@lists.postgresql.org; Mon, 11 Oct 2021 21:59: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 1ma3KX-0006Kp-73 for pgsql-bugs@lists.postgresql.org; Mon, 11 Oct 2021 21:59: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 19BLxV3U523249; Mon, 11 Oct 2021 17:59:31 -0400 From: Tom Lane To: Zhihong Zhang cc: pgsql-bugs Subject: Re: Epoch from age is incorrect In-reply-to: References: Comments: In-reply-to Zhihong Zhang message dated "Mon, 11 Oct 2021 16:39:37 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <523247.1633989571.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 11 Oct 2021 17:59:31 -0400 Message-ID: <523248.1633989571@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Zhihong Zhang writes: > The epoch from following statement is incorrect, > test_db=3D# select extract(epoch from age(TIMESTAMP '2013-02-18 06:15:15= ')); > date_part = > ----------- > 272396685 > (1 row) AFAICS it's following the documented definition of epoch for intervals. regression=3D# begin; BEGIN regression=3D*# select extract(epoch from age(TIMESTAMP '2013-02-18 06:15:= 15')); extract = ------------------ 272223885.000000 (1 row) regression=3D*# select age(TIMESTAMP '2013-02-18 06:15:15'); = age = --------------------------------- 8 years 7 mons 20 days 17:44:45 (1 row) regression=3D*# select ((((8 * 365 + 7 * 30 + 20) * 24 + 17) * 60) + 44) *= 60 + 45; ?column? = ----------- 272223885 (1 row) age() is useful for some purposes, but this isn't one of them. I'd recommend considering it as a human-readable approximation rather than something to do further arithmetic with. You'd get more nearly the answer you probably want with regression=3D*# select extract(epoch from localtimestamp - TIMESTAMP '2013= -02-18 06:15:15'); extract = ------------------ 272806988.613568 (1 row) or even more to the point, regression=3D*# select extract(epoch from now() - TIMESTAMPTZ '2013-02-18 = 06:15:15'); extract = ------------------ 272803388.613568 (1 row) regards, tom lane