Skip to content

pg_freespacemap

examine the free space map (FSM)

pg_freespacemap · examine the free space map (FSM)

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
6950pg_freespacemappg_freespacemap1.3STATPostgreSQLC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
PG18 SQL/control baseline 1.3; older PostgreSQL majors may differ.

Packages

PG19PG18PG17PG16PG15
1.31.31.31.31.3
This contrib extension ships with PostgreSQL.

Install

Create this extension with:

CREATE EXTENSION pg_freespacemap;

Usage

pg_freespacemap: examine the free space map

pg_freespacemap provides functions to examine the free space map (FSM), which tracks available space on each page of a relation.

Functions

Free space on a single page:

SELECT pg_freespace('my_table'::regclass, 0);  -- block 0

Free space on all pages:

SELECT * FROM pg_freespace('my_table'::regclass);

 blkno | avail
-------+-------
     0 |     0
     1 |     0
     2 |   224
     3 |  3456
     4 |  8160

Example: Table Bloat Analysis

-- Pages with significant free space
SELECT blkno, avail
FROM pg_freespace('my_table'::regclass)
WHERE avail > 1000
ORDER BY avail DESC;

-- Total free space in a relation
SELECT sum(avail) AS total_free_bytes,
       count(*) AS total_pages,
       count(*) FILTER (WHERE avail > 0) AS pages_with_free_space
FROM pg_freespace('my_table'::regclass);

Notes

  • FSM values are rounded to 1/256th of BLCKSZ (typically 32 bytes)
  • FSM is not kept fully up-to-date; values may lag behind actual free space
  • For indexes, only entirely unused pages are tracked
  • Access restricted to superusers and pg_stat_scan_tables members

Table reader