diff --git a/tsdb/index/postings.go b/tsdb/index/postings.go index 3256ca55b..ad76f647f 100644 --- a/tsdb/index/postings.go +++ b/tsdb/index/postings.go @@ -855,17 +855,22 @@ func (it *ListPostings) Next() bool { func (it *ListPostings) Seek(x storage.SeriesRef) bool { // If the current value satisfies, then return. - if it.list[it.i] >= x { + if it.i >= 0 && it.list[it.i] >= x { return true } + start := it.i + if start < 0 { + start = 0 + } + // Do binary search between current position and end. - l := it.list[it.i:] + l := it.list[start:] i := sort.Search(len(l), func(i int) bool { return l[i] >= x }) if i < len(l) { - it.i += i + it.i = start + i return true } @@ -921,7 +926,12 @@ func (it *bigEndianPostings) Seek(x storage.SeriesRef) bool { return true } - l := it.list[it.i:] + start := it.i + if start < 0 { + start = 0 + } + + l := it.list[start:] num := len(l) / 4 // Do binary search between current position and end. i := sort.Search(num, func(i int) bool { @@ -930,7 +940,7 @@ func (it *bigEndianPostings) Seek(x storage.SeriesRef) bool { if i < num { j := i * 4 it.cur = binary.BigEndian.Uint32(l[j:]) - it.i += j + it.i = start + j return true }