The list of all the queries

Function in a function-based index of a column is different of the function that is used in the query in a routine based on the column

Query goal: Create appropriate indexes to speed up queries. If you apply a function to a column in a query, then create a function-based index based on the function to the column.
Notes about the query: In the returned body of routine the query replaces each newline character with the line break (br) tag for the better readability in case the query result is displayed in a web browser. The query considers only function-based unique indexes that are based on the functions lower or upper.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: Medium (Medium number of false-positive results)
Query license: MIT License
Fixing suggestion: Replace the index or use another function in the routine.
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

WITH indexes AS (SELECT schemaname, tablename, indexname, indexdef, 'lower' AS opposite_function
FROM pg_catalog.pg_indexes
WHERE indexdef LIKE 'CREATE UNIQUE INDEX%' AND 
indexdef LIKE '%(upper(%'
AND
schemaname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
UNION SELECT schemaname, tablename, indexname, indexdef, 'upper' AS opposite_function
FROM pg_catalog.pg_indexes
WHERE indexdef LIKE 'CREATE UNIQUE INDEX%' AND 
indexdef LIKE '%(lower(%'
AND
schemaname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
),
indexes_with_opposite AS (SELECT schemaname, tablename, indexname, indexdef, opposite_function, translate(substring(indexdef,'[(][^()]*[)]'),'()','') AS indexed_column
FROM indexes),
routines AS (SELECT 
  pg_namespace.nspname AS routine_schema, 
  pg_proc.proname AS routine_name,
pg_get_function_identity_arguments(pg_proc.oid) AS parameters,
  array_to_string(proconfig, ',') AS search_path,
  pg_get_functiondef(pg_proc.oid)  AS routine_src
FROM 
  pg_catalog.pg_proc, 
  pg_catalog.pg_namespace
WHERE 
  pg_proc.pronamespace = pg_namespace.oid
 AND prosecdef=TRUE
AND pg_namespace.nspname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL))
SELECT schemaname AS table_schema, tablename AS table_name, indexdef, routine_schema,  routine_name, parameters, regexp_replace(routine_src,'[\r\n]','<br>','g')  AS routine_src
FROM indexes_with_opposite AS io, routines AS r
WHERE r.search_path ILIKE '%' || io.schemaname || '%' AND 
((r.routine_src ILIKE '%' || opposite_function || '(' || indexed_column || '%' ) OR
(r.routine_src ILIKE '%' || opposite_function || '(' || tablename || '.' || indexed_column || '%' ))
ORDER BY table_schema, table_name;

Collections where the query belongs to

Collection nameCollection description
Find problems automaticallyQueries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .

Categories where the query belongs to

Category nameCategory description
PerformanceQueries of this category provide information about indexes in a database.
System-defined functionsQueries of this category provide information about the use of system-defined functions.
User-defined routinesQueries of this category provide information about the user-defined routines

The list of all the queries