@@ -1746,7 +1746,7 @@ fixeddecimalaggstatein(PG_FUNCTION_ARGS)
17461746 FixedDecimalAggState * state ;
17471747 char * token ;
17481748
1749- state = (FixedDecimalAggState * ) palloc0 (sizeof (FixedDecimalAggState ));
1749+ state = (FixedDecimalAggState * ) palloc (sizeof (FixedDecimalAggState ));
17501750
17511751 token = strtok (str , ":" );
17521752 state -> sumX = DatumGetInt64 (DirectFunctionCall3 (fixeddecimalin , CStringGetDatum (token ), 0 , -1 ));
@@ -1771,6 +1771,7 @@ fixeddecimalaggstateout(PG_FUNCTION_ARGS)
17711771 p = fixeddecimal2str (state -> sumX , buf );
17721772 * p ++ = ':' ;
17731773 p = pg_int64tostr (p , state -> N );
1774+
17741775 PG_RETURN_CSTRING (pnstrdup (buf , p - buf ));
17751776}
17761777
@@ -1810,14 +1811,41 @@ fixeddecimalaggstatesend(PG_FUNCTION_ARGS)
18101811Datum
18111812fixeddecimalaggstatecombine (PG_FUNCTION_ARGS )
18121813{
1813- FixedDecimalAggState * state1 = (FixedDecimalAggState * ) PG_GETARG_POINTER (0 );
1814- FixedDecimalAggState * state2 = (FixedDecimalAggState * ) PG_GETARG_POINTER (1 );
1815- FixedDecimalAggState * newstate = palloc (sizeof (FixedDecimalAggState ));
1814+ FixedDecimalAggState * collectstate ;
1815+ FixedDecimalAggState * transstate ;
1816+ MemoryContext agg_context ;
1817+ MemoryContext old_context ;
1818+
1819+ if (!AggCheckCallContext (fcinfo , & agg_context ))
1820+ elog (ERROR , "aggregate function called in non-aggregate context" );
1821+
1822+ old_context = MemoryContextSwitchTo (agg_context );
18161823
1817- newstate -> sumX = DatumGetInt64 ( DirectFunctionCall2 ( fixeddecimalpl , Int64GetDatum ( state1 -> sumX ), Int64GetDatum ( state2 -> sumX )));
1818- newstate -> N = DatumGetInt64 ( DirectFunctionCall2 ( int8pl , Int64GetDatum ( state1 -> N ), Int64GetDatum ( state2 -> N )) );
1824+ collectstate = PG_ARGISNULL ( 0 ) ? NULL : ( FixedDecimalAggState * )
1825+ PG_GETARG_POINTER ( 0 );
18191826
1820- PG_RETURN_POINTER (newstate );
1827+ if (collectstate == NULL )
1828+ {
1829+ collectstate = (FixedDecimalAggState * ) palloc (sizeof
1830+ (FixedDecimalAggState ));
1831+ collectstate -> sumX = 0 ;
1832+ collectstate -> N = 0 ;
1833+ }
1834+
1835+ transstate = PG_ARGISNULL (1 ) ? NULL : (FixedDecimalAggState * )
1836+ PG_GETARG_POINTER (1 );
1837+
1838+ if (transstate == NULL )
1839+ PG_RETURN_POINTER (collectstate );
1840+
1841+ collectstate -> sumX = DatumGetInt64 (DirectFunctionCall2 (fixeddecimalpl ,
1842+ Int64GetDatum (collectstate -> sumX ), Int64GetDatum (transstate -> sumX )));
1843+ collectstate -> N = DatumGetInt64 (DirectFunctionCall2 (int8pl ,
1844+ Int64GetDatum (collectstate -> N ), Int64GetDatum (transstate -> N )));
1845+
1846+ MemoryContextSwitchTo (old_context );
1847+
1848+ PG_RETURN_POINTER (collectstate );
18211849}
18221850
1823- #endif /* PGXC */
1851+ #endif /* PGXC */
0 commit comments