pgsql: Introduce the concept of read-only StringInfos

Поиск
Список
Период
Сортировка
От David Rowley
Тема pgsql: Introduce the concept of read-only StringInfos
Дата
Msg-id E1qvr6S-002Mev-8s@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Introduce the concept of read-only StringInfos

There were various places in our codebase which conjured up a StringInfo
by manually assigning the StringInfo fields and setting the data field
to point to some existing buffer.  There wasn't much consistency here as
to what fields like maxlen got set to and in one location we didn't
correctly ensure that the buffer was correctly NUL terminated at len
bytes, as per what was documented as required in stringinfo.h

Here we introduce 2 new functions to initialize StringInfos.  One allows
callers to initialize a StringInfo passing along a buffer that is
already allocated by palloc.  Here the StringInfo code uses this buffer
directly rather than doing any memcpying into a new allocation.  Having
this as a function allows us to verify the buffer is correctly NUL
terminated.  StringInfos initialized this way can be appended to and
reset just like any other normal StringInfo.

The other new initialization function also accepts an existing buffer,
but the given buffer does not need to be a pointer to a palloc'd chunk.
This buffer could be a pointer pointing partway into some palloc'd chunk
or may not even be palloc'd at all.  StringInfos initialized this way
are deemed as "read-only".  This means that it's not possible to
append to them or reset them.

For the latter of the two new initialization functions mentioned above,
we relax the requirement that the data buffer must be NUL terminated.
Relaxing this requirement is convenient in a few places as it can save
us from having to allocate an entire new buffer just to add the NUL
terminator or save us from having to temporarily add a NUL only to have to
put the original char back again later.

Incompatibility note:

Here we also forego adding the NUL in a few places where it does not
seem to be required.  These locations are passing the given StringInfo
into a type's receive function.  It does not seem like any of our
built-in receive functions require this, but perhaps there's some UDT
out there in the wild which does require this.  It is likely worthy of
a mention in the release notes that a UDT's receive function mustn't rely
on the input StringInfo being NUL terminated.

Author: David Rowley
Reviewed-by: Tom Lane
Discussion: https://postgr.es/m/CAApHDvorfO3iBZ%3DxpiZvp3uHtJVLyFaPBSvcAhAq2HPLnaNSwQ%40mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/f0efa5aec19358e2282d4968a03db1db56f0ac3f

Modified Files
--------------
.../replication/logical/applyparallelworker.c      |  5 +-
src/backend/replication/logical/proto.c            | 18 ++---
src/backend/replication/logical/worker.c           |  5 +-
src/backend/tcop/postgres.c                        | 22 +++--
src/backend/utils/adt/array_userfuncs.c            | 18 +----
src/backend/utils/adt/arrayfuncs.c                 | 17 +---
src/backend/utils/adt/rowtypes.c                   | 23 ++----
src/common/stringinfo.c                            |  9 +++
src/include/lib/stringinfo.h                       | 93 +++++++++++++++++++---
9 files changed, 127 insertions(+), 83 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Amit Langote
Дата:
Сообщение: pgsql: Prevent duplicate RTEPermissionInfo for plain-inheritance parent
Следующее
От: David Rowley
Дата:
Сообщение: Re: pgsql: Prevent duplicate RTEPermissionInfo for plain-inheritance parent