Skip to content

ADFA-7301 SQLite debugger P2-6: sqlite3 shell tools (suggest_indexes, lint_foreign_keys, dump, export, .dbinfo) - #2323

Open
davidschachterADFA wants to merge 21 commits into
sqlite-debugger/ADFA-7297from
sqlite-debugger/ADFA-7301
Open

davidschachterADFA wants to merge 21 commits into
sqlite-debugger/ADFA-7297from
sqlite-debugger/ADFA-7301

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

ADFA-7301

Phase 2, P2-6 of the SQLite debugger (ADFA-7131): five read-only tools from the sqlite3 shell. Stacked on #2319 (P2-2).

What it adds (subprojects/sqlite-debugger; every tool read-only, with readOnlyHint)

  • suggest_indexes (.expert): SQLite's ext/expert, vendored unchanged from 3.49.1 in src/main/cpp/expert/, compiled into libsqlitedbg.so by sqlitedbg_expert.c through the api table. It runs on the read-only server connection; expert's two in-memory connections open inside sqlitedbg_private_open, so they are neither listed nor traced.
    • sample defaults to 0: no row of the app's data is read, and suggestions rest on SQLite's default estimates. 1 to 100 opts in to sampling, which on a database not in WAL mode stops at 1.5 s like a query (cap_non_wal, now shared with bridge_query). David chose this default with the opt-in.
    • Sampling registers sqlite_expert_sample on the server connection, and at 100 also sqlite_expert_rem, and removes them after; the temp table of sampled rows is on expert's own connection.
    • Limit: only read statements can be analysed. Expert first prepares each statement on the server connection, whose authorizer refuses INSERT, UPDATE and DELETE ("not authorized"). For an UPDATE or DELETE, send a SELECT with the same WHERE.
  • lint_foreign_keys (.lint fkey-indexes): the shell's query-plan globs, in Kotlin. Limit: no COLLATE clause for a child column whose collation differs from its parent key's. When the list of foreign-key columns is cut at 1000 rows, the last key is left out, so no key is checked on part of its columns (truncated: true).
  • dump (.dump): the shell's order. Virtual tables go in through writable_schema, and with table set to a virtual table its shadow tables come too (found through pragma_table_list, type 'shadow'; another virtual table whose name starts with the same name and "_" comes along). Rows come from quote() in rowid order: _rowid_ binds to a user column of that name, and a WITHOUT ROWID table gets the planner's order. Generated columns are left out. Paged by cursor: 1000 statements and about 4 MiB a page, counted in characters, so a page can reach about 8 MiB. A statement over 4 MiB, a CREATE included, is cut and listed in truncated_statements.
  • export (.mode csv, .mode insert): RFC 4180 CSV, or INSERT literals that read back the same. Values are cut at 64 KiB, lowered for a wide result so that one row stays within 4 MiB (never below query's 4096 bytes). The column count comes from the statement wrapped in SELECT * FROM (...) LIMIT 0, which SQLite ends before stepping the inner query; a statement that can't be wrapped (PRAGMA, EXPLAIN) runs to its first row twice.
  • get_stats (.dbinfo): adds the PRAGMA values, schema object counts and schema size; the header fields through sqlite_dbpage when SQLite has it, otherwise header_unavailable. It never reads the file directly, since closing a descriptor would drop the app's POSIX locks.
  • ShellFormat.kt holds the pure formatting (CSV, SQL literals, the glob, the header), tested alone.

Size of libsqlitedbg.so (from the AARs; before is #2319 at a8fca8c):

AAR ABI Before After Added
release arm64-v8a 27,816 B 51,440 B 23,624 B
release armeabi-v7a 20,380 B 37,248 B 16,868 B
debug arm64-v8a 36,016 B 73,472 B 37,456 B
debug armeabi-v7a 23,548 B 47,048 B 23,500 B

When this PR and #2312 (P2-3) meet: #2312's PRAGMA allowlist must let through the pragma functions these tools use: pragma_foreign_key_list, pragma_table_info, pragma_table_xinfo, pragma_table_list, and pragma_index_list inside expert. Expert's sampling also registers its functions on the server connection.

Commits

Review before this PR (a fresh read of 2a1cf82, with the vendored expert compared byte for byte with SQLite's version-3.49.1 tag; all nine fixed as David chose):

  • F01 (IMPORTANT, documented): suggest_indexes can't analyse INSERT, UPDATE or DELETE; the description and README say so, and a C test shows each is refused.
  • F02 (IMPORTANT): export's 64 KiB per value let one wide first row reach about 125 MB in the app's heap; the per-value limit now falls with the column count.
  • F03 (IMPORTANT): dump cut a CREATE over 64 KiB silently; schema SQL is read at the 4 MiB statement limit, a cut one is listed, and the "more than 1000 tables" error names the byte budget when that is the cause.
  • F04, F09 (docs): the page size, and the row order for _rowid_ columns and WITHOUT ROWID tables.
  • F05 (docs): where expert's sampling functions and temp table live.
  • F06 (test): sampling at 50 on 5000 rows; both functions are gone after, temp is empty, and reads still work while writes are refused.
  • F07: a cut foreign-key list drops its last key.
  • F08: a virtual table's shadow tables in a one-table dump.

Tests

  • At c8bf921: :subprojects:sqlite-debugger:testDebugUnitTest (155 tests) and spotlessCheck pass; no C change since 1133a58.
  • At 1133a58: test.sh passes (ASan+UBSan 21267 checks, TSan 21277, JNI 346, 0 failures), and so do :subprojects:sqlite-debugger:testDebugUnitTest (154 tests) and spotlessCheck; 9e9e5c8 was not built alone.
    • Checked on SQLite 3.49.1: the WITHOUT ROWID plan text. The single chain fails at 500 columns, while the groups prepare at 2000 and give byte-identical rows at 250.
    • Revert proofs: without the third glob and the groups, exactly the two new tests fail. Without the whole column list, the 2000-column SELECT stops at c1000.
  • Local at c1596a8: test.sh passes (ASan+UBSan 21281 checks, TSan 21272, JNI 345, 0 failures); :subprojects:sqlite-debugger:testDebugUnitTest (151 tests), spotlessCheck and assembleV8Debug pass. test.sh's JNI copy of libsqlite3x.so now builds with FTS5, so the shadow-table query runs against real SQLite.
  • At 323d3d3: test.sh passes (ASan+UBSan 21281 checks, TSan 21287, JNI 346, 0 failures), and so do :subprojects:sqlite-debugger:testDebugUnitTest (151 tests) and spotlessCheck.
  • Revert proof of the review fixes: with DatabaseTools.kt as before them, 9 of 151 unit tests fail, each for its fix (F02 three, F03 three, F07, F08, and one whose expected query text changed).
  • From the cloud session at 2a1cf82: test.sh 0 failures (JNI 333), 135 JVM tests, and revert proofs: without the private open, 3 connections are listed and 67 statements traced; sampling forced on fails the no-app-data check; without the INTEGER PRIMARY KEY glob a lint test fails; an unquoted empty string fails both CSV tests; without the limit on the sampling path, "sampling was interrupted" fails.

Device check (Galaxy J7, Android 9, armeabi-v7a, requery's libsqlite3x.so, 2026-11-26; ADFA-7301 41612): suggest_indexes at sample 0 and 50 suggested notes(title) for a WHERE on title (about 0.14 s a call), and an UPDATE was refused with "not authorized"; lint_foreign_keys found no foreign keys (RoomNotes has none); dump rebuilt the database in sqlite3 with no error, with every table's row count, the sqlite_master listing and the rows matching; export's INSERT form read back the same rows; get_stats returned header_unavailable, as requery's libsqlite3x.so has no sqlite_dbpage. The database was small, so dump's paging was not reached.

No screens added or changed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn


Code Reviewer: Code Reviewer couldn't review this pull request
The pull request author does not have access to AI for the associated Atlassian organization.

claude and others added 16 commits November 26, 2026 06:52
…_foreign_keys, dump, export, .dbinfo)

Phase 2, P2-6 (ADFA-7131 comments 41477, 41489). On P2-2 (ADFA-7297).
All new tools are read-only, with readOnlyHint.

- suggest_indexes (.expert): SQLite's ext/expert, vendored unchanged
  from 3.49.1 (src/main/cpp/expert, public domain), compiled into
  libsqlitedbg.so by sqlitedbg_expert.c through the api table. It runs
  on the read-only server connection. Expert's two in-memory
  connections open inside sqlitedbg_private_open, so they are neither
  listed nor traced. Sampling is off: no row of the app's data is read
  and nothing is added to the server connection, so suggestions rest
  on SQLite's default estimates. Deadline: progress handler on expert's
  connections plus a check between its steps. JNI suggestIndexes.
- lint_foreign_keys (.lint fkey-indexes): the shell's query-plan globs,
  in Kotlin. Limit: no COLLATE clause for a child column whose
  collation differs from its parent key's.
- dump (.dump): the shell's order (foreign_keys=OFF, BEGIN, tables and
  rows, sqlite_sequence, indexes/triggers/views, COMMIT), virtual
  tables through writable_schema, rows by quote() in rowid order,
  generated columns left out. Paged by cursor: 1000 statements, about
  4 MiB; a statement over 4 MiB is cut and listed.
- export (.mode csv, .mode insert): RFC 4180 CSV; INSERT literals that
  read back the same. Values cut at 64 KiB.
- get_stats (.dbinfo): PRAGMA values, schema object counts and size,
  and header fields through sqlite_dbpage when SQLite has it (else
  header_unavailable). Never reads the file itself: closing a
  descriptor would drop the app's POSIX locks.
- ShellFormat (CSV, SQL literals, glob, header) is pure and tested
  alone. README updated.

The ticket's UNVERIFIED item: ext/expert builds against the 3.49
headers through sqlite3ext.h with no warning, and runs on a read-only
server connection with its authorizer (C and JNI tests). Not yet on
requery's libsqlite3x.so on a device.

Size (estimate, clang 18 -Os with host headers, not the NDK): expert
adds about 22 KB of code on arm64 and 15 KB on armv7 (Thumb-2).

Tests: test.sh passes (ASan+UBSan, TSan, JNI 324 checks, including the
SQL every new tool sends). DatabaseToolsTest, ShellToolsTest,
McpProtocolTest and EventDecoderTest pass (134), compiled with the
Kotlin compiler outside Gradle. Revert proofs: no private open fails
the C test (3 connections listed, 67 statements traced); sampling on
fails "no row of the app's data was read" (3 sampling queries); no
INTEGER PRIMARY KEY glob fails "an INTEGER PRIMARY KEY covers it"; an
unquoted empty string fails both CSV tests. Not run here: Gradle,
spotlessCheck, a device, the real .so sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAZ8yszEP4MqbR9PLba
…ger/ADFA-7301

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAZ8yszEP4MqbR9PLba
David asked for it after the default (no sampling) was explained.

- sample (default 0) is expert's EXPERT_CONFIG_SAMPLE: that percentage of
  the rows of each table with a candidate index is read on the server
  connection, for estimates from the data. Expert registers two
  functions and a temp table there for the run and drops them after.
- Sampling reads the app's rows, so on a database not in WAL mode it
  stops after 1.5 s like a query: the cap moved out of bridge_query into
  cap_non_wal, used by both; sampling's message says to lower sample.
- Description, README and sqlitedbg.h updated.

Tests: test.sh passes (JNI 333 checks): sampling 6,000,000 rows of a
non-WAL database is stopped with the sampling message while the app
commits, and sample 0 on it still suggests with no cap; sample 100
changes the suggestion on a two-row table; the C test sees the sampling
queries and nothing left after. JVM tests pass (135). Revert proof: no
cap on the sampling path fails "sampling was interrupted".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAZ8yszEP4MqbR9PLba
…gger/ADFA-7301

Brings in #2319's commits after ffb3f18: the spotless reformat
(e73b348), the args description (e450122), the test formatting
(e648501) and the merge of ffb3f18 (a8fca8c).

No conflicts. Git merged DatabaseTools.kt and DatabaseToolsTest.kt,
which both sides changed, on its own. This branch's own diff is
unchanged: the +/- lines of `git diff ffb3f18 2a1cf82` and
`git diff a8fca8c HEAD` are identical, in the same 20 files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
spotlessKotlinCheck failed on DatabaseTools.kt, ShellFormat.kt and
ShellToolsTest.kt; this is spotlessApply's output and nothing else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
…live

The texts said sampling registers two functions and a temp table on the
server connection. In the vendored code the temp table is made on expert's
own connection (idxBuildSampleTable on p->dbv); sqlite_expert_sample goes on
db, and sqlite_expert_rem only at sample 100 (below that it is on p->dbv).
Comments and docs only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
…nnection

Only 0 and 100 were tested. At 50 on a 5000-row table: suggestions come
back, the sampling reads run on the server connection, and afterwards
neither sqlite_expert_rem nor sqlite_expert_sample exists there, temp is
empty, reads work and writes are still refused. The 1.5 s non-WAL cap is
not exercised at 50: it sits in cap_non_wal, independent of the sample
value, and the JNI test already reaches it at 100 with 6,000,000 rows;
reaching it at 50 would cost the same seconds again for no new path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
dump read the CREATE statements (tables, and indexes/triggers/views) with
get_schema's 64 KiB limit and never looked at isTruncated, so a longer
CREATE was cut mid-statement, written with ";" and the dump still looked
whole. They are now read with dump's own statement limit (4 MiB), and a
statement still cut is listed in truncated_statements, as a cut row is.
`complete` still means "no more pages": it drives paging, so a cut statement
does not clear it; truncated_statements says the dump is not whole.

The "more than 1000 tables" error also fired when the 4 MiB byte budget, not
the row limit, ended the query; it now says which one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
…change

ktlint splits the tailCut chain added in the previous commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
… one

dump with table set to a virtual table filtered by name = ? and tbl_name = ?,
so its shadow tables (docs_fts_data and the like) were not dumped, though the
description and README say they are, and such a dump does not restore. The
virtual table's shadow tables are now found with pragma_table_list (type
'shadow', SQLite 3.37+) by the virtual table's name and "_", and dumped with
their indexes and triggers. A plain table does not run the extra query.

Known gap: if another virtual table's name also starts with the same prefix
(docs and docs_fts), its shadow tables come along; the README says so.

The JNI test builds libsqlite3x with FTS5 (nothing else needs it) to check
against real SQLite that the server's authorizer allows the query and that the
shadow tables are the five FTS5 ones, not a table that merely shares the prefix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
The first query is cut at 1000 rows, which can end inside a foreign key, so
that key had only some of its columns and its EXPLAIN QUERY PLAN check ran on
a partial key and could report it covered. When the list is cut, the rows of
the last (table, id) group are dropped; truncated stays true.

A cut that falls exactly between two keys also drops a whole key that was
complete; truncated says the list is partial either way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
The engine always returns the first row whole, and export allowed 64 KiB per
cell, so one row of 2000 columns could hold about 125 MB in the app's heap.
The limit is now MAX_TOTAL_BYTES / columns, never above 64 KiB and never below
query's 4096 bytes (a 2000-column row is then bounded as in query, about
7.8 MiB, as the README documents).

The column count comes from a first call that wraps the statement in
SELECT * FROM (...) LIMIT 0: SQLite ends before stepping the inner query
(JNI test: an endless recursive CTE returns at once), so the statement is not
run twice. A statement that cannot be wrapped (PRAGMA, EXPLAIN) is asked for
its first row instead; those are cheap, and the README says such a statement
may run to its first row twice. The C engine is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
ext/expert prepares each statement on the server connection first, and its
read-only authorizer refuses INSERT, UPDATE and DELETE, so they fail with
"not authorized". Say so in the tool description and the README, with the
workaround (a SELECT with the same WHERE). No behaviour change; the C test
now checks the three statements fail with that message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
The page budget counts UTF-16 characters (bytes += st.length) while the
engine counts UTF-8 bytes, and the first-row rule lets a statement of up to
4 MiB through when the budget is nearly used, so a page can reach about
8 MiB. The description and README said about 4 MiB. The counting is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
…only

The README half of the previous commit (the tool description and the C test
were committed there).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
Rows are read ORDER BY _rowid_, which binds to a user column of that name,
so such a table is ordered by the column; a WITHOUT ROWID table gets no
ORDER BY and comes in the planner's order (normally primary key order, not
guaranteed). Document both in the description and the README, which said
paging was stable. No code change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

…qlite-debugger/ADFA-7301

Brings in the misspelt named-args key message from #2319.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn

@davidschachterADFA davidschachterADFA left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of #2323 at 323d3d3138. Stack: #2311 (P2-1) <- #2319 (P2-2) <- #2323 (this, top); claims checked at this head. The vendored ext/expert files were compared byte for byte with SQLite 3.49.1's and are unchanged.

MINOR

  • subprojects/sqlite-debugger/src/main/java/org/appdevforall/sqlitedebugger/mcp/DatabaseTools.kt:832 - lint_foreign_keys reports a WITHOUT ROWID child's primary-key-covered foreign key as uncovered
  • subprojects/sqlite-debugger/src/main/java/org/appdevforall/sqlitedebugger/mcp/DatabaseTools.kt:1095 - dump fails on a table with about 500 or more columns (expression depth limit)

Verdict: COMMENT. This PR is authored by the account posting the review, and GitHub refuses APPROVE and REQUEST_CHANGES on your own PR. Severity rules: CodeOnTheGo REVIEW.md and CLAUDE.md (a ticket moves to QA with no outstanding critical, high or medium findings); addons has no written rule, so the default applied.


Generated by Claude Code

davidschachterADFA and others added 3 commits November 26, 2026 15:21
…gger/ADFA-7301

Brings in the #2311 review fix through #2319.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
…rs the key

A WITHOUT ROWID child whose primary key starts with the foreign key's
columns plans as "SEARCH t USING PRIMARY KEY (x=? AND y=?)", which neither
of the shell's two globs matches, so the key was reported uncovered with a
create_index that duplicated the primary key (#2323 review, comment
4713256686; the shell's .lint has the same gap). A third glob matches that
plan; its b-tree holds every column, so the search covers the key. The
description and README say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
insertsFor joined one quote() per column into a single chain of ||, which
SQLite refuses past a depth of 1000: from about 500 columns, prepare failed
with "Expression tree is too large" and a whole-database dump stopped at
that table (#2323 review, comment 4713256695). Past 100 columns the calls
are now joined in parenthesised groups of 100, so 2000 columns, SQLite's
most, prepare, with the same output (checked on SQLite 3.49.1).

The test for 2000 columns found a second fault: the column list was read
as one page of 1000 rows, so a table of more columns got INSERTs short of
values. It is now read whole, up to 32767 columns, SQLite's highest limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
davidschachterADFA added a commit that referenced this pull request Nov 26, 2026
…gger/ADFA-7302

Brings in the 2026-11-26 review fixes of #2311 and #2323.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
@davidschachterADFA

Copy link
Copy Markdown
Collaborator Author

@hal-eisen-adfa New commits since you were asked to review, for the 2026-11-26 review's two MINOR findings:

Each finding has a reply in its thread. The description lists the tests, the checks on SQLite 3.49.1 and the revert proofs.

…gger/ADFA-7301

Brings in the #2311 review fix through #2319.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
davidschachterADFA added a commit that referenced this pull request Nov 27, 2026
…gger/ADFA-7302

Brings in the #2311 review fix through #2319 and #2323.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAYT7GPoC2cDzo8LErn
@davidschachterADFA

Copy link
Copy Markdown
Collaborator Author

@hal-eisen-adfa A new commit since you were asked to review: c8bf921 merges #2319 at b87338e, which carries #2311's 2d674ec (EventDecoder refuses a row count its bytes cannot hold), with no conflicts. Its diff is exactly 2d674ec's. This PR has no change of its own.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants