pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: albrecht.dress@posteo.de
To: Francisco Olarte <folarte@peoplecall.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Inconsistent results in timestamp/interval comparison
Date: Mon, 04 Mar 2024 13:06:22 +0000
Message-ID: <4faaea681e2e210bbfb7de92fc8cbb35@posteo.de> (raw)
In-Reply-To: <CA+bJJbwvj1_e+2fVPf-kD4Q1pfsMqYUP47++p2OzVGmVx_zy0Q@mail.gmail.com>
References: <839f7314609c96c5123549a28d493534@posteo.de>
	<CA+bJJbwvj1_e+2fVPf-kD4Q1pfsMqYUP47++p2OzVGmVx_zy0Q@mail.gmail.com>

Am 04.03.2024 13:45 schrieb Francisco Olarte:
> 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.

Ah, I see, thanks for the explanation.  I had the (apparently wrong) 
impression that Postgres _internally_ always uses numerical values (i.e. 
the equivalent of EXTRACT(EPOCH …)) for such calculations.  My bad…

However, a clarification in the docs might be helpful!

> If you want to do point in time arithmetic, you will be better of by
> extracting epoch from your timestamps and substracting that.

I can confirm that using the query

select now(), t1, extract(epoch from now() - t1) >= extract (epoch from 
'2 years'::interval), now() >= (t1 + '2 years'::interval) from testtab;

produces consistent results.

Thanks a lot for your help,
Albrecht.





view thread (5+ messages)  latest in thread

Message-ID: <4faaea681e2e210bbfb7de92fc8cbb35@posteo.de>
Permalink:  ../4faaea681e2e210bbfb7de92fc8cbb35@posteo.de/
Also on:    postgresql.org/message-id/4faaea681e2e210bbfb7de92fc8cbb35@posteo.de

 · 

reply

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-general@postgresql.org
  Cc: albrecht.dress@posteo.de, folarte@peoplecall.com
  Subject: Re: Inconsistent results in timestamp/interval comparison
  In-Reply-To: <4faaea681e2e210bbfb7de92fc8cbb35@posteo.de>
  References: <839f7314609c96c5123549a28d493534@posteo.de> <CA+bJJbwvj1_e+2fVPf-kD4Q1pfsMqYUP47++p2OzVGmVx_zy0Q@mail.gmail.com> <4faaea681e2e210bbfb7de92fc8cbb35@posteo.de>

* 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