Java Backend Interview Q&A← All stacks

PostgreSQL — Interview Q&A Matrix

Parent stack: Persistence & Caching Technology: PostgreSQL Levels: Junior · Senior · Architect Questions: 4

Each entry contains a Question, the Expected Depth, an Ideal Answer, and Red Flags.

Junior

Q1 — Indexes: what, why, and the cost

Question: What is a database index, why does it speed up reads, and what does it cost?

Expected Depth:

Ideal Answer:

sql
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at DESC);
sql
CREATE INDEX idx_orders_unpaid
ON orders (created_at)
WHERE paid = false;

Red Flags:


Senior

Q2 — Reading EXPLAIN ANALYZE and fixing a slow query

Question: A query that filters and sorts is slow. Walk through diagnosing it with EXPLAIN ANALYZE in PostgreSQL and the fixes you'd consider.

Expected Depth:

Ideal Answer:

sql
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, total
FROM orders
WHERE customer_id = 42 AND status = 'PAID'
ORDER BY created_at DESC
LIMIT 20;
sql
CREATE INDEX idx_orders_cust_status_created
ON orders (customer_id, status, created_at DESC)
INCLUDE (id, total);
sql
CREATE INDEX idx_users_lower_email
ON users (lower(email));

Red Flags:


Q3 — Connection pooling (HikariCP) and pool sizing

Question: Under load the app throws "connection is not available" timeouts. Explain connection pooling and how you'd size the pool.

Expected Depth:

Ideal Answer:

properties
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.connection-timeout=3000
spring.datasource.hikari.validation-timeout=1000
spring.datasource.hikari.max-lifetime=1700000
spring.datasource.hikari.leak-detection-threshold=20000
sql
SET statement_timeout = '2s';
java
try (Connection connection = dataSource.getConnection();
   PreparedStatement statement = connection.prepareStatement(sql)) {
  statement.setLong(1, customerId);
  try (ResultSet rows = statement.executeQuery()) {
    // map rows
  }
}

Red Flags:


Architect

Q4 — Database sharding and partitioning strategy

Question: A single PostgreSQL primary can no longer hold the write volume or data size. As architect, design a horizontal scaling strategy and its trade-offs.

Expected Depth:

Ideal Answer:

sql
CREATE TABLE events (
    id bigint NOT NULL,
    created_at timestamptz NOT NULL,
    payload jsonb NOT NULL
) PARTITION BY RANGE (created_at);

CREATE TABLE events_2026_08 PARTITION OF events
FOR VALUES FROM ('2026-08-01') TO ('2026-09-01');

Red Flags:


Scoring Rubric

Level Pass bar
JuniorExplains index benefits and costs and chooses basic indexes correctly.
SeniorUses plans and metrics to tune queries, pools, and transactions safely.
ArchitectDesigns scalable PostgreSQL topology with explicit consistency and operational trade-offs.