Skip to content

Instantly share code, notes, and snippets.

@jordanlewis
Created November 11, 2016 16:03
Show Gist options
  • Save jordanlewis/f2494b78d66eb77304e203a5ce636e64 to your computer and use it in GitHub Desktop.
Save jordanlewis/f2494b78d66eb77304e203a5ce636e64 to your computer and use it in GitHub Desktop.
SELECT *
FROM (
SELECT n.nspname,
c.relname,
a.attname,
a.atttypid,
a.attnotnull
or (
t.typtype = 'd'
AND t.typnotnull) AS attnotnull,
a.atttypmod,
a.attlen,
row_number() OVER (partition BY a.attrelid ORDER BY a.attnum) AS attnum,
pg_catalog.pg_get_expr(def.adbin, def.adrelid) AS adsrc,
dsc.description,
t.typbasetype,
t.typtype
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c
ON (
c.relnamespace = n.oid)
JOIN pg_catalog.pg_attribute a
ON (
a.attrelid=c.oid)
JOIN pg_catalog.pg_type t
ON (
a.atttypid = t.oid)
LEFT JOIN pg_catalog.pg_attrdef def
ON (
a.attrelid=def.adrelid
AND a.attnum = def.adnum)
LEFT JOIN pg_catalog.pg_description dsc
ON (
c.oid=dsc.objoid
AND a.attnum = dsc.objsubid)
LEFT JOIN pg_catalog.pg_class dc
ON (
dc.oid=dsc.classoid
AND dc.relname='pg_class')
LEFT JOIN pg_catalog.pg_namespace dn
ON (
dc.relnamespace=dn.oid
AND dn.nspname='pg_catalog')
WHERE c.relkind IN ('r',
'v',
'f',
'm')
AND a.attnum > 0
AND NOT a.attisdropped
AND n.nspname LIKE 'public'
AND c.relname LIKE 'customers') c
WHERE true
AND attname LIKE 'id'
ORDER BY nspname,
c.relname,
attnum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment