Re: Duda sobre como imprimir un campo INTERVAL

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com>
Cc: Alban Hertroys <haramrae(at)gmail(dot)com>, Alejandro Baeza Rangel <jlabaezarangel(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Duda sobre como imprimir un campo INTERVAL
Date: 2022-11-19 22:22:25
Message-ID: 2655245.1668896545@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Ken Tanzer <ken(dot)tanzer(at)gmail(dot)com> writes:
> Thanks. I could understand that they're not identical. But then what's
> going on where Postgres evaluates them as equal? (i1=i2 above.) Are the
> two intervals getting cast or converted to something else before they are
> compared, with whatever makes them non-identical getting lost in the
> conversion?

Hmm ... the code is pretty clear about what it thinks it's doing,
but I'm not sure if this info exists anywhere in the user-facing docs:

* Interval comparison is based on converting interval values to a linear
* representation expressed in the units of the time field (microseconds,
* in the case of integer timestamps) with days assumed to be always 24 hours
* and months assumed to be always 30 days.

So once '1 day 2 hours' and '26 hours' are flattened into this linear
representation, they are indeed "equal". There's precedent elsewhere
for values that are "equal" but not identical, so it's not a bug,
even if it's not the semantics you'd want for some particular use-case.

I think the main factor behind having done it this way is that we need
a linear sort order if we want to support btree indexes or ORDER BY
on intervals.

You can use justify_hours() to get from '26 hours' to '1 day 2 hours'.
I'm not sure if there's a compact way to go the other direction,
though you could always use extract() to get the components and
sum them up.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message vignesh C 2022-11-20 03:59:47 Re: Support logical replication of DDLs
Previous Message Ken Tanzer 2022-11-19 20:37:43 Re: Duda sobre como imprimir un campo INTERVAL