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
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.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Replace the index or use another function in the routine.
Data Source
INFORMATION_SCHEMA+system catalog
SQL Query
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]',' ','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
This query belongs to the following collections:
Name
Description
Find problems automatically
Queries, 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
This query is classified under the following categories:
Name
Description
Performance
Queries of this category provide information about indexes in a database.
System-defined functions
Queries of this category provide information about the use of system-defined functions.
User-defined routines
Queries of this category provide information about the user-defined routines