Skip to content

Commit 9cbc221

Browse files
committed
Use different application names for db connection to show context
Adds a "context" parameter to database connection setup describing what this connection is for. This is something like "middle.query" or "out.pgsql". It will be added to the application name, so querying pg_stat_activity will show it like "osm2pgsql.middle.query/C2" etc.
1 parent d6fedbd commit 9cbc221

13 files changed

+32
-28
lines changed

src/db-copy.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ db_copy_thread_t::thread_t::thread_t(connection_params_t connection_params,
128128
void db_copy_thread_t::thread_t::operator()()
129129
{
130130
try {
131-
m_conn = std::make_unique<pg_conn_t>(m_connection_params);
131+
m_conn = std::make_unique<pg_conn_t>(m_connection_params, "copy");
132132

133133
// Let commits happen faster by delaying when they actually occur.
134134
m_conn->exec("SET synchronous_commit = off");

src/expire-output.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ std::size_t expire_output_t::output_tiles_to_table(
5858
{
5959
auto const qn = qualified_name(m_schema, m_table);
6060

61-
pg_conn_t connection{connection_params};
61+
pg_conn_t connection{connection_params, "expire"};
6262

6363
auto const result = connection.exec("SELECT * FROM {} LIMIT 1", qn);
6464

src/flex-table.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ void table_connection_t::connect(connection_params_t const &connection_params)
245245
{
246246
assert(!m_db_connection);
247247

248-
m_db_connection = std::make_unique<pg_conn_t>(connection_params);
248+
m_db_connection =
249+
std::make_unique<pg_conn_t>(connection_params, "out.flex.table");
249250
m_db_connection->exec("SET synchronous_commit = off");
250251
}
251252

src/gen/osm2pgsql-gen.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void run_tile_gen(connection_params_t const &connection_params,
205205

206206
log_debug("Started generalizer thread for '{}'.",
207207
master_generalizer->strategy());
208-
pg_conn_t db_connection{connection_params};
208+
pg_conn_t db_connection{connection_params, "gen.tile"};
209209
std::string const strategy{master_generalizer->strategy()};
210210
auto generalizer = create_generalizer(strategy, &db_connection, &params);
211211

@@ -286,7 +286,7 @@ class genproc_t
286286
write_to_debug_log(params, "Params (config):");
287287

288288
log_debug("Connecting to database...");
289-
pg_conn_t db_connection{m_connection_params};
289+
pg_conn_t db_connection{m_connection_params, "gen.proc"};
290290

291291
log_debug("Creating generalizer...");
292292
auto generalizer =
@@ -368,7 +368,7 @@ class genproc_t
368368
queries.emplace_back("COMMIT");
369369
}
370370

371-
pg_conn_t const db_connection{m_connection_params};
371+
pg_conn_t const db_connection{m_connection_params, "gen.sql"};
372372

373373
if (m_append && !if_has_rows.empty()) {
374374
auto const result = db_connection.exec(if_has_rows);
@@ -592,7 +592,7 @@ void genproc_t::run()
592592
}
593593

594594
if (!m_append) {
595-
pg_conn_t const db_connection{m_connection_params};
595+
pg_conn_t const db_connection{m_connection_params, "gen.index"};
596596
for (auto const &table : m_tables) {
597597
if (table.id_type() == flex_table_index_type::tile &&
598598
(table.always_build_id_index() || m_updatable)) {
@@ -702,7 +702,7 @@ int main(int argc, char *argv[])
702702

703703
log_debug("Checking database capabilities...");
704704
{
705-
pg_conn_t const db_connection{connection_params};
705+
pg_conn_t const db_connection{connection_params, "gen.check"};
706706
init_database_capabilities(db_connection);
707707
}
708708

src/middle-pgsql.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void middle_pgsql_t::table_desc::build_index(
168168

169169
// Use a temporary connection here because we might run in a separate
170170
// thread context.
171-
pg_conn_t const db_connection{connection_params};
171+
pg_conn_t const db_connection{connection_params, "middle.index"};
172172

173173
log_info("Building index on table '{}'", name());
174174
for (auto const &query : m_create_fw_dep_indexes) {
@@ -1300,7 +1300,7 @@ middle_query_pgsql_t::middle_query_pgsql_t(
13001300
std::shared_ptr<node_locations_t> cache,
13011301
std::shared_ptr<node_persistent_cache> persistent_cache,
13021302
middle_pgsql_options const &options)
1303-
: m_sql_conn(connection_params), m_cache(std::move(cache)),
1303+
: m_sql_conn(connection_params, "middle.query"), m_cache(std::move(cache)),
13041304
m_persistent_cache(std::move(persistent_cache)), m_store_options(options)
13051305
{
13061306
// Disable JIT and parallel workers as they are known to cause
@@ -1649,7 +1649,7 @@ middle_pgsql_t::middle_pgsql_t(std::shared_ptr<thread_pool_t> thread_pool,
16491649
: middle_t(std::move(thread_pool)), m_options(options),
16501650
m_cache(std::make_unique<node_locations_t>(
16511651
static_cast<std::size_t>(options->cache) * 1024UL * 1024UL)),
1652-
m_db_connection(m_options->connection_params),
1652+
m_db_connection(m_options->connection_params, "middle.main"),
16531653
m_copy_thread(std::make_shared<db_copy_thread_t>(options->connection_params)),
16541654
m_db_copy(m_copy_thread), m_append(options->append)
16551655
{

src/osm2pgsql.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static file_info run(options_t const &options)
8686

8787
static void check_db(options_t const &options)
8888
{
89-
pg_conn_t const db_connection{options.connection_params};
89+
pg_conn_t const db_connection{options.connection_params, "check"};
9090

9191
init_database_capabilities(db_connection);
9292

src/output-flex.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ create_expire_tables(std::vector<expire_output_t> const &expire_outputs,
12411241
return;
12421242
}
12431243

1244-
pg_conn_t const connection{connection_params};
1244+
pg_conn_t const connection{connection_params, "out.flex.expire"};
12451245
for (auto const &expire_output : expire_outputs) {
12461246
if (!expire_output.table().empty()) {
12471247
expire_output.create_output_table(connection);

src/output-gazetteer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void output_gazetteer_t::start()
4949
if (!get_options()->append) {
5050
int const srid = get_options()->projection->target_srs();
5151

52-
pg_conn_t const conn{get_options()->connection_params};
52+
pg_conn_t const conn{get_options()->connection_params, "out.gazetteer"};
5353

5454
/* Drop any existing table */
5555
conn.exec("DROP TABLE IF EXISTS place CASCADE");

src/pgsql.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ std::size_t pg_result_t::affected_rows() const noexcept
2929
std::atomic<std::uint32_t> pg_conn_t::connection_id{0};
3030

3131
static PGconn *open_connection(connection_params_t const &connection_params,
32-
std::uint32_t id)
32+
std::string_view context, std::uint32_t id)
3333
{
3434
std::vector<char const *> keywords;
3535
std::vector<char const *> values;
@@ -39,7 +39,7 @@ static PGconn *open_connection(connection_params_t const &connection_params,
3939
values.push_back(v.c_str());
4040
}
4141

42-
std::string const app_name{fmt::format("osm2pgsql/C{}", id)};
42+
std::string const app_name{fmt::format("osm2pgsql.{}/C{}", context, id)};
4343
keywords.push_back("fallback_application_name");
4444
values.push_back(app_name.c_str());
4545

@@ -49,23 +49,25 @@ static PGconn *open_connection(connection_params_t const &connection_params,
4949
return PQconnectdbParams(keywords.data(), values.data(), 1);
5050
}
5151

52-
pg_conn_t::pg_conn_t(connection_params_t const &connection_params)
52+
pg_conn_t::pg_conn_t(connection_params_t const &connection_params,
53+
std::string_view context)
5354
: m_connection_id(connection_id.fetch_add(1))
5455
{
55-
m_conn.reset(open_connection(connection_params, m_connection_id));
56+
m_conn.reset(open_connection(connection_params, context, m_connection_id));
5657

5758
if (!m_conn) {
58-
throw std::runtime_error{"Connecting to database failed."};
59+
throw fmt_error("Connecting to database failed (context={}).", context);
5960
}
6061

6162
if (PQstatus(m_conn.get()) != CONNECTION_OK) {
62-
throw fmt_error("Connecting to database failed: {}.", error_msg());
63+
throw fmt_error("Connecting to database failed (context={}): {}.",
64+
context, error_msg());
6365
}
6466

6567
if (get_logger().log_sql()) {
6668
auto const results = exec("SELECT pg_backend_pid()");
67-
log_sql("(C{}) New database connection (backend_pid={})",
68-
m_connection_id, results.get(0, 0));
69+
log_sql("(C{}) New database connection (context={}, backend_pid={})",
70+
m_connection_id, context, results.get(0, 0));
6971
}
7072

7173
// PostgreSQL sends notices in many different contexts which aren't that

src/pgsql.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class binary_param : public std::string_view
152152
class pg_conn_t
153153
{
154154
public:
155-
explicit pg_conn_t(connection_params_t const &connection_params);
155+
explicit pg_conn_t(connection_params_t const &connection_params,
156+
std::string_view context);
156157

157158
/**
158159
* Run the specified SQL command.

src/properties.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void properties_t::set_string(std::string property, std::string value,
9292
auto const &inserted = *(r.first);
9393
log_debug(" Storing {}='{}'", inserted.first, inserted.second);
9494

95-
pg_conn_t const db_connection{m_connection_params};
95+
pg_conn_t const db_connection{m_connection_params, "prop.set"};
9696
db_connection.exec(
9797
"PREPARE set_property(text, text) AS"
9898
" INSERT INTO {} (property, value) VALUES ($1, $2)"
@@ -119,7 +119,7 @@ void properties_t::store()
119119
auto const table = table_name();
120120

121121
log_info("Storing properties to table '{}'.", table);
122-
pg_conn_t const db_connection{m_connection_params};
122+
pg_conn_t const db_connection{m_connection_params, "prop.store"};
123123

124124
if (m_has_properties_table) {
125125
db_connection.exec("TRUNCATE {}", table);
@@ -153,7 +153,7 @@ bool properties_t::load()
153153
auto const table = table_name();
154154
log_info("Loading properties from table '{}'.", table);
155155

156-
pg_conn_t const db_connection{m_connection_params};
156+
pg_conn_t const db_connection{m_connection_params, "prop.load"};
157157
auto const result = db_connection.exec("SELECT * FROM {}", table);
158158

159159
for (int i = 0; i < result.num_tuples(); ++i) {

src/table.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void table_t::sync() { m_copy.sync(); }
6666

6767
void table_t::connect()
6868
{
69-
m_sql_conn = std::make_unique<pg_conn_t>(m_connection_params);
69+
m_sql_conn = std::make_unique<pg_conn_t>(m_connection_params, "out.pgsql");
7070
//let commits happen faster by delaying when they actually occur
7171
m_sql_conn->exec("SET synchronous_commit = off");
7272
}

tests/common-pg.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class conn_t : public pg_conn_t
3838
{
3939
public:
4040
conn_t(connection_params_t const &connection_params)
41-
: pg_conn_t(connection_params)
41+
: pg_conn_t(connection_params, "test")
4242
{
4343
}
4444

0 commit comments

Comments
 (0)