On 2018-Nov-30, Sergei Kornilov wrote:

> Hello
> 
> I can not build current HEAD cleanly. I got warning:
> 
> > postgres.c: In function ‘check_log_duration’:
> > postgres.c:2254:17: warning: ‘in_sample’ may be used uninitialized in this 
> > function [-Wmaybe-uninitialized]
> >   if ((exceeded && in_sample) || log_duration)

Ah, I feared that some compiler would not be smart enough to be silent
about that.  I hope it does not emit a warning with this patch?

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 9a948f825d..5ab7d3cd8d 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -2244,12 +2244,13 @@ check_log_duration(char *msec_str, bool was_logged)
 		/*
 		 * Do not log if log_statement_sample_rate = 0. Log a sample if
 		 * log_statement_sample_rate <= 1 and avoid unecessary random() call
-		 * if log_statement_sample_rate = 1.
+		 * if log_statement_sample_rate = 1.  But don't compute any of this
+		 * unless needed.
 		 */
-		if (exceeded)
-			in_sample = log_statement_sample_rate != 0 &&
-				(log_statement_sample_rate == 1 ||
-				 random() <= log_statement_sample_rate * MAX_RANDOM_VALUE);
+		in_sample = exceeded &&
+			log_statement_sample_rate != 0 &&
+			(log_statement_sample_rate == 1 ||
+			 random() <= log_statement_sample_rate * MAX_RANDOM_VALUE);
 
 		if ((exceeded && in_sample) || log_duration)
 		{

Reply via email to