Skip to content

Commit 9fe1d9a

Browse files
committed
Fix possible division by zero in pg_xlogdump.
When displaying stats it was possible that a floating point division by zero occured when no FPIs were issued for a type of record. Author: Abhijit Menon-Sen Discussion: 20150417091811.GA14008@toroid.org
1 parent cac7658 commit 9fe1d9a

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

src/bin/pg_xlogdump/pg_xlogdump.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -489,18 +489,36 @@ XLogDumpDisplayRecord(XLogDumpConfig *config, XLogReaderState *record)
489489
*/
490490
static void
491491
XLogDumpStatsRow(const char *name,
492-
uint64 n, double n_pct,
493-
uint64 rec_len, double rec_len_pct,
494-
uint64 fpi_len, double fpi_len_pct,
495-
uint64 total_len, double total_len_pct)
492+
uint64 n, uint64 total_count,
493+
uint64 rec_len, uint64 total_rec_len,
494+
uint64 fpi_len, uint64 total_fpi_len,
495+
uint64 tot_len, uint64 total_len)
496496
{
497+
double n_pct, rec_len_pct, fpi_len_pct, tot_len_pct;
498+
499+
n_pct = 0;
500+
if (total_count != 0)
501+
n_pct = 100 * (double) n / total_count;
502+
503+
rec_len_pct = 0;
504+
if (total_rec_len != 0)
505+
rec_len_pct = 100 * (double) rec_len / total_rec_len;
506+
507+
fpi_len_pct = 0;
508+
if (total_fpi_len != 0)
509+
fpi_len_pct = 100 * (double) fpi_len / total_fpi_len;
510+
511+
tot_len_pct = 0;
512+
if (total_len != 0)
513+
tot_len_pct = 100 * (double) tot_len / total_len;
514+
497515
printf("%-27s "
498516
"%20" INT64_MODIFIER "u (%6.02f) "
499517
"%20" INT64_MODIFIER "u (%6.02f) "
500518
"%20" INT64_MODIFIER "u (%6.02f) "
501519
"%20" INT64_MODIFIER "u (%6.02f)\n",
502520
name, n, n_pct, rec_len, rec_len_pct, fpi_len, fpi_len_pct,
503-
total_len, total_len_pct);
521+
tot_len, tot_len_pct);
504522
}
505523

506524

@@ -515,6 +533,7 @@ XLogDumpDisplayStats(XLogDumpConfig *config, XLogDumpStats *stats)
515533
uint64 total_rec_len = 0;
516534
uint64 total_fpi_len = 0;
517535
uint64 total_len = 0;
536+
double rec_len_pct, fpi_len_pct;
518537

519538
/* ---
520539
* Make a first pass to calculate column totals:
@@ -557,10 +576,8 @@ XLogDumpDisplayStats(XLogDumpConfig *config, XLogDumpStats *stats)
557576
tot_len = rec_len + fpi_len;
558577

559578
XLogDumpStatsRow(desc->rm_name,
560-
count, 100 * (double) count / total_count,
561-
rec_len, 100 * (double) rec_len / total_rec_len,
562-
fpi_len, 100 * (double) fpi_len / total_fpi_len,
563-
tot_len, 100 * (double) tot_len / total_len);
579+
count, total_count, rec_len, total_rec_len,
580+
fpi_len, total_fpi_len, tot_len, total_len);
564581
}
565582
else
566583
{
@@ -583,10 +600,8 @@ XLogDumpDisplayStats(XLogDumpConfig *config, XLogDumpStats *stats)
583600
id = psprintf("UNKNOWN (%x)", rj << 4);
584601

585602
XLogDumpStatsRow(psprintf("%s/%s", desc->rm_name, id),
586-
count, 100 * (double) count / total_count,
587-
rec_len, 100 * (double) rec_len / total_rec_len,
588-
fpi_len, 100 * (double) fpi_len / total_fpi_len,
589-
tot_len, 100 * (double) tot_len / total_len);
603+
count, total_count, rec_len, total_rec_len,
604+
fpi_len, total_fpi_len, tot_len, total_len);
590605
}
591606
}
592607
}
@@ -601,14 +616,22 @@ XLogDumpDisplayStats(XLogDumpConfig *config, XLogDumpStats *stats)
601616
* them from the earlier ones, and are thus up to 9 characters long.
602617
*/
603618

619+
rec_len_pct = 0;
620+
if (total_len != 0)
621+
rec_len_pct = 100 * (double) total_rec_len / total_len;
622+
623+
fpi_len_pct = 0;
624+
if (total_len != 0)
625+
fpi_len_pct = 100 * (double) total_fpi_len / total_len;
626+
604627
printf("%-27s "
605628
"%20" INT64_MODIFIER "u %-9s"
606629
"%20" INT64_MODIFIER "u %-9s"
607630
"%20" INT64_MODIFIER "u %-9s"
608631
"%20" INT64_MODIFIER "u %-6s\n",
609632
"Total", stats->count, "",
610-
total_rec_len, psprintf("[%.02f%%]", 100 * (double)total_rec_len / total_len),
611-
total_fpi_len, psprintf("[%.02f%%]", 100 * (double)total_fpi_len / total_len),
633+
total_rec_len, psprintf("[%.02f%%]", rec_len_pct),
634+
total_fpi_len, psprintf("[%.02f%%]", fpi_len_pct),
612635
total_len, "[100%]");
613636
}
614637

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