Skip to content

Commit

Permalink
Patch for load times with pg_type loading in AR (#14642)
Browse files Browse the repository at this point in the history
* Patch for load times with pg_type loading in AR

- See #14615
- See rails/rails#19578
  • Loading branch information
ethervoid authored Feb 6, 2019
1 parent e0186a4 commit c044c43
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Development

### Bug fixes / enhancements
- Add base URL to lockout redirection in static pages ([#14617](https://github.com/CartoDB/cartodb/pull/14617))
- Improve the in_database operations fixing some rails behaviors that were problematic for us ([#14642]https://github.com/CartoDB/cartodb/pull/14642)
- Makes maps listing go faster with related tables (user db size cache issue, #14165)
- Do not redirect to /login by default when error is unknown in network interceptor ([#14616](https://github.com/CartoDB/cartodb/pull/14616))
- Update CARTO.js to v4.1.10
Expand Down
40 changes: 40 additions & 0 deletions config/initializers/zz_patch_activerecord_type_loading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Every time Activerecord makes a connection, it brings all the `pg_type`
# data into a cache in order to do mapping between pg types and rails types
#
# What is the problem?
#
# Well in our users database we have 300K type. AR picks ~142K which leads to
# spending ~1.3s picking the data and ~0.5s in processing those records so we end
# spending about ~2s or more without a need in all our in_database operations
#
# How does this fix work?
#
# Well, we filter all the table types (table, analysis tables, overviews) because
# in our app they aren't going to be used and in case they're needed, Rails is going
# to query the database for it, so no problem.
#
# This patch was developed for rails 4.2.10, in case of update you need to review it
# to verify if it needs to be modified or even removed.
module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module OID # :nodoc:
class TypeMapInitializer # :nodoc:
def query_conditions_for_initial_load(type_map)
known_type_names = type_map.keys.map { |n| "'#{n}'" }
known_type_types = %w('r' 'e' 'd')
<<-SQL % [known_type_names.join(", "), known_type_types.join(", ")]
LEFT JOIN pg_type as tt ON (tt.typtype = 'c' AND tt.typarray = t.oid AND tt.typinput = 'record_in(cstring,oid,integer)'::regprocedure)
WHERE
tt.oid is null
AND (t.typname IN (%s)
OR t.typtype IN (%s)
OR t.typinput = 'array_in(cstring,oid,integer)'::regprocedure
OR t.typelem != 0)
SQL
end
end
end
end
end
end

0 comments on commit c044c43

Please sign in to comment.