# High Performance PostgreSQL for Rails: Off-by-one length bug in \`SCRUB\_EMAIL\` (page 52)

**URL:** <https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-length-bug-in-scrub-email-page-52/237173>\
**Category:** PragProg Customers\
**Tags:** book-high-performance-postgresql-for-rails, errata\
**Created:** [11 March 2026 12:44 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-length-bug-in-scrub-email-page-52/237173 "2026-03-11T12:44:44Z")\
**Posts on this page:** 2\
**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:** [11 March 2026 12:44 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-length-bug-in-scrub-email-page-52/237173/1 "2026-03-11T12:44:44Z")

</div>

Hi @andatki,

This is a follow-up to my previous error report, **[High Performance PostgreSQL for Rails: Confusing `SUBSTR` arguments in `SCRUB_EMAIL` (page 52)](https://devtalk.com/t/high-performance-postgresql-for-rails-confusing-substr-arguments-in-scrub-email-page-52/179546). The `+ 1` after `GREATEST(…)` was necessary when we were passing 0 as the start index to `SUBSTR`, but now it’s incorrect and results in a scrubbed email address that is one character too long when the original email username has a length greater than or equal to 5. The example on Page 53 actually demonstrates the bug:**

```sql
SELECT SCRUB_EMAIL('bob-and-jane@gmail.com');
-- scrub_email
-- ------------------------
-- 2bffb502c463@gmail.com

```

“bob-and-jane@gmail.com” has length 22, whereas “2bffb502c463@gmail.com” has length 23:

```sql
owner@localhost:5432 rideshare_development# SELECT LENGTH('bob-and-jane@gmail.com'), LENGTH(SCRUB_EMAIL('bob-and-jane@gmail.com'));
 length | length
--------+--------
     22 | 23
(1 row)

```

Thank you,

Doug

---

<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:** [7 April 2026 19:34 UTC](https://forum.devtalk.com/t/high-performance-postgresql-for-rails-off-by-one-length-bug-in-scrub-email-page-52/237173/2 "2026-04-07T19:34:42Z")

</div>

Hi @douglasshuang Shoot! Well unfortunately it’s too late to fix it in the book. I do have the functions or similar ones here in the Rideshare source. If you’d like to create a PR it might help someone else who goes looking. This thread may also help them find it. [rideshare/db/functions at main · andyatkinson/rideshare · GitHub](https://github.com/andyatkinson/rideshare/tree/main/db/functions) Fortunately since the intent was to have similar sized data but scrubbed, if it’s one character too long of random text, at least it will be uniformly 1 character too long, so the proportions of lengths should stay the same. Thanks for continuing to be a reader and bug finder. This forum had gotten pretty quiet lately! 🙂
