Skip to content

Commit

Permalink
Fix fallback generic with no query parameters (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-rychlewski authored May 4, 2024
1 parent e40eab3 commit 52e49a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
3 changes: 3 additions & 0 deletions integration_test/pg/explain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ defmodule Ecto.Integration.ExplainTest do

assert explain =~ "p0.visits = $1"
assert explain =~ "(p0.title)::text = $2"

# Works when no parameters are given
TestRepo.explain(:all, Post, plan: :fallback_generic, verbose: true, timeout: 20000)
end

test "explain with fallback generic plan cannot use analyze" do
Expand Down
19 changes: 13 additions & 6 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -397,28 +397,35 @@ if Code.ensure_loaded?(Postgrex) do
end

def build_fallback_generic_queries(query, num_params, opts) do
prepare_args =
if num_params > 0,
do: ["( ", Enum.map_intersperse(1..num_params, ", ", fn _ -> "unknown" end), " )"],
else: []

prepare =
[
"PREPARE ",
@explain_prepared_statement_name,
"(",
Enum.map_intersperse(1..num_params, ", ", fn _ -> "unknown" end),
") AS ",
prepare_args,
" AS ",
query
]
|> IO.iodata_to_binary()

set = "SET LOCAL plan_cache_mode = force_generic_plan"

execute_args =
if num_params > 0,
do: ["( ", Enum.map_intersperse(1..num_params, ", ", fn _ -> "NULL" end), " )"],
else: []

execute =
[
"EXPLAIN ",
build_explain_opts(opts),
"EXECUTE ",
@explain_prepared_statement_name,
"(",
Enum.map_intersperse(1..num_params, ", ", fn _ -> "NULL" end),
")"
execute_args
]
|> IO.iodata_to_binary()

Expand Down

0 comments on commit 52e49a2

Please sign in to comment.