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]
diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c
index c644cb2cd50..f626d195664 100644
--- a/src/backend/nodes/read.c
+++ b/src/backend/nodes/read.c
@@ -77,6 +77,23 @@ stringToNodeWithLocations(const char *str)
  *
  *****************************************************************************/
 
+/* Character classes for pg_strtok() */
+#define TOKCLASS_SPACE         0x01    /* ' ', '\n', '\t' */
+#define TOKCLASS_SPECIAL       0x02    /* '(', ')', '{', '}' */
+#define TOKCLASS_NUL           0x04    /* '\0' */
+
+/* Character class lookup table for pg_strtok() */
+static const uint8 pg_strtok_class[256] = {
+       ['\0'] = TOKCLASS_NUL,
+       [' '] = TOKCLASS_SPACE,
+       ['\n'] = TOKCLASS_SPACE,
+       ['\t'] = TOKCLASS_SPACE,
+       ['('] = TOKCLASS_SPECIAL,
+       [')'] = TOKCLASS_SPECIAL,
+       ['{'] = TOKCLASS_SPECIAL,
+       ['}'] = TOKCLASS_SPECIAL,
+};
+
 /*
  * pg_strtok --- retrieve next "token" from a string.
  *
@@ -123,7 +140,7 @@ pg_strtok(ReadNodeContext *ctx, int *length)
 
        local_str = ctx->str;
 
-       while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t')
+       while (pg_strtok_class[(unsigned char) *local_str] & TOKCLASS_SPACE)
                local_str++;
 
        if (*local_str == '\0')
@@ -138,8 +155,7 @@ pg_strtok(ReadNodeContext *ctx, int *length)
         */
        ret_str = local_str;
 
-       if (*local_str == '(' || *local_str == ')' ||
-               *local_str == '{' || *local_str == '}')
+       if (pg_strtok_class[(unsigned char) *local_str] & TOKCLASS_SPECIAL)
        {
                /* special 1-character token */
                local_str++;
@@ -147,11 +163,8 @@ pg_strtok(ReadNodeContext *ctx, int *length)
        else
        {
                /* Normal token, possibly containing backslashes */
-               while (*local_str != '\0' &&
-                          *local_str != ' ' && *local_str != '\n' &&
-                          *local_str != '\t' &&
-                          *local_str != '(' && *local_str != ')' &&
-                          *local_str != '{' && *local_str != '}')
+               while (!(pg_strtok_class[(unsigned char) *local_str] &
+                                (TOKCLASS_SPACE | TOKCLASS_SPECIAL | 
TOKCLASS_NUL)))
                {
                        if (*local_str == '\\' && local_str[1] != '\0')
                                local_str += 2;

Attachment: 01.5_partitions.sql
Description: Binary data

Reply via email to