Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f897c47

Browse files
committed
Fix "element <@ range" cost estimation.
The statistics-based cost estimation patch for range types broke that, by incorrectly assuming that the left operand of all range oeprators is a range. That lead to a "type x is not a range type" error. Because it took so long for anyone to notice, add a regression test for that case. We still don't do proper statistics-based cost estimation for that, so you just get a default constant estimate. We should look into implementing that, but this patch at least fixes the regression. Spotted by Tom Lane, when testing query from Josh Berkus.
1 parent f8348ea commit f897c47

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/backend/utils/adt/rangetypes_selfuncs.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ rangesel(PG_FUNCTION_ARGS)
154154
}
155155
}
156156

157-
typcache = range_get_typcache(fcinfo, vardata.vartype);
158-
159157
/*
160158
* OK, there's a Var and a Const we're dealing with here. We need the
161159
* Const to be of same range type as the column, else we can't do anything
@@ -169,6 +167,8 @@ rangesel(PG_FUNCTION_ARGS)
169167
*/
170168
if (operator == OID_RANGE_CONTAINS_ELEM_OP)
171169
{
170+
typcache = range_get_typcache(fcinfo, vardata.vartype);
171+
172172
if (((Const *) other)->consttype == typcache->rngelemtype->type_id)
173173
{
174174
RangeBound lower, upper;
@@ -185,6 +185,8 @@ rangesel(PG_FUNCTION_ARGS)
185185
}
186186
else
187187
{
188+
typcache = range_get_typcache(fcinfo, ((Const *) other)->consttype);
189+
188190
if (((Const *) other)->consttype == vardata.vartype)
189191
constrange = DatumGetRangeType(((Const *) other)->constvalue);
190192
}

src/test/regress/expected/rangetypes.out

+11
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,17 @@ select count(*) from test_range_spgist where ir -|- int4range(100,500);
10431043
RESET enable_seqscan;
10441044
RESET enable_indexscan;
10451045
RESET enable_bitmapscan;
1046+
-- test elem <@ range operator
1047+
create table test_range_elem(i int4);
1048+
create index test_range_elem_idx on test_range_elem (i);
1049+
insert into test_range_elem select i from generate_series(1,100) i;
1050+
select count(*) from test_range_elem where i <@ int4range(10,50);
1051+
count
1052+
-------
1053+
40
1054+
(1 row)
1055+
1056+
drop table test_range_elem;
10461057
--
10471058
-- Btree_gist is not included by default, so to test exclusion
10481059
-- constraints with range types, use singleton int ranges for the "="

src/test/regress/sql/rangetypes.sql

+9
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ RESET enable_seqscan;
286286
RESET enable_indexscan;
287287
RESET enable_bitmapscan;
288288

289+
-- test elem <@ range operator
290+
create table test_range_elem(i int4);
291+
create index test_range_elem_idx on test_range_elem (i);
292+
insert into test_range_elem select i from generate_series(1,100) i;
293+
294+
select count(*) from test_range_elem where i <@ int4range(10,50);
295+
296+
drop table test_range_elem;
297+
289298
--
290299
-- Btree_gist is not included by default, so to test exclusion
291300
-- constraints with range types, use singleton int ranges for the "="

0 commit comments

Comments
 (0)