Skip to content

About

Portable WebAssembly execution plan parser, comparator, deadlock parser, and exporter for SQL Server, PostgreSQL, MySQL, MariaDB, YugabyteDB, and SQLite.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@culpritsql/plans

npm Runtime: Rust + Wasm Zero Dependencies TypeScript License: MIT

Portable WebAssembly execution plan parser, structural differ, deadlock parser, and exporter.
Compiled directly from Culprit SQL Studio and paired with @culpritsql/plan-viewer.

Quickstart · Supported Engines · API Reference · Node.js & Bundlers · Security & Limits


Quickstart

npm install @culpritsql/plans

Initialize the WebAssembly module once, then call the synchronous parser, comparison, deadlock, and export operations:

import {
  init,
  parsePlan,
  diffPlans,
  parseDeadlock,
  exportPlan,
} from "@culpritsql/plans";

await init();

const before = parsePlan(beforePlanText);
const after = parsePlan(afterPlanText, "postgres");
const diff = diffPlans(before, "Before", after, "After");

const deadlocks = parseDeadlock(deadlockLogText);
const exported = exportPlan(after, "checkout-query", "markdown", {
  redact: true,
  notes: [],
});

Rendering in React? Pass Plan, PlanDiff, and Deadlock directly to @culpritsql/plan-viewer. Both packages share the exact same generated ts-rs model types.


Supported Engines

Engine Execution Plans Deadlocks
SQL Server .sqlplan Showplan XML XML deadlock graphs
PostgreSQL EXPLAIN (FORMAT JSON), EXPLAIN (ANALYZE, BUFFERS) text Server log deadlock excerpts
MySQL & MariaDB EXPLAIN FORMAT=JSON, EXPLAIN ANALYZE tree text SHOW ENGINE INNODB STATUS
YugabyteDB EXPLAIN (FORMAT JSON) + DocDB RPC/storage metrics PostgreSQL-compatible logs
SQLite EXPLAIN QUERY PLAN text Not applicable

API Reference

Export Signature Description
init init(input?): Promise<void> Instantiates the Wasm module once; subsequent calls reuse the live instance
parsePlan parsePlan(input: string, engine?: Engine): Plan Auto-detects format or validates strictly against "mssql" | "postgres" | "mysql" | "yugabyte" | "sqlite"
diffPlans diffPlans(a: Plan, nameA: string, b: Plan, nameB: string, context?: CompareContext): PlanDiff Computes structural operator changes, access-path flips, cardinality shifts, and speedup/cost verdicts
parseDeadlock parseDeadlock(input: string): Deadlock[] Extracts victim selection, participant sessions, held/waited locks, and original timestamps
exportPlan exportPlan(plan: Plan, title: string, format: ExportFormat, options: ExportOptions): ExportFile Exports "markdown", "html", or "native" with optional SQL literal redaction
ParserError class ParserError extends Error Structured refusal error (error.error carries { code, message, hint, detail }) instead of Wasm traps

Redacted Exports

Setting redact: true scrubs SQL string and numeric literals from statements, filter predicates, and native Showplan XML parameter lists before returning the file:

const file = exportPlan(plan, "Order Lookup", "html", {
  redact: true,
  notes: [],
});
// file => { name: "order-lookup.html", format: "html", contents: "<!doctype html>..." }

Structured Error Handling

Malformed XML/JSON, mismatched engine hints, DTD attempts, or over-depth trees throw ParserError cleanly without corrupting the Wasm instance:

import { parsePlan, ParserError } from "@culpritsql/plans";

try {
  const plan = parsePlan(untrustedInput, "postgres");
} catch (err) {
  if (err instanceof ParserError) {
    console.error(err.error.code, err.error.message, err.error.hint);
  }
}

Node.js & Bundlers

In browsers, init() resolves culprit_wasm_bg.wasm relative to import.meta.url via fetch. In Node.js or custom bundler setups, pass module_or_path explicitly:

import { readFile } from "node:fs/promises";
import { init, parsePlan } from "@culpritsql/plans";

const wasmBytes = await readFile(
  new URL(
    "./node_modules/@culpritsql/plans/pkg/native/culprit_wasm_bg.wasm",
    import.meta.url,
  ),
);
await init({ module_or_path: wasmBytes });

const plan = parsePlan("QUERY PLAN\n`--SCAN orders", "sqlite");

With Vite or Next.js static asset URLs:

import wasmUrl from "@culpritsql/plans/pkg/native/culprit_wasm_bg.wasm?url";
import { init } from "@culpritsql/plans";

await init({ module_or_path: wasmUrl });

Security & Limits

  • Zero runtime dependencies: Pure Rust compiled to wasm32-unknown-unknown with zero npm dependencies, no database drivers, and no network calls.
  • Hardened XML & JSON bounds: Rejects XML <!DOCTYPE> / entity expansion (XXE) and enforces deterministic depth ceilings (1,024 XML elements, 56 plan operator levels) on a dedicated 32 MiB Wasm linear stack.
  • HTML-safe exports: HTML plan exports escape every identifier, predicate, and title string.
  • CSP compatibility: Serve .wasm assets with Content-Type: application/wasm and include 'wasm-unsafe-eval' in script-src under strict Content Security Policies.
  • Single source of truth: Synced automatically from culpritsql/culprit-sql-studio (crates/culprit-plans, crates/culprit-wasm, packages/culprit-wasm).

License

MIT. Third-party Rust crate notices are distributed in pkg/THIRD-PARTY-LICENSES.txt.

About

Portable WebAssembly execution plan parser, comparator, deadlock parser, and exporter for SQL Server, PostgreSQL, MySQL, MariaDB, YugabyteDB, and SQLite.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages