How we used pg_query to rewrite queries to fix bad query plans
pganalyze explains how it uses the open source pg_query library to rewrite SQL queries programmatically for its new Query Advisor feature, which suggests alternative query patterns to fix bad query plans. pg_query parses queries into a parse tree that can be modified and regenerated as deterministic, syntactically valid SQL, avoiding the pitfalls of regex or AI-generated rewrites. Examples include adding +0 to ORDER BY to avoid index misuse and turning multiple OR clauses into ANY.
AI-written summary, checked against the cited sources. How summaries work