expression
PostgreSQL: bound whole-transaction retries
Retry the complete logical transaction, including reads and decisions, not only its final statement. Each retry uses a fresh transaction. This offline model represents known-aborted attempts; it does not simulate a database or sleep. In production impose an overall deadline, bounded backoff with jitter, and avoid external side effects inside replayed work. No database connection is required by this example. Expected stdout (not executed): result: Some(42) Common mistake: Sending email or charging a payment inside work that may be repeated.
Formal Syntax & Structure
rust syntaxrust
for attempt in 1..=max_attempts { /* begin, read, write, commit */ }Working Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> { let outcomes = [Err("40001"), Err("40P01"), Ok(42)]; let mut result = None; for (i, outcome) in outcomes.into_iter().take(3).enumerate() { match outcome { Ok(value) => { result = Some(value); break; } Err("40001" | "40P01") if i + 1 < 3 => continue, Err(_) => break, } } println!("result: {result:?}"); Ok(())}