Skip to content

More efficient alternative to PQgetCopyData. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jtv
Copy link
Owner

@jtv jtv commented Feb 3, 2023

Saves CPU time (and power usage) by skipping the allocation and
deallocation of buffers for each row. The downside is that the data
is only valid for one iteration, but that's probably all that most
users of COPY OUT need anyway.

The new function, PQhandleCopyData, reduces CPU usage to less
than half of what PQgetCopyData uses.

The disadvantage is that it breaks with existing libpq API style.
But when performance matters, that might be worth it.

@jtv jtv force-pushed the PQgetNextCopyData branch from 8e7c171 to 15a8886 Compare February 9, 2023 21:51
@jtv jtv force-pushed the PQgetNextCopyData branch 2 times, most recently from 4f33275 to c31215c Compare February 28, 2023 19:19
@jtv jtv changed the title Sketch out PQgetNextCopyData. More efficient alternatives to PQgetCopyData. Mar 1, 2023
@jtv jtv force-pushed the PQgetNextCopyData branch 4 times, most recently from 9f17c54 to 1d05c52 Compare March 2, 2023 19:08
Saves CPU time (and power usage) by skipping the allocation and
deallocation of buffers for each row.  The downside is that the data is
only valid for one iteration, but that's probably all that most users of
COPY OUT need anyway.

The callback-based `PQhandleCopyData` reduces CPU usage by more than half
in my toy benchmark.  Wall-clock time stayed about the same as it was.

The disadvantage is that this breaks with existing libpq API style.  But
when performance matters, that might be worth it.
@jtv jtv force-pushed the PQgetNextCopyData branch from 1d05c52 to 93bb715 Compare March 2, 2023 19:39
jtv added 2 commits March 3, 2023 20:25
Pass the buffer to the callback as `const`.

Instead of copying `pqGetCopyData3` as `pqHandleCopyData3`, re-implement
`PQgetCopyData` on top of the callback-based API, now renamed to
`pqGetCopyData3`.

Use `fwrite` instead of `printf` so we don't need zero-terminated
strings.
@jtv jtv changed the title More efficient alternatives to PQgetCopyData. More efficient alternative to PQgetCopyData. Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant