Quick revision
The short answer
I would make pg_stat_statements and performance_schema, with sensible slow-log thresholds, a platform default on all 60 databases, and ship periodic snapshots to a central store, because the in-database counters are cumulative and reset, so trends and regressions only become visible over time. Each query is linked to its service through application_name, SQL comments such as sqlcommenter, or tracing spans. Teams get dashboards and alerts on top queries by total time and on new queries entering the top list after deployments, and because query text and logged parameters can hold personal data, we normalise and restrict access.
What to remember
In this answer, we designed fleet-wide query performance data for 60 databases. Every database gets pg_stat_statements or performance_schema and slow-log thresholds by default, a collector ships snapshots centrally because the counters are cumulative and reset, and queries are linked to services through application_name, sqlcommenter comments and tracing. Teams get dashboards and alerts on top queries by total time and on new top queries after deployments, and normalised text, masking and restricted access protect the personal data that query text and parameters can contain.
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 make pg_stat_statements and performance_schema, with sensible slow-log thresholds, a platform default on all 60 databases, and ship periodic snapshots to a central store, because the in-database counters are cumulative and reset, so trends and regressions only become visible over time. Each query is linked to its service through application_name, SQL comments such as sqlcommenter, or tracing spans. Teams get dashboards and alerts on top queries by total time and on new queries entering the top list after deployments, and because query text and logged parameters can hold personal data, we normalise and restrict access.
Why this question is asked
With 60 databases owned by different teams, nobody can log in to each one and look at query statistics, and each team setting up its own tools leads to gaps exactly where incidents happen. A platform approach makes the data the same everywhere and useful to the people who own the code. Interviewers ask this design question to see whether we understand what the native tools provide, what they do not, and how to turn them into a system people use. In this answer, we will set the defaults on every database, design the central collection of snapshots, link queries to services, decide what teams see and are alerted on, and handle the privacy side.
1Platform Defaults on Every Database
The base layer is that every database, PostgreSQL or MySQL, is created with the same instrumentation, from the platform's templates or infrastructure code, so no team has to remember it. For PostgreSQL that means pg_stat_statements in shared_preload_libraries and created as an extension, track_io_timing on where the clock is cheap, log_min_duration_statement at a threshold such as 500 ms, and auto_explain for statements above a higher threshold, with timing off and sampling. For MySQL it means performance_schema on with statement digests enabled, the slow query log on with long_query_time around 0.5 seconds, and a digest table large enough not to lose entries.
Defaults matter because the most valuable data is the data from before an incident. A team that enables pg_stat_statements after its database got slow has lost the baseline it needs to see what changed.
2Why Central Snapshots Are Needed
The in-database statistics have two limits. They are cumulative: total_exec_time is the sum since the last reset, so it cannot tell us whether a query got slower yesterday. And they are volatile: a restart, a failover to a replica, a manual reset or eviction of entries when the view is full loses them. So a collector reads them from every database at a fixed interval, say every 5 minutes, and ships the snapshots to a central time-series or analytical store. The difference between two snapshots gives calls, time, rows and blocks per interval for each query, and those intervals can be kept for months.
- 60 databases, platform defaultspg_stat_statements, performance_schema, slow logs
- Collector every 5 minutesreads counters, computes deltas, handles resets
- Central storemonths of per-query history, normalised text only
- Service mappingapplication_name, sqlcommenter tags, traces
- Team dashboards and alertstop queries by total time, new entrants after deploys
5 layers, from the top down. Step through them one at a time.
queryid identifies a normalised statement in PostgreSQL, and the digest plays the same role in MySQL. The collector must handle resets, where counters go back to zero, and entries that disappear, so deltas never go negative.
3Linking Queries to Services
A row that says SELECT ... FROM invoices WHERE ... costs 3 hours a day is only useful if we know which service and which code sends it. When several services share a database user, the statistics alone cannot say. There are three ways to add that link. Every service sets application_name in its connection string, which appears in pg_stat_activity and in logs. SQL comments carry richer tags: sqlcommenter libraries add a comment such as /* service='billing', route='/invoices' */ to each statement, which shows up in logs and slow query entries. And distributed tracing records each query as a span inside the request that issued it, linking the database cost to an endpoint.
One catch: pg_stat_statements computes its identifier from the parsed query, which ignores comments, so statements that differ only in their comment are counted as one entry, and the stored text shows the comment of whichever execution was recorded first. The aggregate view therefore cannot be split reliably by comment tags; application_name, logs and traces are the reliable place for per-request tags.
4What Teams See and Get Alerted On
Each team gets a dashboard of its own databases: top queries by total time, by calls and by mean time, blocks per call, and trends over weeks. Total time comes first because it shows where capacity goes. Two alerts carry most of the value. A regression alert fires when a top query's mean time or blocks per call rises sharply compared with its baseline, often after a deployment or a statistics change. A new entrant alert fires when a query that was not in the top list appears there after a deployment, which catches new N+1 patterns and missing indexes within hours instead of weeks.
Alerts go to the owning team, not to a central DBA group, with a link to the query's history and its latest plan from auto_explain. A small platform team maintains the system and helps with hard cases, but the teams own their queries.
- Team deploys a new version
- Collector compares next intervals with baselineper query: total time, mean, blocks per call
- New query in top 10 or mean up 3x?YesAlert owning team with plan and historyNoNo action
- Team fixes or acceptsacceptance recorded so it does not alert again
4 steps in order. Play them, or step through them one at a time.
5Privacy and Access
Query performance data is not as harmless as it looks. Normalised text in pg_stat_statements replaces constants with placeholders, but slow query logs, auto_explain output and logged parameters can contain email addresses, names or account numbers in literal values. So the collector stores only normalised text, the pipeline strips or masks literals from logs before shipping them, parameter logging is off by default, and access to raw logs is restricted to the owning team and the platform team, with retention limits. Plans logged by auto_explain may show literal values in conditions, so they get the same masking. That keeps the system useful without turning it into an uncontrolled copy of production data.
6Follow-up questions
How do you handle the difference between PostgreSQL and MySQL in one system?
The collector has a small adapter per engine that maps pg_stat_statements columns and performance_schema digest columns onto a common model: database, query identifier, normalised text, calls, total time, rows and blocks or rows examined. Dashboards and alerts work on that common model, with engine-specific details available in a second view.
Why not rely on the cloud provider's built-in query insights?
They are useful and may be the collector's source where available, but with 60 databases across engines and possibly providers, we want one consistent model, longer retention, our own service mapping and alert rules tied to deployments. Using provider tools as inputs to the central system gets the best of both.
How do you avoid alert fatigue?
By alerting only on large relative changes for queries that matter by total time, requiring the change to persist over several intervals, letting teams acknowledge expected changes, and reviewing which alerts led to action every few months.
7Conclusion
In this answer, we designed fleet-wide query performance data for 60 databases. Every database gets pg_stat_statements or performance_schema and slow-log thresholds by default, a collector ships snapshots centrally because the counters are cumulative and reset, and queries are linked to services through application_name, sqlcommenter comments and tracing. Teams get dashboards and alerts on top queries by total time and on new top queries after deployments, and normalised text, masking and restricted access protect the personal data that query text and parameters can contain.
Open with
I would treat it as a platform product with four parts. First, defaults: every PostgreSQL database is created with pg_stat_statements, auto_explain with sampling and a sensible log_min_duration_statement, and every MySQL database with performance_schema statement digests and the slow log at around half a second.
Then, if they want more
Defaults matter because the baseline from before an incident is the most valuable data. Second, central collection. The in-database counters are cumulative and are lost on restarts, failovers and resets, so a collector snapshots them every few minutes from all 60 databases into a central store, computes deltas and handles resets.
That gives months of history per query, so we can see trends and regressions. Third, ownership: queries must be linked to services, so every service sets application_name, we add sqlcommenter comments with service and route, and tracing shows queries inside requests.
Fourth, what teams see: dashboards of their top queries by total time, calls and blocks per call, and two main alerts, a regression alert when a top query's mean time or blocks per call jumps, and a new entrant alert when a query enters the top list after a deployment, which catches N+1 patterns quickly.
Alerts go to the owning team with the query's plan and history. Finally, privacy: query text and logged parameters can contain personal data, so we store only normalised text, mask literals in logs and plans, keep parameter logging off and restrict access and retention.
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 "Design how query performance data is collected and used across 60 PostgreSQL and MySQL...": https://scrivio-interview-library.netlify.app/databases/query-optimization/59e4d8269d/