&gt; On EXPLAIN, if you&#x27;ve passed my example PLANS=3 it would first do the plan in its usual way, and then try optimizing several more times, with some chance of randomly making suboptimal decisions at various decision points. It would keep doing this until it either had enough plans or else was making essentially random decisions and still couldn&#x27;t find more.<p>You&#x27;d not get anything useful by doing that. If you really consider this as a dynamic programming problem, at which place in the &#x27;pyramid&#x27; of steps would you choose the worse plan? To quote the source:<p><pre><code>        &#x2F;*
	 * We employ a simple &quot;dynamic programming&quot; algorithm: we first find all
	 * ways to build joins of two jointree items, then all ways to build joins
	 * of three items (from two-item joins and single items), then four-item
	 * joins, and so on until we have considered all ways to join all the
	 * items into one rel.
	 *
</code></pre>
If you&#x27;d just make some random &#x27;bad&#x27; decisions, you&#x27;ll not have a significant likelihood of finding actually useful good plans.<p>&gt; As for the PLAN command, I do not see the problem. I can look at the query and the EXPLAIN output, and I can figure out exactly how and where that query&#x27;s conditions are being incorporated. You might need extra output from EXPLAIN to make it always possible to do in an automated way, but it should be possible.<p>Good luck. Besides generating all possible plans postgres knows to generate, you very quickly essentially run into something equivalent to the halting problem.<p>&gt; Put another way, you propose that the database has a way to look at the query and a plan stored in the table and figure out how to execute that plan for that query.<p>What I&#x27;m proposing is matching on the parse tree of the user supplied query. For each query there&#x27;s <i>exactly one</i> parsetree the postgres parser will generate. We have a way (for the awesome pg_stat_statements module) of building a &#x27;hash&#x27; of that parse tree, and thus can build a fairly efficient mapping of parsetrees to additional data.  In contrast to that, for plans you can have a humongous number of plans for each user supplied query.<p>&gt; What would that table contain that couldn&#x27;t be represented as a chunk of text supplied with a PLAN command?<p>It would contain a, less ambiguous, version of the user supplied query. Now you could argue that you could add that to the PLAN command for matching purposes - but then we&#x27;d need guarantee that you could supply arbitrarily corrupt plan trees to postgres, without being able to cause harm. Something that&#x27;d cause significant slowdown during execution.<p>EDIT: Formatting