Hacker News new | ask | show | jobs
by hans_castorp 16 days 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

1 comments

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

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.

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.

Doesn't it still support pipelining in the extended protocol though? So still no round trip?
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.
And maybe it is time for the postgres drivers to expose better support for pipelining.

I hacked around our query builder to add support for it, and I wish I didn't have to. It feels like a problem that should have been solved 20 or 40 years ago.