Permalink
Browse files

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.
  • Loading branch information...
sgrif committed Nov 17, 2014
1 parent 7da5d8a commit 29b3e548f32c42cb99d9dd2d03c2cd34fa53d867
Showing with 15 additions and 4 deletions.
  1. +15 −4 activerecord/lib/active_record/connection_adapters/postgresql/utils.rb
@@ -18,7 +18,11 @@ def to_s
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 @@ def hash

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 Utils # :nodoc:
# * <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

3 comments on commit 29b3e54

@sgrif

This comment has been minimized.

Copy link
Member Author

sgrif replied Nov 17, 2014

/cc @senny

@senny

This comment has been minimized.

Copy link
Member

senny replied Nov 17, 2014

@sgrif thanks for fixing this 😰 do you think we should annotate the relevant parts with a comment to make sure code authors are aware that these are hotspots?

@sgrif

This comment has been minimized.

Copy link
Member Author

sgrif replied Nov 17, 2014

If you think we should, we certainly can. I skipped it because none of the code stood out as terribly less ideomatic.

Please sign in to comment.