Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9c0a1df

Browse files
author
Nikita Glukhov
committed
Optimize memory consumption of single-pass algorithm
Store only the value lists in the non-temporary analyze context.
1 parent 5f5454c commit 9c0a1df

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/backend/utils/adt/jsonb_typanalyze.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,11 @@ static inline void
492492
jsonAnalyzeJsonValue(JsonAnalyzeContext *ctx, JsonValueStats *vstats,
493493
JsonbValue *jv)
494494
{
495-
JsonbValue *jbv;
496-
JsonbValue jbvtmp;
497-
Datum value;
495+
JsonbValue *jbv;
496+
JsonbValue jbvtmp;
497+
Jsonb *jb;
498+
Datum value;
499+
MemoryContext oldcxt = NULL;
498500

499501
/* XXX if analyzing only scalar values, make containers empty */
500502
if (ctx->scalarsOnly && jv->type == jbvBinary)
@@ -510,9 +512,17 @@ jsonAnalyzeJsonValue(JsonAnalyzeContext *ctx, JsonValueStats *vstats,
510512
else
511513
jbv = jv;
512514

515+
jb = JsonbValueToJsonb(jbv);
516+
517+
if (ctx->single_pass)
518+
{
519+
oldcxt = MemoryContextSwitchTo(ctx->stats->anl_context);
520+
jb = memcpy(palloc(VARSIZE(jb)), jb, VARSIZE(jb));
521+
}
522+
513523
/* always add it to the "global" JSON stats, shared by all types */
514524
JsonValuesAppend(&vstats->jsons.values,
515-
JsonbPGetDatum(JsonbValueToJsonb(jbv)),
525+
JsonbPGetDatum(jb),
516526
ctx->target);
517527

518528
/* also update the type-specific counters */
@@ -571,6 +581,9 @@ jsonAnalyzeJsonValue(JsonAnalyzeContext *ctx, JsonValueStats *vstats,
571581
elog(ERROR, "invalid scalar json value type %d", jv->type);
572582
break;
573583
}
584+
585+
if (ctx->single_pass)
586+
MemoryContextSwitchTo(oldcxt);
574587
}
575588

576589
/*
@@ -1366,12 +1379,15 @@ jsonAnalyzePass(JsonAnalyzeContext *ctx,
13661379

13671380
jb = DatumGetJsonbP(value);
13681381

1369-
MemoryContextSwitchTo(oldcxt);
1382+
if (!ctx->single_pass)
1383+
MemoryContextSwitchTo(oldcxt);
13701384

13711385
ctx->current_rownum = row_num;
13721386
analyzefunc(ctx, jb, analyzearg);
13731387

1374-
oldcxt = MemoryContextSwitchTo(tmpcxt);
1388+
if (!ctx->single_pass)
1389+
oldcxt = MemoryContextSwitchTo(tmpcxt);
1390+
13751391
MemoryContextReset(tmpcxt);
13761392
}
13771393

0 commit comments

Comments
 (0)