Re: Avoid repeated scans of the output buffer in oidvectortypes()

From: Japin Li <japinli(at)hotmail(dot)com>
To: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Cc: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Avoid repeated scans of the output buffer in oidvectortypes()
Date: 2026-10-15 10:19:41
Message-ID: SY7PR01MB10921C22BF71DBB10875DE740B69C2@SY7PR01MB10921.ausprd01.prod.outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, 15 Oct 2026 at 12:41, Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:
> On 15/10/2026 12:09, Chao Li wrote:
>> oidvectortypes() converts a vector of OIDs to a string of
>> concatenated type names. Since it is typically used to display
>> function argument types, the input vectors are usually
>> short. However, if we pass a long vector, this function has a few
>> issues:
>> * It uses strcat() to concatenate each type name. strcat() always
>> needs to scan from the beginning of the result string to its end,
>> making this slow for long vectors. We can simply keep a pointer to
>> the end of the result string and append directly there.
>> * There is no interrupt check in the loop, so cancellation can be delayed for long vectors.
>> * format_type_extended() returns an allocated string, but that
>> string is not freed after being copied into the result. When the
>> input is short, this is not a problem, we can rely on the memory
>> context to free the memory. But when the vector is large, it is
>> better to free these strings to avoid accumulating unnecessary
>> memory.
>
> How about switching to a StringInfo?
>

I believe the current patch is the smallest correct fix. Introducing StringInfo
would be unnecessary over-engineering.

>> Going from 7 seconds to 20 ms is a significant improvement for this large input. Although the benefit in normal use is limited, this seems like a small, worthwhile change.
>
> Yep. And aside from the performance, it's always nice to get rid of
> strcat() calls. Even though there was no bug here, it's so easy to
> mess up the size calculations.
>

+1

--
Regards,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2026-10-15 10:34:25 Re: Bug in logical decoding with DDL and subtransactions
Previous Message shveta malik 2026-10-15 10:06:56 Re: Proposal: Conflict log history table for Logical Replication