Simplify the way of appending comma to stringInfo

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Simplify the way of appending comma to stringInfo
Date: 2025-12-08 08:37:30
Message-ID: CAEoWx2=Sp4Mk9h8N1mm-5J9BZye2TgJ5NnnVFnF4oSCR7DmeSg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Hackers,

In a lot places, there are logic of appending comma separators in a pattern
like:

```
for (int i = 0; i < len; i ++)
{
if (i > 0)
appendStringInfoString(", ");
appendStringInfo(some-item);
}

```
This pattern uses an "if" check and two appendStringInfoString() to build a
comma-delimited string.

This can be simplified as:

```
const char *sep = "";
for (int i = 0; i < len; i ++)
{
appendStringInfo("%s%s", sep, some-item);
sep = ", ";
}
```
The new pattern avoids the "if" check, and combines two
appendStringInfoString() into a single appendStringInfo(). I think the new
pattern is neater and faster.

The old patterns are used in a lot of places, and there are some usages of
the new pattern as well. Instead of creating a big cleanup patch, I
just applied the new pattern to a single file for now to see if the hacker
group likes this change.

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

Attachment Content-Type Size
v1-0001-Simplify-EXPLAIN-for-how-to-append-comma-separato.patch application/octet-stream 3.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2025-12-08 08:41:10 Re: Simplify the way of appending comma to stringInfo
Previous Message David Geier 2025-12-08 08:33:10 Re: Make copyObject work in C++