Skip to content

Commit b1752c3

Browse files
committed
Fix typcache's failure to treat ranges as container types.
Like the similar logic for arrays and records, it's necessary to examine the range's subtype to decide whether the range type can support hashing. We can omit checking the subtype for btree-defined operations, though, since range subtypes are required to have those operations. (Possibly that simplification for btree cases led us to overlook that it does not apply for hash cases.) This is only an issue if the subtype lacks hash support, which is not true of any built-in range type, but it's easy to demonstrate a problem with a range type over, eg, money: you can get a "could not identify a hash function" failure when the planner is misled into thinking that hash join or aggregation would work. This was born broken, so back-patch to all supported branches.
1 parent 2ac5988 commit b1752c3

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

src/backend/utils/cache/typcache.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ static void cache_array_element_properties(TypeCacheEntry *typentry);
168168
static bool record_fields_have_equality(TypeCacheEntry *typentry);
169169
static bool record_fields_have_compare(TypeCacheEntry *typentry);
170170
static void cache_record_field_properties(TypeCacheEntry *typentry);
171+
static bool range_element_has_hashing(TypeCacheEntry *typentry);
172+
static void cache_range_element_properties(TypeCacheEntry *typentry);
171173
static void TypeCacheRelCallback(Datum arg, Oid relid);
172174
static void TypeCacheOpcCallback(Datum arg, int cacheid, uint32 hashvalue);
173175
static void TypeCacheConstrCallback(Datum arg, int cacheid, uint32 hashvalue);
@@ -481,6 +483,13 @@ lookup_type_cache(Oid type_id, int flags)
481483
!array_element_has_hashing(typentry))
482484
hash_proc = InvalidOid;
483485

486+
/*
487+
* Likewise for hash_range.
488+
*/
489+
if (hash_proc == F_HASH_RANGE &&
490+
!range_element_has_hashing(typentry))
491+
hash_proc = InvalidOid;
492+
484493
/* Force update of hash_proc_finfo only if we're changing state */
485494
if (typentry->hash_proc != hash_proc)
486495
typentry->hash_proc_finfo.fn_oid = InvalidOid;
@@ -1113,6 +1122,10 @@ cache_array_element_properties(TypeCacheEntry *typentry)
11131122
typentry->flags |= TCFLAGS_CHECKED_ELEM_PROPERTIES;
11141123
}
11151124

1125+
/*
1126+
* Likewise, some helper functions for composite types.
1127+
*/
1128+
11161129
static bool
11171130
record_fields_have_equality(TypeCacheEntry *typentry)
11181131
{
@@ -1183,6 +1196,43 @@ cache_record_field_properties(TypeCacheEntry *typentry)
11831196
typentry->flags |= TCFLAGS_CHECKED_FIELD_PROPERTIES;
11841197
}
11851198

1199+
/*
1200+
* Likewise, some helper functions for range types.
1201+
*
1202+
* We can borrow the flag bits for array element properties to use for range
1203+
* element properties, since those flag bits otherwise have no use in a
1204+
* range type's typcache entry.
1205+
*/
1206+
1207+
static bool
1208+
range_element_has_hashing(TypeCacheEntry *typentry)
1209+
{
1210+
if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
1211+
cache_range_element_properties(typentry);
1212+
return (typentry->flags & TCFLAGS_HAVE_ELEM_HASHING) != 0;
1213+
}
1214+
1215+
static void
1216+
cache_range_element_properties(TypeCacheEntry *typentry)
1217+
{
1218+
/* load up subtype link if we didn't already */
1219+
if (typentry->rngelemtype == NULL &&
1220+
typentry->typtype == TYPTYPE_RANGE)
1221+
load_rangetype_info(typentry);
1222+
1223+
if (typentry->rngelemtype != NULL)
1224+
{
1225+
TypeCacheEntry *elementry;
1226+
1227+
/* might need to calculate subtype's hash function properties */
1228+
elementry = lookup_type_cache(typentry->rngelemtype->type_id,
1229+
TYPECACHE_HASH_PROC);
1230+
if (OidIsValid(elementry->hash_proc))
1231+
typentry->flags |= TCFLAGS_HAVE_ELEM_HASHING;
1232+
}
1233+
typentry->flags |= TCFLAGS_CHECKED_ELEM_PROPERTIES;
1234+
}
1235+
11861236

11871237
/*
11881238
* lookup_rowtype_tupdesc_internal --- internal routine to lookup a rowtype

src/test/regress/expected/rangetypes.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,18 @@ select *, row_to_json(upper(t)) as u from
13541354
drop type two_ints cascade;
13551355
NOTICE: drop cascades to type two_ints_range
13561356
--
1357+
-- Check behavior when subtype lacks a hash function
1358+
--
1359+
create type cashrange as range (subtype = money);
1360+
set enable_sort = off; -- try to make it pick a hash setop implementation
1361+
select '(2,5)'::cashrange except select '(5,6)'::cashrange;
1362+
cashrange
1363+
---------------
1364+
($2.00,$5.00)
1365+
(1 row)
1366+
1367+
reset enable_sort;
1368+
--
13571369
-- OUT/INOUT/TABLE functions
13581370
--
13591371
create function outparam_succeed(i anyrange, out r anyrange, out t text)

src/test/regress/sql/rangetypes.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,18 @@ select *, row_to_json(upper(t)) as u from
461461

462462
drop type two_ints cascade;
463463

464+
--
465+
-- Check behavior when subtype lacks a hash function
466+
--
467+
468+
create type cashrange as range (subtype = money);
469+
470+
set enable_sort = off; -- try to make it pick a hash setop implementation
471+
472+
select '(2,5)'::cashrange except select '(5,6)'::cashrange;
473+
474+
reset enable_sort;
475+
464476
--
465477
-- OUT/INOUT/TABLE functions
466478
--

0 commit comments

Comments
 (0)