Postgres 19 Repack: Faster, Built-In Table Bloat Cleanup

(snowflake.com)

130 points | by craigkerstiens 10 hours ago ago

21 comments

  • jzelinskie 8 hours ago

    I haven't used these extensions, but I like the direction of being able to extend Postgres by tailing its own internals (pg_squeeze approach) than triggers (pg_repack).

    We (the SpiceDB developers) took a similar approach to solve a different problem a few years ago: implementing strict serializability in Postgres[0]. By hooking into the transactions layer themselves (pg_xact), we could layer on our own MVCC reusing the real Postgres transactions to compute visibility and total ordering. When you can just tail the Postgres internals, you don't even need to implement a Postgres extension, which can keep it portable across locked-down, managed Postgres offerings.

    Do folks know of other clever implementations of behavior by "spying" on the Postgres internals?

    [0]: https://m.youtube.com/watch?v=_f6fHy4DWWM

  • nopurpose 4 hours ago

    They added plan hinting, but sadly not via comments. To use hinting an app needs to issue a `SET` command, which adds round trip latency for each query. Need to use quite unergonomic query batching/pipelining to negate that latency.

    • wantoncl 40 minutes ago

      > They added plan hinting, but sadly not via comments.

      Comments are comments, not syntax. Plan hints have syntax. They are different concepts, and mixing syntax inside comments is a great way to break things.

    • hans_castorp an hour ago

      > To use hinting an app needs to issue a `SET` command, which adds round trip latency for each query

      You can send the SET and the actual SQL command as a single String (delimited by ; obviously), so there is no additional round trip required.

      Additionally: there is the possibility to save such a plan advice to be applied without issuing a "SET pg_plan_advice.advice" before running the statement. So this can also be applied to queries that can't be changed in the source code. This is provided by the extension pg_stash_advice

      • nopurpose an hour ago

        > You can send the SET and the actual SQL command as a single String (delimited by ; obviously), so there is no additional round trip required.

        This works for simple protocol only, with drawbacks like no parameter binding or re-planning of every query even if repeated.

        • hans_castorp an hour ago

          Ah, right. Forgot about that.

          But I think the approach to decouple the actual "hint" from the query still makes more sense, than requiring a change to the original query to include it into the comments. I don't know if something like pg_stash_advice would be possible without that approach.

          • nopurpose 19 minutes ago

            They are orthogonal. You can still do pg_stash_advice based on a query id AND have plan hint embedded in the query for cases where stashed hint is not set.

            They solve different problems:

            - SET syntax, or query comment like in third-party pg_hint_plan extension, are for app developers to embed desired hint into the code

            - pg_stash_advice, or 'hint_plan.hints' table from pg_hint_plan, is a tool to get out of a pickle when query suddenly starts to misbehave and rebuilding and redeploying app with a new hint is not an option or will take too much time.

        • ndriscoll 28 minutes ago

          Doesn't it still support pipelining in the extended protocol though? So still no round trip?

          • nopurpose 24 minutes ago

            It does, but then we are back to my original complaint: pipelining (or query batching) has quite less ergonomic API, at least in PostgreSQL drivers I worked with, comparing to a simple query execution.

    • stingraycharles 2 hours ago

      Huh, I thought most SET commands were handled entirely client-side? Eg setting character encoding doesn’t require a round trip.

  • inkysigma 7 hours ago

    Isn't losing MVCC consistency a pretty big drawback? Am I misinterpreting or does that mean occasionally a query might just return nothing for possibly difficult to diagnose reasons?

  • aurareturn 7 hours ago

    Trying to recoup space at a slow period, I remember using Repack on New Years Eve on my local computer for a multi TB production DB and waiting hours and hours hoping my connection doesn’t drop. When it dropped, I had to restart all over again.

    I thought it would be a simple process but it ended up taking many hours and a few retries. Holy hell it was a miserable experience and it ruined my New Years Eve.

    I’m so happy this is now built in.

    • craigkerstiens 7 hours ago

      I feel like that whole experience deserves a blog post.

      pg_repack, pg_squeeze, pg_reorg, all felt like it was "use as directed" and if you misuse them we're sorry if we corrupt data - that's a little extreme but they were still very advanced tools.

      Having this in core and learning from all the previous Postgres paths is the way of Postgres. I'm with you couldn't be more excited to have this in core, to me it was always the flagship feature and I'm just glad it stayed in 19 vs. a revert compared to so many other flagship features.

    • lefra 7 hours ago

      Before long-running commands, open a screen [1] session. That way, if you get disconnected, the job keeps running, and you can reconnect to the screen session afterwards.

      The man page is quite long, use "screen -R <appropriate name" to create the session or reconnect to it, and (IIRC) CTRL-A D to exit screen without closing the session.

      [1] https://man7.org/linux/man-pages/man1/screen.1.html

      • pezezin 3 hours ago

        Nowadays screen is considered legacy and it is recommended to use tmux, but the idea is the same.

        • DaanDeMeyer an hour ago

          systemd-run is more likely to be installed and another way of achieving this.

          • pezezin a minute ago

            Can it work in interactive mode like screen and tmux?

      • aurareturn 7 hours ago

        It was my first time doing it. I didn’t know it’d be such a frustrating experience.

    • HackerThemAll an hour ago

      > hoping my connection doesn’t drop

      You don't perform resource-intensive operations on a big database over unreliable remote connections. Find another way to be closer to the server, ideally on the same machine, or the same LAN at least.

  • dap 7 hours ago

    Having worked on clusters where bloat was an ongoing serious problem, this looks really useful! Thanks for doing this work.

  • QuentinM 6 hours ago

    I would have loved REPACK CONCURRENTLY when we were generating millions of dead tuples an hour on our trading database …

    Sadly it doesn’t support partitions yet, which is typical when reaching sufficient scale that bloat management really matters. People often mention disk space when talking about dead tuples, but the real killer is what’s hidden under - bloat in the ctid & btree leaf linked lists make every lookup feel like table scans…!

    Thankfully HOT (Heap-only Tuples) has saved us since. Virtually no dead tulles generated, and they get otherwise collected during regular ops like SELECT, amortizing cost over time, and preventing dangerous build up.