Explanation of Loops in EXPLAIN ANALYZE

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: explain analyze 中的loops 解释

| username: Raymond

May I ask, in the explain analyze, the documentation explains that loops are the number of times the current operator is called by the parent operator.

  1. Why is the loops in my execution plan 2?
  2. StreamAgg_17 is already the root operator, so who is its parent operator? How is this loops calculated?
| username: xfworld | Original post link

As shown in the figure, indexReader also has loops 2, and there is push-down computation. The time consumed by streamAgg and indexReader is exactly the same. Basically, once indexReader is completed, this plan is considered completed as well.

For more details, you can refer to:

| username: 人如其名 | Original post link

  1. The reason why the loop in your red section is 2, as I understand (guess), is that the number of loops is actually the number of times Next is called, regardless of whether this Next successfully retrieves data or fails, because this loop increment occurs in the defer inside the Next function. When Next is called for the first time, it returns a row of records, but it does not end there. The next time Next is called, the underlying layer returns an error indicating no data, and then all operators are closed.

  2. StreamAgg does not have a parent operator. Its caller is the connection that continuously retrieves data, i.e., continuously calls StreamAgg’s Next to return chunk data.