Search code examples
postgresqlsql-updateexplain

EXPLAIN UPDATE - side effects or not without ANALYZE?


It is well known that if you run an EXPLAIN ANALYZE on a query, the query will be executed (although the results -assuming a SELECT- will be discarded). So it makes perfect sense that if you run EXPLAIN ANALYZE on an UPDATE statement, the table at hand will be updated.

I understand you can mitigate this by either turning the whole thing into a transaction (as explained here in the documentation: https://www.postgresql.org/docs/9.1/sql-explain.html) or adding 1=0 to the WHERE clause (the latter possibly changing the query plan).

This statement in the documentation is ambiguous though:

"Important: Keep in mind that the statement is actually executed when the ANALYZE option is used. Although EXPLAIN will discard any output that a SELECT would return, other side effects of the statement will happen as usual"

Does this mean that if you run EXPLAIN without the ANALYZE keyword on an UPDATE statement, the side effects (i.e. the update) will occur? Or will they only occur if you add ANALYZE to the EXPLAIN? The first sentence in the quote suggests the latter, but the second sentence suggests the former (hence ambiguity). I know this is kind of an academic question, but I'd still love to know the answer.


Solution

  • a simple test in dbfiddle will answer your question.

    The ANALYSE key word forces the execution of the sql statement, not the EXPLAIN key word.