Skip to content

Optimize undo spaces lock #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open

Conversation

wjunLu
Copy link

@wjunLu wjunLu commented Jul 22, 2025

Analysis

During undo tablespace truncation in MySQL, when the target undo tablespace has grown significantly large, the undo_spaces_lock (a read-write lock) experiences severe contention, as demonstrated below

image

The undo_spaces_lock safeguards concurrent access to the following data structures:

  • undo::spaces::m_spaces
  • undo::space_id_bank
  • undo truncation logs

Performance Impact:
When the purge coordinator thread holds the undo_spaces_lock in write mode, all DML foreground threads become blocked, resulting in measurable throughput degradation during this period.

Critical Section Analysis:
The purge coordinator thread pool acquires the lock solely to protect:

  • undo::space_id_bank
  • undo truncation logs
    (Note: It never modifies undo::spaces::m_spaces)

DML foreground threads require the lock only to ensure: read consistency of undo::spaces::m_spaces
(Key Insight: No inherent logical conflict exists between these thread types)

Optimization Strategy

  1. Snapshot Mechanism:
  • After obtaining the write lock, the purge coordinator creates a snapshot of undo tablespaces
  • DML threads then query this snapshot directly, eliminating their need for read locks
  1. Fine-grained Synchronization:
  • Replaces coarse-grained locking with atomic operations for:
    • Version tracking
    • State synchronization
  • Maintains consistency between purge coordinator and DML threads

Figure 1 - current undo_spaces_lock (Before)
image

Figure 2 - The optimized undo_spaces_lock (After)
image

@wjunLu wjunLu force-pushed the undo-spaces-lock branch from 1844fbd to 5bb4ed8 Compare July 22, 2025 09:41
@mysql-oca-bot
Copy link

Hi, thank you for submitting this pull request. In order to consider your code we need you to sign the Oracle Contribution Agreement (OCA). Please review the details and follow the instructions at https://oca.opensource.oracle.com/
Please make sure to include your MySQL bug system user (email) in the returned form.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants