fix prepared statement insert already exists (#49154)

This commit is contained in:
Alexander Tokmakov 2023-04-26 14:06:10 +03:00 committed by GitHub
parent b8f7abf507
commit 6434fd8a47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -345,6 +345,7 @@ private:
PreparedInsert(pqxx::connection & connection_, const String & table, const String & schema,
const ColumnsWithTypeAndName & columns, const String & on_conflict_)
: Inserter(connection_)
, statement_name("insert_" + getHexUIntLowercase(thread_local_rng()))
{
WriteBufferFromOwnString buf;
buf << getInsertQuery(schema, table, columns, IdentifierQuotingStyle::DoubleQuotes);
@ -357,12 +358,14 @@ private:
}
buf << ") ";
buf << on_conflict_;
connection.prepare("insert", buf.str());
connection.prepare(statement_name, buf.str());
prepared = true;
}
void complete() override
{
connection.unprepare("insert");
connection.unprepare(statement_name);
prepared = false;
tx.commit();
}
@ -371,8 +374,24 @@ private:
pqxx::params params;
params.reserve(row.size());
params.append_multi(row);
tx.exec_prepared("insert", params);
tx.exec_prepared(statement_name, params);
}
~PreparedInsert() override
{
try
{
if (prepared)
connection.unprepare(statement_name);
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
}
const String statement_name;
bool prepared = false;
};
StorageMetadataPtr metadata_snapshot;