Skip to content

Instantly share code, notes, and snippets.

@jordanlewis
Created December 6, 2016 22:36
Show Gist options
  • Save jordanlewis/9d6a5a56b6776d0b16ffc883f14dce59 to your computer and use it in GitHub Desktop.
Save jordanlewis/9d6a5a56b6776d0b16ffc883f14dce59 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 AS n
JOIN pg_catalog.pg_class AS c ON (c.relnamespace = n.oid)
JOIN pg_catalog.pg_attribute AS a ON (a.attrelid = c.oid)
JOIN pg_catalog.pg_type AS t ON (a.atttypid = t.oid)
LEFT JOIN pg_catalog.pg_attrdef AS def
ON ( (a.attrelid = def.adrelid)
AND (a.attnum = def.adnum))
LEFT JOIN pg_catalog.pg_description AS dsc
ON ( (c.oid = dsc.objoid)
AND (a.attnum = dsc.objsubid))
LEFT JOIN pg_catalog.pg_class AS dc
ON ( (dc.oid = dsc.classoid)
AND (dc.relname = 'pg_class'))
LEFT JOIN pg_catalog.pg_namespace AS 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')) AS c
WHERE TRUE
AND (attname LIKE 'id')
ORDER BY nspname,
c.relname,
attnum
# Postgres returns:
# nspname | relname | attname | atttypid | attnotnull | atttypmod | attlen | attnum | adsrc | description | typbasetype | typtype
#---------+-----------+---------+----------+------------+-----------+--------+--------+---------------------------------------+-------------+-------------+---------
# public | customers | id | 20 | t | -1 | 8 | 1 | nextval('customers_id_seq'::regclass) | | 0 | b
#(1 row)
# We return nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment