pub struct PreparedStatementCache { /* private fields */ }Expand description
A cache for prepared statements.
Tracks which SQL strings have been prepared so we emit a trace!
for hits vs. misses. Eviction is true LRU via lru::LruCache —
when the cache reaches max_size the least-recently-used entry is
dropped on the next insert.
The cache is keyed on the SQL string; the actual Statement is
fetched from client.prepare_cached on every call (deadpool reuses
its own per-connection cache).
Implementations§
Source§impl PreparedStatementCache
impl PreparedStatementCache
Sourcepub fn new(max_size: usize) -> Self
pub fn new(max_size: usize) -> Self
Create a new statement cache with the given maximum size.
max_size of 0 is treated as 1 to satisfy NonZeroUsize.
Sourcepub async fn get_or_prepare(
&self,
client: &Object,
sql: &str,
) -> PgResult<Statement>
pub async fn get_or_prepare( &self, client: &Object, sql: &str, ) -> PgResult<Statement>
Get or prepare a statement for the given SQL.
Sourcepub async fn get_or_prepare_in_txn<'a>(
&self,
txn: &Transaction<'a>,
sql: &str,
) -> PgResult<Statement>
pub async fn get_or_prepare_in_txn<'a>( &self, txn: &Transaction<'a>, sql: &str, ) -> PgResult<Statement>
Get or prepare a statement within a transaction.
Sourcepub fn evict(&self, sql: &str) -> bool
pub fn evict(&self, sql: &str) -> bool
Forget a single cached SQL string, so the next get_or_prepare for it
records a miss and re-prepares.
Used to recover from PostgreSQL 0A000 "cached plan must not change result type": DDL altered a referenced table’s result columns while a
prepared statement for this SQL was cached on the connection. Dropping
the key here, combined with a fresh (uncached) prepare on the retry,
re-plans against the current schema. Returns whether the key was
present.