Re: Inconsistent results in timestamp/interval comparison - Mailing list pgsql-general

From Francisco Olarte
Subject Re: Inconsistent results in timestamp/interval comparison
Date
Msg-id CA+bJJbwvj1_e+2fVPf-kD4Q1pfsMqYUP47++p2OzVGmVx_zy0Q@mail.gmail.com
Whole thread Raw
In response to Inconsistent results in timestamp/interval comparison  (albrecht.dress@posteo.de)
Responses Re: Inconsistent results in timestamp/interval comparison  (Alban Hertroys <haramrae@gmail.com>)
Re: Inconsistent results in timestamp/interval comparison  (albrecht.dress@posteo.de)
List pgsql-general
On Mon, 4 Mar 2024 at 13:10, <albrecht.dress@posteo.de> wrote:
> According to the documentation, Table 9.31, IMHO both comparisons should
> produce the same results, as

> timestamp - timestamp → interval
> timestamp + interval → timestamp
Your problem may be due to interval comparison.

Intervals are composed of months, days and seconds, as not every month
has 30 days and not every day has 86400 seconds, so to compare them
you have to normalize them somehow, which can lead to bizarre results.

=> select '2 years'::interval > '1 year 362 days'::interval;
 ?column?
----------
 f
(1 row)

=> select '2 years'::interval > '1 year 359 days'::interval;
 ?column?
----------
 t
(1 row)

=> select '2 years'::interval > '1 year 360 days'::interval;
 ?column?
----------
 f
(1 row)

=> select '2 years'::interval = '1 year 360 days'::interval;
 ?column?
----------
 t
(1 row)

If you want to do point in time arithmetic, you will be better of by
extracting epoch from your timestamps and substracting that. Intervals
are more for calendar arithmetic on the type "set me a date two
months, three days and four hours from the last".

Francisco Olarte.



pgsql-general by date:

Previous
From: Michał Kłeczek
Date:
Subject: Re: postgres_fdw aggregate pushdown for group by with expressions
Next
From: Alban Hertroys
Date:
Subject: Re: Inconsistent results in timestamp/interval comparison