[Reprint] PostgreSQL, Oracle / MySQL and SQL Server MVCC implementation principle way

PostgreSQL, Oracle / MySQL and SQL Server MVCC implementation principle way

https://www.jdon.com/repository/database-mvcc.html

 

  Relational database management system uses a mechanism MVCC (Multiversion Concurrency Control multi-version concurrency control) to avoid clogging write concurrency issues a read operation, MVCC is through the use of multiple versions of data to ensure that no conflicts of concurrent read and write, different databases there are different implementations, this is the place where the database system headache, relationship database surface looks very simple, using standard SQL statements to manipulate people feel comfortable, but with the increase in size of the system of concurrent users increase, there will be performance database reduce the phenomenon, then we might need to enter from the outside to the in-depth study of fine-tuning internal principle, and the principle of each internal database concurrency is different, if we have a number of different databases, you need different tuning method, this time as the core database production system became less reassuring, this article provides an internal market several popular database of different implementations MVCC.

 

Two different implementations of MVCC

  The first implementation is a plurality of versions of data records stored in a database, when these different versions of the data is no longer needed, the garbage collector to recover these records. This approach is adopted PostgreSQL and Firebird / Interbase, SQL Server uses a similar mechanism, except that the old version data is not stored in the database, the database stored in another different from the main database tempdb /

  The second implementation of the latest version stored in the database only data, but dynamically reconfigurable old version data when using undo, in this way by Oracle and MySQL / InnoDB use.

  Let's look at the specific database implementation mechanism.

 

PostgreSQL's MVCC

  In PostgreSQL, when a row is updated, the new version of the line of data (referred tuple) are created and inserted into the table, a pointer to the previous version of the new version, the previous version is marked as "expired" expired, but also remain in the database until the garbage collector reclaim lost.

In order to support multiple versions, each tuple has the following additional data record:

  • xmin - insert and update records to create this tuple transaction ID
  • xmax - delete records or create a new version of the tuple or delete records of transactions. The initial field is null.

Transaction status is stored in the $ Data / pg_clog in the CLOG This table contains two bytes for each transaction state information, a state may have in-progress, committed, or   aborted. When the end of a transaction, PostgreSQL database will not undo the rollback change records, it just CLOG mark in the transaction is aborted. A PostgreSQL table may contain many such aborted exit data transactions.

A process called Vacuum clean-up will offer expired expired / aborted exit record version of the garbage collector, Vacuum cleaner also deletes the tuple index related items are recycled garbage.

A tuple of xmin xmax is valid and invalid when it is visible. "Valid effective" means "either committed or on behalf of the current transaction." In order to avoid repeated operations CLOG table, PostgreSQL maintain the status indicator in the tuple to indicate whether the tuple is "known committed" or "known aborted".

 

Oracle的MVCC

  Oracle is stored in the old version rollback segment (that is, 'undo log'), a transaction ID is not a sequence of numbers, but by a series of numbers, these numbers point to the head of affairs groove rollback (slot) . Make a new transaction rollback can be reused to store, reuse has been submitted or exit of old transactions used the transaction slot, this automatic reuse mechanism allows limited use of Oracle rollback segments to manage a large number of transactions.

Rollback transaction header block is used as a table, which preserves the state of the transaction is called System Change Number or SCN, Oracle transaction ID for each record is not stored in the page, but by saving each page to keep together an array of array rows unique transaction ID is used to save space, only the records stored in the array offset offset, and each transaction ID is a pointer to undo the last transaction record created page, the table record is not In this way store, index records also use the same technology, which is one of the main difference between Oracle and PostgreSQL.

When an Oracle transaction starts, it will mark a current state of affairs SCN. When reading a table or an index page, use the Oracle SCN numbers to decide whether to include the page should not be allowed to affect the results of the current transaction transaction known, by Oracle Looking associated rollback head to check the status of a transaction, but in order to save time, the first time is a real query transactions, query completes its state will be recorded in the page to avoid the query again later, if the page It was found to contain no visible impact of the transaction, Oracle to re-create the old version of the page by such effects undoing each transaction. It scans and records relating to each transaction, the effect of these transactions applied to the page until all those transactions complete effect is applied after the removal, a new page is created in this manner and then used to access the tuple them.

Oracle recording head:
a recording head does not increase, there is always a fixed size, for the non-clustered table, the recording heads is three bytes, one byte is used to store identification, for displaying a byte whether the record is locked (for example, but it is not sure to submit the updated committed),, a byte count for the column.

 

MVCC SQL Server's

  Use within SQL Server database record version implements snapshot isolation and read committed, only need this database will be open and will produce a corresponding overhead costs.

When a record is modified or deleted, using the copy-on-write mechanism to effectively launch version, Row versioning-based transactions can effectively "view to see the" before and after from the past to present various versions of the same data.

Row version version record stored in the version store, which tempdb database reside outside the main database, and more particularly, when an index in a record or table is modified, a new record will carry the transaction performing the modification " sequence_number. "old version record will be copied to the version stored in a new record comprising a pointer to the old version of the record store, if multiple long run long-running transaction exists, and requires multiple" versions versions ", in record the version store may contain pointers to the earlier record pointer.

Clear stored version of SQL Server:
the size of automatic storage management version of SQL Server, maintaining a clear thread to ensure that the number of records in the version store version will not be too long, more than necessary, query, keep a record of the version store version running under snapshot isolation for modify the data until the transaction is complete, and the transaction need to modify the data contained in any statement completed for the SELECT statement running under Read Committed snapshot isolation, a special edition on record is no longer needed once the completion of the implementation of the SELECT statement It is removed.

If tempdb has no free space, SQL Server calls the clearance, increase the size of the file, of course, assuming that our profile is automatically increased, if the disk has no space, files can not automatically grow, SQL Server will stop generating versions, if this happens, any need to read the version of the snapshot query will fail because of space constraints.

SQL Server recording head
4 bytes
- two bytes of metadata records (Record Type)
- two forwardly directed NULL byte bitmap which is recorded in a recording bitmap (fixed column) the difference between the actual size of the offset.

Version stamp Versioning tag - This is a 14-byte structure, comprising a timestamp plus a pointer to the version stored in tempdb, where the timestamp is trasaction_seq_number, when need to support a version of the operation, the version information is added to the time record.

 

Below is a comparative summary of the various databases:

Database database MVCC

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12357147.html