Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XCYAy-0006Sw-AN for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Jul 2014 18:00:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XCYAx-0006pZ-Ms for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Jul 2014 18:00:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XCYAw-0006pS-MZ for pgsql-bugs@postgresql.org; Wed, 30 Jul 2014 18:00:30 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XCYAt-0000kw-CE for pgsql-bugs@postgresql.org; Wed, 30 Jul 2014 18:00:29 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s6UI0NK1002111; Wed, 30 Jul 2014 14:00:23 -0400 From: Tom Lane To: Peter Nelson cc: "pgsql-bugs@postgresql.org" Subject: Re: extract('epoch' from age()) returning wrong number of seconds In-reply-to: <29D4F821F6A75941B4FE0C0AAAE0E7809CA1E6D6@ex01.code42.local> References: <29D4F821F6A75941B4FE0C0AAAE0E7809CA1E6D6@ex01.code42.local> Comments: In-reply-to Peter Nelson message dated "Wed, 30 Jul 2014 15:33:14 -0000" Date: Wed, 30 Jul 2014 14:00:23 -0400 Message-ID: <2110.1406743223@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Peter Nelson writes: > The forth column in this query is returning the wrong value. No it isn't. The underlying result of the age() function is # select age (('2013-12-31'::date) , ('2013-12-31'::date - '5270400 seconds'::interval)); age -------- 2 mons (1 row) which is the desired result in this case: it's two months from 2013-10-31 to 2013-12-31. Then extract(epoch from interval) arbitrarily assumes that a "month" equals 2592000 seconds (30 days), which isn't the greatest thing, but without any context there's no way for it to do much better. For the result that you're after, timestamp subtraction is the way to get it: select (('2013-12-31'::date) - ('2013-12-31'::date - '5270400 seconds'::interval)); ?column? ---------- 61 days (1 row) Bottom line: age() is not meant to exactly reproduce timestamp subtraction, and it's not a bug that it doesn't. It's *supposed* to give you symbolic results like "2 months" rather than "61 days", or "1 year" rather than "365 days" (or sometimes "366 days"). In the same vein, it tries to abstract away from daylight-savings-time changes ... regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs