Range hashing can disagree with equality for a custom subtype operator class
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
PostgreSQL server version: PostgreSQL 19.0, REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10
psql version: PostgreSQL 19.0
Platform: Ubuntu 24.04 LTS, Linux x86_64, kernel 7.0.0-38-generic, glibc 2.39; GCC 13.3.0
Build options: --without-readline --without-zlib --without-icu --without-llvm
Cluster setup: Run as the postgres operating-system user. Set PGDATA to a new empty data directory and SOCKET to a local socket directory, then run:
export PGDATA=/path/to/pgdata
export SOCKET=/path/to/socket
mkdir -p "$SOCKET"
initdb -D "$PGDATA" --no-locale -E UTF8 --auth-local=trust --auth-host=reject
Start the server with:
pg_ctl -D "$PGDATA" -l "$PGDATA/server.log" -o "-c listen_addresses= -c unix_socket_directories=$SOCKET -c port=55488 -c max_connections=10 -c shared_buffers=32MB -c wal_level=logical -c max_replication_slots=10 -c max_wal_senders=10" -w start
Connect to the postgres database with PGHOST=$SOCKET PGPORT=55488 psql -X -U postgres -d postgres. The replication-related settings do not affect this case.
Summary
For a range type whose subtype uses a custom B-tree operator class, range equality can consider two values equal while hash_range and hash_range_extended return different hashes. HashAggregate then groups the values separately, and a hash-index equality scan can omit a row that satisfies the equality operator.
Reproduction
Run the following in psql. All objects are created inside a transaction and removed by ROLLBACK.
BEGIN;
CREATE SCHEMA range_hash_bug;
CREATE FUNCTION range_hash_bug.abs_cmp(float8, float8)
RETURNS integer LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
AS $$
SELECT CASE WHEN abs($1) < abs($2) THEN -1
WHEN abs($1) > abs($2) THEN 1
ELSE 0 END
$$;
CREATE FUNCTION range_hash_bug.abs_lt(float8, float8)
RETURNS boolean LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
AS $$ SELECT abs($1) < abs($2) $$;
CREATE FUNCTION range_hash_bug.abs_le(float8, float8)
RETURNS boolean LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
AS $$ SELECT abs($1) <= abs($2) $$;
CREATE FUNCTION range_hash_bug.abs_eq(float8, float8)
RETURNS boolean LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
AS $$ SELECT abs($1) = abs($2) $$;
CREATE FUNCTION range_hash_bug.abs_ge(float8, float8)
RETURNS boolean LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
AS $$ SELECT abs($1) >= abs($2) $$;
CREATE FUNCTION range_hash_bug.abs_gt(float8, float8)
RETURNS boolean LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
AS $$ SELECT abs($1) > abs($2) $$;
CREATE OPERATOR range_hash_bug.<
(LEFTARG = float8, RIGHTARG = float8, FUNCTION = range_hash_bug.abs_lt);
CREATE OPERATOR range_hash_bug.<=
(LEFTARG = float8, RIGHTARG = float8, FUNCTION = range_hash_bug.abs_le);
CREATE OPERATOR range_hash_bug.=
(LEFTARG = float8, RIGHTARG = float8, FUNCTION = range_hash_bug.abs_eq);
CREATE OPERATOR range_hash_bug.>=
(LEFTARG = float8, RIGHTARG = float8, FUNCTION = range_hash_bug.abs_ge);
CREATE OPERATOR range_hash_bug.>
(LEFTARG = float8, RIGHTARG = float8, FUNCTION = range_hash_bug.abs_gt);
CREATE OPERATOR CLASS range_hash_bug.float8_abs_ops
FOR TYPE float8 USING btree AS
OPERATOR 1 range_hash_bug.<,
OPERATOR 2 range_hash_bug.<=,
OPERATOR 3 range_hash_bug.=,
OPERATOR 4 range_hash_bug.>=,
OPERATOR 5 range_hash_bug.>,
FUNCTION 1 range_hash_bug.abs_cmp(float8, float8);
CREATE TYPE range_hash_bug.absrange AS RANGE
(subtype = float8, subtype_opclass = range_hash_bug.float8_abs_ops);
SELECT '[-1,2]'::range_hash_bug.absrange = '[1,2]'::range_hash_bug.absrange
AS range_equal,
hash_range('[-1,2]'::range_hash_bug.absrange)
= hash_range('[1,2]'::range_hash_bug.absrange) AS hashes_equal,
hash_range_extended('[-1,2]'::range_hash_bug.absrange, 0)
= hash_range_extended('[1,2]'::range_hash_bug.absrange, 0)
AS extended_hashes_equal;
CREATE TABLE range_hash_bug.probe (id integer PRIMARY KEY,
r range_hash_bug.absrange NOT NULL);
CREATE INDEX probe_hash_idx ON range_hash_bug.probe USING hash (r);
INSERT INTO range_hash_bug.probe VALUES
(1, '[-1,2]'::range_hash_bug.absrange),
(2, '[1,2]'::range_hash_bug.absrange);
SET enable_hashagg = on;
SET enable_sort = off;
EXPLAIN (COSTS OFF)
SELECT r FROM range_hash_bug.probe GROUP BY r;
SELECT count(*) AS hashagg_groups
FROM (SELECT r FROM range_hash_bug.probe GROUP BY r) AS grouped;
SET enable_hashagg = off;
SET enable_sort = on;
EXPLAIN (COSTS OFF)
SELECT r FROM range_hash_bug.probe GROUP BY r;
SELECT count(*) AS sortagg_groups
FROM (SELECT r FROM range_hash_bug.probe GROUP BY r) AS grouped;
SET enable_seqscan = on;
SET enable_indexscan = off;
SET enable_bitmapscan = off;
SELECT array_agg(id ORDER BY id) AS sequential_ids
FROM range_hash_bug.probe
WHERE r = '[1,2]'::range_hash_bug.absrange;
SET enable_seqscan = off;
SET enable_indexscan = on;
SET enable_bitmapscan = on;
EXPLAIN (COSTS OFF)
SELECT id FROM range_hash_bug.probe
WHERE r = '[1,2]'::range_hash_bug.absrange;
SELECT array_agg(id ORDER BY id) AS hash_index_ids
FROM range_hash_bug.probe
WHERE r = '[1,2]'::range_hash_bug.absrange;
ROLLBACK;
Observed result
The output is range_equal=t, hashes_equal=f, extended_hashes_equal=f, hashagg_groups=2, sortagg_groups=1, sequential_ids={1,2}, and hash_index_ids={2}. EXPLAIN reports a Bitmap Index Scan on probe_hash_idx for the final query. The exact numeric hash values vary by build and are not required to reproduce the mismatch.
Expected result
Values that compare equal must be compatible with the hash function used for their equality operator. The PostgreSQL documentation says range types can select a subtype B-tree operator class through CREATE TYPE and that hash support functions are used by hash indexes and related operations:
https://www.postgresql.org/docs/19/sql-createtype.html
https://www.postgresql.org/docs/19/rangetypes.html
https://www.postgresql.org/docs/19/xindex.html
Related reports
The November 2026 pgsql-bugs report [“PG 18 JIT regression: int4range hash breaks multi-key GROUP BY”](/messages/by-id/CAEGP1v12rRmZo1h3KZ+ZCS66A9xLoSOuLAyBXu92nd97E3jJPw@mail.gmail.com) concerns JIT/non-JIT hash-combination width handling for a range key followed by another grouping key. It does not involve equal ranges receiving different hashes under a custom subtype operator class. A 2021 [pgsql-hackers discussion about an abandoned empty-range-bounds patch](/messages/by-id/1C23E6FC-9EDD-43DC-8C00-A753236F5F5D@enterprisedb.com) also discussed hash/equality compatibility, but did not report this current custom-opclass behavior.
The behavior reproduced on PostgreSQL 18.6 and PostgreSQL 19.0. PostgreSQL 18.6 was built from REL_18_6 commit 724edf9bde9d356724ad384a2e196edc3c9f80f7; PostgreSQL 19.0 was built from the commit listed above. The PostgreSQL 19.0 result was repeated in two independent psql sessions.
Relevant source location: range_eq_internal() compares bounds using the range type's cached subtype comparator, while hash_range() and hash_range_extended() obtain the subtype's default hash procedures. Source at the tested PostgreSQL 19 commit: https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/utils/adt/rangetypes.c#L581-L630 and https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/utils/adt/rangetypes.c#L1566-L1690