Avoid repeated scans of the output buffer in oidvectortypes()

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Avoid repeated scans of the output buffer in oidvectortypes()
Date: 2026-10-15 09:09:28
Message-ID: 70611808-81DA-4B99-A3DB-7371F7EA4702@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

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.

To compare the performance:

Before the patch:
```
evantest=# SELECT length(oidvectortypes(repeat('0 ', 400000)::oidvector));
length
---------
1199998
(1 row)

Time: 7364.353 ms (00:07.364)
```

After the patch:
```
evantest=# SELECT length(oidvectortypes(repeat('0 ', 400000)::oidvector));
length
---------
1199998
(1 row)

Time: 20.638 ms
```

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.

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

Attachment Content-Type Size
v1-0001-Avoid-repeated-scans-of-the-output-buffer-in-oidv.patch application/octet-stream 2.3 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexandre Felipe 2026-10-15 09:17:26 Re: Fwd: [patch] bufmgr Half state CAS
Previous Message Dmitry Dolgov 2026-10-15 09:00:35 Re: Randomize B-Tree page split location to avoid oscillating patterns