Skip to content
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

defer decoding until after connection is returned to pool? #7

Open
bergey opened this issue Sep 10, 2020 · 4 comments
Open

defer decoding until after connection is returned to pool? #7

bergey opened this issue Sep 10, 2020 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@bergey
Copy link
Owner

bergey commented Sep 10, 2020

In an application with a connection pool, @jfischoff has suggested that it would be better to postpone decoding rows until after the connection is returned to the pool. Can we put together a SQL instance that does so?

If we’re in the middle of a transaction, we need to hold the connection while decoding (assuming the decoded values are used)
If we’re at the end of a transaction, we’d like to return the connection to the pool, decode, then return from runTransaction
I don’t know how to tell whether there are any more statements coming in a do-block; that seems hard with a monad. Maybe we can do something more restrictive, like Applicative?

Maybe an easier approach is lazy decoding. Decoding can fail, and I suspect users still want decoding errors to cause an exception in runTransaction, not later. Maybe we can force any deferred decoding in runTransaction after returning the connection to a pool? That might also let us make field-by-field decisions about decoding intermediate results.
I have not delved into libpq enough to verify that the Result is safe to use while other threads are using the Connection that produced it. Need to do that, or copy all the bytestrings out of the Result before returning the Connection to the Pool - which seems likely to overwhelm any time savings.

@bergey bergey added help wanted Extra attention is needed design needs discussion before code labels Sep 10, 2020
@bergey
Copy link
Owner Author

bergey commented Feb 10, 2021

Benchmarking showed that getvalue' is faster than the version that does not copy the bytestring. So copying all the bytestrings and doing further decoding later (eg, JSON parsing) might be a viable approach.

@bergey bergey removed the design needs discussion before code label Feb 14, 2021
@bergey
Copy link
Owner Author

bergey commented Feb 14, 2021

I pushed a branch that (I think) makes decoding lazy. The benchmarks do not show any savings, likely because the variance in running the query swamps the savings. I don't want to make this change without supporting benchmarks, so I'm waiting for a better benchmark design.

@simfleischman
Copy link

I wonder if you could create a decoder that takes an artificially long time in order to make the decoding time greater than the query time?

@bergey
Copy link
Owner Author

bergey commented Feb 28, 2021

I don't want something totally contrived, like a decoder that sleeps. Maybe a sufficiently large JSON payload would show the effect we're interested in?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants