From b5370b1b02a394cced1f5cf2e81e45d04dd0a63b Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Thu, 15 Oct 2026 17:02:23 +0800 Subject: [PATCH v2] Use StringInfo to build the result in oidvectortypes() Replace manual buffer management with StringInfo to avoid repeatedly scanning the accumulated output when appending type names. This removes quadratic scanning for large input vectors and simplifies the code. Also check for interrupts in the loop and free temporary type names after appending them to the result. Author: Chao Li Reviewed-by: Heikki Linnakangas Reviewed-by: Japin Li Reviewed-by: David Rowley Discussion: https://www.postgresql.org/message-id/70611808-81DA-4B99-A3DB-7371F7EA4702@gmail.com --- src/backend/utils/adt/format_type.c | 36 +++++++++++------------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/src/backend/utils/adt/format_type.c b/src/backend/utils/adt/format_type.c index 7a5695c6245..f6b1704c38c 100644 --- a/src/backend/utils/adt/format_type.c +++ b/src/backend/utils/adt/format_type.c @@ -20,7 +20,9 @@ #include "access/htup_details.h" #include "catalog/namespace.h" #include "catalog/pg_type.h" +#include "lib/stringinfo.h" #include "mb/pg_wchar.h" +#include "miscadmin.h" #include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/lsyscache.h" @@ -447,42 +449,30 @@ Datum oidvectortypes(PG_FUNCTION_ARGS) { oidvector *oidArray = (oidvector *) PG_GETARG_POINTER(0); - char *result; + StringInfoData result; int numargs; int num; - size_t total; - size_t left; /* validate input before fetching dim1 */ check_valid_oidvector(oidArray); numargs = oidArray->dim1; - total = 20 * numargs + 1; - result = palloc(total); - result[0] = '\0'; - left = total - 1; + initStringInfo(&result); for (num = 0; num < numargs; num++) { - char *typename = format_type_extended(oidArray->values[num], -1, - FORMAT_TYPE_ALLOW_INVALID); - size_t slen = strlen(typename); + char *typename; - if (left < (slen + 2)) - { - total += slen + 2; - result = repalloc(result, total); - left += slen + 2; - } + CHECK_FOR_INTERRUPTS(); + + typename = format_type_extended(oidArray->values[num], -1, + FORMAT_TYPE_ALLOW_INVALID); if (num > 0) - { - strcat(result, ", "); - left -= 2; - } - strcat(result, typename); - left -= slen; + appendStringInfoString(&result, ", "); + appendStringInfoString(&result, typename); + pfree(typename); } - PG_RETURN_TEXT_P(cstring_to_text(result)); + PG_RETURN_TEXT_P(cstring_to_text_with_len(result.data, result.len)); } -- 2.50.1 (Apple Git-155)