Use stack-allocated StringInfoData

From: Mats Kindahl <mats(dot)kindahl(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Use stack-allocated StringInfoData
Date: 2025-11-03 07:27:12
Message-ID: 4379aac8-26f1-42f2-a356-ff0e886228d3@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all,

While working on other things I noted that we have a lot of cases where
a StringInfo instance is allocated dynamically even when it is either
thrown away or destroyed at the end, which seems unnecessary, that is,
instead of using:

       StringInfo info = makeStringInfo();
       ...
       appendStringInfo(info, ...);
       ...
       return info->data;

We can use

       StringInfoData info;
       initStringInfo(&info);
       ...
       appendStringInfo(&info, ...);
       ...
       return info.data;

It was corrected in an earlier commit, but that seems to have been
removed so we still have a lot of these cases.

I created a semantic patch to capture most of these cases, which is
present in [1], but this is a slightly modified version that might be
interesting to include regardless of other changes. The patch is applied
and one case that couldn't be matched is manually fixed.

[1]:
https://www.postgresql.org/message-id/8895cba9-48cf-40fe-9c47-9b43ec6b2ab3%40gmail.com

Best wishes,
Mats Kindahl

Attachment Content-Type Size
0001-Use-stack-allocated-StringInfoData.v1.patch text/x-patch 32.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bertrand Drouvot 2025-11-03 07:47:28 Re: Consistently use the XLogRecPtrIsInvalid() macro
Previous Message Peter Smith 2025-11-03 07:24:39 Re: Add support for specifying tables in pg_createsubscriber.