
SELECT
  p.oid,
  p.proname,
  p.pronamespace,
  FORMAT_TYPE(p.prorettype, NULL) AS rettype,
  d.description,
  ARRAY_LENGTH(COALESCE(p.proallargtypes, p.proargtypes), 1) AS pronargs,
  COALESCE(p.proallargtypes, p.proargtypes) AS proargtypes,
  p.proargmodes,
  p.proargnames,
  p.prokind = 'p' AS is_proc,
  s.nspname = 'pg_catalog' AS isbuiltin,
  CASE p.prokind WHEN 'a' THEN 'aggregate' WHEN 'w' THEN 'window' ELSE NULL END AS category
FROM pg_proc p
  LEFT JOIN pg_description d ON p.oid = d.objoid AND d.objsubid = 0
  INNER JOIN pg_namespace s ON p.pronamespace = s.oid
ORDER BY p.pronamespace, p.proname, p.oid;
