Re: pgstattuple: fix free space calculation

Rafia Sabih <rafia.pghackers@gmail.com>

From: Rafia Sabih <rafia.pghackers@gmail.com>
To: Frédéric Yhuel <frederic.yhuel@dalibo.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2024-08-22T19:56:10Z
Lists: pgsql-hackers

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Consistently use PageGetExactFreeSpace() in pgstattuple.

On Thu, 22 Aug 2024 at 10:11, Frédéric Yhuel <frederic.yhuel@dalibo.com>
wrote:

> Hello,
>
> 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);
-- 
Regards,
Rafia Sabih