database-transactions
Skill1 starsChanged 2 months agoMITView on GitHub
Reason about transaction semantics - what each isolation level actually prevents, lost updates and optimistic locking, SELECT FOR UPDATE, savepoints, deadlock avoidance by consistent lock ordering, and why long transactions are the root cause of most production stalls. Use when a user reports phantom or dirty reads, a lost-update or double-spend bug, "could not serialize access due to read/write dependencies", deadlock errors, or is choosing an isolation level or asking whether a transaction is too long. Triggers on "isolation level", "race condition", "lost update", "deadlock", "Serializable", "repeatable read", "transaction not committing".
2 other projects use a version of this.
Adopted by
Most recently taken up by
- jars-demo/jars-skillstheir own version, first seen Oct 25, 2026
- MANVENDRA-github/agentrytheir own version, first seen Jul 6, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- jars-demo/jars-skills★ 0same name
- MANVENDRA-github/agentry★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- java-spring-patternsSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 2 are distinctive, and 3 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 15
Read references/isolation-levels.md for the per-engine anomaly matrix and worked lost-update examples.
- Very specific: line 80
Use a covering predicate: WHERE id = 1 FOR UPDATE locks only id = 1.
- Distinctive: line 115
Use a savepoint to roll back part of a transaction, not to manage long work.
- Very specific: line 125
Every SAVEPOINT is a real subtransaction: it pins an xmin (Postgres) and blocks vacuum across the whole database.
- Distinctive: line 164
Read-only reports: use a read replica, not a long read on the primary.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Database Transactions
Transactions fail in ways that look random until you name the anomaly. The output is a decision: which isolation level, which locking strategy, and where the transaction boundary sits.
Read references/isolation-levels.md for the per-engine anomaly matrix and
worked lost-update examples.
Pick the weakest level that is actually correct
| Need | PostgreSQL | MySQL InnoDB | Notes |
| No dirty reads | READ COMMITTED | READ COMMITTED | The default in both. Start here. |
| No non-repeatable reads | REPEATABLE READ | REPEATABLE READ | Needed for "read, decide, write" over several rows |
| No phantom rows (set-based predicates) | REPEATABLE READ | SERIALIZABLE | Engines differ; SELECT ... WHERE status='new' behaves differently |
| Serializable, predicate-based | SERIALIZABLE | SERIALIZABLE | Retries required; not a correctness guarantee without them |
| Business invariants (balance ≥ 0) | Constraint + SERIALIZABLE/lock, or a conditional UPDATE | SELECT ... FOR UPDATE | Prefer a real constraint where one exists |
READ UNCOMMITTED in Postgres is treated as READ COMMITTED and emits a
warning; in MySQL InnoDB it is also mapped to RC, and rows may show partially
updated values because updates are not row-atomic.
Do not reach for SERIALIZABLE to paper over a modelling problem. It is
faster to fix a missing CHECK or a missing unique index than to serialize an
entire workload.
The default fix for lost updates
A read-modify-write in application code is broken under every isolation level
except SERIALIZABLE, because the read and the write are separate statements:
# WRONG: two concurrent sessions both read total=100, both write 110 -> lost update
total = conn.execute("SELECT total FROM accounts WHERE id=1").scalar()
conn.execute("UPDATE accounts SET total=? WHERE id=1", (total + 10,))Fix it in the database, not the application. Pick the first that applies:
- One atomic statement — no read at all:
UPDATE accounts SET total = total + 10 WHERE id = 1; - Conditional update (compare-and-set) — the row's
versionor the value itself is in theWHERE:UPDATE accounts SET total = ?, version = version + 1 WHERE id = ? AND version = ?; -- 0 rows updated == someone beat us - Optimistic locking — a
versioncolumn bumped by every writer; the application retries a fixed number of times on 0 rows updated. This is the right default for user-driven optimistic UIs. SELECT ... FOR UPDATE— when the read is genuinely needed first.
The error UPDATE ... WHERE version = ? affecting 0 rows is not an error
condition; treat it as a retry signal, not a bug.
SELECT ... FOR UPDATE
BEGIN;
SELECT total FROM accounts WHERE id = 1 FOR UPDATE; -- row lock held
UPDATE accounts SET total = 10 WHERE id = 1;
COMMIT;- Locks the rows the query reads. Under Postgres
READ COMMITTED, if a concurrent update arrives while you wait, the lock returns the updated row — the value you read may be newer than the snapshot you started with. Re-check any predicate you relied on after the lock is granted. - Use a covering predicate:
WHERE id = 1 FOR UPDATElocks onlyid = 1. A bareFOR UPDATEon a query that scansstatus='new'locks every matching row it touches, which is how one transaction blocks a queue. - Take the lock as early as possible, before doing any other work
(HTTP calls, computing, serializing). Every statement between
BEGINandCOMMITextends the lock's lifetime. FOR UPDATE NOWAITraises55P03 could not obtain lock on row in relation ...immediately instead of waiting.FOR UPDATE SKIP LOCKEDskips locked rows and returns the rest — ideal for queue workers, where two workers must not process the same item.
Deadlock avoidance
Postgres reports the real cause:
ERROR: deadlock detected followed by DETAIL: Process 1234 waits for ShareLock on transaction 5678; process 5678 waits for ShareLock on transaction 1234.
Deadlocks are not bad luck; they are two transactions taking the same locks in different orders. Fix the order, not the retry:
- One global lock order. Sort the keys and always acquire in that order —
SELECT ... FOR UPDATEoverWHERE id IN (...) ORDER BY id. An in-memorysorted(ids)is enough. - One lock per transaction where possible. An aggregate (
UPDATE accounts SET balance = balance + delta) or a single row beats a read-then-write over a set. - Short transactions. The window in which an inconsistent order can form is the transaction length.
Retry on deadlock is legitimate, but only with the lock order fixed and a
bounded, jittered retry. database is locked (MySQL/SQLite) and 40P01
deadlock_detected (Postgres) are retryable; 23505 unique_violation is
usually not.
Savepoints
Use a savepoint to roll back part of a transaction, not to manage long work.
BEGIN;
SAVEPOINT sp1;
INSERT INTO audit (...) VALUES (...); -- a chunk you may want to undo
-- on failure: ROLLBACK TO SAVEPOINT sp1; the transaction stays usable
COMMIT;Every SAVEPOINT is a real subtransaction: it pins an xmin (Postgres) and
blocks vacuum across the whole database. A savepoint in a hot loop is a
production problem, not a code smell.
Transactions that run too long
The failure mode is never a single slow query — it is a transaction that quietly stays open. Symptoms and their causes:
| Symptom | Likely cause |
idle in transaction for minutes | Code path that commits only on the success branch — a return or a raised exception skips the commit |
| Replication lag | A long write transaction on the primary replays slowly on standbys |
| Table bloat everywhere | An old xmin blocks vacuum (long read, long transaction, or an abandoned replication slot) |
FATAL: terminating connection due to administrator command storms | idle_in_transaction_session_timeout firing |
| Row locks pile up | Transaction opened early, lock taken late |
Find them in Postgres:
SELECT pid, state, now() - xact_start AS xact_age, left(query, 80)
FROM pg_stat_activity
WHERE state <> 'idle' OR now() - xact_start > interval '1 min'
ORDER BY xact_start;Enforce it in config rather than hoping:
idle_in_transaction_session_timeout = '60s',
statement_timeout per role, and lock_timeout short for anything
user-facing so waits fail fast instead of piling up.
Rules of thumb:
- Open the transaction as late as possible and commit as early as possible.
- Never hold a transaction across an HTTP call, a message publish, a
sleep, or user input. The classic dual-write problem: if you must write to two systems, use an outbox row in the same transaction and deliver asynchronously. - Set the isolation level per transaction that needs it, not per session.
- Read-only reports: use a read replica, not a long read on the primary.
Savepoints, retries, and idempotence together
A retried SERIALIZABLE transaction must be safe to run twice. The
Postgres error is explicit:
ERROR: could not serialize access due to read/write dependencies among transactionsThat is a retry signal, not a bug: the transaction was aborted cleanly, nothing was committed, and re-running it whole is correct. Wrap the entire transaction body in a bounded retry with jitter; retrying only the failed statement is wrong, because the transaction is already aborted.
Gotchas
autocommit=Falsein an ORM does not always begin a transaction on the first SELECT. Some drivers deferBEGINuntil the first write, so a read and a later write are in different transactions — the read-modify-write protection you assumed is absent. Verify with the engine's logs orSHOW TRANSACTION ISOLATION LEVEL/ the pool's current state.- Committing a transaction that also changed schema can take seconds, and
the wait is attributed to the first statement. Set a short
lock_timeoutso the failure names the real culprit. - A savepoint rolled back to does not release the row locks taken after it in some engines. Postgres releases locks acquired after a subtransaction rollback, but buffered I/O and row versions can still block vacuum.
- Two-phase commit is not a substitute for an outbox. It is a correctness tool for the rare case where a distributed atomic commit is genuinely required, and it is not supported the way people expect on replicas.
- An aggregate computed from a
FOR UPDATEsnapshot can still be wrong if the read predicate did not cover the rows you care about; locking locks what you selected, not what you meant. - Idempotency beats every isolation level for retried work. Prefer an idempotency key table with a unique constraint over retry logic scattered across services.
- "Repeatable read" means different things per engine. MySQL's
REPEATABLE READuses gap locks and can lock far more than the rows you read, causing much more contention than Postgres's snapshot isolation.innodb_locks_unsafe_for_binlogand gap locks are the usual suspect for "why is this INSERT blocking".
Files
references/isolation-levels.md— anomaly matrix per engine, the four read/write anomalies, worked lost-update and phantom examples, and how Serializable maps to Raft/CockroachDB.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

