From a03bbdbdfe8b011d20dd670207db059af7b7f40d Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Thu, 20 Apr 2023 09:28:41 +0900 Subject: [PATCH] Fix query hint retrieval with utilities Query jumbling is possible in PostgreSQL 16 for utilities, generating a JumbleState that could involve a query normalization. This would break the retrieval of query hints for quite a few scenarios, we we should try to get the hints from the query planned, mostly around EXPLAIN: - EXPLAIN EXECUTE - EXPLAIN CTAS This commit patches the post parse-analyze hook to ignore entirely utility queries, unbreaking all the remaining regression tests. At this point, the regression tests are stable with PostgreSQL 16. Per pull request #130. --- pg_hint_plan.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pg_hint_plan.c b/pg_hint_plan.c index b1b1389c..ac5a2d4a 100644 --- a/pg_hint_plan.c +++ b/pg_hint_plan.c @@ -2985,6 +2985,17 @@ pg_hint_plan_post_parse_analyze(ParseState *pstate, Query *query, if (plpgsql_recurse_level == 0) current_hint_retrieved = false; + /* + * Query jumbling is possible with utility queries in PostgreSQL 16 and + * newer versions, hence just leave in this case. There could be a point + * in providing hints for some cases, like a CTAS or a matview creation, + * but that should not matter much in practice compared to SELECT and DML + * queries, and nobody has made a case for these now. So let's just + * ignore the case entirely for now. + */ + if (query->utilityStmt) + return; + /* * Jumble state is required when hint table is used. This is the only * chance to have one already generated in-core. If it's not the case, no