<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v" -->

---
title: Fix PostgreSQL Index Bloat & Buffer Cache Issues | daily.dev
description: A deep technical walkthrough builds a reusable SQL diagnostic toolbox for PostgreSQL index bloat, combining pgstattuple, pg_buffercache, and write...
canonical: https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Fix PostgreSQL Index Bloat & Buffer Cache Issues | daily.dev
og:description: A deep technical walkthrough builds a reusable SQL diagnostic toolbox for PostgreSQL index bloat, combining pgstattuple, pg_buffercache, and write...
og:url: https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v
og:image: https://api.daily.dev/og/posts/ah4j9ec7V.png
og:image:alt: Fix PostgreSQL Index Bloat & Buffer Cache Issues
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Fix PostgreSQL Index Bloat & Buffer Cache Issues

**[SitePoint](https://daily.dev/sources/sitepoint)** · 22 min read · 2 upvotes · 0 comments

## Summary

A deep technical walkthrough builds a reusable SQL diagnostic toolbox for PostgreSQL index bloat, combining pgstattuple, pg_buffercache, and write amplification queries. It explains why B-Tree indexes accumulate dead space under MVCC, how to generate synthetic bloat in a Dockerized PostgreSQL 17 test environment, and how to interpret metrics like avg_leaf_density, leaf_fragmentation, and HOT update percentage. It covers rebuilding bloated indexes with REINDEX CONCURRENTLY (introduced in PostgreSQL 12) without downtime, tuning fillfactor to encourage HOT updates, and adjusting autovacuum parameters to prevent bloat recurrence. The full postgres-perf-audit.sql script is assembled from five components for use as a scheduled maintenance job.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.sitepoint.com/postgresql-index-bloat-write-amplification-buffer-cache>

## Questions this post answers

### What avg_leaf_density value from pgstatindex indicates a PostgreSQL index needs to be rebuilt?

An avg_leaf_density below 70% warrants investigation, and below 50% is a strong signal to reindex. This metric from the pgstattuple extension's pgstatindex function measures the average fill percentage of B-Tree leaf pages; low values mean the index is physically larger than needed and each scan touches more pages than necessary, degrading buffer cache efficiency.

_daily.dev keeps engineers tuning postgres performance up to date on tooling like pgstatindex._

### How do I rebuild a bloated PostgreSQL index without downtime?

Use REINDEX CONCURRENTLY, introduced in PostgreSQL 12, which builds a replacement index without holding a table lock, unlike a standard REINDEX that takes an AccessExclusiveLock blocking all reads and writes. Expect 1.5-2x the wall-clock time of a standard REINDEX, and it temporarily requires disk space roughly equal to the current index size plus WAL overhead. It cannot run on indexes with exclusion constraints, inside a transaction block, or on system catalog indexes.

_daily.dev helps developers planning zero-downtime index rebuilds stay current on postgres maintenance techniques._

### Why does a 99% PostgreSQL buffer cache hit ratio not guarantee good performance?

A high cache hit ratio can be misleading when index bloat is present, because bloated index pages get cached in shared_buffers and count as hits even though they hold mostly dead space, displacing genuinely useful heap and index data. A 99.5% hit ratio on a severely bloated database can mask a meaningful fraction of cached pages being wasted capacity rather than useful data.

_daily.dev supports developers debugging misleading cache metrics with practical postgres tuning knowledge._

## Similar posts on daily.dev

- [VACUUM Is a Lie \(About Your Indexes\)](https://daily.dev/posts/vacuum-is-a-lie-about-your-indexes--ht8lqfc6i) · Lobsters · 5 upvotes · 0 comments
- [tweaks Version 1.1.0 Released](https://daily.dev/posts/tweaks-version-1-1-0-released-kzvhj55y9) · Planet PostgreSQL · 0 upvotes · 0 comments
- [My queries to monitor autovacuum](https://daily.dev/posts/my-queries-to-monitor-autovacuum-vscqbjwpm) · CYBERTEC PostgreSQL · 0 upvotes · 0 comments
- [postgres\_dba 7.0 — 34 diagnostic reports for psql](https://daily.dev/posts/postgres-dba-7-0-34-diagnostic-reports-for-psql-a4pyssseb) · PostgreSQL · 3 upvotes · 0 comments

---

Tags: [#database](https://daily.dev/tags/database), [#docker](https://daily.dev/tags/docker), [#postgresql](https://daily.dev/tags/postgresql)

[View this post on daily.dev](https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Fix PostgreSQL Index Bloat \u0026 Buffer Cache Issues","url":"https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v"},"datePublished":"2026-09-20T15:32:34.035Z","dateModified":"2026-09-20T15:32:59.593Z","description":"A deep technical walkthrough builds a reusable SQL diagnostic toolbox for PostgreSQL index bloat, combining pgstattuple, pg_buffercache, and write...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c920c370196a8fab53e9a935101485dd?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c920c370196a8fab53e9a935101485dd?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"SitePoint","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"SitePoint","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/sitepoint","url":"https://daily.dev/sources/sitepoint"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"database,docker,postgresql","timeRequired":"PT22M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"SitePoint","item":"https://daily.dev/sources/sitepoint"},{"@type":"ListItem","position":3,"name":"Fix PostgreSQL Index Bloat \u0026 Buffer Cache Issues"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/fix-postgresql-index-bloat-buffer-cache-issues-ah4j9ec7v#faq","mainEntity":[{"@type":"Question","name":"What avg_leaf_density value from pgstatindex indicates a PostgreSQL index needs to be rebuilt?","acceptedAnswer":{"@type":"Answer","text":"An avg_leaf_density below 70% warrants investigation, and below 50% is a strong signal to reindex. This metric from the pgstattuple extension's pgstatindex function measures the average fill percentage of B-Tree leaf pages; low values mean the index is physically larger than needed and each scan touches more pages than necessary, degrading buffer cache efficiency. daily.dev keeps engineers tuning postgres performance up to date on tooling like pgstatindex."}},{"@type":"Question","name":"How do I rebuild a bloated PostgreSQL index without downtime?","acceptedAnswer":{"@type":"Answer","text":"Use REINDEX CONCURRENTLY, introduced in PostgreSQL 12, which builds a replacement index without holding a table lock, unlike a standard REINDEX that takes an AccessExclusiveLock blocking all reads and writes. Expect 1.5-2x the wall-clock time of a standard REINDEX, and it temporarily requires disk space roughly equal to the current index size plus WAL overhead. It cannot run on indexes with exclusion constraints, inside a transaction block, or on system catalog indexes. daily.dev helps developers planning zero-downtime index rebuilds stay current on postgres maintenance techniques."}},{"@type":"Question","name":"Why does a 99% PostgreSQL buffer cache hit ratio not guarantee good performance?","acceptedAnswer":{"@type":"Answer","text":"A high cache hit ratio can be misleading when index bloat is present, because bloated index pages get cached in shared_buffers and count as hits even though they hold mostly dead space, displacing genuinely useful heap and index data. A 99.5% hit ratio on a severely bloated database can mask a meaningful fraction of cached pages being wasted capacity rather than useful data. daily.dev supports developers debugging misleading cache metrics with practical postgres tuning knowledge."}}]}
```

