-
-
Notifications
You must be signed in to change notification settings - Fork 446
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
Example using FromSql with tokio_postgres query_raw stream? #1140
Comments
You can deserialize directly through Serde with the let value = row.get::<Json<MyType>>(0).0;
|
Edit -- the aggressive string-to-float issue is a data issue, not a tokio_postgres issue, so ignore that for now. Some older data incorrectly codes it as a float, not a string, which is my problem to deal with. I'll give the copy query a shot. Thanks. |
use try_get instead of get and you'll get an Err back on parse errors. |
I think that's just down to how Postgres normalizes JSON values in its backend. |
Is the copy query useful simply because you can supply types? Or does it perform better than a rowstream? I don't actually want all rows, but I do want all rows that match the search query (which is a substantial number of individually-large rows). |
COPY queries use a different mode of the communication protocol optimized for very large bulk transfers: https://www.postgresql.org/docs/current/sql-copy.html. I'm not sure where the perf cutoff between normal queries and copy queries would be - you'd probably need to do some testing. |
Appreciate it. I'll give it a shot. For the type arguments, is there a way to supply custom types? Or does it need to be a postgresql primitive? |
The types are the Postgres types. |
So there's no way to avoid the double deserialization then, is there? If it's going to deserialize these things into postgresql's Jsonb type, then pass that to serde, I've still got to clone and clean up all those strings? |
You can use https://serde.rs/lifetimes.html. |
That doesn't work if I've already deserialized the incoming cell data as a |
What do you mean by that? There isn't a type called |
No, there's no "type", but there is a
(aside: the test code is for some reason copying to stdin, but the postgres spec says to copy to stdout, I don't know how it's possible for that to be a typo, but I find it confusing) |
No, you aren't. You're telling the server the type of the value you want it to ship over the wire. |
@sfackler Okay, great! It sounds like it's all working as intended. I appreciate the detailed attention you've given this issue as well as your sample code. With love I say; is there a way this could have been a bit more discoverable? I was lost completely throughout the process; but clearly people are really using this crate (almost ten million downloads). I wonder if some of this information could find its way into the official documentation, or a curated examples folder, or etc.; I know these things take time. Anyway for anyone that wanders into this issue later, here is what I think I've learned:
|
Hello, I'm currently a beginner with tokio_postgres so I may have missed something, but the documentation on what to do with the
FromSql
trait (as opposed to implementing it, which has a derive macro and is fine) is a little unclear.I am reading a very large amount of data from a postgres database where there are a smallish number of columns, some of which are json blobs which contain a large number of fields I don't need. If I was receiving this as a postbody into a webserver or etc. I would just use
serde
, which would ignore those fields as I'm deserializing them, only keeping the ones I explicitly asked for, which would be great from a performance perspective.The analogue here seems to be the
FromSql
trait, but I'm having trouble using it. Instead, thequery_raw
function (which seems to be the only way to stream results in) returnsRow
objects, which have already split the incoming data into Jsonb objects, which I then have to manually re-parse, clone from, and pay the drop fee.This just doesn't feel right, since the
FromSql
trait is already "out there" and seems like I should be able to pass it in as a generic parameter, but I'm at a loss as to where to actually use it. I was not able to find any explicit mention of the trait in the documentation, except how to implement it (which is straightforward), and one note on one function indicating it could not be used there (but implied that it could be used other places, and this was the exception).Am I missing something? Is there a missing feature, or am I holding it wrong?
For the record, this is how I'm reading data:
Appreciate any assistance. Thank you!
The text was updated successfully, but these errors were encountered: