Revert "Optimize various aggregate deserialization functions"
authorDavid Rowley <drowley@postgresql.org>
Tue, 10 Oct 2023 01:16:54 +0000 (14:16 +1300)
committerDavid Rowley <drowley@postgresql.org>
Tue, 10 Oct 2023 01:16:54 +0000 (14:16 +1300)
This reverts commit 608fd198def5390c3490bfe903730207dfd8eeb4.

On 2nd thoughts, the StringInfo API requires that strings are NUL
terminated and pointing directly to the data in a bytea Datum isn't NUL
terminated.

Discussion: https://postgr.es/m/CAApHDvorfO3iBZ=xpiZvp3uHtJVLyFaPBSvcAhAq2HPLnaNSwQ@mail.gmail.com

src/backend/utils/adt/array_userfuncs.c
src/backend/utils/adt/numeric.c
src/backend/utils/adt/varlena.c

index 7f87df45df654f37b159210c42c65b40d02b59cc..5c4fdcfba4693dcf6d83047806bd62cbe3adfd1d 100644 (file)
@@ -723,13 +723,12 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
    sstate = PG_GETARG_BYTEA_PP(0);
 
    /*
-    * Fake up a StringInfo pointing to the bytea's value so we can "receive"
-    * the serialized aggregate state value.
+    * Copy the bytea into a StringInfo so that we can "receive" it using the
+    * standard recv-function infrastructure.
     */
-   buf.data = VARDATA_ANY(sstate);
-   buf.len = VARSIZE_ANY_EXHDR(sstate);
-   buf.maxlen = 0;
-   buf.cursor = 0;
+   initStringInfo(&buf);
+   appendBinaryStringInfo(&buf,
+                          VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
    /* element_type */
    element_type = pq_getmsgint(&buf, 4);
@@ -826,6 +825,7 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
    }
 
    pq_getmsgend(&buf);
+   pfree(buf.data);
 
    PG_RETURN_POINTER(result);
 }
@@ -1134,13 +1134,12 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
    sstate = PG_GETARG_BYTEA_PP(0);
 
    /*
-    * Fake up a StringInfo pointing to the bytea's value so we can "receive"
-    * the serialized aggregate state value.
+    * Copy the bytea into a StringInfo so that we can "receive" it using the
+    * standard recv-function infrastructure.
     */
-   buf.data = VARDATA_ANY(sstate);
-   buf.len = VARSIZE_ANY_EXHDR(sstate);
-   buf.maxlen = 0;
-   buf.cursor = 0;
+   initStringInfo(&buf);
+   appendBinaryStringInfo(&buf,
+                          VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
    /* element_type */
    element_type = pq_getmsgint(&buf, 4);
@@ -1198,6 +1197,7 @@ array_agg_array_deserialize(PG_FUNCTION_ARGS)
    memcpy(result->lbs, temp, sizeof(result->lbs));
 
    pq_getmsgend(&buf);
+   pfree(buf.data);
 
    PG_RETURN_POINTER(result);
 }
index f4b885005fca8c6ef5d4e8b93539c505e2799798..3c3184f15b5692341342e2d720a09f906d74fba7 100644 (file)
@@ -5190,13 +5190,12 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
    init_var(&tmp_var);
 
    /*
-    * Fake up a StringInfo pointing to the bytea's value so we can "receive"
-    * the serialized aggregate state value.
+    * Copy the bytea into a StringInfo so that we can "receive" it using the
+    * standard recv-function infrastructure.
     */
-   buf.data = VARDATA_ANY(sstate);
-   buf.len = VARSIZE_ANY_EXHDR(sstate);
-   buf.maxlen = 0;
-   buf.cursor = 0;
+   initStringInfo(&buf);
+   appendBinaryStringInfo(&buf,
+                          VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
    result = makeNumericAggStateCurrentContext(false);
 
@@ -5223,6 +5222,7 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
    result->nInfcount = pq_getmsgint64(&buf);
 
    pq_getmsgend(&buf);
+   pfree(buf.data);
 
    free_var(&tmp_var);
 
@@ -5306,13 +5306,12 @@ numeric_deserialize(PG_FUNCTION_ARGS)
    init_var(&tmp_var);
 
    /*
-    * Fake up a StringInfo pointing to the bytea's value so we can "receive"
-    * the serialized aggregate state value.
+    * Copy the bytea into a StringInfo so that we can "receive" it using the
+    * standard recv-function infrastructure.
     */
-   buf.data = VARDATA_ANY(sstate);
-   buf.len = VARSIZE_ANY_EXHDR(sstate);
-   buf.maxlen = 0;
-   buf.cursor = 0;
+   initStringInfo(&buf);
+   appendBinaryStringInfo(&buf,
+                          VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
    result = makeNumericAggStateCurrentContext(false);
 
@@ -5343,6 +5342,7 @@ numeric_deserialize(PG_FUNCTION_ARGS)
    result->nInfcount = pq_getmsgint64(&buf);
 
    pq_getmsgend(&buf);
+   pfree(buf.data);
 
    free_var(&tmp_var);
 
@@ -5677,13 +5677,12 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
    init_var(&tmp_var);
 
    /*
-    * Fake up a StringInfo pointing to the bytea's value so we can "receive"
-    * the serialized aggregate state value.
+    * Copy the bytea into a StringInfo so that we can "receive" it using the
+    * standard recv-function infrastructure.
     */
-   buf.data = VARDATA_ANY(sstate);
-   buf.len = VARSIZE_ANY_EXHDR(sstate);
-   buf.maxlen = 0;
-   buf.cursor = 0;
+   initStringInfo(&buf);
+   appendBinaryStringInfo(&buf,
+                          VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
    result = makePolyNumAggStateCurrentContext(false);
 
@@ -5707,6 +5706,7 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
 #endif
 
    pq_getmsgend(&buf);
+   pfree(buf.data);
 
    free_var(&tmp_var);
 
@@ -5868,13 +5868,12 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
    init_var(&tmp_var);
 
    /*
-    * Fake up a StringInfo pointing to the bytea's value so we can "receive"
-    * the serialized aggregate state value.
+    * Copy the bytea into a StringInfo so that we can "receive" it using the
+    * standard recv-function infrastructure.
     */
-   buf.data = VARDATA_ANY(sstate);
-   buf.len = VARSIZE_ANY_EXHDR(sstate);
-   buf.maxlen = 0;
-   buf.cursor = 0;
+   initStringInfo(&buf);
+   appendBinaryStringInfo(&buf,
+                          VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
    result = makePolyNumAggStateCurrentContext(false);
 
@@ -5890,6 +5889,7 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
 #endif
 
    pq_getmsgend(&buf);
+   pfree(buf.data);
 
    free_var(&tmp_var);
 
index 1aff04fa77d7d3cc19c128f72639b8db3a104aec..72e1e24fe02c24217f354e931808bed941843dc0 100644 (file)
@@ -5289,13 +5289,12 @@ string_agg_deserialize(PG_FUNCTION_ARGS)
    sstate = PG_GETARG_BYTEA_PP(0);
 
    /*
-    * Fake up a StringInfo pointing to the bytea's value so we can "receive"
-    * the serialized aggregate state value.
+    * Copy the bytea into a StringInfo so that we can "receive" it using the
+    * standard recv-function infrastructure.
     */
-   buf.data = VARDATA_ANY(sstate);
-   buf.len = VARSIZE_ANY_EXHDR(sstate);
-   buf.maxlen = 0;
-   buf.cursor = 0;
+   initStringInfo(&buf);
+   appendBinaryStringInfo(&buf,
+                          VARDATA_ANY(sstate), VARSIZE_ANY_EXHDR(sstate));
 
    result = makeStringAggState(fcinfo);
 
@@ -5308,6 +5307,7 @@ string_agg_deserialize(PG_FUNCTION_ARGS)
    appendBinaryStringInfo(result, data, datalen);
 
    pq_getmsgend(&buf);
+   pfree(buf.data);
 
    PG_RETURN_POINTER(result);
 }