| From: | Andres Freund <andres(at)anarazel(dot)de> |
|---|---|
| To: | Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com> |
| Cc: | shinya11(dot)kato(at)gmail(dot)com, pgsql-hackers(at)lists(dot)postgresql(dot)org |
| Subject: | Re: Speed up binary COPY TO by not allocating 1 kB per datum |
| Date: | 2026-11-30 18:31:20 |
| Message-ID: | nurcoatyssmykkbe2hqjxpv232mqe5al2dqu5cfiff66kko27r@wowadgr464ik |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
On 2026-11-24 17:58:26 +0900, Kyotaro Horiguchi wrote:
> At Tue, 24 Nov 2026 11:35:32 +0900, Shinya Kato <shinya11(dot)kato(at)gmail(dot)com> wrote in
> > Hi hackers,
> >
> > The design in the attached patches is Andres's, proposed in [1] and
> > last posted as a ten patch series in [2]. I picked the two patches
> > that size the send buffer, his v3-0004 and v3-0006, and applied the
> > review comments they received [3].
> >
> > Most send functions start with pq_begintypsend(), which allocates the
> > 1 kB default StringInfo buffer. int4send() writes 4 bytes into it.
> > printtup and COPY TO call send functions in a per-row memory context
> > that is reset after each row, and its keeper block is 8 kB, so with a
> > 1 kB chunk per attribute the eighth binary column of a row starts a
> > new block. Every row then costs a malloc() and a free() as well as the
> > oversized chunks.
> ...
> >
> > Thoughts?
>
> While malloc/free and pq_begintypsend() clearly account for a
> noticeable portion of the cost in the profiles, it is somewhat
> difficult to tell how much improvement we would see in real-world
> workloads from a benchmark writing to /dev/null.
That's true, but it's pretty easily demonstratable for cases not using
/dev/null, and even if it weren't, it's a big enough improvement that you'd
start to hit it as soon as you did some basic improvements to the
non-/dev/null output paths.
I think the /dev/null usage is mainly useful to reduce run-to-run noise due to
OS caching of the output file.
> Putting that aside, I would prefer to avoid having individual send
> functions calculate the expected output size if possible, as it adds
> complexity and requires us to keep the size calculations consistent
> with the actual serialization code.
I am doubtful that there's a good alternative as long as the send / output
functions allocate the memory. There's just a huge difference between a send
function that outputs a few bytes and one that allocates megabytes. It
doesn't actually need to be precisely right, just approximately right.
E.g. if you output a text datum, just using the size of the input varlena as a
starting point will often not quite be right, but it still will most commonly
avoid creating a lot of garbage in the output context (because you e.g. go
from VARSIZE() to VARSIZE() * 2, instead of 1024 -> 2048 -> 4096 -> 8192 ->
... VARSIZE() * 2, where all the initial sizes will be allocated inside aset
blocks, instead of separate large allocations).
> As an alternative, how about adaptively increasing the initial block
> size of the per-row memory context based on the amount of memory
> allocated for the previous row?
How does that help for the allocations of the individual stringinfos? You
still end up creating a lot of wastage due to the quadratic growing that
implies.
I think the better argument against 0002 is that it's really not going far
enough. Doing separate memory allocations for every datum's output, just to
then immediately copy the result into another buffer (via
printtup()->pq_send(countedtext|bytes)()) just doesn't make a whole lot of
sense. Even with something like these patches we spend way too much time in
memory allocation functions.
The proper way of doing this would be for both textual and binary output
functions to get the output buffer as an argument, and to serialize directly
into that. Of course there's the challenge of compatibility, but I suspect a
cleaned up version of the hack in
v3-0010-heavy-wip-Allow-string-buffer-reuse-in-send-funct.patch in [1] would
be both a bigger win and more sustainable.
To make it viable I think you'd need to have fcinfo->context point to
something that indicates it's the output buffer (via a node tag) and have a
helper function for output functions that will give you the output buffer
referenced by fcinfo->context if available, and otherwise allocates a new
stringinfo.
Greetings,
Andres Freund
[1] https://www.postgresql.org/message-id/20240218015955.rmw5mcmobt5hbene@awork3.anarazel.de
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Melanie Plageman | 2026-11-30 18:35:10 | Re: Why clearing the VM doesn't require registering vm buffer in wal record |
| Previous Message | Nathan Bossart | 2026-11-30 18:13:51 | Re: Safer and simpler HTAB macros (foreach_hash and hash_make), take two |