Skip to content

Commit 608fd19

Browse files
committed
Optimize various aggregate deserialization functions
The serialized representation of an internal aggregate state is a bytea value. In each deserial function, in order to "receive" the bytea value we appended it onto a short-lived StringInfoData using appendBinaryStringInfo. This was a little wasteful as it meant having to palloc memory, copy a (possibly long) series of bytes then later pfree that memory. Instead of going to this extra trouble, we can just fake up a StringInfoData and point the data directly at the bytea's payload. This should help increase the performance of internal aggregate deserialization. Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/CAApHDvr=e-YOigriSHHm324a40HPqcUhSp6pWWgjz5WwegR=cQ@mail.gmail.com
1 parent 7cc2f59 commit 608fd19

File tree

3 files changed

+42
-42
lines changed

3 files changed

+42
-42
lines changed

src/backend/utils/adt/array_userfuncs.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,13 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
723723
sstate = PG_GETARG_BYTEA_PP(0);
724724

725725
/*
726-
* Copy the bytea into a StringInfo so that we can "receive" it using the
727-
* standard recv-function infrastructure.
726+
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
727+
* the serialized aggregate state value.
728728
*/
729-
initStringInfo(&buf);
730-
appendBinaryStringInfo(&buf,
731-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
729+
buf.data = VARDATA_ANY(sstate);
730+
buf.len = VARSIZE_ANY_EXHDR(sstate);
731+
buf.maxlen = 0;
732+
buf.cursor = 0;
732733

733734
/* element_type */
734735
element_type = pq_getmsgint(&buf, 4);
@@ -825,7 +826,6 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
825826
}
826827

827828
pq_getmsgend(&buf);
828-
pfree(buf.data);
829829

830830
PG_RETURN_POINTER(result);
831831
}
@@ -1134,12 +1134,13 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
11341134
sstate = PG_GETARG_BYTEA_PP(0);
11351135

11361136
/*
1137-
* Copy the bytea into a StringInfo so that we can "receive" it using the
1138-
* standard recv-function infrastructure.
1137+
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
1138+
* the serialized aggregate state value.
11391139
*/
1140-
initStringInfo(&buf);
1141-
appendBinaryStringInfo(&buf,
1142-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
1140+
buf.data = VARDATA_ANY(sstate);
1141+
buf.len = VARSIZE_ANY_EXHDR(sstate);
1142+
buf.maxlen = 0;
1143+
buf.cursor = 0;
11431144

11441145
/* element_type */
11451146
element_type = pq_getmsgint(&buf, 4);
@@ -1197,7 +1198,6 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
11971198
memcpy(result->lbs, temp, sizeof(result->lbs));
11981199

11991200
pq_getmsgend(&buf);
1200-
pfree(buf.data);
12011201

12021202
PG_RETURN_POINTER(result);
12031203
}

src/backend/utils/adt/numeric.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5190,12 +5190,13 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
51905190
init_var(&tmp_var);
51915191

51925192
/*
5193-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5194-
* standard recv-function infrastructure.
5193+
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
5194+
* the serialized aggregate state value.
51955195
*/
5196-
initStringInfo(&buf);
5197-
appendBinaryStringInfo(&buf,
5198-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5196+
buf.data = VARDATA_ANY(sstate);
5197+
buf.len = VARSIZE_ANY_EXHDR(sstate);
5198+
buf.maxlen = 0;
5199+
buf.cursor = 0;
51995200

52005201
result = makeNumericAggStateCurrentContext(false);
52015202

@@ -5222,7 +5223,6 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
52225223
result->nInfcount = pq_getmsgint64(&buf);
52235224

52245225
pq_getmsgend(&buf);
5225-
pfree(buf.data);
52265226

52275227
free_var(&tmp_var);
52285228

@@ -5306,12 +5306,13 @@ numeric_deserialize(PG_FUNCTION_ARGS)
53065306
init_var(&tmp_var);
53075307

53085308
/*
5309-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5310-
* standard recv-function infrastructure.
5309+
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
5310+
* the serialized aggregate state value.
53115311
*/
5312-
initStringInfo(&buf);
5313-
appendBinaryStringInfo(&buf,
5314-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5312+
buf.data = VARDATA_ANY(sstate);
5313+
buf.len = VARSIZE_ANY_EXHDR(sstate);
5314+
buf.maxlen = 0;
5315+
buf.cursor = 0;
53155316

53165317
result = makeNumericAggStateCurrentContext(false);
53175318

@@ -5342,7 +5343,6 @@ numeric_deserialize(PG_FUNCTION_ARGS)
53425343
result->nInfcount = pq_getmsgint64(&buf);
53435344

53445345
pq_getmsgend(&buf);
5345-
pfree(buf.data);
53465346

53475347
free_var(&tmp_var);
53485348

@@ -5677,12 +5677,13 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
56775677
init_var(&tmp_var);
56785678

56795679
/*
5680-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5681-
* standard recv-function infrastructure.
5680+
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
5681+
* the serialized aggregate state value.
56825682
*/
5683-
initStringInfo(&buf);
5684-
appendBinaryStringInfo(&buf,
5685-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5683+
buf.data = VARDATA_ANY(sstate);
5684+
buf.len = VARSIZE_ANY_EXHDR(sstate);
5685+
buf.maxlen = 0;
5686+
buf.cursor = 0;
56865687

56875688
result = makePolyNumAggStateCurrentContext(false);
56885689

@@ -5706,7 +5707,6 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
57065707
#endif
57075708

57085709
pq_getmsgend(&buf);
5709-
pfree(buf.data);
57105710

57115711
free_var(&tmp_var);
57125712

@@ -5868,12 +5868,13 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
58685868
init_var(&tmp_var);
58695869

58705870
/*
5871-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5872-
* standard recv-function infrastructure.
5871+
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
5872+
* the serialized aggregate state value.
58735873
*/
5874-
initStringInfo(&buf);
5875-
appendBinaryStringInfo(&buf,
5876-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5874+
buf.data = VARDATA_ANY(sstate);
5875+
buf.len = VARSIZE_ANY_EXHDR(sstate);
5876+
buf.maxlen = 0;
5877+
buf.cursor = 0;
58775878

58785879
result = makePolyNumAggStateCurrentContext(false);
58795880

@@ -5889,7 +5890,6 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
58895890
#endif
58905891

58915892
pq_getmsgend(&buf);
5892-
pfree(buf.data);
58935893

58945894
free_var(&tmp_var);
58955895

src/backend/utils/adt/varlena.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5289,12 +5289,13 @@ string_agg_deserialize(PG_FUNCTION_ARGS)
52895289
sstate = PG_GETARG_BYTEA_PP(0);
52905290

52915291
/*
5292-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5293-
* standard recv-function infrastructure.
5292+
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
5293+
* the serialized aggregate state value.
52945294
*/
5295-
initStringInfo(&buf);
5296-
appendBinaryStringInfo(&buf,
5297-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5295+
buf.data = VARDATA_ANY(sstate);
5296+
buf.len = VARSIZE_ANY_EXHDR(sstate);
5297+
buf.maxlen = 0;
5298+
buf.cursor = 0;
52985299

52995300
result = makeStringAggState(fcinfo);
53005301

@@ -5307,7 +5308,6 @@ string_agg_deserialize(PG_FUNCTION_ARGS)
53075308
appendBinaryStringInfo(result, data, datalen);
53085309

53095310
pq_getmsgend(&buf);
5310-
pfree(buf.data);
53115311

53125312
PG_RETURN_POINTER(result);
53135313
}

0 commit comments

Comments
 (0)