Pcornel
Databases

Transactions, MVCC & Locking

Cheat sheet · https://pcornel.dk/topic.php?topic=6089

Back to the Topic

Answer

Transactions group statements into all-or-nothing units; MVCC keeps row versions so readers don't block writers; locks protect concurrent changes.

Remember

Keep transactions short. Long open transactions block cleanup (PG vacuum, MySQL purge) and hold locks. Choose the isolation level deliberately.

First checks

  • SELECT * FROM pg_stat_activity WHERE state <> 'idle' — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Commands

SHOW ENGINE INNODB STATUS

Verify in this order

BEGIN → reads (snapshot) and writes (locks) → COMMIT/ROLLBACK → versions cleaned

Where it usually breaks

Common failures include idle transactions retaining old versions/locks, inconsistent object access order, oversized batches, assuming MVCC means no locking, and killing a blocker without understanding rollback cost or why it held the resource.

Transactions, MVCC & Locking verification boundary matrix

BoundaryInspectHealthy interpretationDo not infer
IntentTransaction lifecycleIntended object/scope is explicit and versioned where relevantIntent is already effective
Owning runtime/control planeIsolation levels / MVCCOwner accepts and reports the expected object stateDownstream dependencies are healthy
Effective stateRow/table locksIndependent read-back matches the intended stateEvery client sees the same result
Consumer outcomeBlockingThe original bounded check succeeds in the same time windowProduction correctness outside this scope

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6089

Confirmation

Confirm action

Continue?