aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-02-10 10:52:25 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-02-10 10:52:25 +0900
commit61241833ec54fbb030da490eecc5bc69f44dc86e (patch)
treec42e8bf3f6fe4a8eca735b4a6959bf1f2a21de3e /activerecord/lib/active_record/connection_adapters
parent283250fc3050e55661b709f2e93fb20c0aff99b2 (diff)
downloadrails-61241833ec54fbb030da490eecc5bc69f44dc86e.tar.gz
rails-61241833ec54fbb030da490eecc5bc69f44dc86e.tar.bz2
rails-61241833ec54fbb030da490eecc5bc69f44dc86e.zip
Revert "Simplify and speed up Postgres query for primary_keys"
This reverts commit d6529af2954a67bd57fda45286fa9cfd0ff6b5ac.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb18
1 files changed, 10 insertions, 8 deletions
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 bd8aab526e..a4b1723fc7 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -426,14 +426,16 @@ module ActiveRecord
def primary_keys(table_name) # :nodoc:
select_values(<<-SQL.strip_heredoc, "SCHEMA")
- SELECT a.attname
- FROM pg_index i
- CROSS JOIN unnest(i.indkey) as k
- JOIN pg_attribute a
- ON a.attrelid = i.indrelid
- AND a.attnum = k
- WHERE i.indrelid = #{quote(quote_table_name(table_name))}::regclass
- AND i.indisprimary
+ WITH pk_constraint AS (
+ SELECT conrelid, unnest(conkey) AS connum FROM pg_constraint
+ WHERE contype = 'p'
+ AND conrelid = #{quote(quote_table_name(table_name))}::regclass
+ ), cons AS (
+ SELECT conrelid, connum, row_number() OVER() AS rownum FROM pk_constraint
+ )
+ SELECT attr.attname FROM pg_attribute attr
+ INNER JOIN cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.connum
+ ORDER BY cons.rownum
SQL
end