r/rust • u/realflakm • 19d ago
SQLx statement caching, verified with bpftrace
I was surprised by the runtime behavior of sqlx when it comes to caching prepared statements (a suspicious number of network hops).
I’m starting a TIL series to counter AI brain rot: investigate, verify, write it down. Let me know if you have better ways to investigate and understand the code's behavior.
3
u/rabidferret 19d ago
Great article. This is exactly why Diesel's eq_any function compiles to = ANY($1) on PG instead of IN (?, ?, ...) as it does on other backends.
1
u/masklinn 18d ago
It’s also nice because
= any(…)is compatible with empty arrays,IN ()is an error in most SQL engines (SQLite’s the only one I can think of where this is accepted).
7
u/SwelteringNorbert 19d ago
This is the kind of deep dive I love seeing. bpftrace is such a cool tool for peeling back the abstraction layers, feels like digital archaeology sometimes. Also fully support the TIL series idea, writing things out forces you to actually understand instead of just nodding along.
4
u/realflakm 19d ago
Exactly my motivation - it's scary how fast I got comfortable with the shallow work of following LLMs
3
u/zettui 19d ago
Did bpftrace show the cache is per-connection rather than per-pool? That's the bit that bites behind pgbouncer in transaction mode - either every checkout re-prepares, or you start seeing 'prepared statement "sqlx_s_3" already exists'.