Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1owWEO-0004TA-Ba for pgsql-general@arkaria.postgresql.org; Sat, 19 Nov 2022 22:22:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1owWEM-000261-9Y for pgsql-general@arkaria.postgresql.org; Sat, 19 Nov 2022 22:22:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1owWEL-00025i-VL for pgsql-general@lists.postgresql.org; Sat, 19 Nov 2022 22:22:33 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1owWEG-0002Yl-1f for pgsql-general@postgresql.org; Sat, 19 Nov 2022 22:22:30 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2AJMMPkx2655246; Sat, 19 Nov 2022 17:22:25 -0500 From: Tom Lane To: Ken Tanzer cc: Alban Hertroys , Alejandro Baeza Rangel , pgsql-general@postgresql.org Subject: Re: Duda sobre como imprimir un campo INTERVAL In-reply-to: References: <7DD6FF46-1F92-4E6B-A288-45A26021EE75@gmail.com> Comments: In-reply-to Ken Tanzer message dated "Sat, 19 Nov 2022 12:37:43 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2655244.1668896545.1@sss.pgh.pa.us> Date: Sat, 19 Nov 2022 17:22:25 -0500 Message-ID: <2655245.1668896545@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ken Tanzer 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