Skip to content

Commit 7c70a12

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 06b2a73 commit 7c70a12

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
@@ -131,6 +131,8 @@ static void cache_array_element_properties(TypeCacheEntry *typentry);
131131
static bool record_fields_have_equality(TypeCacheEntry *typentry);
132132
static bool record_fields_have_compare(TypeCacheEntry *typentry);
133133
static void cache_record_field_properties(TypeCacheEntry *typentry);
134+
static bool range_element_has_hashing(TypeCacheEntry *typentry);
135+
static void cache_range_element_properties(TypeCacheEntry *typentry);
134136
static void TypeCacheRelCallback(Datum arg, Oid relid);
135137
static void load_enum_cache_data(TypeCacheEntry *tcache);
136138
static EnumItem *find_enumitem(TypeCacheEnumData *enumdata, Oid arg);
@@ -407,6 +409,13 @@ lookup_type_cache(Oid type_id, int flags)
407409
!array_element_has_hashing(typentry))
408410
hash_proc = InvalidOid;
409411

412+
/*
413+
* Likewise for hash_range.
414+
*/
415+
if (hash_proc == F_HASH_RANGE &&
416+
!range_element_has_hashing(typentry))
417+
hash_proc = InvalidOid;
418+
410419
typentry->hash_proc = hash_proc;
411420
}
412421

@@ -610,6 +619,10 @@ cache_array_element_properties(TypeCacheEntry *typentry)
610619
typentry->flags |= TCFLAGS_CHECKED_ELEM_PROPERTIES;
611620
}
612621

622+
/*
623+
* Likewise, some helper functions for composite types.
624+
*/
625+
613626
static bool
614627
record_fields_have_equality(TypeCacheEntry *typentry)
615628
{
@@ -680,6 +693,43 @@ cache_record_field_properties(TypeCacheEntry *typentry)
680693
typentry->flags |= TCFLAGS_CHECKED_FIELD_PROPERTIES;
681694
}
682695

696+
/*
697+
* Likewise, some helper functions for range types.
698+
*
699+
* We can borrow the flag bits for array element properties to use for range
700+
* element properties, since those flag bits otherwise have no use in a
701+
* range type's typcache entry.
702+
*/
703+
704+
static bool
705+
range_element_has_hashing(TypeCacheEntry *typentry)
706+
{
707+
if (!(typentry->flags & TCFLAGS_CHECKED_ELEM_PROPERTIES))
708+
cache_range_element_properties(typentry);
709+
return (typentry->flags & TCFLAGS_HAVE_ELEM_HASHING) != 0;
710+
}
711+
712+
static void
713+
cache_range_element_properties(TypeCacheEntry *typentry)
714+
{
715+
/* load up subtype link if we didn't already */
716+
if (typentry->rngelemtype == NULL &&
717+
typentry->typtype == TYPTYPE_RANGE)
718+
load_rangetype_info(typentry);
719+
720+
if (typentry->rngelemtype != NULL)
721+
{
722+
TypeCacheEntry *elementry;
723+
724+
/* might need to calculate subtype's hash function properties */
725+
elementry = lookup_type_cache(typentry->rngelemtype->type_id,
726+
TYPECACHE_HASH_PROC);
727+
if (OidIsValid(elementry->hash_proc))
728+
typentry->flags |= TCFLAGS_HAVE_ELEM_HASHING;
729+
}
730+
typentry->flags |= TCFLAGS_CHECKED_ELEM_PROPERTIES;
731+
}
732+
683733

684734
/*
685735
* 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
@@ -1299,6 +1299,18 @@ select array[1,3] <@ arrayrange(array[1,2], array[2,1]);
12991299
t
13001300
(1 row)
13011301

1302+
--
1303+
-- Check behavior when subtype lacks a hash function
1304+
--
1305+
create type cashrange as range (subtype = money);
1306+
set enable_sort = off; -- try to make it pick a hash setop implementation
1307+
select '(2,5)'::cashrange except select '(5,6)'::cashrange;
1308+
cashrange
1309+
---------------
1310+
($2.00,$5.00)
1311+
(1 row)
1312+
1313+
reset enable_sort;
13021314
--
13031315
-- OUT/INOUT/TABLE functions
13041316
--

src/test/regress/sql/rangetypes.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,18 @@ select arrayrange(ARRAY[2,1], ARRAY[1,2]); -- fail
438438
select array[1,1] <@ arrayrange(array[1,2], array[2,1]);
439439
select array[1,3] <@ arrayrange(array[1,2], array[2,1]);
440440

441+
--
442+
-- Check behavior when subtype lacks a hash function
443+
--
444+
445+
create type cashrange as range (subtype = money);
446+
447+
set enable_sort = off; -- try to make it pick a hash setop implementation
448+
449+
select '(2,5)'::cashrange except select '(5,6)'::cashrange;
450+
451+
reset enable_sort;
452+
441453
--
442454
-- OUT/INOUT/TABLE functions
443455
--

0 commit comments

Comments
 (0)