diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb index 203087bc36..ec020cb8f0 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb @@ -33,15 +33,27 @@ module ActiveRecord composites.each { |row| register_composite_type(row) } end - def query_conditions_for_initial_load + def query_conditions_for_known_type_names known_type_names = @store.keys.map { |n| "'#{n}'" } - known_type_types = %w('r' 'e' 'd') - <<~SQL % [known_type_names.join(", "), known_type_types.join(", ")] + <<~SQL % known_type_names.join(", ") WHERE t.typname IN (%s) - OR t.typtype IN (%s) - OR t.typinput = 'array_in(cstring,oid,integer)'::regprocedure - OR t.typelem != 0 + SQL + end + + def query_conditions_for_known_type_types + known_type_types = %w('r' 'e' 'd') + <<~SQL % known_type_types.join(", ") + WHERE + t.typtype IN (%s) + SQL + end + + def query_conditions_for_array_types + known_type_oids = @store.keys.reject { |k| k.is_a?(String) } + <<~SQL % [known_type_oids.join(", ")] + WHERE + t.typelem IN (%s) SQL end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 8b9abc7f43..ca8b886c53 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -624,21 +624,25 @@ module ActiveRecord def load_additional_types(oids = nil) initializer = OID::TypeMapInitializer.new(type_map) + load_types_queries(initializer, oids) do |query| + execute_and_clear(query, "SCHEMA", []) do |records| + initializer.run(records) + end + end + end + def load_types_queries(initializer, oids) query = <<~SQL SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype FROM pg_type as t LEFT JOIN pg_range as r ON oid = rngtypid SQL - if oids - query += "WHERE t.oid IN (%s)" % oids.join(", ") + yield query + "WHERE t.oid IN (%s)" % oids.join(", ") else - query += initializer.query_conditions_for_initial_load - end - - execute_and_clear(query, "SCHEMA", []) do |records| - initializer.run(records) + yield query + initializer.query_conditions_for_known_type_names + yield query + initializer.query_conditions_for_known_type_types + yield query + initializer.query_conditions_for_array_types end end