Database & SQL Engineering / TRANSACTIONS, MVCC & CONCURRENCY / SQL-017

Atomic Transfer

Explore concept graph

Latihan soal

Jalankan lab PostgreSQL sungguhan

Dari root repository, dengan Docker terpasang:

bash labs/SQL-017/verify-postgres.sh

Bukti yang diharapkan:

sql017-seed=verified
sql017-commit=atomic
sql017-forced-failure=rolled-back
sql017-explicit-rollback=verified
sql017-fresh-database=verified
sql017-postgres16=verified

Lab memakai database disposable PostgreSQL 16, tanpa host port dan tanpa akses network keluar. Script melakukan tiga operasi dalam satu transaction; memaksa kegagalan UNIQUE/PRIMARY KEY pada ledger; lalu memakai SELECT pada koneksi baru untuk membandingkan persisted state. Gunakan juga labs/SQL-017/inspect.sql untuk memeriksa query aktual.

Proof boundary: generator V3 tidak menjalankan database; receipt fixture bertanda not-executable. Hanya tes Docker PostgreSQL terpisah dalam CI yang membuktikan perilaku DB.

Konsep 1 / 3

Wrap debit, credit and ledger insert in one PostgreSQL BEGIN/COMMIT transaction.

Coba prediksi atau perbaiki

Which observable condition distinguishes a complete 125.00 transfer from a partial debit?

Kalau jawabanmu berbeda

Kesalahan yang mungkin: Treating three SQL statements as atomic merely because they appear in the same file.

Mengapa tidak bekerja: With implicit per-statement commits, an error after debit can leave durable partial updates.

Coba perbaiki: Surround debit, credit and insert with explicit BEGIN/COMMIT and verify the effects in a separate connection.

Periksa hasil sendiri

Hasil yang diharapkan: A-01=875.00, A-02=325.00, ledger=1 and conserved total 1200.00.

Kalau berbeda: Inspect BEGIN/COMMIT placement, WHERE predicates and which database and schema the read-only inspection uses.

Konsep 2 / 3

Demonstrate that a duplicate ledger PRIMARY KEY before COMMIT rolls back earlier debit and credit changes.

Coba prediksi atau perbaiki

The ledger INSERT fails, but the debit SQL had already returned UPDATE 1. What can you conclude about the persisted debit?

Kalau jawabanmu berbeda

Kesalahan yang mungkin: Assuming the database already stored the debit because UPDATE affected one row before the INSERT error.

Mengapa tidak bekerja: Rows modified inside an uncommitted transaction are not durable committed state, and a failed transaction aborts.

Coba perbaiki: Run a fresh SELECT after the failing session ends and compare the full before/after snapshot.

Periksa hasil sendiri

Hasil yang diharapkan: Nonzero psql status and exactly the same balances/ledger as before the failure.

Kalau berbeda: Check that the session used ON_ERROR_STOP=1, that COMMIT was not executed, and that no statement ran outside BEGIN.

Konsep 3 / 3

Verify explicit ROLLBACK and fresh-database replay without pretending static SQL is runtime evidence.

Coba prediksi atau perbaiki

A psql script ends with ROLLBACK and exits 0. Does exit code 0 demonstrate that account balances changed?

Kalau jawabanmu berbeda

Kesalahan yang mungkin: Treating a nonzero exit from a simulated failure, or a zero exit from ROLLBACK, as the whole proof of atomicity.

Mengapa tidak bekerja: Exit codes describe process outcomes, not the full persisted state of both accounts and the ledger.

Coba perbaiki: Use a separate inspection session and compare every protected row/ledger count with the baseline.

Periksa hasil sendiri

Hasil yang diharapkan: Explicit ROLLBACK leaves the same balance and ledger snapshot; a fresh sandbox yields the known committed state.

Kalau berbeda: Check the database name, schema qualifier, and that SELECT runs after the transaction session closed.

Lima soal — Atomic Transfer

Jawab tanpa melihat pembahasan dulu. Kamu bisa memperbaiki jawaban dan mencoba lagi. Kuis ini latihan mandiri dan tidak menyimpan skor atau progres.

Tiga statement debit, kredit, dan INSERT ledger ditulis di satu file tanpa BEGIN/COMMIT. Kesimpulan yang benar?
Setelah commit transfer 125.00 dari saldo 1000.00 ke saldo 200.00, observasi valid mana?
Ledger INSERT dengan transfer_id duplikat gagal setelah kedua UPDATE di dalam BEGIN. Apa bukti rollback?
Sebuah transaction melakukan tiga perubahan valid lalu menjalankan ROLLBACK dan psql exit 0. Apa hasil persisted-nya?
Apa batas bukti dari lab SQL-017 yang menguji sequential BEGIN/COMMIT/ROLLBACK dengan PostgreSQL nyata?