Skip to content

Commit c81bba8

Browse files
mitarjackc
authored andcommitted
Use pgtype.PreallocBytes in LargeObject's Read.
Fixes #1876.
1 parent 523411a commit c81bba8

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

large_objects.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"io"
7+
8+
"github.com/jackc/pgx/v5/pgtype"
79
)
810

911
// The PostgreSQL wire protocol has a limit of 1 GB - 1 per message. See definition of
@@ -115,9 +117,10 @@ func (o *LargeObject) Read(p []byte) (int, error) {
115117
expected = maxLargeObjectMessageLength
116118
}
117119

118-
var res []byte
120+
res := pgtype.PreallocBytes(p[nTotal:])
119121
err := o.tx.QueryRow(o.ctx, "select loread($1, $2)", o.fd, expected).Scan(&res)
120-
copy(p[nTotal:], res)
122+
// We compute expected so that it always fits into p, so it should never happen
123+
// that PreallocBytes's ScanBytes had to allocate a new slice.
121124
nTotal += len(res)
122125
if err != nil {
123126
return nTotal, err

0 commit comments

Comments
 (0)