PostgreSQL is one of the most powerful open-source relational databases available, but running it efficiently in production requires deliberate tuning. Default configurations are designed for compatibility, not performance. At Nexis Limited, we have tuned PostgreSQL instances handling millions of transactions daily, and the patterns below reflect hard-won lessons from real production workloads.

Configuration Tuning

The first step in any PostgreSQL optimization effort is adjusting postgresql.conf to reflect your hardware and workload profile. The defaults assume a modest environment with limited memory. For a production server with 64 GB RAM, consider these starting points:

# Memory Settings
shared_buffers = 16GB            # 25% of total RAM
effective_cache_size = 48GB      # 75% of total RAM
work_mem = 256MB                 # Per-operation sort/hash memory
maintenance_work_mem = 2GB       # For VACUUM, CREATE INDEX

# WAL Settings
wal_buffers = 64MB
checkpoint_completion_target = 0.9
max_wal_size = 4GB

# Connection Settings
max_connections = 200

The shared_buffers setting controls how much memory PostgreSQL uses for caching data pages. Setting it to roughly 25% of total RAM is a well-established guideline. The effective_cache_size tells the query planner how much memory is available for caching, including the OS page cache, which influences whether it chooses index scans over sequential scans.

Indexing Strategies

Indexes are the single most impactful tool for query performance. However, over-indexing wastes disk space and slows down writes. Focus on columns that appear in WHERE, JOIN, and ORDER BY clauses. PostgreSQL supports several index types beyond the default B-tree:

  • B-tree: Default index type, ideal for equality and range queries.
  • GIN (Generalized Inverted Index): Best for full-text search, JSONB containment queries, and array operations.
  • GiST (Generalized Search Tree): Suitable for geometric data, range types, and proximity searches.
  • BRIN (Block Range Index): Extremely compact indexes for naturally ordered data like timestamps.

Partial and Covering Indexes

Partial indexes reduce index size by only indexing rows that match a predicate. For example, if you frequently query active users:

CREATE INDEX idx_users_active_email
ON users (email)
WHERE is_active = true;

Covering indexes use the INCLUDE clause to add non-key columns, enabling index-only scans:

CREATE INDEX idx_orders_customer
ON orders (customer_id)
INCLUDE (order_total, created_at);

Query Analysis with EXPLAIN

The EXPLAIN ANALYZE command is indispensable for understanding query execution plans. Always run it against representative data volumes, since the planner makes cost-based decisions that change with table size.

EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT o.id, o.order_total, c.name
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.created_at > '2025-01-01'
  AND o.status = 'completed';

Look for sequential scans on large tables, nested loop joins where hash joins would be more efficient, and high buffer read counts indicating cache misses. The BUFFERS option reveals how many pages were read from shared buffers versus disk.

Connection Pooling with PgBouncer

Each PostgreSQL connection consumes roughly 5–10 MB of memory. Applications that open hundreds of connections can exhaust server resources. PgBouncer sits between your application and PostgreSQL, multiplexing many client connections over a smaller pool of server connections. In transaction pooling mode, a server connection is assigned only for the duration of a transaction, dramatically improving connection utilization.

Vacuuming and Autovacuum Tuning

PostgreSQL's MVCC architecture creates dead tuples during updates and deletes. The autovacuum process reclaims this space, but its default settings are conservative. For write-heavy tables, increase the autovacuum frequency:

ALTER TABLE high_write_table SET (
    autovacuum_vacuum_scale_factor = 0.02,
    autovacuum_analyze_scale_factor = 0.01,
    autovacuum_vacuum_cost_delay = 5
);

Monitoring and Observability

Use the pg_stat_statements extension to track query performance over time. It records execution counts, total and mean execution time, and buffer usage for every distinct query. Combine this with tools like Prometheus and Grafana for real-time dashboards. Monitor key metrics: cache hit ratio (should be above 99%), transaction rate, replication lag, and lock contention.

PostgreSQL performance tuning is an iterative process that requires understanding your workload, measuring before and after each change, and resisting the temptation to apply cargo-cult configurations. If you need expert guidance tuning your PostgreSQL deployment, reach out to our team.