Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions integration_test/pg/prepare_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ defmodule Ecto.Integration.PrepareTest do
alias Ecto.Integration.TestRepo
alias Ecto.Integration.Post

defmodule Item do
use Ecto.Schema

schema "items" do
field :value, :integer
end
end

test "prepare option" do
one = TestRepo.insert!(%Post{title: "one"})
two = TestRepo.insert!(%Post{title: "two"})
Expand All @@ -16,4 +24,95 @@ defmodule Ecto.Integration.PrepareTest do
assert TestRepo.all(Post, prepare: :unnamed) == [one, two]
assert TestRepo.all(Post, prepare: :named) == [one, two]
end

test "inserts with long source names write to the intended table" do
prefix = "prepare_test_" <> String.duplicate("x", 38)
first = prefix <> "_a"
second = prefix <> "_b"
create_table(first)
create_table(second)

# The old names only differ after byte 63. Matching parameter types would
# let a cached insert silently execute the other table's statement.
insert_item!(first, 1)
insert_item!(second, 2)
insert_item!(first, 3)
insert_item!(second, 4)

assert values(first) == [[1], [3]]
assert values(second) == [[2], [4]]

assert [[name]] = statement_names(first)
assert name =~ ~r/^ecto_insert_prepare_test_x+[0-9]+_0$/
assert byte_size(name) == 63
end

test "bulk inserts with multibyte source names write to the intended table" do
prefix = String.duplicate("é", 24) <> "xé"
first = prefix <> "_a"
second = prefix <> "_b"
create_table(first)
create_table(second)

assert {1, nil} = TestRepo.insert_all(first, [%{value: 1}])
assert {1, nil} = TestRepo.insert_all(second, [%{value: 2}])
assert {1, nil} = TestRepo.insert_all(first, [%{value: 3}])
assert {1, nil} = TestRepo.insert_all(second, [%{value: 4}])

assert values(first) == [[1], [3]]
assert values(second) == [[2], [4]]

assert [[name]] = statement_names(first)
assert name =~ ~r/^ecto_insert_all_x_a[0-9]+$/
assert byte_size(name) <= 63
end

test "long sources without ASCII bytes use only a hash" do
source = String.duplicate("é", 30)
create_table(source)
insert_item!(source, 1)

assert [[name]] = statement_names(source)
assert name =~ ~r/^ecto_insert_[0-9]+_0$/
assert byte_size(name) <= 63
assert values(source) == [[1]]
end

test "preserves names at the limit and explicit cache options" do
source = String.duplicate("s", 49)
name = "ecto_insert_#{source}_0"
assert byte_size(name) == 63
create_table(source)

insert_item!(source, 1, cache_statement: false)
assert statement_names(source) == []

insert_item!(source, 2)
assert statement_names(source) == [[name]]

insert_item!(source, 3, cache_statement: "custom_statement")
assert statement_names(source) == [["custom_statement"], [name]]
assert values(source) == [[1], [2], [3]]
end

defp create_table(source) do
TestRepo.query!(~s|CREATE TEMP TABLE "#{source}" (id bigserial PRIMARY KEY, value integer)|)
end

defp insert_item!(source, value, opts \\ []) do
%Item{value: value}
|> Ecto.put_meta(source: source)
|> TestRepo.insert!(opts)
end

defp values(source) do
TestRepo.query!(~s|SELECT value FROM "#{source}" ORDER BY value|).rows
end

defp statement_names(source) do
TestRepo.query!(
"SELECT name FROM pg_prepared_statements WHERE statement LIKE $1 ORDER BY name",
[~s|INSERT INTO "#{source}"%|]
).rows
end
end
29 changes: 27 additions & 2 deletions lib/ecto/adapters/sql.ex
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ defmodule Ecto.Adapters.SQL do

opts =
if is_nil(Keyword.get(opts, :cache_statement)) do
[{:cache_statement, "ecto_insert_all_#{source}"} | opts]
[{:cache_statement, cache_statement_name("ecto_insert_all_", source)} | opts]
else
opts
end
Expand Down Expand Up @@ -1186,7 +1186,8 @@ defmodule Ecto.Adapters.SQL do
) do
opts =
if is_nil(Keyword.get(opts, :cache_statement)) do
[{:cache_statement, "ecto_#{operation}_#{source}_#{length(params)}"} | opts]
name = cache_statement_name("ecto_#{operation}_", source, "_#{length(params)}")
[{:cache_statement, name} | opts]
else
opts
end
Expand Down Expand Up @@ -1216,6 +1217,30 @@ defmodule Ecto.Adapters.SQL do
end
end

# Fit PostgreSQL's default 63-byte limit so the client and server agree on
# their cache keys.
defp cache_statement_name(prefix, source, suffix \\ "") do
source = to_string(source)
name = prefix <> source <> suffix

if byte_size(name) <= 63 do
name
else
hash = Integer.to_string(:erlang.phash2(name))
budget = 63 - byte_size(prefix <> hash <> suffix)
prefix <> ascii_prefix(source, budget) <> hash <> suffix
end
end

defp ascii_prefix(_, 0), do: ""
defp ascii_prefix(<<>>, _), do: ""

defp ascii_prefix(<<byte, rest::binary>>, size) when byte < 128 do
<<byte, ascii_prefix(rest, size - 1)::binary>>
end

defp ascii_prefix(<<_, rest::binary>>, size), do: ascii_prefix(rest, size)

## Transactions

@doc false
Expand Down