From 9a3846dcd392bca01d3bf338078b5f24e31974eb Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 6 Mar 2025 00:53:29 +0100 Subject: [PATCH] Add possibility to skip deallocating prepared statements This can take a surprisingly long time, so let programs that terminate quickly skip doing this, as it will be done anyhow when session is closed. --- docs/backends/postgresql.md | 6 ++++++ include/soci/postgresql/soci-postgresql.h | 13 +++++++++++++ src/backends/postgresql/session.cpp | 3 +++ 3 files changed, 22 insertions(+) diff --git a/docs/backends/postgresql.md b/docs/backends/postgresql.md index 38bc900b..1b0efdfc 100644 --- a/docs/backends/postgresql.md +++ b/docs/backends/postgresql.md @@ -156,3 +156,9 @@ The PostgreSQL backend provides the following concrete classes for native API ac The PostgreSQL backend supports working with data stored in columns of type UUID via simple string operations. All string representations of UUID supported by PostgreSQL are accepted on input, the backend will return the standard format of UUID on output. See the test `test_uuid_column_type_support` for usage examples. + +### Optimizing Prepared Statements Deallocation + +By default, this backend executes `DEALLOCATE` statement for each prepared statement when it is destroyed. This is correct, but may be surprisingly time-consuming and is not necessary for short-running programs as all resources associated with a connection, including any statements prepared by it, are deallocated when it is closed in any case. + +To save time spent on deallocation, `postgresql_session_backend::set_deallocate_prepared_statements(bool)` function can be used to disable automatic deallocation of prepared statements. This function can be called at any time, but will only affect statements destroyed after its call, so it is recommended to do it early. diff --git a/include/soci/postgresql/soci-postgresql.h b/include/soci/postgresql/soci-postgresql.h index d336d5fc..6bb89e2b 100644 --- a/include/soci/postgresql/soci-postgresql.h +++ b/include/soci/postgresql/soci-postgresql.h @@ -399,6 +399,18 @@ struct SOCI_POSTGRESQL_DECL postgresql_session_backend : details::session_backen void commit() override; void rollback() override; + // This PostgreSQL-specific function can be used to prevent statement + // deallocation, which happens by default when a prepared statement is + // destroyed. This can be used to optimize the running time of short-lived + // sessions, but should be avoided for long-running programs as allocated + // statements consume server resources. + void set_deallocate_prepared_statements(bool deallocate) + { + deallocatePreparedStatements_ = deallocate; + } + + // Does nothing if set_deallocate_prepared_statements(false) was called, + // otherwise deallocates the statement with the given name. void deallocate_prepared_statement(const std::string & statementName); bool get_next_sequence_value(session & s, @@ -420,6 +432,7 @@ struct SOCI_POSTGRESQL_DECL postgresql_session_backend : details::session_backen std::string get_column_descriptions_query() const override; int statementCount_; + bool deallocatePreparedStatements_ = true; bool single_row_mode_; PGconn * conn_; connection_parameters connectionParameters_; diff --git a/src/backends/postgresql/session.cpp b/src/backends/postgresql/session.cpp index 955ebb1c..5db89403 100644 --- a/src/backends/postgresql/session.cpp +++ b/src/backends/postgresql/session.cpp @@ -286,6 +286,9 @@ void postgresql_session_backend::rollback() void postgresql_session_backend::deallocate_prepared_statement( const std::string & statementName) { + if (!deallocatePreparedStatements_) + return; + const std::string & query = "DEALLOCATE " + statementName; hard_exec(*this, conn_, query.c_str(),