pgcat
pgcat copied to clipboard
errors on insert with prepared statements
Describe the bug error returned from database: prepared statement "sqlx_s_1" already exists
To Reproduce Cargo.toml
[package]
name = "sqlx_pgcat_insert"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio-util = "0.7.10"
anyhow = "1.0.42"
sqlx = { version = "0.7", features = [
"runtime-tokio",
"postgres",
"chrono",
"rust_decimal",
"migrate",
"runtime-tokio-native-tls",
] }
tokio = { version = "1", features = ["full"] }
dotenvy_macro = "0.15.7"
main.rs
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("Hello, world!");
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(5)
.connect(dotenvy_macro::dotenv!("DATABASE_URL"))
.await?;
for _ in 0..1000 {
match sqlx::query("SELECT one").fetch_all(&pool).await {
Ok(_) => (),
Err(err) => {
if err.to_string().contains("prepared statement") {
panic!("prepared statement error: {}", err);
}
}
}
}
println!("select one done");
for i in 0..1000 {
sqlx::query("INSERT INTO users (name, age) VALUES ($1, $2)")
.bind("Sean")
.bind(i)
.execute(&pool)
.await?;
}
Ok(())
}
Result:
Hello, world!
select one done
Error: error returned from database: prepared statement "sqlx_s_198" already exists
pgcat.toml
[general]
host = "0.0.0.0"
port = 6432
enable_prometheus_exporter = true
prometheus_exporter_port = 9930
admin_username = "postgres"
admin_password = "x"
prepared_statements = true
[pools.postgres]
[pools.postgres.users.0]
pool_size = 100
username = "postgres"
password = "x"
[pools.postgres.shards.0]
database = "postgres"
servers = [["timescaledb", 5432, "primary"]]
Expected behavior should run
Same issue with https://github.com/sfackler/rust-postgres. Seems like it's not on on sqlx side
prepared_statements = true doesn't actually turn prepared statements on.
You actually need to add a value for prepared_statements_cache_size
prepared_statements_cache_size = 500