Make pg_strtok() a bit faster

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: PostgreSQL Developers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Make pg_strtok() a bit faster
Date: 2026-11-23 23:44:08
Message-ID: CAApHDvo-YKLqfHfjrCFJCBxMXni83Xsfn6Nbe79CWspCHKta_Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/464677.1738620525@sss.pgh.pa.us

Attachment Content-Type Size
pg_strtok.patch text/plain 2.0 KB
01.5_partitions.sql application/octet-stream 3.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Masahiko Sawada 2026-11-23 23:47:01 Re: [PATCH] Add hint to wal_level errors for replication slots and pg_log_standby_snapshot
Previous Message Manu 2026-11-23 23:39:00 Re: Bug in logical decoding with DDL and subtransactions