The list of all the queries

Multiple RETURNING clauses in a SQL function

Query goal: Find SQL functions that have multiple statements with the RETURNING clause.
Notes about the query: The query excludes routines that only have multiple RETURNING within one statement.
Query type: Problem detection (Each row in the result could represent a flaw in the design)
Query reliability: High (Few or no false-positive results)
Query license: MIT License
Fixing suggestion: Think through the order of statements in the function and what should be the returned value. Remove all the RETURNING clauses except one.
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

SELECT 
  n.nspname AS func_schema, 
  pr.proname AS func_name, 
   pg_get_function_identity_arguments(pr.oid) AS parameters,
   t.typname AS return_type,
  regexp_replace(pg_get_functiondef(pr.oid),'[\r\n]','<br>','g')  AS func_src
FROM 
  pg_catalog.pg_proc pr, 
  pg_catalog.pg_namespace n,
  pg_catalog.pg_language l,
  pg_catalog.pg_type t
WHERE 
  pr.pronamespace = n.oid 
  AND pr.prolang=l.oid 
  AND pr.prorettype=t.oid 
  AND pr.prokind='f' 
  AND l.lanname='sql' 
  AND pg_get_functiondef(pr.oid)~*'RETURNING[[:space:]].*[;].*RETURNING[[:space:]]'   
AND n.nspname NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
ORDER BY func_schema, func_name, parameters;

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
User-defined routinesQueries of this category provide information about the user-defined routines

The list of all the queries