PATCH: Making constant StringInfo

From: Craig Ringer <craig(at)2ndquadrant(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: PATCH: Making constant StringInfo
Date: 2017-06-19 04:16:22
Message-ID: CAMsr+YFD1=5i1jNgzVcLMXXrw7_v2QHXYKwhPOd2rd_s4tDYyw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all

TL;DR: formalize using StringInfo over an existing buffer as a cursor
for pq_getmsg.

In src/backend/replication/logical/worker.c we intern a pre-existing
string in a StringInfo so it can be used with the pq_getmsg functions
etc.

StringInfoData s;

....

s.data = buf;
s.len = len;
s.cursor = 0;
s.maxlen = -1;

and this strikes me as something that stringinfo.h should expose and
bless, so we don't have to fiddle with the guts of a StringInfo
directly.

Reasonable?

void
initConstantStringInfo(StringInfo str, const char *buf, Size length);

resetStringInfo() on such a string would raise an error, as would
appending and enlarging. (Right now resetting will just let you
trample on the original buffer). If anyone later wants to be able to
take such an interned stringinfo and make a writeable buffer with it,
a new function like "reallocateStringInfo" could do that, but it's
easy enough to initStringInfo a new string and appendBinaryStringInfo
it, so there's not much point.

The reason for not just copying the string when attempts to modify it
are made is clear ownership. A StringInfo usually owns its buffer, but
these ones don't, and we don't want to leave the result of a given
append call as "maybe this stringinfo still references an
outside-owned buffer, maybe it doesn't". There doesn't seem a great
deal of call for a StringInfo that can start with an external buffer
and append to it until it runs out of room, then copy it only if
needed.

Patch for constrant StringInfo attached.

--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

Attachment Content-Type Size
v1-0001-Introduce-constant-StringInfo.patch text/x-patch 7.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Ashutosh Sharma 2017-06-19 04:42:15 Re: Getting server crash on Windows when using ICU collation
Previous Message Pavel Stehule 2017-06-19 03:58:36 Re: proposal psql \gdesc