PREPARE TRANSACTION (2PC)
PREPARE TRANSACTION (2PC) is a SQL statement in the Transactions category. PostgreSQL two-phase commit: prepares a transaction for committing across multiple databases or systems. The syntax is PREPARE TRANSACTION 'transaction_id';. It returns prepared transaction. A typical example: BEGIN; UPDATE accounts SET balance = balance - 100 WHERE id = 1; PREPARE TRANSACTION 'payment_txn_123'; -- Transaction is now prepared (in-doubt) -- Can be committed or rolled back later, even after connection restart -- Later: COMMIT PREPARED 'payment_txn_123'; -- Or: ROLLBACK PREPARED 'payment_txn_123'; A close relative is BEGIN / START TRANSACTION, which begins a new transaction. Changes made within are invisible to others until COMMIT. A close relative is COMMIT, which saves all changes made in the current transaction permanently. A close relative is ROLLBACK, which undoes all changes made in the current transaction (or to a savepoint). More about this category: Transaction control — BEGIN, COMMIT, ROLLBACK, SAVEPOINT, ACID properties.