Thinking transactions speed up updates by parallelism
Believing partial changes are visible immediately
Assuming transactions create automatic backups
4. You run this PostgreSQL code:
BEGIN;
UPDATE products SET stock = stock - 1 WHERE id = 10;
-- forgot to COMMIT or ROLLBACK
What problem can occur if you forget to commit or rollback?
medium
A. The update will be saved automatically after a timeout.
B. The update will never be saved and locks may block other users.
C. The database will delete the product with id 10.
D. Other users can edit the same product simultaneously without issues.
Solution
Step 1: Understand transaction finalization
Without COMMIT or ROLLBACK, the transaction stays open and changes are not saved.
Step 2: Identify impact on locks and other users
Open transactions hold locks, blocking others from accessing the updated row.
Final Answer:
The update will never be saved and locks may block other users. -> Option B
Quick Check:
Unfinished transaction = no save + locks [OK]
Hint: Always COMMIT or ROLLBACK to release locks [OK]
Common Mistakes:
Assuming update saves automatically after timeout
Thinking database deletes data on unfinished transactions
Believing other users can edit locked rows freely
5. In a high-traffic PostgreSQL system, two transactions try to update the same row simultaneously. What concurrency control mechanism helps avoid data corruption?
hard
A. Automatic rollback of all transactions after 1 second.
B. Creating duplicate rows to avoid conflicts.
C. Ignoring conflicts and overwriting data silently.
D. Row-level locking to serialize access to the row.
Solution
Step 1: Identify concurrency control methods in PostgreSQL
PostgreSQL uses row-level locks to control access to individual rows during updates.
Step 2: Understand how row-level locking prevents corruption
Locks serialize access so only one transaction modifies a row at a time, avoiding conflicts.
Final Answer:
Row-level locking to serialize access to the row. -> Option D