cockroachdb / cockroachdb/cockroach
sql/opt: always preserve ordering of SELECT FOR UPDATE subqueries
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
(In the same vein as #121479.) Unless `propagate_input_ordering` is set, we currently ignore ORDER BY in subqueries. If the subquery uses `FOR UPDATE` or `FOR SHARE`, however, the ordering can be important for preventing deadlocks. We should always preserve the ordering of SFU subqueries. (This might also apply to mutation subqueries / CTEs that use ordering?)
Here's a demonstration on v24.1.0-alpha.5:
```sql
CREATE TABLE ab (a int PRIMARY KEY, b int);
SET optimizer_use_lock_op_for_serializable = on;
-- We lose the ORDER BY here, even though it could help prevent deadlocks.
EXPLAIN (OPT, VERBOSE) SELECT count(*) FROM (SELECT * FROM ab WHERE b % 10 = 6 ORDER BY a FOR UPDATE);
```
Jira issue: CRDB-37564
Contributor guide
Assessment
This issue has not been assessed yet.