From a4d58727191419b89126f06f23237986cae966fc Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Fri, 8 Jun 2007 21:21:28 +0000
Subject: [PATCH] Disallow the cost balancing code from resulting in a zero
 cost limit, which causes a division-by-zero error in the vacuum code.  This
 can happen when there are more workers than cost limit units.

Per report from Galy Lee in
<200705310914.l4V9E6JA094603@wwwmaster.postgresql.org>.
---
 src/backend/postmaster/autovacuum.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 07bf40707e3..82a2bc71a0c 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.48 2007/06/08 21:09:49 alvherre Exp $
+ *	  $PostgreSQL: pgsql/src/backend/postmaster/autovacuum.c,v 1.49 2007/06/08 21:21:28 alvherre Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1599,7 +1599,11 @@ autovac_balance_cost(void)
 			int     limit = (int)
 				(cost_avail * worker->wi_cost_limit_base / cost_total);
 
-			worker->wi_cost_limit = Min(limit, worker->wi_cost_limit_base);
+			/*
+			 * We put a lower bound of 1 to the cost_limit, to avoid division-
+			 * by-zero in the vacuum code.
+			 */
+			worker->wi_cost_limit = Max(Min(limit, worker->wi_cost_limit_base), 1);
 
 			elog(DEBUG2, "autovac_balance_cost(pid=%u db=%u, rel=%u, cost_limit=%d, cost_delay=%d)",
 				 worker->wi_workerpid, worker->wi_dboid,
-- 
GitLab