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 →
- Confirm with Performance Insights or
pg_stat_activitythat 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;
- Quick fix: point reporting tools at a read replica.
- Protect the primary: a separate DB user for reporting with
statement_timeout, and connection limits. - 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.
More on Data and databases
A release needs to rename a column that the running app reads and writes. How do you do it with zero downtime?IntermediateHow do you prove your database backups actually work?IntermediateDesign backups for an RDS PostgreSQL database with RPO of 5 minutes and RTO of 1 hour, including a region failure.AdvancedYour migrations run in an init container and two pods ran them at the same time during a deploy, leaving the schema half-applied. How do you fix the approach?IntermediateUpgrade RDS PostgreSQL from 13 to 17 with minimal downtime. How?AdvancedYou need to add an index and a column to a 500-million-row table that's written to constantly. How do you avoid locking it?Advanced