One page

Databases cheat sheet

What to keep at hand. If a line is not clear, go back to the matching lesson in the course.

Open the course: Databases: going deeper

PostgreSQL isolation levels

WhatHow
Read Committed (default)each statement sees a fresh snapshot; non-repeatable read, phantom, lost update are possible
Repeatable Readone snapshot per transaction; no phantoms; a write conflict gives 40001
SerializableSSI, as if run one by one; catches write skew; transactions must be retried
Set the levelBEGIN ISOLATION LEVEL REPEATABLE READ;
Protection from lost updateUPDATE t SET n = n - 1 ... | SELECT ... FOR UPDATE | version column

Index types

WhatHow
B-tree=, <, >, BETWEEN, ORDER BY, LIKE 'abc%'
CompositeCREATE INDEX ON orders (user_id, created_at);
CoveringCREATE INDEX ON orders (user_id) INCLUDE (total);
ExpressionCREATE INDEX ON users (lower(email));
PartialCREATE INDEX ON orders (created_at) WHERE status = 'pending';
GINjsonb, arrays, full-text search, pg_trgm
GiSTgeodata, ranges, nearest neighbor search
BRINhuge tables that grow over time
Hashonly =

Scaling PostgreSQL

WhatHow
Replica lagSELECT replay_lag FROM pg_stat_replication;
Partitioned tableCREATE TABLE events (...) PARTITION BY RANGE (created_at);
PartitionCREATE TABLE events_2026_11 PARTITION OF events FOR VALUES FROM (...) TO (...);
Logical replicationCREATE PUBLICATION pub FOR TABLE orders;
ShardingCitus or a shard key at the application level

Which model to choose

WhatHow
Relational (PostgreSQL)transactions, JOINs, reports; the default choice
Document (MongoDB)the entity is read as a whole, flexible structure
Key-value (Redis, DynamoDB)cache, sessions, counters, access by key
Wide-column (Cassandra)very many writes, queries known in advance
Graph (Neo4j)queries that walk several hops of relationships

PostgreSQL in production

WhatHow
Real planEXPLAIN (ANALYZE, BUFFERS) SELECT ...;
Statistics for dependent columnsCREATE STATISTICS s (dependencies) ON city, country FROM addresses;
Job queueSELECT ... FOR UPDATE SKIP LOCKED LIMIT 10;
Who is blockingSELECT pid, pg_blocking_pids(pid) FROM pg_stat_activity;
Timeout before DDLSET lock_timeout = '3s';
PgBouncerpool_mode = transaction
Logical backuppg_dump -Fc -d app -f app.dump
Index without blockingCREATE INDEX CONCURRENTLY ...
Constraint without blockingADD CONSTRAINT ... NOT VALID; VALIDATE CONSTRAINT ...

Other cheat sheets