PostgreSQL – Side-Effects of Disabling Nested Loops

performancepostgresqlquery-performance

A query of multiple joins spends a lot of time on nested loops. I disabled the nested loops and the query ran much faster. However, I have been told that nested loops might give me wrong results by skipping some tuples. Is this true?

Best Answer

Using debugging parameters like enable_nestloop should not change the results of a query. If it does that'd generally be a bug.

However, you should not use enable_nestloop = off or similar in production. It's a very big hammer, and while it might appear to fix your immediate problem, it'll create performance problems elsewhere. A nested loop is often the correct plan node for best performance in a part of a query, and forcing PostgreSQL to prefer other options might make queries significantly slower.

Instead, you should generally adjust the cost parameters like random_page_cost to encourge PostgreSQL to choose a plan that performs better on your hardware and workload. For more information see the tuning advice on the PostgreSQl wiki and in the main documentation.