# High Performance PostgreSQL for Rails: Off-by-one bug in \`SCRUB\_BATCHES\` procedure (page 65)

**URL:** <https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-bug-in-scrub-batches-procedure-page-65/183819>\
**Category:** PragProg Customers\
**Tags:** book-high-performance-postgresql-for-rails, errata\
**Created:** [13 January 2025 14:24 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-bug-in-scrub-batches-procedure-page-65/183819 "2025-01-13T14:24:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![douglasshuang](https://forum.devtalk.com/user_avatar/forum.devtalk.com/douglasshuang/32/6809_2.png) [@douglasshuang](https://forum.devtalk.com/u/douglasshuang)\
**Post date:** [13 January 2025 14:24 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-bug-in-scrub-batches-procedure-page-65/183819/1 "2025-01-13T14:24:15Z")

</div>

@andatki

The `SCRUB_BATCHES` procedure has an off-by-one bug that causes it to skip one row after each batch. Here is output showing the incorrect behavior:

```auto
owner@localhost:5432 rideshare_development# CALL SCRUB_BATCHES();
NOTICE: current_id: 1 - Number of rows updated: 1000
NOTICE: current_id: 1002 - Number of rows updated: 1000
NOTICE: current_id: 2003 - Number of rows updated: 1000
NOTICE: current_id: 3004 - Number of rows updated: 1000
NOTICE: current_id: 4005 - Number of rows updated: 1000
NOTICE: current_id: 5006 - Number of rows updated: 1000
NOTICE: current_id: 6007 - Number of rows updated: 1000
NOTICE: current_id: 7008 - Number of rows updated: 1000
NOTICE: current_id: 8009 - Number of rows updated: 1000
NOTICE: current_id: 9010 - Number of rows updated: 1000
NOTICE: current_id: 10011 - Number of rows updated: 1000
NOTICE: current_id: 11012 - Number of rows updated: 1000
NOTICE: current_id: 12013 - Number of rows updated: 1000
NOTICE: current_id: 13014 - Number of rows updated: 1000
NOTICE: current_id: 14015 - Number of rows updated: 1000
NOTICE: current_id: 15016 - Number of rows updated: 1000
NOTICE: current_id: 16017 - Number of rows updated: 1000
NOTICE: current_id: 17018 - Number of rows updated: 1000
NOTICE: current_id: 18019 - Number of rows updated: 1000
NOTICE: current_id: 19020 - Number of rows updated: 1000
NOTICE: current_id: 20021 - Number of rows updated: 190
CALL
owner@localhost:5432 rideshare_development# SELECT id, email FROM users WHERE id IN (1000, 1001, 1002);
  id | email
------+-----------------------------------------
 1000 | 6ddd3da1abbacd649a2853d9@email.com
 1001 | Jenee-Robel-driver-1000@email.com
 1002 | efa11a15129ea1bed93f96@email.com
(3 rows)

```

The bug is in this assignment statement at the end of the loop body:

```auto
current_id := current_id + batch_size + 1;

```

It should read as follows:

```auto
current_id := current_id + batch_size;

```

Here is the output after the correction:

```auto
owner@localhost:5432 rideshare_development# CALL SCRUB_BATCHES();
NOTICE: current_id: 1 - Number of rows updated: 1000
NOTICE: current_id: 1001 - Number of rows updated: 1000
NOTICE: current_id: 2001 - Number of rows updated: 1000
NOTICE: current_id: 3001 - Number of rows updated: 1000
NOTICE: current_id: 4001 - Number of rows updated: 1000
NOTICE: current_id: 5001 - Number of rows updated: 1000
NOTICE: current_id: 6001 - Number of rows updated: 1000
NOTICE: current_id: 7001 - Number of rows updated: 1000
NOTICE: current_id: 8001 - Number of rows updated: 1000
NOTICE: current_id: 9001 - Number of rows updated: 1000
NOTICE: current_id: 10001 - Number of rows updated: 1000
NOTICE: current_id: 11001 - Number of rows updated: 1000
NOTICE: current_id: 12001 - Number of rows updated: 1000
NOTICE: current_id: 13001 - Number of rows updated: 1000
NOTICE: current_id: 14001 - Number of rows updated: 1000
NOTICE: current_id: 15001 - Number of rows updated: 1000
NOTICE: current_id: 16001 - Number of rows updated: 1000
NOTICE: current_id: 17001 - Number of rows updated: 1000
NOTICE: current_id: 18001 - Number of rows updated: 1000
NOTICE: current_id: 19001 - Number of rows updated: 1000
NOTICE: current_id: 20001 - Number of rows updated: 210
CALL
owner@localhost:5432 rideshare_development# SELECT id, email FROM users WHERE id IN (1000, 1001, 1002);
  id | email
------+-----------------------------------------
 1000 | 4b44cfd50568b6a9c54@email.com
 1001 | fc7266ea448226ed25ccdc7f1@email.com
 1002 | 6fdbe4620e31b4464b1b5fd95c64c@email.com
(3 rows)

```

---

<div class="post-metadata">

**Author:** ![andatki](https://forum.devtalk.com/user_avatar/forum.devtalk.com/andatki/32/5332_2.png) [@andatki](https://forum.devtalk.com/u/andatki)\
**Post date:** [2 May 2025 19:36 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-bug-in-scrub-batches-procedure-page-65/183819/2 "2025-05-02T19:36:24Z")

</div>

Hi @douglasshuang. Thank you for the detailed bug report. I was able to verify what you’re saying and agree that the last assignment within the loop should not add 1. I took that off as you suggested and the print output matches what you have.

I’m submitting a fix and hopefully it makes it into a future edition of the book. Thanks again for taking the time to notice this and prepare a detailed write-up!

---

<div class="post-metadata">

**Author:** ![douglasshuang](https://forum.devtalk.com/user_avatar/forum.devtalk.com/douglasshuang/32/6809_2.png) [@douglasshuang](https://forum.devtalk.com/u/douglasshuang)\
**Post date:** [5 May 2025 13:51 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-bug-in-scrub-batches-procedure-page-65/183819/3 "2025-05-05T13:51:35Z")

</div>

My pleasure, Thank you!

---

<div class="post-metadata">

**Author:** ![andatki](https://forum.devtalk.com/user_avatar/forum.devtalk.com/andatki/32/5332_2.png) [@andatki](https://forum.devtalk.com/u/andatki)\
**Post date:** [12 July 2025 03:21 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-bug-in-scrub-batches-procedure-page-65/183819/4 "2025-07-12T03:21:18Z")

</div>

Closed on 12 July 2025 03:21 UTC


