SELECT DISTINCT tt.nspname AS "Schema",
                tt.name AS "Name",
                tt.object AS "Object",
                d.description AS "Description"
FROM (
-- Aggregate description
  SELECT p.oid as oid, p.tableoid as tableoid,
  n.nspname as nspname,
  CAST(p.proname AS pg_catalog.text) as name,
  CAST(pg_catalog.pg_gettext('aggregate') AS pg_catalog.text) as object
  FROM pg_catalog.pg_proc p
       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace,
       pg_catalog.pg_name_pattern($2) AS (schpat text, propat text)
  WHERE p.proisagg
    AND n.nspname ~ schpat
    AND p.proname ~ propat

-- Function descriptions
UNION ALL
  SELECT p.oid as oid, p.tableoid as tableoid,
  n.nspname as nspname,
  CAST(p.proname AS pg_catalog.text) as name,
  CAST(pg_catalog.pg_gettext('function') AS pg_catalog.text) as object
  FROM pg_catalog.pg_proc p
       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace,
       pg_catalog.pg_name_pattern($2) AS (schpat text, propat text)
  WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype
    AND p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype
    AND NOT p.proisagg
    AND n.nspname ~ schpat
    AND p.proname ~ propat  

-- Operator descriptions (only if operator has its own comment)
UNION ALL
  SELECT o.oid as oid, o.tableoid as tableoid,
  n.nspname as nspname,
  CAST(o.oprname AS pg_catalog.text) as name,
  CAST(pg_catalog.pg_gettext('operator') AS pg_catalog.text) as object
  FROM pg_catalog.pg_operator o
       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace,
       pg_catalog.pg_name_pattern($2) AS (schpat text, oprpat text)
  WHERE n.nspname ~ schpat
    AND o.oprname ~ oprpat

-- Type description
UNION ALL
  SELECT t.oid as oid, t.tableoid as tableoid,
  n.nspname as nspname,
  pg_catalog.format_type(t.oid, NULL) as name,
  CAST(pg_catalog.pg_gettext('data type') AS pg_catalog.text) as object
  FROM pg_catalog.pg_type t
       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace,
       pg_catalog.pg_name_pattern($2) AS (schpat text, typpat text)
  WHERE n.nspname ~ schpat
    AND pg_catalog.format_type(t.oid, NULL) ~ typpat

-- Relation (tables, views, indexes, sequences) descriptions
UNION ALL
  SELECT c.oid as oid, c.tableoid as tableoid,
  n.nspname as nspname,
  CAST(c.relname AS pg_catalog.text) as name,
  CAST(
    CASE c.relkind WHEN 'r' THEN pg_catalog.pg_gettext('table')
                   WHEN 'v' THEN pg_catalog.pg_gettext('view')
                   WHEN 'i' THEN pg_catalog.pg_gettext('index')
                   WHEN 'S' THEN pg_catalog.pg_gettext('sequence')
    END AS pg_catalog.text
  ) as object
  FROM pg_catalog.pg_class c
       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace,
       pg_catalog.pg_name_pattern($2) AS (schpat text, relpat text)
  WHERE c.relkind IN ('r', 'v', 'i', 'S')
    AND n.nspname ~ schpat
    AND c.relname ~ relpat

-- Rule description (ignore rules for views)
UNION ALL
  SELECT r.oid as oid, r.tableoid as tableoid,
  n.nspname as nspname,
  CAST(r.rulename AS pg_catalog.text) as name,
  CAST(pg_catalog.pg_gettext('rule') AS pg_catalog.text) as object
  FROM pg_catalog.pg_rewrite r
       JOIN pg_catalog.pg_class c ON c.oid = r.ev_class
       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace,
       pg_catalog.pg_name_pattern($2) AS (schpat text, rlepat text)
  WHERE r.rulename != '_RETURN'
    AND n.nspname ~ schpat
    AND r.rulename ~ rlepat

-- Trigger description
UNION ALL
  SELECT t.oid as oid, t.tableoid as tableoid,
  n.nspname as nspname,
  CAST(t.tgname AS pg_catalog.text) as name,
  CAST(pg_catalog.pg_gettext('trigger') AS pg_catalog.text) as object
  FROM pg_catalog.pg_trigger t
       JOIN pg_catalog.pg_class c ON c.oid = t.tgrelid
       LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace,
       pg_catalog.pg_name_pattern($2) AS (schpat text, tgrpat text)
  WHERE n.nspname ~ schpat
    AND t.tgname ~ tgrpat
) AS tt
  JOIN pg_catalog.pg_description d
       ON (tt.oid = d.objoid and tt.tableoid = d.classoid and d.objsubid = 0),
  (SELECT $1 AS cmd) AS cmd
ORDER BY 1, 2, 3