Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

PostgreSQL in Depth Module 14

Applications, scaling and what comes next interview questions

Interview questions on applications, scaling and what comes next: warm-ups, then what each harder question tests and how a good answer starts.

7 questions: 2 warm-ups and 5 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    Why does a COPY of many rows beat INSERT statements sent one at a time?

    Answered in the lesson From app code: COPY, pipelining, RETURNING

  2. Mid

    Why do parameters stop SQL injection when escaping quotes by hand does not?

    Answered in the lesson From app code: COPY, pipelining, RETURNING

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    A sync job loads 50,000 rows with COPY in one transaction every 10 minutes. Once a day the network drops after the job has sent COMMIT and before the answer arrives, and the job retries the whole batch. Some days the table ends up with 100,000 rows. How do you make the retry safe?

    What it tests
    Whether you know that a lost answer leaves the commit unknown, and can design an idempotent load with a batch identity, natural keys and a check before retrying.
    First step
    State the problem first: after a lost answer the client cannot know whether COMMIT happened. So the retry must be harmless either way. Give every batch an id the source can repeat, and make the load record that id in the same transaction as the rows.
    Follow-ups

    From the lesson From app code: COPY, pipelining, RETURNING

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Senior to staff

    A monthly COPY of a 40 GB CSV file into a table with six indexes and two foreign keys takes 9 hours on a server that otherwise sits idle that night. The business wants it under 2 hours. What do you change, in which order, and what does each change give up?

    What it tests
    Whether you can tell the costs of a big load apart (index maintenance, constraint checks, WAL, checkpoints) and pick changes from the manual's advice on populating a database, with their risks.
    First step
    Measure where the 9 hours go before changing anything: load a sample into the table as it is and into a copy without indexes and foreign keys, and compare the time and the WAL written. That tells you whether index maintenance, constraint checks or WAL volume dominates.
    Follow-ups

    From the lesson From app code: COPY, pipelining, RETURNING

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    A shop moved all reads to two streaming replicas. Now some customers add an item to the cart, land on the cart page and see it empty; a reload a second later shows the item. Replication lag is usually under 200 ms. What is happening, and how do you fix it without giving up the replicas?

    What it tests
    Whether you recognise a read-your-writes problem caused by replica lag, and can choose between routing to the primary and waiting for the replica, with the cost of each.
    First step
    Confirm the cause: compare the time between the cart write and the cart read with the replicas' replay lag, and check that the empty pages come only from replicas. Then decide which reads must see the user's own write.
    Follow-ups

    From the lesson Scaling PostgreSQL: replicas, caches, shards

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Mid to senior

    A sales dashboard reads a materialized view of totals per store and hour, built from a 60 million row orders table. A job runs REFRESH MATERIALIZED VIEW every 5 minutes, and dashboard requests time out for about 40 seconds each time. What do you change, and what does each option cost?

    What it tests
    Whether you know what a plain refresh locks, what a concurrent refresh needs and what it costs, and when incremental maintenance or a summary table kept by the application fits better.
    First step
    Check what the dashboard waits for while the refresh runs, in pg_stat_activity and pg_locks, and how long the refresh itself takes. Then compare how often the totals change with how often the dashboard reads them.
    Follow-ups

    From the lesson Scaling PostgreSQL: replicas, caches, shards

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Senior to staff

    An invoicing service holds 4 TB for 30,000 tenants on one tuned PostgreSQL primary at 85 % CPU, mostly from writes. Five tenants produce 35 % of the writes, and finance runs reports across all tenants each month. Plan the move to several servers.

    What it tests
    Whether you can choose a shard key, route queries, plan for big tenants, cross-tenant reports and transactions, and migrate without downtime.
    First step
    Confirm that the writes are the limit and that the statements are already tuned, from pg_stat_statements and the server's own metrics. Then check that nearly every table and query carries the tenant id, because that decides whether the tenant can be the shard key.
    Follow-ups

    From the lesson Scaling PostgreSQL: replicas, caches, shards

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All PostgreSQL in Depth interview questions · The PostgreSQL in Depth track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.