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.
This commit is contained in:
Vadim Zeitlin
2025-03-06 00:53:29 +01:00
parent 89f213abdb
commit 9a3846dcd3
3 changed files with 22 additions and 0 deletions

View File

@@ -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.

View File

@@ -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_;

View File

@@ -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(),