I think that pgstattuple should use PageGetExactFreeSpace() instead of PageGetHeapFreeSpace() or PageGetFreeSpace(). The latter two compute the free space minus the space of a line pointer. They are used like this in the rest of the code (heapam.c):
pagefree = PageGetHeapFreeSpace(page);
if (newtupsize > pagefree) { we need a another page for the tuple }
... so it makes sense to take the line pointer into account in this context.
But it in the pgstattuple context, I think we want the exact free space.
I have attached a patch.
Best regards, Frédéric
I agree with the approach here.
A minor comment here is to change the comments in code referring to the PageGetHeapFreeSpace.
--- a/contrib/pgstattuple/pgstatapprox.c +++ b/contrib/pgstattuple/pgstatapprox.c @@ -111,7 +111,7 @@ statapprox_heap(Relation rel, output_type *stat) * treat them as being free space for our purposes. */ if (!PageIsNew(page)) - stat->free_space += PageGetHeapFreeSpace(page); + stat->free_space += PageGetExactFreeSpace(page);