@@ -2961,7 +2961,14 @@ jsonbzIteratorInit(JsonContainer *jc)
2961
2961
return jsonbIteratorInitExt (jc , jbc , cjb );
2962
2962
}
2963
2963
2964
- List * * jsonb_detoast_iterators ;
2964
+ #define JSONB_FREE_ITERATORS
2965
+ #ifdef JSONB_FREE_ITERATORS
2966
+ static struct
2967
+ {
2968
+ List * iterators ;
2969
+ MemoryContext mcxt ;
2970
+ } * jsonb_detoast_iterators ;
2971
+ #endif
2965
2972
2966
2973
static void
2967
2974
#ifndef JSONB_DETOAST_ITERATOR
@@ -2987,12 +2994,6 @@ jsonbzInitFromDetoastIterator(JsonContainerData *jc, DetoastIterator iter)
2987
2994
cjb -> iter = iter ;
2988
2995
cjb -> offset = offsetof(JsonbDatum , root );
2989
2996
2990
- #define JSONB_FREE_ITERATORS
2991
- #ifdef JSONB_FREE_ITERATORS
2992
- if (jsonb_detoast_iterators )
2993
- * jsonb_detoast_iterators = lappend (* jsonb_detoast_iterators , iter );
2994
- #endif
2995
-
2996
2997
if (!jsonb_partial_decompression )
2997
2998
PG_DETOAST_ITERATE (iter , iter -> buf -> capacity );
2998
2999
else
@@ -3006,7 +3007,9 @@ void
3006
3007
jsonbInitIterators (void )
3007
3008
{
3008
3009
#ifdef JSONB_FREE_ITERATORS
3009
- jsonb_detoast_iterators = palloc0 (sizeof (* jsonb_detoast_iterators ));
3010
+ jsonb_detoast_iterators = palloc (sizeof (* jsonb_detoast_iterators ));
3011
+ jsonb_detoast_iterators -> mcxt = CurrentMemoryContext ;
3012
+ jsonb_detoast_iterators -> iterators = NIL ;
3010
3013
#endif
3011
3014
}
3012
3015
@@ -3017,10 +3020,13 @@ jsonbFreeIterators(void)
3017
3020
ListCell * lc ;
3018
3021
3019
3022
if (jsonb_detoast_iterators )
3020
- foreach (lc , * jsonb_detoast_iterators )
3023
+ {
3024
+ foreach (lc , jsonb_detoast_iterators -> iterators )
3021
3025
free_detoast_iterator (lfirst (lc ));
3022
3026
3023
- jsonb_detoast_iterators = NULL ;
3027
+ pfree (jsonb_detoast_iterators );
3028
+ jsonb_detoast_iterators = NULL ;
3029
+ }
3024
3030
#endif
3025
3031
}
3026
3032
@@ -3045,8 +3051,19 @@ jsonbzInit(JsonContainerData *jc, Datum value)
3045
3051
3046
3052
jsonbzInitFromCompresedDatum (jc , cd );
3047
3053
#else
3054
+ #ifdef JSONB_FREE_ITERATORS
3055
+ MemoryContext oldcxt = jsonb_detoast_iterators ? MemoryContextSwitchTo (jsonb_detoast_iterators -> mcxt ) : NULL ;
3056
+ #endif
3048
3057
DetoastIterator iter = create_detoast_iterator ((struct varlena * ) DatumGetPointer (value ));
3049
3058
3059
+ #ifdef JSONB_FREE_ITERATORS
3060
+ if (jsonb_detoast_iterators )
3061
+ {
3062
+ jsonb_detoast_iterators -> iterators = lappend (jsonb_detoast_iterators -> iterators , iter );
3063
+ MemoryContextSwitchTo (oldcxt );
3064
+ }
3065
+ #endif
3066
+
3050
3067
jsonbzInitFromDetoastIterator (jc , iter );
3051
3068
#endif
3052
3069
}
@@ -3105,13 +3122,23 @@ DatumGetJsonbPC(Datum datum, Json *tmp, bool copy)
3105
3122
if (!cd .compressed )
3106
3123
return DatumGetJson (PointerGetDatum (cd .data ), & jsonbContainerOps , tmp );
3107
3124
#else
3125
+ # ifdef JSONB_FREE_ITERATORS
3126
+ MemoryContext oldcxt = jsonb_detoast_iterators ? MemoryContextSwitchTo (jsonb_detoast_iterators -> mcxt ) : NULL ;
3127
+ # endif
3128
+
3108
3129
if (!jsonb_partial_detoast )
3109
3130
src = detoast_external_attr (src );
3110
3131
3111
3132
iter = create_detoast_iterator (src );
3112
3133
3113
3134
if (!iter )
3135
+ {
3136
+ # ifdef JSONB_FREE_ITERATORS
3137
+ if (jsonb_detoast_iterators )
3138
+ MemoryContextSwitchTo (oldcxt );
3139
+ # endif
3114
3140
return DatumGetJson (PointerGetDatum (src ), & jsonbContainerOps , tmp );
3141
+ }
3115
3142
#endif
3116
3143
3117
3144
js = JsonExpand (tmp , (Datum ) 0 , false, & jsonbzContainerOps );
@@ -3121,6 +3148,15 @@ DatumGetJsonbPC(Datum datum, Json *tmp, bool copy)
3121
3148
memcpy (palloc (sizeof (cd )), & cd , sizeof (cd )));
3122
3149
#else
3123
3150
jsonbzInitFromDetoastIterator (& js -> root , iter );
3151
+
3152
+ # ifdef JSONB_FREE_ITERATORS
3153
+ if (jsonb_detoast_iterators )
3154
+ {
3155
+ jsonb_detoast_iterators -> iterators = lappend (jsonb_detoast_iterators -> iterators , iter );
3156
+ MemoryContextSwitchTo (oldcxt );
3157
+ }
3158
+ # endif
3124
3159
#endif
3160
+
3125
3161
return js ;
3126
3162
}
0 commit comments