Sunday 26 May 2024

Behind the Scenes of COMMIT in Oracle: A Peek Under the Hood

 Ever wondered what really happens when you hit the COMMIT button in Oracle?

Step-by-step Guide to COMMIT

  1. LGWR Wakes Up: Picture this as your database's loyal night watchman, always ready to spring into action.
  2. Acquiring the Latches: LGWR grabs the redo allocation latch and redo copy latch. Think of these as the keys to the vault where all your changes are stored.
  3. Flushing the Redo Log Buffer: LGWR quickly transfers all the redo log buffer contents to the log files (and does it in parallel if there are multiple members). It's like moving precious items from a safe to a more secure vault.
  4. Releasing the Latches: After securing everything in the log files, LGWR releases the latches, like a locksmith putting the keys back.
  5. Posting Session A: Finally, LGWR signals session A that the job is done.
And there you have it! The LGWR diligently ensures that the log buffer is safely stored in the online redo log.

Why Redo Entries Matter

When you execute a DML operation (like INSERT, UPDATE, or DELETE), Oracle generates redo entries to track the changes. These entries are temporarily stored in memory during the transaction.
  • On COMMIT: The redo entries are swiftly written to disk along with the redo for the commit itself. Oracle won't return control to you until this task is completely finished, ensuring all your changes are safely recorded.
  • The Waiting Game: The session waits for this process to complete before returning control, ensuring data integrity and durability.

Asynchronous Commit

With Oracle 10g R2, things got even more interesting. Oracle introduced the concept of asynchronous commit. Here’s how it changes the game:

  • Delayed Write: The log writer can now write redo information to disk at its own pace, rather than immediately.
  • Early Return: The commit operation can return control to you before the writing process is finished, which can speed up your transaction times.

Understanding Commit 

Committing a transaction means you're asking Oracle to make your changes permanent. This can happen in two ways:
  1. Explicit Commit: You directly issue a COMMIT statement.
  2. Implicit Commit: This happens automatically after a data definition language (DDL) operation or normal termination of an application.
Once a transaction is committed, all changes become permanent and visible to other users. Any queries executed after the commit will reflect these changes.


Special Case in RAC: The Exclusive Block Transfer

Oracle Real Application Clusters (RAC) adds an extra layer of complexity and fun to the COMMIT process. Imagine this scenario:

  • Exclusive Block Transfer: When a requesting instance asks for an exclusive block transfer, the master node tells the holder instance to transfer the block it holds in exclusive mode.
  • LGWR Flush: Before transferring the block to the requester instance, the holder instance’s LGWR flushes the redo to the log files. This step is crucial for maintaining data consistency.
  • Creating a Past Image: The holder creates a past image (PI) of that block, ensuring a historical snapshot is maintained. This PI is essential for maintaining the integrity of the database.
This condition is specific to RAC, highlighting the intricate dance of data management in a clustered environment.

Naming Transactions

To keep track of long-running transactions or to resolve distributed transactions that are in doubt, you can name your transaction using the SET TRANSACTION ... NAME statement before starting it. This makes monitoring and managing transactions much easier.

By understanding these processes, you can better appreciate the behind-the-scenes magic that ensures your data's safety and integrity in Oracle. 

Happy committing! 😉✨


No comments:

Post a Comment