From 963cb6ed7d27cd7112c4fc4b4fb138b63edf087e Mon Sep 17 00:00:00 2001
From: James Coleman <jtc331@gmail.com>
Date: Mon, 6 Apr 2020 20:45:57 -0400
Subject: [PATCH v1 2/2] Comment show_incremental_sort_info assumtions

It's not immediately obvious when reading this code that ignoring the
prefix group info is correct if there are no full groups, so add a
comment explaining the rationale.
---
 src/backend/commands/explain.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index b1a20eba27..b8fd542c9b 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -2884,6 +2884,15 @@ show_incremental_sort_info(IncrementalSortState *incrsortstate,
 	if (!es->analyze)
 		return;
 
+	/*
+	 * Since we never have any prefix groups unless we've first sorted a full
+	 * groups and transitioned modes (copying the tuples into a prefix group),
+	 * we don't need to do anything if there were 0 full groups.
+	 *
+	 * We still have to continue after this block if there are no full groups,
+	 * though, since it's possible that we have workers that did real work even
+	 * if the leader didn't participate.
+	 */
 	if (fullsortGroupInfo->groupCount > 0)
 	{
 		show_incremental_sort_group_info(fullsortGroupInfo, "Full-sort", true, es);
@@ -2915,6 +2924,13 @@ show_incremental_sort_info(IncrementalSortState *incrsortstate,
 			 */
 			fullsortGroupInfo = &incsort_info->fullsortGroupInfo;
 			prefixsortGroupInfo = &incsort_info->prefixsortGroupInfo;
+
+			/*
+			 * Since we never have any prefix groups unless we've first sorted
+			 * a full groups and transitioned modes (copying the tuples into a
+			 * prefix group), we don't need to do anything if there were 0 full
+			 * groups.
+			 */
 			if (fullsortGroupInfo->groupCount == 0 &&
 				prefixsortGroupInfo->groupCount == 0)
 				continue;
-- 
2.17.1

