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
This commit is contained in:
parent
7cc2f59dd5
commit
608fd198de
|
@ -723,12 +723,13 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
|
||||||
sstate = PG_GETARG_BYTEA_PP(0);
|
sstate = PG_GETARG_BYTEA_PP(0);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the bytea into a StringInfo so that we can "receive" it using the
|
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
|
||||||
* standard recv-function infrastructure.
|
* the serialized aggregate state value.
|
||||||
*/
|
*/
|
||||||
initStringInfo(&buf);
|
buf.data = VARDATA_ANY(sstate);
|
||||||
appendBinaryStringInfo(&buf,
|
buf.len = VARSIZE_ANY_EXHDR(sstate);
|
||||||
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
|
buf.maxlen = 0;
|
||||||
|
buf.cursor = 0;
|
||||||
|
|
||||||
/* element_type */
|
/* element_type */
|
||||||
element_type = pq_getmsgint(&buf, 4);
|
element_type = pq_getmsgint(&buf, 4);
|
||||||
|
@ -825,7 +826,6 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
|
||||||
}
|
}
|
||||||
|
|
||||||
pq_getmsgend(&buf);
|
pq_getmsgend(&buf);
|
||||||
pfree(buf.data);
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
PG_RETURN_POINTER(result);
|
||||||
}
|
}
|
||||||
|
@ -1134,12 +1134,13 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
|
||||||
sstate = PG_GETARG_BYTEA_PP(0);
|
sstate = PG_GETARG_BYTEA_PP(0);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the bytea into a StringInfo so that we can "receive" it using the
|
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
|
||||||
* standard recv-function infrastructure.
|
* the serialized aggregate state value.
|
||||||
*/
|
*/
|
||||||
initStringInfo(&buf);
|
buf.data = VARDATA_ANY(sstate);
|
||||||
appendBinaryStringInfo(&buf,
|
buf.len = VARSIZE_ANY_EXHDR(sstate);
|
||||||
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
|
buf.maxlen = 0;
|
||||||
|
buf.cursor = 0;
|
||||||
|
|
||||||
/* element_type */
|
/* element_type */
|
||||||
element_type = pq_getmsgint(&buf, 4);
|
element_type = pq_getmsgint(&buf, 4);
|
||||||
|
@ -1197,7 +1198,6 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
|
||||||
memcpy(result->lbs, temp, sizeof(result->lbs));
|
memcpy(result->lbs, temp, sizeof(result->lbs));
|
||||||
|
|
||||||
pq_getmsgend(&buf);
|
pq_getmsgend(&buf);
|
||||||
pfree(buf.data);
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
PG_RETURN_POINTER(result);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5190,12 +5190,13 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
|
||||||
init_var(&tmp_var);
|
init_var(&tmp_var);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the bytea into a StringInfo so that we can "receive" it using the
|
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
|
||||||
* standard recv-function infrastructure.
|
* the serialized aggregate state value.
|
||||||
*/
|
*/
|
||||||
initStringInfo(&buf);
|
buf.data = VARDATA_ANY(sstate);
|
||||||
appendBinaryStringInfo(&buf,
|
buf.len = VARSIZE_ANY_EXHDR(sstate);
|
||||||
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
|
buf.maxlen = 0;
|
||||||
|
buf.cursor = 0;
|
||||||
|
|
||||||
result = makeNumericAggStateCurrentContext(false);
|
result = makeNumericAggStateCurrentContext(false);
|
||||||
|
|
||||||
|
@ -5222,7 +5223,6 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
|
||||||
result->nInfcount = pq_getmsgint64(&buf);
|
result->nInfcount = pq_getmsgint64(&buf);
|
||||||
|
|
||||||
pq_getmsgend(&buf);
|
pq_getmsgend(&buf);
|
||||||
pfree(buf.data);
|
|
||||||
|
|
||||||
free_var(&tmp_var);
|
free_var(&tmp_var);
|
||||||
|
|
||||||
|
@ -5306,12 +5306,13 @@ numeric_deserialize(PG_FUNCTION_ARGS)
|
||||||
init_var(&tmp_var);
|
init_var(&tmp_var);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the bytea into a StringInfo so that we can "receive" it using the
|
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
|
||||||
* standard recv-function infrastructure.
|
* the serialized aggregate state value.
|
||||||
*/
|
*/
|
||||||
initStringInfo(&buf);
|
buf.data = VARDATA_ANY(sstate);
|
||||||
appendBinaryStringInfo(&buf,
|
buf.len = VARSIZE_ANY_EXHDR(sstate);
|
||||||
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
|
buf.maxlen = 0;
|
||||||
|
buf.cursor = 0;
|
||||||
|
|
||||||
result = makeNumericAggStateCurrentContext(false);
|
result = makeNumericAggStateCurrentContext(false);
|
||||||
|
|
||||||
|
@ -5342,7 +5343,6 @@ numeric_deserialize(PG_FUNCTION_ARGS)
|
||||||
result->nInfcount = pq_getmsgint64(&buf);
|
result->nInfcount = pq_getmsgint64(&buf);
|
||||||
|
|
||||||
pq_getmsgend(&buf);
|
pq_getmsgend(&buf);
|
||||||
pfree(buf.data);
|
|
||||||
|
|
||||||
free_var(&tmp_var);
|
free_var(&tmp_var);
|
||||||
|
|
||||||
|
@ -5677,12 +5677,13 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
|
||||||
init_var(&tmp_var);
|
init_var(&tmp_var);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the bytea into a StringInfo so that we can "receive" it using the
|
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
|
||||||
* standard recv-function infrastructure.
|
* the serialized aggregate state value.
|
||||||
*/
|
*/
|
||||||
initStringInfo(&buf);
|
buf.data = VARDATA_ANY(sstate);
|
||||||
appendBinaryStringInfo(&buf,
|
buf.len = VARSIZE_ANY_EXHDR(sstate);
|
||||||
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
|
buf.maxlen = 0;
|
||||||
|
buf.cursor = 0;
|
||||||
|
|
||||||
result = makePolyNumAggStateCurrentContext(false);
|
result = makePolyNumAggStateCurrentContext(false);
|
||||||
|
|
||||||
|
@ -5706,7 +5707,6 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pq_getmsgend(&buf);
|
pq_getmsgend(&buf);
|
||||||
pfree(buf.data);
|
|
||||||
|
|
||||||
free_var(&tmp_var);
|
free_var(&tmp_var);
|
||||||
|
|
||||||
|
@ -5868,12 +5868,13 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
|
||||||
init_var(&tmp_var);
|
init_var(&tmp_var);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the bytea into a StringInfo so that we can "receive" it using the
|
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
|
||||||
* standard recv-function infrastructure.
|
* the serialized aggregate state value.
|
||||||
*/
|
*/
|
||||||
initStringInfo(&buf);
|
buf.data = VARDATA_ANY(sstate);
|
||||||
appendBinaryStringInfo(&buf,
|
buf.len = VARSIZE_ANY_EXHDR(sstate);
|
||||||
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
|
buf.maxlen = 0;
|
||||||
|
buf.cursor = 0;
|
||||||
|
|
||||||
result = makePolyNumAggStateCurrentContext(false);
|
result = makePolyNumAggStateCurrentContext(false);
|
||||||
|
|
||||||
|
@ -5889,7 +5890,6 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
pq_getmsgend(&buf);
|
pq_getmsgend(&buf);
|
||||||
pfree(buf.data);
|
|
||||||
|
|
||||||
free_var(&tmp_var);
|
free_var(&tmp_var);
|
||||||
|
|
||||||
|
|
|
@ -5289,12 +5289,13 @@ string_agg_deserialize(PG_FUNCTION_ARGS)
|
||||||
sstate = PG_GETARG_BYTEA_PP(0);
|
sstate = PG_GETARG_BYTEA_PP(0);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copy the bytea into a StringInfo so that we can "receive" it using the
|
* Fake up a StringInfo pointing to the bytea's value so we can "receive"
|
||||||
* standard recv-function infrastructure.
|
* the serialized aggregate state value.
|
||||||
*/
|
*/
|
||||||
initStringInfo(&buf);
|
buf.data = VARDATA_ANY(sstate);
|
||||||
appendBinaryStringInfo(&buf,
|
buf.len = VARSIZE_ANY_EXHDR(sstate);
|
||||||
VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
|
buf.maxlen = 0;
|
||||||
|
buf.cursor = 0;
|
||||||
|
|
||||||
result = makeStringAggState(fcinfo);
|
result = makeStringAggState(fcinfo);
|
||||||
|
|
||||||
|
@ -5307,7 +5308,6 @@ string_agg_deserialize(PG_FUNCTION_ARGS)
|
||||||
appendBinaryStringInfo(result, data, datalen);
|
appendBinaryStringInfo(result, data, datalen);
|
||||||
|
|
||||||
pq_getmsgend(&buf);
|
pq_getmsgend(&buf);
|
||||||
pfree(buf.data);
|
|
||||||
|
|
||||||
PG_RETURN_POINTER(result);
|
PG_RETURN_POINTER(result);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue