While using the data explorer, I recently wondered what ‘Include query plan’ was for. So I enabled it to find out - but I didn’t notice any change.
So I asked someone I’ve learned a lot from over the years and he shared a screenshot of how it’s supposed to look. I was curious why it wasn’t working, so I checked and noticed that explain: true
is included in the request send[1], but not included in the response[2]. So I tried to understand where it got lost. At some point I asked ChatGPT because I wasn’t sure where to look next. It was very helpful and navigated me though the whole debugging. I tried the fix, after adding explain: opts[:explain]
the query plan appeared. Though I have to admit that I only have a faint idea of what I was doing. That’s why I asked ChatGPT to write a summary of our findings for the bug report[3]:
Bug Summary – Missing `explain` in Query Results (Discourse Data Explorer) AI
Issue:
The explain
field was not being included in the JSON response of the Data Explorer /run
endpoint, even when explain=true
was passed in the request. As a result, query plans were not visible in the frontend UI, even though Postgres returned them correctly.
Root Cause:
While the backend correctly passed opts[:explain] = true
into the query execution logic and the result[:explain]
value was present, this value was not forwarded to the ResultFormatConverter
. The to_json
method in ResultFormatConverter
only includes the explain
key if opts[:explain] == true
. However, this option was not included in the convert
call.
Fix:
Add explain: opts[:explain]
to the call to ResultFormatConverter.convert
, like so:
render json:
ResultFormatConverter.convert(
:json,
result,
query_params: query_params,
download: params[:download],
+ explain: opts[:explain]
)
Result:
Once passed correctly, the explain
field is included in the JSON response and rendered properly in the frontend UI.