Skip to content

Commit bdd5583

Browse files
authored
Merge pull request #2155 from joto/db-conn-name
Consistently use m_db_connection for the database connection member var
2 parents 6466532 + ba56530 commit bdd5583

6 files changed

+54
-50
lines changed

src/db-copy.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,16 @@ 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, "copy");
131+
m_db_connection =
132+
std::make_unique<pg_conn_t>(m_connection_params, "copy");
132133

133134
// Disable sequential scan on database tables in the copy threads.
134135
// The copy threads only do COPYs (which are unaffected by this
135136
// setting) and DELETEs which we know benefit from the index. For
136137
// some reason PostgreSQL chooses in some cases not to use that index,
137138
// possibly because the DELETEs get a large list of ids to delete of
138139
// which many are not in the table which confuses the query planner.
139-
m_conn->exec("SET enable_seqscan = off");
140+
m_db_connection->exec("SET enable_seqscan = off");
140141

141142
bool done = false;
142143
while (!done) {
@@ -167,7 +168,7 @@ void db_copy_thread_t::thread_t::operator()()
167168

168169
finish_copy();
169170

170-
m_conn.reset();
171+
m_db_connection.reset();
171172
} catch (std::runtime_error const &e) {
172173
log_error("DB copy thread failed: {}", e.what());
173174
std::exit(2); // NOLINT(concurrency-mt-unsafe)
@@ -181,13 +182,13 @@ void db_copy_thread_t::thread_t::write_to_db(db_cmd_copy_t *buffer)
181182
finish_copy();
182183
}
183184

184-
buffer->delete_data(m_conn.get());
185+
buffer->delete_data(m_db_connection.get());
185186

186187
if (!m_inflight) {
187188
start_copy(buffer->target);
188189
}
189190

190-
m_conn->copy_send(buffer->buffer, buffer->target->name());
191+
m_db_connection->copy_send(buffer->buffer, buffer->target->name());
191192
}
192193

193194
void db_copy_thread_t::thread_t::start_copy(
@@ -208,15 +209,15 @@ void db_copy_thread_t::thread_t::start_copy(
208209
}
209210

210211
sql.push_back('\0');
211-
m_conn->copy_start(sql.data());
212+
m_db_connection->copy_start(sql.data());
212213

213214
m_inflight = target;
214215
}
215216

216217
void db_copy_thread_t::thread_t::finish_copy()
217218
{
218219
if (m_inflight) {
219-
m_conn->copy_end(m_inflight->name());
220+
m_db_connection->copy_end(m_inflight->name());
220221
m_inflight.reset();
221222
}
222223
}

src/db-copy.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class db_copy_thread_t
301301
void delete_rows(db_cmd_copy_t *buffer);
302302

303303
connection_params_t m_connection_params;
304-
std::unique_ptr<pg_conn_t> m_conn;
304+
std::unique_ptr<pg_conn_t> m_db_connection;
305305

306306
// Target for copy operation currently ongoing.
307307
std::shared_ptr<db_target_descr_t> m_inflight;

src/middle-pgsql.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ middle_pgsql_t::table_desc::table_desc(options_t const &options,
141141

142142
void middle_query_pgsql_t::exec_sql(std::string const &sql_cmd) const
143143
{
144-
m_sql_conn.exec(sql_cmd);
144+
m_db_connection.exec(sql_cmd);
145145
}
146146

147147
void middle_pgsql_t::table_desc::drop_table(
@@ -665,7 +665,7 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_db(
665665

666666
// get any remaining nodes from the DB
667667
// Nodes must have been written back at this point.
668-
auto const res = m_sql_conn.exec_prepared("get_node_list", id_list());
668+
auto const res = m_db_connection.exec_prepared("get_node_list", id_list());
669669
std::unordered_map<osmid_t, osmium::Location> locs;
670670
for (int i = 0; i < res.num_tuples(); ++i) {
671671
locs.emplace(osmium::string_to_object_id(res.get_value(i, 0)),
@@ -776,7 +776,7 @@ std::size_t middle_query_pgsql_t::get_way_node_locations_flatnodes(
776776

777777
osmium::Location middle_query_pgsql_t::get_node_location_db(osmid_t id) const
778778
{
779-
auto const res = m_sql_conn.exec_prepared("get_node", id);
779+
auto const res = m_db_connection.exec_prepared("get_node", id);
780780
if (res.num_tuples() == 0) {
781781
return osmium::Location{};
782782
}
@@ -1026,7 +1026,7 @@ bool middle_query_pgsql_t::way_get(osmid_t id,
10261026
{
10271027
assert(buffer);
10281028

1029-
auto const res = m_sql_conn.exec_prepared("get_way", id);
1029+
auto const res = m_db_connection.exec_prepared("get_way", id);
10301030

10311031
if (res.num_tuples() != 1) {
10321032
return false;
@@ -1061,7 +1061,7 @@ middle_query_pgsql_t::rel_members_get(osmium::Relation const &rel,
10611061

10621062
// ...and get those ways from database
10631063
if (!way_ids.empty()) {
1064-
res = m_sql_conn.exec_prepared("get_way_list", way_ids());
1064+
res = m_db_connection.exec_prepared("get_way_list", way_ids());
10651065
wayidspg = get_ids_from_result(res);
10661066
}
10671067
}
@@ -1178,7 +1178,7 @@ bool middle_query_pgsql_t::relation_get_format1(
11781178
{
11791179
assert(buffer);
11801180

1181-
auto const res = m_sql_conn.exec_prepared("get_rel", id);
1181+
auto const res = m_db_connection.exec_prepared("get_rel", id);
11821182
if (res.num_tuples() != 1) {
11831183
return false;
11841184
}
@@ -1205,7 +1205,7 @@ bool middle_query_pgsql_t::relation_get_format2(
12051205
{
12061206
assert(buffer);
12071207

1208-
auto const res = m_sql_conn.exec_prepared("get_rel", id);
1208+
auto const res = m_db_connection.exec_prepared("get_rel", id);
12091209

12101210
if (res.num_tuples() == 0) {
12111211
return false;
@@ -1305,13 +1305,13 @@ middle_query_pgsql_t::middle_query_pgsql_t(
13051305
std::shared_ptr<node_locations_t> cache,
13061306
std::shared_ptr<node_persistent_cache> persistent_cache,
13071307
middle_pgsql_options const &options)
1308-
: m_sql_conn(connection_params, "middle.query"), m_cache(std::move(cache)),
1308+
: m_db_connection(connection_params, "middle.query"), m_cache(std::move(cache)),
13091309
m_persistent_cache(std::move(persistent_cache)), m_store_options(options)
13101310
{
13111311
// Disable JIT and parallel workers as they are known to cause
13121312
// problems when accessing the intarrays.
1313-
m_sql_conn.set_config("jit_above_cost", "-1");
1314-
m_sql_conn.set_config("max_parallel_workers_per_gather", "0");
1313+
m_db_connection.set_config("jit_above_cost", "-1");
1314+
m_db_connection.set_config("max_parallel_workers_per_gather", "0");
13151315
}
13161316

13171317
static void table_setup(pg_conn_t const &db_connection,

src/middle-pgsql.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class middle_query_pgsql_t : public middle_query_t
8585
std::size_t get_way_node_locations_flatnodes(osmium::WayNodeList *nodes) const;
8686
std::size_t get_way_node_locations_db(osmium::WayNodeList *nodes) const;
8787

88-
pg_conn_t m_sql_conn;
88+
pg_conn_t m_db_connection;
8989
std::shared_ptr<node_locations_t> m_cache;
9090
std::shared_ptr<node_persistent_cache> m_persistent_cache;
9191

src/table.cpp

+33-30
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,26 @@ table_t::table_t(table_t const &other,
5454
// if the other table has already started, then we want to execute
5555
// the same stuff to get into the same state. but if it hasn't, then
5656
// this would be premature.
57-
if (other.m_sql_conn) {
57+
if (other.m_db_connection) {
5858
connect();
5959
prepare();
6060
}
6161
}
6262

63-
void table_t::teardown() { m_sql_conn.reset(); }
63+
void table_t::teardown() { m_db_connection.reset(); }
6464

6565
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, "out.pgsql");
69+
m_db_connection =
70+
std::make_unique<pg_conn_t>(m_connection_params, "out.pgsql");
7071
}
7172

7273
void table_t::start(connection_params_t const &connection_params,
7374
std::string const &table_space)
7475
{
75-
if (m_sql_conn) {
76+
if (m_db_connection) {
7677
throw fmt_error("{} cannot start, its already started.",
7778
m_target->name());
7879
}
@@ -88,11 +89,11 @@ void table_t::start(connection_params_t const &connection_params,
8889

8990
// we are making a new table
9091
if (!m_append) {
91-
m_sql_conn->exec("DROP TABLE IF EXISTS {} CASCADE", qual_name);
92+
m_db_connection->exec("DROP TABLE IF EXISTS {} CASCADE", qual_name);
9293
}
9394

9495
// These _tmp tables can be left behind if we run out of disk space.
95-
m_sql_conn->exec("DROP TABLE IF EXISTS {}", qual_tmp_name);
96+
m_db_connection->exec("DROP TABLE IF EXISTS {}", qual_tmp_name);
9697

9798
//making a new table
9899
if (!m_append) {
@@ -128,10 +129,10 @@ void table_t::start(connection_params_t const &connection_params,
128129
sql += m_table_space;
129130

130131
//create the table
131-
m_sql_conn->exec(sql);
132+
m_db_connection->exec(sql);
132133

133134
if (m_srid != "4326") {
134-
create_geom_check_trigger(*m_sql_conn, m_target->schema(),
135+
create_geom_check_trigger(*m_db_connection, m_target->schema(),
135136
m_target->name(), "ST_IsValid(NEW.way)");
136137
}
137138
}
@@ -143,9 +144,9 @@ void table_t::prepare()
143144
{
144145
//let postgres cache this query as it will presumably happen a lot
145146
auto const qual_name = qualified_name(m_target->schema(), m_target->name());
146-
m_sql_conn->exec("PREPARE get_wkb(int8) AS"
147-
" SELECT way FROM {} WHERE osm_id = $1",
148-
qual_name);
147+
m_db_connection->exec("PREPARE get_wkb(int8) AS"
148+
" SELECT way FROM {} WHERE osm_id = $1",
149+
qual_name);
149150
}
150151

151152
void table_t::generate_copy_column_list()
@@ -187,7 +188,7 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
187188

188189
if (!m_append) {
189190
if (m_srid != "4326") {
190-
drop_geom_check_trigger(*m_sql_conn, m_target->schema(),
191+
drop_geom_check_trigger(*m_db_connection, m_target->schema(),
191192
m_target->name());
192193
}
193194

@@ -216,27 +217,29 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
216217
sql += "way";
217218
}
218219

219-
m_sql_conn->exec(sql);
220+
m_db_connection->exec(sql);
220221

221-
m_sql_conn->exec("DROP TABLE {}", qual_name);
222-
m_sql_conn->exec(R"(ALTER TABLE {} RENAME TO "{}")", qual_tmp_name,
223-
m_target->name());
222+
m_db_connection->exec("DROP TABLE {}", qual_name);
223+
m_db_connection->exec(R"(ALTER TABLE {} RENAME TO "{}")", qual_tmp_name,
224+
m_target->name());
224225

225226
log_info("Creating geometry index on table '{}'...", m_target->name());
226227

227228
// Use fillfactor 100 for un-updatable imports
228-
m_sql_conn->exec("CREATE INDEX ON {} USING GIST (way) {} {}", qual_name,
229-
(updateable ? "" : "WITH (fillfactor = 100)"),
230-
tablespace_clause(table_space_index));
229+
m_db_connection->exec("CREATE INDEX ON {} USING GIST (way) {} {}",
230+
qual_name,
231+
(updateable ? "" : "WITH (fillfactor = 100)"),
232+
tablespace_clause(table_space_index));
231233

232234
/* slim mode needs this to be able to apply diffs */
233235
if (updateable) {
234236
log_info("Creating osm_id index on table '{}'...",
235237
m_target->name());
236-
m_sql_conn->exec("CREATE INDEX ON {} USING BTREE (osm_id) {}",
237-
qual_name, tablespace_clause(table_space_index));
238+
m_db_connection->exec("CREATE INDEX ON {} USING BTREE (osm_id) {}",
239+
qual_name,
240+
tablespace_clause(table_space_index));
238241
if (m_srid != "4326") {
239-
create_geom_check_trigger(*m_sql_conn, m_target->schema(),
242+
create_geom_check_trigger(*m_db_connection, m_target->schema(),
240243
m_target->name(),
241244
"ST_IsValid(NEW.way)");
242245
}
@@ -247,18 +250,18 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
247250
log_info("Creating hstore indexes on table '{}'...",
248251
m_target->name());
249252
if (m_hstore_mode != hstore_column::none) {
250-
m_sql_conn->exec("CREATE INDEX ON {} USING GIN (tags) {}",
251-
qual_name,
252-
tablespace_clause(table_space_index));
253+
m_db_connection->exec("CREATE INDEX ON {} USING GIN (tags) {}",
254+
qual_name,
255+
tablespace_clause(table_space_index));
253256
}
254257
for (auto const &hcolumn : m_hstore_columns) {
255-
m_sql_conn->exec(R"(CREATE INDEX ON {} USING GIN ("{}") {})",
256-
qual_name, hcolumn,
257-
tablespace_clause(table_space_index));
258+
m_db_connection->exec(
259+
R"(CREATE INDEX ON {} USING GIN ("{}") {})", qual_name,
260+
hcolumn, tablespace_clause(table_space_index));
258261
}
259262
}
260263
log_info("Analyzing table '{}'...", m_target->name());
261-
analyze_table(*m_sql_conn, m_target->schema(), m_target->name());
264+
analyze_table(*m_db_connection, m_target->schema(), m_target->name());
262265
}
263266
teardown();
264267
}
@@ -444,6 +447,6 @@ void table_t::escape_type(std::string const &value, ColumnType flags)
444447

445448
pg_result_t table_t::get_wkb(osmid_t id)
446449
{
447-
return m_sql_conn->exec_prepared_as_binary("get_wkb", id);
450+
return m_db_connection->exec_prepared_as_binary("get_wkb", id);
448451
}
449452

src/table.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class table_t
7373
connection_params_t m_connection_params;
7474
std::shared_ptr<db_target_descr_t> m_target;
7575
std::string m_type;
76-
std::unique_ptr<pg_conn_t> m_sql_conn;
76+
std::unique_ptr<pg_conn_t> m_db_connection;
7777
std::string m_srid;
7878
bool m_append;
7979
hstore_column m_hstore_mode;

0 commit comments

Comments
 (0)