aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2011-08-24 23:50:25 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2011-08-24 23:50:25 -0400
commite99bb79e505f7af40376b7ee0ad8547b0e9ed370 (patch)
tree606001a930914b4fcc4a14e8f9b72a684b146ede
parentcb54b6614b6c34f5396d29de1e92b34a83b6c072 (diff)
downloadpostgresql-e99bb79e505f7af40376b7ee0ad8547b0e9ed370.tar.gz
postgresql-e99bb79e505f7af40376b7ee0ad8547b0e9ed370.zip
Fix pgstatindex() to give consistent results for empty indexes.
For an empty index, the pgstatindex() function would compute 0.0/0.0 for its avg_leaf_density and leaf_fragmentation outputs. On machines that follow the IEEE float arithmetic standard with any care, that results in a NaN. However, per report from Rushabh Lathia, Microsoft couldn't manage to get this right, so you'd get a bizarre error on Windows. Fix by forcing the results to be NaN explicitly, rather than relying on the division operator to give that or the snprintf function to print it correctly. I have some doubts that this is really the most useful definition, but it seems better to remain backward-compatible with those platforms for which the behavior wasn't completely broken. Back-patch to 8.2, since the code is like that in all current releases.
-rw-r--r--contrib/pgstattuple/pgstatindex.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c
index 83b50bb432c..9a7af67778c 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -234,9 +234,17 @@ pgstatindex(PG_FUNCTION_ARGS)
values[j] = palloc(32);
snprintf(values[j++], 32, INT64_FORMAT, indexStat.deleted_pages);
values[j] = palloc(32);
- snprintf(values[j++], 32, "%.2f", 100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
+ if (indexStat.max_avail > 0)
+ snprintf(values[j++], 32, "%.2f",
+ 100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
+ else
+ snprintf(values[j++], 32, "NaN");
values[j] = palloc(32);
- snprintf(values[j++], 32, "%.2f", (double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
+ if (indexStat.leaf_pages > 0)
+ snprintf(values[j++], 32, "%.2f",
+ (double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
+ else
+ snprintf(values[j++], 32, "NaN");
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
values);