Avoid needless large memcpys in libpq socket writing
authorDavid Rowley <drowley@postgresql.org>
Sun, 7 Apr 2024 09:20:18 +0000 (21:20 +1200)
committerDavid Rowley <drowley@postgresql.org>
Sun, 7 Apr 2024 09:20:18 +0000 (21:20 +1200)
commitc4ab7da60617f020e8d75b1584d0754005d71830
treeeda07d743bf458a94188cc67e9a5346880d82f9c
parenta97bbe1f1df9eba0b18207c321c67de80b33db16
Avoid needless large memcpys in libpq socket writing

Until now, when calling pq_putmessage to write new data to a libpq
socket, all writes are copied into a buffer and that buffer gets flushed
when full to avoid having to perform small writes to the socket.

There are cases where we must write large amounts of data to the socket,
sometimes larger than the size of the buffer.  In this case, it's
wasteful to memcpy this data into the buffer and flush it out, instead,
we can send it directly from the memory location that the data is already
stored in.

Here we adjust internal_putbytes() so that after having just flushed the
buffer to the socket, if the remaining bytes to send is as big or bigger
than the buffer size, we just send directly rather than needlessly
copying into the PqSendBuffer buffer first.

Examples of operations that write large amounts of data in one message
are; outputting large tuples with SELECT or COPY TO STDOUT and
pg_basebackup.

Author: Melih Mutlu
Reviewed-by: Heikki Linnakangas
Reviewed-by: Jelte Fennema-Nio
Reviewed-by: David Rowley
Reviewed-by: Ranier Vilela
Reviewed-by: Andres Freund
Discussion: https://postgr.es/m/CAGPVpCR15nosj0f6xe-c2h477zFR88q12e6WjEoEZc8ZYkTh3Q@mail.gmail.com
src/backend/libpq/pqcomm.c