The Internals of PostgreSQL

(interdb.jp)

312 points | by ibobev 15 hours ago

10 comments

  • dang 10 hours ago
    Related:

    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)

    • weitendorf 1 hour ago
      Didn't see alignment padding mentioned anywhere so I'll drop it here: did you know that the order of a table's columns affects how much space a record uses?

        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 TOAST
      • gopalv 15 hours ago
        The latest Jepsen report on particledb triggered a new found interest in the SSI implementation in postgres.

        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.

        • refset 13 hours ago
          Incidentally, the 2020 Jepsen report on Postgres found a long-standing bug in the SSI implementation [0]

          [0] https://news.ycombinator.com/item?id=41560556

          • hmartin 11 hours ago
            The had a safe super intelligence bug dating back to 2011?

            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.)

        • jon128 10 hours ago
          Didn't find the new MVCC in there.

            https://www.orioledb.com/
          • amw-zero 9 hours ago
            Because Postgres hasn't adopted that yet, and I'm not aware of any actual plan to adopt it? Tom Lane just did an interview where he said he doesn't see any world where they move away from dead tuples and vacuuming.
            • hans_castorp 6 hours ago
              For anyone interested, this is the interview with Tom Lane: https://www.snowflake.com/en/blog/engineering/30-years-of-po...

              This article about MVCC is also quite interesting: https://boringsql.com/posts/mvcc-bad-bad/

              • mike_hearn 4 hours ago
                The MVCC article is good but feels like it's trying too hard to be a "on one hand, on the other hand" kind of analysis. Postgres is the only database that uses its design because, as the article admits at the end, it has "operationally loud failure modes".

                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.

                • matharmin 5 hours ago
                  Thanks, that MVCC article was a great read.
                  • guenthert 2 hours ago
                    Yeah, I love those time-travellers' blogs. Wished only he'd tell us about next years' market movements instead of issues in Oracle's DB.
                  • akoboldfrying 3 hours ago
                    The boringsql article was very informative, if you can get past some flourishes typical of a coauthor we all know:

                    > 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.

                  • ksec 4 hours ago
                    Damn this is sad. I just wish mySQL development isn't stalled as it is.
                    • jon128 8 hours ago
                      Users of software often do not blindly do, or not do, whatever the upstream open source project does.

                      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/
                      • hans_castorp 6 hours ago
                        All of the problems Uber had, could have been solved back then, had they actually asked for help on the mailing list. Several of the issues they had have also been fixed/changed in Postgres itself as a reaction to that discussion.
                  • hubertzhang 7 hours ago
                    Back in 2018, when I was at Greenplum, my team worked through The Internals of PostgreSQL together alongside Andy Pavlo’s CMU database courses. It was invaluable for anyone working on the database kernel. Today, a beginner can get that same interactive learning experience simply by chatting with AI, much like the discussions our team had back then.

                    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

                    • favori995749721 10 hours ago
                      Chapter 12 (full-text search) quietly changed under this book in Postgres 18: FTS now reads its dictionaries and config through the cluster's default collation provider instead of always libc, so clusters on ICU or the new builtin PG_UNICODE_FAST can see different behavior in FTS and pg_trgm — the release notes explicitly tell you to reindex those indexes after pg_upgrade. Also in 18, LIKE with nondeterministic collations stopped being an error, which was the main reason people avoided case-insensitive ICU collations on searchable columns.
                      • getbrew 11 hours ago
                        The HOT chapter is the one with the most direct practical payoff. Two things worth knowing from it: HOT only kicks in when no indexed column changes and the new tuple fits on the same page, so an index on a frequently-updated column (status, updated_at) quietly disables it for every update to that table. And lowering fillfactor (say 80-90) on update-heavy tables leaves free space on each page so HOT can actually happen. Comparing n_tup_hot_upd to n_tup_upd in pg_stat_user_tables is a cheap way to see whether you're getting HOT updates before tuning anything.
                        • ComputerGuru 10 hours ago
                          Depends on how wide your columns are and how often your records are modified. Past a certain width and I’m not sure lowering fillfactor is a good idea.
                          • ak39 8 hours ago
                            Why does this comment sound AI generated?
                            • mike_hearn 5 hours ago
                              Because it probably was. Pangram say it's too short to be certain but they think 100% of the text is possibly AI.

                              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.

                            • hmokiguess 10 hours ago
                              I’ll be real, I use Postgres every day and I have no clue what you’re talking about
                          • blenser4 10 hours ago
                            [dead]
                            • yksalun 6 hours ago
                              [flagged]
                              • CEOatMindfries 4 hours ago
                                I have been developer since 2 years, from noob to beginner but I have used only two databases, one in MongoDB and second is pgsql. I have used others as well like firebase and redis and all but I am talking about in wide range. And it become super exciting for me to know internals of the pgsql.