Interview Q&A / PostgreSQL

INTERVIEW Q&A · 69 QUESTIONS

PostgreSQL interview questions

Indexes, EXPLAIN, transactions and isolation, JSONB and the Postgres specifics backend interviews probe.

  1. How do you add a foreign key or CHECK constraint to a huge table without a long lock?

    Add it as NOT VALID, which skips the check of existing rows and only needs a brief lock, then run VALIDATE CONSTRAINT separately. Validation scans the table but only takes a weaker lock, so writes continue.

    AdvancedQuick answer1 min read
  2. How do you stop two bookings from overlapping in PostgreSQL?

    Use an exclusion constraint with a range column and the overlap operator && on a GiST index. The database then rejects any row whose range overlaps an existing row for the same resource.

    AdvancedQuick answer1 min read
  3. What is a DEFERRABLE constraint in PostgreSQL and when is it needed?

    A deferrable constraint can be checked at COMMIT instead of after each statement. This lets you insert rows that reference each other, or swap unique values, as long as everything is consistent by the end of the transaction.

    AdvancedQuick answer1 min read