PostgreSQL Interview Questions
Practice diagnosing what PostgreSQL actually does with concurrent transactions and changing data. Focus on the guarantees you can rely on, not on how a query happens to run on a small fixture.
Try answering aloud before opening each explanation.
Try the PostgreSQL practice demoFoundations
How does MVCC let readers and writers work concurrently?
An update creates a new row version. Each reader sees the version visible to its snapshot, so an ordinary read does not need to wait for an uncommitted update to finish.
What does two SELECT statements seeing different data in one default transaction tell you?
Under Read Committed, each statement gets its own snapshot. Another transaction can commit between the queries, so the second statement can see new data.
What is the difference between a primary key and a unique nullable column?
A primary key enforces both uniqueness and non-nullability. A unique constraint ordinarily permits multiple nulls in PostgreSQL unless configured with NULLS NOT DISTINCT.
Does declaring a foreign key index the referencing column?
Not automatically. Index the referencing columns when lookup or parent deletes and updates justify it; PostgreSQL already requires a suitable unique key on the referenced side.
What does WAL provide in crash recovery?
PostgreSQL records changes before the data pages they describe need to reach durable storage. Recovery replays necessary records from a checkpoint rather than assuming every changed page was flushed on commit.
Applied
When does SELECT FOR UPDATE help protect a business rule?
It locks rows you intend to change until the transaction ends. It does not lock a row that does not exist; use a constraint or another appropriate coordination mechanism for absence-based rules.
Why can a Serializable transaction still return an error?
PostgreSQL can abort a transaction to prevent a non-serializable outcome. Retry the whole transaction with fresh reads and avoid replaying external side effects from the failed attempt.
What is the difference between estimated and actual rows in a query plan?
Estimated rows are planner predictions; actual rows are observed by EXPLAIN ANALYZE. Large differences can signal stale statistics or misestimated correlations. Account for the plan node's loop count.
Why does an index-only plan sometimes still fetch heap pages?
The index may contain all requested values but not enough MVCC visibility information. PostgreSQL uses the visibility map to avoid heap checks on pages known to be all visible.
When is SKIP LOCKED useful and what does it sacrifice?
Multiple workers can claim different available queue rows without waiting for one another. The result deliberately skips locked rows and is not a consistent read of all eligible work.
Production scenarios
How can a long-lived transaction cause table bloat?
Its old snapshot can keep dead row versions potentially visible. Investigate long or idle-in-transaction sessions and let vacuum reclaim reusable space after the retention condition clears.
How do you diagnose a query that is waiting rather than using CPU?
Inspect its wait event and blocking sessions before changing the query. A lock conflict and slow storage need different fixes even if the request times look similar.
Why can a sequential scan be right after adding an index?
For a query reading a large fraction of a table, scanning it may be cheaper than many heap lookups. Confirm with realistic plans and buffers rather than forcing an index.
What should you test before adding an index concurrently on a busy table?
Check the statement's transaction restrictions, expected disk and load headroom, and cleanup of an invalid index after failure. Verify the migration on a realistically sized table.
Why might PostgreSQL benefit from extended statistics?
The planner can underestimate or overestimate predicates on correlated columns when it treats them independently. Extended statistics can capture some multi-column relationships, but still verify the affected query plan.
Further reading
Official documentation
Related question sets
- Node.js interview questions
- TypeScript interview questions
- REST API interview questions
- Microservices interview questions
- SQL interview questions
- Python interview questions
- JavaScript interview questions
- React interview questions
- Kubernetes interview questions
- System Design interview questions
- Java interview questions
- Docker interview questions
- MongoDB interview questions
- Redis interview questions
- NestJS interview questions
- RabbitMQ interview questions