@@ -33,8 +33,11 @@ Result<Nothing> Connection::end_write() {
3333 }
3434 const auto res = PQgetResult (conn_.get ());
3535 if (PQresultStatus (res) != PGRES_COMMAND_OK) {
36- return error (PQerrorMessage (conn_.get ()));
36+ const auto err = error (PQerrorMessage (conn_.get ()));
37+ PQclear (res);
38+ return err;
3739 }
40+ PQclear (res);
3841 return Nothing{};
3942}
4043
@@ -68,6 +71,7 @@ Result<Nothing> Connection::insert_impl(
6871 const auto & d = _data[i];
6972
7073 if (d.size () != current_row.size ()) {
74+ execute (" ROLLBACK;" );
7175 execute (" DEALLOCATE " + name + " ;" );
7276 return error (" Error in entry " + std::to_string (i) + " : Expected " +
7377 std::to_string (current_row.size ()) + " entries, got " +
@@ -88,13 +92,15 @@ Result<Nothing> Connection::insert_impl(
8892 );
8993
9094 const auto status = PQresultStatus (res);
91-
9295 if (status != PGRES_COMMAND_OK) {
96+ PQclear (res);
9397 const auto err = error (std::string (" Executing INSERT failed: " ) +
9498 PQresultErrorMessage (res));
99+ execute (" ROLLBACK;" );
95100 execute (" DEALLOCATE " + name + " ;" );
96101 return err;
97102 }
103+ PQclear (res);
98104 }
99105
100106 return execute (" DEALLOCATE " + name + " ;" );
@@ -170,6 +176,9 @@ Result<Nothing> Connection::write_impl(
170176 static_cast <int >(buffer.size ()));
171177 if (success != 1 ) {
172178 PQputCopyEnd (conn_.get (), NULL );
179+ while (auto res = PQgetResult (conn_.get ()))
180+ PQclear (res);
181+
173182 return error (" Error occurred while writing data to postgres." );
174183 }
175184 }
0 commit comments