expression

PostgreSQL: classify transaction retry errors

A conservative retry policy can identify serialization failures (40001) and deadlocks (40P01). It should not automatically retry every integrity or connection error. Some unique violations can represent races, but only application-specific reasoning establishes whether retry is useful. This is a policy model, not an actual database error. No database connection is required by this example. Expected stdout (not executed): 40001: true 40P01: true 23505: false unknown: false Common mistake: Assuming every failure at commit is safe to replay; a lost connection can leave the result unknown.

Formal Syntax & Structure

rust syntaxrust
matches!(code, Some("40001" | "40P01"))

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> {    fn retryable(code: Option<&str>) -> bool {        matches!(code, Some("40001" | "40P01"))    }    for code in [Some("40001"), Some("40P01"), Some("23505"), None] {        println!("{}: {}", code.unwrap_or("unknown"), retryable(code));    }    Ok(())}
Rust Reference