From fb42fae809f2927874c1a9265a28c22617d6197f Mon Sep 17 00:00:00 2001 From: Shinya Kato Date: Fri, 4 Sep 2026 13:06:14 +0900 Subject: [PATCH v1 2/2] Use pq_begintypsend_with_size() in send functions with a known size Send functions for fixed-length types, and for variable-length types whose output size follows from the value, now pass that size instead of taking the 1 kB default. Their buffers then fit in the per-row memory context's keeper block, so printtup and COPY TO no longer malloc() and free() a block for every row. Arrays, records, ranges, jsonb and the other container types keep the default, because their output size is not known until they build it. Author: Andres Freund Author: Shinya Kato Discussion: https://postgr.es/m/ --- src/backend/utils/adt/bool.c | 2 +- src/backend/utils/adt/cash.c | 2 +- src/backend/utils/adt/char.c | 2 +- src/backend/utils/adt/date.c | 6 +++--- src/backend/utils/adt/enum.c | 6 ++++-- src/backend/utils/adt/float.c | 4 ++-- src/backend/utils/adt/geo_ops.c | 14 +++++++------- src/backend/utils/adt/int.c | 4 ++-- src/backend/utils/adt/int8.c | 2 +- src/backend/utils/adt/json.c | 2 +- src/backend/utils/adt/mac.c | 2 +- src/backend/utils/adt/mac8.c | 2 +- src/backend/utils/adt/name.c | 5 +++-- src/backend/utils/adt/network.c | 5 +++-- src/backend/utils/adt/numeric.c | 2 +- src/backend/utils/adt/oid.c | 2 +- src/backend/utils/adt/oid8.c | 2 +- src/backend/utils/adt/pg_lsn.c | 2 +- src/backend/utils/adt/pseudotypes.c | 7 ++++--- src/backend/utils/adt/tid.c | 2 +- src/backend/utils/adt/timestamp.c | 6 +++--- src/backend/utils/adt/uuid.c | 2 +- src/backend/utils/adt/varbit.c | 2 +- src/backend/utils/adt/varlena.c | 7 ++++--- src/backend/utils/adt/xid.c | 6 +++--- src/backend/utils/adt/xid8funcs.c | 2 +- src/backend/utils/adt/xml.c | 6 ++++-- 27 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/backend/utils/adt/bool.c b/src/backend/utils/adt/bool.c index 090ad7df9ca..64068175de2 100644 --- a/src/backend/utils/adt/bool.c +++ b/src/backend/utils/adt/bool.c @@ -190,7 +190,7 @@ boolsend(PG_FUNCTION_ARGS) bool arg1 = PG_GETARG_BOOL(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 1); pq_sendbyte(&buf, arg1 ? 1 : 0); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/cash.c b/src/backend/utils/adt/cash.c index 0820187b8e6..93ecfd03712 100644 --- a/src/backend/utils/adt/cash.c +++ b/src/backend/utils/adt/cash.c @@ -625,7 +625,7 @@ cash_send(PG_FUNCTION_ARGS) Cash arg1 = PG_GETARG_CASH(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/char.c b/src/backend/utils/adt/char.c index 698863924ee..19fa2ba4ed2 100644 --- a/src/backend/utils/adt/char.c +++ b/src/backend/utils/adt/char.c @@ -107,7 +107,7 @@ charsend(PG_FUNCTION_ARGS) char arg1 = PG_GETARG_CHAR(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 1); pq_sendbyte(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c index 7f746dd84c9..6b4cb081e4f 100644 --- a/src/backend/utils/adt/date.c +++ b/src/backend/utils/adt/date.c @@ -229,7 +229,7 @@ date_send(PG_FUNCTION_ARGS) DateADT date = PG_GETARG_DATEADT(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 4); pq_sendint32(&buf, date); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -1654,7 +1654,7 @@ time_send(PG_FUNCTION_ARGS) TimeADT time = PG_GETARG_TIMEADT(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, time); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -2492,7 +2492,7 @@ timetz_send(PG_FUNCTION_ARGS) TimeTzADT *time = PG_GETARG_TIMETZADT_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 12); pq_sendint64(&buf, time->time); pq_sendint32(&buf, time->zone); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); diff --git a/src/backend/utils/adt/enum.c b/src/backend/utils/adt/enum.c index 9b87373e0e9..d0fd5f8852e 100644 --- a/src/backend/utils/adt/enum.c +++ b/src/backend/utils/adt/enum.c @@ -221,6 +221,7 @@ Datum enum_send(PG_FUNCTION_ARGS) { Oid enumval = PG_GETARG_OID(0); + int slen; StringInfoData buf; HeapTuple tup; Form_pg_enum en; @@ -232,9 +233,10 @@ enum_send(PG_FUNCTION_ARGS) errmsg("invalid internal value for enum: %u", enumval))); en = (Form_pg_enum) GETSTRUCT(tup); + slen = strlen(NameStr(en->enumlabel)); - pq_begintypsend(&buf); - pq_sendtext(&buf, NameStr(en->enumlabel), strlen(NameStr(en->enumlabel))); + pq_begintypsend_with_size(&buf, slen); + pq_sendtext(&buf, NameStr(en->enumlabel), slen); ReleaseSysCache(tup); diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 49ce8c1bd6d..97029f2bfe5 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -393,7 +393,7 @@ float4send(PG_FUNCTION_ARGS) float4 num = PG_GETARG_FLOAT4(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 4); pq_sendfloat4(&buf, num); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -607,7 +607,7 @@ float8send(PG_FUNCTION_ARGS) float8 num = PG_GETARG_FLOAT8(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendfloat8(&buf, num); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/geo_ops.c b/src/backend/utils/adt/geo_ops.c index 73324b91fe5..b0be719a2fd 100644 --- a/src/backend/utils/adt/geo_ops.c +++ b/src/backend/utils/adt/geo_ops.c @@ -506,7 +506,7 @@ box_send(PG_FUNCTION_ARGS) BOX *box = PG_GETARG_BOX_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 32); pq_sendfloat8(&buf, box->high.x); pq_sendfloat8(&buf, box->high.y); pq_sendfloat8(&buf, box->low.x); @@ -1109,7 +1109,7 @@ line_send(PG_FUNCTION_ARGS) LINE *line = PG_GETARG_LINE_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 24); pq_sendfloat8(&buf, line->A); pq_sendfloat8(&buf, line->B); pq_sendfloat8(&buf, line->C); @@ -1578,7 +1578,7 @@ path_send(PG_FUNCTION_ARGS) StringInfoData buf; int32 i; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 5 + path->npts * 16); pq_sendbyte(&buf, path->closed ? 1 : 0); pq_sendint32(&buf, path->npts); for (i = 0; i < path->npts; i++) @@ -1921,7 +1921,7 @@ point_send(PG_FUNCTION_ARGS) Point *pt = PG_GETARG_POINT_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 16); pq_sendfloat8(&buf, pt->x); pq_sendfloat8(&buf, pt->y); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); @@ -2175,7 +2175,7 @@ lseg_send(PG_FUNCTION_ARGS) LSEG *ls = PG_GETARG_LSEG_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 32); pq_sendfloat8(&buf, ls->p[0].x); pq_sendfloat8(&buf, ls->p[0].y); pq_sendfloat8(&buf, ls->p[1].x); @@ -3597,7 +3597,7 @@ poly_send(PG_FUNCTION_ARGS) StringInfoData buf; int32 i; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 4 + poly->npts * 16); pq_sendint32(&buf, poly->npts); for (i = 0; i < poly->npts; i++) { @@ -4831,7 +4831,7 @@ circle_send(PG_FUNCTION_ARGS) CIRCLE *circle = PG_GETARG_CIRCLE_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 24); pq_sendfloat8(&buf, circle->center.x); pq_sendfloat8(&buf, circle->center.y); pq_sendfloat8(&buf, circle->radius); diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c index 68fecbfe2ed..1a042540cb7 100644 --- a/src/backend/utils/adt/int.c +++ b/src/backend/utils/adt/int.c @@ -100,7 +100,7 @@ int2send(PG_FUNCTION_ARGS) int16 arg1 = PG_GETARG_INT16(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 2); pq_sendint16(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -353,7 +353,7 @@ int4send(PG_FUNCTION_ARGS) int32 arg1 = PG_GETARG_INT32(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 4); pq_sendint32(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c index 1a8bddd6bb1..c73c3dddab4 100644 --- a/src/backend/utils/adt/int8.c +++ b/src/backend/utils/adt/int8.c @@ -98,7 +98,7 @@ int8send(PG_FUNCTION_ARGS) int64 arg1 = PG_GETARG_INT64(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index 1e273160b25..a04e1d86a8e 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -142,7 +142,7 @@ json_send(PG_FUNCTION_ARGS) text *t = PG_GETARG_TEXT_PP(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, VARSIZE_ANY_EXHDR(t)); pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/mac.c b/src/backend/utils/adt/mac.c index 3edaac8bb53..6b55cd2b48e 100644 --- a/src/backend/utils/adt/mac.c +++ b/src/backend/utils/adt/mac.c @@ -152,7 +152,7 @@ macaddr_send(PG_FUNCTION_ARGS) macaddr *addr = PG_GETARG_MACADDR_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 6); pq_sendbyte(&buf, addr->a); pq_sendbyte(&buf, addr->b); pq_sendbyte(&buf, addr->c); diff --git a/src/backend/utils/adt/mac8.c b/src/backend/utils/adt/mac8.c index 0425ea473a5..9c284802393 100644 --- a/src/backend/utils/adt/mac8.c +++ b/src/backend/utils/adt/mac8.c @@ -289,7 +289,7 @@ macaddr8_send(PG_FUNCTION_ARGS) macaddr8 *addr = PG_GETARG_MACADDR8_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendbyte(&buf, addr->a); pq_sendbyte(&buf, addr->b); pq_sendbyte(&buf, addr->c); diff --git a/src/backend/utils/adt/name.c b/src/backend/utils/adt/name.c index cbd1808bb5c..5becb7e833d 100644 --- a/src/backend/utils/adt/name.c +++ b/src/backend/utils/adt/name.c @@ -106,10 +106,11 @@ Datum namesend(PG_FUNCTION_ARGS) { Name s = PG_GETARG_NAME(0); + int slen = strlen(NameStr(*s)); StringInfoData buf; - pq_begintypsend(&buf); - pq_sendtext(&buf, NameStr(*s), strlen(NameStr(*s))); + pq_begintypsend_with_size(&buf, slen); + pq_sendtext(&buf, NameStr(*s), slen); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c index 52a3540cf9e..152765de16a 100644 --- a/src/backend/utils/adt/network.c +++ b/src/backend/utils/adt/network.c @@ -272,11 +272,12 @@ network_send(inet *addr, bool is_cidr) int nb, i; - pq_begintypsend(&buf); + nb = ip_addrsize(addr); + + pq_begintypsend_with_size(&buf, 4 + nb); pq_sendbyte(&buf, ip_family(addr)); pq_sendbyte(&buf, ip_bits(addr)); pq_sendbyte(&buf, is_cidr); - nb = ip_addrsize(addr); pq_sendbyte(&buf, nb); addrptr = (char *) ip_addr(addr); for (i = 0; i < nb; i++) diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index ff6dec42010..0b3f27caf4a 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -1152,7 +1152,7 @@ numeric_send(PG_FUNCTION_ARGS) init_var_from_num(num, &x); - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, (x.ndigits + 4) * sizeof(int16)); pq_sendint16(&buf, x.ndigits); pq_sendint16(&buf, x.weight); diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c index a3419728971..f5cd5429526 100644 --- a/src/backend/utils/adt/oid.c +++ b/src/backend/utils/adt/oid.c @@ -73,7 +73,7 @@ oidsend(PG_FUNCTION_ARGS) Oid arg1 = PG_GETARG_OID(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 4); pq_sendint32(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/oid8.c b/src/backend/utils/adt/oid8.c index cfebcb13428..e7b310c7410 100644 --- a/src/backend/utils/adt/oid8.c +++ b/src/backend/utils/adt/oid8.c @@ -74,7 +74,7 @@ oid8send(PG_FUNCTION_ARGS) Oid8 arg1 = PG_GETARG_OID8(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c index 3ec2c2b650e..e0f7cefeada 100644 --- a/src/backend/utils/adt/pg_lsn.c +++ b/src/backend/utils/adt/pg_lsn.c @@ -79,7 +79,7 @@ pg_lsn_send(PG_FUNCTION_ARGS) XLogRecPtr lsn = PG_GETARG_LSN(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, lsn); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/pseudotypes.c b/src/backend/utils/adt/pseudotypes.c index 4581c4b1697..f25649cc332 100644 --- a/src/backend/utils/adt/pseudotypes.c +++ b/src/backend/utils/adt/pseudotypes.c @@ -134,10 +134,11 @@ Datum cstring_send(PG_FUNCTION_ARGS) { char *str = PG_GETARG_CSTRING(0); + int slen = strlen(str); StringInfoData buf; - pq_begintypsend(&buf); - pq_sendtext(&buf, str, strlen(str)); + pq_begintypsend_with_size(&buf, slen); + pq_sendtext(&buf, str, slen); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -287,7 +288,7 @@ void_send(PG_FUNCTION_ARGS) StringInfoData buf; /* send an empty string */ - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 0); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c index f730b2f07c2..6e9e1116a7c 100644 --- a/src/backend/utils/adt/tid.c +++ b/src/backend/utils/adt/tid.c @@ -161,7 +161,7 @@ tidsend(PG_FUNCTION_ARGS) ItemPointer itemPtr = PG_GETARG_ITEMPOINTER(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 6); pq_sendint32(&buf, ItemPointerGetBlockNumberNoCheck(itemPtr)); pq_sendint16(&buf, ItemPointerGetOffsetNumberNoCheck(itemPtr)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 43efa548452..1191f99b431 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -288,7 +288,7 @@ timestamp_send(PG_FUNCTION_ARGS) Timestamp timestamp = PG_GETARG_TIMESTAMP(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, timestamp); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -837,7 +837,7 @@ timestamptz_send(PG_FUNCTION_ARGS) TimestampTz timestamp = PG_GETARG_TIMESTAMPTZ(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, timestamp); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -1025,7 +1025,7 @@ interval_send(PG_FUNCTION_ARGS) Interval *interval = PG_GETARG_INTERVAL_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 16); pq_sendint64(&buf, interval->time); pq_sendint32(&buf, interval->day); pq_sendint32(&buf, interval->month); diff --git a/src/backend/utils/adt/uuid.c b/src/backend/utils/adt/uuid.c index 49e59e7c75a..943f5a27c09 100644 --- a/src/backend/utils/adt/uuid.c +++ b/src/backend/utils/adt/uuid.c @@ -302,7 +302,7 @@ uuid_send(PG_FUNCTION_ARGS) pg_uuid_t *uuid = PG_GETARG_UUID_P(0); StringInfoData buffer; - pq_begintypsend(&buffer); + pq_begintypsend_with_size(&buffer, UUID_LEN); pq_sendbytes(&buffer, uuid->data, UUID_LEN); PG_RETURN_BYTEA_P(pq_endtypsend(&buffer)); } diff --git a/src/backend/utils/adt/varbit.c b/src/backend/utils/adt/varbit.c index 00ee1535208..3d445b2c3f9 100644 --- a/src/backend/utils/adt/varbit.c +++ b/src/backend/utils/adt/varbit.c @@ -683,7 +683,7 @@ varbit_send(PG_FUNCTION_ARGS) VarBit *s = PG_GETARG_VARBIT_P(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, VARBITBYTES(s) + 4); pq_sendint32(&buf, VARBITLEN(s)); pq_sendbytes(&buf, VARBITS(s), VARBITBYTES(s)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 6c95fcb24b7..c25182aa6bf 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -320,7 +320,7 @@ textsend(PG_FUNCTION_ARGS) text *t = PG_GETARG_TEXT_PP(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, VARSIZE_ANY_EXHDR(t)); pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -373,10 +373,11 @@ unknownsend(PG_FUNCTION_ARGS) { /* representation is same as cstring */ char *str = PG_GETARG_CSTRING(0); + int slen = strlen(str); StringInfoData buf; - pq_begintypsend(&buf); - pq_sendtext(&buf, str, strlen(str)); + pq_begintypsend_with_size(&buf, slen); + pq_sendtext(&buf, str, slen); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c index f746a5f97dd..12f141f5c1d 100644 --- a/src/backend/utils/adt/xid.c +++ b/src/backend/utils/adt/xid.c @@ -69,7 +69,7 @@ xidsend(PG_FUNCTION_ARGS) TransactionId arg1 = PG_GETARG_TRANSACTIONID(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 4); pq_sendint32(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -227,7 +227,7 @@ xid8send(PG_FUNCTION_ARGS) FullTransactionId arg1 = PG_GETARG_FULLTRANSACTIONID(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 8); pq_sendint64(&buf, (uint64) U64FromFullTransactionId(arg1)); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } @@ -386,7 +386,7 @@ cidsend(PG_FUNCTION_ARGS) CommandId arg1 = PG_GETARG_COMMANDID(0); StringInfoData buf; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 4); pq_sendint32(&buf, arg1); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c index c607e78d9ac..8d497f46888 100644 --- a/src/backend/utils/adt/xid8funcs.c +++ b/src/backend/utils/adt/xid8funcs.c @@ -538,7 +538,7 @@ pg_snapshot_send(PG_FUNCTION_ARGS) StringInfoData buf; uint32 i; - pq_begintypsend(&buf); + pq_begintypsend_with_size(&buf, 20 + snap->nxip * 8); pq_sendint32(&buf, snap->nxip); pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xmin)); pq_sendint64(&buf, (int64) U64FromFullTransactionId(snap->xmax)); diff --git a/src/backend/utils/adt/xml.c b/src/backend/utils/adt/xml.c index 44a3a21d135..2451d32856f 100644 --- a/src/backend/utils/adt/xml.c +++ b/src/backend/utils/adt/xml.c @@ -438,6 +438,7 @@ xml_send(PG_FUNCTION_ARGS) { xmltype *x = PG_GETARG_XML_P(0); char *outval; + int outlen; StringInfoData buf; /* @@ -445,9 +446,10 @@ xml_send(PG_FUNCTION_ARGS) * declaration. pq_sendtext will do the conversion. */ outval = xml_out_internal(x, pg_get_client_encoding()); + outlen = strlen(outval); - pq_begintypsend(&buf); - pq_sendtext(&buf, outval, strlen(outval)); + pq_begintypsend_with_size(&buf, outlen); + pq_sendtext(&buf, outval, outlen); pfree(outval); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); } -- 2.47.3