Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

PostgreSQL Index-Only Scans and Visibility Maps: The Query Optimization That Most Backends Leave on the Table

--- title : " PostgreSQL Index-Only Scans: Why Your Covering Indexes May Be Lying to You" published : true description : " Learn how PostgreSQL's visibility map controls index-only scan efficiency, why autovacuum frequency is critical for high-write backends, and how to diagnose heap fetch fallbacks with EXPLAIN ANALYZE." tags : postgresql, architecture, performance, api canonical_url :…

PostgreSQL's visibility map controls whether an index-only scan can avoid fetching data from the heap. A covering index containing all columns a query needs should, in theory, allow the database to skip the heap entirely. However, in high-write mobile backends, this promise is often not fulfilled due to the visibility map being silently killed by heap fetches.

The visibility map is a compact one-bit-per-page structure where a set bit indicates all tuples on that heap page are visible to all current and future transactions. When the visibility map bit is set, Postgres can perform an index-only scan without touching the heap.

During write operations, such as updates or deletes on a heap page, the visibility map bit is cleared. For index-only scans to be effective, this bit needs to be set again. This is where autovacuum comes into play. Autovacuum automatically runs VACUUM to reset visibility map bits. In mobile backends, however, the write throughput is typically high, such as 100 rows per second or more.

This rapid write activity can lead to autovacuum failing to keep up with the clearing of visibility map bits, resulting in thousands of unnecessary heap fetches. Consequently, an index-only scan that shows "Index Only Scan" in EXPLAIN output may still perform thousands of heap fetches, as indicated by the "Heap Fetches" metric in EXPLAIN ANALYZE.

This performance degradation can go unnoticed if one only looks at the plan label without checking the actual heap fetches.

To address this issue, one must first read EXPLAIN ANALYZE correctly, looking not just at the plan node type but also at the "Heap Fetches" and "Buffers" metrics. High heap fetch rates indicate that the visibility map bits are not set as they should be. For mobile backends, tuning autovacuum per table is crucial. Default autovacuum scales may not be sufficient.

For high-write tables, it is recommended to set `autovacuum_vacuum_scale_factor` to a very low value (e.g., 0.01), allowing autovacuum to run more frequently. Additionally, increasing `autovacuum_vacuum_cost_delay` and `autovacuum_vacuum_cost_limit` provides more I/O budget for heap cleanup. After making these adjustments, manual VACUUM can be run after batch loads to ensure that the visibility map is updated promptly.

Monitoring the ratio of `vm_visible_pages / heap_pages` can provide early warning signs of index-only scan degradation. A dropping ratio before the latency graphs catch up signals that the performance bottleneck is about to manifest. By paying attention to the visibility map and autovacuum configuration, developers can ensure that their covering indexes truly deliver on their promise, avoiding the pitfall of index-only scans that are misled by stale visibility map bits.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Prompt Injection Isn't Just a Chatbot Problem — It's Coming for Your Internal Tools Too

Most engineering teams' mental model of prompt injection is narrow: someone tricks a public-facing chatbot into saying something embarrassing or bypassing its content guidelines.

  • Prompt injection threat extends beyond chatbots to internal tools.
  • LLMs lack code-data separation, allowing malicious instructions in shared text channel.
  • Defense requires least privilege, human confirmation, and output validation for security.

Judgment Is the Job Now

The machine can generate the code. It can generate ten versions. What it can't do is tell you which one should exist. As producing things gets cheap, the value moves to choosing — knowing what's worth…

Bot Defenses Are Becoming Behavioral. Your Scraper Architecture Has to Change.

Rotating proxies solves a problem from about five years ago. If your team's response to a new block is "add more IPs to the pool," it's worth asking when that fix last actually worked cleanly.

  • Behavioral defenses now common in web scraping anti-bot systems
  • Traditional proxy rotation insufficient against new detection methods
  • Architectures must shift to session-based, consistent client environments

From IT Technician to Cloud & DevOps: My Journey Has Just Begun

From IT Technician to Cloud & DevOps: My Journey Has Just Begun Hi DEV Community! This is my very first post here, even though I've been part of this community for over a year (yes, I just unlocked…

  • One-year-old DEV Community member shares journey from IT Technician to Cloud & DevOps.
  • Currently studying BCA while working as IT Technician for 1.5 years.
  • Aims to become proficient in AWS and transition to Cloud/DevOps role.

More from Tuesday 18 August →