Interview Q&A · questions with model answers
PostgreSQL Interview Questions
Part of the study plan for: Data Engineer
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked How does MVCC work in PostgreSQL?
Every row version (tuple) carries xmin (the transaction that created it) and xmax (the transaction that deleted or updated it). An UPDATE writes a new tuple and marks the old one dead; each…
-
Most asked JSON or JSONB in PostgreSQL, and how do you index JSONB?
JSONB stores a parsed binary form: it is slightly slower to write but much faster to query, supports containment and existence operators and can be indexed with GIN. JSON keeps the exact text…
-
Most asked How does CREATE INDEX CONCURRENTLY work, and what are its caveats?
It builds the index without blocking writes, using multiple passes over the table and waiting for existing transactions to finish. It takes longer, cannot run inside a transaction block, and if it…
-
Most asked How does INSERT ... ON CONFLICT work in PostgreSQL?
It is PostgreSQL's atomic upsert: insert a row, and if it violates a specified unique constraint or index, either do nothing (DO NOTHING) or update the existing row (DO UPDATE SET ..., with EXCLUDED…
-
Most asked Streaming (physical) replication or logical replication: what is the difference?
Streaming replication ships WAL to standbys, which are byte-for-byte copies of the whole cluster (read-only hot standbys, used for HA and read scaling). Logical replication publishes row changes for…
-
Most asked How do backups and point-in-time recovery work in PostgreSQL?
Take physical base backups (pg_basebackup, or tools such as pgBackRest, Barman, WAL-G) and archive WAL continuously. To recover to a point in time, restore the base backup and replay archived WAL up…
-
Most asked text or varchar(n)? timestamp or timestamptz?
In PostgreSQL text and varchar perform the same; use text (with a CHECK constraint if you need a limit). For moments in time use timestamptz, which stores an absolute instant (UTC internally) and…
-
How should an application handle serialization failures and deadlocks in PostgreSQL?
Catch SQLSTATE 40001 (serialization_failure) and 40P01 (deadlock_detected) and retry the entire transaction from the beginning, with a small randomised backoff and a retry limit. Retrying only the…
-
Give a practical example of window functions in PostgreSQL.
Window functions compute values over related rows without collapsing them: ranking, running totals, moving averages, and comparing a row with the previous one. Example: the top three products by…
-
Sequential scan, index scan, or bitmap scan: how does the planner choose?
It estimates the cost of each plan from statistics and cost settings. A sequential scan is cheapest when a large fraction of rows match; an index scan when few rows match; a bitmap scan in between,…
-
Explain nested loop, hash and merge joins.
Nested loop: for each outer row, look up matching inner rows (great when the outer side is small and the inner side is indexed). Hash join: build a hash table on the smaller input, probe it with the…
-
How do planner statistics work, and what are extended statistics?
ANALYZE samples each column and stores the fraction of NULLs, distinct values, most-common values and a histogram in pg_statistic (readable via pg_stats). The planner assumes columns are…
-
Are CTEs optimisation fences in PostgreSQL?
Not anymore by default. Since PostgreSQL 12, a non-recursive, side-effect-free CTE referenced once is inlined into the main query, so the planner can push filters into it. You can force the old…
-
How does full-text search work in PostgreSQL?
Text is converted to a tsvector (normalised lexemes with positions) and queries to a tsquery; the @@ operator matches them, ts_rank scores results, and a GIN index on the tsvector (often a stored…
-
How do you scale PostgreSQL writes beyond one server?
First get the most out of one primary (query and index fixes, connection pooling, partitioning, archiving, hardware) and move reads to replicas. Beyond that, shard: split data by a key across…
-
How is high availability usually achieved for self-managed PostgreSQL?
PostgreSQL has replication built in but no automatic failover. Tools such as Patroni (with etcd, Consul or ZooKeeper for leader election), repmgr or pg_auto_failover promote a standby when the…
-
How do roles, privileges and schemas work in PostgreSQL, and what is the search_path risk?
Roles are users and groups; privileges are granted on databases, schemas, tables, sequences and functions; schemas are namespaces inside a database. search_path decides which schemas unqualified…