From 6c85189eda913596d4fa1c32b92eefcbd7532b4d Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Thu, 5 Apr 2018 17:20:42 +0000
Subject: [PATCH v8 06/12] Create a helper function for determining the log
 level for skipped relations.

---
 src/backend/commands/analyze.c | 45 +++++++++++-----------------------
 src/backend/commands/vacuum.c  | 55 +++++++++++++++++++++++++-----------------
 src/include/commands/vacuum.h  |  1 +
 3 files changed, 48 insertions(+), 53 deletions(-)

diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index f27b5bd535..3db80c7d51 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -159,38 +159,21 @@ analyze_rel(Oid relid, RangeVar *relation, int options,
 	 */
 	if (!onerel)
 	{
-		/*
-		 * If the RangeVar is not defined, we do not have enough information
-		 * to provide a meaningful log statement.  Chances are that
-		 * analyze_rel's caller has intentionally not provided this
-		 * information so that this logging is skipped, anyway.
-		 */
-		if (relation == NULL)
-			return;
-
-		/*
-		 * Determine the log level.  For autovacuum logs, we emit a LOG if
-		 * log_autovacuum_min_duration is not disabled.  For manual ANALYZE,
-		 * we emit a WARNING to match the log statements in the permissions
-		 * checks.
-		 */
-		if (!IsAutoVacuumWorkerProcess())
-			elevel = WARNING;
-		else if (params->log_min_duration >= 0)
-			elevel = LOG;
-		else
-			return;
+		elevel = get_elevel_for_skipped_relation(relation, params);
 
-		if (!rel_lock)
-			ereport(elevel,
-					(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
-					 errmsg("skipping analyze of \"%s\" --- lock not available",
-							relation->relname)));
-		else
-			ereport(elevel,
-					(errcode(ERRCODE_UNDEFINED_TABLE),
-					 errmsg("skipping analyze of \"%s\" --- relation no longer exists",
-							relation->relname)));
+		if (elevel != 0)
+		{
+			if (!rel_lock)
+				ereport(elevel,
+						(errcode(ERRCODE_LOCK_NOT_AVAILABLE),
+						 errmsg("skipping analyze of \"%s\" --- lock not available",
+								relation->relname)));
+			else
+				ereport(elevel,
+						(errcode(ERRCODE_UNDEFINED_TABLE),
+						 errmsg("skipping analyze of \"%s\" --- relation no longer exists",
+								relation->relname)));
+		}
 
 		return;
 	}
diff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.c
index d539e33b8b..1e70806a51 100644
--- a/src/backend/commands/vacuum.c
+++ b/src/backend/commands/vacuum.c
@@ -1393,28 +1393,7 @@ vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params)
 	 */
 	if (!onerel)
 	{
-		int			elevel = 0;
-
-		/*
-		 * Determine the log level.
-		 *
-		 * If the RangeVar is not defined, we do not have enough information
-		 * to provide a meaningful log statement.  Chances are that
-		 * vacuum_rel's caller has intentionally not provided this information
-		 * so that this logging is skipped, anyway.
-		 *
-		 * Otherwise, for autovacuum logs, we emit a LOG if
-		 * log_autovacuum_min_duration is not disabled.  For manual VACUUM, we
-		 * emit a WARNING to match the log statements in the permission
-		 * checks.
-		 */
-		if (relation != NULL)
-		{
-			if (!IsAutoVacuumWorkerProcess())
-				elevel = WARNING;
-			else if (params->log_min_duration >= 0)
-				elevel = LOG;
-		}
+		int			elevel = get_elevel_for_skipped_relation(relation, params);
 
 		if (elevel != 0)
 		{
@@ -1724,3 +1703,35 @@ vacuum_delay_point(void)
 		CHECK_FOR_INTERRUPTS();
 	}
 }
+
+/*
+ * get_elevel_for_skip_locked
+ *
+ * This is a helper function for determining the appropriate logging level for
+ * messages related to SKIP LOCKED or concurrently dropped relations.
+ *
+ * If the RangeVar is not defined, we do not have enough information to provide
+ * a meaningful log statement, and 0 is returned.  Chances are that the caller
+ * has intentionally not provided this information so that the logging is
+ * skipped anyway.
+ *
+ * Otherwise, for autovacuum logs, we emit a LOG if log_autovacuum_min_duration
+ * is not disabled.  For manual commands, we emit a WARNING to match the log
+ * statements in the permission checks for VACUUM and ANALYZE.
+ */
+int
+get_elevel_for_skipped_relation(RangeVar *relation, VacuumParams *params)
+{
+	Assert(params != NULL);
+
+	if (relation == NULL)
+		return 0;
+
+	if (!IsAutoVacuumWorkerProcess())
+		return WARNING;
+
+	if (params->log_min_duration >= 0)
+		return LOG;
+
+	return 0;
+}
diff --git a/src/include/commands/vacuum.h b/src/include/commands/vacuum.h
index 69b1e384ef..64215ea60a 100644
--- a/src/include/commands/vacuum.h
+++ b/src/include/commands/vacuum.h
@@ -186,6 +186,7 @@ extern void vacuum_set_xid_limits(Relation rel,
 					  MultiXactId *mxactFullScanLimit);
 extern void vac_update_datfrozenxid(void);
 extern void vacuum_delay_point(void);
+extern int	get_elevel_for_skipped_relation(RangeVar *relation, VacuumParams *params);
 
 /* in commands/vacuumlazy.c */
 extern void lazy_vacuum_rel(Relation onerel, int options,
-- 
2.16.2

