I spent some time this afternoon going through Buffer Cache

Arpit Bhayani

Arpit Bhayani

Jul 17, 2024 • 2 min read


I spent some time this afternoon going through Buffer Cache in PostgreSQL, and here’s the gist of what it is and some interesting details about it ⚡

In PostgreSQL, Buffer Cache helps reduce the query response times and reduce time spent in synchronous I/O by caching the recently accessed disk pages in memory. The cache is checked upon every single page access or update.

The background writer process (discussed in the last post) periodically writes modified buffers back to the disk. This ensures the complete durability of the changes and we do not bloat up the Buffer Cache.

Interestingly, PostgreSQL uses a clock-sweep algorithm for buffer eviction. Whenever a buffer is accessed, the usage count in the buffer header is incremented. The clock-sweep algorithm circularly goes through all buffers and decreases their usage counts by one. The buffer that is selected for eviction:

  • has a zero usage count
  • has a zero pin count (i. e. is not pinned)

Also, a page is considered for eviction only if its usage count is less than the threshold (5). Otherwise, the sweep algorithm moves on.

If you want to explore Buffer Cache deeper. then just install a popular PG extension called pg_buffercache. It helps you examine what’s happening in the shared buffer cache in real-time. Pretty amazing. You will get some amazing insights.

I will be creating a detailed on this topic, covering the cache eviction and observability. But until then, keep going through other stuff I put out. I keep things no fluff.

youtube.com/c/ArpitBhayani

Arpit Bhayani

Principal Engineer II at Razorpay - building Agent Studio, Ex-staff engg at GCP Memorystore & Dataproc, Creator of DiceDB, ex-Amazon Fast Data, ex-Director of Engg. SRE and Data Engineering at Unacademy. I spark engineering curiosity through my no-fluff engineering videos on YouTube and my courses