Top 40 PostgreSQL Interview Questions & Answers
Top 40 PostgreSQL interview questions and answers: MVCC, JSONB, index types, EXPLAIN ANALYZE, VACUUM, replication, partitioning and performance tuning.
PostgreSQL Basics
Q1. What is PostgreSQL?
An open-source object-relational database known for standards compliance, reliability and extensibility. It supports advanced types, full-text search, JSON and custom extensions.
Q2. What is the difference between PostgreSQL and MySQL?
PostgreSQL has stricter SQL compliance, richer types such as arrays and JSONB, more index types, and powerful extensions. MySQL is simpler to run and long favoured for read-heavy web apps. Both are fast and production-ready.
Q3. What is psql?
PostgreSQL's command-line client. Meta-commands such as \l list databases, \dt lists tables and \d table describes a table.
Q4. What is the difference between a database and a schema in PostgreSQL?
A database is a separate container you connect to. A schema is a namespace inside a database, such as public, that groups tables. One query can use several schemas but not several databases.
Q5. What is SERIAL, and what replaces it?
SERIAL creates an auto-incrementing integer backed by a sequence. The SQL-standard replacement is GENERATED ALWAYS AS IDENTITY, which is now recommended.
Q6. What is the difference between JSON and JSONB?
JSON stores the exact text. JSONB stores a parsed binary form that is faster to query and can be indexed with GIN, but does not keep key order or duplicate keys. Use JSONB in most cases.
Q7. How do you query JSONB data?
-> returns a JSON value, ->> returns text, @> checks containment, and ? checks whether a key exists. For example: WHERE data->>'status' = 'active'.
Q8. What are arrays in PostgreSQL?
Columns can hold arrays such as text[]. You query them with ANY, @> and unnest(), and index them with GIN.
Q9. What does UPSERT look like in PostgreSQL?
INSERT ... ON CONFLICT (email) DO UPDATE SET name = EXCLUDED.name inserts a row or updates it if the unique key already exists. DO NOTHING skips it instead.
Q10. What does RETURNING do?
It returns columns from rows affected by INSERT, UPDATE or DELETE, such as the new id, which saves a second query.
MVCC, Transactions & VACUUM
Q11. What is MVCC?
Multi-Version Concurrency Control: an update writes a new version of the row instead of overwriting it. Readers see a consistent snapshot without blocking writers, and writers do not block readers.
Q12. What is VACUUM?
A process that reclaims space from dead row versions left by updates and deletes, updates planner statistics with ANALYZE, and prevents transaction ID wraparound.
Q13. What is autovacuum?
A background process that runs VACUUM and ANALYZE automatically when enough rows change. It should stay on, and busy tables may need tuned thresholds.
Q14. What is the difference between VACUUM and VACUUM FULL?
VACUUM marks dead space for reuse without locking the table. VACUUM FULL rewrites the table to return space to the OS but takes an exclusive lock.
Q15. What is table bloat?
Space used by dead rows that has not been reclaimed, which makes tables and indexes larger and slower. It grows when autovacuum cannot keep up or long transactions hold old snapshots.
Q16. What is transaction ID wraparound?
Transaction IDs are 32-bit and eventually wrap. VACUUM freezes old rows to prevent this, and if it falls far behind, PostgreSQL stops accepting writes to protect data.
Q17. What isolation levels does PostgreSQL support?
Read Committed (the default), Repeatable Read and Serializable. Read Uncommitted behaves like Read Committed. Serializable uses SSI and may abort transactions, which must be retried.
Q18. What is the WAL?
The Write-Ahead Log: every change is written to the log before the data files. It provides crash recovery, point-in-time recovery and replication.
Q19. What are advisory locks?
Application-defined locks taken with functions like pg_advisory_lock(id). They coordinate work between processes, such as making sure a job runs only once.
Q20. What does SELECT ... FOR UPDATE SKIP LOCKED do?
It locks the rows it reads and skips rows already locked by others. This lets several workers take different jobs from a queue table without blocking each other.
Indexes & Query Performance
Q21. What index types does PostgreSQL have?
B-tree (the default, for equality and ranges), Hash, GIN (for JSONB, arrays and full-text search), GiST (for geometry and ranges), SP-GiST and BRIN (for very large, naturally ordered tables).
Q22. What is a partial index?
An index with a WHERE clause, such as CREATE INDEX ON orders (created_at) WHERE status = 'pending'. It is smaller and faster when queries target that subset.
Q23. What is an expression index?
An index on the result of an expression, such as CREATE INDEX ON users (LOWER(email)), so queries that filter on LOWER(email) can use it.
Q24. What is a covering index?
An index that includes extra columns with INCLUDE, so a query can be answered from the index alone with an index-only scan.
Q25. What is the difference between EXPLAIN and EXPLAIN ANALYZE?
EXPLAIN shows the planned execution and estimated costs. EXPLAIN ANALYZE runs the query and shows real times and row counts, which reveals bad estimates.
Q26. What is the difference between a sequential scan and an index scan?
A sequential scan reads the whole table, which is efficient when many rows match. An index scan uses an index to find few rows. The planner picks based on statistics.
Q27. How do you create an index without locking the table?
Use CREATE INDEX CONCURRENTLY, which allows writes during the build. It takes longer and cannot run inside a transaction.
Q28. What is pg_stat_statements?
An extension that records statistics for every query, such as calls, total time and rows. It is the main tool for finding the slowest queries.
Q29. Why use connection pooling with PostgreSQL?
Each connection is a separate server process using memory. A pooler such as PgBouncer shares a few connections among many clients, which matters for serverless and busy apps.
Q30. How does full-text search work in PostgreSQL?
Text is converted to a tsvector of normalised words, queries to a tsquery, and matched with @@. A GIN index on the tsvector makes it fast.
Replication, Partitioning & Operations
Q31. What is streaming replication?
Replicas receive the primary's WAL in near real time and replay it, giving read-only standbys for high availability and read scaling.
Q32. What is the difference between synchronous and asynchronous replication?
With synchronous replication, a commit waits until a replica confirms it, so no data is lost on failover but writes are slower. Asynchronous replication is faster but can lose the last commits.
Q33. What is logical replication?
Replication of row changes for chosen tables through publications and subscriptions. It works across major versions and suits upgrades and syncing data to other systems.
Q34. What is table partitioning?
Splitting a large table into smaller tables by range, list or hash, such as one partition per month. Queries scan only relevant partitions, and old data can be dropped quickly.
Q35. How do you back up PostgreSQL?
pg_dump creates logical backups of a database. For large systems, take physical base backups and archive WAL with tools such as pgBackRest, which allows point-in-time recovery.
Q36. What is point-in-time recovery?
Restoring a base backup and replaying archived WAL up to a chosen moment, such as just before an accidental DELETE.
Q37. What are extensions in PostgreSQL?
Add-ons installed with CREATE EXTENSION, such as pg_stat_statements, PostGIS for geospatial data, pgvector for vector search, and pg_trgm for fuzzy text matching.
Q38. What memory settings matter most?
shared_buffers (often about 25% of RAM), work_mem for sorts and hashes per operation, maintenance_work_mem for VACUUM and index builds, and effective_cache_size as a hint to the planner.
Q39. What is a CTE in PostgreSQL, and is it an optimisation fence?
A WITH query. Since PostgreSQL 12, simple CTEs are inlined into the main query. Add MATERIALIZED to force computing it once.
Q40. How do you find blocking queries?
Query pg_stat_activity for long-running or waiting sessions, and use pg_blocking_pids(pid) to see which sessions block them.