From 10f7c289c7ddf12a8ee20420f29e8cf464e2c3c8 Mon Sep 17 00:00:00 2001 From: Pavel Zbitskiy Date: Fri, 12 Jan 2024 09:57:10 -0500 Subject: [PATCH] writer: deallocate partially prepared statements on error --- idb/postgres/internal/writer/writer.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/idb/postgres/internal/writer/writer.go b/idb/postgres/internal/writer/writer.go index 20ed3f2bc..1f154d696 100644 --- a/idb/postgres/internal/writer/writer.go +++ b/idb/postgres/internal/writer/writer.go @@ -124,11 +124,17 @@ func MakeWriter(tx pgx.Tx) (Writer, error) { tx: tx, } + names := make([]string, 0, len(statements)) for name, query := range statements { _, err := tx.Prepare(context.Background(), name, query) if err != nil { + for _, name := range names { + // this is an escape path, ignore error + _ = tx.Conn().Deallocate(context.Background(), name) + } return Writer{}, fmt.Errorf("MakeWriter() prepare statement for name '%s' err: %w", name, err) } + names = append(names, name) } return w, nil