Skip to content

Commit ae42744

Browse files
committed
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.
1 parent 6016005 commit ae42744

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

contrib/pgstattuple/pgstatindex.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,17 @@ pgstatindex(PG_FUNCTION_ARGS)
377377
values[j] = palloc(32);
378378
snprintf(values[j++], 32, "%d", indexStat.deleted_pages);
379379
values[j] = palloc(32);
380-
snprintf(values[j++], 32, "%.2f", 100.0 - (float) indexStat.free_space / (float) indexStat.max_avail * 100.0);
380+
if (indexStat.max_avail > 0)
381+
snprintf(values[j++], 32, "%.2f",
382+
100.0 - (double) indexStat.free_space / (double) indexStat.max_avail * 100.0);
383+
else
384+
snprintf(values[j++], 32, "NaN");
381385
values[j] = palloc(32);
382-
snprintf(values[j++], 32, "%.2f", (float) indexStat.fragments / (float) indexStat.leaf_pages * 100.0);
386+
if (indexStat.leaf_pages > 0)
387+
snprintf(values[j++], 32, "%.2f",
388+
(double) indexStat.fragments / (double) indexStat.leaf_pages * 100.0);
389+
else
390+
snprintf(values[j++], 32, "NaN");
383391

384392
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
385393
values);

0 commit comments

Comments
 (0)