SELECT 'View definition' AS key,
       pg_catalog.pg_get_viewdef(c.oid) AS value
  FROM pg_catalog.pg_class c,
       (SELECT $1 AS cmd) AS cmd
 WHERE c.oid = $2 AND relkind = 'v'
 UNION ALL
SELECT 'Index' AS key,
       CASE WHEN tab.indisprimary THEN 'primary key '
            WHEN tab.indisunique THEN 'unique '
            ELSE ''
       END ||
       SUBSTRING(tab.indexdef FROM POSITION('USING' IN tab.indexdef) + 6) AS value
  FROM (SELECT i.indisprimary,
               i.indisunique,
               pg_catalog.pg_get_indexdef(i.indexrelid) as indexdef
          FROM pg_catalog.pg_class c,
               pg_catalog.pg_index i
         WHERE c.oid = i.indrelid
              AND c.oid = $2 
       ) as tab
 UNION ALL
SELECT 'Rules' AS key,
       r.rulename AS value
  FROM pg_catalog.pg_class c,
       pg_catalog.pg_rewrite r
 WHERE r.ev_class = c.oid
   AND c.oid = $2
   AND (c.relkind != 'v' OR r.rulename != '_RETURN')
 UNION ALL
SELECT CASE contype
         WHEN 'c' THEN 'Check constraints'
         WHEN 'f' THEN 'Foreign Key constraints'
       END AS key,
       CASE contype
         WHEN 'c' THEN '"' || conname || '" ' || consrc
         WHEN 'f' THEN conname || ' ' || pg_catalog.pg_get_constraintdef(oid)
       END AS value
  FROM pg_catalog.pg_constraint r
 WHERE r.conrelid = $2
       AND r.contype IN ('c', 'f')
 UNION ALL
SELECT 'Triggers' AS key,
       t.tgname AS value
  FROM pg_catalog.pg_trigger t
 WHERE t.tgrelid = $2
   and (NOT tgisconstraint
        OR NOT EXISTS
          (SELECT 1
             FROM pg_catalog.pg_depend d,
                  pg_catalog.pg_constraint c
            WHERE d.refclassid = c.tableoid
                  AND d.refobjid = c.oid
                  AND d.classid = t.tableoid
                  AND d.objid = t.oid
                  AND d.deptype = 'i'
                  AND c.contype = 'f'))