Skip to content

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
MariaDB:11.4from
DerZc:fix-mdev-39773

Conversation

@DerZc

@DerZc DerZc commented Sep 21, 2026

Copy link
Copy Markdown

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

  • 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.

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.test
  • mysql-test/main/prefix_index_primary_key.result

Validation

On 11.4 at d10e5d726799b1cd57cc866f40aad68c720803da:

  • The server build passed.
  • main.prefix_index_primary_key failed on the unchanged target branch and passed with this fix.
  • Existing MTR tests passed: main.select.
  • MTR reported no test-state cleanup failures.
  • The full regression suite was not run.

@CLAassistant

CLAassistant commented Sep 21, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants