SELECT a.attname AS fields_name, pg_catalog.format_type(a.atttypid, a.atttypmod), CASE WHEN a.attnotnull THEN 'N' ELSE '' END AS fields_not_null, pg_attrdef.adsrc AS fields_default, b.contype, pg_description.description AS fields_comment FROM pg_catalog.pg_attribute a INNER JOIN pg_class ON a.attrelid = pg_class.oid AND pg_class.relname='tablename' INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid AND pg_namespace.nspname = 'public' INNER JOIN pg_type ON a.atttypid = pg_type.oid LEFT OUTER JOIN pg_attrdef ON pg_attrdef.adrelid = pg_class.oid AND pg_attrdef.adnum = a.attnum LEFT OUTER JOIN pg_description ON pg_description.objoid = pg_class.oid AND pg_description.objsubid = a.attnum LEFT OUTER JOIN (SELECT a.attnum, pg_constraint.contype FROM pg_catalog.pg_attribute a INNER JOIN pg_class ON a.attrelid = pg_class.oid AND pg_class.relname='tablename' INNER JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid AND pg_namespace.nspname = 'public' INNER JOIN pg_constraint ON pg_constraint.conrelid = pg_class.oid AND pg_constraint.connamespace = pg_namespace.oid AND a.attnum = ANY(pg_constraint.conkey) GROUP BY a.attnum, pg_constraint.contype) b ON a.attnum = b.attnum WHERE a.attnum > 0 AND attisdropped <> 't' AND a.attname <> 'oid' ORDER BY a.attnum;