r/PostgreSQL • u/fykup • 21d ago
Projects PostgreSQL 18 on a 512 MiB VM: testing two low-memory configurations
I've been experimenting with PostgreSQL 18 tuning for memory-constrained VPS deployments, particularly where the database needs to leave enough RAM for a small application.
I initially tried running the experiment on a 256 MiB VM (~204 MiB usable RAM), but PostgreSQL with its default configuration ran into memory pressure and the kernel OOM-killed a backend during the smoke test. I moved to 512 MiB to complete the comparison.
Given the roughly 50 MiB peak PostgreSQL PSS measured with Theory A, I suspect this configuration could also work on 256 MiB, potentially alongside a small application. That's a hypothesis for a follow-up experiment, though - I haven't validated Theory A at 256 MiB yet.
I wanted to compare two approaches against PostgreSQL defaults:
- Theory A - Memory First: 16 MB
shared_buffers, 1 MBwork_mem, 16 MBmaintenance_work_mem, retaining PostgreSQL 18's default worker-based I/O. - Theory B - Lean Machinery: 32 MB
shared_buffers, defaultwork_mem, and synchronous I/O (io_method=sync).
Both configurations limit connections to 5 (one reserved for a superuser), disable JIT and parallel queries, and retain fsync, autovacuum, and standard WAL behavior.
Test setup: PostgreSQL 18.6, 1 vCPU, 512 MiB VM (~456 MiB usable), no swap, 222 MB dataset, and 4 concurrent connections running point, range, and aggregate queries.
Results:
| Metric | Default | A | B |
|---|---|---|---|
| Peak PSS | 180.1 MiB | 50.4 MiB | 79.7 MiB |
| Queries/s | 863 | 1,056 | 861 |
| Point p95 | 2.90 ms | 2.64 ms | 2.92 ms |
| Aggregate p95 | 98.55 ms | 74.25 ms | 103.18 ms |
What surprised me was that Theory A used about 72% less process memory than the default while also delivering higher throughput in this run.
PSS accounts for shared pages between PostgreSQL processes, so these numbers aren't total VPS memory consumption.
This was deliberately a small exploratory experiment: one 50-second measured run per configuration, fixed order, warm filesystem cache, and a read-only workload. No claim that A is universally faster or that this configuration is suitable for every application.
I published the configuration, methodology, scripts, and raw measurements for anyone interested in checking the results:
https://github.com/PVRLabs/experiments/tree/main/postgres/light-memory-p95
Short write-up with configuration instructions:
https://pvrlabs.xyz/java-performance/experiments/postgresql-low-memory.html
I'm curious whether others running PostgreSQL on small VPS instances have found similar benefits from reducing shared buffers and per-query memory, or whether the trade-offs become significantly worse with sustained writes and more complex queries.
Update (256 MiB follow-up): After the original comparison, I resized the VM back to 256 MiB (~204 MiB usable) and tested Theory A and B again. I skipped the default configuration, which had already failed with an OOM during the earlier smoke test.
Both configurations completed the workload with four active connections and no observed OOM, swap activity, or client errors.
| Metric | Theory A | Theory B |
|---|---|---|
| Peak PSS | 46.2 MiB | 70.2 MiB |
| Throughput | 769 q/s | 604 q/s |
| Point p95 | 3.58 ms | 4.00 ms |
Theory A again performed better in this short run. The database ran alone, so this doesn't yet establish whether an application can share the same 256 MiB VM.
3
u/Jonjolt 21d ago
Thanks for this, I was wondering how it would run.
1
u/DickBrainTime 21d ago
Interesting result, especially with the single warm-cache-run caveat. On a 256 MiB VM I'd look at peak memory with hash joins/sorts and concurrent writes, not just steady-state PostgreSQL PSS. work_mem is a budget per sort/hash operation rather than a per-connection cap; hash operations can use work_mem times hash_mem_multiplier (2 by default). Autovacuum needs its own memory too, so a few overlapping operations might expose a limit that the current read-only run doesn't hit.
For the follow-up, I'd run each config several times in randomized order, add a sustained small write load and a cold-cache pass, and watch cgroup v2 peak memory and OOM counters alongside PostgreSQL temp-file spill stats and p95 latency. That should help distinguish a promising low-memory baseline from something that's actually safe with an app sharing a 256 MiB VM.
1
u/Significant_Tune9219 21d ago
Interesting that A beat B on aggregate p95 even with work_mem at 1 MB, which suggests your aggregates were mostly hash or sort sized to fit anyway on a 222 MB dataset. Before trying 256 MiB I'd run one query that deliberately spills (a big ORDER BY or a hash join on unindexed columns) and watch temp_files in pg_stat_database, since that's where a 1 MB work_mem usually shows up. Also worth noting that autovacuum workers each get maintenance_work_mem (or autovacuum_work_mem), so capping autovacuum_max_workers at 1 might matter more at 256 than shared_buffers does.
1
u/raks1991 20d ago
A and B differ in three settings that matter for a read-only run (shared_buffers 16 vs 32 MB, work_mem 1 MB vs default, io_method worker vs sync), so the throughput gap can't be pinned on any one of them yet. A 2x2 on shared_buffers and io_method with work_mem held fixed would separate them, and that's only four more short runs.
Snapshotting pg_stat_io before and after each run would show the mechanism too:
With a 222 MB dataset and 16 MB of shared buffers, I'd expect lots of reads and evictions, with those reads served from the kernel page cache, which PSS doesn't count. That page cache is also what an app would be competing for on a 256 MiB box.
1
u/Hostman_com 19d ago
54 MiB available at the lowest point is a useful baseline. For the next run, try restarting the app on the same VM while it’s taking traffic. If the old and new processes overlap during a deploy, both can open DB connections. With four slots for normal clients in this setup, you could hit the connection limit while there’s still RAM left.
0
u/AutoModerator 21d ago
AI Policy:
Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.
Mod decisions will be based on the quality of the content, not who or what generated it.
Sub Resources:
Youtube Channel
Free Postgres Webinars and Workshops
Discord: People, Postgres, Data
Join us, we have cookies and nice people.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.