Skip to content
This repository has been archived by the owner on May 20, 2019. It is now read-only.

Commit

Permalink
Update bootstrap_query to be compatible with CockroachDB
Browse files Browse the repository at this point in the history
CockroachDB does not support yet correlated subqueries which is used by the default bootstrap query.

See: cockroachdb/cockroach#3288
  • Loading branch information
tlvenn committed Sep 21, 2017
1 parent 1f689cd commit 4671cc8
Showing 1 changed file with 31 additions and 41 deletions.
72 changes: 31 additions & 41 deletions lib/postgrex/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,37 @@ defmodule Postgrex.Types do
end
end

@doc false
@spec bootstrap_query({pos_integer, non_neg_integer, non_neg_integer}, state) :: binary
def bootstrap_query(version, {_, table}) do
oids = :ets.select(table, [{{:"$1", :_, :_}, [], [:"$1"]}])

{rngsubtype, join_range} =
if version >= {9, 2, 0} do
{"coalesce(r.rngsubtype, 0)",
"LEFT JOIN pg_range AS r ON r.rngtypid = t.oid"}
else
{"0", ""}
end

filter_oids =
case oids do
[] ->
""
_ ->
# equiv to `WHERE t.oid NOT IN (SELECT unnest(ARRAY[#{Enum.join(oids, ",")}]))`
# `unnest` is not supported in redshift or postgres version prior to 8.4
"""
WHERE t.oid NOT IN (
SELECT (ARRAY[#{Enum.join(oids, ",")}])[i]
FROM generate_series(1, #{length(oids)}) AS i
)
"""
end

"""
SELECT t.oid, t.typname, t.typsend, t.typreceive, t.typoutput, t.typinput,
t.typelem, #{rngsubtype}, ARRAY (
SELECT a.atttypid
FROM pg_attribute AS a
WHERE a.attrelid = t.typrelid AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
)
FROM pg_type AS t
#{join_range}
#{filter_oids}
"""
end
@doc false
@spec bootstrap_query({pos_integer, non_neg_integer, non_neg_integer}, state) :: binary
def bootstrap_query(version, {_, table}) do
oids = :ets.select(table, [{{:"$1", :_, :_}, [], [:"$1"]}])

{rngsubtype, join_range} =
if version >= {9, 2, 0} do
{"coalesce(r.rngsubtype, 0)",
"LEFT JOIN pg_range AS r ON r.rngtypid = t.oid OR (t.typbasetype <> 0 AND r.rngtypid = t.typbasetype)"}
else
{"0", ""}
end

filter_oids =
case oids do
[] ->
""
_ ->
"WHERE t.oid::INT NOT IN (SELECT unnest(ARRAY[#{Enum.join(oids, ",")}]))"
end

"""
SELECT
t.oid, t.typname, t.typsend, t.typreceive, t.typoutput, t.typinput, t.typelem,
coalesce(r.rngsubtype, 0),
null
FROM pg_type AS t
LEFT JOIN pg_range AS r ON r.rngtypid = t.oid
#{filter_oids}
"""
end

@doc false
@spec build_type_info(binary) :: TypeInfo.t
Expand Down

0 comments on commit 4671cc8

Please sign in to comment.