BUG #19965: btree_gist index on timetz returns too few rows for <, > and <>

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: aman(at)stack-auth(dot)com
Subject: BUG #19965: btree_gist index on timetz returns too few rows for <, > and <>
Date: 2026-12-01 23:20:22
Message-ID: 19965-be9751b45fab7607@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19965
Logged by: Aman Ganapathy
Email address: aman(at)stack-auth(dot)com
PostgreSQL version: 19.0
Operating system: Debian 13 (trixie), aarch64
Description:

A btree_gist index on a timetz column returns too few rows for <, > and
<> when a stored value is the same UTC time as the comparison value with
a different time zone offset, such as 09:00+00 and 10:00+01. An
exclusion constraint using <> on such a column also accepts rows that
violate it.

Setup: the official postgres:19.0 Docker image, started with

docker run -d --name pg19 -e POSTGRES_HOST_AUTH_METHOD=trust \
-e POSTGRES_INITDB_ARGS="--locale=C.UTF-8 --encoding=UTF8" -e TZ=UTC \
postgres:19.0 postgres -c log_error_verbosity=verbose

Test case, run with "psql -X" in a new, empty database:

CREATE EXTENSION btree_gist;
CREATE TABLE t (c timetz);
INSERT INTO t VALUES ('09:00+00'), ('10:00+01'), ('11:00+02');
SELECT c FROM t WHERE c <> '10:00+01';
SELECT c FROM t WHERE c < '10:00+01';
SELECT c FROM t WHERE c > '10:00+01';
CREATE INDEX ON t USING gist (c);
SET enable_seqscan = off;
SELECT c FROM t WHERE c <> '10:00+01';
SELECT c FROM t WHERE c < '10:00+01';
SELECT c FROM t WHERE c > '10:00+01';
RESET enable_seqscan;

CREATE TABLE ex (c timetz, EXCLUDE USING gist (c WITH <>));
INSERT INTO ex VALUES ('10:00+01');
INSERT INTO ex VALUES ('09:00+00');
SELECT c FROM ex;

Output (psql -X -a -q, so each statement is shown above its result):

CREATE EXTENSION btree_gist;
CREATE TABLE t (c timetz);
INSERT INTO t VALUES ('09:00+00'), ('10:00+01'), ('11:00+02');
SELECT c FROM t WHERE c <> '10:00+01';
c
-------------
09:00:00+00
11:00:00+02
(2 rows)

SELECT c FROM t WHERE c < '10:00+01';
c
-------------
11:00:00+02
(1 row)

SELECT c FROM t WHERE c > '10:00+01';
c
-------------
09:00:00+00
(1 row)

CREATE INDEX ON t USING gist (c);
SET enable_seqscan = off;
SELECT c FROM t WHERE c <> '10:00+01';
c
---
(0 rows)

SELECT c FROM t WHERE c < '10:00+01';
c
---
(0 rows)

SELECT c FROM t WHERE c > '10:00+01';
c
---
(0 rows)

RESET enable_seqscan;
CREATE TABLE ex (c timetz, EXCLUDE USING gist (c WITH <>));
INSERT INTO ex VALUES ('10:00+01');
INSERT INTO ex VALUES ('09:00+00');
SELECT c FROM ex;
c
-------------
10:00:00+01
09:00:00+00
(2 rows)

Expected: the same rows with the index as without it, and the second
INSERT into ex should fail with "conflicting key value violates
exclusion constraint", since '09:00+00'::timetz <> '10:00+01'::timetz
is true. The btree_gist documentation lists timetz among the supported
types and <> among the supported operators, and its example is an
exclusion constraint using <>.

The same output on master at b4cc5e924b7 (2026-11-25), built with
--enable-debug --with-icu --enable-cassert.

Environment: PostgreSQL 19.0 (Debian 19.0-2.pgdg13+1) on
aarch64-unknown-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0,
64-bit; psql 19.0 (Debian 19.0-2.pgdg13+1). Debian 13 (trixie) in the
image, Linux 7.0.14 aarch64, glibc 2.41, 14 CPUs, 16 GB RAM, running in
an OrbStack VM on macOS 15.7.9 (Apple Silicon).

I've been working on a patch with a regression test.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-12-01 23:50:43 BUG #19966: FULL JOIN testing a NOT NULL column with IS NULL fails in 19, works in 18
Previous Message Zsolt Parragi 2026-12-01 21:52:33 Re: BUG #19928: pg_upgrade fails on v18 schema with NOT NULL ... NO INHERIT and inheritance