@@ -87,7 +87,8 @@ db_copy_thread_t::db_copy_thread_t(connection_params_t const &connection_params)
87
87
// Connection params are captured by copy here, because we don't know
88
88
// whether the reference will still be valid once we get around to running
89
89
// the thread.
90
- m_worker = std::thread{thread_t {connection_params, &m_shared}};
90
+ m_worker =
91
+ std::thread{thread_t {pg_conn_t {connection_params, " copy" }, &m_shared}};
91
92
}
92
93
93
94
db_copy_thread_t ::~db_copy_thread_t () { finish (); }
@@ -126,24 +127,21 @@ void db_copy_thread_t::finish()
126
127
}
127
128
}
128
129
129
- db_copy_thread_t ::thread_t ::thread_t (connection_params_t connection_params ,
130
+ db_copy_thread_t ::thread_t ::thread_t (pg_conn_t &&db_connection ,
130
131
shared *shared)
131
- : m_connection_params (std::move(connection_params )), m_shared(shared)
132
+ : m_db_connection (std::move(db_connection )), m_shared(shared)
132
133
{}
133
134
134
135
void db_copy_thread_t::thread_t::operator ()()
135
136
{
136
137
try {
137
- m_db_connection =
138
- std::make_unique<pg_conn_t >(m_connection_params, " copy" );
139
-
140
138
// Disable sequential scan on database tables in the copy threads.
141
139
// The copy threads only do COPYs (which are unaffected by this
142
140
// setting) and DELETEs which we know benefit from the index. For
143
141
// some reason PostgreSQL chooses in some cases not to use that index,
144
142
// possibly because the DELETEs get a large list of ids to delete of
145
143
// which many are not in the table which confuses the query planner.
146
- m_db_connection-> exec (" SET enable_seqscan = off" );
144
+ m_db_connection. exec (" SET enable_seqscan = off" );
147
145
148
146
bool done = false ;
149
147
while (!done) {
@@ -166,8 +164,6 @@ void db_copy_thread_t::thread_t::operator()()
166
164
}
167
165
168
166
finish_copy ();
169
-
170
- m_db_connection.reset ();
171
167
} catch (std::runtime_error const &e) {
172
168
log_error (" DB copy thread failed: {}" , e.what ());
173
169
std::exit (2 ); // NOLINT(concurrency-mt-unsafe)
@@ -182,13 +178,13 @@ bool db_copy_thread_t::thread_t::execute(db_cmd_copy_delete_t<DELETER> &cmd)
182
178
finish_copy ();
183
179
}
184
180
185
- cmd.delete_data (* m_db_connection);
181
+ cmd.delete_data (m_db_connection);
186
182
187
183
if (!m_inflight) {
188
184
start_copy (cmd.target );
189
185
}
190
186
191
- m_db_connection-> copy_send (cmd.buffer , cmd.target ->name ());
187
+ m_db_connection. copy_send (cmd.buffer , cmd.target ->name ());
192
188
193
189
return false ;
194
190
}
@@ -224,15 +220,15 @@ void db_copy_thread_t::thread_t::start_copy(
224
220
}
225
221
226
222
sql.push_back (' \0 ' );
227
- m_db_connection-> copy_start (sql.data ());
223
+ m_db_connection. copy_start (sql.data ());
228
224
229
225
m_inflight = target;
230
226
}
231
227
232
228
void db_copy_thread_t::thread_t::finish_copy ()
233
229
{
234
230
if (m_inflight) {
235
- m_db_connection-> copy_end (m_inflight->name ());
231
+ m_db_connection. copy_end (m_inflight->name ());
236
232
m_inflight.reset ();
237
233
}
238
234
}
0 commit comments