Print a given subplan only once in EXPLAIN.

Enterprise / PostgreSQL - Tom Lane [sss.pgh.pa.us] - 11 July 2016 17:14 UTC

We have, for a very long time, allowed the same subplan (same member of the PlannedStmt.subplans list) to be referenced by more than one SubPlan node; this avoids problems for cases such as subplans within an IndexScan's indxqual and indxqualorig fields. However, EXPLAIN had not gotten the memo and would print each reference as though it were an independent identical subplan. To fix, track plan_ids of subplans we've printed and don't print the same plan_id twice. Per report from Pavel Stehule.

BTW: the particular case of IndexScan didn't cause visible duplication in a plain EXPLAIN, only EXPLAIN ANALYZE, because in the former case we short-circuit executor startup before the indxqual field is processed by ExecInitExpr. That seems like it could easily lead to other EXPLAIN problems in future, but it's not clear how to avoid it without breaking the "EXPLAIN a plan using hypothetical indexes" use-case. For now I've left that issue alone.

Although this is a longstanding bug, it's purely cosmetic (no great harm is done by the repeat printout) and we haven't had field complaints before. So I'm hesitant to back-patch it, especially since there is some small risk of ABI problems due to the need to add a new field to ExplainState.

In passing, rearrange order of fields in ExplainState to be less random, and update some obsolete comments about when/where to initialize them.

Report:

4d04299 Print a given subplan only once in EXPLAIN.
src/backend/commands/explain.c | 22 ++++++++++++++++++++--
src/include/commands/explain.h | 8 +++++---
2 files changed, 25 insertions(+), 5 deletions(-)

Upstream: git.postgresql.org


  • Share