Skip to content

fix(postgres): re-parse unnamed statement when statement cache is disabled - #4555

Open
aloneangel45 wants to merge 1 commit into
transact-rs:mainfrom
aloneangel45:fix/postgres-unnamed-reparse-cache-disabled
Open

aloneangel45 wants to merge 1 commit into
transact-rs:mainfrom
aloneangel45:fix/postgres-unnamed-reparse-cache-disabled

Conversation

@aloneangel45

Copy link
Copy Markdown

Fixes #4554

Problem

In sqlx-postgres/src/connection/executor.rs, prepare() assigns StatementId::UNNAMED whenever !persistent || !conn.inner.cache_statement.is_enabled() (e.g., statement_cache_capacity(0)).

When preparing a query whose return types trigger a simple catalog query in resolve_statement_metadata(), PostgreSQL destroys the unnamed prepared statement. #4361 added a re-parse guard, but it checked if !persistent && ran_catalog_query rather than id == StatementId::UNNAMED && ran_catalog_query. With statement_cache_capacity(0) and default persistent = true inside a transaction (where SQLSTATE 26000 cannot retry outside check_stale_plan), subsequent Bind fails with unnamed prepared statement does not exist.

Fix

  • Re-parse whenever id == StatementId::UNNAMED && ran_catalog_query in sqlx-postgres/src/connection/executor.rs.
  • Add synchronous in-memory mock-socket unit tests in sqlx-postgres verifying StatementId::UNNAMED is re-parsed for both statement_cache_capacity(0) (persistent = true) and persistent = false, while named statements are not unnecessarily re-parsed.
  • Add an end-to-end integration test test_issue_4554 in tests/postgres/postgres.rs.

Verification

  • cargo fmt --all -- --check: passed
  • cargo clippy -p sqlx-postgres --no-deps --lib --tests: clean on modified files
  • cargo test -p sqlx-postgres --lib: 157 passed, 0 failed
  • cargo test --no-run --test postgres --features postgres,runtime-tokio,tls-none,macros: compiled cleanly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres: "unnamed prepared statement does not exist" with statement_cache_capacity(0) when a query needs a type lookup

1 participant