@@ -167,14 +167,15 @@ std::string flex_table_t::build_sql_prepare_get_wkb() const
167167
168168 if (has_multicolumn_id_index ()) {
169169 return fmt::format (
170- R"( PREPARE get_wkb (char(1), bigint) AS)"
170+ R"( PREPARE get_wkb_{} (char(1), bigint) AS)"
171171 R"( SELECT {} FROM {} WHERE "{}" = $1 AND "{}" = $2)" ,
172- columns, full_name (), m_columns[0 ].name (), m_columns[1 ].name ());
172+ m_table_num, columns, full_name (), m_columns[0 ].name (),
173+ m_columns[1 ].name ());
173174 }
174175
175- return fmt::format (R"( PREPARE get_wkb (bigint) AS)"
176+ return fmt::format (R"( PREPARE get_wkb_{} (bigint) AS)"
176177 R"( SELECT {} FROM {} WHERE "{}" = $1)" ,
177- columns, full_name (), id_column_names ());
178+ m_table_num, columns, full_name (), id_column_names ());
178179}
179180
180181std::string
@@ -241,14 +242,6 @@ bool flex_table_t::has_columns_with_expire() const noexcept
241242 [](auto const &column) { return column.has_expire (); });
242243}
243244
244- void table_connection_t::connect (connection_params_t const &connection_params)
245- {
246- assert (!m_db_connection);
247-
248- m_db_connection =
249- std::make_unique<pg_conn_t >(connection_params, " out.flex.table" );
250- }
251-
252245static void enable_check_trigger (pg_conn_t const &db_connection,
253246 flex_table_t const &table)
254247{
@@ -273,50 +266,46 @@ static void enable_check_trigger(pg_conn_t const &db_connection,
273266 checks);
274267}
275268
276- void table_connection_t::start (bool append)
269+ void table_connection_t::start (pg_conn_t const &db_connection, bool append)
277270{
278- assert (m_db_connection);
279-
280271 if (!append) {
281- m_db_connection-> exec (" DROP TABLE IF EXISTS {} CASCADE" ,
282- table ().full_name ());
272+ db_connection. exec (" DROP TABLE IF EXISTS {} CASCADE" ,
273+ table ().full_name ());
283274 }
284275
285276 // These _tmp tables can be left behind if we run out of disk space.
286- m_db_connection-> exec (" DROP TABLE IF EXISTS {}" , table ().full_tmp_name ());
277+ db_connection. exec (" DROP TABLE IF EXISTS {}" , table ().full_tmp_name ());
287278
288279 if (!append) {
289- m_db_connection-> exec (table ().build_sql_create_table (
280+ db_connection. exec (table ().build_sql_create_table (
290281 table ().cluster_by_geom () ? flex_table_t ::table_type::interim
291282 : flex_table_t ::table_type::permanent,
292283 table ().full_name ()));
293284
294- enable_check_trigger (*m_db_connection , table ());
285+ enable_check_trigger (db_connection , table ());
295286 }
296287
297- prepare ();
288+ prepare (db_connection );
298289}
299290
300- void table_connection_t::stop (bool updateable, bool append)
291+ void table_connection_t::stop (pg_conn_t const &db_connection, bool updateable,
292+ bool append)
301293{
302- assert (m_db_connection);
303-
304294 m_copy_mgr.sync ();
305295
306296 if (append) {
307- teardown ();
308297 return ;
309298 }
310299
311300 if (table ().cluster_by_geom ()) {
312301 if (table ().geom_column ().needs_isvalid ()) {
313- drop_geom_check_trigger (*m_db_connection , table ().schema (),
302+ drop_geom_check_trigger (db_connection , table ().schema (),
314303 table ().name ());
315304 }
316305
317306 log_info (" Clustering table '{}' by geometry..." , table ().name ());
318307
319- m_db_connection-> exec (table ().build_sql_create_table (
308+ db_connection. exec (table ().build_sql_create_table (
320309 flex_table_t ::table_type::permanent, table ().full_tmp_name ()));
321310
322311 std::string const columns = table ().build_sql_column_list ();
@@ -349,15 +338,15 @@ void table_connection_t::stop(bool updateable, bool append)
349338 sql += geom_column_name;
350339 }
351340
352- m_db_connection-> exec (sql);
341+ db_connection. exec (sql);
353342
354- m_db_connection-> exec (" DROP TABLE {}" , table ().full_name ());
355- m_db_connection-> exec (R"( ALTER TABLE {} RENAME TO "{}")" ,
356- table ().full_tmp_name (), table ().name ());
343+ db_connection. exec (" DROP TABLE {}" , table ().full_name ());
344+ db_connection. exec (R"( ALTER TABLE {} RENAME TO "{}")" ,
345+ table ().full_tmp_name (), table ().name ());
357346 m_id_index_created = false ;
358347
359348 if (updateable) {
360- enable_check_trigger (*m_db_connection , table ());
349+ enable_check_trigger (db_connection , table ());
361350 }
362351 }
363352
@@ -369,55 +358,53 @@ void table_connection_t::stop(bool updateable, bool append)
369358 index.columns ());
370359 auto const sql = index.create_index (
371360 qualified_name (table ().schema (), table ().name ()));
372- m_db_connection-> exec (sql);
361+ db_connection. exec (sql);
373362 }
374363 }
375364
376365 if ((table ().always_build_id_index () || updateable) &&
377366 table ().has_id_column ()) {
378- create_id_index ();
367+ create_id_index (db_connection );
379368 }
380369
381370 log_info (" Analyzing table '{}'..." , table ().name ());
382- analyze ();
383-
384- teardown ();
371+ analyze (db_connection);
385372}
386373
387- void table_connection_t::prepare ()
374+ void table_connection_t::prepare (pg_conn_t const &db_connection )
388375{
389- assert (m_db_connection);
390376 if (table ().has_id_column () && table ().has_columns_with_expire ()) {
391- m_db_connection-> exec (table ().build_sql_prepare_get_wkb ());
377+ db_connection. exec (table ().build_sql_prepare_get_wkb ());
392378 }
393379}
394380
395- void table_connection_t::analyze ()
381+ void table_connection_t::analyze (pg_conn_t const &db_connection )
396382{
397- analyze_table (*m_db_connection , table ().schema (), table ().name ());
383+ analyze_table (db_connection , table ().schema (), table ().name ());
398384}
399385
400- void table_connection_t::create_id_index ()
386+ void table_connection_t::create_id_index (pg_conn_t const &db_connection )
401387{
402388 if (m_id_index_created) {
403389 log_debug (" Id index on table '{}' already created." , table ().name ());
404390 } else {
405391 log_info (" Creating id index on table '{}'..." , table ().name ());
406- m_db_connection-> exec (table ().build_sql_create_id_index ());
392+ db_connection. exec (table ().build_sql_create_id_index ());
407393 m_id_index_created = true ;
408394 }
409395}
410396
411- pg_result_t table_connection_t::get_geoms_by_id (osmium::item_type type,
397+ pg_result_t table_connection_t::get_geoms_by_id (pg_conn_t const &db_connection,
398+ osmium::item_type type,
412399 osmid_t id) const
413400{
414401 assert (table ().has_geom_column ());
415- assert (m_db_connection );
402+ std::string const stmt = fmt::format ( " get_wkb_{} " , table (). num () );
416403 if (table ().has_multicolumn_id_index ()) {
417- return m_db_connection-> exec_prepared_as_binary (" get_wkb " ,
418- type_to_char (type), id);
404+ return db_connection. exec_prepared_as_binary (stmt. c_str () ,
405+ type_to_char (type), id);
419406 }
420- return m_db_connection-> exec_prepared_as_binary (" get_wkb " , id);
407+ return db_connection. exec_prepared_as_binary (stmt. c_str () , id);
421408}
422409
423410void table_connection_t::delete_rows_with (osmium::item_type type, osmid_t id)
0 commit comments