R2d2, postgres and prepare
⚓ Rust 📅 2026-01-04 👤 surdeus 👁️ 4I'm writing a server that uses a postgresql database, it uses r2d2_postgres for connection pooling. I have a few queries that I'd like to prepare, and there's a prepare() method that returns a Statement. I figured I'd use the connection pool's CustomizeConnection::on_acquire() to prepare the statement. However, I discovered that there's no good place to actually store the Statement (so that is associated with the connection Client).
I figured I'd fall back to using SQL PREPARE and EXECUTE instead.
I prepare a plan using:
dbclnt.simple_query(
"PREPARE authplan (TEXT, TEXT) AS
SELECT * FROM users WHERE name=$1 AND passhash = crypt($2, salt);"
)?;
..and later I attempt to execute it using:
let row = dbclnt.query_opt("EXECUTE authplan($1, $2);", &[&name, &pass])?;
.. but I get an expected 0 parameters but got 2 error when I do so.
I found some older post about using r2d2 with postgres and prepared statements, which indicates that it used to have a prepare_cached, which does not appear to still be available.
Anyone know why the SQL version doesn't work? Why is it telling me it expected 0 parameters? Or does anyone know how to store/retrieve a Statement when using the r2d2 connection pool?
(One can always create a server-side PL/pgSQL function, but that feels kind of derpy for really small things).
1 post - 1 participant
🏷️ Rust_feed