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.

  1. Most asked How do you read EXPLAIN ANALYZE output in PostgreSQL?

    Read the plan tree from the innermost nodes outward. For each node compare estimated rows with actual rows, look at actual time (per loop, multiplied by loops), and add BUFFERS to see how many pages…

  2. Most asked How do you build a job queue on PostgreSQL with SELECT ... FOR UPDATE SKIP LOCKED?

    Workers select queued rows with FOR UPDATE SKIP LOCKED, which locks the rows they take and skips rows other workers have locked, so many workers can claim jobs concurrently without blocking or…

  3. Most asked How do you prevent lost updates in PostgreSQL under READ COMMITTED?

    Do not read a value, compute in the application and write it back. Use an atomic statement (UPDATE accounts SET balance = balance - $1 WHERE id = $2 AND balance >= $1), lock the row first with…

  4. How do you find the queries that cost the most in PostgreSQL?

    Enable pg_stat_statements and rank normalised queries by total_exec_time (and look at mean_exec_time, calls, rows, shared block reads). Use auto_explain to log plans of slow queries, and…

  5. How do you make LIKE '%term%' searches fast in PostgreSQL?

    Use the pg_trgm extension with a GIN (or GiST) trigram index: CREATE INDEX ON products USING gin (name gin_trgm_ops);. It accelerates LIKE/ILIKE with leading wildcards, regular expressions, and…

  6. How should you store money in PostgreSQL?

    Use numeric(p, s) for exact decimal arithmetic, or bigint in minor units (cents). Avoid real/double precision (binary floating point) and avoid the money type, which depends on the lc_monetary…

  7. How do you detect and fix table and index bloat?

    Estimate bloat with the pgstattuple extension or community bloat queries, and watch table and index sizes against row counts. Fix the cause (autovacuum not keeping up, long transactions), then…

  8. How do you implement efficient pagination in PostgreSQL?

    Use keyset (seek) pagination: WHERE (created_at, id) < ($1, $2) ORDER BY created_at DESC, id DESC LIMIT 20 with an index on (created_at, id). PostgreSQL supports row-value comparisons, which makes…

  9. How do you upgrade PostgreSQL to a new major version?

    Options: pg_dump/pg_restore (simple, long downtime for big databases), pg_upgrade (in place; with --link downtime is minutes regardless of size), or logical replication to a new-version cluster…

Was this page helpful?

Report a mistake