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

Question: pgx v5 now supports scan to struct. Are there any differences between scany and it? #127

Open
ccrotty opened this issue Feb 27, 2024 · 5 comments

Comments

@ccrotty
Copy link

ccrotty commented Feb 27, 2024

Pretty much the title question. I have used scany for a while with pgx v4 and am now updating to pgx v5. With pgx v5 supporting scan to struct with RowToStruct..., are there any differences between using scany or the pgx libs? Looked online, but couldn't find anything comparing the two.

Thanks, for the time and thanks for the great library!

@sourcec0de
Copy link

sourcec0de commented Mar 6, 2024

Doesn't appear to be the case.
Just dropped scany in favor of pgx5 libs.
Great library if you're stuck on pgx4 for some reason.

pgx.CollectRows(rows, pgx.RowToStructByName[*MyType])

@georgysavva
Copy link
Owner

@ccrotty I honestly haven't had a chance to investigate the capabilities of pgx v5 and its structure scanning yet

@zolstein
Copy link
Contributor

zolstein commented Mar 31, 2024

I've recently done some work with the struct scanning in pgx and scany (though I'm more familiar with pgx than scany). So hopefully I can comment usefully.

pgx can map from SQL columns to struct fields by field-name, tag, or position in the struct, whereas I think scany only supports field-name and tag. pgx's name mapping is also, I think, a bit more flexible than scany in how it handles non-ASCII characters. pgx also handles nested structs and pointers to structs substantially differently from scany - if you have code using scany to populate fields of nested structs, swapping to pgx might just work, but I wouldn't assume it will by default.

pgx's mechanism for scanning into structs is also (as of now) substantially slower than scany, and does much more allocation, which puts more pressure on the GC. I actually created a drop-in replacement library to pgx's collect functions to address some of these issues. It's probably not directly relevant to you since you could just keep using scany rather than pull in a new library, but it may be useful to compare against pgx and see what potential improvements exist. (Also, scany does better in part because I was able to port some of the improvements from my library.)

@georgysavva
Copy link
Owner

@zolstein, thank you for your detailed review. I hope it was helpful for the people in this thread. I can only add that scany also supports the generic database/sql interface apart from the pgx's native one. It means you can use scany with other postgres database driver libraries other than pgx and other relational databases.

@smlx
Copy link

smlx commented Dec 18, 2024

pgx v5 still omits the single call Select and Get functions, but it looks like they are coming in a future version, and with the new pgx API it is quite simple to write your own implementation.

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

No branches or pull requests

5 participants