Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0c882a2

Browse filesBrowse files
committed
Optimize various aggregate deserialization functions, take 2
f0efa5a added initReadOnlyStringInfo to allow a StringInfo to be initialized from an existing buffer and also relaxed the requirement that a StringInfo's buffer must be NUL terminated at data[len]. Now that we have that, there's no need for these aggregate deserial functions to use appendBinaryStringInfo() as that rather wastefully palloc'd a new buffer and memcpy'd in the bytea's buffer. Instead, we can just use the bytea's buffer and point the StringInfo directly to that using the new initializer function. In Amdahl's law, this speeds up the serial portion of parallel aggregates and makes sum(numeric), avg(numeric), var_pop(numeric), var_samp(numeric), variance(numeric), stddev_pop(numeric), stddev_samp(numeric), stddev(numeric), array_agg(anyarray), string_agg(text) and string_agg(bytea) scale better in parallel queries. Author: David Rowley Discussion: https://postgr.es/m/CAApHDvr%3De-YOigriSHHm324a40HPqcUhSp6pWWgjz5WwegR%3DcQ%40mail.gmail.com
1 parent 26f9882 commit 0c882a2
Copy full SHA for 0c882a2

File tree

3 files changed

+28
-42
lines changed
Filter options

3 files changed

+28
-42
lines changed

‎src/backend/utils/adt/array_userfuncs.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/array_userfuncs.c
+8-12Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,11 @@ 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+
* Initialize a StringInfo so that we can "receive" it using the standard
727+
* recv-function infrastructure.
728728
*/
729-
initStringInfo(&buf);
730-
appendBinaryStringInfo(&buf,
731-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
729+
initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
730+
VARSIZE_ANY_EXHDR(sstate));
732731

733732
/* element_type */
734733
element_type = pq_getmsgint(&buf, 4);
@@ -815,7 +814,6 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
815814
}
816815

817816
pq_getmsgend(&buf);
818-
pfree(buf.data);
819817

820818
PG_RETURN_POINTER(result);
821819
}
@@ -1124,12 +1122,11 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
11241122
sstate = PG_GETARG_BYTEA_PP(0);
11251123

11261124
/*
1127-
* Copy the bytea into a StringInfo so that we can "receive" it using the
1128-
* standard recv-function infrastructure.
1125+
* Initialize a StringInfo so that we can "receive" it using the standard
1126+
* recv-function infrastructure.
11291127
*/
1130-
initStringInfo(&buf);
1131-
appendBinaryStringInfo(&buf,
1132-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
1128+
initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
1129+
VARSIZE_ANY_EXHDR(sstate));
11331130

11341131
/* element_type */
11351132
element_type = pq_getmsgint(&buf, 4);
@@ -1187,7 +1184,6 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
11871184
memcpy(result->lbs, temp, sizeof(result->lbs));
11881185

11891186
pq_getmsgend(&buf);
1190-
pfree(buf.data);
11911187

11921188
PG_RETURN_POINTER(result);
11931189
}

‎src/backend/utils/adt/numeric.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/numeric.c
+16-24Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5190,12 +5190,11 @@ 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+
* Initialize a StringInfo so that we can "receive" it using the standard
5194+
* recv-function infrastructure.
51955195
*/
5196-
initStringInfo(&buf);
5197-
appendBinaryStringInfo(&buf,
5198-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5196+
initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
5197+
VARSIZE_ANY_EXHDR(sstate));
51995198

52005199
result = makeNumericAggStateCurrentContext(false);
52015200

@@ -5222,7 +5221,6 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
52225221
result->nInfcount = pq_getmsgint64(&buf);
52235222

52245223
pq_getmsgend(&buf);
5225-
pfree(buf.data);
52265224

52275225
free_var(&tmp_var);
52285226

@@ -5306,12 +5304,11 @@ numeric_deserialize(PG_FUNCTION_ARGS)
53065304
init_var(&tmp_var);
53075305

53085306
/*
5309-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5310-
* standard recv-function infrastructure.
5307+
* Initialize a StringInfo so that we can "receive" it using the standard
5308+
* recv-function infrastructure.
53115309
*/
5312-
initStringInfo(&buf);
5313-
appendBinaryStringInfo(&buf,
5314-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5310+
initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
5311+
VARSIZE_ANY_EXHDR(sstate));
53155312

53165313
result = makeNumericAggStateCurrentContext(false);
53175314

@@ -5342,7 +5339,6 @@ numeric_deserialize(PG_FUNCTION_ARGS)
53425339
result->nInfcount = pq_getmsgint64(&buf);
53435340

53445341
pq_getmsgend(&buf);
5345-
pfree(buf.data);
53465342

53475343
free_var(&tmp_var);
53485344

@@ -5677,12 +5673,11 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
56775673
init_var(&tmp_var);
56785674

56795675
/*
5680-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5681-
* standard recv-function infrastructure.
5676+
* Initialize a StringInfo so that we can "receive" it using the standard
5677+
* recv-function infrastructure.
56825678
*/
5683-
initStringInfo(&buf);
5684-
appendBinaryStringInfo(&buf,
5685-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5679+
initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
5680+
VARSIZE_ANY_EXHDR(sstate));
56865681

56875682
result = makePolyNumAggStateCurrentContext(false);
56885683

@@ -5706,7 +5701,6 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
57065701
#endif
57075702

57085703
pq_getmsgend(&buf);
5709-
pfree(buf.data);
57105704

57115705
free_var(&tmp_var);
57125706

@@ -5868,12 +5862,11 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
58685862
init_var(&tmp_var);
58695863

58705864
/*
5871-
* Copy the bytea into a StringInfo so that we can "receive" it using the
5872-
* standard recv-function infrastructure.
5865+
* Initialize a StringInfo so that we can "receive" it using the standard
5866+
* recv-function infrastructure.
58735867
*/
5874-
initStringInfo(&buf);
5875-
appendBinaryStringInfo(&buf,
5876-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5868+
initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
5869+
VARSIZE_ANY_EXHDR(sstate));
58775870

58785871
result = makePolyNumAggStateCurrentContext(false);
58795872

@@ -5889,7 +5882,6 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
58895882
#endif
58905883

58915884
pq_getmsgend(&buf);
5892-
pfree(buf.data);
58935885

58945886
free_var(&tmp_var);
58955887

‎src/backend/utils/adt/varlena.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/varlena.c
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5289,12 +5289,11 @@ 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+
* Initialize a StringInfo so that we can "receive" it using the standard
5293+
* recv-function infrastructure.
52945294
*/
5295-
initStringInfo(&buf);
5296-
appendBinaryStringInfo(&buf,
5297-
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
5295+
initReadOnlyStringInfo(&buf, VARDATA_ANY(sstate),
5296+
VARSIZE_ANY_EXHDR(sstate));
52985297

52995298
result = makeStringAggState(fcinfo);
53005299

@@ -5307,7 +5306,6 @@ string_agg_deserialize(PG_FUNCTION_ARGS)
53075306
appendBinaryStringInfo(result, data, datalen);
53085307

53095308
pq_getmsgend(&buf);
5310-
pfree(buf.data);
53115309

53125310
PG_RETURN_POINTER(result);
53135311
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.