Permalink
Browse files

Backport explain fixes.

* Mark as SCHEMA some schema database queries. #7648
* Don't explain queries except normal CRUD sql. #7657

Closes #6458
Closes #7544
  • Loading branch information...
1 parent dbfdd00 commit 6abe0e7b4e10b8626d1a294eaa5fcaa28ec9f27d @rafaelfranca rafaelfranca committed Sep 17, 2012
@@ -1,4 +1,10 @@
## Rails 3.2.9 (unreleased)
+
+* Explain only normal CRUD sql (select / update / insert / delete).
+ Fix problem that explains unexplainable sql. Closes #7544 #6458.
+
+ *kennyj*
+
* Backport test coverage to ensure that PostgreSQL auto-reconnect functionality
remains healthy.
@@ -817,7 +817,7 @@ def schema_exists?(name)
# Returns an array of indexes for the given table.
def indexes(table_name, name = nil)
- result = query(<<-SQL, name)
+ result = query(<<-SQL, 'SCHEMA')
SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid
FROM pg_class t
INNER JOIN pg_index d ON t.oid = d.indrelid
@@ -837,7 +837,7 @@ def indexes(table_name, name = nil)
inddef = row[3]
oid = row[4]
- columns = Hash[query(<<-SQL, "Columns for index #{row[0]} on #{table_name}")]
+ columns = Hash[query(<<-SQL, "SCHEMA")]
SELECT a.attnum, a.attname
FROM pg_attribute a
WHERE a.attrelid = #{oid}
@@ -849,7 +849,7 @@ def indexes(table_name, name = nil)
# add info on sort order for columns (only desc order is explicitly specified, asc is the default)
desc_order_columns = inddef.scan(/(\w+) DESC/).flatten
orders = desc_order_columns.any? ? Hash[desc_order_columns.map {|order_column| [order_column, :desc]}] : {}
-
+
column_names.empty? ? nil : IndexDefinition.new(table_name, index_name, unique, column_names, [], orders)
end.compact
end
@@ -864,7 +864,7 @@ def columns(table_name, name = nil)
# Returns the current database name.
def current_database
- query('select current_database()')[0][0]
+ query('select current_database()', 'SCHEMA')[0][0]
end
# Returns the current schema name.
@@ -874,7 +874,7 @@ def current_schema
# Returns the current database encoding format.
def encoding
- query(<<-end_sql)[0][0]
+ query(<<-end_sql, 'SCHEMA')[0][0]
SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database
WHERE pg_database.datname LIKE '#{current_database}'
end_sql
@@ -937,7 +937,7 @@ def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
if pk && sequence
quoted_sequence = quote_table_name(sequence)
- select_value <<-end_sql, 'Reset sequence'
+ select_value <<-end_sql, 'SCHEMA'
SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
end_sql
end
@@ -947,7 +947,7 @@ def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
def pk_and_sequence_for(table) #:nodoc:
# First try looking for a sequence with a dependency on the
# given table's primary key.
- result = query(<<-end_sql, 'PK and serial sequence')[0]
+ result = query(<<-end_sql, 'SCHEMA')[0]
SELECT attr.attname, seq.relname
FROM pg_class seq,
pg_attribute attr,
@@ -968,7 +968,7 @@ def pk_and_sequence_for(table) #:nodoc:
# If that fails, try parsing the primary key's default value.
# Support the 7.x and 8.0 nextval('foo'::text) as well as
# the 8.1+ nextval('foo'::regclass).
- result = query(<<-end_sql, 'PK and custom sequence')[0]
+ result = query(<<-end_sql, 'SCHEMA')[0]
SELECT attr.attname,
CASE
WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
@@ -1086,7 +1086,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil)
end
when 'integer'
return 'integer' unless limit
-
+
case limit
when 1, 2; 'smallint'
when 3, 4; 'integer'
@@ -359,12 +359,12 @@ def columns(table_name, name = nil) #:nodoc:
# Returns an array of indexes for the given table.
def indexes(table_name, name = nil) #:nodoc:
- exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", name).map do |row|
+ exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", 'SCHEMA').map do |row|
IndexDefinition.new(
table_name,
row['name'],
row['unique'] != 0,
- exec_query("PRAGMA index_info('#{row['name']}')").map { |col|
+ exec_query("PRAGMA index_info('#{row['name']}')", 'SCHEMA').map { |col|
col['name']
})
end
@@ -15,8 +15,9 @@ def call(*args)
# On the other hand, we want to monitor the performance of our real database
# queries, not the performance of the access to the query cache.
IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN CACHE)
+ EXPLAINED_SQLS = /\A\s*(select|update|delete|insert)/i
def ignore_payload?(payload)
- payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name])
+ payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name]) || payload[:sql] !~ EXPLAINED_SQLS
end
ActiveSupport::Notifications.subscribe("sql.active_record", new)
@@ -38,11 +38,18 @@ def test_collects_pairs_of_queries_and_binds
end
end
+ def test_collects_nothing_if_unexplained_sqls
+ with_queries([]) do |queries|
+ SUBSCRIBER.call(:name => 'SQL', :sql => 'SHOW max_identifier_length')
+ assert queries.empty?
+ end
+ end
+
def with_queries(queries)
Thread.current[:available_queries_for_explain] = queries
yield queries
ensure
Thread.current[:available_queries_for_explain] = nil
end
end
-end
+end

0 comments on commit 6abe0e7

Please sign in to comment.