Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sqlx-postgres/src/connection/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ async fn prepare(
persistent: bool,
resolve_column_origin: bool,
) -> Result<(StatementId, Arc<PgStatementMetadata>), Error> {
let id = if persistent {
// if cache is disabled, persistent statements get an id but are never evicted
// which causes a memory leak
let id = if persistent && conn.inner.cache_statement.is_enabled() {
let id = conn.inner.next_statement_id;
conn.inner.next_statement_id = id.next();
id
Expand Down
26 changes: 26 additions & 0 deletions tests/postgres/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,32 @@ async fn it_closes_statements_when_not_persistent_issue_3850() -> anyhow::Result
Ok(())
}

#[sqlx_macros::test]
async fn it_closes_statements_when_caching_is_disabled_issue_4328() -> anyhow::Result<()> {
sqlx_test::setup_if_needed();

let mut options: PgConnectOptions = env::var("DATABASE_URL")?.parse().unwrap();

options = options.statement_cache_capacity(0);

let mut conn = PgConnection::connect_with(&options).await?;

let _row = sqlx::query("SELECT $1 AS val")
.bind(Oid(1))
.fetch_one(&mut conn)
.await?;

let row = sqlx::query("SELECT count(*) AS num_prepared_statements FROM pg_prepared_statements")
.persistent(false)
.fetch_one(&mut conn)
.await?;

let n: i64 = row.get("num_prepared_statements");
assert_eq!(0, n, "no prepared statements should be open");

Ok(())
}

#[sqlx_macros::test]
async fn it_sets_application_name() -> anyhow::Result<()> {
sqlx_test::setup_if_needed();
Expand Down
Loading