From: Tom Lane Date: Wed, 8 Jan 2014 19:33:52 +0000 (-0500) Subject: Avoid extra AggCheckCallContext() checks in ordered-set aggregates. X-Git-Tag: REL9_4_BETA1~721 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=847e46abc92333a5a948d8fa886604832c1db238;p=postgresql Avoid extra AggCheckCallContext() checks in ordered-set aggregates. In the transition functions, we don't really need to recheck this after the first call. I had been feeling paranoid about possibly getting a non-null argument value in some other context; but it's probably game over anyway if we have a non-null "internal" value that's not what we are expecting. In the final functions, the general convention in pre-existing final functions seems to be that an Assert() is good enough, so do it like that here too. This seems to save a few tenths of a percent of overall query runtime, which isn't much, but still it's just overhead if there's not a plausible case where the checks would fire. --- diff --git a/src/backend/utils/adt/orderedsetaggs.c b/src/backend/utils/adt/orderedsetaggs.c index d98f5a807c..216c498a2f 100644 --- a/src/backend/utils/adt/orderedsetaggs.c +++ b/src/backend/utils/adt/orderedsetaggs.c @@ -122,8 +122,8 @@ ordered_set_startup(FunctionCallInfo fcinfo, bool use_tuples) int numSortCols; /* - * Check we're called as aggregate, and get the Agg node's - * group-lifespan context + * Check we're called as aggregate (and not a window function), and + * get the Agg node's group-lifespan context */ if (AggCheckCallContext(fcinfo, &gcontext) != AGG_CONTEXT_AGGREGATE) elog(ERROR, "ordered-set aggregate called in non-aggregate context"); @@ -356,12 +356,7 @@ ordered_set_transition(PG_FUNCTION_ARGS) if (PG_ARGISNULL(0)) osastate = ordered_set_startup(fcinfo, false); else - { - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0); - } /* Load the datum into the tuplesort object, but only if it's not null */ if (!PG_ARGISNULL(1)) @@ -389,12 +384,7 @@ ordered_set_transition_multi(PG_FUNCTION_ARGS) if (PG_ARGISNULL(0)) osastate = ordered_set_startup(fcinfo, true); else - { - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); osastate = (OSAPerGroupState *) PG_GETARG_POINTER(0); - } /* Form a tuple from all the other inputs besides the transition value */ slot = osastate->qstate->tupslot; @@ -435,9 +425,7 @@ percentile_disc_final(PG_FUNCTION_ARGS) bool isnull; int64 rownum; - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); + Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE); /* Get and check the percentile argument */ if (PG_ARGISNULL(1)) @@ -542,9 +530,7 @@ percentile_cont_final_common(FunctionCallInfo fcinfo, double proportion; bool isnull; - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); + Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE); /* Get and check the percentile argument */ if (PG_ARGISNULL(1)) @@ -752,9 +738,7 @@ percentile_disc_multi_final(PG_FUNCTION_ARGS) bool isnull = true; int i; - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); + Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE); /* If there were no regular rows, the result is NULL */ if (PG_ARGISNULL(0)) @@ -875,9 +859,7 @@ percentile_cont_multi_final_common(FunctionCallInfo fcinfo, bool isnull; int i; - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); + Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE); /* If there were no regular rows, the result is NULL */ if (PG_ARGISNULL(0)) @@ -1045,9 +1027,7 @@ mode_final(PG_FUNCTION_ARGS) FmgrInfo *equalfn; bool shouldfree; - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); + Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE); /* If there were no regular rows, the result is NULL */ if (PG_ARGISNULL(0)) @@ -1173,9 +1153,7 @@ hypothetical_rank_common(FunctionCallInfo fcinfo, int flag, TupleTableSlot *slot; int i; - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); + Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE); /* If there were no regular rows, the rank is always 1 */ if (PG_ARGISNULL(0)) @@ -1305,9 +1283,7 @@ hypothetical_dense_rank_final(PG_FUNCTION_ARGS) MemoryContext tmpcontext; int i; - /* safety check */ - if (AggCheckCallContext(fcinfo, NULL) != AGG_CONTEXT_AGGREGATE) - elog(ERROR, "ordered-set aggregate called in non-aggregate context"); + Assert(AggCheckCallContext(fcinfo, NULL) == AGG_CONTEXT_AGGREGATE); /* If there were no regular rows, the rank is always 1 */ if (PG_ARGISNULL(0))