The pganalyze team demonstrates using the pg_query open-source library to programmatically rewrite SQL queries and fix inefficient Postgres query plans. The article walks through practical examples including adding expressions to ORDER BY clauses to prevent index misuse, and transforming multiple OR conditions into ANY
Table of contents
Query rewrite 101Example #1 - Add +0 to ORDER BY to avoid index misuseExample #2 - Transform multiple OR clauses to ANYConclusionSort: