Rounding error on extract(epoch ..)

From: Moshe Jacobson <moshe(at)neadwerx(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: Rounding error on extract(epoch ..)
Date: 2014-01-24 16:40:59
Message-ID: CAJ4CxLkA6Y+SEL-z+nmeZafXZEGWOJSRUt9r48SjD8yvd-+Apw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

It seems that extract(epoch ..) does not preserve the entire precision of
the passed-in timestamp. In the following example, it rounds to the nearest
0.00001:

> select extract(epoch from '2014-01-21 16:10:30.403777'::timestamp);

date_part
------------------
1390320630.40378
(1 row)

Just to be sure, I convert this value back to timestamp and it is still
rounded (and incorrect):

> select to_timestamp(1390320630.40378);

to_timestamp
------------------------------
2014-01-21 11:10:30.40378-05
(1 row)

Oddly, when the result of extract(epoch) is passed directly back into
to_timestamp, the result is correct!

> select to_timestamp(extract(epoch from '2014-01-21 16:10:30.403777'::timestamp));

to_timestamp
-------------------------------
2014-01-21 11:10:30.403777-05
(1 row)

Moshe Jacobson
Manager of Systems Engineering, Nead Werx Inc. <http://www.neadwerx.com>
2323 Cumberland Parkway · Suite 201 · Atlanta, GA 30339

“Quality is not an act, it is a habit.” — Aristotle

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2014-01-24 16:52:53 Re: Rounding error on extract(epoch ..)
Previous Message Bruce Momjian 2014-01-24 16:34:09 Re: BUG #8218: Error when querying an JSON data, 9.3beta