What is MVCC?
20/11/2023 - 7 phút
What is MVCC?
One of the important and attractive points of the PostgreSQL database management system is how it handles contention between transactions. In PostgreSQL, reading data never blocks writing data and vice versa. This is accomplished through a mechanism called “Multi Version Concurrency Control” (MVCC).
MVCC is not a unique feature of PostgreSQL, it is also implemented in some other database management systems such as Oracle, Berkeley DB, CouchDB, and many other systems. What’s important is understanding how PostgreSQL implements MVCC, as this helps software developers design applications capable of concurrent operation using PostgreSQL, and also helps solve a range of complex problems that may arise in the future.
How does MVCC work?
sequenceDiagram participant Client as Client participant Database as Database participant Transaction1 as Transaction 1 participant Transaction2 as Transaction 2 Client->>Database: Begin Transaction alt Transaction 1 Database->>Transaction1: Assign Transaction ID Transaction1->>Database: Read Data else Transaction 2 Database->>Transaction2: Assign Transaction ID Transaction2->>Database: Read Data end Client->>Database: Perform Update alt Transaction 1 Database->>Transaction1: Create New Version Transaction1->>Database: Update Data else Transaction 2 Database->>Transaction2: Create New Version Transaction2->>Database: Update Data end Client->>Database: Commit Transaction alt Transaction 1 Database->>Transaction1: Save New Version else Transaction 2 Database->>Transaction2: Save New Version end
In the above sequence diagram, we have two transactions, Transaction 1 and Transaction 2, being performed concurrently on the database.
- Each transaction starts with the Client calling “Begin Transaction” with the database.
- The database then assigns a unique transaction ID to each transaction to track them.
- Transaction 1 and Transaction 2 perform data reading (Read Data) from the database.
- After reading the data, both transactions perform data updating (Perform Update). The database creates a new version of the data to ensure consistency.
- Finally, both transactions end by calling “Commit Transaction” and the database saves the new version of the data.
Detailed process of inserting a record into the database
sequenceDiagram participant Client as "Client" participant MVCC as "MVCC" participant Database as "Database" Client->>+MVCC: Begin Transaction (Insert Record) MVCC->>+Database: Begin Transaction (Insert Record) Database-->>-MVCC: Response (Transaction Started) Client->>+MVCC: INSERT INTO Details (StudentID, CourseID, Score) VALUES (1, 101, 85) MVCC->>+Database: INSERT INTO Details (StudentID, CourseID, Score) VALUES (1, 101, 85) Database-->>-MVCC: Response (Details Record Added) Client->>+MVCC: End Transaction (Insert Record) MVCC->>+Database: End Transaction (Insert Record) Database-->>-MVCC: Response (Transaction Ended)
Client starts the transaction by sending a request to insert a record to MVCC.
MVCC receives the request from Client and proceeds by sending a request to insert a record to Database.
Database receives the request from MVCC and confirms that the transaction has started.
Client executes the INSERT INTO query to insert a record into the database. In this example, the record is inserted into the Details table with the values StudentID, CourseID, and Score being 1, 101, and 85 respectively.
MVCC receives the INSERT INTO query from Client and proceeds by sending it to Database.
Database performs the insertion of the record into the Details table and then sends a response to MVCC to inform that the Details record has been successfully added.
Client ends the transaction by sending a request to end the transaction to MVCC.
MVCC proceeds by sending the request to end the transaction to Database.
Database receives the request to end the transaction from MVCC and confirms that the transaction has ended.
Thus, the process of inserting a record into the Database via MVCC includes the steps of starting the transaction, executing the INSERT INTO query, and ending the transaction, ensuring concurrency and avoiding conflicts between different transactions.
Detailed process of deleting a record from the database
sequenceDiagram participant Client as "Client" participant MVCC as "MVCC" participant Database as "Database" Client->>+MVCC: Begin Transaction (Delete Record) MVCC->>+Database: Begin Transaction (Delete Record) Database-->>-MVCC: Response (Transaction Started) Client->>+MVCC: DELETE FROM Details WHERE ID = 1 MVCC->>+Database: DELETE FROM Details WHERE ID = 1 Database-->>-MVCC: Response (Details Record Marked for Deletion) Client->>+MVCC: End Transaction (Delete Record) MVCC->>+Database: End Transaction (Delete Record) Database-->>-MVCC: Response (Transaction Ended)
Client starts the transaction by sending a request to delete a record to MVCC.
MVCC receives the request from Client and proceeds by sending a request to delete a record to Database.
Database receives the request from MVCC and confirms that the transaction has started.
Client executes the DELETE FROM query to delete a record from the Details table based on the condition WHERE ID = 1. In this example, the record with ID 1 will be deleted.
MVCC receives the DELETE FROM query from Client and proceeds by sending it to Database.
The database performs the deletion of the record from the Details table based on the given condition and then sends a response to MVCC to inform that the record has been marked for deletion.
Client ends the transaction by sending a request to end the transaction to MVCC.
MVCC proceeds by sending the request to end the transaction to Database.
Database receives the request to end the transaction from MVCC and confirms that the transaction has ended.
Thus, the process of deleting a record from the Database via MVCC includes the steps of starting the transaction, executing the DELETE query, and ending the transaction, ensuring concurrency and avoiding conflicts between different transactions.
Detailed process of updating a record in the database
sequenceDiagram participant Client as "Client" participant MVCC as "MVCC" participant Database as "Database" Client->>+MVCC: Begin Transaction (Update Record) MVCC->>+Database: Begin Transaction (Update Record) Database-->>-MVCC: Response (Transaction Started) Client->>+MVCC: UPDATE Details SET Score = 90 WHERE ID = 1 MVCC->>+Database: UPDATE Details SET Score = 90 WHERE ID = 1 Database-->>-MVCC: Response (Details Record Updated) Client->>+MVCC: End Transaction (Update Record) MVCC->>+Database: End Transaction (Update Record) Database-->>-MVCC: Response (Transaction Ended)
Client starts the transaction by sending a request to update a record to MVCC.
MVCC receives the request from Client and proceeds by sending a request to update a record to Database.
Database receives the request from MVCC and confirms that the transaction has started.
Client executes the UPDATE query to update a record in the Details table. In this example, the record with ID 1 will be updated with a new value for the Score field of 90.
MVCC receives the UPDATE query from Client and proceeds by sending it to Database.
Database performs the update of the record in the Details table based on the given UPDATE query and then sends a response to MVCC to inform that the record has been updated.
Client ends the transaction by sending a request to end the transaction to MVCC.
MVCC proceeds by sending the request to end the transaction to Database.
Database receives the request to end the transaction from MVCC and confirms that the transaction has ended.
Thus, the process of updating a record in the Database via MVCC includes the steps of starting the transaction, executing the UPDATE query, and ending the transaction, ensuring concurrency and avoiding conflicts between different transactions.
Process of reading data from the database
sequenceDiagram participant Client as "Client" participant MVCC as "MVCC" participant Database as "Database" Client->>+MVCC: Begin Transaction (Read Data) MVCC->>+Database: Begin Transaction (Read Data) Database-->>-MVCC: Response (Read Transaction Started) Client->>+MVCC: SELECT * FROM Details WHERE StudentID = 1 MVCC->>+Database: SELECT * FROM Details WHERE StudentID = 1 Database-->>-MVCC: Response (Query Result) Client->>+MVCC: End Transaction (Read Data) MVCC->>+Database: End Transaction (Read Data) Database-->>-MVCC: Response (Read Transaction Ended)
Client starts a read data transaction and sends a request to MVCC.
MVCC continues by sending a read data request to Database.
Database processes the request and returns the result (data) to MVCC.
MVCC returns the read data result to Client.
Finally, Client ends the read data transaction.
In this process, the Client requests to read data from the database through MVCC, and then MVCC acts as an intermediary to send the request to the Database. The result of the query is data returned from the database through MVCC. After the read data transaction is complete, the Client ends the transaction.
Advantages of MVCC
- Provides concurrency by allowing multiple transactions to occur simultaneously.
- Each transaction is independent of others.
- Creates multiple versions of the database, providing real-time data recovery capabilities.
Disadvantages of MVCC
- MVCC requires more memory and can be more complex than regular databases.
- They can be slower due to processing multiple queries at once.
- Old versions of data may not be removed from the database, leading to an increase in database size.