Avoid rescanning prefixes in appendStringLiteralDQ()

Started by Chao Li5 days ago3 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

While reviewing another patch, I spotted a trivial optimization in appendStringLiteralDQ().

Looking at the code:
```
/*
* Make sure we choose a delimiter which (without the trailing $) is not
* present in the string being quoted. We don't check with the trailing $
* because a string ending in $foo must not be quoted with $foo$.
*/
while (strstr(str, delimBuf->data) != NULL)
{
appendPQExpBufferChar(delimBuf, suffixes[nextchar++]);
nextchar %= sizeof(suffixes) - 1;
}
```

The while loop always searches from the beginning of str. But actually, a later search can just start from the previously returned pointer. This function is currently only used by pg_dump. I don’t expect a noticeable performance improvement in typical use, but it seems to me to be a tiny but harmless optimization.

I recently made a similar optimization [1]https://git.postgresql.org/cgit/postgresql.git/commit/?id=a999971331301a4a2f945220733b11af90cc2962, where the old code called strcat() in a loop, always scanning the destination string from the beginning.

See the attached patch details.

[1]: https://git.postgresql.org/cgit/postgresql.git/commit/?id=a999971331301a4a2f945220733b11af90cc2962

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t259439_1
v1-0001-Avoid-rescanning-prefixes-in-appendStringLiteralD.patchapplication/octet-stream; name=v1-0001-Avoid-rescanning-prefixes-in-appendStringLiteralD.patch; x-unix-mode=0644Download+2-2
#2Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Chao Li (#1)
Re: Avoid rescanning prefixes in appendStringLiteralDQ()

On 19/11/2026 08:32, Chao Li wrote:

While reviewing another patch, I spotted a trivial optimization in
appendStringLiteralDQ().

Looking at the code:
```
/*
* Make sure we choose a delimiter which (without the trailing $) is not
* present in the string being quoted. We don't check with the trailing $
* because a string ending in $foo must not be quoted with $foo$.
*/
while (strstr(str, delimBuf->data) != NULL)
{
appendPQExpBufferChar(delimBuf, suffixes[nextchar++]);
nextchar %= sizeof(suffixes) - 1;
}
```

The while loop always searches from the beginning of str. But
actually, a later search can just start from the previously returned
pointer. This function is currently only used by pg_dump. I don’t
expect a noticeable performance improvement in typical use, but it
seems to me to be a tiny but harmless optimization.

Makes sense. There's a similar loop in pg_get_functiondef(), too. I
applied the same optimization there, and pushed.

- Heikki

#3Chao Li
li.evan.chao@gmail.com
In reply to: Heikki Linnakangas (#2)
Re: Avoid rescanning prefixes in appendStringLiteralDQ()

On Nov 19, 2026, at 19:43, Heikki Linnakangas <hlinnaka@iki.fi> wrote:

On 19/11/2026 08:32, Chao Li wrote:

While reviewing another patch, I spotted a trivial optimization in
appendStringLiteralDQ().
Looking at the code:
```
/*
* Make sure we choose a delimiter which (without the trailing $) is not
* present in the string being quoted. We don't check with the trailing $
* because a string ending in $foo must not be quoted with $foo$.
*/
while (strstr(str, delimBuf->data) != NULL)
{
appendPQExpBufferChar(delimBuf, suffixes[nextchar++]);
nextchar %= sizeof(suffixes) - 1;
}
```

The while loop always searches from the beginning of str. But
actually, a later search can just start from the previously returned
pointer. This function is currently only used by pg_dump. I don’t
expect a noticeable performance improvement in typical use, but it
seems to me to be a tiny but harmless optimization.

Makes sense. There's a similar loop in pg_get_functiondef(), too. I applied the same optimization there, and pushed.

- Heikki

Thanks a lot for applying this patch.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/