Skip to content

Postgres: Move io to background task. #3891

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 15 commits into
base: main
Choose a base branch
from

Conversation

joeydewaal
Copy link
Contributor

@joeydewaal joeydewaal commented Jun 8, 2025

This PR moves all the io in the postgres driver to a background task. This should fix some long standing issues with cancellation safety and unblock/allow features like query pipelining, CopyBoth mode (#2924) and the ability to cancel queries. This also simplifies the dropping of PgTransaction, PgCopyIn and PgListener. There is still some work to do to increase performance but that's out of the scope of this PR.

Notable changes:

  • SocketExt: An extension trait for Socket which has async methods for reading, writing, flushing and closing.
  • BufferedSocket now has poll methods for reading messages.
  • BufferedSocket has a Sink<&[u8]> impl to send bytes.
  • Pipe is added; a temporary stream of responses from the background worker.
  • Worker is added; the background worker that handles all the io.
    • The worker now handles ReadyForQuery messages because the wait_until_ready mechanism is removed.
  • Shared is added; a shared structure between the connection and background worker.

This is a pretty big change that touches a lot in the postgres driver. I've added a lot of comments to explain my thinking, if there is anything else I can do to make this easier lmk.

Does your PR solve an issue?

TODO

Is this a breaking change?

No, this PR only has breaking changes in sqlx-core which is semver exempt. One unit test had to be changed for PgListener because the buffering mechanism is changed. I don't think that makes this a breaking change and I don't think Hyrum's law applies here.

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