Skip to content

MDEV-40927 Crash on long unique insert into partition - #5604

Open
FarihaIS wants to merge 1 commit into
MariaDB:10.11from
FarihaIS:mdev-40927
Open

MDEV-40927 Crash on long unique insert into partition#5604
FarihaIS wants to merge 1 commit into
MariaDB:10.11from
FarihaIS:mdev-40927

Conversation

@FarihaIS

@FarihaIS FarihaIS commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Description

INSERT ... PARTITION into a partitioned table with a long unique key prunes lock_partitions to the named partitions, but check_insert_or_replace_autoincrement() widens read_partitions to all.

The unique hash check then reads a partition that was never external locked for the statement, tripping the assertion.

Lock all read_partitions in ha_partition::external_lock() for write locks (F_WRLCK) on a table with a long unique key, so every partition the check reads is bound to the current statement. lock_partitions is left untouched, so the write stays restricted to the selected partitions.

Add a test to main.long_unique_bugs.

Release Notes

N/A

How can this PR be tested?

Execute the main suite in mysql-test-run in debug mode.

This commit adds a test in the long_unique_bugs test of the main suite (main.long_unique_bugs).

Before the fix

Running main.long_unique_bugs (with the added test) fails as below in debug mode:

main.long_unique_bugs                    [ fail ]
        Test ended at 2026-08-26 18:09:02
…
mariadbd: /quick-rebuilds/mariadb-server/storage/innobase/handler/ha_innodb.cc:9112: int ha_innobase::change_active_index(uint): Assertion `m_user_thd == ha_thd()' failed.
260826 18:08:57 [ERROR] /quick-rebuilds/build/sql/mariadbd got signal 6 ;
…
Connection ID (thread ID): 5
Status: NOT_KILLED
Query (0x772018014ec0): INSERT INTO t PARTITION (p1) VALUES (1,1)
…
Thread 1 (Thread 0x7f20544416c0 (LWP 28737)):
#0  0x00007f205804f3bc in ?? () from /usr/lib/x86_64-linux-gnu/libc.so.6
#1  0x000055e86bc7e55a in my_write_core (sig=6) at /quick-rebuilds/mariadb-server/mysys/stacktrace.c:424
#2  0x000055e86b3d0fd4 in handle_fatal_signal (sig=6) at /quick-rebuilds/mariadb-server/sql/signal_handler.cc:300
#3  <signal handler called>
#4  0x00007f205804f3bc in ?? () from /usr/lib/x86_64-linux-gnu/libc.so.6
#5  0x00007f2057ff8942 in raise () from /usr/lib/x86_64-linux-gnu/libc.so.6
#6  0x00007f2057fe04ac in abort () from /usr/lib/x86_64-linux-gnu/libc.so.6
#7  0x00007f2057fe0420 in ?? () from /usr/lib/x86_64-linux-gnu/libc.so.6
#8  0x000055e86b733ae1 in ha_innobase::change_active_index (this=0x772004024628, keynr=1) at /quick-rebuilds/mariadb-server/storage/innobase/handler/ha_innodb.cc:9112
#9  0x000055e86b7330be in ha_innobase::index_init (this=0x772004024628, keynr=1) at /quick-rebuilds/mariadb-server/storage/innobase/handler/ha_innodb.cc:8785
#10 0x000055e86ae4c508 in handler::ha_index_init (this=0x772004024628, idx=1, sorted=false) at /quick-rebuilds/mariadb-server/sql/handler.h:3410
#11 0x000055e86b6f8918 in ha_partition::index_init (this=0x772004023d88, inx=1, sorted=false) at /quick-rebuilds/mariadb-server/sql/ha_partition.cc:5818
#12 0x000055e86ae4c508 in handler::ha_index_init (this=0x772004023d88, idx=1, sorted=false) at /quick-rebuilds/mariadb-server/sql/handler.h:3410
#13 0x000055e86b3e5a8a in handler::check_duplicate_long_entry_key (this=0x772004023d88, new_rec=0x772004025628 "\371\001", key_no=1) at /quick-rebuilds/mariadb-server/sql/handler.cc:7523
#14 0x000055e86b3e61ec in handler::ha_check_long_uniques (this=0x772004023d88, old_rec=0x0, new_rec=0x772004025628 "\371\001") at /quick-rebuilds/mariadb-server/sql/handler.cc:7623
#15 0x000055e86b3e5f1e in handler::ha_check_inserver_constraints (this=0x772004023d88, old_data=0x0, new_data=0x772004025628 "\371\001") at /quick-rebuilds/mariadb-server/sql/handler.cc:7597
#16 0x000055e86b3e6fa3 in handler::ha_write_row (this=0x772004023d88, buf=0x772004025628 "\371\001") at /quick-rebuilds/mariadb-server/sql/handler.cc:7848
#17 0x000055e86af5a252 in Write_record::single_insert (this=0x7f205443f870, inserted=0x7f205443f690) at /quick-rebuilds/mariadb-server/sql/sql_insert.cc:2326
#18 0x000055e86af5a426 in Write_record::write_record (this=0x7f205443f870) at /quick-rebuilds/mariadb-server/sql/sql_insert.cc:2368
#19 0x000055e86af56b3a in mysql_insert (thd=0x772018000e38, table_list=0x772018015010, fields=..., values_list=..., update_fields=..., update_values=..., duplic=DUP_ERROR, ignore=false, result=0x0) at /quick-rebuilds/mariadb-server/sql/sql_insert.cc:1216

After the fix

Running main.long_unique_bugs (with the added test) succeeds in debug mode:

main.long_unique_bugs                    [ pass ]   1932

Basing the PR against the correct MariaDB version

  • This is a bug fix, and the PR is based against the earliest maintained branch on which the bug can be reproduced: 10.11.

Copyright

All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services, Inc.

@FarihaIS
FarihaIS marked this pull request as ready for review August 26, 2026 21:37
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Aug 27, 2026
@gkodinov gkodinov self-assigned this Aug 27, 2026
@gkodinov
gkodinov requested a lite review from Copilot August 27, 2026 07:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a debug-mode crash when executing INSERT ... PARTITION on partitioned tables with long unique keys and an AUTO_INCREMENT column by ensuring partitions that may be read during long-unique checks are also externally locked for the statement.

Changes:

  • Adjust ha_partition::external_lock() to lock read_partitions (not just lock_partitions) for long-unique tables.
  • Add a regression test case for MDEV-40927 to main.long_unique_bugs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
sql/ha_partition.cc Changes partition selection for external_lock() to prevent reading from partitions that weren’t externally locked in the current statement.
mysql-test/main/long_unique_bugs.test Adds a new MDEV-40927 regression test exercising INSERT … PARTITION with long unique hash + AUTO_INCREMENT.
mysql-test/main/long_unique_bugs.result Updates expected output for the new regression test.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread sql/ha_partition.cc Outdated

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! This is a preliminary review.

I believe co-pilot has a point in what it's saying. Please address that.

Otherwise, LGTM.

INSERT ... PARTITION into a partitioned table with a long unique key
prunes lock_partitions to the named partitions, but
check_insert_or_replace_autoincrement() widens read_partitions to all.
The unique hash check then reads a partition that was never external
locked for the statement, tripping the assertion.

Lock all read_partitions in ha_partition::external_lock() for write
locks (F_WRLCK) on a table with a long unique key, so every partition
the check reads is bound to the current statement. lock_partitions is
left untouched, so the write stays restricted to the selected
partitions.

Add a test to main.long_unique_bugs.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer Amazon Web
Services, Inc.
@FarihaIS

Copy link
Copy Markdown
Contributor Author

@gkodinov I made the change Copilot suggested - could you please take another look? Thank you

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@gkodinov
gkodinov requested a review from holyfoot August 28, 2026 08:22
@gkodinov gkodinov assigned holyfoot and unassigned gkodinov Aug 28, 2026
@gkodinov

Copy link
Copy Markdown
Member

FYI: According to our development cycle we work on bugs In the following periods 15 Mar-30 Apr, 15 Jun-30 Jul, 15 Sep-30 Oct and 15 Dec-31 Jan. So, please, expect to get a review somewhere between these two dates and the goal is to have your PR merged before the second date

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

4 participants