@@ -492,9 +492,11 @@ static inline void
492
492
jsonAnalyzeJsonValue (JsonAnalyzeContext * ctx , JsonValueStats * vstats ,
493
493
JsonbValue * jv )
494
494
{
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 ;
498
500
499
501
/* XXX if analyzing only scalar values, make containers empty */
500
502
if (ctx -> scalarsOnly && jv -> type == jbvBinary )
@@ -510,9 +512,17 @@ jsonAnalyzeJsonValue(JsonAnalyzeContext *ctx, JsonValueStats *vstats,
510
512
else
511
513
jbv = jv ;
512
514
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
+
513
523
/* always add it to the "global" JSON stats, shared by all types */
514
524
JsonValuesAppend (& vstats -> jsons .values ,
515
- JsonbPGetDatum (JsonbValueToJsonb ( jbv ) ),
525
+ JsonbPGetDatum (jb ),
516
526
ctx -> target );
517
527
518
528
/* also update the type-specific counters */
@@ -571,6 +581,9 @@ jsonAnalyzeJsonValue(JsonAnalyzeContext *ctx, JsonValueStats *vstats,
571
581
elog (ERROR , "invalid scalar json value type %d" , jv -> type );
572
582
break ;
573
583
}
584
+
585
+ if (ctx -> single_pass )
586
+ MemoryContextSwitchTo (oldcxt );
574
587
}
575
588
576
589
/*
@@ -1366,12 +1379,15 @@ jsonAnalyzePass(JsonAnalyzeContext *ctx,
1366
1379
1367
1380
jb = DatumGetJsonbP (value );
1368
1381
1369
- MemoryContextSwitchTo (oldcxt );
1382
+ if (!ctx -> single_pass )
1383
+ MemoryContextSwitchTo (oldcxt );
1370
1384
1371
1385
ctx -> current_rownum = row_num ;
1372
1386
analyzefunc (ctx , jb , analyzearg );
1373
1387
1374
- oldcxt = MemoryContextSwitchTo (tmpcxt );
1388
+ if (!ctx -> single_pass )
1389
+ oldcxt = MemoryContextSwitchTo (tmpcxt );
1390
+
1375
1391
MemoryContextReset (tmpcxt );
1376
1392
}
1377
1393
0 commit comments