@@ -3176,6 +3176,22 @@ makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
3176
3176
return state ;
3177
3177
}
3178
3178
3179
+ /*
3180
+ * Like makeNumericAggState(), but allocate the state in the current memory
3181
+ * context.
3182
+ */
3183
+ static NumericAggState *
3184
+ makeNumericAggStateCurrentContext (bool calcSumX2 )
3185
+ {
3186
+ NumericAggState * state ;
3187
+
3188
+ state = (NumericAggState * ) palloc0 (sizeof (NumericAggState ));
3189
+ state -> calcSumX2 = calcSumX2 ;
3190
+ state -> agg_context = CurrentMemoryContext ;
3191
+
3192
+ return state ;
3193
+ }
3194
+
3179
3195
/*
3180
3196
* Accumulate a new input value for numeric aggregate functions.
3181
3197
*/
@@ -3374,7 +3390,7 @@ numeric_combine(PG_FUNCTION_ARGS)
3374
3390
{
3375
3391
old_context = MemoryContextSwitchTo (agg_context );
3376
3392
3377
- state1 = makeNumericAggState ( fcinfo , true);
3393
+ state1 = makeNumericAggStateCurrentContext ( true);
3378
3394
state1 -> N = state2 -> N ;
3379
3395
state1 -> NaNcount = state2 -> NaNcount ;
3380
3396
state1 -> maxScale = state2 -> maxScale ;
@@ -3465,7 +3481,7 @@ numeric_avg_combine(PG_FUNCTION_ARGS)
3465
3481
{
3466
3482
old_context = MemoryContextSwitchTo (agg_context );
3467
3483
3468
- state1 = makeNumericAggState ( fcinfo , false);
3484
+ state1 = makeNumericAggStateCurrentContext ( false);
3469
3485
state1 -> N = state2 -> N ;
3470
3486
state1 -> NaNcount = state2 -> NaNcount ;
3471
3487
state1 -> maxScale = state2 -> maxScale ;
@@ -3579,12 +3595,12 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
3579
3595
3580
3596
/*
3581
3597
* Copy the bytea into a StringInfo so that we can "receive" it using the
3582
- * standard pq API .
3598
+ * standard recv-function infrastructure .
3583
3599
*/
3584
3600
initStringInfo (& buf );
3585
3601
appendBinaryStringInfo (& buf , VARDATA (sstate ), VARSIZE (sstate ) - VARHDRSZ );
3586
3602
3587
- result = makeNumericAggState ( fcinfo , false);
3603
+ result = makeNumericAggStateCurrentContext ( false);
3588
3604
3589
3605
/* N */
3590
3606
result -> N = pq_getmsgint64 (& buf );
@@ -3691,12 +3707,12 @@ numeric_deserialize(PG_FUNCTION_ARGS)
3691
3707
3692
3708
/*
3693
3709
* Copy the bytea into a StringInfo so that we can "receive" it using the
3694
- * standard pq API .
3710
+ * standard recv-function infrastructure .
3695
3711
*/
3696
3712
initStringInfo (& buf );
3697
3713
appendBinaryStringInfo (& buf , VARDATA (sstate ), VARSIZE (sstate ) - VARHDRSZ );
3698
3714
3699
- result = makeNumericAggState ( fcinfo , false);
3715
+ result = makeNumericAggStateCurrentContext ( false);
3700
3716
3701
3717
/* N */
3702
3718
result -> N = pq_getmsgint64 (& buf );
@@ -3803,6 +3819,21 @@ makeInt128AggState(FunctionCallInfo fcinfo, bool calcSumX2)
3803
3819
return state ;
3804
3820
}
3805
3821
3822
+ /*
3823
+ * Like makeInt128AggState(), but allocate the state in the current memory
3824
+ * context.
3825
+ */
3826
+ static Int128AggState *
3827
+ makeInt128AggStateCurrentContext (bool calcSumX2 )
3828
+ {
3829
+ Int128AggState * state ;
3830
+
3831
+ state = (Int128AggState * ) palloc0 (sizeof (Int128AggState ));
3832
+ state -> calcSumX2 = calcSumX2 ;
3833
+
3834
+ return state ;
3835
+ }
3836
+
3806
3837
/*
3807
3838
* Accumulate a new input value for 128-bit aggregate functions.
3808
3839
*/
@@ -3831,9 +3862,11 @@ do_int128_discard(Int128AggState *state, int128 newval)
3831
3862
3832
3863
typedef Int128AggState PolyNumAggState ;
3833
3864
#define makePolyNumAggState makeInt128AggState
3865
+ #define makePolyNumAggStateCurrentContext makeInt128AggStateCurrentContext
3834
3866
#else
3835
3867
typedef NumericAggState PolyNumAggState ;
3836
3868
#define makePolyNumAggState makeNumericAggState
3869
+ #define makePolyNumAggStateCurrentContext makeNumericAggStateCurrentContext
3837
3870
#endif
3838
3871
3839
3872
Datum
@@ -4072,12 +4105,12 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
4072
4105
4073
4106
/*
4074
4107
* Copy the bytea into a StringInfo so that we can "receive" it using the
4075
- * standard pq API .
4108
+ * standard recv-function infrastructure .
4076
4109
*/
4077
4110
initStringInfo (& buf );
4078
4111
appendBinaryStringInfo (& buf , VARDATA (sstate ), VARSIZE (sstate ) - VARHDRSZ );
4079
4112
4080
- result = makePolyNumAggState ( fcinfo , false);
4113
+ result = makePolyNumAggStateCurrentContext ( false);
4081
4114
4082
4115
/* N */
4083
4116
result -> N = pq_getmsgint64 (& buf );
@@ -4210,7 +4243,8 @@ int8_avg_combine(PG_FUNCTION_ARGS)
4210
4243
4211
4244
/*
4212
4245
* int8_avg_serialize
4213
- * Serialize PolyNumAggState into bytea using the standard pq API.
4246
+ * Serialize PolyNumAggState into bytea using the standard
4247
+ * recv-function infrastructure.
4214
4248
*/
4215
4249
Datum
4216
4250
int8_avg_serialize (PG_FUNCTION_ARGS )
@@ -4284,12 +4318,12 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
4284
4318
4285
4319
/*
4286
4320
* Copy the bytea into a StringInfo so that we can "receive" it using the
4287
- * standard pq API .
4321
+ * standard recv-function infrastructure .
4288
4322
*/
4289
4323
initStringInfo (& buf );
4290
4324
appendBinaryStringInfo (& buf , VARDATA (sstate ), VARSIZE (sstate ) - VARHDRSZ );
4291
4325
4292
- result = makePolyNumAggState ( fcinfo , false);
4326
+ result = makePolyNumAggStateCurrentContext ( false);
4293
4327
4294
4328
/* N */
4295
4329
result -> N = pq_getmsgint64 (& buf );
0 commit comments