Repository navigation
Description
Summary
An IVFFLAT vector search with ... by rank with option 'mode=pre' and more than one distance predicate on the indexed column can return incorrect rows: the index scan uses one distance predicate as its search bound and drops the others instead of keeping them as residual filters. Which predicate is kept vs. dropped depends on the order of the filters in the plan's FilterList, so the result is sensitive to filter ordering — a plan detail that should never affect correctness.
This was surfaced by PR #25616 (a sort.Slice → slices refactor that reordered equal-cost filters); the query planning change was reverted there, but the underlying index behavior is a latent bug and is the real defect.
Reproduce
CREATE TABLE mini_vector_data (id VARCHAR(64) NOT NULL, vec vecf32(8), PRIMARY KEY (id));
CREATE INDEX idx_vec USING ivfflat ON mini_vector_data (vec) lists = 16 op_type 'vector_l2_ops';
INSERT INTO mini_vector_data VALUES
('id1','[0.12,-0.33,0.51,0.27,-0.48,0.19,0.72,-0.11]'),
('id2','[0.44,0.09,-0.31,0.62,0.18,-0.55,0.21,0.33]'),
('id3','[-0.21,0.73,-0.12,0.51,-0.44,0.09,0.14,0.82]'),
('id4','[0.51,0.25,0.09,-0.33,0.74,-0.28,0.41,0.17]'),
('id5','[0.08,-0.41,0.66,0.12,0.53,-0.15,0.38,-0.22]'),
('id6','[0.32,0.71,-0.26,0.14,-0.03,0.66,-0.41,0.19]'),
('id7','[-0.55,0.12,0.44,-0.19,0.27,0.51,0.11,-0.03]'),
('id8','[0.23,-0.14,0.82,0.39,-0.52,0.08,0.26,0.73]'),
('id9','[0.61,0.33,-0.48,0.09,0.12,0.54,-0.29,0.41]'),
('id10','[0.19,-0.22,0.33,0.77,-0.04,0.12,0.58,-0.31]');
SELECT id FROM mini_vector_data
WHERE l2_distance(vec, '[0.1,-0.2,0.3,0.4,-0.1,0.2,0.0,0.5]') < 1.1
AND l2_distance(vec, '[0.1,-0.2,0.3,0.4,-0.1,0.2,0.0,0.5]') < 1.2
ORDER BY l2_distance(vec, '[0.1,-0.2,0.3,0.4,-0.1,0.2,0.0,0.5]')
LIMIT 10 by rank with option 'mode=pre';l2 < 1.1 AND l2 < 1.2 is logically just l2 < 1.1. True distances to the query vector (brute force):
| id | l2_distance | < 1.1? |
|---|---|---|
| id8 | 0.776 | ✅ |
| id1 | 1.055 | ✅ |
| id10 | 1.072 | ✅ |
| id2 | 1.155 | ❌ |
| id3 | 1.183 | ❌ |
| id7 | 1.191 | ❌ |
| id5 | 1.198 | ❌ |
Correct result: id8, id1, id10 (3 rows).
When the plan happens to place < 1.2 ahead of < 1.1 in the scan's FilterList, the IVF mode=pre scan probes with < 1.2 and drops the < 1.1 residual, returning 7 rows (id8, id1, id10, id2, id3, id7, id5) — rows with distance up to 1.198 that violate < 1.1.
Root cause (suspected)
For mode=pre, the IVF index scan appears to select a single distance predicate as the search radius / probe bound and does not re-apply the remaining distance predicate(s) as residual filters. The choice of "which predicate becomes the bound" is driven by FilterList order (filter-cost sort in pkg/sql/plan/stats.go), so a change in that order changes which predicate is enforced.
The existing test test/distributed/cases/vector/vector_ivf_mode.sql already encodes the intended invariant in a comment:
-- second upper bound must remain as a residual filter instead of replacing it.
so this contract is known; the implementation just doesn't hold it independent of filter order.
Expected
All distance predicates on the indexed column must be enforced for mode=pre (as index bound + residual filters), so the result is correct and independent of FilterList ordering. mode=post / mode=force should be checked for the same issue.
Impact
Silent wrong results (extra rows past the intended distance threshold) for mode=pre vector queries that carry more than one distance predicate on the indexed column. No error is raised.
Activity
fix(review): use strict comparators; keep semantic filter sort on sor…
fengttt commentedon Jul 12, 2026
Related planner-side issue: #25646 — the filter-cost sort in
stats.goproduces the ill-definedFilterListorder that this bug is sensitive to (non-strictcost1 <= cost2comparator). The two are complementary: this issue is the IVFmode=prescan dropping residual distance predicates (the order should not matter); #25646 is the planner emitting an undefined tie order in the first place. Resolving this one unblocks making the filter order strict/deterministic in #25646.fix(vector): fold multiple distance predicates into the tightest rang…
fengttt commentedon Jul 12, 2026
Fixed in PR #25616 (commit edd3c9f):
getDistRangeFromFiltersnow folds all same-side distance predicates into the tightest bound, so the IVF index enforces the intersection regardless of filter order. Verified:l2 < 1.1 AND l2 < 1.2returns the correct 3 rows in both filter orders. Added a unit test (TestGetDistRangeFromFiltersKeepsTightestBound) covering both orders;vector/vector_ivf_modeBVT passes. Will auto-close on merge.perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…
fix(ivfflat): NULL and per-row prepared distance bounds
perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…
perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…
perf: replace stdlib sort.Slice with slices pkg to eliminate heap esc…