Description
In CockroachDB to , a query withORDER BY and LIMIT clauses could return incorrect results if it scanned a multi-column index containing the ORDER BY columns, and a prefix of the index columns was held fixed to two or more constant values by the query filter or schema. Examples of schema elements that could constrain the index columns include:
- A
CHECKconstraint - A computed column expression
- A
PARTITION BYclause
ORDER BY and LIMIT.
Statement
The fix has been applied to the maintenance release of CockroachDB .Mitigation
Users of CockroachDB to are encouraged to upgrade to or a later version. To determine whether your queries may be affected by this issue on v22.1.0 to v22.1.8, examine the query plans of any queries with anORDER BY and LIMIT clause or aggregation with min or max by using an EXPLAIN (OPT) statement. In particular, you should examine plans for queries that have a multi-column index containing the ORDER BY, min, or max columns and for which a prefix of the index columns are constrained to two or more constant values by one of the following:
- A query filter (e.g.,
WHERE a IN (1, 3)) - A
CHECKconstraint (e.g.,CHECK (a IN (1, 3))) - A computed column expression (e.g.,
b INT AS (a + 10) STOREDgiven column a has filterWHERE a IN (1, 3)) - A
PARTITION BYclause (e.g.,INDEX (a,...) PARTITION BY LIST (a) (PARTITION p VALUES ((1), (3))))
EXPLAIN (OPT) uses the multi-column index and shows a union-all, these queries may produce incorrect results.
To mitigate this problem for affected queries on v22.1.0 to v22.1.8, remove the multi-column index that is used by the scans that are children of the union-all.
The best mitigation, however, is to upgrade to v22.1.9 as soon as possible.
Impact
Some queries with anORDER BY and LIMIT clause or aggregation with min or max could produce incorrect results if a table in the query contained a multi-column index with the ORDER BY, min, or max columns, and a prefix of the index columns was held fixed to two or more constant values by the query or schema. Versions affected include v22.1.0-alpha.1 to v22.1.8 and v22.2.0-alpha.1 to v22.2.0-beta.2.
Please reach out to the support team if more information or assistance is needed.
