Skip to content

Commit 849172f

Browse files
committed
btree_gin: Fix calculation of leftmost interval value.
Formerly, the value computed by leftmostvalue_interval() was a long way short of the minimum possible interval value. As a result, an index scan on a GIN index on an interval column with < or <= operators would miss large negative interval values. Fix by setting all fields of the leftmost interval to their minimum values, ensuring that the result is less than any other possible interval. Since this only affects index searches, no index rebuild is necessary. Back-patch to all supported branches. Dean Rasheed, reviewed by Heikki Linnakangas. Discussion: https://postgr.es/m/CAEZATCV80%2BgOfF8ehNUUfaKBZgZMDfCfL-g1HhWGb6kC3rpDfw%40mail.gmail.com
1 parent 237f876 commit 849172f

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

contrib/btree_gin/btree_gin.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ leftmostvalue_interval(void)
306306
{
307307
Interval *v = palloc(sizeof(Interval));
308308

309-
v->time = DT_NOBEGIN;
310-
v->day = 0;
311-
v->month = 0;
309+
v->time = PG_INT64_MIN;
310+
v->day = PG_INT32_MIN;
311+
v->month = PG_INT32_MIN;
312312
return IntervalPGetDatum(v);
313313
}
314314

contrib/btree_gin/expected/interval.out

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,34 @@ CREATE TABLE test_interval (
33
i interval
44
);
55
INSERT INTO test_interval VALUES
6+
( '-178000000 years' ),
67
( '03:55:08' ),
78
( '04:55:08' ),
89
( '05:55:08' ),
910
( '08:55:08' ),
1011
( '09:55:08' ),
11-
( '10:55:08' )
12+
( '10:55:08' ),
13+
( '178000000 years' )
1214
;
1315
CREATE INDEX idx_interval ON test_interval USING gin (i);
1416
SELECT * FROM test_interval WHERE i<'08:55:08'::interval ORDER BY i;
1517
i
1618
--------------------------
19+
@ 178000000 years ago
1720
@ 3 hours 55 mins 8 secs
1821
@ 4 hours 55 mins 8 secs
1922
@ 5 hours 55 mins 8 secs
20-
(3 rows)
23+
(4 rows)
2124

2225
SELECT * FROM test_interval WHERE i<='08:55:08'::interval ORDER BY i;
2326
i
2427
--------------------------
28+
@ 178000000 years ago
2529
@ 3 hours 55 mins 8 secs
2630
@ 4 hours 55 mins 8 secs
2731
@ 5 hours 55 mins 8 secs
2832
@ 8 hours 55 mins 8 secs
29-
(4 rows)
33+
(5 rows)
3034

3135
SELECT * FROM test_interval WHERE i='08:55:08'::interval ORDER BY i;
3236
i
@@ -40,12 +44,14 @@ SELECT * FROM test_interval WHERE i>='08:55:08'::interval ORDER BY i;
4044
@ 8 hours 55 mins 8 secs
4145
@ 9 hours 55 mins 8 secs
4246
@ 10 hours 55 mins 8 secs
43-
(3 rows)
47+
@ 178000000 years
48+
(4 rows)
4449

4550
SELECT * FROM test_interval WHERE i>'08:55:08'::interval ORDER BY i;
4651
i
4752
---------------------------
4853
@ 9 hours 55 mins 8 secs
4954
@ 10 hours 55 mins 8 secs
50-
(2 rows)
55+
@ 178000000 years
56+
(3 rows)
5157

contrib/btree_gin/sql/interval.sql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ CREATE TABLE test_interval (
55
);
66

77
INSERT INTO test_interval VALUES
8+
( '-178000000 years' ),
89
( '03:55:08' ),
910
( '04:55:08' ),
1011
( '05:55:08' ),
1112
( '08:55:08' ),
1213
( '09:55:08' ),
13-
( '10:55:08' )
14+
( '10:55:08' ),
15+
( '178000000 years' )
1416
;
1517

1618
CREATE INDEX idx_interval ON test_interval USING gin (i);

0 commit comments

Comments
 (0)