diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index d3d33aae935..14e8895ec70 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -5073,7 +5073,15 @@ numeric_combine(PG_FUNCTION_ARGS) state2 = PG_ARGISNULL(1) ? NULL : (NumericAggState *) PG_GETARG_POINTER(1); if (state2 == NULL) + { + /* + * NULL state2 is easy, just return state1, which we know is already + * in the agg_context + */ + if (state1 == NULL) + PG_RETURN_NULL(); PG_RETURN_POINTER(state1); + } /* manually copy all fields from state2 to state1 */ if (state1 == NULL) @@ -5165,7 +5173,15 @@ numeric_avg_combine(PG_FUNCTION_ARGS) state2 = PG_ARGISNULL(1) ? NULL : (NumericAggState *) PG_GETARG_POINTER(1); if (state2 == NULL) + { + /* + * NULL state2 is easy, just return state1, which we know is already + * in the agg_context + */ + if (state1 == NULL) + PG_RETURN_NULL(); PG_RETURN_POINTER(state1); + } /* manually copy all fields from state2 to state1 */ if (state1 == NULL) @@ -5649,7 +5665,15 @@ numeric_poly_combine(PG_FUNCTION_ARGS) state2 = PG_ARGISNULL(1) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(1); if (state2 == NULL) + { + /* + * NULL state2 is easy, just return state1, which we know is already + * in the agg_context + */ + if (state1 == NULL) + PG_RETURN_NULL(); PG_RETURN_POINTER(state1); + } /* manually copy all fields from state2 to state1 */ if (state1 == NULL) @@ -5852,7 +5876,15 @@ int8_avg_combine(PG_FUNCTION_ARGS) state2 = PG_ARGISNULL(1) ? NULL : (PolyNumAggState *) PG_GETARG_POINTER(1); if (state2 == NULL) + { + /* + * NULL state2 is easy, just return state1, which we know is already + * in the agg_context + */ + if (state1 == NULL) + PG_RETURN_NULL(); PG_RETURN_POINTER(state1); + } /* manually copy all fields from state2 to state1 */ if (state1 == NULL) diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index afd5d7f7502..b57739b7e83 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -4035,7 +4035,15 @@ interval_avg_combine(PG_FUNCTION_ARGS) state2 = PG_ARGISNULL(1) ? NULL : (IntervalAggState *) PG_GETARG_POINTER(1); if (state2 == NULL) + { + /* + * NULL state2 is easy, just return state1, which we know is already + * in the agg_context + */ + if (state1 == NULL) + PG_RETURN_NULL(); PG_RETURN_POINTER(state1); + } if (state1 == NULL) {