On 1/3/19, Amit Kapila <amit.kapil...@gmail.com> wrote:
> Yeah, that makes sense, John, can you provide a patch on top of the
> current patch where we check either the last block or every other
> block.

I've attached two patches for testing. Each one applies on top of the
current patch.

Mithun, I'll respond to your other review comments later this week.

-John Naylor
diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c
index 2ab1cb58b7..d6352aabd9 100644
--- a/src/backend/storage/freespace/freespace.c
+++ b/src/backend/storage/freespace/freespace.c
@@ -1103,15 +1103,9 @@ fsm_allow_writes(Relation rel, BlockNumber heapblk,
 static void
 fsm_local_set(Relation rel, BlockNumber curr_nblocks)
 {
-	BlockNumber blkno,
-				cached_target_block;
+	BlockNumber cached_target_block;
 
-	/*
-	 * Mark blocks available starting after the last block we have mapped,
-	 * and ending at the current last block in the relation.
-	 */
-	for (blkno = fsm_local_map.nblocks; blkno < curr_nblocks; blkno++)
-		fsm_local_map.map[blkno] = FSM_LOCAL_AVAIL;
+	fsm_local_map.map[curr_nblocks - 1] = FSM_LOCAL_AVAIL;
 
 	/* Cache the number of blocks. */
 	fsm_local_map.nblocks = curr_nblocks;
diff --git a/src/backend/storage/freespace/freespace.c b/src/backend/storage/freespace/freespace.c
index 2ab1cb58b7..820a2579ae 100644
--- a/src/backend/storage/freespace/freespace.c
+++ b/src/backend/storage/freespace/freespace.c
@@ -1110,8 +1110,15 @@ fsm_local_set(Relation rel, BlockNumber curr_nblocks)
 	 * Mark blocks available starting after the last block we have mapped,
 	 * and ending at the current last block in the relation.
 	 */
-	for (blkno = fsm_local_map.nblocks; blkno < curr_nblocks; blkno++)
+	blkno = curr_nblocks - 1;
+	for (;;)
+	{
 		fsm_local_map.map[blkno] = FSM_LOCAL_AVAIL;
+		if (blkno >= fsm_local_map.nblocks + 2)
+			blkno -= 2;
+		else
+			break;
+	}
 
 	/* Cache the number of blocks. */
 	fsm_local_map.nblocks = curr_nblocks;
diff --git a/src/test/regress/expected/fsm.out b/src/test/regress/expected/fsm.out
index 4c44c4bd6e..973b9319e7 100644
--- a/src/test/regress/expected/fsm.out
+++ b/src/test/regress/expected/fsm.out
@@ -2,16 +2,16 @@
 -- Free Space Map test
 --
 CREATE TABLE fsm_check_size (num int, str text);
--- Fill 2 blocks with as many large records as will fit
+-- Fill 3 blocks with as many large records as will fit
 -- No FSM
 INSERT INTO fsm_check_size SELECT g, rpad('', 1024, 'a')
-FROM generate_series(1,7*2) g;
+FROM generate_series(1,7*3) g;
 VACUUM fsm_check_size;
 SELECT pg_relation_size('fsm_check_size', 'main') AS heap_size,
 pg_relation_size('fsm_check_size', 'fsm') AS fsm_size;
  heap_size | fsm_size 
 -----------+----------
-     16384 |        0
+     24576 |        0
 (1 row)
 
 -- Clear some space on block 0
@@ -26,7 +26,7 @@ SELECT pg_relation_size('fsm_check_size', 'main') AS heap_size,
 pg_relation_size('fsm_check_size', 'fsm') AS fsm_size;
  heap_size | fsm_size 
 -----------+----------
-     16384 |        0
+     24576 |        0
 (1 row)
 
 -- Extend table with enough blocks to exceed the FSM threshold
diff --git a/src/test/regress/sql/fsm.sql b/src/test/regress/sql/fsm.sql
index fad99da1c2..a864c7fd88 100644
--- a/src/test/regress/sql/fsm.sql
+++ b/src/test/regress/sql/fsm.sql
@@ -4,10 +4,10 @@
 
 CREATE TABLE fsm_check_size (num int, str text);
 
--- Fill 2 blocks with as many large records as will fit
+-- Fill 3 blocks with as many large records as will fit
 -- No FSM
 INSERT INTO fsm_check_size SELECT g, rpad('', 1024, 'a')
-FROM generate_series(1,7*2) g;
+FROM generate_series(1,7*3) g;
 VACUUM fsm_check_size;
 SELECT pg_relation_size('fsm_check_size', 'main') AS heap_size,
 pg_relation_size('fsm_check_size', 'fsm') AS fsm_size;
@@ -16,7 +16,7 @@ pg_relation_size('fsm_check_size', 'fsm') AS fsm_size;
 DELETE FROM fsm_check_size where num <= 5;
 VACUUM fsm_check_size;
 
--- Insert small record in block 1 to set the cached smgr targetBlock
+-- Insert small record in block 2 to set the cached smgr targetBlock
 INSERT INTO fsm_check_size values(99, 'b');
 
 -- Insert large record and make sure it goes in block 0 rather than

Reply via email to