aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkennyj <kennyj@gmail.com>2012-12-05 01:12:46 +0900
committerkennyj <kennyj@gmail.com>2012-12-05 02:19:03 +0900
commit634d28e6cf536d2135e1dfd32a58a57441608216 (patch)
tree9ed13a7f0e576a490518afb84af4862017dbd7f3
parentd3f4625aed505353573960526f2a40f753f205a1 (diff)
downloadrails-634d28e6cf536d2135e1dfd32a58a57441608216.tar.gz
rails-634d28e6cf536d2135e1dfd32a58a57441608216.tar.bz2
rails-634d28e6cf536d2135e1dfd32a58a57441608216.zip
Fix #8414. Performance problem with postgresql adapter primary_key function.
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb5
2 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index aaaf27a211..a769a1c65e 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,11 @@
## Rails 4.0.0 (unreleased) ##
+* Fix performance problem with primary_key method in PostgreSQL adapter when having many schemas.
+ Uses pg_constraint table instead of pg_depend table which has many records in general.
+ Fix #8414
+
+ *kennyj*
+
* Do not instantiate intermediate Active Record objects when eager loading.
These records caused `after_find` to run more than expected.
Fix #3313
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
index 9f7086cd07..18bf14d1fb 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -305,12 +305,11 @@ module ActiveRecord
# Returns just a table's primary key
def primary_key(table)
row = exec_query(<<-end_sql, 'SCHEMA').rows.first
- SELECT DISTINCT(attr.attname)
+ SELECT attr.attname
FROM pg_attribute attr
- INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid
INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
WHERE cons.contype = 'p'
- AND dep.refobjid = '#{quote_table_name(table)}'::regclass
+ AND cons.conrelid = '#{quote_table_name(table)}'::regclass
end_sql
row && row.first