Leanpub Header

Skip to main content

PostgreSQL Under Load: Connections, Transactions, MVCC and Concurrency

A definitive guide to operating PostgreSQL reliably under real-world production workloads.

PostgreSQL Under Load: Connections, Transactions, MVCC and Concurrency
This book is 100% completeLast updated on 2026-09-03

PostgreSQL Under Load shows what really happens when production gets busy. Learn how connections, transactions, MVCC, locks, WAL and autovacuum interact under pressure, then use that knowledge to diagnose incidents, tune PostgreSQL and build systems that stay reliable when concurrency spikes.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
203
Pages
About

About

About the Book

PostgreSQL Under Load is written for experienced software engineers, database administrators, SREs, and systems engineers who need to understand how PostgreSQL behaves when it actually matters: under sustained or sudden concurrency, when connections pile up, locks collide, autovacuum falls behind, or replication drifts. This book explains the mechanisms that govern PostgreSQL's behavior, its process model, MVCC snapshots, locking system, WAL durability guarantees, and vacuuming architecture, not as isolated features but as an interconnected system where every configuration choice and application pattern has consequences under load. You will learn how to configure PostgreSQL correctly, diagnose production incidents systematically, design resilient architectures, and reason about performance and correctness when the traffic is real.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 420 engineers and researchers. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

A definitive guide to operating PostgreSQL reliably under real-world production workloads.

Introduction: Why PostgreSQL Fails Under Load (And How to Stop It)

  1. A Production Incident: Connection Exhaustion Cascades Into Lock Contention
  2. What This Book Will Teach You
  3. Prerequisites and How to Use This Book
  4. Version Scope and Notation Conventions

Chapter 1: The PostgreSQL Process Model and Memory Architecture

  1. Why PostgreSQL Uses Processes Instead of Threads
  2. Backend Processes, Shared Buffers, and Work Memory
  3. Connection Lifecycle: From TCP Accept to Query Execution
  4. Memory Configuration: shared_buffers, work_mem, maintenance_work_mem
  5. Operating System Interactions: ulimits, cgroups, NUMA Considerations
  6. The Cost of a Connection: What Each Process Actually Consumes

Chapter Summary

Chapter 2: Connection Management and Pooling

  1. The Problem with One-Connection-Per-Request Architectures
  2. How PgBouncer Works: Session, Transaction, and Statement Pooling Modes
  3. Configuring PgBouncer for Different Workload Types
  4. Alternative Poolers: Pgpool-II, Odyssey, and Built-in Application-Level Pooling
  5. Connection Storms: Causes, Symptoms, and Mitigation Strategies
  6. Pool Exhaustion vs Server Exhaustion: Diagnosing Which Layer Is the Bottleneck

Chapter Summary

Chapter 3: Transactions, Durability, and Write-Ahead Logging

  1. The ACID Contract in PostgreSQL: What It Actually Means
  2. Write-Ahead Logging: Structure, Segments, and Flush Behavior
  3. fsync, Commit Protocols, and Durability Guarantees
  4. Checkpoints: How They Work, Why They Cause I/O Spikes, and How to Tune Them
  5. WAL Volume Under Load: Estimating and Controlling Write Amplification
  6. Trade-offs: Performance vs Safety in Production Deployments

Chapter Summary

Chapter 4: MVCC, Snapshots, and Concurrency Semantics

  1. How MVCC Works: Tuple Versions, xmin/xmax, and Visibility Maps
  2. Snapshot Creation: Transaction IDs and the Global Snapshot Model
  3. Read Committed vs Repeatable Read vs Serializable: What Actually Changes
  4. Long-Running Transactions: Why They Are Dangerous in PostgreSQL
  5. Vacuum’s Role in MVCC: Cleaning Up Dead Tuples and Preventing Bloat
  6. The Interaction Between Concurrency, Snapshots, and Storage Growth

Chapter Summary

Chapter 5: Locking, Deadlocks, and Contention

  1. Lock Types in PostgreSQL: Row-Level, Page-Level, Table-Level, Advisory
  2. Lock Modes and Compatibility: Understanding What Blocks What
  3. How Deadlock Detection Works and Why It Sometimes Fails
  4. Common Contention Patterns: Hot Rows, Index Contention, Maintenance Locks
  5. Diagnosing Lock Issues: System Catalog Queries and Wait Events
  6. Designing Around Contention: Application Patterns That Avoid Lock Bottlenecks

Chapter Summary

Chapter 6: Isolation Levels in Practice: Serialization Failures and Anomalies

  1. Read Committed: The Default Behavior and Its Subtle Gotchas
  2. Phantom Reads and Non-Repeatable Reads: When They Matter
  3. Serializable Isolation: SSI, Conflict Detection, and Performance Cost
  4. Handling Serialization Failures in Application Code
  5. Real-World Scenarios Where Isolation Level Choice Matters
  6. Testing Concurrency Correctness: Proving Your Application Handles Conflicts

Chapter Summary

Chapter 7: Query Behavior Under Load: Execution Plans, Contention, and Scaling

  1. Why Queries That Are Fast Alone Become Slow Under Concurrency
  2. Resource Contention: CPU, Memory, I/O, and Lock Queues
  3. Plan Stability and the Cost of Replanning Under Load
  4. Batch Operations and Bulk Loads: Avoiding Thrashing with Online Traffic
  5. Connection Pool Interactions with Long-Running Queries
  6. Query Patterns That Scale (and Ones That Do Not)

Chapter Summary

Chapter 8: Vacuum, Autovacuum, Bloat, and Transaction ID Wraparound

  1. What Vacuum Actually Does: Dead Tuple Reclamation and Visibility Map Updates
  2. Autovacuum Configuration: Thresholds, Scale Factors, and Tuning for High-Churn Tables
  3. Table and Index Bloat: Measuring It, Understanding Its Impact, and Fixing It
  4. Transaction ID Wraparound: The Catastrophic Failure Mode and Prevention
  5. VACUUM FULL vs Online Vacuum: When to Use Each and Their Operational Costs
  6. Autovacuum Under Load: Why It Falls Behind and How to Keep It Ahead

Chapter Summary

Chapter 9: Replication, High Availability, and Failover

  1. Streaming Replication: Architecture, Lag Measurement, and Performance Impact
  2. Synchronous vs Asynchronous Replication: Durability vs Latency Trade-offs
  3. Connection Routing Under Failover: How Applications Handle Primary Changes
  4. Read Replicas: Offloading Queries Without Causing New Bottlenecks
  5. Patroni, pg_auto_failover, and Other HA Tools: What They Actually Do
  6. Testing Failover: Procedures That Catch Problems Before Production

Chapter Summary

Chapter 10: Observability, Monitoring, and Metrics

  1. The Essential Metrics: Connections, Transactions, Locks, Replication Lag, Checkpoints
  2. pg_stat_* Views and Extension-Based Monitoring (pg_stat_statements)
  3. Wait Events: PostgreSQL’s Built-In Performance Diagnosis System
  4. Log Analysis and Slow Query Detection in Production
  5. Building Dashboards That Actually Help During Incidents
  6. Correlating Database Metrics with Application and Infrastructure Signals

Chapter Summary

Chapter 11: Troubleshooting Production Incidents

  1. Incident Response Framework: How to Triage PostgreSQL Problems Under Pressure
  2. Connection Exhaustion Incidents: Diagnosis and Recovery Steps
  3. Lock Contention and Deadlock Storms: Finding and Killing the Culprit
  4. Autovacuum Falling Behind: Emergency Procedures and Long-Term Fixes
  5. Replication Lag Spikes: Root Cause Analysis and Resolution
  6. I/O Saturation and Checkpoint Storms: When the Disk Becomes the Bottleneck
  7. Memory Pressure and OOM Kills: Prevention and Recovery

Chapter Summary

Chapter 12: Production Configuration Guide

  1. Core Server Configuration: Memory Settings, WAL, Checkpoint, Autovacuum Parameters
  2. Operating System Tuning: Kernel Parameters, Filesystem Choices, Resource Limits
  3. Connection Pooling Configuration: PgBouncer Setup for Different Workload Profiles
  4. Authentication and Security: pg_hba.conf, SSL/TLS, Role Management
  5. Logging and Monitoring Setup: Log Settings, Slow Query Capture, Metric Collection
  6. Backup Configuration: pg_basebackup, WAL Archiving, Recovery Testing
  7. Replication Configuration: Primary/Standby Settings, Streaming Replication Setup
  8. Production Deployment Patterns: Single-Node HA, Multi-AZ, Cloud-Native Considerations

Chapter Summary

Chapter 13: Capacity Planning, Resilience, and Advanced Operations

  1. Capacity Planning Methodology: From Metrics to Hardware Decisions
  2. Vertical vs Horizontal Scaling: When Each Approach Makes Sense
  3. Handling Sudden Load Spikes: Rate Limiting, Queueing, and Degradation Strategies
  4. Online Schema Changes Under Load: Partitioning, Index Creation, and DDL Safety
  5. Sharding and Connection Distribution: When PostgreSQL Alone Is Not Enough
  6. Disaster Recovery Planning: RPO, RTO, and Testing Your Procedures

Chapter Summary

Conclusion: Operating PostgreSQL with Confidence

  1. The Interconnected System: How Architecture, Configuration, and Workload Shape Behavior
  2. Principles for Reliable PostgreSQL Operations Under Load
  3. Continuing Education: Resources for Staying Current
  4. Final Thoughts on Building Systems That Survive Real Traffic

References

  1. Official PostgreSQL Documentation
  2. PgBouncer Documentation
  3. Key Technical Articles and Blogs
  4. Conference and Academic Sources
  5. Operational and Troubleshooting Resources
  6. High Availability Tools
  7. Backup and Recovery

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub