88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.152 2009/01/01 17:23:49 momjian Exp $
11+ * $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.153 2009/01/30 21:21:18 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -4665,7 +4665,7 @@ array_unnest(PG_FUNCTION_ARGS)
46654665/* stuff done only on the first call of the function */
46664666if (SRF_IS_FIRSTCALL ())
46674667{
4668- ArrayType * arr = PG_GETARG_ARRAYTYPE_P ( 0 ) ;
4668+ ArrayType * arr ;
46694669
46704670/* create a function context for cross-call persistence */
46714671funcctx = SRF_FIRSTCALL_INIT ();
@@ -4675,13 +4675,19 @@ array_unnest(PG_FUNCTION_ARGS)
46754675 */
46764676oldcontext = MemoryContextSwitchTo (funcctx -> multi_call_memory_ctx );
46774677
4678+ /*
4679+ * Get the array value and detoast if needed. We can't do this
4680+ * earlier because if we have to detoast, we want the detoasted
4681+ * copy to be in multi_call_memory_ctx, so it will go away when
4682+ * we're done and not before. (If no detoast happens, we assume
4683+ * the originally passed array will stick around till then.)
4684+ */
4685+ arr = PG_GETARG_ARRAYTYPE_P (0 );
4686+
46784687/* allocate memory for user context */
46794688fctx = (array_unnest_fctx * )palloc (sizeof (array_unnest_fctx ));
46804689
4681- /*
4682- * Initialize state. Note we assume that the originally passed
4683- * array will stick around for the whole call series.
4684- */
4690+ /* initialize state */
46854691fctx -> arr = arr ;
46864692fctx -> nextelem = 0 ;
46874693fctx -> numelems = ArrayGetNItems (ARR_NDIM (arr ),ARR_DIMS (arr ));