StormaticsStormatics

How to Cut PostgreSQL Cloud Costs

The cheapest way to cut your PostgreSQL cloud bill is to make the database do less work per transaction before you pay for a bigger instance. When we benchmarked this on AWS, tuning alone gave us 4 to 4.7 times more throughput on the same hardware and cut the cost per transaction by 75 to 78%.

This post pulls together two of our webinars: Reducing Cloud Costs by Optimizing PostgreSQL (July 2024) and Capacity Planning and Selecting the Right Instance Size (February 2025). PostgreSQL has shipped two major releases since then, and cloud providers now charge for old versions, so I’ve updated the advice for where things stand today.

For managers: our guide When PostgreSQL Performance Becomes a Business Risk shows the signs to watch for.

Scaling by credit card only delays the bill

Here’s the pattern I see again and again. The app works fine in testing. It goes to production, traffic grows, and queries slow down. The quickest fix is a bigger instance, so that’s what the team buys.

It works, for a while. Then the same inefficient queries catch up with the new hardware, and you upsize again. Each step costs more, and the underlying problem is still there. There’s no “make it fast” button in the cloud console. If you find one, please let me know!

What tuning did to our cost per transaction

We ran BenchBase (Resource Stresser workload, scale factor 80,000, 50 terminals, 3 hours per run) on three AWS m5d instances, first with default settings and then tuned. The annual cost covers the instance and its storage at 2024 AWS prices, with the same storage on all three.

InstanceSpecsCost per yearTPS untunedTPS tunedThroughput gainCost per million txns, untunedCost per million txns, tunedSaving
m5d.8xlarge32 vCPUs, 128 GB$34,3212148714.07x$5.09$1.2575.4%
m5d.4xlarge16 vCPUs, 64 GB$25,921903964.40x$9.13$2.0877.3%
m5d.xlarge4 vCPUs, 16 GB$19,622401864.65x$15.60$3.3578.5%
Bar chart: PostgreSQL tuning cut cost per million transactions on AWS m5d instances by 75.4% (8xlarge), 77.3% (4xlarge) and 78.5% (xlarge) compared with default settings
Bar chart: PostgreSQL tuning cut cost per million transactions on AWS m5d instances by 75.4% (8xlarge), 77.3% (4xlarge) and 78.5% (xlarge) compared with default settings

We do this tuning for clients.

Look at it from the other direction too. The tuned m5d.4xlarge handled 396 TPS, nearly twice what the untuned m5d.8xlarge managed, at about 24% less per year. The tuned m5d.xlarge got within 13% of the untuned m5d.8xlarge at 43% less per year. That’s the real saving: tuning lets you run on a smaller instance.

The gains came from the five areas below. After those, I’ll cover how to pick the right instance size once the database is efficient.

1. Find the queries that cost you money

You can’t fix what you can’t see. Start with pg_stat_statements. It ships with PostgreSQL but isn’t loaded by default in community builds; most managed services preload it, and you still need to run CREATE EXTENSION. It costs a small amount of CPU (we measured around 4%), which is well worth it.

Sort by total time first, because that’s where your CPU hours go:

SELECT query, calls, total_exec_time, mean_exec_time, stddev_exec_time
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

Then look at mean_exec_time for slow queries, calls for chatty ones, and stddev_exec_time for queries that are sometimes fast and sometimes not. For I/O-heavy queries, use shared_blk_read_time and shared_blk_write_time (called blk_read_time and blk_write_time before PostgreSQL 17). Keep in mind the numbers are cumulative since the last reset, so reset them or take snapshots if you want a time window.

Once you have a suspect, run EXPLAIN ANALYZE on it. A sequential scan over a large table with a selective filter, a sort that spills to disk, or row estimates that are far off from actual rows are the usual culprits. Wrong estimates usually mean stale statistics, so make sure ANALYZE runs regularly.

And watch out for locks. A long transaction that updates many rows makes every other session wait for it. Keep transactions short and touch rows in a consistent order.

2. Use indexes, but don’t overdo it

The right index turns a full table scan into a few page reads. B-tree is the default, and PostgreSQL also gives you composite, partial, covering (INCLUDE), hash and BRIN indexes. BRIN is a good fit for large, naturally ordered tables like time series, because it’s tiny.

Every index also costs you storage and write I/O, and in the cloud you pay for both. Skip indexes on small tables, on queries that read most of the table anyway, and on write-heavy tables with few reads. Find indexes nobody uses:

SELECT relname, indexrelname, pg_size_pretty(pg_relation_size(indexrelid)) AS size
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY pg_relation_size(indexrelid) DESC;

Webinar on demand: Automating PostgreSQL Index Tuning Using AI

New since my webinar: PostgreSQL 18 can use a multicolumn B-tree index even when your query doesn’t filter on its leading column (skip scan). That means you may be able to drop some of the extra indexes you created for that case.

3. Let PostgreSQL do its job

Many performance features just work: parallel query, heap-only tuple (HOT) updates, incremental sort and autovacuum. Your job is to not get in their way. Keep autovacuum on and tuned for your busiest tables, because table bloat is storage you pay for every month and pages your queries read for nothing.

The newer releases help here too. PostgreSQL 17 made vacuum use far less memory, and PostgreSQL 18 added asynchronous I/O, which the PostgreSQL project says reads from storage up to 3 times faster in some workloads. On cloud disks, where every read has network latency, that matters.

Related: Your Postgres Database Is Slow, and It Isn’t Postgres and PostgreSQL, NUMA, and the Cloud

4. Don’t overload a single node

If your primary is serving reports, analytics and the app at the same time, send the reads somewhere else. In our test, spreading a read-only pgbench workload (25 clients) across a three-node cluster raised throughput by 30%, from 372 to 486 TPS, and cut average latency from 67 ms to 51 ms.

Partitioning helps with large tables. A query for August reads only the August partition, indexes stay small, and dropping old data is a quick partition drop instead of a huge DELETE.

Check your connections too. Each PostgreSQL connection is a process with its own memory, and I’ve seen teams upsize just to hold hundreds of mostly idle connections. A connection pooler such as PgBouncer is often a cheaper fix than a bigger instance.

5. Tune parameters for your hardware and workload

Defaults are built to run anywhere, not to run well on your server. Managed services change some of them for you, so check what’s actually set before you change anything. The ones that matter most:

  • shared_buffers: the data cache. The community default is 128 MB; 25 to 40% of RAM is a common target.
  • work_mem: memory per sort or hash operation, 4 MB by default. Sorts spilling to disk tell you it’s too low. It’s per operation, so raise it carefully.
  • wal_buffers: up to 16 MB can help when many transactions commit at once.
  • effective_cache_size: tells the planner how much data is likely cached (it doesn’t allocate anything). Set it to what the server really has.
  • random_page_cost: defaults to 4.0, which assumes spinning disks. On SSD-backed cloud storage a lower value lets the planner pick index scans more often.

Expect some trial and error. Change one thing, measure, then move on.

Whitepaper: High-Performance PostgreSQL: A Practical Guide to Optimization

6. Size the instance for the workload you actually have

Once the database is efficient, pick the instance that fits it. Too big, and you pay for idle capacity every hour. Too small, and your users pay in slow pages, and you pay in lost revenue. Capacity planning just means making that choice on purpose.

Start with a baseline. Track CPU, memory, disk I/O and storage over time, next to pg_stat_statements, in something like Prometheus and Grafana. Then set it against what’s coming: your growth trend, seasonal peaks and the next big feature launch.

These are the rules of thumb we use:

  • Under 70% most of the time? You’re probably over-provisioned. Step down a size.
  • Over 80% regularly? Alert at 80%, and size up if it stays there.
  • Spikes? If they’re predictable, plan for them. If they’re not, look at the application before you buy hardware for the worst minute of the day.
  • Review every quarter. Demand changes, and an instance that fit last year may not fit now.

Match the instance family to the workload. Memory-optimized instances suit databases that need a big cache, compute-optimized suit CPU-heavy queries, and burstable instances are fine for dev and test but throttle or bill extra under steady load. Read the specs carefully too: a vCPU is usually a thread, not a full core, and a GiB is about 7% bigger than a GB.

Size storage for IOPS and growth, not just gigabytes. Data, indexes, WAL and backups all grow, and provisioned IOPS you never use are pure cost. Plan for failover as well: if a replica has to take over the primary’s load, size it like the primary. Test with pgbench or HammerDB before you commit, and keep spot instances for non-critical work like test environments.

I went much deeper on this in the capacity planning webinar.

What’s changed since 2024: old versions now cost money

This is the biggest change in cloud costs since my webinar. AWS now charges RDS Extended Support for PostgreSQL versions past their standard support date: $0.100 per vCPU-hour in years one and two (US East), on top of the instance. Multi-AZ standbys and read replicas are billed too, and reserved instance discounts don’t apply.

PostgreSQL 14 reached community end of life on November 12, 2026, and RDS starts charging Extended Support for it on March 1, 2027. A 16 vCPU Multi-AZ database is 32 billed vCPUs, which works out to about $28,000 a year for staying on an old version. Upgrading also gets you the newer performance work, so it pays twice.

Two more lessons on the bill itself:

  • Commit last. Reserved instances and savings plans are a good deal for stable workloads, but they lock in a size. Tune and right-size first, then commit.
  • Look beyond compute. Provisioned IOPS you never use, oversized storage, and replicas nobody reads from all show up on the bill.

Where to start

  1. Turn on pg_stat_statements and baseline CPU, memory, I/O and storage.
  2. Fix your top 10 queries by total time and their indexes, and drop indexes nobody uses.
  3. Check autovacuum, bloat and your connection count.
  4. Tune the main parameters for your instance size.
  5. Right-size: step down if you’re under 70%, and alert at 80%.
  6. Plan the upgrade off any version past end of life.
  7. Then commit to a reservation, and review every quarter.

Or have us do steps 1 to 5.

Database performance has a lot of moving parts. Optimize how your data is accessed before you scale by credit card. If you’re not sure whether your database has crossed from an engineering problem into a business one, our manager guide When PostgreSQL Performance Becomes a Business Risk shows the signs to watch for. And if you’d like us to find where your PostgreSQL spend is going, talk to a PostgreSQL expert.

Leave A Comment