From 28f3bdd55cd8784d0af4d53a4e88845782e2eaa0 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@2ndquadrant.com>
Date: Sat, 31 Oct 2020 02:07:20 +0100
Subject: [PATCH 2/2] minor tweaks

---
 src/backend/commands/explain.c    | 32 +++++++++++++++----------
 src/backend/executor/instrument.c | 39 ++++++++++++++++---------------
 2 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 72dae57ee2..fa8deac55a 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -1575,7 +1575,8 @@ ExplainNode(PlanState *planstate, List *ancestors,
 
 		if (es->format == EXPLAIN_FORMAT_TEXT)
 		{
-			if (nodeTag(plan) == T_NestLoop) {
+			if (nodeTag(plan) == T_NestLoop)
+			{
 				if (es->timing)
 					appendStringInfo(es->str,
 									 " (actual time=%.3f..%.3f min_time=%.3f max_time=%.3f min_rows=%.0f rows=%.0f max_rows=%.0f loops=%.0f)",
@@ -1599,8 +1600,10 @@ ExplainNode(PlanState *planstate, List *ancestors,
 		}
 		else
 		{
-			if (nodeTag(plan) == T_NestLoop) {
-				if (es->timing) {
+			if (nodeTag(plan) == T_NestLoop)
+			{
+				if (es->timing)
+				{
 					ExplainPropertyFloat("Actual Startup Time", "s", startup_ms,
 										 3, es);
 					ExplainPropertyFloat("Actual Total Time", "s", total_ms,
@@ -1617,7 +1620,8 @@ ExplainNode(PlanState *planstate, List *ancestors,
 			}
 			else
 			{
-				if (es->timing) {
+				if (es->timing)
+				{
 					ExplainPropertyFloat("Actual Startup Time", "s", startup_ms,
 										 3, es);
 					ExplainPropertyFloat("Actual Total Time", "s", total_ms,
@@ -1670,18 +1674,19 @@ ExplainNode(PlanState *planstate, List *ancestors,
 				continue;
 			startup_ms = 1000.0 * instrument->startup / nloops;
 			total_ms = 1000.0 * instrument->total / nloops;
-			min_t_ms = 1000.0 * planstate->instrument->min_t;
-			max_t_ms = 1000.0 * planstate->instrument->max_t;
+			min_t_ms = 1000.0 * instrument->min_t;
+			max_t_ms = 1000.0 * instrument->max_t;
 			rows = instrument->ntuples / nloops;
-			min_r = planstate->instrument->min_tuples;
-			max_r = planstate->instrument->max_tuples;
+			min_r = instrument->min_tuples;
+			max_r = instrument->max_tuples;
 
 			ExplainOpenWorker(n, es);
 
 			if (es->format == EXPLAIN_FORMAT_TEXT)
 			{
 				ExplainIndentText(es);
-				if (nodeTag(plan) == T_NestLoop) {
+				if (nodeTag(plan) == T_NestLoop)
+				{
 					if (es->timing)
 						appendStringInfo(es->str,
 										 "actual time=%.3f..%.3f min_time=%.3f  max_time=%.3f min_rows=%.0f rows=%.0f max_rows=%.0f loops=%.0f\n",
@@ -1705,8 +1710,10 @@ ExplainNode(PlanState *planstate, List *ancestors,
 			}
 			else
 			{
-				if (nodeTag(plan) == T_NestLoop) {
-					if (es->timing) {
+				if (nodeTag(plan) == T_NestLoop)
+				{
+					if (es->timing)
+					{
 						ExplainPropertyFloat("Actual Startup Time", "ms",
 											 startup_ms, 3, es);
 						ExplainPropertyFloat("Actual Total Time", "ms",
@@ -1723,7 +1730,8 @@ ExplainNode(PlanState *planstate, List *ancestors,
 				}
 				else
 				{
-					if (es->timing) {
+					if (es->timing)
+					{
 						ExplainPropertyFloat("Actual Startup Time", "ms",
 											 startup_ms, 3, es);
 						ExplainPropertyFloat("Actual Total Time", "ms",
diff --git a/src/backend/executor/instrument.c b/src/backend/executor/instrument.c
index b0748721c3..0198bb3a5a 100644
--- a/src/backend/executor/instrument.c
+++ b/src/backend/executor/instrument.c
@@ -118,8 +118,9 @@ InstrStopNode(Instrumentation *instr, double nTuples)
 
 /* Finish a run cycle for a plan node */
 void
-InstrEndLoop(Instrumentation *instr) {
-	double totaltime;
+InstrEndLoop(Instrumentation *instr)
+{
+	double		totaltime;
 
 	/* Skip if nothing has happened, or already shut down */
 	if (!instr->running)
@@ -133,34 +134,34 @@ InstrEndLoop(Instrumentation *instr) {
 
 	instr->startup += instr->firsttuple;
 	instr->total += totaltime;
-	if (instr->nloops == 0)					/* this is first loop */
+
+	/*
+	 * this is first loop
+	 *
+	 * We only initialize the min values. We don't need to bother with
+	 * the max, because those are 0 and the non-zero values will get
+	 * updated a couple lines later.
+	 */
+	if (instr->nloops == 0)
 	{
-		if (instr->min_t == 0)
-		{
-			instr->min_t = totaltime;		/* init min_t */
-		}
-		if (instr->min_tuples == 0)
-		{
-			instr->min_tuples = instr->tuplecount;	/* init min_tuples */
-		}
+		instr->min_t = totaltime;
+		instr->min_tuples = instr->tuplecount;
 	}
+
 	if (instr->min_t > totaltime)
-	{
 		instr->min_t = totaltime;
-	}
+
 	if (instr->max_t < totaltime)
-	{
 		instr->max_t = totaltime;
-	}
+
 	instr->ntuples += instr->tuplecount;
+
 	if (instr->min_tuples > instr->tuplecount)
-	{
 		instr->min_tuples = instr->tuplecount;
-	}
+
 	if (instr->max_tuples < instr->tuplecount)
-	{
 		instr->max_tuples = instr->tuplecount;
-	}
+
 	instr->nloops += 1;
 
 	/* Reset for next cycle (if any) */
-- 
2.26.2

