From 1e565b0d440fee00c9c6d4422e0ffa152f6194e8 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 19 Feb 2022 11:28:39 -0600 Subject: [PATCH 1/2] Handle stmtCache.Get error previously thought impossible The statement cache is already prefilled, but it is possible for the ctx to be canceled between when the statement is prepared and when the statement is retrieved for use. refs #1156 --- conn.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/conn.go b/conn.go index a6136471..5824a25b 100644 --- a/conn.go +++ b/conn.go @@ -773,8 +773,7 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults { var err error sd, err = stmtCache.Get(ctx, bi.query) if err != nil { - // the stmtCache was prefilled from distinctUnpreparedQueries above so we are guaranteed no errors - panic("BUG: unexpected error from stmtCache") + return &batchResults{ctx: ctx, conn: c, err: err} } } From b6b24f9e8a5d7422c229b0d35330347e6e913bdb Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Wed, 29 Dec 2021 17:25:43 -0600 Subject: [PATCH 2/2] Allocate connRows on demand instead of preallocating in bulk The 64 element preallocatedRows may be pinning memory from previous queries. See https://github.com/jackc/pgx/issues/1127 --- conn.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/conn.go b/conn.go index 5824a25b..4bc5db6b 100644 --- a/conn.go +++ b/conn.go @@ -73,9 +73,8 @@ type Conn struct { connInfo *pgtype.ConnInfo - wbuf []byte - preallocatedRows []connRows - eqb extendedQueryBuilder + wbuf []byte + eqb extendedQueryBuilder } // Identifier a PostgreSQL identifier or name. Identifiers can be composed of @@ -513,12 +512,7 @@ func (c *Conn) execPrepared(ctx context.Context, sd *pgconn.StatementDescription } func (c *Conn) getRows(ctx context.Context, sql string, args []interface{}) *connRows { - if len(c.preallocatedRows) == 0 { - c.preallocatedRows = make([]connRows, 64) - } - - r := &c.preallocatedRows[len(c.preallocatedRows)-1] - c.preallocatedRows = c.preallocatedRows[0 : len(c.preallocatedRows)-1] + r := &connRows{} r.ctx = ctx r.logger = c