summaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/array_userfuncs.c
diff options
context:
space:
mode:
authorPavan Deolasee2017-06-14 05:42:18 +0000
committerPavan Deolasee2017-06-14 05:42:18 +0000
commit15dd5274c323fb93e4e3ea9ad2185aaaec10f79c (patch)
tree9dafb4c7f735d9429ea461dc792933af87493c33 /src/backend/utils/adt/array_userfuncs.c
parentdfbb88e3bbb526dcb204b456b9e5cfd9d10d0d0a (diff)
parentd5cb3bab564e0927ffac7c8729eacf181a12dd40 (diff)
Merge from PG master upto d5cb3bab564e0927ffac7c8729eacf181a12dd40
This is the result of the "git merge remotes/PGSQL/master" upto the said commit point. We have done some basic analysis, fixed compilation problems etc, but bulk of the logical problems in conflict resolution etc will be handled by subsequent commits.
Diffstat (limited to 'src/backend/utils/adt/array_userfuncs.c')
-rw-r--r--src/backend/utils/adt/array_userfuncs.c92
1 files changed, 18 insertions, 74 deletions
diff --git a/src/backend/utils/adt/array_userfuncs.c b/src/backend/utils/adt/array_userfuncs.c
index c44e227439..8311c7ef11 100644
--- a/src/backend/utils/adt/array_userfuncs.c
+++ b/src/backend/utils/adt/array_userfuncs.c
@@ -3,7 +3,7 @@
* array_userfuncs.c
* Misc user-visible array support functions
*
- * Copyright (c) 2003-2016, PostgreSQL Global Development Group
+ * Copyright (c) 2003-2017, PostgreSQL Global Development Group
*
* IDENTIFICATION
* src/backend/utils/adt/array_userfuncs.c
@@ -32,6 +32,10 @@ static Datum array_position_common(FunctionCallInfo fcinfo);
* Caution: if the input is a read/write pointer, this returns the input
* argument; so callers must be sure that their changes are "safe", that is
* they cannot leave the array in a corrupt state.
+ *
+ * If we're being called as an aggregate function, make sure any newly-made
+ * expanded array is allocated in the aggregate state context, so as to save
+ * copying operations.
*/
static ExpandedArrayHeader *
fetch_array_arg_replace_nulls(FunctionCallInfo fcinfo, int argno)
@@ -39,6 +43,7 @@ fetch_array_arg_replace_nulls(FunctionCallInfo fcinfo, int argno)
ExpandedArrayHeader *eah;
Oid element_type;
ArrayMetaState *my_extra;
+ MemoryContext resultcxt;
/* If first time through, create datatype cache struct */
my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
@@ -51,10 +56,17 @@ fetch_array_arg_replace_nulls(FunctionCallInfo fcinfo, int argno)
fcinfo->flinfo->fn_extra = my_extra;
}
+ /* Figure out which context we want the result in */
+ if (!AggCheckCallContext(fcinfo, &resultcxt))
+ resultcxt = CurrentMemoryContext;
+
/* Now collect the array value */
if (!PG_ARGISNULL(argno))
{
+ MemoryContext oldcxt = MemoryContextSwitchTo(resultcxt);
+
eah = PG_GETARG_EXPANDED_ARRAYX(argno, my_extra);
+ MemoryContextSwitchTo(oldcxt);
}
else
{
@@ -72,7 +84,7 @@ fetch_array_arg_replace_nulls(FunctionCallInfo fcinfo, int argno)
errmsg("input data type is not an array")));
eah = construct_empty_expanded_array(element_type,
- CurrentMemoryContext,
+ resultcxt,
my_extra);
}
@@ -443,76 +455,6 @@ array_cat(PG_FUNCTION_ARGS)
/*
- * used by text_to_array() in varlena.c
- */
-ArrayType *
-create_singleton_array(FunctionCallInfo fcinfo,
- Oid element_type,
- Datum element,
- bool isNull,
- int ndims)
-{
- Datum dvalues[1];
- bool nulls[1];
- int16 typlen;
- bool typbyval;
- char typalign;
- int dims[MAXDIM];
- int lbs[MAXDIM];
- int i;
- ArrayMetaState *my_extra;
-
- if (ndims < 1)
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("invalid number of dimensions: %d", ndims)));
- if (ndims > MAXDIM)
- ereport(ERROR,
- (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
- errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
- ndims, MAXDIM)));
-
- dvalues[0] = element;
- nulls[0] = isNull;
-
- for (i = 0; i < ndims; i++)
- {
- dims[i] = 1;
- lbs[i] = 1;
- }
-
- /*
- * We arrange to look up info about element type only once per series of
- * calls, assuming the element type doesn't change underneath us.
- */
- my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
- if (my_extra == NULL)
- {
- fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
- sizeof(ArrayMetaState));
- my_extra = (ArrayMetaState *) fcinfo->flinfo->fn_extra;
- my_extra->element_type = ~element_type;
- }
-
- if (my_extra->element_type != element_type)
- {
- /* Get info about element type */
- get_typlenbyvalalign(element_type,
- &my_extra->typlen,
- &my_extra->typbyval,
- &my_extra->typalign);
- my_extra->element_type = element_type;
- }
- typlen = my_extra->typlen;
- typbyval = my_extra->typbyval;
- typalign = my_extra->typalign;
-
- return construct_md_array(dvalues, nulls, ndims, dims, lbs, element_type,
- typlen, typbyval, typalign);
-}
-
-
-/*
* ARRAY_AGG(anynonarray) aggregate function
*/
Datum
@@ -787,7 +729,8 @@ array_position_common(FunctionCallInfo fcinfo)
format_type_be(element_type))));
my_extra->element_type = element_type;
- fmgr_info(typentry->eq_opr_finfo.fn_oid, &my_extra->proc);
+ fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
+ fcinfo->flinfo->fn_mcxt);
}
/* Examine each array element until we find a match. */
@@ -925,7 +868,8 @@ array_positions(PG_FUNCTION_ARGS)
format_type_be(element_type))));
my_extra->element_type = element_type;
- fmgr_info(typentry->eq_opr_finfo.fn_oid, &my_extra->proc);
+ fmgr_info_cxt(typentry->eq_opr_finfo.fn_oid, &my_extra->proc,
+ fcinfo->flinfo->fn_mcxt);
}
/*