From 29b3e548f32c42cb99d9dd2d03c2cd34fa53d867 Mon Sep 17 00:00:00 2001
From: Sean Griffin <sean@thoughtbot.com>
Date: Mon, 17 Nov 2014 07:11:13 -0800
Subject: Improve the performance of quoting table names on PG

This caused a pretty major performance regression for 4.2, as this is a
hotspot for query construction. We're still slightly slower than 4.1,
but it's much less significant.
---
 .../connection_adapters/postgresql/utils.rb           | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

(limited to 'activerecord/lib')

diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb b/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
index 0290bcb48c..9a0b80d7d3 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
@@ -18,7 +18,11 @@ module ActiveRecord
         end
 
         def quoted
-          parts.map { |p| PGconn.quote_ident(p) }.join SEPARATOR
+          if schema
+            PGconn.quote_ident(schema) << SEPARATOR << PGconn.quote_ident(identifier)
+          else
+            PGconn.quote_ident(identifier)
+          end
         end
 
         def ==(o)
@@ -32,8 +36,11 @@ module ActiveRecord
 
         protected
           def unquote(part)
-            return unless part
-            part.gsub(/(^"|"$)/,'')
+            if part && part.start_with?('"')
+              part[1..-2]
+            else
+              part
+            end
           end
 
           def parts
@@ -57,7 +64,11 @@ module ActiveRecord
         # * <tt>"schema_name".table_name</tt>
         # * <tt>"schema.name"."table name"</tt>
         def extract_schema_qualified_name(string)
-          table, schema = string.scan(/[^".\s]+|"[^"]*"/)[0..1].reverse
+          schema, table = string.scan(/[^".\s]+|"[^"]*"/)
+          if table.nil?
+            table = schema
+            schema = nil
+          end
           PostgreSQL::Name.new(schema, table)
         end
       end
-- 
cgit v1.2.3