Skip to content

Commit 6ec5cff

Browse files
committed
Always ResetConnection on error
When using the `clickhouse-cpp` library, always `ResetConnection` on error and release the insert block. Otherwise, the client can be stuck in an unusable state that can interfere with future queries. This also changes the behavior after `EndInsert` fails, changing it to a proper error rather than a `NOTICE`; it doesn't seem right to let the next query fail, as the previous implementation had it.
1 parent 4932618 commit 6ec5cff

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/binary.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,17 @@ void ch_binary_insert_state_free(void * c)
291291
if (state->insert_block)
292292
{
293293
/* Finish the insert to set the proper ClickHouse state */
294+
delete (Block *)state->insert_block;
294295
Client * client = (Client *)state->conn->client;
295296
try
296297
{
297298
client->EndInsert();
298299
}
299300
catch (const std::exception & e)
300301
{
301-
// just ignore, next query will fail
302-
elog(NOTICE, "pg_clickhouse: could not finish INSERT: - %s", e.what());
302+
client->ResetConnection();
303+
elog(ERROR, "pg_clickhouse: could not finish INSERT - %s", e.what());
303304
}
304-
delete (Block *)state->insert_block;
305305
}
306306
}
307307

@@ -316,6 +316,7 @@ void ch_binary_prepare_insert(void * conn, char * query, ch_binary_insert_state
316316
}
317317
catch (const std::exception & e)
318318
{
319+
client->ResetConnection();
319320
elog(ERROR, "pg_clickhouse: could not prepare insert - %s", e.what());
320321
}
321322

@@ -585,16 +586,18 @@ void ch_binary_column_append_data(ch_binary_insert_state * state, size_t colidx)
585586

586587
void ch_binary_insert_columns(ch_binary_insert_state * state)
587588
{
589+
Client * client = (Client *)state->conn->client;
590+
auto block = (Block *)state->insert_block;
588591
try
589592
{
590-
Client * client = (Client *)state->conn->client;
591-
auto block = (Block *)state->insert_block;
592593
block->RefreshRowCount();
593594
client->SendInsertBlock(*block);
594595
block->Clear();
595596
}
596597
catch (const std::exception & e)
597598
{
599+
client->ResetConnection();
600+
delete block;
598601
elog(ERROR, "pg_clickhouse: could not insert columns - %s", e.what());
599602
}
600603
}

0 commit comments

Comments
 (0)