> On Nov 24, 2026, at 07:44, David Rowley <[email protected]> wrote:
>
> pg_strtok() has been reported as being slow before, so this isn't
> news. There was a mention in [1] about changing the format to solve
> this issue. That hasn't happened yet.
>
> This slowness came up again for me in a report I received about slow
> pg_upgrades with a large number of partitions. I've attached a script
> which is a modified version of what I received in the report. I
> removed the foreign key part, which is the primary source of the
> slowness.
>
> Setting max_locks_per_transaction to 100k and using the attached
> script to create a schema, then doing the pg_upgrade, I see:
>
> pg_upgrade -d pgdata -D pgdata_new -b ~/pg/bin -B ~/pg/bin | ts
> '%Y-%m-%dT%H:%M:%S%z'
>
> 2026-11-24T08:17:18+1300 Restoring global objects in the new cluster
> ok
> 2026-11-24T08:25:31+1300 Restoring database schemas in the new cluster
> ok (8 min 13 sec)
>
> On watching perf top during "Restoring global objects in the new
> cluster", the worst I saw for pg_strtok was:
>
> 39.16% postgres [.] pg_strtok
> 11.33% libc.so.6 [.]
> __GI_____strtoll_l_internal
> 5.41% postgres [.] palloc0
> 4.85% postgres [.] parseNodeString
> 3.43% libc.so.6 [.]
> __GI_____strtoull_l_internal
> 3.30% postgres [.] nodeRead
> 2.55% postgres [.] AllocSetAlloc
> 2.01% postgres [.] _readConst
> 2.00% postgres [.] readDatum
> 1.74% postgres [.]
> _readPartitionBoundSpec
> 1.58% postgres [.]
> SearchCatCacheInternal
>
> The attached patch creates a little 256-byte lookup table to define
> some character classes and adjusts the complex while loop condition
> that was checking for special chars, replacing it with a single lookup
> to the character class table.
>
> I'm only getting a 5% performance increase, but since it's a fairly
> self-contained adjustment, it seems worthwhile.
>
> The patch gives:
>
> 2026-11-24T08:55:36+1300 Restoring global objects in the new cluster
> ok
> 2026-11-24T09:03:22+1300 Restoring database schemas in the new cluster
> ok (7 min 46 sec)
>
> and with the patch the highest I saw pg_strtok in perf top was:
>
> 30.02% postgres [.] pg_strtok
> 13.09% libc.so.6 [.]
> __GI_____strtoll_l_internal
> 6.55% postgres [.] palloc0
> 4.60% postgres [.] parseNodeString
> 4.25% libc.so.6 [.]
> __GI_____strtoull_l_internal
> 3.78% postgres [.] nodeRead
> 2.97% postgres [.] AllocSetAlloc
> 2.30% postgres [.] readDatum
> 2.22% postgres [.] _readConst
> 1.86% postgres [.]
> SearchCatCacheInternal
>
> Again, hardly revolutionary, but the patch is small and
> self-contained. Changing the format requires bigger discussion that I
> don't want to have here.
>
> David
>
> [1] https://www.postgresql.org/message-id/[email protected]
> <pg_strtok.patch><01.5_partitions.sql>
Yep, this is a simple optimization. By introducing a small lookup table, it
replaces multiple delimiter comparisons with a table lookup and a bitmask test
without changing the main logic.
In your test, the schema restoration time was reduced from 8 min 13 sec to 7
min 46 sec, roughly a 5.5% improvement.
I tested the change on my MacBook using your procedure. As MacOS doesn't
builtin provide the ts command, I just used /usr/bin/time to measure the test
run durations:
Master branch:
```
cndb % /usr/bin/time -p pg_upgrade -d . -D ../pgtest -b /usr/local/pgsql/bin -B
/usr/local/pgsql/bin
real 358.35
user 6.99
sys 52.45
```
With this patch:
```
real 334.41
user 7.00
sys 52.70
```
Roughly a 6.7% improvement, the result is consistent with yours. So, I agree
this is a worthwhile optimization change.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/