Skip to content

Commit daac6e9

Browse files
committed
Fix unnest() to handle a toasted input array safely. Per report from
Alvaro.
1 parent 4265ed9 commit daac6e9

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/backend/utils/adt/arrayfuncs.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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 */
46664666
if (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 */
46714671
funcctx = SRF_FIRSTCALL_INIT();
@@ -4675,13 +4675,19 @@ array_unnest(PG_FUNCTION_ARGS)
46754675
*/
46764676
oldcontext = 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 */
46794688
fctx = (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 */
46854691
fctx->arr = arr;
46864692
fctx->nextelem = 0;
46874693
fctx->numelems = ArrayGetNItems(ARR_NDIM(arr), ARR_DIMS(arr));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy