Re: Since '2001-09-09 01:46:40'::timestamp microseconds are lost when extracting epoch

From: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
To: Petr Fedorov <petr(dot)fedorov(at)phystech(dot)edu>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: Since '2001-09-09 01:46:40'::timestamp microseconds are lost when extracting epoch
Date: 2019-12-02 22:52:31
Message-ID: CA+hUKGK+K8NyS57tU841m1tSF6ha3W0udN2dw_Hbc=D_0OPbmA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

On Tue, Dec 3, 2019 at 12:08 AM Petr Fedorov <petr(dot)fedorov(at)phystech(dot)edu> wrote:
> It appears that extract epoch returns double precision, not float8. And
> the program below seems to be demonstrating that there are enough
> 'floating-point numbers' as defined by IEEE-754 to represent
> 1000000000.000021 precisely enough:

Double precision and float8 are different names for the same type in PostgreSQL.

> I'm not an expert in floating point math but hopefully it means that no
> type change is required - double precision can handle it.

Me neither, but the SQL standard requires us to use an exact numeric
type, so it's wrong on that level by definition.

It's also wrong because binary floating point numbers can't represent
0.000001 (one microsecond represented as seconds) exactly, and that's
our unit of counting for timestamps. You can get pretty far by
thinking of the decimal number you see on the screen as the true
number and the double as a fuzzy internal storage or transport that
does the job just fine due to the round trip conversion guarantee
provided by DBL_DIG, but the double is still going to have the wrong
value in some cases. As soon as you start doing any arithmetic or
comparisons with the double directly, interesting things can start to
happen to make the error visible and break things; for example
0.1::float8 + 0.2::float8 = 0.3::float8 is false.

> And since it works correctly on v12 for this particular date may be all
> what is needed it to verify that it works for the other dates too! For
> example what was changed in v12 (comparing to 11.6 I use) so extract
> epoch works correctly?

PostgreSQL 12 adopted a different algorithm[1] for converting float8
to text that can affect how many digits are shown, as Tom explained.
The manual has some notes about it[2].

[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=02ddd499322ab6f2f0d58692955dc9633c2150fc
[2] https://www.postgresql.org/docs/12/datatype-numeric.html#DATATYPE-FLOAT

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Thomas Munro 2019-12-03 09:01:57 Re: BUG #15548: Unaccent does not remove combining diacritical characters
Previous Message Grigory Smolkin 2019-12-02 17:49:04 Re: logical replication: could not create file "state.tmp": File exists

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2019-12-02 22:54:11 Re: surprisingly expensive join planning query
Previous Message Tomas Vondra 2019-12-02 22:39:33 Re: surprisingly expensive join planning query