Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Last active April 17, 2021 02:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hopsoft/480fb99187cee3de51d521ee1bd264ff to your computer and use it in GitHub Desktop.
Save hopsoft/480fb99187cee3de51d521ee1bd264ff to your computer and use it in GitHub Desktop.
Query the recommended column order for PostgreSQL tables
SELECT
pg_attribute.attname,
pg_type.typname,
pg_type.typalign,
pg_type.typlen
FROM
pg_class
JOIN pg_attribute ON pg_attribute.attrelid = pg_class.oid
JOIN pg_type ON pg_type.oid = pg_attribute.atttypid
WHERE
pg_class.relname = 'YOUR_TABLE_NAME'
AND pg_attribute.attnum >= 0
ORDER BY
pg_type.typlen DESC, pg_attribute.attname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment