惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX - 技术
D
DataBreaches.Net
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
Cisco Talos Blog
Cisco Talos Blog
Microsoft Security Blog
Microsoft Security Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
Kaspersky official blog
F
Fortinet All Blogs
GbyAI
GbyAI
Forbes - Security
Forbes - Security
The Cloudflare Blog
博客园 - 司徒正美
博客园_首页
量子位
Schneier on Security
Schneier on Security
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
N
News | PayPal Newsroom
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
T
Tor Project blog
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
Jina AI
Jina AI
Help Net Security
Help Net Security
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
N
News and Events Feed by Topic
I
Intezer
B
Blog
美团技术团队
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
AWS News Blog
AWS News Blog
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
雷峰网
雷峰网
有赞技术团队
有赞技术团队

The Practical Developer

The Libuv Thread Pool Trap: Why Node.js Async APIs Stall Under Load Postgres DISTINCT ON: The Fastest Way to Get the Latest Row Per Group Postgres Transaction Isolation: The Anomalies Your App Actually Faces in Production Linux TCP Tuning for Node.js Microservices: The Kernel Settings That Stop Silent Connection Drops Under Load Postgres HOT Updates and Fillfactor: Why Not All Writes Are Created Equal Database Connection Pool Leaks: Finding the Promise That Never Returns Its Seat Linux OOM Killer in Production: Why Your Node.js Containers Die Without a Stack Trace Postgres Materialized Views: Refresh Strategies That Do Not Lock Your Dashboards API Dependency Health Checks: Why /health Is Not Enough Authorization with Zanzibar Tuples: How Google Manages Permissions and How To Build the Same Check in Node.js Postgres Advisory Locks: The 20-Character Primitive That Replaces Redis for Coordination Dead Letter Queues: The Message Queue Pattern That Saves You at 2 a.m. File Descriptor Exhaustion: The Kernel Limit That Silently Drops Node.js Connections Graceful Degradation: The Pattern That Turns Total Outages into Partial Success PostgreSQL Full-Text Search: Dropping Elasticsearch for 90% of Use Cases S3 Presigned Multipart Uploads: Stop Your API Server from Being a File Upload Bottleneck MessagePack vs JSON: The Binary Serialization Switch That Cut Our Internal RPC Overhead by 40% DNS Caching in Node.js: The Silent Cause of Production Latency Spikes Reliable Cron Jobs: The Pattern That Stops Double Runs, Missed Executions, And The 2 AM Page GraphQL Query Complexity: Stop the OOM Query Before It Reaches Your Resolver Node.js Event Loop Lag: The Hidden Metric Behind Random Latency Spikes API Request Validation with Zod: The Schema That Catches Bad Input Before It Corrupts Your Database Load Shedding in Node.js: How to Reject Traffic Before You Drown Request Hedging: Cut Tail Latency In Half Without Overprovisioning Git Bisect: The Automated Binary Search That Finds Breaking Commits in Minutes Node.js Garbage Collection Tuning: Stop Letting V8 Pause Your Event Loop Node.js Server Timeouts: The Settings That Stop Slow Clients from Holding Sockets Hostage Postgres BRIN Indexes: The Time-Series Secret That Shrinks Indexes by 99% Event Sourcing with PostgreSQL: The Pragmatic 80% Solution Node.js Cluster Mode: Scaling the Event Loop Across CPU Cores Postgres Partial Indexes: Stopping Soft Deletes from Ruining Your Query Performance Request Coalescing with the Singleflight Pattern: Stop Drowning Your Database on Every Cache Miss The Bulkhead Pattern: Why One Slow Endpoint Should Not Drown Your Whole Service Node.js AsyncLocalStorage: End-to-End Request Context Without the Propagation Hell Postgres Deadlocks: Logging the Victim, Reproducing the Race, and Fixing the Lock Order Your Node.js HTTP Client Is the Bottleneck: Connection Pool Tuning That Works Optimistic Locking in Postgres: Stop Losing Data to Race Conditions Postgres Read Replicas: Stop Serving Stale Data to Your Users Cursor Pagination: Why Offset Queries Explode at Scale and How to Fix Them Node.js Worker Threads: 60 Lines That Stop a CSV Upload from Timing Out Every Other Request Reliable Webhook Delivery: Architecture for Outbound HTTP You Can Trust Request Timeouts and Deadline Propagation: Stop the Chain of Slowness Advanced Security Practices in Node.js Graceful Shutdown in Node.js: The 40 Lines That Stop 502s During Deploys Finding Node.js Memory Leaks with Heap Snapshots Idempotency Keys in 30 Lines: Stop Your Webhook From Charging Customers Twice Backpressure In Node.js: The Fix For Slow-Motion Queue Meltdowns Retries Done Right: Jitter, Budgets, and the Stampede You Did Not See Coming The Cache Stampede: Why Your "Just Add Redis" Layer Crashes Postgres at 3 a.m. Postgres SKIP LOCKED: An 80-Line Job Queue You Can Run Without Redis Stop Doing Work Nobody Wants: AbortController in Node.js, Done Right The N+1 Query Problem: We Found 23 In One Codebase And Killed Every One I Tried 5 AI Coding Tools for a Month. Here Is What I Actually Use CI/CD From Zero to Production in 30 Minutes With GitHub Actions Node.js vs Bun vs Deno: Which Runtime Should You Pick in 2025? Kubernetes Resource Requests And Limits: The Numbers That Decide If Your Cluster Is Stable The Three Pillars of Observability Are A Myth: What Actually Matters In Production pnpm Vs npm Vs yarn Vs Bun For Monorepos: Which One Earns The Migration In 2024 JSONB Indexing In Postgres: GIN Vs Expression Indexes, And When Each Is The Right Choice A Code Review Checklist That Ends The Same Three Arguments Every Sprint gRPC Vs REST In 2024: When The Switch Pays For Itself React Suspense For Data Fetching: The Pattern That Replaces Half Your Loading State Code The Five-Stage Rollout: How To Ship A Risky Change Without Holding Your Breath GitHub Actions In A Monorepo: Caching, Path Filters, And Secret Boundaries That Actually Work The Blameless Postmortem That Actually Improves Things: A Template And Six Hard-Won Rules Recursive CTEs In Postgres: How To Query A Tree Without N Round Trips Node.js Streams: When They Actually Help, And When They Just Add Complexity Playwright Vs Cypress In 2024: The Honest Comparison Of Which One Earns The Test Time React Server Components: The Mental Model That Makes The "use client" Boundary Obvious Pod Disruption Budgets: The K8s Object That Keeps Your Service Up During Cluster Maintenance Postgres LISTEN/NOTIFY: The Pub/Sub You Already Have And Are Not Using Chaos Engineering Starter Kit: The Five Drills That Don't Need Netflix-Scale Spec-Driven API Development With OpenAPI: How To Stop Drifting From Your Docs Saga Pattern vs Two-Phase Commit: Distributed Transactions Without The Lies Kubernetes Autoscaling Beyond CPU: The Custom-Metric HPA Pattern That Actually Works Postgres Partitioning For Time-Series: The Boring Setup That Saves Your Database Distributed Locks With Redis: An Honest Look At Redlock And When You Don't Need It HTTP/2 vs HTTP/3: What Actually Changes For Your App, And What Doesn't Image Optimization For The Web In 2023: srcset, AVIF, And The Lighthouse Score You Actually Want Kafka vs RabbitMQ: A Decision Tree That Doesn't Hate You UUID vs Bigint Primary Keys In Postgres: The Index Math That Decides For You Flame Graphs: How To Find The Slow Function In 30 Seconds Without Profiling Theatre Postgres Streaming Vs. Logical Replication: Which One Solves Your Actual Problem ESLint Rules That Earn Their Keep: The Twelve I Enable On Every Project Pre-Commit Hooks That Pay For Themselves: Husky, lint-staged, And The Five Rules That Stick Zero-Downtime Database Migrations: The Six-Step Pattern That Rules Them All Circuit Breakers In Node.js: 50 Lines That Stop A Failing Dependency From Taking Down Your Service Postgres VACUUM Is Not Magic: How Your Hot Table Bloats To 80GB And How To Fix It Kubernetes Liveness And Readiness Probes: The Difference That Causes Half Your Outages Rate Limiting In Production: A Token Bucket In 30 Lines Of Redis The Outbox Pattern: How To Stop Losing Events When Postgres And Kafka Disagree Load Testing With k6: The Three Scenarios That Find Real Bugs (Not Synthetic Numbers) Postgres Row-Level Security For Multi-Tenant Apps: The Pattern That Stops You From Leaking Data Rebase vs. Merge: The Team Policy That Ends The Argument Forever OpenTelemetry in Node.js: Distributed Tracing That Actually Helps During an Incident Feature Flags That Pay Rent: The 4 Flag Types And When To Delete Each ETag, Last-Modified, and the Caching Headers Most APIs Get Wrong Connection Pooling Without the Cargo Cult: pgbouncer in 100 Lines of Config JSONB Is Not a Schema: When To Reach For It in Postgres, And When To Stop Bash Strict Mode: The Three Lines That Stop Your Deploy Script From Lying To You
Postgres Covering Indexes with INCLUDE: Eliminate Heap Fetches on Read-Heavy Workloads
The Practica · 2026-05-27 · via The Practical Developer

The endpoint that powers our user dashboard was taking 180 ms at p95. The query plan showed an Index Scan using idx_orders_user_status. The index was there. The planner was using it. But the execution plan also showed 8,000 Heap Fetches for a query that returned 200 rows. Every time Postgres found a matching entry in the index, it jumped back to the heap to grab the columns the query actually needed. The index was not covering the query. It was merely starting it.

Adding more indexes was not the answer. The table already had six of them. The answer was the one index type that most teams skip because it sits in the shadow of composite indexes: the covering index created with INCLUDE.

This post is about how INCLUDE indexes work in Postgres, when they create a true Index Only Scan, the visibility map gotcha that breaks them, and the exact EXPLAIN output that tells you whether you have a covering index or a fake one.

What the planner is actually doing

When Postgres runs a query like this:

SELECT order_id, total, created_at
FROM orders
WHERE user_id = 42 AND status = 'shipped';

It has several strategies. A Seq Scan reads every page of the table. An Index Scan walks the B-tree index on (user_id, status) to find matching rows, then fetches each corresponding heap page to read order_id, total, and created_at. An Index Only Scan walks the index and never touches the heap at all, because every column the query needs is either in the index predicate or stored in the index leaf nodes.

Heap fetches are random I/O. Even if your index is in memory, jumping to the heap for every matching row means page lookups, buffer pin contention, and waiting on disk if the working set does not fit in RAM. For a query that returns 200 rows from an index with 10,000 matches, an Index Scan may touch 200 heap pages. An Index Only Scan touches zero.

The difference between 8 ms and 180 ms is often this single decision.

The composite index trap

The first instinct is to add the needed columns to the index key:

CREATE INDEX idx_orders_user_status_covering
ON orders (user_id, status, order_id, total, created_at);

This works. The planner can now satisfy the query entirely from the index. But it is wasteful. The three trailing columns (order_id, total, created_at) are not part of the search predicate. They are payload. By adding them to the key, you force the index to keep them sorted at every level of the B-tree. That increases index size, slows inserts and updates, and wastes space in interior nodes where the payload columns are stored redundantly.

Postgres 11 introduced INCLUDE for exactly this case:

CREATE INDEX idx_orders_user_status_include
ON orders (user_id, status)
INCLUDE (order_id, total, created_at);

Columns in the INCLUDE list are stored only in the leaf nodes of the index. They are not part of the sort order. They do not participate in the search. They exist purely as payload so that an Index Only Scan can return them without visiting the heap. Interior nodes stay small, insert overhead stays low, and the query still gets its columns.

Why INCLUDE is structurally different

In a composite index on (a, b, c), all three columns appear in every level of the index tree including the root and branch nodes. If c is a 16-byte timestamp that is never used for range filtering, it is still copied into every branch node, inflating the tree and increasing the depth. Deeper trees mean more page reads during the scan.

In an index on (a, b) INCLUDE (c), only a and b appear in branch nodes. c lives only at the leaves, appended to each tuple pointer. The branch nodes are smaller, the tree is flatter, and the payload columns are stored exactly once per row. For wide payload columns, the size difference is massive. We measured a 40% reduction in index size by moving four payload columns from the key to the INCLUDE list.

Even more important: because the payload columns are not in the key, an UPDATE that changes a payload column does not force a reorganization of the index structure. If total changes and it is in the key, the index tuple must be deleted and reinserted in the correct sort position. If total is in INCLUDE, the index tuple stays in place and only the payload is updated. This is a significant win for tables with high update rates on included columns.

The visibility map requirement

Here is the catch that breaks most first attempts at covering indexes. Postgres will only use an Index Only Scan if it can prove that every row on the relevant heap pages is visible to the current transaction. If a page contains a dead tuple from an uncommitted or recently committed transaction, Postgres must visit the heap to check visibility.

That visibility information is stored in the visibility map, a bitmap where each bit represents one heap page. When VACUUM runs, it marks pages as all-visible if every tuple on the page is visible to all transactions. Only then can an Index Only Scan skip the heap entirely for rows on that page.

If your visibility map is stale, your shiny INCLUDE index still produces heap fetches. The plan says Index Only Scan, but the execution says Heap Fetches: 7500. You fixed the structure but not the visibility.

Check the visibility map state with:

SELECT
  relname,
  n_live_tup,
  n_dead_tup,
  CASE WHEN n_live_tup + n_dead_tup > 0
    THEN ROUND(n_dead_tup::numeric / (n_live_tup + n_dead_tup) * 100, 2)
    ELSE 0
  END AS dead_pct
FROM pg_stat_user_tables
WHERE relname = 'orders';

If dead_pct is above 5%, your table needs more aggressive vacuuming before Index Only Scan will deliver. The visibility map is also frozen and set during VACUUM (not VACUUM FULL), so autovacuum must be keeping up.

You can also inspect the visibility map directly with the pg_visibility extension:

CREATE EXTENSION IF NOT EXISTS pg_visibility;

SELECT
  all_visible,
  count(*)
FROM pg_visibility_map('orders')
GROUP BY all_visible;

If a large percentage of pages show all_visible = false, heap fetches are inevitable regardless of how perfect your index is.

Seeing the difference in EXPLAIN ANALYZE

Before the covering index, the plan looks like this:

Index Scan using idx_orders_user_status on orders
  Index Cond: ((user_id = 42) AND (status = 'shipped'::text))
  Heap Fetches: 8243

Note Index Scan, not Index Only Scan. The planner needed the heap.

After creating the INCLUDE index, if visibility is clean, the plan becomes:

Index Only Scan using idx_orders_user_status_include on orders
  Index Cond: ((user_id = 42) AND (status = 'shipped'::text))
  Heap Fetches: 0

The presence of Heap Fetches: 0 is the only proof that matters. If you see Index Only Scan but Heap Fetches > 0, the visibility map is the problem, not the index design.

A complete before-and-after example

Here is a realistic schema and workload:

CREATE TABLE orders (
  id bigserial PRIMARY KEY,
  user_id bigint NOT NULL,
  status text NOT NULL,
  order_id uuid NOT NULL,
  total numeric(12,2) NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz NOT NULL DEFAULT now()
);

-- Populate with 1 million rows
INSERT INTO orders (user_id, status, order_id, total)
SELECT
  (random() * 100000)::bigint,
  (ARRAY['pending','shipped','delivered','cancelled'])[1 + (random() * 3)::int],
  gen_random_uuid(),
  (random() * 500)::numeric(12,2)
FROM generate_series(1, 1000000);

-- Old composite index that covers but bloats
CREATE INDEX idx_old_composite
ON orders (user_id, status, order_id, total, created_at);

Run the dashboard query:

EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT order_id, total, created_at
FROM orders
WHERE user_id = 4242 AND status = 'shipped';

You will likely see an Index Only Scan on idx_old_composite with Heap Fetches: 0, but the index is huge and slow to maintain. Now try the INCLUDE version:

DROP INDEX idx_old_composite;

CREATE INDEX idx_orders_user_status_include
ON orders (user_id, status)
INCLUDE (order_id, total, created_at);

VACUUM ANALYZE orders;

Run the same EXPLAIN. You should see Index Only Scan on the new index, same zero heap fetches, and a smaller index size:

SELECT
  indexrelname,
  pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_index
JOIN pg_class ON pg_class.oid = pg_index.indexrelid
WHERE indrelid = 'orders'::regclass;

On our test dataset, the composite index was 142 MB. The INCLUDE index was 89 MB. That is a 37% reduction in index size for the same query performance.

When to use INCLUDE vs composite keys

Use INCLUDE when:

  • The extra columns are only needed for projection (SELECT list), not filtering, sorting, or grouping.
  • The query returns many rows and heap fetches dominate latency.
  • You have a stable access pattern where a few queries drive most of the read load.

Use a regular composite key (no INCLUDE) when:

  • The extra columns are used in ORDER BY or range conditions. INCLUDE columns are not sorted and cannot support an ordered scan.
  • You need the index to enforce uniqueness on a combination of columns. INCLUDE columns are ignored for uniqueness.
  • The column is tiny (boolean, smallint) and the structural savings of INCLUDE are negligible.

Never use INCLUDE for columns that change frequently unless you have checked that HOT updates are working on the table. An UPDATE to an included column still writes a new index tuple at the leaf, just like any index. The savings come from avoiding branch node churn, not from making the column free to update.

Monitoring heap fetches in production

The single most useful metrics are already in pg_stat_user_tables:

SELECT
  relname,
  idx_scan,
  idx_tup_read,
  idx_tup_fetch
FROM pg_stat_user_tables
WHERE relname = 'orders';

idx_tup_read is the number of index entries examined. idx_tup_fetch is the number of heap rows fetched. If idx_tup_fetch is close to idx_tup_read, your indexes are not covering. In a well-tuned read-heavy workload, idx_tup_fetch should be significantly lower than idx_tup_read because many scans should be Index Only.

For per-index statistics:

SELECT
  indexrelname,
  idx_scan,
  idx_tup_read,
  idx_tup_fetch
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
ORDER BY idx_tup_fetch DESC;

An index with millions of idx_tup_fetch visits is a candidate for INCLUDE extension. Find the queries that use it with pg_stat_statements (if enabled), check their SELECT lists, and add the needed columns to INCLUDE.

The write cost trade-off

Nothing is free. Every column in INCLUDE adds to the index leaf size and increases write amplification. When you insert a row, every index on the table gets a new tuple. When you update an included column, every covering index that includes it gets a new leaf tuple. If you have four covering indexes and you update a column included in all four, that update writes four index tuples.

Measure the impact before shipping to production. Use pg_stat_user_indexes to track index growth after adding INCLUDE. If insert latency rises by more than 10%, the column may not be worth including, or you may have over-indexed the table.

As a rule of thumb, keep the payload under 200 bytes per row in the INCLUDE list. Wide payloads (jsonb blobs, long text) defeat the purpose. The index leaf pages fill faster, the tree gets taller, and the scan reverts to memory pressure. If you truly need a huge column in an Index Only Scan, consider whether the table should be narrower or whether the column belongs in a separate table.

The three-step tuning workflow

When a query is slow and the planner already uses an index, use this workflow before adding more hardware:

  1. Confirm the scan type. If the plan shows Index Scan (not Index Only Scan), the query needs columns that are not in the index. Check the SELECT list and WHERE conditions.

  2. Check heap fetches. Even with Index Only Scan, nonzero Heap Fetches means the visibility map is stale. Run VACUUM or tighten autovacuum_vacuum_scale_factor for the table until the map is current.

  3. Add INCLUDE for payload columns. Move non-predicate columns from the key to INCLUDE. Re-run EXPLAIN ANALYZE. Verify Heap Fetches: 0. Verify index size did not explode. Verify insert and update latency stayed flat.

If step three increases write latency too much, the table may be write-bound and read-bound in equal measure. In that case, a materialized view or a read replica with a different index strategy is a better architectural fit than a larger index on the primary.

Common mistakes

Assuming all Index Only Scans are zero-cost. They are not. Index Only Scan means the planner avoided the heap, but if the index is huge and not in shared_buffers, it still reads pages from disk. Buffers: shared read=4500 in EXPLAIN is the truth.

Including columns just in case. Do not add INCLUDE (created_at, updated_at, metadata, notes) because some future query might need them. Every included column increases index maintenance and page pressure. Add only the columns proven by query logs.

Ignoring the visibility map on replicas. If you route reads to a replica, the replica has its own visibility map. A primary that vacuums aggressively does not update the replica’s map. If replica queries show heap fetches, vacuum the replica independently or accept that covering indexes have limited effect on stale replicas.

Using INCLUDE for ORDER BY columns. The planner cannot use an INCLUDE column to avoid a sort. If your query is ORDER BY created_at DESC and created_at is only in INCLUDE, the planner will fetch heap rows or add a separate sort step.

The practical takeaway

Covering indexes with INCLUDE are the most underused optimization in Postgres because they do not show up in pg_stat_statements as a missing index warning. The planner already has an index. It just is not complete. The symptom is slow queries with fast-looking plans and unexplained heap fetches.

Before your next performance review, run this on your top five tables:

SELECT
  schemaname,
  relname,
  indexrelname,
  idx_scan,
  idx_tup_read,
  idx_tup_fetch,
  CASE WHEN idx_tup_read > 0
    THEN ROUND(idx_tup_fetch::numeric / idx_tup_read * 100, 2)
    ELSE 0
  END AS fetch_ratio
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
ORDER BY idx_tup_fetch DESC
LIMIT 20;

Any index with a fetch_ratio above 80% is a prime candidate. Find the queries, check the SELECT lists, and consider whether INCLUDE can turn heap-bound index scans into true index-only scans. The latency drop is immediate, the disk savings are real, and the queries need no changes.

A note from Yojji

Query performance work that looks at the difference between Index Scan and Index Only Scan, that measures heap fetches instead of just wall-clock time, and that understands the visibility map as a prerequisite for covering indexes, is the kind of deep database craft that most product teams skip. It is also exactly the work that keeps production fast as data grows.

Yojji is an international custom software development company founded in 2016, with offices across Europe, the US, and the UK. Their senior engineering teams build scalable backend systems and performance-tune production databases as part of their full-cycle delivery practice, from product discovery through cloud deployment.