Skip to content

Commit

Permalink
Merge pull request #8417 from kennyj/fix_8414
Browse files Browse the repository at this point in the history
Fix #8414. Performance problem with postgresql adapter primary_key function.
Conflicts:
	activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
  • Loading branch information
rafaelfranca committed Dec 5, 2012
1 parent fbf23ed commit d70539c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,11 @@
## Rails 3.2.10 (unreleased) ## Rails 3.2.10 (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. * Do not instantiate intermediate Active Record objects when eager loading.
These records caused `after_find` to run more than expected. These records caused `after_find` to run more than expected.
Fix #3313 Fix #3313
Expand Down
Expand Up @@ -988,12 +988,11 @@ def pk_and_sequence_for(table) #:nodoc:
# Returns just a table's primary key # Returns just a table's primary key
def primary_key(table) def primary_key(table)
row = exec_query(<<-end_sql, 'SCHEMA').rows.first row = exec_query(<<-end_sql, 'SCHEMA').rows.first
SELECT DISTINCT(attr.attname) SELECT attr.attname
FROM pg_attribute attr 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] INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
WHERE cons.contype = 'p' WHERE cons.contype = 'p'
AND dep.refobjid = '#{quote_table_name(table)}'::regclass AND cons.conrelid = '#{quote_table_name(table)}'::regclass
end_sql end_sql


row && row.first row && row.first
Expand Down

0 comments on commit d70539c

Please sign in to comment.