expression
SQLx: choose PostgreSQL transaction isolation
Isolation controls visibility and conflicts between concurrent transactions. PostgreSQL Read Committed uses a fresh snapshot for ordinary statements; Repeatable Read uses a stable transaction snapshot and can still suffer serialization anomalies. Serializable prevents those anomalies among successful serializable transactions, but can reject an attempt. Set isolation before data access. This fixture inspects configuration, not concurrent behavior. Requires an explicitly authorized disposable local PostgreSQL lab with verified TLS; see fixture README. Expected stdout (not executed): repeatable read Common mistake: Assuming Repeatable Read prevents every possible anomaly, or setting isolation after the first data query.
Formal Syntax & Structure
rust syntaxrust
SET TRANSACTION ISOLATION LEVEL REPEATABLE READWorking Example
Example Snippetrust
async fn lab_pool() -> Result<sqlx::PgPool, Box<dyn std::error::Error>> { use sqlx::postgres::{PgConnectOptions, PgPoolOptions, PgSslMode}; use std::time::Duration; if std::env::var("BM_SQLX_LAB_OPT_IN").as_deref() != Ok("yes") { return Err("disposable PostgreSQL lab required".into()); } // Reject ambient PostgreSQL settings to keep the target explicit. if std::env::vars_os().any(|(key, _)| key.to_string_lossy().to_ascii_uppercase().starts_with("PG")) { return Err("remove ambient PG* environment settings".into()); } let password = std::env::var("BM_SQLX_LAB_PASSWORD")?; let ca = std::env::var("BM_SQLX_LAB_CA")?; let options = PgConnectOptions::new_without_pgpass() .host("127.0.0.1").port(55432) .username("bm_learner").database("bm_sqlx_v86_lab") .password(&password).ssl_mode(PgSslMode::VerifyFull) .ssl_root_cert(ca); let pool = PgPoolOptions::new().max_connections(2) .acquire_timeout(Duration::from_secs(3)) .connect_with(options).await?; Ok(pool)}
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> { let pool = lab_pool().await?; let mut tx = pool.begin().await?; sqlx::query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ") .execute(&mut *tx).await?; let level: String = sqlx::query_scalar("SHOW transaction_isolation") .fetch_one(&mut *tx).await?; println!("{level}"); tx.rollback().await?; pool.close().await; Ok(())}