From bbf0ebadaf3f79c388dfbc37d114be3d227cc426 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 11 Jun 2004 17:20:39 +0000
Subject: [PATCH] StrategyDirtyBufferList wasn't being careful to honor
 max_buffers limit. Bug is only latent given that sole caller is passing
 NBuffers, but it could bite someone in the rear someday.

---
 src/backend/storage/buffer/freelist.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c
index b4702e7ceb2..526d45d455f 100644
--- a/src/backend/storage/buffer/freelist.c
+++ b/src/backend/storage/buffer/freelist.c
@@ -12,7 +12,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/storage/buffer/freelist.c,v 1.44 2004/06/03 02:08:03 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/storage/buffer/freelist.c,v 1.45 2004/06/11 17:20:39 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -709,6 +709,7 @@ StrategyInvalidateBuffer(BufferDesc *buf)
 	 */
 	CLEAR_BUFFERTAG(buf->tag);
 	buf->flags &= ~(BM_VALID | BM_DIRTY);
+	buf->cntxDirty = false;
 	buf->bufNext = StrategyControl->listFreeBuffers;
 	StrategyControl->listFreeBuffers = buf->buf_id;
 }
@@ -757,8 +758,7 @@ StrategyDirtyBufferList(BufferDesc **buffers, BufferTag *buftags,
 	cdb_id_t1 = StrategyControl->listHead[STRAT_LIST_T1];
 	cdb_id_t2 = StrategyControl->listHead[STRAT_LIST_T2];
 
-	while ((cdb_id_t1 >= 0 || cdb_id_t2 >= 0) && 
-		   num_buffer_dirty < max_buffers)
+	while (cdb_id_t1 >= 0 || cdb_id_t2 >= 0)
 	{
 		if (cdb_id_t1 >= 0)
 		{
@@ -772,6 +772,8 @@ StrategyDirtyBufferList(BufferDesc **buffers, BufferTag *buftags,
 					buffers[num_buffer_dirty] = buf;
 					buftags[num_buffer_dirty] = buf->tag;
 					num_buffer_dirty++;
+					if (num_buffer_dirty >= max_buffers)
+						break;
 				}
 			}
 
@@ -790,6 +792,8 @@ StrategyDirtyBufferList(BufferDesc **buffers, BufferTag *buftags,
 					buffers[num_buffer_dirty] = buf;
 					buftags[num_buffer_dirty] = buf->tag;
 					num_buffer_dirty++;
+					if (num_buffer_dirty >= max_buffers)
+						break;
 				}
 			}
 
-- 
GitLab