-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Feature request: Expose an endpoint that returns the sql query generated instead of the result of the query #1573
Comments
I'm not sure whether your approach is really the best way to go about this, but personally I would love to have this for debugging purposes. If I could easily have the query returned instead of executed, that might help development a lot. One way to do this would be, by just making the same request with a different Now, not really answering your question: Have you considered putting those CTEs, |
@wolfgangwalther for debugging (locally) just log all queries and look at the database logs, no need for special endpoints for this |
Sure, that's what I am doing right now. But sometimes you would first need to create that local environment to replicate. If I face something weird in production (or on a server where I don't have access to the database logs...), instead of manually replicating the environment and issue first, I could immediately repeat the same query with the other header and get the query. I guess this could also improve bug reports, if people can easily provide the executed query in the issue as well. |
Hmmm, in order to keep the question concise and to the point, I simplified it too much @wolfgangwalther . The actual use-case is much complex, and the view approach would not work out. In a nutshell, we have to create dynamic SQL based on user input. It is this dynamic SQL which I was talking about in my earlier response that becomes complex because of Good news is, have written all of this already, the only part left is the "where" clause, which instead of writing on my own, I am hoping to re-use from this library. |
Also @wolfgangwalther , do you have any inputs on which place to look into for going the I will keep this thread updated if I find something from my side before you reply back. Also, I am new to Haskell, so please excuse any wrong terminologies used by me. Thanks. |
I guessed you were going to say something like that. In theory, that's nothing (yet) that wouldn't be possible with an RPC/
Still, there's one fundamental problem I see: If we were to introduce either a |
I'm assuming you're getting the query somewhere around here? postgrest/src/PostgREST/App.hs Line 137 in bd2160d
It's already
I am new as well :). @steve-chavez will be able to help you much better. |
@viiicky Have you tried creating a stored procedure that does dynamic SQL? Here's an example: #915 (comment). |
One problem with this direction, the query structure might change later when we parametrize the values. The WHERE part could change to: |
@wolfgangwalther @steve-chavez yes, I have written a bunch of postgres function only - that helps first create the dynamic SQL and then execute it. I cannot use the |
btw, I just finished up my requirement. This unblocks me! Let me know if you guys have any comments. Also, let me know if you think this feature can make it to upstream, in which case, happy to contribute after fixing comments, if any. This is how it works:
Same request with the new header:
Have of course added this header support for just |
@wolfgangwalther I did not solve for this problem, as this is not super-worrying for my use-case. |
This was helpful @wolfgangwalther . Thanks for the direction. |
Yes, parameterized query isn't going to be helpful. But, now that, I understand the area, and how it is working, I am good to go. Can always write my own stuff. |
Also, since my issue is resolved, let me know if I should be closing this issue now. Thank you all _/_ |
That's not true. As long as you're not mutating the database, you can call it with |
Hmm.. I still think that the use case could have been solved by dynamic SQL + func(as Wolfgang mentions, it also works with GET). That being said, besides the SQL query, sometime ago I had the request to expose the metadata for how pgrst does the JOINs on resource embedding(see Embedding disambiguation), this currently only gets exposed when there's an error. So for now I'm not sure if it's worth to expose the SQL or other information. Also wanted to mention that application/sql is indeed a IANA media type: https://tools.ietf.org/html/rfc6922. |
Not a coincidence - I looked that up before suggesting ;) |
I can imagine this being very helpful. It feels like this could be in the OpenAPI output as well. Couldn't find much on that, only that such relations seem not to be a part of the OpenAPI spec currently: OAI/OpenAPI-Specification#1646 If not in the OpenAPI, the
Same here. Still, some thoughts, if we were to implement this. If we want to do this for modifying statements (INSERT/UPDATE/DELETE) and RPC as well - just returning the SQL with Another approach could be to use the http method Implementing |
Okay folks - as I am no longer blocked, I am closing this off from my side. Feel free to re-open this if you guys want to continue the discussion or if it's revisited in future. Thanks to all of you for quick responses and directions to the code-base. It was extremely helpful. :party_parrot: |
I've noted that viiicky/postgrest only gives the main SQL query( |
Yeah, I just needed the main SQL query for my need, and I figured out we don't need this feature in the near future in the upstream, so kept it custom to my reqs. Also, I have already released the docker image for this. In case future readers find it helpful: https://hub.docker.com/r/fylehq/postgrest |
Environment
Feature request
We use the above-mentioned postgrest docker image with no problems.
Now, we need to write a new web service. We have decided not to use PostgREST in this new service because most of the SQL generated by this service will be of complex nature, including lots of CTEs, group-bys, aggregate functions etc. We, however, wish to keep the API contract of this service similar to that of PostgREST.
We are thinking to reuse the "filters" part of the SQL generated by PostgREST somehow. That way, we can keep the contract for query params of APIs of this new service same as PostgREST. Thus, if PostgREST exposes an endpoint, where I can make a request with these query params and get the SQL generated, I can build my complex SQL on top of this SQL without worrying about the "where" part of the SQL.
For example, below request to PostgREST:
GET /query/people?amount=gte.1000&bill=eq.true&date=gte.2020-06-30T18:30:00.000Z&state=in.(DONE)&everified=eq.true&limit=10&offset=0&or=(status.ilike.*ACTIVE*)&total_amount=gte.2000 HTTP/1.1
should return:
select * from people where amount >= 1000 and bill is true and date >= '2020-06-30T18:30:00.000Z' and state in ('DONE') and verified is true and (status ilike '%ACTIVE%') and total_amount >= 2000 offset 0 limit 10
I am going to go through the code, and based on my findings, will put my suggestions here on how to go about it. Right now struggling to setup Haskell dev setup - never worked on it.
Any directions on which part of the code I should be looking at would be helpful.
Thanks.
The text was updated successfully, but these errors were encountered: