Skip to content

fix(duckdb): prepare a statement only inside a transaction it runs in - #4364

Merged
datlechin merged 3 commits into
mainfrom
fix/duckdb-side-effecting-prepare
Nov 20, 2026
Merged

datlechin merged 3 commits into
mainfrom
fix/duckdb-side-effecting-prepare

Conversation

@datlechin

@datlechin datlechin commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

  • DuckDB 2.0 prepares through a PREPARE statement that never writes. A statement whose binding takes a sequence value, such as SET threads = nextval('seq') or SELECT * FROM range(nextval('seq')), then invalidated the whole database until it was reopened. Extracting the statements first prepares the same way (measured).
  • macOS plugin: a statement without parameters that does not start like a SELECT runs through duckdb_query, one statement at a time, which prepares nothing. A streamed read, and a statement with parameters, is prepared inside the session's transaction or one the plugin opens when the session holds none, and committed when its result ends; a failed COMMIT is the statement's error.
  • iOS app: the grid's streamed read and the export read are prepared the same way; with no transaction to prepare in they are read whole through duckdb_query.
  • Core DuckDB driver: an editor run opens a transaction for each statement while the session holds none, except BEGIN, COMMIT and ROLLBACK, and tracks the state across the transaction statements it runs.
  • Statements DuckDB does not stream are faster on macOS: an INSERT on a file takes 176 µs through duckdb_query against 450 µs prepared.
  • ADR 0037's thread following is kept, with the statement type from the result on the duckdb_query path.
  • Core ADR 0040 records the decision. apple/scripts/probes/check-duckdb-side-effecting-prepare.sh measures what it rests on.

Test plan

  • Probe on the shipped libduckdb v2.0.0: a prepare whose binding takes a sequence value invalidates the database outside a transaction and not inside one; 15 statement kinds run alike inside a transaction.
  • iOS DuckDBSideEffectingBindTests: a streamed read and an export whose table function takes a sequence value leave the database open; a read inside the user's transaction keeps it; a SET takes its value once; which statements stream.
  • Core sessions: a binding that takes a sequence value leaves the database open; a script that ends its transaction reads the next binding outside one; a writing read cut at the row limit completes.
  • macOS plugin driven from a swiftc harness: on main the bounded read of range(nextval('seq')) fails with the internal error and every later statement with FATAL; here it reads 20 rows, the SET takes its value once, rows affected and RETURNING read as before, and the threads stay taken over.
  • Every DuckDB iOS suite (72 cases), cargo test -p tablepro-driver-duckdb and clippy, verify.sh plugins, lint, l10n. On macOS open_semantics::an_attach_of_a_fifo_is_refused fails on the /var against /private/var spelling of the temporary directory, in code this change does not touch; CI runs that test on Linux.

@datlechin
datlechin merged commit 9b06e3b into main Nov 20, 2026
33 of 34 checks passed
@datlechin
datlechin deleted the fix/duckdb-side-effecting-prepare branch November 20, 2026 04:59
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.

1 participant