Bug #85749 | Manual does NOT explain gap locks set by SELECT ... FOR UPDATE | ||
---|---|---|---|
Submitted: | 31 Mar 2017 16:45 | Modified: | 15 May 2018 18:45 |
Reporter: | Valeriy Kravchuk | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: Documentation | Severity: | S3 (Non-critical) |
Version: | 5.7 | OS: | Any |
Assigned to: | CPU Architecture: | Any | |
Tags: | locks, missing manual, SELECT ... FOR UPDATE |
[31 Mar 2017 16:45]
Valeriy Kravchuk
[31 Mar 2017 18:10]
MySQL Verification Team
Hi! I have run your test case and got the same results as you have. Upon further analysis, I concluded that this is a bug. A small bug , but a bug. Verified.
[1 Apr 2017 15:55]
Valeriy Kravchuk
Sorry, but I had to change synopsis and reclassify the bug as documentation request based on further debugging and code review. No locks are released in this case, but we do request X lock on the gap before the next, non-matching record when non-unique secondary index is used. Check code starting from this line (https://github.com/mysql/mysql-server/blob/71f48ab393bce80a59e5a2e498cd1f46f6b43f9a/storag...): /* Try to place a gap lock on the next index record to prevent phantoms in ORDER BY ... DESC queries */ const rec_t* next_rec = page_rec_get_next_const(rec); offsets = rec_get_offsets(next_rec, index, offsets, ULINT_UNDEFINED, &heap); err = sel_set_rec_lock(pcur, next_rec, index, offsets, prebuilt->select_lock_type, LOCK_GAP, thr, &mtr); in row_search_mvcc(). See the (potential) reason to set this gap lock in the comment above. Maybe there is another reason for the behavior we see. Then it should be also documented.
[3 Apr 2017 13:55]
MySQL Verification Team
Valerii, Thank you for finding that comment. We shall see that this is documented fully.
[15 May 2018 18:26]
Daniel Price
Posted by developer: The reference documentation has been updated. https://dev.mysql.com/doc/refman/5.7/en/innodb-locks-set.html The changes should appear online soon. "For locking reads (SELECT with FOR UPDATE or FOR SHARE), UPDATE, and DELETE statements, the locks that are taken depend on whether the statement uses a unique index with a unique search condition, or a range-type search condition. For a unique index with a unique search condition, InnoDB locks only the index record found, not the gap before it. For other search conditions, and for non-unique indexes, InnoDB locks the index range scanned, using gap locks or next-key locks to block insertions by other sessions into the gaps covered by the range." Thank you for the bug report.