-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Postgres: force generic plan for better nullability inference. #3541
base: main
Are you sure you want to change the base?
Conversation
query
and query_as
type inference.
I think providing a constant value may end up with the same problem in different queries. I'm wondering if at a certain point, it would be easier to parse the SQL, just to get an idea of its structure. We'd still want to use the database for type analysis though. |
I just found out about |
That sounds like it has potential, yeah. Reducing the planning overhead might help with compile times a bit too. Make sure to do |
Technically breaking since it can change the output of analysis. Also kind of why I'm starting to think maybe we should parse the SQL. The output of |
I think that's going to make the macro's quite a bit slower though |
It'd be on-par with parsing the |
I had some time on my hands the last few weeks so I thought I'd try and experiment a bit with parsing the sql myself.
Which is 3 queries per macro invocation. With my implementation:
So this means 1 query for every macro invocation and 1 query once for all the macro's (per database). So it's quite a bit faster and this also gives a lot more control over nullability which make the macro's more correct. My implementation is however not complete and far from ready, it doesn't support schema's also the integration in sqlx with query parameters is difficult but it's enough to experiment a bit. so it's definitely feasible but a lot of work to get right. |
Currently when using the
query!
andquery_as!
macro's sqlx usesexplain (verbose, format json) execute {statement_id}
to get the query plan of a prepared statement/query. Then looks through the plans and tries to be more accurate with the type inference.If a prepared statement has query parameters it needs to fill then in with a valid argument and uses
NULL
for every one because it is always a valid argument.This causes the query planner to give a query plan back that is optimized when the function arguments are all
NULL
, this is not always correct.This pr forces postgres to use a generic execution plan when describing a prepared statement with function arguments. That way it won't make optimizations based on the given parameters.
Fixes #3539, #1265, #2796 (comment), #3408, #2127