GaussDB(DWS) supports the ACID properties of database transactions. It provides the READ COMMITTED and REPEATABLE READ isolation levels of transactions.
For example, if account A transfers money to account B, two operations are involved: deducting fees from account A and adding the same amount to account B.
The two operations are independent from each other, and there is a probability that only one of them succeeds. To avoid the situation that the fees are deducted from account A but not added to account B, a transaction will roll back all its operations if one of them fails.
The purposes of database transactions are as follows:
After a transaction is committed in DBMS, DBMS needs to ensure that all operations in the transaction are successfully completed and the results are permanently stored in the database. If an operation in the transaction fails, all the operations in the transaction must be rolled back to the status before the transaction is executed. Transactions run independently and do not interfere with each other or affect database running.
A transaction has atomicity, consistency, isolation, and durability (ACID) properties.
For example, if account A transfers an amount of money to account B, $500 are deducted from account A and $500 added to account B. If the amount fails to be added to account B, it cannot be deducted from account A. If atomicity is not guaranteed, the account balances will be consistent.
For example, if account A transfers $500 to account B, $500 is deducted from account A and added to account B. The sum of the deducted amount (-$500) and the added amount (+500) should be 0. The total account balance of accounts A and B remains unchanged, no matter whether the money is transferred.
Transactions are often executed concurrently. Isolation ensures that concurrent execution of transactions leaves the database in the same state that would have been obtained if the transactions were executed sequentially. Transaction isolation is divided into different levels, including READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE.
ACID |
Purpose |
---|---|
Atomicity |
Concurrency control and fault recovery |
Consistency |
SQL integrity constraints (primary key and foreign key constraints) |
Isolation |
Concurrency control |
Durability |
Fault recovery |
Common concurrency control technologies include lock-based and timestamp-based concurrency control. GaussDB(DWS) uses the two-phase lock technology for DDL statements and uses multi-version concurrency control (MVCC) for DML statements. GaussDB(DWS) databases fault recovery is based on WAL logs. MVCC mainly uses redo logs to ensure transaction read/write consistency.
Isolation prevents data inconsistency during the execution of concurrent transactions. A transaction isolation level specifies how concurrent transactions process the same object.
In GaussDB(DWS), transaction isolation levels are controlled by the GUC parameter transaction_isolation or the SET TRANSACTION syntax. The following isolation levels are supported. The default isolation level is READ COMMITTED.
GaussDB(DWS) starts a transaction using START TRANSACTION and BEGIN. For details, see START TRANSACTION and BEGIN.
GaussDB(DWS) sets a transaction using SET TRANSACTION or SET LOCAL TRANSACTION. For details, see SET TRANSACTION.
GaussDB(DWS) commits all operations of a transaction using COMMIT or END. For details, see COMMIT | END.
If a fault occurs during a transaction and the transaction cannot proceed, the system performs rollback to cancel all the completed database operations related to the transaction. For details, see ROLLBACK.
If an execution request (not in a transaction block) received in the database contains multiple statements, the statements will be packed into a transaction. If one of the statements fails, the entire request will be rolled back.
A customer buys a $100 item in a store using an e-payment account. At least two operations are involved: 1. $100 is deducted from the customer's account. 2. $100 is added to the store's account. In DBMS, the two operations must be both completed or not executed at all.
1 2 3 4 5 | CREATE TABLE customer_info ( NAME VARCHAR(32) PRIMARY KEY, MONEY INTEGER ); INSERT INTO customer_info (name, money) VALUES ('buyer', 500), ('shop', 500); |
1 2 3 4 5 6 | SELECT * FROM customer_info; name | money -------+------- buyer | 500 shop | 500 (2 rows) |
Deduct $100 from the customer's account and add $100 to the store's account.
1 2 3 4 5 6 7 8 9 | UPDATE customer_info SET money = money-100 WHERE name IN (SELECT name FROM customer_info WHERE name = 'buyer'); UPDATE customer_info SET money = money+100 WHERE name IN (SELECT name FROM customer_info WHERE name = 'shop'); SELECT * FROM customer_info; name | money -------+------- buyer | 400 shop | 600 (2 rows) |
1 2 3 4 5 6 7 | UPDATE customer_info SET money=500; select * from customer_info; name | money -------+------- shop | 500 buyer | 500 (2 rows) |
$100 is deducted from the customer's account but fails to be added to the store's account.
1 | UPDATE customer_info SET money = money-100 WHERE name IN (SELECT name FROM customer_info WHERE name = 'buyer'); |
1 | UPDATE customer_info SET money = money+100 WHERE name IN (SELECT name FROM customer_info WHERE name = 'shop'); |
1 2 3 4 5 6 | SELECT * FROM customer_info; name | money -------+------- buyer | 400 shop | 500 (2 rows) |
Without ACID properties, the account balances will be incorrect once an error occurs during SQL statement execution..
1 | UPDATE customer_info SET money=500; |
1 2 | BEGIN TRANSACTION; UPDATE customer_info SET money = money-100 WHERE name IN (SELECT name FROM customer_info WHERE name = 'buyer'); |
1 | UPDATE customer_info SET money = money+100 WHERE name IN (SELECT name FROM customer_info WHERE name = 'shop'); |
1 2 3 4 | ERROR: syntax error at or near "shop" LINE 1: ...e IN (SELECT name FROM customer_info WHERE name = ''shop''); END TRANSACTION; ROLLBACK |
1 2 3 4 5 6 | SELECT * FROM customer_info; name | money -------+------- buyer | 500 shop | 500 (2 rows) |
GaussDB(DWS) uses the distributed shared nothing architecture. Table data is distributed on different nodes. One or more statements on the client may modify data on multiple nodes at the same time. In this case, a distributed transaction is generated. GaussDB(DWS) uses two-phase commit transactions to ensure data consistency and atomicity in distributed transactions. Two-phase commit divides transaction commit into two phases, usually for transactions that contain write operations. When data is written to different nodes, the atomicity requirement of the transaction must be met, that is, either all data is committed or all data is rolled back.
Two-phase commit is not supported in the following scenarios:
1 2 3 | BEGIN; PREPARE TRANSACTION 'p1'; ERROR: Explicit prepare transaction is not supported. |
1 2 | REINDEX TABLE pg_class; ERROR: cannot PREPARE a transaction that modified relation mapping. |
1 2 3 4 5 | BEGIN; CREATE TABLE t1(a int); SELECT pg_export_snapshot(); END; ERROR: cannot PREPARE a transaction that has exported snapshots. |