Skip to content

Commit 35e6d57

Browse files
author
Nikita Glukhov
committed
Fix jsonb iterator freeing
1 parent 64c19e4 commit 35e6d57

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

src/backend/access/common/detoast.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ create_detoast_iterator(struct varlena *attr)
4242
iter = (DetoastIterator) palloc0(sizeof(DetoastIteratorData));
4343
iter->done = false;
4444
iter->nrefs = 1;
45-
iter->gen.free = (void (*)(void *)) free_detoast_iterator;
45+
iter->gen.free_callback.func = (void (*)(void *)) free_detoast_iterator;
4646

4747
/* This is an externally stored datum --- initialize fetch datum iterator */
4848
iter->fetch_datum_iterator = fetch_iter = create_fetch_datum_iterator(attr);
@@ -87,7 +87,7 @@ create_detoast_iterator(struct varlena *attr)
8787
iter = (DetoastIterator) palloc0(sizeof(DetoastIteratorData));
8888
iter->done = false;
8989
iter->nrefs = 1;
90-
iter->gen.free = (void (*)(void *)) free_detoast_iterator;
90+
iter->gen.free_callback.func = (void (*)(void *)) free_detoast_iterator;
9191

9292
iter->fetch_datum_iterator = palloc0(sizeof(*iter->fetch_datum_iterator));
9393
iter->fetch_datum_iterator->buf = buf = create_toast_buffer(VARSIZE_ANY(attr), true);

src/backend/utils/adt/jsonb_util.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,7 +2961,6 @@ jsonbzIteratorInit(JsonContainer *jc)
29612961
return jsonbIteratorInitExt(jc, jbc, cjb);
29622962
}
29632963

2964-
#define JSONB_FREE_ITERATORS
29652964
#ifdef JSONB_FREE_ITERATORS
29662965
static struct
29672966
{
@@ -3003,20 +3002,20 @@ jsonbzInitFromDetoastIterator(JsonContainerData *jc, DetoastIterator iter)
30033002
#endif
30043003
}
30053004

3005+
#ifdef JSONB_FREE_ITERATORS
30063006
void
30073007
jsonbInitIterators(void)
30083008
{
3009-
#ifdef JSONB_FREE_ITERATORS
30103009
jsonb_detoast_iterators = palloc(sizeof(*jsonb_detoast_iterators));
30113010
jsonb_detoast_iterators->mcxt = CurrentMemoryContext;
30123011
jsonb_detoast_iterators->iterators = NIL;
3013-
#endif
30143012
}
3013+
#endif
30153014

3015+
#ifdef JSONB_FREE_ITERATORS
30163016
void
30173017
jsonbFreeIterators(void)
30183018
{
3019-
#ifdef JSONB_FREE_ITERATORS
30203019
ListCell *lc;
30213020

30223021
if (jsonb_detoast_iterators)
@@ -3031,20 +3030,28 @@ jsonbFreeIterators(void)
30313030
pfree(jsonb_detoast_iterators);
30323031
jsonb_detoast_iterators = NULL;
30333032
}
3034-
#endif
30353033
}
3034+
#endif
30363035

3036+
#ifdef JSONB_FREE_ITERATORS
30373037
MemoryContext
30383038
jsonbGetIteratorContext(void)
30393039
{
30403040
return jsonb_detoast_iterators ? jsonb_detoast_iterators->mcxt : NULL;
30413041
}
3042+
#endif
30423043

30433044
void
30443045
jsonbRegisterIterator(GenericDetoastIterator iter)
30453046
{
3047+
#ifndef JSONB_FREE_ITERATORS
3048+
//iter->free_callback.func = iter->free;
3049+
iter->free_callback.arg = iter;
3050+
MemoryContextRegisterResetCallback(CurrentMemoryContext, &iter->free_callback);
3051+
#else
30463052
if (jsonb_detoast_iterators)
30473053
jsonb_detoast_iterators->iterators = lappend(jsonb_detoast_iterators->iterators, iter);
3054+
#endif
30483055
}
30493056

30503057
static void
@@ -3079,6 +3086,8 @@ jsonbzInit(JsonContainerData *jc, Datum value)
30793086
jsonbRegisterIterator(&iter->gen);
30803087
MemoryContextSwitchTo(oldcxt);
30813088
}
3089+
#else
3090+
jsonbRegisterIterator(&iter->gen);
30823091
#endif
30833092

30843093
jsonbzInitFromDetoastIterator(jc, iter);
@@ -3172,6 +3181,8 @@ DatumGetJsonbPC(Datum datum, Json *tmp, bool copy)
31723181
jsonbRegisterIterator(&iter->gen);
31733182
MemoryContextSwitchTo(oldcxt);
31743183
}
3184+
# else
3185+
jsonbRegisterIterator(&iter->gen);
31753186
# endif
31763187
#endif
31773188

src/include/access/detoast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ typedef struct FetchDatumIteratorData *FetchDatumIterator;
9595

9696
typedef struct GenericDetoastIteratorData
9797
{
98-
void (*free)(void *iter);
98+
MemoryContextCallback free_callback;
9999
} GenericDetoastIteratorData, *GenericDetoastIterator;
100100

101101
typedef struct DetoastIteratorData

src/include/utils/jsonb.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,15 @@ extern bool jsonb_sort_field_values; /* GUC */
255255
extern bool jsonb_partial_decompression; /* GUC */
256256
extern bool jsonb_partial_detoast; /* GUC */
257257

258+
//#define JSONB_FREE_ITERATORS
259+
#ifdef JSONB_FREE_ITERATORS
258260
extern void jsonbInitIterators(void);
259261
extern void jsonbFreeIterators(void);
260262
extern MemoryContext jsonbGetIteratorContext(void);
263+
#else
264+
# define jsonbInitIterators() ((void) 0)
265+
# define jsonbFreeIterators() ((void) 0)
266+
#endif
261267
extern void jsonbRegisterIterator(GenericDetoastIterator iter);
262268

263269
#endif /* __JSONB_H__ */

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