Avoid rescanning prefixes in appendStringLiteralDQ()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Avoid rescanning prefixes in appendStringLiteralDQ()
Date: 2026-11-19 06:32:29
Message-ID: 6E01C9AD-0446-4C2C-BA28-375FC4DA800A@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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], 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/

Attachment Content-Type Size
v1-0001-Avoid-rescanning-prefixes-in-appendStringLiteralD.patch application/octet-stream 1.6 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Zhijie Hou 2026-11-19 06:32:37 Re: Detect mismatched subscription and replication slot properties
Previous Message Xuneng Zhou 2026-11-19 06:26:29 Re: test: avoid redundant standby catchup in 049_wait_for_lsn