Skip to content

Defer Memory Allocation of AHI Hash Tables Until AHI Is Enabled - #729

Open
SongLibing wants to merge 1 commit into
mysql:trunkfrom
SongLibing:port_delay_ahi_memory_allocation
Open

Defer Memory Allocation of AHI Hash Tables Until AHI Is Enabled#729
SongLibing wants to merge 1 commit into
mysql:trunkfrom
SongLibing:port_delay_ahi_memory_allocation

Conversation

@SongLibing

@SongLibing SongLibing commented Aug 24, 2026

Copy link
Copy Markdown

Problem

The AHI hash tables are initialized at startup even when AHI is disabled. They are initialized with
buf_pool_get_curr_size()/sizeof(void *)/64 cells. Each cell takes 8 bytes. So the total memory they occupy is approximately equal to 1/64 of the buffer pool size. The memory is wasted if AHI is disabled. On large servers, initializing large hash tables slows startup.

Solution

This commit initializes the AHI hash tables with one cell per AHI partition if AHI is disabled. The hash tables are restored to their normal size before AHI is enabled.

With one slot existing, the AHI system is fully initialized. This preserves the invariant that the per-partition hash table objects always exist.

During a buffer pool resizing, AHI is temporarily disabled. If AHI was enabled when the resize started, its full-sized hash tables are retained and resized only when necessary, avoiding an unnecessary shrink-and-restore cycle.

What does this change do?

https://bugs.mysql.com/bug.php?id=112223

Why is it needed?

This patch reduces memory usage and startup time when AHI is disabled. AHI has been disabled by default since 8.4 and AHI is not recommended for production use. Therefore, without this commit, most of the server would wastes an amount of memory approximately equal to 1/64 of buffer pool size.

How was it tested?

  • Added/updated MTR tests under mysql-test/
  • scripts/ci/mtr.sh passes locally
  • Ran the relevant full suite (name it): ______

Contributor checklist

  • Code is formatted (scripts/ci/format.sh)
  • Commits are focused with descriptive messages

AI assistance

  • I did not use AI assistance for this contribution
  • I used AI assistance for this contribution

If AI assistance was used, describe the tool(s) and extent of use: Port the patch, added more testcases.

Areas touched

InnoDB

@SongLibing
SongLibing requested a review from a team August 24, 2026 11:09
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Aug 24, 2026
@github-actions github-actions Bot added InnoDB Changes touching InnoDB storage engine code Tests Changes touching test code or test data Review Requested Review requested from code owners labels Aug 24, 2026
@gopshank
gopshank requested review from mayprasa and removed request for seemasundara August 24, 2026 23:43
@github-actions github-actions Bot added Build Passed PR build passed MTR Failed MTR suite failed labels Aug 26, 2026
Problem
=======
The AHI hash tables are initialized at startup even when AHI
is disabled. They are initialized with
buf_pool_get_curr_size()/sizeof(void *)/64 cells. Each slot takes
8 bytes. So the total memory they occupy is approximately equal to
1/64 of the buffer pool size. The memory is wasted if AHI is disabled.
On large servers, initializing large hash tables slows startup.

Solution
========
When AHI is disabled, this commit initializes the AHI hash tables with one cell
per partition as requested; ut::find_prime() rounds this up to 103 cells
(~824 bytes) per partition. The hash tables are restored to their normal size
before AHI is enabled.

Repeated SET GLOBAL innodb_adaptive_hash_index = ON statements still invoke
the update callback. The enable path therefore checks the actual AHI state
while holding btr_search_enabled_mutex and returns if AHI is already enabled.
This makes repeated enable operations idempotent and also closes the race
between buffer pool resize publishing its completion and its final attempt to
re-enable AHI.

With the resulting 103 cells per partition, the AHI system is fully
initialized. This preserves the invariant that the per-partition hash table
objects always exist.

During a buffer pool resizing, AHI is temporarily disabled. If AHI was
enabled when the resize started, its full-sized hash tables are retained
and resized only when necessary, avoiding an unnecessary
shrink-and-restore cycle.

Tests
=====
The regression test restores minimized hash tables, performs repeated
primary-key point lookups until AHI allocates a node heap, and then disables
AHI again. It verifies query correctness, hash entry allocation, node heap
release, and restoration of the minimal hash table size.
@SongLibing
SongLibing force-pushed the port_delay_ahi_memory_allocation branch from 40eb059 to 86d5222 Compare September 4, 2026 11:23
@github-actions github-actions Bot removed Build Passed PR build passed MTR Failed MTR suite failed labels Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

InnoDB Changes touching InnoDB storage engine code OCA Verified All contributors have signed the Oracle Contributor Agreement. Review Requested Review requested from code owners Tests Changes touching test code or test data

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant