Quick revision
The short answer
I would first take an inventory of all 150 objects, with owner, consumers, refresh time, cost and last read, and remove the ones nobody uses. Then I would move analytical workloads off the OLTP database to a replica or a CDC-fed analytics platform, convert the most expensive remaining full refreshes to incremental maintenance, and set rules so new heavy aggregates need a review and an owner and all refreshes run under a scheduler with resource limits and alerts. Finally, database CPU is tracked per job so each owning team sees its cost.
What to remember
In this answer, we handled 150 refresh jobs that crowd the OLTP database at night. We protected the morning peak with timeouts and a cutoff, built an inventory with owners, consumers, cost and last read, and dropped unused objects. We moved analytical work to a replica or a CDC-fed analytics platform, converted the most expensive remaining refreshes to incremental maintenance, and set rules: owners and reviews for new heavy aggregates, one scheduler with limits and alerts, and database CPU reported by team.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would first take an inventory of all 150 objects, with owner, consumers, refresh time, cost and last read, and remove the ones nobody uses. Then I would move analytical workloads off the OLTP database to a replica or a CDC-fed analytics platform, convert the most expensive remaining full refreshes to incremental maintenance, and set rules so new heavy aggregates need a review and an owner and all refreshes run under a scheduler with resource limits and alerts. Finally, database CPU is tracked per job so each owning team sees its cost.
Why this question is asked
This is how reporting debt looks after a few years: each materialized view was a reasonable fix for one slow screen, and nobody owned the total. Now the night window is full, a refresh that runs late collides with morning traffic, and customers feel it. Interviewers ask this leadership question to see whether we can clean up a shared resource without stopping the teams that depend on it, and whether we put rules in place so it does not return. In this answer, we will walk through the situation, the inventory, the quick wins, the structural moves, and the governance, with the reasoning for the order.
1Understanding the Situation
A full refresh of a materialized view or summary table recomputes its entire query, so its cost grows with the base tables, not with the amount of new data. With 150 such objects scheduled by separate cron jobs, the night becomes a pile of heavy queries competing for CPU, memory and disk on the same primary that serves orders. As tables grow by, say, 20 percent a year, the total refresh time grows too, until the last jobs spill past the end of the night window into the morning peak, where they slow customer traffic.
The immediate risk is the morning overlap. The long-term problem is that nobody can say which of the 150 objects matter, who owns them, or what they cost. So the plan starts with protecting the morning, then gets visibility, and only then makes bigger changes.
2Protect the Morning First
Before any cleanup, we stop the damage. Refresh jobs that are still running at the start of the peak are a bigger problem than stale reports, so we give the refresh role a statement_timeout and have the scheduler skip jobs that would start after a cutoff time, for example 06:00. A refresh that does not finish leaves yesterday's data in place, which is acceptable for a short period while we fix the cause, and we tell the affected teams which reports may be a day old.
3Inventory: Owner, Consumers, Cost and Last Read
Next we build an inventory from the database catalogs and the logs. pg_matviews lists materialized views; the cron configuration and scheduler logs give each job's schedule and run time; pg_stat_statements gives each refresh query's total execution time, which is a good proxy for CPU; and pg_stat_user_tables shows how often each summary table or materialized view is scanned, which tells us whether anyone reads it. Owners and consumers come from version control history and by asking teams.
The usual result is uncomfortable but useful. In inventories like this, a large share of objects, often a third or more, have no reads in the last month: dashboards that were retired, experiments, duplicates of each other. Those are dropped after a short notice period, with a backup of the definition. That alone can cut the night's work substantially without touching anything anyone uses.
| Object | Owner | Refresh time | Reads in 30 days | Action |
|---|---|---|---|---|
| promo_2024_summary | unknown | 18 min | 0 | drop after notice |
| region_kpi_old | analytics | 25 min | 0 | drop, duplicate of region_kpi |
| sales_by_day | orders | 42 min | 48,211 | convert to incremental |
| finance_monthly | finance | 55 min | 310 | move to warehouse |
| stock_levels | inventory | 1 min | 12,900 | keep, cheap and hot |
Notice that these counters reset when statistics are reset, so we check when that happened, and we combine them with a month of observation before declaring an object unused.
4Move Analytics Off the OLTP Database
Many of the remaining objects serve reporting, not the product. They do not need to live on the primary at all. As a first step, read-heavy reports can run on a replica, though a replica cannot hold materialized views of its own, since it is a read-only copy of the primary, so this works for queries rather than refreshes. The structural move is an analytics platform, a warehouse or analytical database, fed by change data capture, a stream of every committed change read from the database's log. Reports and their aggregates then live there, with tools designed for large scans, and the primary does none of that work.
We migrate by cost: the inventory tells us which reporting objects consume the most CPU, and those move first. Each move frees a measurable slice of the night.
5Convert Expensive Full Refreshes to Incremental Maintenance
Objects that must stay close to the product, such as aggregates shown in the application itself, are converted from full refreshes to incremental maintenance. Instead of recomputing three years of orders every night, a job recomputes only the time buckets touched since its last run, using an index on the change timestamp, and upserts the results into a summary table. A refresh that took 42 minutes becomes a run of a few seconds every few minutes, which is also fresher. This takes engineering work per object, so we apply it only to the top expensive objects that are both used and must stay in OLTP.
6Rules So It Does Not Come Back
The cleanup is wasted if the next two years add another 150 objects. So we set a few rules. Every new materialized view or summary table on an OLTP database needs a named owner, a documented consumer and an estimate of its refresh cost, and anything heavier than a threshold, for example more than five minutes per run, needs a short review where the alternatives are an incremental design or the analytics platform. All refreshes run under one scheduler instead of scattered cron jobs, with concurrency limits, a cutoff time, statement timeouts, and alerts on failures and on run times that grow.
Finally, we make cost visible. Each job runs under a database role or application name tagged with its owning team, and a monthly report shows database CPU by team from pg_stat_statements. When a team sees that its reports consume 30 percent of the primary's night, conversations about moving them get easier, and the platform team is no longer the only one who cares.
7Follow-up questions
How do you drop objects safely when owners are unknown?
Announce the list with a date, revoke read access first or rename the object so any remaining user gets a clear error that can be reversed in seconds, wait a few weeks, then drop it while keeping the definition in version control so it can be recreated.
Why not just buy a bigger database server?
It buys months, not years, because full refresh cost grows with the data, and it leaves the morning overlap risk in place. It can be a sensible stopgap while the inventory and moves happen, but not the plan.
Who should own the scheduler?
The platform or data team owns the scheduler, its limits and alerts; each job is owned by the team that consumes its output. That split keeps the shared rules consistent while costs and failures land with the people who can act on them.
8Conclusion
In this answer, we handled 150 refresh jobs that crowd the OLTP database at night. We protected the morning peak with timeouts and a cutoff, built an inventory with owners, consumers, cost and last read, and dropped unused objects. We moved analytical work to a replica or a CDC-fed analytics platform, converted the most expensive remaining refreshes to incremental maintenance, and set rules: owners and reviews for new heavy aggregates, one scheduler with limits and alerts, and database CPU reported by team.
Open with
My first concern would be the morning overlap, because that hurts customers. So I would immediately put a statement timeout on the refresh role and a cutoff in the scheduler, so no refresh starts after a fixed time and anything still running at peak is cancelled; a report that is a day old is better than a slow checkout.
Then, if they want more
Then I would build an inventory of all 150 objects: owner, consumers, schedule, refresh time from the scheduler logs and pg_stat_statements, and reads from pg_stat_user_tables over a month. In my experience a good share will have no readers at all, and those can be revoked and then dropped with notice, which frees a lot of the night quickly.
For the rest I would decide per object. Reporting aggregates move off the OLTP database to an analytics platform fed by CDC, starting with the most expensive. Aggregates the product needs stay, but the expensive ones get converted from full refreshes to incremental jobs that only recompute recently changed buckets. Cheap, hot ones stay as they are.
To stop it coming back, new heavy aggregates need an owner and a short review, all refreshes run under one scheduler with concurrency limits, timeouts and alerts, and database CPU is reported per team every month so cost is visible to the owners.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Over time, teams created 150 materialized views and summary tables refreshed by cron...": https://scrivio-interview-library.netlify.app/databases/stored-code/d5ec8c5c13/