MDEV-39773 Prefix index on VARCHAR PRIMARY KEY column causes equality lookup to return 0 rows (wrong result) when optimizer chooses const access via the prefix index - #5709
Open
DerZc wants to merge 1 commit into
Conversation
… lookup to return 0 rows (wrong result) when optimizer chooses const access via the prefix index An equality lookup through a VARCHAR prefix index can miss an existing row when the optimizer treats that index as a unique, single-row access path. Extended-key construction considers a primary-key field covered by a secondary-key part using only fieldnr. A prefix of the same field is not equivalent to its full primary-key part, but can consequently receive the uniqueness properties of the full key. Require both the field number and key-part length to match before treating a secondary-key part as covering a primary-key part. Keep the full primary-key part available when the secondary index contains only a prefix, preventing an invalid HA_EXT_NOSAME classification. The regression inserts -1 and -2 into an InnoDB table with a VARCHAR primary key, adds a one-character prefix index, and requires a forced prefix-index lookup for -2 to return that row. Bug report: https://jira.mariadb.org/browse/MDEV-39773
DerZc
force-pushed
the
fix-mdev-39773
branch
from
September 21, 2026 05:56
a43bff3 to
2067e37
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An equality lookup through a VARCHAR prefix index can miss an existing row when the optimizer treats that index as a unique, single-row access path.
Bug report: https://jira.mariadb.org/browse/MDEV-39773
Root cause
Extended-key construction considers a primary-key field covered by a secondary-key part using only fieldnr. A prefix of the same field is not equivalent to its full primary-key part, but can consequently receive the uniqueness properties of the full key.
Changes
Regression coverage
The regression inserts -1 and -2 into an InnoDB table with a VARCHAR primary key, adds a one-character prefix index, and requires a forced prefix-index lookup for -2 to return that row.
mysql-test/main/prefix_index_primary_key.testmysql-test/main/prefix_index_primary_key.resultValidation
On
11.4atd10e5d726799b1cd57cc866f40aad68c720803da:main.prefix_index_primary_keyfailed on the unchanged target branch and passed with this fix.main.select.