logoalt Hacker News

Looking Forward to Postgres 19: Query Hints

210 pointsby jjgreenlast Friday at 3:15 PM40 commentsview on HN

Comments

zackmorristoday at 3:36 PM

I'm against this because hints should be a last resort in declarative programming languages like SQL.

Our productivity is proportional to our ability to recruit abstractions. The more we deal with pure concepts like relational algebra and data-driven development, the more bang we get for our buck.

If we get lost in the weeds having to worry about doing the planner's job, it's like we're paying a tax that doesn't need to exist.

This is why the syntactic sugar of Ruby, the async design patterns of JavaScript, the footgun avoidance best practices of C++, even the impure workarounds of functional languages, (all meant to improve developer and/or execution performance) don't really do it for me. They hint at avoidance of deeper understanding. Once we learn higher abstractions like copy-on-write, compare-and-swap, higher-order methods, etc, we start to see that languages pass the cost of their externalities on to us.

I'd prefer that Postgres move the opposite direction. For example, databases need a universal index that turns as many operations as possible into O(1) at the cost of memory, since resource prices tend to always fall on a long enough timescale. Stuff that works more like a content-addressable memory for ludicrous scaling. In other words, whatever it takes to make planner hints obsolete, is what Postgres maintainers should be putting their efforts into.

I guess a stopgap might be to have an automated way to profile an app during testing and generate planner hints for the main use cases. Or maybe be able to cache them to avoid cold start latency. But if my work ever requires me to deal with them directly, I'll be treating that as a code smell.

-

After writing this out, I realized that performance is an orthogonal concern to conceptual correctness. So a more appropriate phrasing might be that the planner is none of SQL's business. So technically, anyone's opinion on it is valid. In which case, we should choose the path of kindness. If allowing access to the planner saves someone's bacon, than we should allow it. But work to alleviate whatever pain necessitated its use in the first place.

alexpotatotoday at 11:15 AM

At a past job, I managed the DBA team that owned the books and records database for a major hedge fund.

Some quick stories about databases in general and type hints in particular:

1. Some of the joins involved 10+ tables AND had query hints

That sounds cool until you realize that the queries were designed for the planner from 3 versions ago

2. Was in multiple conversations with head of the DBA team and application owners that went something like this:

Developer: "This query hasn't changed in YEARS! Why is it not performing well now??"

Me: "Have you considered that maybe the data cardinality has changed?" and then I or the lead DBA would explain how database indices worked.

I mention the above only as a warning to folks newer to DBA land that some of these features can seem great but can also be a crutch. What should have happened is that the schema, indices or application code should have been reviewed by the app teams (with the DBAs) periodically to ensure assumptions were correct.

(This was several years ago so I'm curious what kind of impact the LLMs have had on this)

show 1 reply
lfittltoday at 12:08 AM

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

crimsonnoodle58yesterday at 11:20 PM

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

show 2 replies
trollbridgetoday at 2:27 AM

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

show 3 replies
cryptonectortoday at 4:09 AM

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

show 1 reply
shay_kertoday at 1:12 PM

I have a naive question: why did this take 15 years? I understand that good APIs need time and thoughtful design, but I struggle to understand why we couldn’t get to the same (or better) solution faster.

robertlagrantyesterday at 11:22 PM

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

tschellenbachtoday at 7:51 AM

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.

show 1 reply
bob1029today at 7:26 AM

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.

show 1 reply
simmschitoday at 8:06 AM

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.

show 2 replies
ksectoday at 11:54 AM

OT: This reminded me of OrioleDB, what happened to it?

show 1 reply
aeontechtoday at 3:05 AM

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!

jbellistoday at 12:44 AM

man, Tom Lane has hated query hints for literally decades

did he finally come around?

show 1 reply
cgnguyentoday at 7:33 AM

[flagged]

imJacktoday at 9:05 AM

[flagged]

EvanXuetoday at 2:40 AM

[flagged]

haeseongtoday at 4:12 AM

[dead]