Skip to content

[Bug]: Visual Explain enables ANALYZE by default for REPLACE, MERGE and data-modifying CTEs #884

Closed

Description

@debba

Describe the bug

Visual Explain turns Analyze on by default unless the statement is detected as data-modifying. With ANALYZE the statement is really executed. The detection only looks at the first keyword:

// src/utils/sql.ts
export function isDataModifyingQuery(query: string): boolean {
  const trimmed = query.trim().toUpperCase();
  return trimmed.startsWith("INSERT") || trimmed.startsWith("UPDATE") || trimmed.startsWith("DELETE")
    || trimmed.startsWith("DROP") || trimmed.startsWith("ALTER") || trimmed.startsWith("TRUNCATE");
}

So these are treated as read-only, get ANALYZE enabled with no warning, and are executed when the plan is fetched:

  • REPLACE INTO … (MySQL/MariaDB/SQLite)
  • MERGE INTO … (PostgreSQL 15+, SQL Server)
  • data-modifying CTEs such as WITH d AS (DELETE FROM t RETURNING *) SELECT * FROM d
  • statements preceded by a comment (-- note\nDELETE …)

MERGE is explainable per src/utils/sqlSplitter/classify.ts, so it does reach Visual Explain.

To Reproduce

  1. Open a PostgreSQL connection with a scratch table t.
  2. Select WITH d AS (DELETE FROM t RETURNING *) SELECT * FROM d and open Visual Explain.
  3. Analyze is ticked and there's no data-modification warning; the rows are deleted.

Expected behavior

Any statement that can modify data defaults to Analyze off and shows the existing warning.

Where to start

  • src/utils/sql.ts isDataModifyingQuery: strip leading comments, add REPLACE, MERGE, UPSERT, CREATE, GRANT/REVOKE, CALL/EXEC, and for WITH look for INSERT|UPDATE|DELETE|MERGE outside string literals. The tokenizer in src/utils/sqlSplitter/ may help.
  • Callers: src/hooks/useExplainAnalyze.ts, src/components/modals/VisualExplainModal.tsx.

Acceptance criteria

  • The cases above (and their lowercase / leading-comment variants) are detected as data-modifying.
  • Plain SELECT and read-only WITH … SELECT still default to Analyze on.
  • Unit tests for every case.

Contributor notes

  • Follow the repo rules in .rules/ (TypeScript, React, Rust, testing).
  • Any new UI string needs a key in every locale under src/i18n/locales/.
  • Run pnpm lint, pnpm typecheck and pnpm test (and cargo test for Rust changes) before opening the PR.
  • Questions? Ask here or on Discord.

Activity

  1. added
    bug
    good first issue
    on Oct 7, 2026
  2. added a commit that references this issue on Oct 8, 2026
    61dba51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions