Fillfactor storage tuning

From CarlosCaPe/octorato · View interactive version

Fillfactor storage tuning is an AI agent skill from the GitHub repository CarlosCaPe/octorato. Fillfactor & Storage Parameter Tuning

How to use this skill

Skill file

Fillfactor & Storage Parameter Tuning

What

Adjusting the fillfactor storage parameter on tables and indexes to optimize

for workload patterns. Fillfactor controls how much of each 8KB page PostgreSQL

fills during INSERT/COPY, leaving the rest for in-page HOT updates.

Why

Default fillfactor differs by object type (PG 16 docs):

For tables with frequent UPDATEs, fillfactor 100 forces every update to

create a new tuple on a different page, causing:

Lowering fillfactor to 90 leaves 10% free space per page, enabling

Heap-Only Tuples (HOT) updates that reuse space in-place.

How

Analyze the workload first

-- Check update frequency vs insert frequency
SELECT
    relname,
    n_tup_ins AS inserts,
    n_tup_upd AS updates,
    n_tup_del AS deletes,
    ROUND(100.0 * n_tup_upd / NULLIF(n_tup_ins + n_tup_upd + n_tup_del, 0), 1)
        AS update_pct
FROM pg_stat_user_tables
WHERE schemaname = 'public'
ORDER BY n_tup_upd DESC;

Decision matrix

| Update % | Fillfactor | Rationale |

|----------|------------|-----------|

| < 10% | 100 (default) | Mostly INSERT-only, maximize density |

| 10-50% | 90 | Balance between density and HOT updates |

| > 50% | 80-85 | Heavy UPDATE workload, maximize HOT success |

| Append-only (logs) | 100 | Never updated, maximize density |

Apply fillfactor

-- On the table (affects future INSERTs and rewrites)
ALTER TABLE public."AppointmentInfo"
    SET (fillfactor = 90);

-- On a BTREE index
ALTER INDEX public."PK_AppointmentInfo"
    SET (fillfactor = 90);

-- IMPORTANT: fillfactor only applies to NEW pages.
-- To apply to existing data, REINDEX or VACUUM FULL:
REINDEX INDEX CONCURRENTLY public."PK_AppointmentInfo";

Validate HOT update effectiveness

SELECT
    relname,
    n_tup_hot_upd,
    n_tup_upd,
    ROUND(100.0 * n_tup_hot_upd / NULLIF(n_tup_upd, 0), 1) AS hot_pct
FROM pg_stat_user_tables
WHERE n_tup_upd > 0
ORDER BY n_tup_upd DESC;

Target: HOT update percentage > 90% for update-heavy tables.

When to Use

Where We Used It

References

-- see "Maintenance, vacuum, and bloat" section

Gotchas

REINDEX CONCURRENTLY or VACUUM FULL to reorganize

-- use REINDEX CONCURRENTLY instead for indexes

lock -- lightweight, does not block reads or writes

(PG 16 docs: sql-altertable.html)

further for heavily-updated indexes


Category: Strategy | Origin: ,

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.