Find views and materialized views that use string_agg aggregate function.
Notes
In the returned subquery of view/materialized view the query adds line break (br) tag at the beginning of each clause for the better readability in case the query result is displayed in a web browser. LEFT JOIN and RIGHT JOIN keywords are marked with red because quite often outer join is used unnecessarily because INNER JOIN operation would be enough. The query uses (b) tags to mark some of the main clauses in the subquery of the derived table.
Type
General (Overview of some aspect of the database.)
WITH user_defined_derived_tables AS (SELECT
table_schema,
table_name,
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
regexp_replace(
view_definition,'FROM',' FROM','g'),
'ORDER BY',' ORDER BY','g'),
'WHERE',' WHERE','g'),
'GROUP BY',' GROUP BY','g'),
'SELECT','SELECT','g'),
'LEFT JOIN','LEFT JOIN','g'),
'RIGHT JOIN','RIGHT JOIN','g'),
'HAVING',' HAVING','g'),
'UNION',' UNION','g'),
'EXCEPT',' EXCEPT','g'),
'INTERSECT',' INTERSECT','g') AS def,
table_type
FROM (SELECT
views.table_schema,
views.table_name,
views.view_definition,
'VIEW' AS table_type
FROM
information_schema.views
WHERE table_schema 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, matviewname, definition, 'MATERIALIZED VIEW' AS table_type
FROM pg_catalog.pg_matviews
ORDER BY table_name) AS foo)
SELECT table_schema, table_name, def, table_type
FROM user_defined_derived_tables
WHERE def~*'string_agg[(]'
ORDER BY table_schema, table_name;
Categories
This query is classified under the following categories:
Name
Description
Derived tables
Queries of this category provide information about the derived tables (views, materialized views), which are used to implement virtual data layer.