Long-running analytics queries on the primary database slow down the customer-facing app. What do you do?

BeginnerScenarioData and databases
Answer

Situation read → Reporting and transactional workloads are competing for the same CPU, memory and I/O. Separate them.

What I'd do →

  1. Confirm with Performance Insights or pg_stat_activity that the slow periods line up with the reporting queries.
SELECT pid, now() - query_start AS runtime, usename, left(query, 80)
FROM pg_stat_activity WHERE state = 'active' ORDER BY runtime DESC LIMIT 10;
  1. Quick fix: point reporting tools at a read replica.
  2. Protect the primary: a separate DB user for reporting with statement_timeout, and connection limits.
  3. Long term: export data to a warehouse (Redshift, BigQuery, Snowflake) or S3 + Athena via CDC, so analytics never touch the OLTP database.

Why → Isolation means a bad report can slow a report, not checkout.

Interview tip → Note that heavy queries on a PostgreSQL replica can cause replica lag or cancelled queries; it's not a free lunch.

All Real-World Interview questions Roadmap: Interview readiness

More on Data and databases