Database Performance Diagnostics: A Practical Guide to the Four-Component Toolkit
This article walks through a practical, five-step workflow for diagnosing database performance problems using a four-component toolkit built around sampling, comparison, session reconstruction, and prescriptions. It explains why point-in-time tools fail once the incident is over, which configuration parameters must be checked first, how to preload the required libraries, how to configure sampling and create the extension, and how to generate and read snapshot reports. It also covers differential analysis, blocking-session reconstruction, a symptom-to-action mapping table, and common pitfalls to avoid.
1. Why the “Four Components”: A Closed Loop for Performance Diagnosis
The most common frustration in database performance troubleshooting is that the scene is already gone. The business team reports slow responses in the morning, but by the time you log in during the afternoon, the load has subsided and every active session, wait event, and execution plan has been rewritten. At that point no single-point tool can help. The only viable path is to rely on periodic, replayable sampled data.
Mature performance diagnosis systems in the industry usually build a closed loop around four actions:
- Sampling: capture system load and performance snapshots on a fixed interval, forming a replayable historical baseline.
- Comparison: diff two snapshots from different points in time, converting “cumulative values” into “interval deltas” to answer “what actually happened during this window”.
- Reconstruction: rebuild the session and execution context at the moment of failure, so you can see who was running what and where it was stuck.
- Prescription: use rules and models to output optimization advice, turning data conclusions into executable actions.
These four actions map to four diagnostic components. They do not replace one another; they have a strict sequential dependency. The sampling component is the foundation, the comparison component depends on two snapshots, the reconstruction component depends on session history, and the prescription component depends on the structured data produced by the first three. Understanding this dependency chain is the key to avoiding the “installed the tools but still can’t find the problem” trap.
2. Read This Before You Start: An Easy Parameter to Get Wrong
All four components rely on background worker processes to run collection and aggregation tasks, so the maximum number of background worker processes must be confirmed first. This parameter determines how many auxiliary processes the system can host at the same time. If it is too low, collection processes cannot even be launched, which shows up as “the extension is created successfully but snapshots are always empty”.
Two configuration principles apply:
- Cluster architecture: the default value is usually sufficient (default is 8) and should not be lowered. If it must be changed, set it above the default, otherwise replicas may fail to start.
- Standalone architecture: the value must be strictly greater than 4. A value of 6 or higher is recommended, otherwise sampling-related processes cannot start and no snapshots are generated.
Example configuration snippet:
# Recommended value for standalone environments; must be strictly greater than 4max_worker_processes = 6
The instance must be restarted for the change to take effect. Always validate in a test environment before restarting, and avoid changing production directly, which can break connections.
3. Step 1: Load the Dependency Components
Diagnostic capability depends on two foundational abilities, SQL parsing and statistics, so both must be added to the preload library list used at instance startup. The key point is to append, not overwrite, and to keep the existing content:
shared_preload_libraries = '...,liboracle_parser'
The goal of the verification command is not to “see one line”, but to confirm that the output contains all three keyword groups: parser, sampling, and statistics:
grep -v '^#' /data/kingbase.conf | grep -Ei 'parser|kwr|statements'
If any of the three is missing from the output, the append position was wrong or the original value was overwritten, and you need to roll back and redo it. A failure at this step is the most common root cause of every downstream feature being unavailable.
4. Step 2: Configure Sampling Parameters and Create the Extension
The behavior of the sampling component is controlled by a set of parameters, mainly four categories:
- Sampling interval: the gap between two snapshots. A shorter interval gives finer data but increases storage and overhead.
- Retention count: how many historical snapshots are kept, which determines the time window you can look back over.
- Collection switches: whether to collect execution plans, wait events, statistics deltas, and so on.
- Storage location: the on-disk path for snapshot data and the archiving policy.
Once the parameters are in place, create the extension to activate the capability. Once the extension is created successfully, the comparison, reconstruction, and prescription functions become available at the same time, with no need to install them separately. Example:
CREATE EXTENSION sys_kwr;
5. Step 3: Generate a Snapshot and Read the Report
Generate one snapshot manually to establish a baseline:
SELECT sys_kwr_snapshot();
Report generation requires the start and end snapshot IDs, and the output can be text or HTML:
SELECT sys_kwr_report(1, 2, 'html');
When reading the report, follow a fixed order to avoid getting lost in details:
- Start with the time range and load overview to confirm the sampling window covers the failure moment.
- Then look at the top N wait events to judge whether the bottleneck is CPU, IO, locks, or network.
- Next, review the SQL ranking by elapsed time to separate “slow single executions” from “high cumulative time caused by many executions”.
- Finally, check execution plan changes to confirm whether stale statistics caused plan degradation.
6. Step 4: Differential Comparison and Session Reconstruction
Cumulative values have no diagnostic value on their own. The delta between two snapshots is the actual evidence. The comparison report outputs newly added parse counts, execution counts, logical reads, physical reads, rows sorted, and so on within the interval, helping you narrow “the whole system got slower” down to specific SQL fingerprints.
Session reconstruction answers “who was blocking whom at that moment”. It rebuilds the session state, wait chain, and blocking source from historical session records. When using it, locate the blocking session first, then follow the wait chain to the root node, rather than starting from the slowest SQL. A blocked session often looks like the slowest one but is not the root cause.
7. Step 5: From Data to Prescription
The prescription stage maps metrics to actions. Common mappings:
| Symptom | Common root cause | Action direction |
|---|---|---|
| Logical reads far exceed physical reads | Missing or poorly selective indexes | Add or rebuild indexes |
| Sudden spike in physical reads | Lower cache hit ratio | Add memory, optimize large table scans |
| Concentrated lock waits | Long transactions, hot-row updates | Shorten transactions, split hotspots |
| Sudden plan change | Stale statistics | Re-collect statistics |
Note that a prescription is a hypothesis. Cross-validate it with the comparison report and reconstruction data before acting, and never draw conclusions from a single report alone.
8. Implementation Advice and Common Pitfalls
- Match the sampling interval and retention count to available disk capacity; estimate first, then enable.
- Walk through the entire workflow in a test environment before changing parameters.
- Diagnostic conclusions must be reproducible; keep the snapshot IDs and report files.
- Common pitfalls include: the preload library list being overwritten, too few background processes, looking only at cumulative values instead of deltas, and treating a blocked session as the root cause.
Master the sampling-comparison-reconstruction-prescription chain, and performance troubleshooting turns from luck-dependent guessing into an evidence-based, replayable, verifiable engineering process.
