From 2042afee003100c7cb59b2e5f68771eb541d986a Mon Sep 17 00:00:00 2001 From: Yahor Yuzefovich Date: Wed, 9 Aug 2023 10:24:58 -0700 Subject: [PATCH] sql: fix recently introduced minor bug around PREPARE In just merged 4a3e78753b9c043c51d2a483b8b591f3abc1457e, we had a minor bug that in case `addPreparedStmt` call fails, we don't restore the original placeholders, which can then lead to panics. Release note: None --- pkg/sql/conn_executor_exec.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/sql/conn_executor_exec.go b/pkg/sql/conn_executor_exec.go index fc892e541487..3345e4d7b439 100644 --- a/pkg/sql/conn_executor_exec.go +++ b/pkg/sql/conn_executor_exec.go @@ -903,16 +903,18 @@ func (ex *connExecutor) execStmtInOpenState( // PREPARE statement itself. oldPlaceholders := p.extendedEvalCtx.Placeholders p.extendedEvalCtx.Placeholders = nil + defer func() { + // The call to addPreparedStmt changed the planner stmt to the + // statement being prepared. Set it back to the PREPARE statement, + // so that it's logged correctly. + p.stmt = stmt + p.extendedEvalCtx.Placeholders = oldPlaceholders + }() if _, err := ex.addPreparedStmt( ctx, name, prepStmt, typeHints, rawTypeHints, PreparedStatementOriginSQL, ); err != nil { return makeErrEvent(err) } - // The call to addPreparedStmt changed the planner stmt to the statement - // being prepared. Set it back to the PREPARE statement, so that it's - // logged correctly. - p.stmt = stmt - p.extendedEvalCtx.Placeholders = oldPlaceholders return nil, nil, nil }