sfackler / rust-postgres

Native PostgreSQL driver for the Rust programming language
Apache License 2.0
3.52k stars 446 forks source link

Should Prepare use query as Prepare Name? #1132

Closed zhixinwen closed 7 months ago

zhixinwen commented 7 months ago

We are using the crate to talk to CRDB, and we notice the CRDB server memory is constantly growing. We think the issue is due to every time we execute a query, the crate would prepare the query with a different name: https://github.com/sfackler/rust-postgres/blob/98f5a11bc0a8e451552d8941ffa078c7eb6cd60c/tokio-postgres/src/prepare.rs#L66.

Is this a valid choice to use int as name instead of query or something else? I dont think other lib is doing this, for example in go it is using query: https://github.com/lib/pq/blob/3d613208bca2e74f2a20e04126ed30bcb5c4cc27/conn.go#L917

sfackler commented 7 months ago

Prepared statements are closed when they are dropped: https://github.com/sfackler/rust-postgres/blob/master/tokio-postgres/src/statement.rs#L15-L26

If CRDB doesn't clean up resources when that happens, it seems like a bug in CRDB.

zhixinwen commented 7 months ago

Thanks for the reply. I am not very familiar with the Postgres protocols, but looks like DEALLOCATE is for releasing prepared statements, and CLOSE is for cursor.

sfackler commented 7 months ago

We don't close statements at the SQL layer, we close them at the protocol layer. https://www.postgresql.org/docs/16/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY

zhixinwen commented 7 months ago

Thank you!