From: Tom Lane <tgl@sss.pgh.pa.us>
To: Peter Nelson <peter.nelson@code42.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: extract('epoch' from age()) returning wrong number of seconds
Date: Wed, 30 Jul 2014 14:00:23 -0400
Message-ID: <2110.1406743223@sss.pgh.pa.us> (raw)
In-Reply-To: <29D4F821F6A75941B4FE0C0AAAE0E7809CA1E6D6@ex01.code42.local>
References: <29D4F821F6A75941B4FE0C0AAAE0E7809CA1E6D6@ex01.code42.local>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Peter Nelson <peter.nelson@code42.com> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, peter.nelson@code42.com
Subject: Re: extract('epoch' from age()) returning wrong number of seconds
In-Reply-To: <2110.1406743223@sss.pgh.pa.us>
References: <29D4F821F6A75941B4FE0C0AAAE0E7809CA1E6D6@ex01.code42.local> <2110.1406743223@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox