379 points · by ibobev · 1mo ago · 38 comments
The Internals of PostgreSQL - https://news.ycombinator.com/item?id=43241404 - March 2025 (40 comments)
The Internals of PostgreSQL - https://news.ycombinator.com/item?id=30372750 - Feb 2022 (19 comments)
The Internals of PostgreSQL - https://news.ycombinator.com/item?id=30086374 - Jan 2022 (39 comments)
Internals of PostgreSQL - https://news.ycombinator.com/item?id=18950460 - Jan 2019 (25 comments)
The internals of PostgreSQL - https://news.ycombinator.com/item?id=13488315 - Jan 2017 (53 comments)
The Internals of PostgreSQL - https://news.ycombinator.com/item?id=12142364 - July 2016 (1 comment)
(Reposts are fine after a year or so; links to past threads are just to satisfy extra-curious readers)
If you are just skimming this doc, The HoT update path is the section I would jump to.
Understand HoT and then read the zHeap implementation notes (also of TOAST) puts a good mental model of what postgres does and what it doesn't do.
Wow so ahead of their time.
(And since I'm bad at sarcasm and don't wish to mislead, SSI = Serializable Snapshot Isolation in this context.)
bigint, bigint, bool, bool: 18 bytes (8+8+1+1), aligned up to 24
bool, bigint, bool, bigint: 32 bytes (1 + 7 (alignment padding) + 8 + 1 + 7 + 8)
But wait! There is the per-row overhead of ~30bytes. Your 18 byte row actually takes 54bytes of space. Well, multiples of 54 depending on how often you update/vacuum it. But don't make your rows too big either or you're TOASTAt runtime, if you've allocated a boolean or want to store a byte somewhere, you've either done it on the stack (it's temporary and you know it will need to be accessed at least once as an independent unit of data, and packing it together with other small data structures will make you use an extra instruction to extract it into its own register anyway, so there is not really any benefit to packing them tighter) or heap (you might actually gain something by reducing memory usage here, but packing data together binds the lifecycle and structure of everything at the same address and either requires additional lookups to check the interior structure, or a fancy compiler/build process to bake in the instruction to extract packed data without knowing what other data is packed with it).
When you read serialized data for a record from the disk, while running a typed database, the lifecycle/structure within the record is already shared. Most times you load the record you probably won't be accessing any particular packed data structure (eg if you have 5 bool columns in a table, most queries won't use all or any of them). You already have a global way to handle internal types/optimized reads, because that's what databases do.
So it would actually be feasible to load packed data and efficiently access or calculate with it eg XOR against 0x00..<single bit flipped to match bool index in word>00 to use a bool, or bit shift to load a subword into a register. It would be at worst 1 instruction per read slower if implemented efficiently (the query planner can handle the type lookup/deciding how to do the optimized read, which amortizes and isn't the query bottleneck or even on the critical path).
BUT denser data = more cache coherency and higher read throughput (more rows/s). And if you're doing fancy bit operations and arithmetic to extract and perform calculations on the packed data, it might actually be easier to batch operations with SIMD. So you could get arguably higher performance + much less passive disk usage (saving actual money) by packing data better probably. I'm about 30% of the way towards convincing myself to try this
https://www.orioledb.com/This article about MVCC is also quite interesting: https://boringsql.com/posts/mvcc-bad-bad/
Compare Postgres and Oracle (block undo based) for what happens when the database is underprovisioned:
• Postgres: bottlenecks on CPU. The database falls behind on vacuuming and dies.
• Oracle: bottlenecks on free disk space. Individual long running statements, SNAPSHOT and READ ONLY transactions get cancelled with a "snapshot too old" error.
In Oracle the database continues running and the blast radius is limited to a single statement. Because it's the longest running statements that die first, that almost certainly means an asynchronous business report and not live user traffic - which is exactly what you'd hope for. Most businesses can tolerate a missing day in a report much better than customer-facing outages, and fixing it is just a single SQL statement if you have free disk space (adding more storage is easy due to the SAN).
That's much preferable! How many post mortems do we see on HN from some startup bemoaning a multi-hour outage caused by Postgres' MVCC choices? Quite a lot. Now think about how often do you recall reading a post mortem of an outage at some big bank, hospital, shipping port etc that traces back to Oracle's MVCC engine? I don't think I ever read one. The startup world likes to make fun of Big Enterprise, but they have a much better track record of database uptime because they stay away from vacuum-based MVCC.
Disclosure: I have a part time role in the Oracle DB group. Unapproved opinions may be found above, caveat emptor.
> That's the honest shape of the mitigation: HOT is opportunistic, arrives in steady state rather than on demand, and quietly costs you 30% of your table size in reserved space.
OrioleDB is friendlier to SSDs and is de facto "the other" MVCC of Postgres. OrioleDB may be still be "beta" but that hasn't stopped operators/users.
MariaDB has some alternative MVCC, one of them is particularly outstanding.
SSD endurance at scale has on and off been a major concern for me, so I empathize with that. Others deserve to know of OrioleDB, and Postgres more broadly is probably better off for it too.
https://www.uber.com/us/en/blog/postgres-to-mysql-migration/Now I’m building Lavik, a new cache service for PostgreSQL. Traditional caches like Redis are DRAM-based, so they can only hold a small slice of hot data. Once your data outgrows memory, you’re exposed to cache miss storms, where a flood of requests falls through to PostgreSQL all at once. Nobody likes those. (OpenAI encounter the similar cache miss storms problem: https://openai.com/index/scaling-postgresql/)
Lavik takes a different path: it runs on NVMe SSDs, and it’s still faster than Redis running on DRAM. That’s not what most people expect. Conventional wisdom says SSD-based systems trade speed for capacity, but Lavik gives you both. You can keep TB-scale data in cache while getting 1M QPS and sub-millisecond P99 latency.
Lavik is open source under the Apache License. GitHub: https://github.com/eloqdata/lavik Website: https://lavik.dev
Also going faster than Redis doing only GET and SET is fairly easy, the very very very early versions of cachegrand (pre 0.1) were doing well over 100M/s of GET and SET on a 32 core / 64 threads machine with pipelining.
Once you start to chuck the rest to make it actually "usable" it's where you start to massively lose performances.
That doesn't mean the comment is useless, though. There's a whole philosophical discussion to be had here about whether what matters is the comment or the identity of the speaker.