duckdb-rs icon indicating copy to clipboard operation
duckdb-rs copied to clipboard

Why remove the prepared statements from cache?

Open domoritz opened this issue 1 year ago • 1 comments

Maybe I am misunderstanding the logic here but I wonder why duckdb removes the statement from the cache instead of just getting it? I would expect a call to https://paritytech.github.io/try-runtime-cli/hashlink/lru_cache/struct.LruCache.html#method.get.

https://github.com/duckdb/duckdb-rs/blame/2bd811e7b1b7398c4f461de4de263e629572dc90/crates/duckdb/src/cache.rs#L142

domoritz avatar Nov 06 '24 20:11 domoritz

I didn't write this code but from https://github.com/duckdb/duckdb-rs/commit/80f6f90c129a8ce882420680064b7e62f938122d it looks like the idea is that the statement is mutable while it's in use, perhaps at the DuckDB level. When you get the statement from the cache, the caller gets effectively exclusive use of it. But when the CachedStatement is dropped, it automatically puts itself back into the cache.

It seems a little strange compared to e.g. cloning when things are taken from the cache, or just keeping only immutable representations in the cache. But probably the C API constrains how things can be done.

sourcefrog avatar Dec 19 '24 23:12 sourcefrog

The use of remove() instead of get() is intentional and necessary. Prepared statements are mutable during execution - RawStatement::execute() takes &mut self and modifies internal state. If we used get(), the cache would maintain a reference to the statement while also giving out a mutable reference to the caller, violating Rust's borrowing rules.

This is the same pattern used by rusqlite (which this crate was modeled after): https://github.com/rusqlite/rusqlite/blob/9d701b626fd15ab6c712ed2b082bf36a5b48bba3/src/cache.rs#L143

mlafeldt avatar Oct 06 '25 10:10 mlafeldt