Skip to content

Commit

Permalink
randgen: generate single-column indexes more often
Browse files Browse the repository at this point in the history
This commit makes `randgen` more likely to generate single-column
indexes. It is motivated by the bug #111963, which surprisingly lived on
the master branch for sixth months without being detected. It's not
entirely clear why TLP or other randomized tests did not catch the bug,
which has such a simple reproduction.

One theory is that indexes tend to be multi-column and constrained scans
on multi-column inverted indexes are not commonly planned for randomly
generated queries because the set of requirements to generate the scan
are very specific: the query must hold each prefix column constant, e.g.
`a=1 AND b=2 AND j='5'::JSON`. The likelihood of randomly generating
such an expression may be so low that the bug was not caught.

By making 50% of indexes single-column, this bug may have been more
likely to be caught because only the inverted index column needs to be
constrained by an equality filter.

Release note: None
  • Loading branch information
mgartner committed Oct 20, 2023
1 parent fc1ca9e commit 92e57af
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pkg/sql/randgen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,28 @@ func randIndexTableDefFromCols(
cpy := make([]*tree.ColumnTableDef, len(columnTableDefs))
copy(cpy, columnTableDefs)
rng.Shuffle(len(cpy), func(i, j int) { cpy[i], cpy[j] = cpy[j], cpy[i] })
nCols := rng.Intn(len(cpy)) + 1

// Determine the number of indexed columns.
var nCols int
r := rng.Intn(100)
switch {
case r < 50:
// Create a single-column index 40% of the time. Single-column indexes
// are more likely then multi-column indexes to be used in query plans
// for randomly generated queries, so there is some benefit to
// guaranteeing that they are generated often.
nCols = 1
case r < 75:
nCols = 2
case r < 90:
nCols = 3
default:
nCols = rng.Intn(len(cpy)) + 1
}
if nCols > len(cpy) {
// nCols cannot be greater than the length of columnTableDefs.
nCols = len(cpy)
}

cols := cpy[:nCols]

Expand Down

0 comments on commit 92e57af

Please sign in to comment.