Applied cleanly for me. Here are my performance test results:
count
---------
3000000
(1 row)
Time: 3167.836 ms (00:03.168)
count
----------
10100000
(1 row)
Time: 6074.369 ms (00:06.074)
count
-------
10001
(1 row)
Time: 8.159 ms
The performance improves substantially in case 2 as advertised:
count
---------
3000000
(1 row)
Time: 3387.612 ms (00:03.388)
count
----------
10100000
(1 row)
Time: 43794.224 ms (00:43.794)
count
-------
10001
(1 row)
Time: 1436.587 ms (00:01.437)
I'll do some more testing to determine how this behaves in the presence of multibyte characters in UTF-8.