diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml
index 07ba05dccd9..11907e2ca02 100644
--- a/doc/src/sgml/catalogs.sgml
+++ b/doc/src/sgml/catalogs.sgml
@@ -6166,6 +6166,16 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
       </para></entry>
      </row>
 
+     <row>
+      <entry role="catalog_table_entry"><para role="column_definition">
+       <structfield>prointernal</structfield> <type>bool</type>
+      </para>
+      <para>
+       Marks if the function is for internal purposes only.  Internal
+       functions cannot be called from SQL directly.
+      </para></entry>
+     </row>
+
      <row>
       <entry role="catalog_table_entry"><para role="column_definition">
        <structfield>pronargs</structfield> <type>int2</type>
diff --git a/src/backend/catalog/pg_aggregate.c b/src/backend/catalog/pg_aggregate.c
index 60a16ffcec7..afed3e7dfd8 100644
--- a/src/backend/catalog/pg_aggregate.c
+++ b/src/backend/catalog/pg_aggregate.c
@@ -889,6 +889,7 @@ lookup_agg_function(List *fnName,
 {
 	Oid			fnOid;
 	bool		retset;
+	bool		prointernal;
 	int			nvargs;
 	Oid			vatype;
 	Oid		   *true_oid_array;
@@ -907,7 +908,7 @@ lookup_agg_function(List *fnName,
 	fdresult = func_get_detail(fnName, NIL, NIL,
 							   nargs, input_types, false, false, false,
 							   &fgc_flags,
-							   &fnOid, rettype, &retset,
+							   &fnOid, rettype, &retset, &prointernal,
 							   &nvargs, &vatype,
 							   &true_oid_array, NULL);
 
diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c
index c4ba284fa18..8ebb63a0fd9 100644
--- a/src/backend/parser/parse_func.c
+++ b/src/backend/parser/parse_func.c
@@ -115,6 +115,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
 	List	   *argdefaults;
 	Node	   *retval;
 	bool		retset;
+	bool		prointernal;
 	int			nvargs;
 	Oid			vatype;
 	FuncDetailCode fdresult;
@@ -271,7 +272,7 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
 							   actual_arg_types,
 							   !func_variadic, true, proc_call,
 							   &fgc_flags,
-							   &funcid, &rettype, &retset,
+							   &funcid, &rettype, &retset, &prointernal,
 							   &nvargs, &vatype,
 							   &declared_arg_types, &argdefaults);
 
@@ -281,6 +282,16 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
 	 * Check for various wrong-kind-of-routine cases.
 	 */
 
+	/* Reject functions that are only meant to be called internally */
+	if (fdresult == FUNCDETAIL_NORMAL && prointernal)
+		ereport(ERROR,
+				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+				 errmsg("function %s cannot be called directly",
+						func_signature_string(funcname, nargs, argnames,
+											  actual_arg_types)),
+				 errdetail("This function is for internal use only."),
+				 parser_errposition(pstate, location)));
+
 	/* If this is a CALL, reject things that aren't procedures */
 	if (proc_call &&
 		(fdresult == FUNCDETAIL_NORMAL ||
@@ -1586,6 +1597,7 @@ func_get_detail(List *funcname,
 				Oid *funcid,	/* return value */
 				Oid *rettype,	/* return value */
 				bool *retset,	/* return value */
+				bool *internal, /* return value */
 				int *nvargs,	/* return value */
 				Oid *vatype,	/* return value */
 				Oid **true_typeids, /* return value */
@@ -1598,6 +1610,7 @@ func_get_detail(List *funcname,
 	*funcid = InvalidOid;
 	*rettype = InvalidOid;
 	*retset = false;
+	*internal = false;
 	*nvargs = 0;
 	*vatype = InvalidOid;
 	*true_typeids = NULL;
@@ -1713,6 +1726,7 @@ func_get_detail(List *funcname,
 					*funcid = InvalidOid;
 					*rettype = targetType;
 					*retset = false;
+					*internal = false;
 					*nvargs = 0;
 					*vatype = InvalidOid;
 					*true_typeids = argtypes;
@@ -1815,6 +1829,7 @@ func_get_detail(List *funcname,
 		pform = (Form_pg_proc) GETSTRUCT(ftup);
 		*rettype = pform->prorettype;
 		*retset = pform->proretset;
+		*internal = pform->prointernal;
 		*vatype = pform->provariadic;
 		/* fetch default args if caller wants 'em */
 		if (argdefaults && best_candidate->ndargs > 0)
diff --git a/src/backend/tcop/fastpath.c b/src/backend/tcop/fastpath.c
index 89300d09cd9..d460bde7908 100644
--- a/src/backend/tcop/fastpath.c
+++ b/src/backend/tcop/fastpath.c
@@ -28,6 +28,7 @@
 #include "libpq/protocol.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "parser/parse_func.h"
 #include "tcop/fastpath.h"
 #include "tcop/tcopprot.h"
 #include "utils/acl.h"
@@ -150,6 +151,16 @@ fetch_fp_info(Oid func_id, struct fp_info *fip)
 				 errmsg("cannot call function \"%s\" via fastpath interface",
 						NameStr(pp->proname))));
 
+	/* reject functions that are only meant to be called internally */
+	if (pp->prointernal)
+		ereport(ERROR,
+				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+				 errmsg("function %s cannot be called directly",
+						func_signature_string(list_make1(makeString(NameStr(pp->proname))),
+											  pp->pronargs, NIL,
+											  pp->proargtypes.values)),
+				 errdetail("This function is for internal use only.")));
+
 	/* watch out for catalog entries with more than FUNC_MAX_ARGS args */
 	if (pp->pronargs > FUNC_MAX_ARGS)
 		elog(ERROR, "function %s has more than %d arguments",
diff --git a/src/backend/utils/adt/array_userfuncs.c b/src/backend/utils/adt/array_userfuncs.c
index ae2cde19c43..76a1d9aafb8 100644
--- a/src/backend/utils/adt/array_userfuncs.c
+++ b/src/backend/utils/adt/array_userfuncs.c
@@ -565,6 +565,7 @@ array_agg_transfn(PG_FUNCTION_ARGS)
 	MemoryContext aggcontext;
 	ArrayBuildState *state;
 	Datum		elem;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
 	if (arg1_typeid == InvalidOid)
 		ereport(ERROR,
@@ -577,11 +578,10 @@ array_agg_transfn(PG_FUNCTION_ARGS)
 	 * while resolving the input/result types of this polymorphic aggregate.
 	 */
 
-	if (!AggCheckCallContext(fcinfo, &aggcontext))
-	{
-		/* cannot be called directly because of internal-type argument */
-		elog(ERROR, "array_agg_transfn called in non-aggregate context");
-	}
+	isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	if (PG_ARGISNULL(0))
 		state = initArrayResult(arg1_typeid, aggcontext, false);
@@ -611,9 +611,12 @@ array_agg_combine(PG_FUNCTION_ARGS)
 	ArrayBuildState *state2;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state1 = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0);
 	state2 = PG_ARGISNULL(1) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(1);
@@ -798,8 +801,8 @@ array_agg_deserialize(PG_FUNCTION_ARGS)
 	int64		nelems;
 	const char *temp;
 
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	sstate = PG_GETARG_BYTEA_PP(0);
 
@@ -940,6 +943,7 @@ array_agg_array_transfn(PG_FUNCTION_ARGS)
 	Oid			arg1_typeid = get_fn_expr_argtype(fcinfo->flinfo, 1);
 	MemoryContext aggcontext;
 	ArrayBuildStateArr *state;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
 	if (arg1_typeid == InvalidOid)
 		ereport(ERROR,
@@ -952,11 +956,10 @@ array_agg_array_transfn(PG_FUNCTION_ARGS)
 	 * resolving the input/result types of this polymorphic aggregate.
 	 */
 
-	if (!AggCheckCallContext(fcinfo, &aggcontext))
-	{
-		/* cannot be called directly because of internal-type argument */
-		elog(ERROR, "array_agg_array_transfn called in non-aggregate context");
-	}
+	isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 
 	if (PG_ARGISNULL(0))
@@ -985,9 +988,12 @@ array_agg_array_combine(PG_FUNCTION_ARGS)
 	ArrayBuildStateArr *state2;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
+
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state1 = PG_ARGISNULL(0) ? NULL : (ArrayBuildStateArr *) PG_GETARG_POINTER(0);
 	state2 = PG_ARGISNULL(1) ? NULL : (ArrayBuildStateArr *) PG_GETARG_POINTER(1);
diff --git a/src/backend/utils/adt/bool.c b/src/backend/utils/adt/bool.c
index 090ad7df9ca..2a611978281 100644
--- a/src/backend/utils/adt/bool.c
+++ b/src/backend/utils/adt/bool.c
@@ -325,9 +325,12 @@ makeBoolAggState(FunctionCallInfo fcinfo)
 {
 	BoolAggState *state;
 	MemoryContext agg_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state = (BoolAggState *) MemoryContextAlloc(agg_context,
 												sizeof(BoolAggState));
diff --git a/src/backend/utils/adt/bytea.c b/src/backend/utils/adt/bytea.c
index 3b1b02a7125..856a6ae0f67 100644
--- a/src/backend/utils/adt/bytea.c
+++ b/src/backend/utils/adt/bytea.c
@@ -409,12 +409,12 @@ bytea_string_agg_transfn(PG_FUNCTION_ARGS)
 		{
 			MemoryContext aggcontext;
 			MemoryContext oldcontext;
+			bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-			if (!AggCheckCallContext(fcinfo, &aggcontext))
-			{
-				/* cannot be called directly because of internal-type argument */
-				elog(ERROR, "bytea_string_agg_transfn called in non-aggregate context");
-			}
+			isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+			/* cannot be called directly because of internal-type argument */
+			Assert(isaggctx);
 
 			/*
 			 * Create state in aggregate context.  It'll stay there across
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 49ce8c1bd6d..a7038525f53 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -3009,6 +3009,9 @@ float8_combine(PG_FUNCTION_ARGS)
 				Sx,
 				Sxx;
 
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
 	transvalues1 = check_float8_array(transarray1, "float8_combine", 3);
 	transvalues2 = check_float8_array(transarray2, "float8_combine", 3);
 
@@ -3055,32 +3058,12 @@ float8_combine(PG_FUNCTION_ARGS)
 			float_overflow_error();
 	}
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we construct a
-	 * new array with the updated transition data and return it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-	{
-		transvalues1[0] = N;
-		transvalues1[1] = Sx;
-		transvalues1[2] = Sxx;
-
-		PG_RETURN_ARRAYTYPE_P(transarray1);
-	}
-	else
-	{
-		Datum		transdatums[3];
-		ArrayType  *result;
-
-		transdatums[0] = Float8GetDatumFast(N);
-		transdatums[1] = Float8GetDatumFast(Sx);
-		transdatums[2] = Float8GetDatumFast(Sxx);
 
-		result = construct_array_builtin(transdatums, 3, FLOAT8OID);
+	transvalues1[0] = N;
+	transvalues1[1] = Sx;
+	transvalues1[2] = Sxx;
 
-		PG_RETURN_ARRAYTYPE_P(result);
-	}
+	PG_RETURN_ARRAYTYPE_P(transarray1);
 }
 
 Datum
@@ -3094,6 +3077,9 @@ float8_accum(PG_FUNCTION_ARGS)
 				Sxx,
 				tmp;
 
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
 	transvalues = check_float8_array(transarray, "float8_accum", 3);
 	N = transvalues[0];
 	Sx = transvalues[1];
@@ -3136,32 +3122,12 @@ float8_accum(PG_FUNCTION_ARGS)
 			Sxx = get_float8_nan();
 	}
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we construct a
-	 * new array with the updated transition data and return it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-	{
-		transvalues[0] = N;
-		transvalues[1] = Sx;
-		transvalues[2] = Sxx;
-
-		PG_RETURN_ARRAYTYPE_P(transarray);
-	}
-	else
-	{
-		Datum		transdatums[3];
-		ArrayType  *result;
-
-		transdatums[0] = Float8GetDatumFast(N);
-		transdatums[1] = Float8GetDatumFast(Sx);
-		transdatums[2] = Float8GetDatumFast(Sxx);
 
-		result = construct_array_builtin(transdatums, 3, FLOAT8OID);
+	transvalues[0] = N;
+	transvalues[1] = Sx;
+	transvalues[2] = Sxx;
 
-		PG_RETURN_ARRAYTYPE_P(result);
-	}
+	PG_RETURN_ARRAYTYPE_P(transarray);
 }
 
 Datum
@@ -3177,6 +3143,9 @@ float4_accum(PG_FUNCTION_ARGS)
 				Sxx,
 				tmp;
 
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
 	transvalues = check_float8_array(transarray, "float4_accum", 3);
 	N = transvalues[0];
 	Sx = transvalues[1];
@@ -3219,32 +3188,12 @@ float4_accum(PG_FUNCTION_ARGS)
 			Sxx = get_float8_nan();
 	}
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we construct a
-	 * new array with the updated transition data and return it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-	{
-		transvalues[0] = N;
-		transvalues[1] = Sx;
-		transvalues[2] = Sxx;
-
-		PG_RETURN_ARRAYTYPE_P(transarray);
-	}
-	else
-	{
-		Datum		transdatums[3];
-		ArrayType  *result;
-
-		transdatums[0] = Float8GetDatumFast(N);
-		transdatums[1] = Float8GetDatumFast(Sx);
-		transdatums[2] = Float8GetDatumFast(Sxx);
 
-		result = construct_array_builtin(transdatums, 3, FLOAT8OID);
+	transvalues[0] = N;
+	transvalues[1] = Sx;
+	transvalues[2] = Sxx;
 
-		PG_RETURN_ARRAYTYPE_P(result);
-	}
+	PG_RETURN_ARRAYTYPE_P(transarray);
 }
 
 Datum
@@ -3407,6 +3356,9 @@ float8_regr_accum(PG_FUNCTION_ARGS)
 				tmpY,
 				scale;
 
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
 	transvalues = check_float8_array(transarray, "float8_regr_accum", 8);
 	N = transvalues[0];
 	Sx = transvalues[1];
@@ -3507,42 +3459,17 @@ float8_regr_accum(PG_FUNCTION_ARGS)
 		commonY = newvalY;
 	}
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we construct a
-	 * new array with the updated transition data and return it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-	{
-		transvalues[0] = N;
-		transvalues[1] = Sx;
-		transvalues[2] = Sxx;
-		transvalues[3] = Sy;
-		transvalues[4] = Syy;
-		transvalues[5] = Sxy;
-		transvalues[6] = commonX;
-		transvalues[7] = commonY;
-
-		PG_RETURN_ARRAYTYPE_P(transarray);
-	}
-	else
-	{
-		Datum		transdatums[8];
-		ArrayType  *result;
-
-		transdatums[0] = Float8GetDatumFast(N);
-		transdatums[1] = Float8GetDatumFast(Sx);
-		transdatums[2] = Float8GetDatumFast(Sxx);
-		transdatums[3] = Float8GetDatumFast(Sy);
-		transdatums[4] = Float8GetDatumFast(Syy);
-		transdatums[5] = Float8GetDatumFast(Sxy);
-		transdatums[6] = Float8GetDatumFast(commonX);
-		transdatums[7] = Float8GetDatumFast(commonY);
 
-		result = construct_array_builtin(transdatums, 8, FLOAT8OID);
+	transvalues[0] = N;
+	transvalues[1] = Sx;
+	transvalues[2] = Sxx;
+	transvalues[3] = Sy;
+	transvalues[4] = Syy;
+	transvalues[5] = Sxy;
+	transvalues[6] = commonX;
+	transvalues[7] = commonY;
 
-		PG_RETURN_ARRAYTYPE_P(result);
-	}
+	PG_RETURN_ARRAYTYPE_P(transarray);
 }
 
 /*
@@ -3587,6 +3514,9 @@ float8_regr_combine(PG_FUNCTION_ARGS)
 				Cx,
 				Cy;
 
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
 	transvalues1 = check_float8_array(transarray1, "float8_regr_combine", 8);
 	transvalues2 = check_float8_array(transarray2, "float8_regr_combine", 8);
 
@@ -3719,42 +3649,17 @@ float8_regr_combine(PG_FUNCTION_ARGS)
 		}
 	}
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we construct a
-	 * new array with the updated transition data and return it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-	{
-		transvalues1[0] = N;
-		transvalues1[1] = Sx;
-		transvalues1[2] = Sxx;
-		transvalues1[3] = Sy;
-		transvalues1[4] = Syy;
-		transvalues1[5] = Sxy;
-		transvalues1[6] = Cx;
-		transvalues1[7] = Cy;
-
-		PG_RETURN_ARRAYTYPE_P(transarray1);
-	}
-	else
-	{
-		Datum		transdatums[8];
-		ArrayType  *result;
-
-		transdatums[0] = Float8GetDatumFast(N);
-		transdatums[1] = Float8GetDatumFast(Sx);
-		transdatums[2] = Float8GetDatumFast(Sxx);
-		transdatums[3] = Float8GetDatumFast(Sy);
-		transdatums[4] = Float8GetDatumFast(Syy);
-		transdatums[5] = Float8GetDatumFast(Sxy);
-		transdatums[6] = Float8GetDatumFast(Cx);
-		transdatums[7] = Float8GetDatumFast(Cy);
 
-		result = construct_array_builtin(transdatums, 8, FLOAT8OID);
+	transvalues1[0] = N;
+	transvalues1[1] = Sx;
+	transvalues1[2] = Sxx;
+	transvalues1[3] = Sy;
+	transvalues1[4] = Syy;
+	transvalues1[5] = Sxy;
+	transvalues1[6] = Cx;
+	transvalues1[7] = Cy;
 
-		PG_RETURN_ARRAYTYPE_P(result);
-	}
+	PG_RETURN_ARRAYTYPE_P(transarray1);
 }
 
 
diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 1e273160b25..645ee54362d 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -793,12 +793,12 @@ json_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null)
 				oldcontext;
 	JsonAggState *state;
 	Datum		val;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &aggcontext))
-	{
-		/* cannot be called directly because of internal-type argument */
-		elog(ERROR, "json_agg_transfn called in non-aggregate context");
-	}
+	isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	if (PG_ARGISNULL(0))
 	{
@@ -1020,12 +1020,12 @@ json_object_agg_transfn_worker(FunctionCallInfo fcinfo,
 	Datum		arg;
 	bool		skip;
 	int			key_offset;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &aggcontext))
-	{
-		/* cannot be called directly because of internal-type argument */
-		elog(ERROR, "json_object_agg_transfn called in non-aggregate context");
-	}
+	isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	if (PG_ARGISNULL(0))
 	{
diff --git a/src/backend/utils/adt/jsonb.c b/src/backend/utils/adt/jsonb.c
index 8a22d177414..b9dfa982e57 100644
--- a/src/backend/utils/adt/jsonb.c
+++ b/src/backend/utils/adt/jsonb.c
@@ -1519,12 +1519,12 @@ jsonb_agg_transfn_worker(FunctionCallInfo fcinfo, bool absent_on_null)
 	JsonbAggState *state;
 	Datum		val;
 	JsonbInState *result;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &aggcontext))
-	{
-		/* cannot be called directly because of internal-type argument */
-		elog(ERROR, "jsonb_agg_transfn called in non-aggregate context");
-	}
+	isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	/* set up the accumulator on the first go round */
 
@@ -1628,12 +1628,12 @@ jsonb_object_agg_transfn_worker(FunctionCallInfo fcinfo,
 	Datum		val;
 	JsonbInState *result;
 	bool		skip;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &aggcontext))
-	{
-		/* cannot be called directly because of internal-type argument */
-		elog(ERROR, "jsonb_object_agg_transfn called in non-aggregate context");
-	}
+	isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	/* set up the accumulator on the first go round */
 
diff --git a/src/backend/utils/adt/multirangetypes.c b/src/backend/utils/adt/multirangetypes.c
index f5114bd791d..010a1b9a738 100644
--- a/src/backend/utils/adt/multirangetypes.c
+++ b/src/backend/utils/adt/multirangetypes.c
@@ -1425,9 +1425,12 @@ range_agg_transfn(PG_FUNCTION_ARGS)
 	MemoryContext aggContext;
 	Oid			rngtypoid;
 	ArrayBuildState *state;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &aggContext))
-		elog(ERROR, "range_agg_transfn called in non-aggregate context");
+	isaggctx = AggCheckCallContext(fcinfo, &aggContext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	rngtypoid = get_fn_expr_argtype(fcinfo->flinfo, 1);
 	if (!type_is_range(rngtypoid))
@@ -1454,7 +1457,6 @@ range_agg_transfn(PG_FUNCTION_ARGS)
 Datum
 range_agg_finalfn(PG_FUNCTION_ARGS)
 {
-	MemoryContext aggContext;
 	Oid			mltrngtypoid;
 	TypeCacheEntry *typcache;
 	ArrayBuildState *state;
@@ -1462,8 +1464,8 @@ range_agg_finalfn(PG_FUNCTION_ARGS)
 	RangeType **ranges;
 	int			i;
 
-	if (!AggCheckCallContext(fcinfo, &aggContext))
-		elog(ERROR, "range_agg_finalfn called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	state = PG_ARGISNULL(0) ? NULL : (ArrayBuildState *) PG_GETARG_POINTER(0);
 	if (state == NULL)
@@ -1499,9 +1501,12 @@ multirange_agg_transfn(PG_FUNCTION_ARGS)
 	TypeCacheEntry *typcache;
 	TypeCacheEntry *rngtypcache;
 	ArrayBuildState *state;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &aggContext))
-		elog(ERROR, "multirange_agg_transfn called in non-aggregate context");
+	isaggctx = AggCheckCallContext(fcinfo, &aggContext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	mltrngtypoid = get_fn_expr_argtype(fcinfo->flinfo, 1);
 	if (!type_is_multirange(mltrngtypoid))
@@ -1547,7 +1552,6 @@ multirange_agg_transfn(PG_FUNCTION_ARGS)
 Datum
 multirange_intersect_agg_transfn(PG_FUNCTION_ARGS)
 {
-	MemoryContext aggContext;
 	Oid			mltrngtypoid;
 	TypeCacheEntry *typcache;
 	MultirangeType *result;
@@ -1557,8 +1561,8 @@ multirange_intersect_agg_transfn(PG_FUNCTION_ARGS)
 	RangeType **ranges1;
 	RangeType **ranges2;
 
-	if (!AggCheckCallContext(fcinfo, &aggContext))
-		elog(ERROR, "multirange_intersect_agg_transfn called in non-aggregate context");
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	mltrngtypoid = get_fn_expr_argtype(fcinfo->flinfo, 1);
 	if (!type_is_multirange(mltrngtypoid))
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index ff6dec42010..159f9db6482 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -4790,9 +4790,12 @@ makeNumericAggState(FunctionCallInfo fcinfo, bool calcSumX2)
 	NumericAggState *state;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	old_context = MemoryContextSwitchTo(agg_context);
 
@@ -5014,9 +5017,12 @@ numeric_combine(PG_FUNCTION_ARGS)
 	NumericAggState *state2;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
+
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state1 = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
 	state2 = PG_ARGISNULL(1) ? NULL : (NumericAggState *) PG_GETARG_POINTER(1);
@@ -5114,9 +5120,12 @@ numeric_avg_combine(PG_FUNCTION_ARGS)
 	NumericAggState *state2;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
+
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state1 = PG_ARGISNULL(0) ? NULL : (NumericAggState *) PG_GETARG_POINTER(0);
 	state2 = PG_ARGISNULL(1) ? NULL : (NumericAggState *) PG_GETARG_POINTER(1);
@@ -5195,9 +5204,8 @@ numeric_avg_serialize(PG_FUNCTION_ARGS)
 	bytea	   *result;
 	NumericVar	tmp_var;
 
-	/* Ensure we disallow calling when not in aggregate context */
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	state = (NumericAggState *) PG_GETARG_POINTER(0);
 
@@ -5247,8 +5255,8 @@ numeric_avg_deserialize(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 	NumericVar	tmp_var;
 
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	sstate = PG_GETARG_BYTEA_PP(0);
 
@@ -5305,9 +5313,8 @@ numeric_serialize(PG_FUNCTION_ARGS)
 	bytea	   *result;
 	NumericVar	tmp_var;
 
-	/* Ensure we disallow calling when not in aggregate context */
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	state = (NumericAggState *) PG_GETARG_POINTER(0);
 
@@ -5361,8 +5368,8 @@ numeric_deserialize(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 	NumericVar	tmp_var;
 
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	sstate = PG_GETARG_BYTEA_PP(0);
 
@@ -5470,9 +5477,12 @@ makeInt128AggState(FunctionCallInfo fcinfo, bool calcSumX2)
 	Int128AggState *state;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	old_context = MemoryContextSwitchTo(agg_context);
 
@@ -5586,9 +5596,12 @@ numeric_poly_combine(PG_FUNCTION_ARGS)
 	Int128AggState *state2;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
+
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state1 = PG_ARGISNULL(0) ? NULL : (Int128AggState *) PG_GETARG_POINTER(0);
 	state2 = PG_ARGISNULL(1) ? NULL : (Int128AggState *) PG_GETARG_POINTER(1);
@@ -5662,9 +5675,8 @@ numeric_poly_serialize(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 	bytea	   *result;
 
-	/* Ensure we disallow calling when not in aggregate context */
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	state = (Int128AggState *) PG_GETARG_POINTER(0);
 
@@ -5696,8 +5708,8 @@ numeric_poly_deserialize(PG_FUNCTION_ARGS)
 	Int128AggState *result;
 	StringInfoData buf;
 
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	sstate = PG_GETARG_BYTEA_PP(0);
 
@@ -5755,9 +5767,12 @@ int8_avg_combine(PG_FUNCTION_ARGS)
 	Int128AggState *state2;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
+
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state1 = PG_ARGISNULL(0) ? NULL : (Int128AggState *) PG_GETARG_POINTER(0);
 	state2 = PG_ARGISNULL(1) ? NULL : (Int128AggState *) PG_GETARG_POINTER(1);
@@ -5807,9 +5822,8 @@ int8_avg_serialize(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 	bytea	   *result;
 
-	/* Ensure we disallow calling when not in aggregate context */
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	state = (Int128AggState *) PG_GETARG_POINTER(0);
 
@@ -5838,8 +5852,8 @@ int8_avg_deserialize(PG_FUNCTION_ARGS)
 	Int128AggState *result;
 	StringInfoData buf;
 
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	sstate = PG_GETARG_BYTEA_PP(0);
 
@@ -6464,15 +6478,11 @@ int2_avg_accum(PG_FUNCTION_ARGS)
 	Int8TransTypeData *transdata;
 	int64		newsum;
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we need to make
-	 * a copy of it before scribbling on it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-		transarray = PG_GETARG_ARRAYTYPE_P(0);
-	else
-		transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
+
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
+	transarray = PG_GETARG_ARRAYTYPE_P(0);
 
 	if (ARR_HASNULL(transarray) ||
 		ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
@@ -6499,15 +6509,10 @@ int4_avg_accum(PG_FUNCTION_ARGS)
 	Int8TransTypeData *transdata;
 	int64		newsum;
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we need to make
-	 * a copy of it before scribbling on it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-		transarray = PG_GETARG_ARRAYTYPE_P(0);
-	else
-		transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
+	transarray = PG_GETARG_ARRAYTYPE_P(0);
 
 	if (ARR_HASNULL(transarray) ||
 		ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
@@ -6535,8 +6540,8 @@ int4_avg_combine(PG_FUNCTION_ARGS)
 	Int8TransTypeData *state2;
 	int64		newsum;
 
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	transarray1 = PG_GETARG_ARRAYTYPE_P(0);
 	transarray2 = PG_GETARG_ARRAYTYPE_P(1);
@@ -6570,15 +6575,10 @@ int2_avg_accum_inv(PG_FUNCTION_ARGS)
 	int16		newval = PG_GETARG_INT16(1);
 	Int8TransTypeData *transdata;
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we need to make
-	 * a copy of it before scribbling on it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-		transarray = PG_GETARG_ARRAYTYPE_P(0);
-	else
-		transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
+	transarray = PG_GETARG_ARRAYTYPE_P(0);
 
 	if (ARR_HASNULL(transarray) ||
 		ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
@@ -6598,15 +6598,10 @@ int4_avg_accum_inv(PG_FUNCTION_ARGS)
 	int32		newval = PG_GETARG_INT32(1);
 	Int8TransTypeData *transdata;
 
-	/*
-	 * If we're invoked as an aggregate, we can cheat and modify our first
-	 * parameter in-place to reduce palloc overhead. Otherwise we need to make
-	 * a copy of it before scribbling on it.
-	 */
-	if (AggCheckCallContext(fcinfo, NULL))
-		transarray = PG_GETARG_ARRAYTYPE_P(0);
-	else
-		transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
+
+	transarray = PG_GETARG_ARRAYTYPE_P(0);
 
 	if (ARR_HASNULL(transarray) ||
 		ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
@@ -6627,6 +6622,7 @@ int8_avg(PG_FUNCTION_ARGS)
 	Datum		countd,
 				sumd;
 
+	/* XXX consider changing this ERROR to an Assert as this is prointernal */
 	if (ARR_HASNULL(transarray) ||
 		ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
 		elog(ERROR, "expected 2-element int8 array");
@@ -6652,6 +6648,7 @@ int2int4_sum(PG_FUNCTION_ARGS)
 	ArrayType  *transarray = PG_GETARG_ARRAYTYPE_P(0);
 	Int8TransTypeData *transdata;
 
+	/* XXX consider changing this ERROR to an Assert as this is prointernal */
 	if (ARR_HASNULL(transarray) ||
 		ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
 		elog(ERROR, "expected 2-element int8 array");
diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c
index 86ce90ba569..b5df3a183a7 100644
--- a/src/backend/utils/adt/rangetypes.c
+++ b/src/backend/utils/adt/rangetypes.c
@@ -1392,14 +1392,13 @@ range_minus_multi_internal(TypeCacheEntry *typcache, RangeType *r1,
 Datum
 range_intersect_agg_transfn(PG_FUNCTION_ARGS)
 {
-	MemoryContext aggContext;
 	Oid			rngtypoid;
 	TypeCacheEntry *typcache;
 	RangeType  *result;
 	RangeType  *current;
 
-	if (!AggCheckCallContext(fcinfo, &aggContext))
-		elog(ERROR, "range_intersect_agg_transfn called in non-aggregate context");
+	/* cannot be called directly because of prointernal == true */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	rngtypoid = get_fn_expr_argtype(fcinfo->flinfo, 1);
 	if (!type_is_range(rngtypoid))
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index e9c1c9db1b1..a1beba3a429 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -13551,6 +13551,7 @@ generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes,
 	Oid			p_funcid;
 	Oid			p_rettype;
 	bool		p_retset;
+	bool		p_internal;
 	int			p_nvargs;
 	Oid			p_vatype;
 	Oid		   *p_true_typeids;
@@ -13608,7 +13609,7 @@ generate_function_name(Oid funcid, int nargs, List *argnames, Oid *argtypes,
 								   !use_variadic, true, false,
 								   &fgc_flags,
 								   &p_funcid, &p_rettype,
-								   &p_retset, &p_nvargs, &p_vatype,
+								   &p_retset, &p_internal, &p_nvargs, &p_vatype,
 								   &p_true_typeids, NULL);
 	else
 	{
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 43efa548452..e0c2df6dc69 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -3994,9 +3994,12 @@ makeIntervalAggState(FunctionCallInfo fcinfo)
 	IntervalAggState *state;
 	MemoryContext agg_context;
 	MemoryContext old_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	old_context = MemoryContextSwitchTo(agg_context);
 
@@ -4145,9 +4148,8 @@ interval_avg_serialize(PG_FUNCTION_ARGS)
 	StringInfoData buf;
 	bytea	   *result;
 
-	/* Ensure we disallow calling when not in aggregate context */
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	state = (IntervalAggState *) PG_GETARG_POINTER(0);
 
@@ -4183,8 +4185,8 @@ interval_avg_deserialize(PG_FUNCTION_ARGS)
 	IntervalAggState *result;
 	StringInfoData buf;
 
-	if (!AggCheckCallContext(fcinfo, NULL))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(AggCheckCallContext(fcinfo, NULL));
 
 	sstate = PG_GETARG_BYTEA_PP(0);
 
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 6c95fcb24b7..f993cb09dbd 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -4305,12 +4305,12 @@ makeStringAggState(FunctionCallInfo fcinfo)
 	StringInfo	state;
 	MemoryContext aggcontext;
 	MemoryContext oldcontext;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
 
-	if (!AggCheckCallContext(fcinfo, &aggcontext))
-	{
-		/* cannot be called directly because of internal-type argument */
-		elog(ERROR, "string_agg_transfn called in non-aggregate context");
-	}
+	isaggctx = AggCheckCallContext(fcinfo, &aggcontext);
+
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	/*
 	 * Create state in aggregate context.  It'll stay there across subsequent
@@ -4384,9 +4384,12 @@ string_agg_combine(PG_FUNCTION_ARGS)
 	StringInfo	state1;
 	StringInfo	state2;
 	MemoryContext agg_context;
+	bool		isaggctx PG_USED_FOR_ASSERTS_ONLY;
+
+	isaggctx = AggCheckCallContext(fcinfo, &agg_context);
 
-	if (!AggCheckCallContext(fcinfo, &agg_context))
-		elog(ERROR, "aggregate function called in non-aggregate context");
+	/* cannot be called directly because of internal-type argument */
+	Assert(isaggctx);
 
 	state1 = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0);
 	state2 = PG_ARGISNULL(1) ? NULL : (StringInfo) PG_GETARG_POINTER(1);
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 50fb182cb79..f881b4ae6dc 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -608,7 +608,7 @@
   proname => 'float4abs', prorettype => 'float4', proargtypes => 'float4',
   prosrc => 'float4abs' },
 { oid => '208', descr => 'aggregate transition function',
-  proname => 'float4_accum', prorettype => '_float8',
+  proname => 'float4_accum', prointernal => 't', prorettype => '_float8',
   proargtypes => '_float8 float4', prosrc => 'float4_accum' },
 { oid => '209', descr => 'larger of two',
   proname => 'float4larger', prorettype => 'float4',
@@ -649,10 +649,10 @@
   proname => 'float8abs', prorettype => 'float8', proargtypes => 'float8',
   prosrc => 'float8abs' },
 { oid => '222', descr => 'aggregate transition function',
-  proname => 'float8_accum', prorettype => '_float8',
+  proname => 'float8_accum', prointernal => 't', prorettype => '_float8',
   proargtypes => '_float8 float8', prosrc => 'float8_accum' },
 { oid => '276', descr => 'aggregate combine function',
-  proname => 'float8_combine', prorettype => '_float8',
+  proname => 'float8_combine', prointernal => 't', prorettype => '_float8',
   proargtypes => '_float8 _float8', prosrc => 'float8_combine' },
 { oid => '223', descr => 'larger of two',
   proname => 'float8larger', prorettype => 'float8',
@@ -4979,19 +4979,19 @@
 
 # Aggregate-related functions
 { oid => '1830', descr => 'aggregate final function',
-  proname => 'float8_avg', prorettype => 'float8', proargtypes => '_float8',
-  prosrc => 'float8_avg' },
+  proname => 'float8_avg', prointernal => 't', prorettype => 'float8',
+  proargtypes => '_float8', prosrc => 'float8_avg' },
 { oid => '2512', descr => 'aggregate final function',
-  proname => 'float8_var_pop', prorettype => 'float8', proargtypes => '_float8',
-  prosrc => 'float8_var_pop' },
+  proname => 'float8_var_pop', prointernal => 't', prorettype => 'float8',
+  proargtypes => '_float8', prosrc => 'float8_var_pop' },
 { oid => '1831', descr => 'aggregate final function',
-  proname => 'float8_var_samp', prorettype => 'float8',
+  proname => 'float8_var_samp', prointernal => 't', prorettype => 'float8',
   proargtypes => '_float8', prosrc => 'float8_var_samp' },
 { oid => '2513', descr => 'aggregate final function',
-  proname => 'float8_stddev_pop', prorettype => 'float8',
+  proname => 'float8_stddev_pop', prointernal => 't', prorettype => 'float8',
   proargtypes => '_float8', prosrc => 'float8_stddev_pop' },
 { oid => '1832', descr => 'aggregate final function',
-  proname => 'float8_stddev_samp', prorettype => 'float8',
+  proname => 'float8_stddev_samp', prointernal => 't', prorettype => 'float8',
   proargtypes => '_float8', prosrc => 'float8_stddev_samp' },
 { oid => '1833', descr => 'aggregate transition function',
   proname => 'numeric_accum', proisstrict => 'f', prorettype => 'internal',
@@ -5065,7 +5065,7 @@
   proname => 'int8_avg_deserialize', prorettype => 'internal',
   proargtypes => 'bytea internal', prosrc => 'int8_avg_deserialize' },
 { oid => '3324', descr => 'aggregate combine function',
-  proname => 'int4_avg_combine', prorettype => '_int8',
+  proname => 'int4_avg_combine', prointernal => 't', prorettype => '_int8',
   proargtypes => '_int8 _int8', prosrc => 'int4_avg_combine' },
 { oid => '3178', descr => 'aggregate final function',
   proname => 'numeric_sum', proisstrict => 'f', prorettype => 'numeric',
@@ -5141,31 +5141,31 @@
   proname => 'interval_sum', proisstrict => 'f', prorettype => 'interval',
   proargtypes => 'internal', prosrc => 'interval_sum' },
 { oid => '1962', descr => 'aggregate transition function',
-  proname => 'int2_avg_accum', prorettype => '_int8',
+  proname => 'int2_avg_accum', prointernal => 't', prorettype => '_int8',
   proargtypes => '_int8 int2', prosrc => 'int2_avg_accum' },
 { oid => '1963', descr => 'aggregate transition function',
-  proname => 'int4_avg_accum', prorettype => '_int8',
+  proname => 'int4_avg_accum', prointernal => 't', prorettype => '_int8',
   proargtypes => '_int8 int4', prosrc => 'int4_avg_accum' },
 { oid => '3570', descr => 'aggregate transition function',
-  proname => 'int2_avg_accum_inv', prorettype => '_int8',
+  proname => 'int2_avg_accum_inv', prointernal => 't', prorettype => '_int8',
   proargtypes => '_int8 int2', prosrc => 'int2_avg_accum_inv' },
 { oid => '3571', descr => 'aggregate transition function',
-  proname => 'int4_avg_accum_inv', prorettype => '_int8',
+  proname => 'int4_avg_accum_inv', prointernal => 't', prorettype => '_int8',
   proargtypes => '_int8 int4', prosrc => 'int4_avg_accum_inv' },
 { oid => '1964', descr => 'aggregate final function',
-  proname => 'int8_avg', prorettype => 'numeric', proargtypes => '_int8',
-  prosrc => 'int8_avg' },
+  proname => 'int8_avg', prointernal => 't', prorettype => 'numeric',
+  proargtypes => '_int8', prosrc => 'int8_avg' },
 { oid => '3572', descr => 'aggregate final function',
-  proname => 'int2int4_sum', prorettype => 'int8', proargtypes => '_int8',
-  prosrc => 'int2int4_sum' },
+  proname => 'int2int4_sum', prointernal => 't', prorettype => 'int8',
+  proargtypes => '_int8', prosrc => 'int2int4_sum' },
 { oid => '2805', descr => 'aggregate transition function',
   proname => 'int8inc_float8_float8', prorettype => 'int8',
   proargtypes => 'int8 float8 float8', prosrc => 'int8inc_float8_float8' },
 { oid => '2806', descr => 'aggregate transition function',
-  proname => 'float8_regr_accum', prorettype => '_float8',
+  proname => 'float8_regr_accum', prointernal => 't', prorettype => '_float8',
   proargtypes => '_float8 float8 float8', prosrc => 'float8_regr_accum' },
 { oid => '3342', descr => 'aggregate combine function',
-  proname => 'float8_regr_combine', prorettype => '_float8',
+  proname => 'float8_regr_combine', prointernal => 't', prorettype => '_float8',
   proargtypes => '_float8 _float8', prosrc => 'float8_regr_combine' },
 { oid => '2807', descr => 'aggregate final function',
   proname => 'float8_regr_sxx', prorettype => 'float8',
@@ -11212,7 +11212,7 @@
   proname => 'rangesel', provolatile => 's', prorettype => 'float8',
   proargtypes => 'internal oid internal int4', prosrc => 'rangesel' },
 { oid => '4401', descr => 'range aggregate by intersecting',
-  proname => 'range_intersect_agg_transfn', prorettype => 'anyrange',
+  proname => 'range_intersect_agg_transfn', prointernal => 't', prorettype => 'anyrange',
   proargtypes => 'anyrange anyrange', prosrc => 'range_intersect_agg_transfn' },
 { oid => '4450', descr => 'range aggregate by intersecting',
   proname => 'range_intersect_agg', prokind => 'a', proisstrict => 'f',
@@ -11571,8 +11571,8 @@
   prorettype => 'anymultirange', proargtypes => 'anymultirange',
   prosrc => 'aggregate_dummy' },
 { oid => '4388', descr => 'range aggregate by intersecting',
-  proname => 'multirange_intersect_agg_transfn', prorettype => 'anymultirange',
-  proargtypes => 'anymultirange anymultirange',
+  proname => 'multirange_intersect_agg_transfn', prointernal => 't',
+  prorettype => 'anymultirange', proargtypes => 'anymultirange anymultirange',
   prosrc => 'multirange_intersect_agg_transfn' },
 { oid => '4389', descr => 'range aggregate by intersecting',
   proname => 'range_intersect_agg', prokind => 'a', proisstrict => 'f',
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index 48010e93313..117708d79d4 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -78,6 +78,14 @@ CATALOG(pg_proc,1255,ProcedureRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(81,Proce
 	/* see PROPARALLEL_ categories below */
 	char		proparallel BKI_DEFAULT(s);
 
+	/*
+	 * true means the function cannot be called directly via SQL.  This is not
+	 * expected to be watertight.  It's intended to stop users from shooting
+	 * themselves in the foot by using things like aggregate transfns directly
+	 * in ways they're not designed for.
+	 */
+	bool		prointernal BKI_DEFAULT(f);
+
 	/* number of arguments */
 	/* Note: need not be given in pg_proc.dat; genbki.pl will compute it */
 	int16		pronargs;
diff --git a/src/include/parser/parse_func.h b/src/include/parser/parse_func.h
index cbff17abee0..bcd11c5ae2e 100644
--- a/src/include/parser/parse_func.h
+++ b/src/include/parser/parse_func.h
@@ -42,7 +42,8 @@ extern FuncDetailCode func_get_detail(List *funcname,
 									  bool include_out_arguments,
 									  int *fgc_flags,
 									  Oid *funcid, Oid *rettype,
-									  bool *retset, int *nvargs, Oid *vatype,
+									  bool *retset, bool *internal,
+									  int *nvargs, Oid *vatype,
 									  Oid **true_typeids, List **argdefaults);
 
 extern int	func_match_argtypes(int nargs,
