Generated execute() ignores the prepared statement: queries without RETURNING are re-prepared on every call
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 76/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- postgresql, rust
- Ambito
- backend-api-design, databases
Direzione di ricerca
Read the no-row-type else branch in src/codegen/queries.rs around line 493, then compare how one/opt handle prepared statements in codegen/client.rs. Check the generated bind behavior for both sync and async clients, including statement caching. Done means generated execute helpers use the prepared statement or cache consistently, and a regression test covers queries without RETURNING.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
For a query without a result set (no RETURNING), the generated bind ignores the prepared Statement it holds (self.1) and calls client.execute(self.0, …) with the SQL text. tokio-postgres then prepares an unnamed statement on every call, so every write costs an extra Parse/Describe round trip. The generated one/opt/all/iter helpers do use self.1 or C::stmt_cache(); only execute does not.
Where
src/codegen/queries.rs, the else branch for queries without a row type (line 493 on main at f454a7e):
client.execute(self.0, &[#(#params_wrap,)*])#fn_await
Reproduced with 1.0.1 (crates.io) and on main f454a7e.
Repro
--! touch_item (id)
UPDATE item SET updated_at = now() WHERE id = :id;
cornucopia schema schema.sql -q queries --async true --sync false. The generated TouchItemStmt::bind calls client.execute(self.0, &[id]). Executing it N times against a server with log_statement = all (or pg_stat_statements) shows N parses of an unnamed statement. A --! … : ()-style query that goes through one() does not.
Impact
Each write statement costs one extra round trip (Parse + Describe of an unnamed statement) and defeats the client-side statement cache. On a write-heavy API (deadpool-postgres behind PgBouncer in transaction mode) we patch the generated code as below. We have not measured this change on its own.
Workaround we use
After generation, a script rewrites client.execute(self.0, (any whitespace, because the output is wrapped over lines) to a helper appended to client/async_.rs:
pub async fn execute<C: GenericClient>(
client: &C,
query: &str,
cached: Option<&Statement>,
params: &[&(dyn ToSql + Sync)],
) -> Result<u64, Error> {
if let Some(cached) = cached {
client.execute(cached, params).await
} else if C::stmt_cache() {
let cached = client.prepare(query).await?;
client.execute(&cached, params).await
} else {
client.execute(query, params).await
}
}
That mirrors what the one/opt helpers in codegen/client.rs already do. Generating bind through a helper like this (sync and async) would fix it. Happy to open a PR if that shape is acceptable.
- Lingua principale
- Rust
- Stelle
- 1.2k
- Fork
- 77
- Merge medio
- 5g 12h
- PR unite (30g)
- 1
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di cornucopia-rs/cornucopia
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
cornucopia-rs/cornucopia#334 ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 45/100
cornucopia-rs/cornucopia#330 · 2 commenti ·
-
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
cornucopia-rs/cornucopia#309 · 2 commenti ·
-
configuration feature
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
cornucopia-rs/cornucopia#292 ·
-
codegen feature
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
cornucopia-rs/cornucopia#291 ·
Tutte le issue di cornucopia-rs/cornucopia
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
RustCrypto/utils#1597 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
Non-`main` default branchesAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
max-sixty/worktrunk#4727 · 7 commenti · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
I maintainer di solito rispondono entro 1 giorno
-
follow-up
Difficoltà 2/5 1-3 ore Idoneità per principianti 73/100
carrick-sh/carrick#1859 ·
I maintainer di solito rispondono entro 1 giorno