From ec12856c29f3ea57c45f119e323a2ec6b0ef13c1 Mon Sep 17 00:00:00 2001 From: Shinya Kato Date: Tue, 25 Aug 2026 14:30:28 +0900 Subject: [PATCH v1 1/2] Add pq_begintypsend_with_size() for send functions with a known size pq_begintypsend() allocates the 1 kB default StringInfo buffer however little the send function writes. printtup and COPY TO call send functions in a per-row memory context, so a row with more than a few binary columns overflows the context's 8 kB keeper block and costs a malloc() and free() per row. Add pq_begintypsend_with_size(), which takes the number of bytes the caller expects to append, not counting the length word. The size is a hint, not a limit: appending more enlarges the buffer as before. pq_begintypsend() keeps allocating 1 kB. The length word is now zeroed with one store instead of four appendStringInfoCharMacro() calls. It is zeroed rather than left uninitialized so that a send function that does not call pq_endtypsend() is still rejected by its caller, instead of producing a length from whatever the allocator left there. Author: Andres Freund Author: Shinya Kato Discussion: https://postgr.es/m/ --- src/backend/libpq/pqformat.c | 42 ++++++++++++++++++++++++++++++------ src/include/libpq/pqformat.h | 7 ++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/backend/libpq/pqformat.c b/src/backend/libpq/pqformat.c index 8b41aa4f1cb..7bafc24d103 100644 --- a/src/backend/libpq/pqformat.c +++ b/src/backend/libpq/pqformat.c @@ -49,6 +49,7 @@ * * typsend support (construct a bytea value containing external binary data): * pq_begintypsend - initialize StringInfo buffer + * pq_begintypsend_with_size - initialize StringInfo buffer with a size hint * pq_endtypsend - return the completed string as a "bytea*" * * Special-case message output: @@ -77,6 +78,7 @@ #include "libpq/pqformat.h" #include "mb/pg_wchar.h" #include "port/pg_bswap.h" +#include "utils/memutils.h" #include "varatt.h" @@ -319,17 +321,45 @@ pq_endmessage_reuse(StringInfo buf) /* -------------------------------- * pq_begintypsend - initialize for constructing a bytea result + * + * Callers that can cheaply determine how large their result will be should + * use pq_begintypsend_with_size() instead. * -------------------------------- */ void pq_begintypsend(StringInfo buf) { - initStringInfo(buf); - /* Reserve four bytes for the bytea length word */ - appendStringInfoCharMacro(buf, '\0'); - appendStringInfoCharMacro(buf, '\0'); - appendStringInfoCharMacro(buf, '\0'); - appendStringInfoCharMacro(buf, '\0'); + pq_begintypsend_with_size(buf, PQ_TYPSEND_DEFAULT_SIZE); +} + +/* -------------------------------- + * pq_begintypsend_with_size - like pq_begintypsend, but with size hint + * + * 'size' is the number of bytes the caller expects to append, not counting + * the length word. It is a hint, not a limit. + * -------------------------------- + */ +void +pq_begintypsend_with_size(StringInfo buf, int size) +{ + Assert(size >= 0); + + /* + * Add room for the length word and the terminating NUL. Clamp the result + * to MaxAllocSize, which initStringInfoExt() requires. A larger value + * then fails when it is appended, in enlargeStringInfo(). + */ + size = Min(size, (int) MaxAllocSize - VARHDRSZ - 1); + initStringInfoExt(buf, size + VARHDRSZ + 1); + + /* + * Reserve space for the length word, which pq_endtypsend() fills in. + * Zero it, so that a send function that never calls pq_endtypsend() + * produces a zero length, which callers reject. + */ + memset(buf->data, 0, VARHDRSZ); + buf->len = VARHDRSZ; + buf->data[VARHDRSZ] = '\0'; } /* -------------------------------- diff --git a/src/include/libpq/pqformat.h b/src/include/libpq/pqformat.h index bc4ab1381a9..21bff693077 100644 --- a/src/include/libpq/pqformat.h +++ b/src/include/libpq/pqformat.h @@ -188,7 +188,14 @@ pq_sendint(StringInfo buf, uint32 i, int b) } +/* + * Size hint used by pq_begintypsend(). With the length word and the + * terminating NUL added, the buffer is STRINGINFO_DEFAULT_SIZE bytes. + */ +#define PQ_TYPSEND_DEFAULT_SIZE (STRINGINFO_DEFAULT_SIZE - VARHDRSZ - 1) + extern void pq_begintypsend(StringInfo buf); +extern void pq_begintypsend_with_size(StringInfo buf, int size); extern bytea *pq_endtypsend(StringInfo buf); extern void pq_puttextmessage(char msgtype, const char *str); -- 2.47.3