[{"id":"postgresqlintermediate-001","courseId":"postgresql-intermediate","domain":"concurrency-mvcc","topic":"Phantom reads under Repeatable Read","prompt":"Under Repeatable Read in PostgreSQL, a transaction that runs the same range query twice can see rows that another transaction inserted and committed between the two queries.","hint":"Compare what the standard requires at this level with what PostgreSQL actually implements.","explain":"The SQL standard lists which anomalies each isolation level must prevent. A level may prevent more than the minimum, and PostgreSQL's snapshot design decides what a repeated query sees.","source":"https://www.postgresql.org/docs/current/transaction-iso.html","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","whyNot":"PostgreSQL's Repeatable Read does not allow phantom reads, so the second run of the query returns the same set of rows."},{"id":"b","text":"False","why":"PostgreSQL's Repeatable Read implementation does not allow phantom reads, which is stronger than the standard requires."}],"answer":"b","explanation":"PostgreSQL's Repeatable Read does not allow phantom reads. The standard only requires certain anomalies to be prevented, and stronger guarantees are acceptable.","concept":"PostgreSQL's Repeatable Read does not allow phantom reads. The standard only requires certain anomalies to be prevented, and stronger guarantees are acceptable.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Repeatable Read in PostgreSQL is stronger than the SQL minimum: a repeated range query sees the same rows."]},{"id":"postgresqlintermediate-002","courseId":"postgresql-intermediate","domain":"concurrency-mvcc","topic":"Repeatable Read update conflicts","prompt":"Transaction A runs at REPEATABLE READ. Its first statement, a SELECT on the accounts table, has already run. Then transaction B updates row 7 and commits. Afterwards, A runs SELECT ... FOR UPDATE on row 7. What happens?","hint":"Compare this with how Read Committed handles a row that another transaction changed.","explain":"Each isolation level sets what a transaction may see and when it must wait. Repeatable Read keeps the snapshot taken at the transaction's first statement, and it compares locking reads against that snapshot.","source":"https://www.postgresql.org/docs/current/transaction-iso.html","interaction":{"type":"single"},"options":[{"id":"a","text":"An error is raised, because row 7 changed after A's first statement ran.","why":"Under REPEATABLE READ, a row changed by a transaction that committed after A's snapshot raises an error when A tries to lock it."},{"id":"b","text":"A gets the newer version of row 7 and locks it, as it would under Read Committed.","whyNot":"That is Read Committed behavior. Repeatable Read raises an error for this case instead."},{"id":"c","text":"A gets the newer version of row 7 with no error, because locking reads always see the latest commit.","whyNot":"Under Repeatable Read the changed row produces an error, not a fresh read of the newest version."},{"id":"d","text":"A skips row 7 silently, since it no longer matches A's snapshot.","whyNot":"Nothing is skipped silently here; the transaction gets an error. Skipping locked rows only happens with SKIP LOCKED."}],"answer":"a","explanation":"Within a REPEATABLE READ or SERIALIZABLE transaction, an error is thrown if a row to be locked has changed since the transaction started.","concept":"Within a REPEATABLE READ or SERIALIZABLE transaction, an error is thrown if a row to be locked has changed since the transaction started.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Under Repeatable Read, a locking read on a row changed after the transaction began raises an error."]},{"id":"postgresqlintermediate-003","courseId":"postgresql-intermediate","domain":"concurrency-mvcc","topic":"Serialization failure retries","prompt":"Your app runs SERIALIZABLE transactions. One fails with SQLSTATE 40001 after it has already read several rows and computed a total. What should the app do?","hint":"Think about which results the app may still trust once the transaction has failed.","explain":"Serializable transactions can be rejected when concurrent work would give a result no one-at-a-time order could produce. The rejection carries a known error code the app can detect.","source":"https://www.postgresql.org/docs/current/transaction-iso.html","interaction":{"type":"single"},"options":[{"id":"a","text":"Roll back and run the whole transaction again from the start.","why":"Data read by a transaction that fails cannot be trusted, so the application must retry the transaction until it succeeds."},{"id":"b","text":"Re-run only the statement that failed, then keep going with the earlier results.","whyNot":"Results read in a transaction that later aborted must not be relied on, so the earlier reads cannot be reused."},{"id":"c","text":"Keep the totals already computed and commit, since each row was valid when it was read.","whyNot":"Data read from a user table is not valid until the transaction that read it has successfully committed."},{"id":"d","text":"Ignore the 40001 code and commit, because it is only a warning.","whyNot":"A serialization failure means the transaction may not commit; SQLSTATE 40001 is an error, not a warning."}],"answer":"a","explanation":"A serialization failure always returns SQLSTATE 40001. Applications must not depend on results read in a transaction that aborted; they should retry it.","concept":"A serialization failure always returns SQLSTATE 40001. Applications must not depend on results read in a transaction that aborted; they should retry it.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Treat SQLSTATE 40001 as a signal to retry the whole transaction, not as a reason to keep partial results."]},{"id":"postgresqlintermediate-004","courseId":"postgresql-intermediate","domain":"concurrency-mvcc","topic":"Updates to different rows","prompt":"Transaction A is updating account 11111. At the same moment, transaction B updates account 22222 in the same table. What happens to B?","hint":"Think about the size of lock a row update needs, and whom it blocks.","explain":"PostgreSQL uses locks at several sizes: whole tables, individual rows, and short-lived page locks. Choosing the right size decides which other sessions have to wait.","source":"https://www.postgresql.org/docs/current/explicit-locking.html","interaction":{"type":"single"},"options":[{"id":"a","text":"It runs at once, because row-level locks only block other writers of the same row.","why":"Row-level locks block only writers and lockers to the same row, and row 22222 is not the row A holds."},{"id":"b","text":"It waits until A commits, because UPDATE locks the entire table.","whyNot":"An UPDATE takes row-level locks on the rows it changes; it does not lock the whole table."},{"id":"c","text":"It waits until A commits, because both rows sit on the same disk page.","whyNot":"Page-level locks are released right after each row is fetched or updated, so they do not hold B until commit."},{"id":"d","text":"It fails with an error, since two sessions cannot update one table at once.","whyNot":"Many sessions update the same table concurrently; only updates to the same row conflict."}],"answer":"a","explanation":"Row-level locks block only writers and lockers of the same row, so updates to different rows in one table do not block each other.","concept":"Row-level locks block only writers and lockers of the same row, so updates to different rows in one table do not block each other.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","takeaways":["Two row updates conflict only when they touch the same row."]},{"id":"postgresqlintermediate-005","courseId":"postgresql-intermediate","domain":"concurrency-mvcc","topic":"SKIP LOCKED for queue consumers","prompt":"Worker A has locked job 1 with FOR UPDATE. Worker B runs the query below while jobs 1, 2 and 3 are all queued. What does B get back?","hint":"Look at what the locking clause does with rows it cannot lock right away.","explain":"A locking clause can be set to wait, to fail, or to pass over rows that are already locked. Each choice trades completeness of the result for speed or consistency.","source":"https://www.postgresql.org/docs/current/sql-select.html","interaction":{"type":"single"},"options":[{"id":"a","text":"Jobs 2 and 3; job 1 is skipped because it cannot be locked right now.","why":"SKIP LOCKED skips selected rows that cannot be locked immediately and returns the rest."},{"id":"b","text":"Jobs 1, 2 and 3, after it waits for worker A to finish.","whyNot":"That is the behavior of plain FOR UPDATE; SKIP LOCKED does not wait for locked rows."},{"id":"c","text":"An error, because job 1 is locked by another session.","whyNot":"That is the NOWAIT behavior; SKIP LOCKED passes over locked rows instead of reporting an error."},{"id":"d","text":"Jobs 1, 2 and 3 at once, since SKIP LOCKED only affects writes.","whyNot":"SKIP LOCKED decides which selected rows the locking read returns, so job 1 is left out."}],"answer":"a","explanation":"With SKIP LOCKED, any selected rows that cannot be immediately locked are skipped, rather than waited for or reported as errors.","concept":"With SKIP LOCKED, any selected rows that cannot be immediately locked are skipped, rather than waited for or reported as errors.","rev":1,"updatedAt":"2026-10-23","kind":"scenario","code":"SELECT *\nFROM jobs\nWHERE status = 'queued'\nFOR UPDATE SKIP LOCKED;","takeaways":["SKIP LOCKED gives an inconsistent view, so use it for queue-style consumers rather than general reads."]}]