turbine-orm 0.56.0 → 0.57.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/dist/cli/index.js CHANGED
@@ -32,7 +32,8 @@ import { generate, generatePrismaMap } from '../generate.js';
32
32
  import { buildCreateIndexSql, buildDropIndexSql, collectDoctorProbeIndexNames, collectRelationProbeColumns, findMissingRelationIndexes, } from '../index-advisor.js';
33
33
  import { auditDoctorIndexes, collectStatsSnapshot, collectTableHeat, findInvalidIndexes, findRedundantIndexes, findUnusedIndexes, formatBytes, isSnapshotUsable, STATS_THRESHOLDS, scoreMissingIndex, } from '../index-stats.js';
34
34
  import { introspect } from '../introspect.js';
35
- import { collectDivergenceCandidateColumns, findPlanDivergence, PLAN_DIVERGENCE_THRESHOLDS, } from '../plan-divergence.js';
35
+ import { collectDivergenceCandidateColumns, collectDivergenceOrderColumns, findPlanDivergence, PLAN_DIVERGENCE_THRESHOLDS, } from '../plan-divergence.js';
36
+ import { snakeToCamel } from '../schema.js';
36
37
  import { DestructivePushRefusal, schemaDiff, schemaPush } from '../schema-sql.js';
37
38
  import { configTemplate, DEFAULT_INIT_SEED_FILE, findConfigFile, loadConfigResult, looksLikeSchemaFilePath, resolveConfig, resolveSeedFile, unwrapModuleDefault, } from './config.js';
38
39
  import { DESTRUCTIVE_KIND_LABEL } from './destructive.js';
@@ -2274,8 +2275,14 @@ async function cmdDoctor(args, config) {
2274
2275
  // one-connection snapshot rather than opening a second read.
2275
2276
  const divergenceOn = args.noPlanDivergence !== true;
2276
2277
  const divergenceColumns = divergenceOn ? collectDivergenceCandidateColumns(schema) : [];
2278
+ // The columns a finding could ORDER BY, read alongside the candidates: the
2279
+ // size of an unindexed-filter flip turns on the ORDER column's correlation,
2280
+ // not the filter column's, and reading only the latter is how an earlier
2281
+ // revision printed a statistic about the wrong column.
2282
+ const divergenceOrderColumns = divergenceOn ? collectDivergenceOrderColumns(schema) : [];
2277
2283
  const probedTables = [...new Set(missing.map((m) => m.table))];
2278
2284
  const statsTables = [...new Set([...probedTables, ...divergenceColumns.map((c) => c.table)])];
2285
+ const distributionColumns = [...divergenceColumns, ...divergenceOrderColumns];
2279
2286
  let snapshot;
2280
2287
  try {
2281
2288
  snapshot = await collectStatsSnapshot({
@@ -2283,7 +2290,7 @@ async function cmdDoctor(args, config) {
2283
2290
  schema: config.schema,
2284
2291
  tables: statsTables,
2285
2292
  columns: probedColumns,
2286
- distributionColumns: divergenceColumns,
2293
+ distributionColumns,
2287
2294
  });
2288
2295
  }
2289
2296
  catch (err) {
@@ -2328,7 +2335,7 @@ async function cmdDoctor(args, config) {
2328
2335
  // cluster with a NULL stats_reset (the default) must still get the check.
2329
2336
  const divergence = divergenceOn && snapshot.available
2330
2337
  ? findPlanDivergence(schema, snapshot)
2331
- : { findings: [], notices: [], candidatesConsidered: 0 };
2338
+ : { findings: [], notices: [], candidatesConsidered: 0, consideredIndexed: 0, consideredUnindexed: 0 };
2332
2339
  if (jsonMode) {
2333
2340
  spinner?.stop();
2334
2341
  console.log(JSON.stringify(buildDoctorJson({ schema, findings, invalid, snapshot, usable, heat, subtract, divergence, args }), null, 2));
@@ -2415,6 +2422,14 @@ function buildDoctorJson(ctx) {
2415
2422
  // just because the section was skipped or found nothing.
2416
2423
  out.planDivergence = ctx.divergence.findings;
2417
2424
  out.planDivergenceNotices = ctx.divergence.notices;
2425
+ // How large the scored population was, and how it split. A consumer counting
2426
+ // findings alone cannot tell "considered and clean" from "never looked", and
2427
+ // the unindexed half of that population did not exist before.
2428
+ out.planDivergenceScored = {
2429
+ considered: ctx.divergence.candidatesConsidered,
2430
+ indexed: ctx.divergence.consideredIndexed,
2431
+ unindexed: ctx.divergence.consideredUnindexed,
2432
+ };
2418
2433
  return out;
2419
2434
  }
2420
2435
  async function renderDoctorHuman(ctx) {
@@ -2433,6 +2448,11 @@ async function renderDoctorHuman(ctx) {
2433
2448
  newline();
2434
2449
  return;
2435
2450
  }
2451
+ // One column, one place. An unindexed filter column that ALSO diverges is one
2452
+ // problem with one remedy (the index), so the divergence evidence renders as
2453
+ // an extra block on the missing-index finding rather than as a second,
2454
+ // unrelated-looking entry in the cached-plan section.
2455
+ const attached = attachDivergenceToMissingIndexes(findings, divergence);
2436
2456
  if (findings.length > 0) {
2437
2457
  warn(`Found ${bold(String(findings.length))} unindexed relation probe(s)`);
2438
2458
  newline();
@@ -2440,10 +2460,10 @@ async function renderDoctorHuman(ctx) {
2440
2460
  console.log(` ${dim('once per parent row, so an unindexed FK costs a full table scan PER PARENT.')}`);
2441
2461
  newline();
2442
2462
  if (usable) {
2443
- renderTiers(findings, snapshot, args);
2463
+ renderTiers(findings, snapshot, args, attached);
2444
2464
  }
2445
2465
  else {
2446
- renderTopologyFallback(findings, snapshot);
2466
+ renderTopologyFallback(findings, snapshot, attached);
2447
2467
  }
2448
2468
  // Heat honesty: one line when the workload-heat boost could not be sourced.
2449
2469
  if (!heat.available && heat.notice) {
@@ -2452,7 +2472,7 @@ async function renderDoctorHuman(ctx) {
2452
2472
  }
2453
2473
  }
2454
2474
  renderInvalidIndexes(invalid);
2455
- renderPlanDivergence(divergence);
2475
+ renderPlanDivergence(divergence, attached);
2456
2476
  if (subtract.unusedRan) {
2457
2477
  renderUnusedIndexes(subtract.unused, subtract.minScans, snapshot);
2458
2478
  renderRedundantIndexes(subtract.redundant);
@@ -2549,8 +2569,96 @@ function renderDoctorAudit(audit, minScans, snapshot) {
2549
2569
  console.log(` ${dim('Consider dropping the ones you confirm are unused. Nothing here is auto-dropped.')}`);
2550
2570
  newline();
2551
2571
  }
2572
+ function attachDivergenceToMissingIndexes(findings, divergence) {
2573
+ const byColumn = new Map();
2574
+ for (const d of divergence.findings) {
2575
+ if (d.branch !== 'unindexed-filter')
2576
+ continue;
2577
+ byColumn.set(`${d.table}\u0000${d.column}`, d);
2578
+ }
2579
+ const attached = new Map();
2580
+ for (const f of findings) {
2581
+ // Single-column probes only: a composite probe's index is not the thing the
2582
+ // single-column divergence model reasons about.
2583
+ if (f.missing.columns.length !== 1 || f.missing.columns[0] === undefined)
2584
+ continue;
2585
+ const key = `${f.missing.table}\u0000${f.missing.columns[0]}`;
2586
+ const d = byColumn.get(key);
2587
+ if (d)
2588
+ attached.set(key, d);
2589
+ }
2590
+ return attached;
2591
+ }
2592
+ /** The key a missing-index finding is looked up by in {@link AttachedDivergence}. */
2593
+ function attachKey(f) {
2594
+ return `${f.missing.table}\u0000${f.missing.columns[0] ?? ''}`;
2595
+ }
2596
+ /**
2597
+ * The cached-plan evidence block printed UNDER a missing-index finding.
2598
+ *
2599
+ * It never recommends `forceCustomPlan`: the remedy is the index the same
2600
+ * finding already prints, and recommending a per-query plan-cache override for a
2601
+ * missing index would be advice to paper over a table scan.
2602
+ */
2603
+ /**
2604
+ * How big an `unindexed-filter` flip is, and under what condition, as plain
2605
+ * lines both branch-B renderers print.
2606
+ *
2607
+ * The condition is not decoration. The generic plan's cost is one heap fetch per
2608
+ * index entry, so the ratio is the table's rows-per-page when the heap is not in
2609
+ * `orderColumn` order and ~1x when it is: 80x and 1.2x on two fixtures identical
2610
+ * in every scored input. An earlier revision printed the ratio unconditionally
2611
+ * and quoted the FILTER column's correlation next to a sentence about the ORDER
2612
+ * column's physical order, so the one field offered as the reader's escape hatch
2613
+ * was measured on the wrong column.
2614
+ */
2615
+ function divergenceAmplificationLines(d) {
2616
+ const amp = divInt(d.worstCaseAmplification ?? 0);
2617
+ const corr = d.orderColumnCorrelation;
2618
+ const corrLabel = corr === null || corr === undefined
2619
+ ? `no pg_stats correlation available for "${d.orderColumn}"`
2620
+ : `correlation ${corr.toFixed(5)} on "${d.orderColumn}"`;
2621
+ if (d.heapNearlyOrdered === true) {
2622
+ return [
2623
+ `The size of that flip turns on the heap's physical order, and THIS heap is in near-exact`,
2624
+ `"${d.orderColumn}" order (${corrLabel}), so consecutive index entries hit the`,
2625
+ `same pinned page: measured ~1x, not the ~${amp}x an unordered heap reads. Most likely this`,
2626
+ `one is not costing you anything today. It is also one sampled statistic away from the`,
2627
+ `much worse reading, so measure rather than assume in either direction.`,
2628
+ ];
2629
+ }
2630
+ return [
2631
+ `That costs ~${amp}x the buffers of the seq scan, because each index entry is its own heap`,
2632
+ `fetch (${corrLabel}). The one shape that reads ~1x instead is a heap`,
2633
+ `in near-exact "${d.orderColumn}" order; two pages of local disorder already reads ~41x.`,
2634
+ ];
2635
+ }
2636
+ function renderDivergenceEvidence(d) {
2637
+ const tuples = divInt(d.tuplesWalked ?? d.rows);
2638
+ console.log(` ${dim(symbols.tee)} ${yellow('cached-plan risk:')} this unindexed filter column can also flip a cached plan.`);
2639
+ console.log(` ${dim(`${divInt(d.rows)} rows in ${divInt(d.pages)} pages, rarest value ~${divInt(d.rarestBucket)} rows, below the assumed LIMIT ${d.assumedLimit}.`)}`);
2640
+ console.log(` ${dim(`Without the index the good plan is a seq scan (${divInt(d.pages)} pages); a promoted generic`)}`);
2641
+ console.log(` ${dim(`plan cannot see the value is rare, keeps the ordered "${d.orderColumn}" walk, and reads`)}`);
2642
+ console.log(` ${dim(`up to ~${tuples} tuples before it fills the LIMIT.`)}`);
2643
+ console.log(` ${dim('Postgres promotes this shape exactly when the workload keeps asking for the rare')}`);
2644
+ console.log(` ${dim('value: that is the case where the custom plan is expensive enough for the generic')}`);
2645
+ console.log(` ${dim('estimate to look cheaper.')}`);
2646
+ for (const line of divergenceAmplificationLines(d))
2647
+ console.log(` ${dim(line)}`);
2648
+ console.log(` ${dim('Adding the index above is the fix. Confirm first if you want to:')}`);
2649
+ for (const line of d.diagnosticSql.split('\n')) {
2650
+ console.log(` ${green(line)}`);
2651
+ }
2652
+ console.log(` ${dim('After adding this index, re-run doctor. This column is expected to reappear as a')}`);
2653
+ console.log(` ${dim('sparse-value finding in the cached-plan section. That later finding is exposure, not')}`);
2654
+ console.log(` ${dim('a regression: the index makes the good plan much cheaper, which is why the ratio it')}`);
2655
+ console.log(` ${dim('quotes is larger, and on a measured fixture it is also what stops Postgres from')}`);
2656
+ console.log(` ${dim('promoting the generic plan at all. Treat the reappearance as the normal end state;')}`);
2657
+ console.log(` ${dim("use the diagnostic block's generic_plans counter to decide whether anything more is")}`);
2658
+ console.log(` ${dim('warranted.')}`);
2659
+ }
2552
2660
  /** Cost-aware tiered output: three sections, each finding annotated with its numbers. */
2553
- function renderTiers(findings, snapshot, _args) {
2661
+ function renderTiers(findings, snapshot, _args, attached) {
2554
2662
  const ageLabel = snapshot.statsAgeDays !== null ? `${Math.round(snapshot.statsAgeDays)}d` : 'unknown';
2555
2663
  console.log(` ${dim(`Cost triage based on live statistics (stats reset ${ageLabel} ago). Thresholds: tiny < ${STATS_THRESHOLDS.tinyTableRows.toLocaleString()} rows,`)}`);
2556
2664
  console.log(` ${dim(`"real" write rate >= ${STATS_THRESHOLDS.highWritesPerDay.toLocaleString()}/day, "many" indexes >= ${STATS_THRESHOLDS.manyIndexes}.`)}`);
@@ -2569,12 +2677,12 @@ function renderTiers(findings, snapshot, _args) {
2569
2677
  console.log(` ${bold(tierColor[tier](`${TIER_LABEL[tier]} (${inTier.length})`))}`);
2570
2678
  newline();
2571
2679
  for (const f of inTier) {
2572
- renderFinding(f, { concurrently: true, withReasons: true });
2680
+ renderFinding(f, { concurrently: true, withReasons: true, divergence: attached.get(attachKey(f)) });
2573
2681
  }
2574
2682
  }
2575
2683
  }
2576
2684
  /** Degraded output: today's size-sorted topology report when stats are absent/young. */
2577
- function renderTopologyFallback(findings, snapshot) {
2685
+ function renderTopologyFallback(findings, snapshot, attached) {
2578
2686
  warn('Statistics unavailable or too young to score cost: showing size-sorted topology only.');
2579
2687
  for (const notice of snapshot.notices)
2580
2688
  console.log(` ${dim(`- ${notice}`)}`);
@@ -2584,7 +2692,7 @@ function renderTopologyFallback(findings, snapshot) {
2584
2692
  newline();
2585
2693
  const sorted = [...findings].sort((a, b) => (b.score.metrics.rows ?? 0) - (a.score.metrics.rows ?? 0));
2586
2694
  for (const f of sorted) {
2587
- renderFinding(f, { concurrently: false, withReasons: false });
2695
+ renderFinding(f, { concurrently: false, withReasons: false, divergence: attached.get(attachKey(f)) });
2588
2696
  }
2589
2697
  }
2590
2698
  /** Render one finding: table + columns, probing relations, reasons, and the create SQL. */
@@ -2602,7 +2710,10 @@ function renderFinding(f, opts) {
2602
2710
  console.log(` ${dim(symbols.tee)} ${dim(reason)}`);
2603
2711
  }
2604
2712
  }
2605
- console.log(` ${dim(symbols.teeEnd)} ${green(doctorCreateSql(f, { concurrently: opts.concurrently }))}`);
2713
+ const last = opts.divergence ? symbols.tee : symbols.teeEnd;
2714
+ console.log(` ${dim(last)} ${green(doctorCreateSql(f, { concurrently: opts.concurrently }))}`);
2715
+ if (opts.divergence)
2716
+ renderDivergenceEvidence(opts.divergence);
2606
2717
  newline();
2607
2718
  }
2608
2719
  /** The invalid-index report section (a failed CONCURRENTLY build leaves these behind). */
@@ -2618,6 +2729,29 @@ function renderInvalidIndexes(invalid) {
2618
2729
  newline();
2619
2730
  }
2620
2731
  }
2732
+ /**
2733
+ * The release in which `turbine-orm/prisma-compat` began forwarding Turbine-only
2734
+ * query options (`forceCustomPlan` among them) to the core client.
2735
+ *
2736
+ * Printed rather than assumed, and the sentence stays even after that release:
2737
+ * doctor's audience routinely runs a CLI newer than the library pinned in the
2738
+ * app, and on an older library the option is accepted and silently ignored.
2739
+ */
2740
+ const COMPAT_PASSTHROUGH_VERSION = '0.57.0';
2741
+ /**
2742
+ * The gates + scored-population footer. Split out because it is printed from two
2743
+ * places: the normal section, and the case where every finding was attached to a
2744
+ * missing-index finding instead.
2745
+ */
2746
+ function renderDivergenceGates(divergence) {
2747
+ const t = PLAN_DIVERGENCE_THRESHOLDS;
2748
+ console.log(` ${dim(`Gates, indexed column: the wrong plan must walk >= ${t.minWalkPages} pages and >= ${Math.round(t.minWalkFraction * 100)}% of the table.`)}`);
2749
+ console.log(` ${dim(`Gates, unindexed column: the rarest value must hold fewer rows than the limit, and the wrong`)}`);
2750
+ console.log(` ${dim(`plan must walk >= ${t.minGenericTupleWalk.toLocaleString('en-US')} tuples. Assumed LIMIT ${t.assumedLimit} throughout.`)}`);
2751
+ console.log(` ${dim(`${divergence.candidatesConsidered} column(s) were scored (${divergence.consideredIndexed} indexed, ${divergence.consideredUnindexed} unindexed). That population is relation-probe`)}`);
2752
+ console.log(` ${dim('and leading-index columns only: a filter column that is neither is not covered. Skip this')}`);
2753
+ console.log(` ${dim('section with --no-plan-divergence.')}`);
2754
+ }
2621
2755
  /** Round to a whole number and group it, for the divergence report's estimates. */
2622
2756
  function divInt(n) {
2623
2757
  if (!Number.isFinite(n))
@@ -2630,8 +2764,26 @@ function divInt(n) {
2630
2764
  * application code (scope the plan-cache mode to the affected reads), and the
2631
2765
  * index that looks like a fix is measured NOT to be one.
2632
2766
  */
2633
- function renderPlanDivergence(divergence) {
2634
- const { findings, notices } = divergence;
2767
+ function renderPlanDivergence(divergence, attached) {
2768
+ const { notices } = divergence;
2769
+ // Anything already rendered as evidence on a missing-index finding is NOT
2770
+ // repeated here: one column, one problem, one remedy.
2771
+ const rendered = new Set(attached.values());
2772
+ const findings = divergence.findings.filter((f) => !rendered.has(f));
2773
+ // Every divergence finding was attached above, so this section has no entries
2774
+ // of its own. The pointer, the gates and the scored population still belong in
2775
+ // the report: they are output of THIS check, and a reader must be able to tell
2776
+ // "considered and clean" from "never looked".
2777
+ //
2778
+ // Printed BEFORE the notices rather than inside an early return: an early
2779
+ // return that also required `notices.length === 0` dropped both blocks
2780
+ // whenever any candidate lacked a pg_stats row, which is the normal reason a
2781
+ // notice exists.
2782
+ if (findings.length === 0 && rendered.size > 0) {
2783
+ console.log(` ${dim(`Cached-plan divergence: ${rendered.size} finding(s), shown with the index findings above.`)}`);
2784
+ renderDivergenceGates(divergence);
2785
+ newline();
2786
+ }
2635
2787
  if (findings.length === 0 && notices.length === 0)
2636
2788
  return;
2637
2789
  if (findings.length > 0) {
@@ -2644,16 +2796,39 @@ function renderPlanDivergence(divergence) {
2644
2796
  console.log(` ${dim('land on the other side of a plan boundary from the real value, the plan flips.')}`);
2645
2797
  newline();
2646
2798
  }
2799
+ const analyzedLabel = (f) => f.lastAnalyze === null
2800
+ ? 'last ANALYZE unknown'
2801
+ : `last analyzed ${Math.max(0, Math.round((Date.now() - f.lastAnalyze.getTime()) / 86_400_000))}d ago`;
2647
2802
  for (const f of findings) {
2803
+ if (f.branch === 'unindexed-filter') {
2804
+ // Only reached when the column has no missing-index finding to hang this
2805
+ // on (an index that exists but cannot serve the equality: partial,
2806
+ // expression, or non-btree). The remedy is still an index, not a
2807
+ // plan-cache setting, so this entry never suggests forceCustomPlan.
2808
+ console.log(` ${yellow(symbols.warning)} ${bold(cyan(`${f.table}.${f.column}`))} ${gray('UNINDEXED-FILTER FLIP')}`);
2809
+ console.log(` ${dim(symbols.tee)} ${divInt(f.rows)} rows in ${divInt(f.pages)} pages, rarest value ~${bold(divInt(f.rarestBucket))} rows, below the assumed LIMIT ${f.assumedLimit}`);
2810
+ console.log(` ${dim(symbols.tee)} no index serves ${f.column} = $1, so the good plan is a seq scan (${divInt(f.pages)} pages);`);
2811
+ console.log(` ${dim(`a promoted generic plan keeps the ordered "${f.orderColumn}" walk and reads up to ~${divInt(f.tuplesWalked ?? f.rows)} tuples`)}`);
2812
+ console.log(` ${dim('before it fills the LIMIT.')}`);
2813
+ for (const line of divergenceAmplificationLines(f))
2814
+ console.log(` ${dim(line)}`);
2815
+ console.log(` ${dim(symbols.tee)} ${dim(`filter-column correlation ${f.correlation.toFixed(2)}, ${analyzedLabel(f)}`)}`);
2816
+ console.log(` ${dim(symbols.tee)} ${dim('the fix is an index that can serve this equality. A partial or expression index')}`);
2817
+ console.log(` ${dim('on the column does not: the planner has no path for the bare predicate. A hash')}`);
2818
+ console.log(` ${dim('index does, and a column served by one is scored by the other rule instead.')}`);
2819
+ console.log(` ${dim(symbols.teeEnd)} ${dim('confirm with YOUR values before changing anything:')}`);
2820
+ for (const line of f.diagnosticSql.split('\n')) {
2821
+ console.log(` ${green(line)}`);
2822
+ }
2823
+ newline();
2824
+ continue;
2825
+ }
2648
2826
  console.log(` ${yellow(symbols.warning)} ${bold(cyan(`${f.table}.${f.column}`))} ${gray('SPARSE-VALUE FLIP')}`);
2649
2827
  console.log(` ${dim(symbols.tee)} generic estimate ${bold(divInt(f.genericEstimate))} rows ${dim(`(${divInt(f.rows)} rows / ${divInt(f.distinctValues)} distinct values)`)}`);
2650
2828
  console.log(` ${dim(symbols.tee)} rarest value bucket ${bold(divInt(f.rarestBucket))} rows ${dim('(pg_stats most_common_freqs / residual bucket)')}`);
2651
- console.log(` ${dim(symbols.tee)} crossover ${bold(divInt(f.crossoverRows))} rows ${dim(`(sqrt(limit ${f.assumedLimit} x ${divInt(f.pages)} pages); ${divInt(f.crossoverRowsWide)} at limit ${f.thresholds.wideLimit})`)}`);
2652
- const analyzed = f.lastAnalyze === null
2653
- ? 'last ANALYZE unknown'
2654
- : `last analyzed ${Math.max(0, Math.round((Date.now() - f.lastAnalyze.getTime()) / 86_400_000))}d ago`;
2655
- console.log(` ${dim(symbols.tee)} ${dim(`values below the crossover: ${divInt(f.valuesBelowCrossover)} of ${divInt(f.distinctValues)}, correlation ${f.correlation.toFixed(2)}, ${analyzed}`)}`);
2656
- console.log(` ${dim(symbols.tee)} for such a value the generic plan walks ~${bold(divInt(f.walkPages))} of ${divInt(f.pages)} pages ${dim(`(${Math.round(f.walkFraction * 100)}% of the table)`)}`);
2829
+ console.log(` ${dim(symbols.tee)} crossover ${bold(divInt(f.crossoverRows ?? 0))} rows ${dim(`(sqrt(limit ${f.assumedLimit} x ${divInt(f.pages)} pages); ${divInt(f.crossoverRowsWide ?? 0)} at limit ${f.thresholds.wideLimit})`)}`);
2830
+ console.log(` ${dim(symbols.tee)} ${dim(`values below the crossover: ${divInt(f.valuesBelowCrossover ?? 0)} of ${divInt(f.distinctValues)}, filter-column correlation ${f.correlation.toFixed(2)}, ${analyzedLabel(f)}`)}`);
2831
+ console.log(` ${dim(symbols.tee)} for such a value the generic plan walks ~${bold(divInt(f.walkPages ?? 0))} of ${divInt(f.pages)} pages ${dim(`(${Math.round((f.walkFraction ?? 0) * 100)}% of the table)`)}`);
2657
2832
  console.log(` ${dim(`for reads shaped WHERE ${f.column} = $1 ORDER BY ${f.orderColumn} LIMIT $n,`)}`);
2658
2833
  console.log(` ${dim("where the custom plan reads only that value's own rows.")}`);
2659
2834
  console.log(` ${dim(symbols.tee)} ${dim('No amplification figure is printed, deliberately. This models how many rows a')}`);
@@ -2666,6 +2841,7 @@ function renderPlanDivergence(divergence) {
2666
2841
  newline();
2667
2842
  }
2668
2843
  if (findings.length > 0) {
2844
+ const first = findings.find((f) => f.branch === 'sparse-value');
2669
2845
  console.log(` ${bold('What to do, in order:')}`);
2670
2846
  console.log(` 1. Check that this shape is promoted AT ALL. Step 1 of the block above: while`);
2671
2847
  console.log(` ${dim('generic_plans is 0, Postgres is planning with your real values and there is nothing')}`);
@@ -2673,27 +2849,68 @@ function renderPlanDivergence(divergence) {
2673
2849
  console.log(` 2. If it does promote, compare the two plans. Both SETs matter: without them a`);
2674
2850
  console.log(` ${dim('repeated seq scan resumes where the last one stopped and a catastrophic case reads')}`);
2675
2851
  console.log(` ${dim('as harmless.')}`);
2676
- console.log(` 3. If the flip is real, scope the fix to those reads:`);
2677
- const first = findings[0];
2678
- console.log(` ${cyan(`db.${first.table}.findMany({ where: { ${first.columnField}: value }, orderBy: { ${first.orderColumnField}: 'asc' },`)}`);
2679
- console.log(` ${cyan(` limit: 20, forceCustomPlan: true })`)}`);
2680
- console.log(` ${dim('That withholds the prepared-statement NAME for that one query, so the driver')}`);
2681
- console.log(` ${dim('re-parses it every execution and it is always planned with the real values. No')}`);
2682
- console.log(` ${dim('GUC, no SET LOCAL, no transaction, no extra round trip.')}`);
2683
- console.log(` 4. Do NOT set planCacheMode on the client to fix this. There are measured shapes`);
2684
- console.log(` ${dim('where a generic plan is dramatically better (an unordered LIMIT over a value whose')}`);
2685
- console.log(` ${dim('rows are packed at the end of the heap: 4,262 buffers custom vs 71 generic on a')}`);
2686
- console.log(` ${dim('reproducible fixture). A custom plan is not automatically the better plan.')}`);
2687
- console.log(` 5. A composite index on (${first.column}, ${first.orderColumn}) makes the GOOD plan better. It`);
2688
- console.log(` ${dim('does NOT stop the generic plan from choosing the other one, and it can widen the gap.')}`);
2689
- console.log(` ${dim('Add it for the custom-plan win, not as a fix for this finding.')}`);
2852
+ if (first) {
2853
+ // The OPTION is named first and both call shapes follow, so no step
2854
+ // assumes which client the reader is holding. The compat example uses
2855
+ // Prisma's `take`: printing `limit` there would be a second wrong
2856
+ // instruction, since `limit` is a Turbine spelling compat does not read.
2857
+ console.log(` 3. If the flip is real, scope the fix to those reads with ${cyan('forceCustomPlan')}. It`);
2858
+ console.log(` ${dim('withholds the prepared-statement NAME for that one query, so the driver re-parses')}`);
2859
+ console.log(` ${dim('it every execution and it is always planned with the real values. No GUC, no SET')}`);
2860
+ console.log(` ${dim('LOCAL, no transaction, no extra round trip.')}`);
2861
+ // Hanging indent rather than an alignment that pretends to line up: the
2862
+ // call's own width depends on the table name, so a fixed padding column
2863
+ // misaligns on every schema but the one it was written against.
2864
+ const args = `where: { ${first.columnField}: value }, orderBy: { ${first.orderColumnField}: 'asc' },`;
2865
+ // The accessor is the camelCase FIELD spelling, not the raw table name:
2866
+ // TurbineClient and the code generator both define table accessors through
2867
+ // snakeToCamel, so `db.inventory_location` is undefined on every
2868
+ // snake_case schema. The finding's own `columnField` / `orderColumnField`
2869
+ // are already field-space for the same reason.
2870
+ console.log(` ${dim('On the core client:')}`);
2871
+ console.log(` ${cyan(`db.${snakeToCamel(first.table)}.findMany({`)}`);
2872
+ console.log(` ${cyan(args)}`);
2873
+ console.log(` ${cyan('limit: 20, forceCustomPlan: true,')}`);
2874
+ console.log(` ${cyan('})')}`);
2875
+ console.log(` ${dim("Through turbine-orm/prisma-compat, the same option on the delegate call (Prisma's")}`);
2876
+ console.log(` ${dim('`take`, not `limit`). The Prisma MODEL name is not knowable from the schema side,')}`);
2877
+ console.log(` ${dim('so substitute your own:')}`);
2878
+ console.log(` ${cyan('compat.<Model>.findMany({')}`);
2879
+ console.log(` ${cyan(args)}`);
2880
+ console.log(` ${cyan('take: 20, forceCustomPlan: true,')}`);
2881
+ console.log(` ${cyan('})')}`);
2882
+ console.log(` ${dim(`The compat passthrough requires turbine >= ${COMPAT_PASSTHROUGH_VERSION}. On an older version the option`)}`);
2883
+ console.log(` ${dim('is accepted and ignored there, so confirm at the wire with the same')}`);
2884
+ console.log(` ${dim('pg_prepared_statements check in step 1 rather than assuming it took effect.')}`);
2885
+ console.log(` 4. Reaching for a database-wide plan_cache_mode is not the fix, whichever client you`);
2886
+ console.log(` ${dim('use. There are measured shapes where a generic plan is dramatically better: an')}`);
2887
+ console.log(` ${dim('unordered LIMIT over a value whose rows are packed at the end of the heap reads')}`);
2888
+ console.log(` ${dim('4,262 buffers under a custom plan against 71 under a generic one. That fixture is')}`);
2889
+ console.log(` ${dim('printed in full at turbineorm.dev/relations, so the number is checkable rather')}`);
2890
+ console.log(` ${dim('than asserted. Pinning every statement')}`);
2891
+ console.log(` ${dim('in one direction trades this finding for its mirror image. That applies equally to')}`);
2892
+ console.log(` ${dim("Turbine's client-level `planCacheMode` and to a SET or ALTER ROLE applied outside")}`);
2893
+ console.log(` ${dim('Turbine.')}`);
2894
+ console.log(` 5. A composite index on (${first.column}, ${first.orderColumn}) makes the GOOD plan better. It`);
2895
+ console.log(` ${dim('does NOT stop the generic plan from choosing the other one, and it can widen the gap.')}`);
2896
+ console.log(` ${dim('Add it for the custom-plan win, not as a fix for this finding.')}`);
2897
+ }
2898
+ // Stated separately because the first remedy genuinely differs by branch: an
2899
+ // UNINDEXED column's flip is fixed by the index, and a per-query plan-cache
2900
+ // override there would only paper over a table scan.
2901
+ if (findings.some((f) => f.branch === 'unindexed-filter')) {
2902
+ console.log(` ${first ? 6 : 3}. A finding on an UNINDEXED column has a different FIRST remedy: add an index that`);
2903
+ console.log(` ${dim('serves the equality, then re-run doctor and re-score. The index moves the')}`);
2904
+ console.log(` ${dim('divergence in both directions at once (it makes the good plan much cheaper, which')}`);
2905
+ console.log(` ${dim('widens the ratio, and on a measured fixture it also stopped Postgres promoting the')}`);
2906
+ console.log(` ${dim('generic plan at all), so do not assume the finding is closed by adding it.')}`);
2907
+ }
2690
2908
  newline();
2691
2909
  console.log(` ${dim('This finding is derived from statistics, not from your traffic: it says the DISTRIBUTION')}`);
2692
- console.log(` ${dim('admits a damaging flip, not that a query is running one today. It models ONE shape,')}`);
2693
- console.log(` ${dim('the rare value that loses its bitmap plan. It cannot see where a value physically')}`);
2694
- console.log(` ${dim('sits in the heap, so a clean report is not evidence of immunity.')}`);
2695
- console.log(` ${dim(`Gates: the wrong plan must walk >= ${PLAN_DIVERGENCE_THRESHOLDS.minWalkPages} pages and >= ${Math.round(PLAN_DIVERGENCE_THRESHOLDS.minWalkFraction * 100)}% of the table, at an assumed`)}`);
2696
- console.log(` ${dim(`LIMIT ${PLAN_DIVERGENCE_THRESHOLDS.assumedLimit}. ${divergence.candidatesConsidered} column(s) were scored. Skip this section with --no-plan-divergence.`)}`);
2910
+ console.log(` ${dim('admits a damaging flip, not that a query is running one today. It cannot see where a')}`);
2911
+ console.log(` ${dim('value physically sits in the heap, so a clean report is not evidence of immunity, and')}`);
2912
+ console.log(` ${dim('a column that is neither an FK nor indexed is not in the scored population at all.')}`);
2913
+ renderDivergenceGates(divergence);
2697
2914
  newline();
2698
2915
  }
2699
2916
  if (notices.length > 0) {
package/dist/client.js CHANGED
@@ -27,7 +27,7 @@ import { setErrorMessageMode, TimeoutError, UnsupportedFeatureError, ValidationE
27
27
  import { ObserveEngine } from './observe.js';
28
28
  import { executePipeline, pipelineSupported } from './pipeline.js';
29
29
  import { QueryInterface, } from './query/index.js';
30
- import { closestName, markTurbineParser, quoteIdent, registerUtcTemporalParsers, warnParserOverwrite, } from './query/utils.js';
30
+ import { markTurbineParser, quoteIdent, registerUtcTemporalParsers, suggestKey, warnParserOverwrite, } from './query/utils.js';
31
31
  import { shouldWarnOnce, WARN_NS } from './query/warn-registry.js';
32
32
  import { createSubscription, validateChannel, } from './realtime.js';
33
33
  import { buildTypedSql, TypedSqlQuery } from './typed-sql.js';
@@ -141,50 +141,14 @@ const CONFIG_KEY_SET = new Set(Object.keys(TURBINE_CONFIG_KEYS));
141
141
  * engine factories' first argument. Same story as `schema`.
142
142
  */
143
143
  const NON_CONFIG_KEYS = new Set(['queryInterfaceFactory', 'schema', 'url']);
144
- /** camelCase name → its lowercased words (`logQueryParams` → log, query, params). */
145
- function camelWords(name) {
146
- return name
147
- .split(/(?=[A-Z])/)
148
- .map((w) => w.toLowerCase())
149
- .filter(Boolean);
150
- }
151
144
  /**
152
145
  * The real config key `key` most likely meant, or null when nothing is close.
153
146
  *
154
- * {@link closestName} (the same helper the unknown-COLUMN message uses) decides
155
- * first, so both diagnostics rank near-misses identically. It is bounded by edit
156
- * distance, which covers typos but not the miss this warning exists for: a
157
- * guessed name that omits a whole word. `logParams` is five edits from
158
- * `logQueryParams`, past the bound, yet it names the same words in the same
159
- * order, so a second pass accepts a candidate whose camelCase words CONTAIN the
160
- * guess's words in order, preferring the one that adds fewest words.
147
+ * {@link suggestKey} is shared with the prisma-compat query-option warner, so
148
+ * both diagnostics rank near-misses identically.
161
149
  */
162
150
  function suggestConfigKey(key) {
163
- const direct = closestName(key, CONFIG_KEY_SET);
164
- if (direct)
165
- return direct;
166
- const wanted = camelWords(key);
167
- if (wanted.length < 2)
168
- return null;
169
- let best = null;
170
- let bestExtra = Number.POSITIVE_INFINITY;
171
- for (const candidate of CONFIG_KEY_SET) {
172
- const words = camelWords(candidate);
173
- if (words.length <= wanted.length)
174
- continue;
175
- let i = 0;
176
- for (const w of words)
177
- if (w === wanted[i])
178
- i++;
179
- if (i !== wanted.length)
180
- continue;
181
- const extra = words.length - wanted.length;
182
- if (extra < bestExtra) {
183
- bestExtra = extra;
184
- best = candidate;
185
- }
186
- }
187
- return best;
151
+ return suggestKey(key, CONFIG_KEY_SET);
188
152
  }
189
153
  /**
190
154
  * Dev-mode notice for a key on the config object that is not part of the config