Skip to content

Commit b0febb4

Browse files
authored
Merge pull request #1897 from joto/sql-log-sessionid
Give each database connection a unique id and use it for logging
2 parents 0642fce + 228ab05 commit b0febb4

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/pgsql.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,30 @@
1818
#include <string>
1919
#include <utility>
2020

21+
std::atomic<std::uint32_t> pg_conn_t::connection_id{0};
22+
2123
pg_conn_t::pg_conn_t(std::string const &conninfo)
22-
: m_conn(PQconnectdb(conninfo.c_str()))
24+
: m_conn(PQconnectdb(conninfo.c_str())),
25+
m_connection_id(connection_id.fetch_add(1))
2326
{
2427
if (!m_conn) {
2528
throw std::runtime_error{"Connecting to database failed."};
2629
}
2730
if (PQstatus(m_conn.get()) != CONNECTION_OK) {
2831
throw fmt_error("Connecting to database failed: {}.", error_msg());
2932
}
33+
34+
if (get_logger().log_sql()) {
35+
auto const results = exec("SELECT pg_backend_pid()");
36+
log_sql("(C{}) New database connection (backend_pid={})",
37+
m_connection_id, results.get(0, 0));
38+
}
39+
}
40+
41+
void pg_conn_t::close()
42+
{
43+
log_sql("(C{}) Closing database connection", m_connection_id);
44+
m_conn.reset();
3045
}
3146

3247
char const *pg_conn_t::error_msg() const noexcept
@@ -49,7 +64,7 @@ pg_result_t pg_conn_t::exec(char const *sql) const
4964
{
5065
assert(m_conn);
5166

52-
log_sql("{}", sql);
67+
log_sql("(C{}) {}", m_connection_id, sql);
5368
pg_result_t res{PQexec(m_conn.get(), sql)};
5469
if (res.status() != PGRES_COMMAND_OK && res.status() != PGRES_TUPLES_OK) {
5570
throw fmt_error("Database error: {}", error_msg());
@@ -66,7 +81,7 @@ void pg_conn_t::copy_start(char const *sql) const
6681
{
6782
assert(m_conn);
6883

69-
log_sql("{}", sql);
84+
log_sql("(C{}) {}", m_connection_id, sql);
7085
pg_result_t const res{PQexec(m_conn.get(), sql)};
7186
if (res.status() != PGRES_COPY_IN) {
7287
throw fmt_error("Database error on COPY: {}", error_msg());
@@ -78,7 +93,8 @@ void pg_conn_t::copy_send(std::string const &data,
7893
{
7994
assert(m_conn);
8095

81-
log_sql_data("Copy data to '{}':\n{}", context, data);
96+
log_sql_data("(C{}) Copy data to '{}':\n{}", m_connection_id, context,
97+
data);
8298
int const r = PQputCopyData(m_conn.get(), data.c_str(), (int)data.size());
8399

84100
switch (r) {
@@ -141,7 +157,7 @@ pg_result_t pg_conn_t::exec_prepared_internal(char const *stmt, int num_params,
141157
assert(m_conn);
142158

143159
if (get_logger().log_sql()) {
144-
log_sql("EXECUTE {}({})", stmt,
160+
log_sql("(C{}) EXECUTE {}({})", m_connection_id, stmt,
145161
concat_params(num_params, param_values));
146162
}
147163

src/pgsql.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <libpq-fe.h>
2424

2525
#include <array>
26+
#include <atomic>
2627
#include <cassert>
2728
#include <map>
2829
#include <memory>
@@ -205,7 +206,7 @@ class pg_conn_t
205206
char const *error_msg() const noexcept;
206207

207208
/// Close database connection.
208-
void close() noexcept { m_conn.reset(); }
209+
void close();
209210

210211
private:
211212
pg_result_t exec_prepared_internal(char const *stmt, int num_params,
@@ -289,6 +290,12 @@ class pg_conn_t
289290
};
290291

291292
std::unique_ptr<PGconn, pg_conn_deleter_t> m_conn;
293+
294+
// Used to generate unique ids for each database connection.
295+
static std::atomic<std::uint32_t> connection_id;
296+
297+
// The unique id of this database connection.
298+
std::uint32_t m_connection_id;
292299
};
293300

294301
/**

0 commit comments

Comments
 (0)