Hints and hazards: pg_plan_advice in PG19

4 points by eatonphil


mqudsi

Honestly, with the right indexes and multivariate statistics in place, the most important usage is probably a variant of the first to tell postgres which of the indexes to use for your query. Previously you really could only tune extremely coarse-grained and only indirectly correlated knobs like the cost of random page lookups to try and get postgres to prefer one index over the other, but now you can do

SET pg_plan_advice.advice = 'INDEX_SCAN(orders orders_customer_id_idx)';
SELECT * FROM orders WHERE customer_id = 50;

but for cases where the query is three screens long and relies on index expressions that don’t guarantee filtering out all matching records and may or may not do pre filtering.

But the problem with using it as a SET expression preceding the query rather than inline comments containing planner hints is that it becomes difficult to translate this to the real world in some cases. For example, for a query that performs an index lookup to seed some CTEs or joins then does a self-join at the end to retrieve additional columns initially omitted to speed up intermediate sorts, is there syntax to specify which of the table lookups you want the hint to apply to?