[HACKERS] PATCH: Making constant StringInfo - Mailing list pgsql-hackers

From Craig Ringer
Subject [HACKERS] PATCH: Making constant StringInfo
Date
Msg-id CAMsr+YFD1=5i1jNgzVcLMXXrw7_v2QHXYKwhPOd2rd_s4tDYyw@mail.gmail.com
Whole thread Raw
List 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

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachment

pgsql-hackers by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: [HACKERS] proposal psql \gdesc
Next
From: Ashutosh Sharma
Date:
Subject: Re: [HACKERS] Getting server crash on Windows when using ICU collation