Skip to content
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

Improve performance of type map creation #376

Merged
merged 3 commits into from
Jul 29, 2021
Merged
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
36 changes: 10 additions & 26 deletions lib/pg/basic_type_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,12 @@ def coder_by_oid(oid)
end
end

private

def supports_ranges?(connection)
connection.server_version >= 90200
end

def build_coder_maps(connection)
if supports_ranges?(connection)
result = connection.exec <<-SQL
SELECT t.oid, t.typname, t.typelem, t.typdelim, ti.proname AS typinput, r.rngsubtype
FROM pg_type as t
JOIN pg_proc as ti ON ti.oid = t.typinput
LEFT JOIN pg_range as r ON t.oid = r.rngtypid
SQL
else
result = connection.exec <<-SQL
SELECT t.oid, t.typname, t.typelem, t.typdelim, ti.proname AS typinput
FROM pg_type as t
JOIN pg_proc as ti ON ti.oid = t.typinput
SQL
end
result = connection.exec(<<-SQL).to_a
SELECT t.oid, t.typname, t.typelem, t.typdelim, ti.proname AS typinput
FROM pg_type as t
JOIN pg_proc as ti ON ti.oid = t.typinput
SQL

[
[0, :encoder, PG::TextEncoder::Array],
Expand All @@ -120,6 +105,7 @@ def build_coder_maps(connection)
h
end
end
module_function :build_coder_maps

ValidFormats = { 0 => true, 1 => true }
ValidDirections = { :encoder => true, :decoder => true }
Expand All @@ -135,8 +121,6 @@ def check_format_and_direction(format, direction)
# objects as values.
CODERS_BY_NAME = []

public

# Register an encoder or decoder instance for casting a PostgreSQL type.
#
# Coder#name must correspond to the +typname+ column in the +pg_type+ table.
Expand Down Expand Up @@ -303,8 +287,8 @@ def typecast_result_value(result, _tuple, field)
end
end

def initialize(connection)
@coder_maps = build_coder_maps(connection)
def initialize(connection, coder_maps: nil)
@coder_maps = coder_maps || build_coder_maps(connection)

# Populate TypeMapByOid hash with decoders
@coder_maps.flat_map{|f| f[:decoder].coders }.each do |coder|
Expand Down Expand Up @@ -395,8 +379,8 @@ class BinaryData < String

include PG::BasicTypeRegistry

def initialize(connection)
@coder_maps = build_coder_maps(connection)
def initialize(connection, coder_maps: nil)
@coder_maps = coder_maps || build_coder_maps(connection)
@array_encoders_by_klass = array_encoders_by_klass
@encode_array_as = :array
init_encoders
Expand Down
8 changes: 8 additions & 0 deletions spec/pg/basic_type_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,12 @@ def to_s
end
end
end

describe PG::BasicTypeRegistry do
it "can build coder maps" do
expect do
described_class.build_coder_maps(@conn)
end.to_not raise_error
end
end
end