Looking Forward to Postgres 19: Query Hints

(pgedge.com)

160 points | by jjgreen 4 days ago ago

23 comments

  • lfittl 10 hours ago

    Its also worth reading the original post by Robert Haas (the author of pg_plan_advice) on motivation/design: https://rhaas.blogspot.com/2026/03/pgplanadvice-plan-stabili...

    Also, I'll add my perspective: I think "EXPLAIN (PLAN_ADVICE)" is a key piece to making this a plan stability feature, not (just) a hinting feature. The extensibility/framework pg_plan_advice adds is a foundation, that over time will over time address the age-old "Postgres doesn't have hints" problem, even if the initial release doesn't check all the boxes yet, e.g. no way to use advice for adjusting row/join estimates.

    To give an example on extensibility: Some people that I've spoken to are asking "but why is it not a comment-style hint". There are reasons why Postgres didn't go that way for this release (comment parsing in core is non-existent today, and comments don't work correctly e.g. for functions), but its easy to write an extension that sets up an advisor hook to parse comments: https://github.com/pganalyze/pg_advice_comment

  • crimsonnoodle58 11 hours ago

    > How many of us have toggled enable_seqscan to off to force an index scan? Or thrown an OFFSET 0 into a subquery to prevent the planner from flattening it?

    enable_nestloop = off here.

    For us, joining many complex views quickly trips the planner up, so I'm really glad to see this.

    > They break on upgrades.

    The irony is so does the planner. I've seen queries working perfectly fine in older PG's suddenly run away in newer versions. So hints will actually bring stability.

    • da_chicken 8 hours ago

      The planner breaking on updates is common for almost all RDBMSs. They introduce optimizations that work great for 95% of customers, and some will just have queries that now act like cardinality is way off or covering indexes are missing.

      • mpyne 6 hours ago

        This issue was one of AWS's listed reasons for tending to prefer NoSQL style databases over "more performant" RDBMS, because of the more consistent worst-case performance, even if the result is worse average-case performance, which was important in their assumptions for scalability planning.

    • throwatdem12311 6 hours ago

      Every single time I’ve thought I’ve needed to try these it made it worse.

      Every time Claude tries to tell me to try these, it made it worse.

      Not once has it made it better for me. I’m doing materialized view refreshes with a billion rows, which is small enough maybe that this doesn’t come into play…but so far the planner knows best.

      If the database can’t make it fast with just smart joins and filtering then it’s the architecture that’s a problem, not the database.

      Usually the only thing I need to do is increase work_mem.

      • silon42 5 hours ago

        The hints are needed when you have a web scale / online transaction processing application, where you want to guarantee no table scan ever. Ideally, I'd want to rrun without statistics on the tables in that case (ie, no smart joins).

        • setr 2 hours ago

          honestly I dont want plan hinting so much as being able to ban table-scanning / nested loops / etc on specific tables, and be able to set that independent of actual table-size so I can coerce its failure in dev environments

          • silon42 2 hours ago

            +1 I agree... (except nested loops are rarely the problem, they are often the best/simplest alternative for join).

  • trollbridge 7 hours ago

    Shudder. Flashbacks to having to write optimiser hints in Oracle (and the resulting fun times when you'd upgrade the database, something would change, and your hints would make a query slower).

    • winrid 5 hours ago

      As opposed to not upgrading and the planner picking a plan at 3am that is slower?

    • jeffbee 7 hours ago

      I can't believe we're still doing this. You should be able to dictate the query execution without involving a planner.

      • timacles 6 hours ago

        I really don’t think you’re considering all the edge cases in such a scenario.

        Most devs have problems writing decent queries (in some situations), now you want to introduce writing the query execution plan into the mix ?

        • akoboldfrying 5 hours ago

          No one is taking away the existing automatic planning that works well 95% of the time. You're welcome to continue using that.

          The worst thing that could possibly happen is that you give it bad advice leading to slow queries, and then the obvious first step to fixing that is to drop the manual advice and see whether the automatic planner handles it better.

          It baffles me that PostgreSQL, which is so deeply customisable in almost every other way, resisted this form of customisability for so long. This is great news.

  • tschellenbach 2 hours ago

    Imagine you have a SAAS app. Microsoft and Apple are customers. You have a table of devices. Type is either windows or osx. How does postgres know how to handle the query devices, where type = OSX properly? How does it know that this matches ~0 or ~100 of rows depending on the customer?

    This is the main thing the planner doesn't handle well. Postgres was built before SAAS was as big. You have different distributions per customer, and thousands of customers. In most cases the query planner will guess right, but sometimes it will fail and scan millions of rows.

  • simmschi 2 hours ago

    FINALLY!

    I never understood the issues PG had with hints. Running a non-trivial DB with a non-trivial schema and scale is, well, non-trivial at all. At some point the DB stops being a black box and starts being a tool that you have to know inside and out to avoid performance issues.

    >The optimizer is usually smarter than you think.

    Except for when it isn't, and moves heavy calculation inside a nested loop inside a nested loop to avoid an index scan. Nothing is perfect.

    • williamdclt an hour ago

      > > The optimizer is usually smarter than you think.

      > Except for when it isn't, and moves heavy calculation inside a nested loop inside a nested loop to avoid an index scan. Nothing is perfect.

      Yeah that's also been my experience. It's true that Postgres is usually smarter than I think, when I try to figure out why it's not using a better query plan I eventually find out that it wouldn't be better at all. But from time to time, it genuinely is taking a bad decision and having no power over that at all is a problem

  • bob1029 2 hours ago

    Give the customer what they want, even if it sucks to do so. The alternative it be cast into irrelevance over time. You can run an OSS project however you want, but you can't avoid the consequences of doing so.

    Principles driven development (we will never or always do X regardless of context) typically comes off as a petty ego trip. The point of the technology is to serve some kind of downstream business. Most people who download Postgres are seeking to solve a real world problem, not to demonstrate their ideological purity.

  • robertlagrant 10 hours ago

    I'm not an expert in database hints, but the syntax looks very readable and composable. That's great thing to have got right.

  • cryptonector 6 hours ago

    > The advice language is surprisingly expressive for something the community resisted for decades.

    FINALLY!

    I like this design.

    And yes, the community resisted this for way too long.

  • aeontech 7 hours ago

    Very interesting - I just installed pg_hint_plan [0] extension a few months ago to get around a query that was confusing the planner too much. Edge case, but when you need it you really need it.

    Haven't seen pg_plan_advice before, TIL!

  • jbellis 9 hours ago

    man, Tom Lane has hated query hints for literally decades

    did he finally come around?

    • lfittl 9 hours ago

      I don't think Tom's perspective has necessarily changed (and there is certainly concern from others that this could cause less reports on planner bugs), but Tom is pretty good about not standing in the way of others (i.e. Robert Haas in this case) trying to make things work, and being open to new perspectives.

      I do know that one of the important criteria for getting this in was that a bad advice can't cause the planner to fail, and that's something that was explicitly included in the design of pg_plan_advice.