From 89f213abdbaad485c30cb45604984de4e197a29d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 6 Mar 2025 00:44:17 +0100 Subject: [PATCH] Stop setting "extra_float_digits" for PostgreSQL >= 12 This is unnecessary as v12 implemented Ryu algorithm for floating point number formatting which avoids precision loss by default, so don't run an extra query on every connection in both PostgreSQL backend and ODBC backend when using PostgreSQL driver. This commit is best viewed ignoring whitespace-only changes. --- src/backends/odbc/session.cpp | 24 +++++++++++++++--------- src/backends/postgresql/session.cpp | 20 ++++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/backends/odbc/session.cpp b/src/backends/odbc/session.cpp index 66eccf77..81895ad3 100644 --- a/src/backends/odbc/session.cpp +++ b/src/backends/odbc/session.cpp @@ -171,16 +171,22 @@ void odbc_session_backend::configure_connection() "\" in unrecognizable format."); } - details::auto_statement st(*this); - - std::string const q(major_ver >= 9 ? "SET extra_float_digits = 3" - : "SET extra_float_digits = 2"); - rc = SQLExecDirect(st.hstmt_, sqlchar_cast(q), static_cast(q.size())); - - if (is_odbc_error(rc)) + // As explained in src/backends/postgresql/session.cpp, we need to + // increase the number of digits used for floating point values to + // ensure that all numbers round trip correctly with old PostgreSQL. + if (major_ver < 12) { - throw odbc_soci_error(SQL_HANDLE_DBC, henv_, - "setting extra_float_digits for PostgreSQL"); + details::auto_statement st(*this); + + std::string const q(major_ver >= 9 ? "SET extra_float_digits = 3" + : "SET extra_float_digits = 2"); + rc = SQLExecDirect(st.hstmt_, sqlchar_cast(q), static_cast(q.size())); + + if (is_odbc_error(rc)) + { + throw odbc_soci_error(SQL_HANDLE_DBC, henv_, + "setting extra_float_digits for PostgreSQL"); + } } // This is extracted from pgapifunc.h header from psqlODBC driver. diff --git a/src/backends/postgresql/session.cpp b/src/backends/postgresql/session.cpp index 0d66d3f4..955ebb1c 100644 --- a/src/backends/postgresql/session.cpp +++ b/src/backends/postgresql/session.cpp @@ -231,15 +231,19 @@ void postgresql_session_backend::connect( PQtrace(conn, traceFile_); } - // Increase the number of digits used for floating point values to ensure - // that the conversions to/from text round trip correctly, which is not the - // case with the default value of 0. Use the maximal supported value, which - // was 2 until 9.x and is 3 since it. + // With older PostgreSQL versions we need to change the extra_float_digits + // parameter to ensure that the conversions to/from text round trip + // losslessly. This is not necessary since 12.0, which behaves correctly by + // default, but for older versions use the maximal supported value, which + // was 2 until 9.x and 3 after it. int const version = PQserverVersion(conn); - hard_exec(*this, conn, - version >= 90000 ? "SET extra_float_digits = 3" - : "SET extra_float_digits = 2", - "Cannot set extra_float_digits parameter"); + if (version < 120000) + { + hard_exec(*this, conn, + version >= 90000 ? "SET extra_float_digits = 3" + : "SET extra_float_digits = 2", + "Cannot set extra_float_digits parameter"); + } conn_ = conn; connectionParameters_ = parameters;