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 cockroachdb#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 10% 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 18, 2023
1 parent 37502b3 commit aa20ef3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/sql/randgen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,13 @@ func randIndexTableDefFromCols(
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
if rng.Intn(10) == 0 {
// Create a single-column index 10% 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.
nCols = 1
}

cols := cpy[:nCols]

Expand Down

0 comments on commit aa20ef3

Please sign in to comment.