@@ -62,6 +62,12 @@ typedef struct SerializeMetrics
62
62
#define X_CLOSE_IMMEDIATE 2
63
63
#define X_NOWHITESPACE 4
64
64
65
+ /*
66
+ * Various places within need to convert bytes to kilobytes. Round these up
67
+ * to the next whole kilobyte.
68
+ */
69
+ #define BYTES_TO_KILOBYTES (b ) (((b) + 1023) / 1024)
70
+
65
71
static void ExplainOneQuery (Query * query , int cursorOptions ,
66
72
IntoClause * into , ExplainState * es ,
67
73
const char * queryString , ParamListInfo params ,
@@ -1120,11 +1126,11 @@ ExplainPrintSerialize(ExplainState *es, SerializeMetrics *metrics)
1120
1126
if (es -> timing )
1121
1127
appendStringInfo (es -> str , "Serialization: time=%.3f ms output=" UINT64_FORMAT "kB format=%s\n" ,
1122
1128
1000.0 * INSTR_TIME_GET_DOUBLE (metrics -> timeSpent ),
1123
- (metrics -> bytesSent + 512 ) / 1024 ,
1129
+ BYTES_TO_KILOBYTES (metrics -> bytesSent ) ,
1124
1130
format );
1125
1131
else
1126
1132
appendStringInfo (es -> str , "Serialization: output=" UINT64_FORMAT "kB format=%s\n" ,
1127
- (metrics -> bytesSent + 512 ) / 1024 ,
1133
+ BYTES_TO_KILOBYTES (metrics -> bytesSent ) ,
1128
1134
format );
1129
1135
1130
1136
if (es -> buffers && peek_buffer_usage (es , & metrics -> bufferUsage ))
@@ -1141,7 +1147,7 @@ ExplainPrintSerialize(ExplainState *es, SerializeMetrics *metrics)
1141
1147
1000.0 * INSTR_TIME_GET_DOUBLE (metrics -> timeSpent ),
1142
1148
3 , es );
1143
1149
ExplainPropertyUInteger ("Output Volume" , "kB" ,
1144
- (metrics -> bytesSent + 512 ) / 1024 , es );
1150
+ BYTES_TO_KILOBYTES (metrics -> bytesSent ) , es );
1145
1151
ExplainPropertyText ("Format" , format , es );
1146
1152
if (es -> buffers )
1147
1153
show_buffer_usage (es , & metrics -> bufferUsage );
@@ -3275,7 +3281,7 @@ show_hash_info(HashState *hashstate, ExplainState *es)
3275
3281
3276
3282
if (hinstrument .nbatch > 0 )
3277
3283
{
3278
- long spacePeakKb = (hinstrument .space_peak + 1023 ) / 1024 ;
3284
+ uint64 spacePeakKb = BYTES_TO_KILOBYTES (hinstrument .space_peak ) ;
3279
3285
3280
3286
if (es -> format != EXPLAIN_FORMAT_TEXT )
3281
3287
{
@@ -3287,15 +3293,15 @@ show_hash_info(HashState *hashstate, ExplainState *es)
3287
3293
hinstrument .nbatch , es );
3288
3294
ExplainPropertyInteger ("Original Hash Batches" , NULL ,
3289
3295
hinstrument .nbatch_original , es );
3290
- ExplainPropertyInteger ("Peak Memory Usage" , "kB" ,
3291
- spacePeakKb , es );
3296
+ ExplainPropertyUInteger ("Peak Memory Usage" , "kB" ,
3297
+ spacePeakKb , es );
3292
3298
}
3293
3299
else if (hinstrument .nbatch_original != hinstrument .nbatch ||
3294
3300
hinstrument .nbuckets_original != hinstrument .nbuckets )
3295
3301
{
3296
3302
ExplainIndentText (es );
3297
3303
appendStringInfo (es -> str ,
3298
- "Buckets: %d (originally %d) Batches: %d (originally %d) Memory Usage: %ldkB \n" ,
3304
+ "Buckets: %d (originally %d) Batches: %d (originally %d) Memory Usage: " UINT64_FORMAT "kB \n" ,
3299
3305
hinstrument .nbuckets ,
3300
3306
hinstrument .nbuckets_original ,
3301
3307
hinstrument .nbatch ,
@@ -3306,7 +3312,7 @@ show_hash_info(HashState *hashstate, ExplainState *es)
3306
3312
{
3307
3313
ExplainIndentText (es );
3308
3314
appendStringInfo (es -> str ,
3309
- "Buckets: %d Batches: %d Memory Usage: %ldkB \n" ,
3315
+ "Buckets: %d Batches: %d Memory Usage: " UINT64_FORMAT "kB \n" ,
3310
3316
hinstrument .nbuckets , hinstrument .nbatch ,
3311
3317
spacePeakKb );
3312
3318
}
@@ -3376,9 +3382,9 @@ show_memoize_info(MemoizeState *mstate, List *ancestors, ExplainState *es)
3376
3382
* when mem_peak is 0.
3377
3383
*/
3378
3384
if (mstate -> stats .mem_peak > 0 )
3379
- memPeakKb = (mstate -> stats .mem_peak + 1023 ) / 1024 ;
3385
+ memPeakKb = BYTES_TO_KILOBYTES (mstate -> stats .mem_peak ) ;
3380
3386
else
3381
- memPeakKb = (mstate -> mem_used + 1023 ) / 1024 ;
3387
+ memPeakKb = BYTES_TO_KILOBYTES (mstate -> mem_used ) ;
3382
3388
3383
3389
if (es -> format != EXPLAIN_FORMAT_TEXT )
3384
3390
{
@@ -3427,7 +3433,7 @@ show_memoize_info(MemoizeState *mstate, List *ancestors, ExplainState *es)
3427
3433
* MemoizeInstrumentation.mem_peak field for us. No need to do the
3428
3434
* zero checks like we did for the serial case above.
3429
3435
*/
3430
- memPeakKb = (si -> mem_peak + 1023 ) / 1024 ;
3436
+ memPeakKb = BYTES_TO_KILOBYTES (si -> mem_peak ) ;
3431
3437
3432
3438
if (es -> format == EXPLAIN_FORMAT_TEXT )
3433
3439
{
@@ -3464,7 +3470,7 @@ static void
3464
3470
show_hashagg_info (AggState * aggstate , ExplainState * es )
3465
3471
{
3466
3472
Agg * agg = (Agg * ) aggstate -> ss .ps .plan ;
3467
- int64 memPeakKb = (aggstate -> hash_mem_peak + 1023 ) / 1024 ;
3473
+ int64 memPeakKb = BYTES_TO_KILOBYTES (aggstate -> hash_mem_peak ) ;
3468
3474
3469
3475
if (agg -> aggstrategy != AGG_HASHED &&
3470
3476
agg -> aggstrategy != AGG_MIXED )
@@ -3545,7 +3551,7 @@ show_hashagg_info(AggState *aggstate, ExplainState *es)
3545
3551
continue ;
3546
3552
hash_disk_used = sinstrument -> hash_disk_used ;
3547
3553
hash_batches_used = sinstrument -> hash_batches_used ;
3548
- memPeakKb = (sinstrument -> hash_mem_peak + 1023 ) / 1024 ;
3554
+ memPeakKb = BYTES_TO_KILOBYTES (sinstrument -> hash_mem_peak ) ;
3549
3555
3550
3556
if (es -> workers_state )
3551
3557
ExplainOpenWorker (n , es );
@@ -3942,22 +3948,22 @@ show_wal_usage(ExplainState *es, const WalUsage *usage)
3942
3948
static void
3943
3949
show_memory_counters (ExplainState * es , const MemoryContextCounters * mem_counters )
3944
3950
{
3951
+ int64 memUsedkB = BYTES_TO_KILOBYTES (mem_counters -> totalspace -
3952
+ mem_counters -> freespace );
3953
+ int64 memAllocatedkB = BYTES_TO_KILOBYTES (mem_counters -> totalspace );
3954
+
3945
3955
if (es -> format == EXPLAIN_FORMAT_TEXT )
3946
3956
{
3947
3957
ExplainIndentText (es );
3948
3958
appendStringInfo (es -> str ,
3949
- "Memory: used=%lld bytes allocated=%lld bytes" ,
3950
- (long long ) (mem_counters -> totalspace - mem_counters -> freespace ),
3951
- (long long ) mem_counters -> totalspace );
3959
+ "Memory: used=" INT64_FORMAT "kB allocated=" INT64_FORMAT "kB" ,
3960
+ memUsedkB , memAllocatedkB );
3952
3961
appendStringInfoChar (es -> str , '\n' );
3953
3962
}
3954
3963
else
3955
3964
{
3956
- ExplainPropertyInteger ("Memory Used" , "bytes" ,
3957
- mem_counters -> totalspace - mem_counters -> freespace ,
3958
- es );
3959
- ExplainPropertyInteger ("Memory Allocated" , "bytes" ,
3960
- mem_counters -> totalspace , es );
3965
+ ExplainPropertyInteger ("Memory Used" , "kB" , memUsedkB , es );
3966
+ ExplainPropertyInteger ("Memory Allocated" , "kB" , memAllocatedkB , es );
3961
3967
}
3962
3968
}
3963
3969
0 commit comments