@tabularis/explain 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +104 -0
- package/dist/diagnostics-DPSgsjFk.d.ts +157 -0
- package/dist/flow.d.ts +40 -0
- package/dist/flow.js +171 -0
- package/dist/index.d.ts +325 -0
- package/dist/index.js +1792 -0
- package/dist/react.d.ts +91 -0
- package/dist/react.js +1617 -0
- package/package.json +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Andrea Debernardi
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# @tabularis/explain
|
|
2
|
+
|
|
3
|
+
Analyse and visualise a database EXPLAIN plan.
|
|
4
|
+
|
|
5
|
+
Takes a plan that has *already* been produced and derives everything from it:
|
|
6
|
+
per-node exclusive metrics, diagnostics, statistics, and React views. It never
|
|
7
|
+
connects to a database, builds an `EXPLAIN` statement, or runs a query.
|
|
8
|
+
|
|
9
|
+
## Entry points
|
|
10
|
+
|
|
11
|
+
| Import | Contains | Runtime deps |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `@tabularis/explain` | plan types, metrics, diagnostics, stats, formatters, tree helpers | **none** |
|
|
14
|
+
| `@tabularis/explain/react` | the views: graph, table, diagram, stats, node details, bars | react, react-i18next, @xyflow/react, dagre, clsx, lucide-react |
|
|
15
|
+
| `@tabularis/explain/flow` | ReactFlow node/edge adapter + dagre layout | @xyflow/react, dagre |
|
|
16
|
+
|
|
17
|
+
The core entry point has **no runtime dependencies at all**, so plan analysis can
|
|
18
|
+
run anywhere — a browser, a worker, a Node script, a test.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import {
|
|
22
|
+
computeExplainMetrics,
|
|
23
|
+
getPlanDiagnostics,
|
|
24
|
+
getExplainPlanSummary,
|
|
25
|
+
} from "@tabularis/explain";
|
|
26
|
+
|
|
27
|
+
const metrics = computeExplainMetrics(plan);
|
|
28
|
+
const diagnostics = getPlanDiagnostics(plan, metrics);
|
|
29
|
+
const summary = getExplainPlanSummary(plan, metrics);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { ExplainGraph, ExplainTableView } from "@tabularis/explain/react";
|
|
34
|
+
|
|
35
|
+
<ExplainGraph
|
|
36
|
+
plan={plan}
|
|
37
|
+
metrics={metrics}
|
|
38
|
+
diagnostics={diagnostics}
|
|
39
|
+
selectedNodeId={selectedNodeId}
|
|
40
|
+
onSelectNode={setSelectedNodeId}
|
|
41
|
+
/>;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Where a plan comes from
|
|
45
|
+
|
|
46
|
+
This package never runs a query — it turns *already-produced* EXPLAIN output
|
|
47
|
+
into an `ExplainPlan` and analyses it. The parsers cover Postgres
|
|
48
|
+
`EXPLAIN (FORMAT JSON)` and text output, MySQL/MariaDB `FORMAT=JSON`,
|
|
49
|
+
`EXPLAIN ANALYZE` / `ANALYZE FORMAT=TEXT` trees and decoded tabular rows, and
|
|
50
|
+
SQLite `EXPLAIN QUERY PLAN` rows:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { parseExplain, parseExplainFor, parseRawExplain } from "@tabularis/explain";
|
|
54
|
+
|
|
55
|
+
// Sniff the format of a pasted blob or an uploaded file:
|
|
56
|
+
const plan = parseExplain(raw);
|
|
57
|
+
|
|
58
|
+
// Or name the engine when the caller knows it:
|
|
59
|
+
const hinted = parseExplainFor(raw, "mysql");
|
|
60
|
+
|
|
61
|
+
// Or parse the raw payload a host driver handed over:
|
|
62
|
+
const fromDriver = parseRawExplain({ engine, format, payload, original_query });
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
A host obtains the bytes however it likes — a driver, a pasted textarea, an
|
|
66
|
+
uploaded file — and hands them over untouched. Drivers for engines these
|
|
67
|
+
parsers do not know (plugin drivers) supply a fully-parsed `ExplainPlan`
|
|
68
|
+
instead; both shapes flow through `resolveExplainOutput`.
|
|
69
|
+
|
|
70
|
+
## Host requirements for the React entry point
|
|
71
|
+
|
|
72
|
+
The views are presentational but not self-contained. A host must provide:
|
|
73
|
+
|
|
74
|
+
- **Tailwind**, with the same colour tokens the desktop app uses — components
|
|
75
|
+
emit utility class names rather than scoped CSS;
|
|
76
|
+
- **an initialised `react-i18next` instance** exposing the
|
|
77
|
+
`editor.visualExplain.*` key namespace. The keys deliberately stay with the
|
|
78
|
+
host's translation catalogue rather than being bundled here;
|
|
79
|
+
- **`@xyflow/react`'s stylesheet**, if `ExplainGraph` is used.
|
|
80
|
+
|
|
81
|
+
## Deliberately not included
|
|
82
|
+
|
|
83
|
+
- Running `EXPLAIN`, or deciding whether a statement can be explained
|
|
84
|
+
(`isExplainableQuery` / `isDataModifyingQuery` are host SQL concerns).
|
|
85
|
+
- Any Tauri, IPC or transport call.
|
|
86
|
+
- The AI plan explanation, which is a host feature backed by a host-configured
|
|
87
|
+
provider.
|
|
88
|
+
- The raw-output tab, which the desktop app renders with Monaco and its own
|
|
89
|
+
theme system.
|
|
90
|
+
|
|
91
|
+
The last two are why the desktop app keeps its own `VisualExplainView`
|
|
92
|
+
composition: it wires those host pieces around the components exported here.
|
|
93
|
+
|
|
94
|
+
## Development
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
pnpm --filter @tabularis/explain typecheck
|
|
98
|
+
pnpm --filter @tabularis/explain build # tsup → dist, for publishing
|
|
99
|
+
pnpm vitest run packages/explain # tests run from the repo root
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Workspace consumers resolve the entry points straight to TypeScript source, so
|
|
103
|
+
no build step is needed during development; `publishConfig` swaps in `dist` when
|
|
104
|
+
the package is published.
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
interface ExplainNode {
|
|
2
|
+
id: string;
|
|
3
|
+
node_type: string;
|
|
4
|
+
relation: string | null;
|
|
5
|
+
startup_cost: number | null;
|
|
6
|
+
total_cost: number | null;
|
|
7
|
+
plan_rows: number | null;
|
|
8
|
+
actual_rows: number | null;
|
|
9
|
+
actual_time_ms: number | null;
|
|
10
|
+
actual_loops: number | null;
|
|
11
|
+
buffers_hit: number | null;
|
|
12
|
+
buffers_read: number | null;
|
|
13
|
+
filter: string | null;
|
|
14
|
+
index_condition: string | null;
|
|
15
|
+
join_type: string | null;
|
|
16
|
+
hash_condition: string | null;
|
|
17
|
+
extra: Record<string, unknown>;
|
|
18
|
+
children: ExplainNode[];
|
|
19
|
+
}
|
|
20
|
+
interface ExplainPlan {
|
|
21
|
+
root: ExplainNode;
|
|
22
|
+
planning_time_ms: number | null;
|
|
23
|
+
execution_time_ms: number | null;
|
|
24
|
+
original_query: string;
|
|
25
|
+
driver: string;
|
|
26
|
+
has_analyze_data: boolean;
|
|
27
|
+
raw_output: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Per-node metrics derived from a plan tree.
|
|
32
|
+
*
|
|
33
|
+
* The values reported by the database are *inclusive* and, for timings, are
|
|
34
|
+
* *averages per loop*:
|
|
35
|
+
*
|
|
36
|
+
* - `Total Cost` covers the node and everything below it.
|
|
37
|
+
* - `Actual Total Time` covers the node and everything below it, averaged over
|
|
38
|
+
* `Actual Loops` iterations.
|
|
39
|
+
*
|
|
40
|
+
* Using those numbers directly makes the plan root look like the most expensive
|
|
41
|
+
* and slowest node in every plan, which hides the real bottleneck. The metrics
|
|
42
|
+
* below restate them as totals across loops and as *exclusive* (self) values,
|
|
43
|
+
* which is what the views highlight.
|
|
44
|
+
*/
|
|
45
|
+
interface ExplainNodeMetrics {
|
|
46
|
+
nodeId: string;
|
|
47
|
+
/** 1-based depth-first position. Used as the node label across all views. */
|
|
48
|
+
index: number;
|
|
49
|
+
/** Nesting level, 0 for the plan root. */
|
|
50
|
+
depth: number;
|
|
51
|
+
/** Time in this node and its children, summed over every loop. */
|
|
52
|
+
inclusiveTimeMs: number | null;
|
|
53
|
+
/** Time in this node alone, summed over every loop. */
|
|
54
|
+
exclusiveTimeMs: number | null;
|
|
55
|
+
/** Planner cost of this node and its children. */
|
|
56
|
+
totalCost: number | null;
|
|
57
|
+
/** Planner cost attributable to this node alone. */
|
|
58
|
+
exclusiveCost: number | null;
|
|
59
|
+
/** Rows produced across every loop. */
|
|
60
|
+
totalRows: number | null;
|
|
61
|
+
/** Shared buffers touched by this node and its children. */
|
|
62
|
+
inclusiveBuffers: number | null;
|
|
63
|
+
/** Shared buffers touched by this node alone. */
|
|
64
|
+
exclusiveBuffers: number | null;
|
|
65
|
+
/** `exclusiveTimeMs` as a fraction of the whole plan, 0..1. */
|
|
66
|
+
timeShare: number | null;
|
|
67
|
+
/** The planner produced the node but the executor never ran it. */
|
|
68
|
+
neverExecuted: boolean;
|
|
69
|
+
}
|
|
70
|
+
interface ExplainMetrics {
|
|
71
|
+
byId: Map<string, ExplainNodeMetrics>;
|
|
72
|
+
/** Depth-first order, matching the `index` field. */
|
|
73
|
+
order: ExplainNodeMetrics[];
|
|
74
|
+
maxExclusiveTimeMs: number;
|
|
75
|
+
maxExclusiveCost: number;
|
|
76
|
+
maxTotalRows: number;
|
|
77
|
+
maxExclusiveBuffers: number;
|
|
78
|
+
/** Sum of every node's exclusive time, i.e. the whole plan's execution time. */
|
|
79
|
+
totalExclusiveTimeMs: number;
|
|
80
|
+
}
|
|
81
|
+
/** Metric a bar chart or heat map can be driven by. */
|
|
82
|
+
type ExplainMetricKind = "time" | "rows" | "cost" | "buffers";
|
|
83
|
+
declare const EXPLAIN_METRIC_KINDS: readonly ExplainMetricKind[];
|
|
84
|
+
/** Compute exclusive/total metrics for every node in `plan`. */
|
|
85
|
+
declare function computeExplainMetrics(plan: ExplainPlan): ExplainMetrics;
|
|
86
|
+
/** Metrics for a single node, or `null` when the id is unknown. */
|
|
87
|
+
declare function getNodeMetrics(metrics: ExplainMetrics, nodeId: string | null): ExplainNodeMetrics | null;
|
|
88
|
+
/** The value a given metric contributes for one node. */
|
|
89
|
+
declare function getMetricValue(metrics: ExplainNodeMetrics, kind: ExplainMetricKind): number | null;
|
|
90
|
+
/** The largest value of a given metric across the plan. */
|
|
91
|
+
declare function getMetricMax(metrics: ExplainMetrics, kind: ExplainMetricKind): number;
|
|
92
|
+
/** Whether any node in the plan carries data for a given metric. */
|
|
93
|
+
declare function isMetricAvailable(metrics: ExplainMetrics, kind: ExplainMetricKind): boolean;
|
|
94
|
+
/** Metrics that carry data, in display order. */
|
|
95
|
+
declare function getAvailableMetricKinds(metrics: ExplainMetrics): ExplainMetricKind[];
|
|
96
|
+
/**
|
|
97
|
+
* Metric the heat map and diagram default to: measured time when the plan was
|
|
98
|
+
* run with ANALYZE, planner cost otherwise. `null` when the plan carries no
|
|
99
|
+
* metric at all, so callers never select a metric that has no data.
|
|
100
|
+
*/
|
|
101
|
+
declare function getDefaultMetricKind(metrics: ExplainMetrics): ExplainMetricKind | null;
|
|
102
|
+
|
|
103
|
+
type ExplainDiagnosticKind = "hotspot" | "over-estimate" | "under-estimate" | "disk-sort" | "filter-loss" | "large-seq-scan" | "heap-fetches" | "workers-underused" | "high-loops" | "cache-miss" | "never-executed";
|
|
104
|
+
type ExplainDiagnosticSeverity = "info" | "warning" | "critical";
|
|
105
|
+
interface ExplainDiagnostic {
|
|
106
|
+
kind: ExplainDiagnosticKind;
|
|
107
|
+
severity: ExplainDiagnosticSeverity;
|
|
108
|
+
/** i18n key for the short chip label. */
|
|
109
|
+
labelKey: string;
|
|
110
|
+
/** i18n key for the one-line explanation shown in the node details. */
|
|
111
|
+
descriptionKey: string;
|
|
112
|
+
/** Pre-formatted figure shown next to the label, when the check has one. */
|
|
113
|
+
value?: string;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Thresholds every check is calibrated against. Exported so tests pin the
|
|
117
|
+
* boundaries rather than duplicating the numbers.
|
|
118
|
+
*/
|
|
119
|
+
declare const EXPLAIN_DIAGNOSTIC_THRESHOLDS: {
|
|
120
|
+
/** Share of total plan time above which a node is called a hotspot. */
|
|
121
|
+
readonly hotspotTimeShare: 0.25;
|
|
122
|
+
/** Nodes faster than this never count as a hotspot, however large the share. */
|
|
123
|
+
readonly hotspotMinTimeMs: 1;
|
|
124
|
+
/** Row estimate off by this factor is a warning. */
|
|
125
|
+
readonly estimateWarnFactor: 4;
|
|
126
|
+
/** Row estimate off by this factor is critical. */
|
|
127
|
+
readonly estimateCriticalFactor: 10;
|
|
128
|
+
/** Estimate checks are skipped below this many actual rows, to cut noise. */
|
|
129
|
+
readonly estimateMinRows: 10;
|
|
130
|
+
/** Fraction of scanned rows dropped by a filter that counts as wasted work. */
|
|
131
|
+
readonly filterLossRatio: 0.9;
|
|
132
|
+
/** Filter checks are skipped below this many discarded rows. */
|
|
133
|
+
readonly filterLossMinRows: 1000;
|
|
134
|
+
/** Sequential scans below this many rows are not reported. */
|
|
135
|
+
readonly seqScanMinRows: 10000;
|
|
136
|
+
/** Heap fetches above this count suggest the visibility map is stale. */
|
|
137
|
+
readonly heapFetchesMinCount: 1000;
|
|
138
|
+
/** Loop count above which repeated execution is worth pointing out. */
|
|
139
|
+
readonly highLoopsMinCount: 1000;
|
|
140
|
+
/** Fraction of buffer accesses that missed shared buffers. */
|
|
141
|
+
readonly cacheMissRatio: 0.5;
|
|
142
|
+
/** Cache checks are skipped below this many blocks read from disk. */
|
|
143
|
+
readonly cacheMissMinBlocks: 1000;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Checks that apply to a single node. `metrics` is optional so a node can be
|
|
147
|
+
* inspected without a full plan walk; the hotspot check is skipped without it.
|
|
148
|
+
*/
|
|
149
|
+
declare function getNodeDiagnostics(node: ExplainNode, metrics?: ExplainNodeMetrics | null): ExplainDiagnostic[];
|
|
150
|
+
/** Diagnostics for every node in the plan, keyed by node id. */
|
|
151
|
+
declare function getPlanDiagnostics(plan: ExplainPlan, metrics: ExplainMetrics): Map<string, ExplainDiagnostic[]>;
|
|
152
|
+
/** Highest severity in a list, or `null` when the list is empty. */
|
|
153
|
+
declare function getWorstSeverity(diagnostics: ExplainDiagnostic[]): ExplainDiagnosticSeverity | null;
|
|
154
|
+
/** How many diagnostics of each severity the whole plan produced. */
|
|
155
|
+
declare function countDiagnosticsBySeverity(diagnostics: Map<string, ExplainDiagnostic[]>): Record<ExplainDiagnosticSeverity, number>;
|
|
156
|
+
|
|
157
|
+
export { type ExplainPlan as E, type ExplainNode as a, type ExplainMetrics as b, EXPLAIN_DIAGNOSTIC_THRESHOLDS as c, EXPLAIN_METRIC_KINDS as d, type ExplainDiagnostic as e, type ExplainDiagnosticKind as f, type ExplainDiagnosticSeverity as g, type ExplainMetricKind as h, type ExplainNodeMetrics as i, computeExplainMetrics as j, countDiagnosticsBySeverity as k, getAvailableMetricKinds as l, getDefaultMetricKind as m, getMetricMax as n, getMetricValue as o, getNodeDiagnostics as p, getNodeMetrics as q, getPlanDiagnostics as r, getWorstSeverity as s, isMetricAvailable as t };
|
package/dist/flow.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { Node, Edge } from '@xyflow/react';
|
|
2
|
+
import { a as ExplainNode, i as ExplainNodeMetrics, e as ExplainDiagnostic, E as ExplainPlan, b as ExplainMetrics } from './diagnostics-DPSgsjFk.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* ReactFlow adapter: turns a plan tree into positioned nodes and edges.
|
|
6
|
+
*
|
|
7
|
+
* Split out of `plan.ts` so the analysis core stays free of `@xyflow/react` and
|
|
8
|
+
* `dagre` — a headless consumer never pulls a graph library.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Payload carried by every `explainPlan` node in the ReactFlow graph. */
|
|
12
|
+
interface ExplainPlanNodeData extends Record<string, unknown> {
|
|
13
|
+
node: ExplainNode;
|
|
14
|
+
metrics: ExplainNodeMetrics | null;
|
|
15
|
+
/** Largest exclusive cost in the plan, used to scale the heat colour. */
|
|
16
|
+
maxExclusiveCost: number;
|
|
17
|
+
/** Largest exclusive time in the plan, used to scale the heat colour. */
|
|
18
|
+
maxExclusiveTimeMs: number;
|
|
19
|
+
diagnostics: ExplainDiagnostic[];
|
|
20
|
+
hasAnalyzeData: boolean;
|
|
21
|
+
isSelected: boolean;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Build the ReactFlow graph for a plan.
|
|
25
|
+
*
|
|
26
|
+
* `metrics` and `diagnostics` may be supplied by a caller that already computed
|
|
27
|
+
* them for other views, so a plan is only walked once per render. Diagnostics
|
|
28
|
+
* are passed in rather than derived here, which keeps this module independent of
|
|
29
|
+
* `explainDiagnostics` — that module needs the helpers below.
|
|
30
|
+
*/
|
|
31
|
+
declare function explainPlanToFlow(plan: ExplainPlan, selectedNodeId?: string | null, metrics?: ExplainMetrics, diagnostics?: Map<string, ExplainDiagnostic[]>): {
|
|
32
|
+
nodes: Node[];
|
|
33
|
+
edges: Edge[];
|
|
34
|
+
};
|
|
35
|
+
declare function layoutExplainNodes(nodes: Node[], edges: Edge[]): {
|
|
36
|
+
nodes: Node[];
|
|
37
|
+
edges: Edge[];
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export { type ExplainPlanNodeData, explainPlanToFlow, layoutExplainNodes };
|
package/dist/flow.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import dagre from 'dagre';
|
|
2
|
+
|
|
3
|
+
// src/flow.ts
|
|
4
|
+
|
|
5
|
+
// src/metrics.ts
|
|
6
|
+
function loopCount(node) {
|
|
7
|
+
return node.actual_loops != null && node.actual_loops > 0 ? node.actual_loops : 1;
|
|
8
|
+
}
|
|
9
|
+
function isSubplanChild(node) {
|
|
10
|
+
const relationship = node.extra["Parent Relationship"];
|
|
11
|
+
return relationship === "InitPlan" || relationship === "SubPlan";
|
|
12
|
+
}
|
|
13
|
+
function bufferCount(node) {
|
|
14
|
+
if (node.buffers_hit == null && node.buffers_read == null) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
return (node.buffers_hit ?? 0) + (node.buffers_read ?? 0);
|
|
18
|
+
}
|
|
19
|
+
function selfValue(total, childSum) {
|
|
20
|
+
if (total == null) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
return Math.max(0, total - childSum);
|
|
24
|
+
}
|
|
25
|
+
function computeExplainMetrics(plan) {
|
|
26
|
+
const byId = /* @__PURE__ */ new Map();
|
|
27
|
+
const order = [];
|
|
28
|
+
let nextIndex = 1;
|
|
29
|
+
function walk(node, depth) {
|
|
30
|
+
const metrics = {
|
|
31
|
+
nodeId: node.id,
|
|
32
|
+
index: nextIndex,
|
|
33
|
+
depth,
|
|
34
|
+
inclusiveTimeMs: null,
|
|
35
|
+
exclusiveTimeMs: null,
|
|
36
|
+
totalCost: node.total_cost,
|
|
37
|
+
exclusiveCost: null,
|
|
38
|
+
totalRows: null,
|
|
39
|
+
inclusiveBuffers: bufferCount(node),
|
|
40
|
+
exclusiveBuffers: null,
|
|
41
|
+
timeShare: null,
|
|
42
|
+
neverExecuted: node.actual_loops === 0
|
|
43
|
+
};
|
|
44
|
+
nextIndex += 1;
|
|
45
|
+
byId.set(node.id, metrics);
|
|
46
|
+
order.push(metrics);
|
|
47
|
+
let childTimeMs = 0;
|
|
48
|
+
let childCost = 0;
|
|
49
|
+
let childBuffers = 0;
|
|
50
|
+
for (const child of node.children) {
|
|
51
|
+
const contribution = walk(child, depth + 1);
|
|
52
|
+
childCost += contribution.cost;
|
|
53
|
+
childBuffers += contribution.buffers;
|
|
54
|
+
if (!isSubplanChild(child)) {
|
|
55
|
+
childTimeMs += contribution.timeMs;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const loops = loopCount(node);
|
|
59
|
+
metrics.inclusiveTimeMs = node.actual_time_ms != null ? node.actual_time_ms * loops : null;
|
|
60
|
+
metrics.exclusiveTimeMs = selfValue(metrics.inclusiveTimeMs, childTimeMs);
|
|
61
|
+
metrics.exclusiveCost = selfValue(metrics.totalCost, childCost);
|
|
62
|
+
metrics.exclusiveBuffers = selfValue(metrics.inclusiveBuffers, childBuffers);
|
|
63
|
+
metrics.totalRows = node.actual_rows != null ? node.actual_rows * loops : null;
|
|
64
|
+
return {
|
|
65
|
+
timeMs: metrics.inclusiveTimeMs ?? 0,
|
|
66
|
+
cost: metrics.totalCost ?? 0,
|
|
67
|
+
buffers: metrics.inclusiveBuffers ?? 0
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
walk(plan.root, 0);
|
|
71
|
+
let maxExclusiveTimeMs = 0;
|
|
72
|
+
let maxExclusiveCost = 0;
|
|
73
|
+
let maxTotalRows = 0;
|
|
74
|
+
let maxExclusiveBuffers = 0;
|
|
75
|
+
let totalExclusiveTimeMs = 0;
|
|
76
|
+
for (const metrics of order) {
|
|
77
|
+
maxExclusiveTimeMs = Math.max(
|
|
78
|
+
maxExclusiveTimeMs,
|
|
79
|
+
metrics.exclusiveTimeMs ?? 0
|
|
80
|
+
);
|
|
81
|
+
maxExclusiveCost = Math.max(maxExclusiveCost, metrics.exclusiveCost ?? 0);
|
|
82
|
+
maxTotalRows = Math.max(maxTotalRows, metrics.totalRows ?? 0);
|
|
83
|
+
maxExclusiveBuffers = Math.max(
|
|
84
|
+
maxExclusiveBuffers,
|
|
85
|
+
metrics.exclusiveBuffers ?? 0
|
|
86
|
+
);
|
|
87
|
+
totalExclusiveTimeMs += metrics.exclusiveTimeMs ?? 0;
|
|
88
|
+
}
|
|
89
|
+
if (totalExclusiveTimeMs > 0) {
|
|
90
|
+
for (const metrics of order) {
|
|
91
|
+
if (metrics.exclusiveTimeMs != null) {
|
|
92
|
+
metrics.timeShare = metrics.exclusiveTimeMs / totalExclusiveTimeMs;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
byId,
|
|
98
|
+
order,
|
|
99
|
+
maxExclusiveTimeMs,
|
|
100
|
+
maxExclusiveCost,
|
|
101
|
+
maxTotalRows,
|
|
102
|
+
maxExclusiveBuffers,
|
|
103
|
+
totalExclusiveTimeMs
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/flow.ts
|
|
108
|
+
function explainPlanToFlow(plan, selectedNodeId, metrics, diagnostics) {
|
|
109
|
+
const planMetrics = metrics ?? computeExplainMetrics(plan);
|
|
110
|
+
const rawNodes = [];
|
|
111
|
+
const edges = [];
|
|
112
|
+
function walk(node) {
|
|
113
|
+
const nodeMetrics = planMetrics.byId.get(node.id);
|
|
114
|
+
const data = {
|
|
115
|
+
node,
|
|
116
|
+
metrics: nodeMetrics ?? null,
|
|
117
|
+
maxExclusiveCost: planMetrics.maxExclusiveCost,
|
|
118
|
+
maxExclusiveTimeMs: planMetrics.maxExclusiveTimeMs,
|
|
119
|
+
diagnostics: diagnostics?.get(node.id) ?? [],
|
|
120
|
+
hasAnalyzeData: plan.has_analyze_data,
|
|
121
|
+
isSelected: selectedNodeId === node.id
|
|
122
|
+
};
|
|
123
|
+
rawNodes.push({
|
|
124
|
+
id: node.id,
|
|
125
|
+
type: "explainPlan",
|
|
126
|
+
position: { x: 0, y: 0 },
|
|
127
|
+
data
|
|
128
|
+
});
|
|
129
|
+
for (const child of node.children) {
|
|
130
|
+
edges.push({
|
|
131
|
+
id: `${node.id}-${child.id}`,
|
|
132
|
+
source: node.id,
|
|
133
|
+
target: child.id,
|
|
134
|
+
animated: true,
|
|
135
|
+
style: { stroke: "#6366f1" }
|
|
136
|
+
});
|
|
137
|
+
walk(child);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
walk(plan.root);
|
|
141
|
+
return layoutExplainNodes(rawNodes, edges);
|
|
142
|
+
}
|
|
143
|
+
function layoutExplainNodes(nodes, edges) {
|
|
144
|
+
const g = new dagre.graphlib.Graph();
|
|
145
|
+
g.setDefaultEdgeLabel(() => ({}));
|
|
146
|
+
g.setGraph({ rankdir: "TB", ranksep: 80, nodesep: 40 });
|
|
147
|
+
const NODE_WIDTH = 280;
|
|
148
|
+
for (const node of nodes) {
|
|
149
|
+
const data = node.data;
|
|
150
|
+
const lines = 3 + (data.hasAnalyzeData ? 2 : 0) + (data.node.filter ? 1 : 0) + (data.diagnostics.length > 0 ? 1 : 0);
|
|
151
|
+
const height = 28 + lines * 22;
|
|
152
|
+
g.setNode(node.id, { width: NODE_WIDTH, height });
|
|
153
|
+
}
|
|
154
|
+
for (const edge of edges) {
|
|
155
|
+
g.setEdge(edge.source, edge.target);
|
|
156
|
+
}
|
|
157
|
+
dagre.layout(g);
|
|
158
|
+
const layoutedNodes = nodes.map((node) => {
|
|
159
|
+
const pos = g.node(node.id);
|
|
160
|
+
return {
|
|
161
|
+
...node,
|
|
162
|
+
position: {
|
|
163
|
+
x: pos.x - NODE_WIDTH / 2,
|
|
164
|
+
y: pos.y - pos.height / 2
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
return { nodes: layoutedNodes, edges };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export { explainPlanToFlow, layoutExplainNodes };
|