Skip to main content
← PROBLEMS / PRODUCTION-DEBUGGING
An end-to-end domain learning journey

Eliminating Full Table
Scans Under Concurrency.

A 180M-row Postgres settlement query scanning cold pages thrashes shared_buffers and hits 14.8s p99; creating a Partial Covering Index CONCURRENTLY for status='PENDING_SETTLEMENT' cuts p99 below 10 ms with zero write downtime.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When a single bottleneck threatens end-to-end availability, build a Partial Covering Index (WHERE status='PENDING_SETTLEMENT' with INCLUDE) using CREATE INDEX CONCURRENTLY to shrink the index by 97%, enable Index-Only Scans, and avoid ACCESS EXCLUSIVE locks.

TL;DR30-second simplified summary
▾

Core Mechanics: On a 180M-row (240 GB) PostgreSQL 16 orders_ledger table where only 3.2% (5.7M rows) are PENDING_SETTLEMENT, an index solely on (created_at) forces Postgres to scan millions of historical SETTLED rows backward.

Visual & Structural Flow: Depicts Partial Index (3.2% rows) + INCLUDE -> Index-Only Scan (<10ms) (merchant_id -> created_at DESC -> WHERE PENDING -> INCLUDE); Index-Only Scan in shared_buffers + Bulkhead HikariCP Pools (Settlement Svc -> shared_buffers -> Visibility Map -> 240GB Heap).

Trade-Offs & Production: A naive fix runs CREATE INDEX idx_merchant_status_created ON orders_ledger (merchant_id, status, created_at DESC);: Table Write Outage: Without CONCURRENTLY, CREATE. Production Postgres remediation combines online indexing with pool protection: **Partial Covering Index**: CREATE INDEX CONCURRENTLY idx_orders_pending_merchant ON orders_ledger.

Failure Modes & Application: Partial covering indexes are a high-leverage optimization across OLTP workloads: Job Queues & Outbox Tables: Indexing only WHERE status = 'PENDING' keeps queue polling O(active) instead of O(history).

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why full/backward index scans thrash buffer pools at 2,400 QPS.

Why this matters: The mechanism: understand how Why full/backward index scans thrash buffer pools at 2,400 QPS works from first principles.

Think of Why full/backward index scans thrash buffer pools at 2,400 QPS as the intuition layer for Eliminating Full Table Scans Under Concurrency: The mechanism: understand how Why full/backward index scans thrash buffer pools at 2,400 QPS works from first principles.

On a 180M-row (240 GB) PostgreSQL 16 orders_ledger table where only 3.2% (5.7M rows) are PENDING_SETTLEMENT, an index solely on (created_at) forces Postgres to scan millions of historical SETTLED rows backward, pulling cold heap pages into the 32 GB shared_buffers. At 2,400 QPS, NVMe read IOPS saturate at 16,000, p99 jumps from 12 ms to 14.8 s, and all 500 HikariCP connections block.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

What happens without a partial covering index and lock-safe deployment.

Why this matters: The problem: see what breaks without What happens without a partial covering index and lock-safe deployment under real concurrency or scale.

Two compounding mistakes turn a missing index into a production outage:

  1. Buffer Pool Thrashing: Scanning cold heap rows evicts hot working-set pages, causing every concurrent checkout query to stall on disk I/O.
  2. Blocking DDL: Running plain CREATE INDEX acquires a SHARE lock that blocks all 1,200 writes/sec (INSERT/UPDATE) for 20+ minutes.
Partial Covering B-Tree

Partial Index (3.2% rows) + INCLUDE -> Index-Only Scan (<10ms)

merchant_idEquality prefixcreated_at DESCWHERE PENDINGPage 0INCLUDEPage 2Page 3

1.Partial Predicate: WHERE status='PENDING_SETTLEMENT' shrinks index size by ~97%.

2.Column Order: (merchant_id, created_at DESC) satisfies filter + sort with zero sort node.

3.INCLUDE Columns: Covers SELECT fields so Postgres skips random heap page fetches.

4.CONCURRENTLY: Builds online without blocking 1,200 writes/sec on orders_ledger.

A 5.7M-row partial covering index fits entirely in shared_buffers and serves 2,400 QPS via Index-Only Scans in < 10 ms.

Engineering Decision Framework
Problem
How to implement Eliminating Full Table Scans Under Concurrency without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When a single bottleneck threatens end-to-end availability, build a Partial Covering Index (WHERE status='PENDING_SETTLEMENT' with INCLUDE) using CREATE INDEX CONCURRENTLY to shrink the index by 97%, enable Index-Only Scans, and avoid ACCESS EXCLUSIVE locks.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Eliminating Full Table Scans Under Concurrency experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Naive solution (and why full composite indexes or plain CREATE INDEX fail).

Why this matters: The problem: see what breaks without Naive solution under real concurrency or scale.

A naive fix runs CREATE INDEX idx_merchant_status_created ON orders_ledger (merchant_id, status, created_at DESC);:

  • Table Write Outage: Without CONCURRENTLY, CREATE INDEX blocks all INSERT/UPDATE traffic on the 240 GB table.
  • Bloated Index: Indexing all 180M rows wastes tens of GBs of RAM and write amplification for 96.8% historical rows.
Design Trade-Off

Naive solution (and why full composite indexes or plain CREATE INDEX fail)

Before · Unbounded Coupling
  • No backpressure on spikes
  • Implicit shared state
  • Cascading tail latency
After · Bounded & Observable
  • Explicit capacity budgets
  • Isolated failure domains
  • Predictable p99 latency
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: CREATE INDEX CONCURRENTLY + Partial Covering Index + Guardrails.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Production Postgres remediation combines online indexing with pool protection:

  • Partial Covering Index: CREATE INDEX CONCURRENTLY idx_orders_pending_merchant ON orders_ledger (merchant_id, created_at DESC) INCLUDE (order_id, amount_cents) WHERE status = 'PENDING_SETTLEMENT'; (with lock_timeout = '2s').
  • Query & Pool Guardrails: Set statement_timeout = '2000ms' and isolate settlement reads onto a read replica or separate bulkhead HikariCP pool so checkout transactions are never starved.
  • CI/CD Regression Check: Enforce EXPLAIN (ANALYZE, BUFFERS) checks in CI against realistic stats.
Buffer Pool & Pool Isolation

Index-Only Scan in shared_buffers + Bulkhead HikariCP Pools

Settlement SvcSeparate pool +…shared_buffersHot 5.7M-row B-TreeVisibility MapSkips heap fetch240GB HeapZero cold-page thrash

1.Index-Only Scan: Visibility Map + INCLUDE columns avoid touching the 240 GB heap.

2.Statement Timeout: 2s DB timeout prevents runaway queries from pinning HikariCP.

3.Pool Bulkhead: Separates settlement batch reads from checkout write connections.

4.pg_stat_statements: Alerts on mean_exec_time or shared_blks_read regressions.

Keeping the partial index hot in shared_buffers drops NVMe IOPS and brings p99 latency under 10 ms at 2,400 QPS.

-- Zero-downtime Partial Covering Index on 180M-row Postgres 16 table
SET lock_timeout = '2s';
SET maintenance_work_mem = '2GB';

CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_pending_merchant_created
ON orders_ledger (merchant_id, created_at DESC)
INCLUDE (order_id, amount_cents, currency)
WHERE status = 'PENDING_SETTLEMENT';

-- Ensure Visibility Map stays all-visible for Index-Only Scans:
-- ALTER TABLE orders_ledger SET (autovacuum_vacuum_scale_factor = 0.01);
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where Partial & Covering Indexes apply in System Design.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Partial covering indexes are a high-leverage optimization across OLTP workloads:

  • Job Queues & Outbox Tables: Indexing only WHERE status = 'PENDING' keeps queue polling O(active) instead of O(history).
  • Payment & Ledger Dashboards: Serving LIMIT 50 merchant views via Index-Only Scans.
  • Zero-Downtime Migrations: Using CONCURRENTLY + lock_timeout in automated migration pipelines.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why is a Partial Covering Index with CREATE INDEX CONCURRENTLY ideal for the 180M-row orders_ledger settlement query?

Recommended Learning Flow