You’ve probably seen this: someone writes a perfectly sensible create table with columns in whatever order popped into their head. A boolean here, a string there, a bigint at the end. It works, but those mixed types can cause alignment padding that wastes bytes per row. That’s harmless for small tables, but at tens or hundreds of millions of rows those wasted bytes become extra pages on disk and more cache misses.
Many years ago, back when I was mining for Gems
I found this gem: pg_column_byte_packer. More recently I came across Roger Welin’s pg_column_tetris, a PostgreSQL extension that enforces optimal column alignment to minimize row padding waste (as described by its author), and it really does that and more, check it out. I’d always wanted to find the time to build something similar in Elixir. Many, many weekends and breaks and hiatuses later, I finally finished it.
So I wrote psql_tetris. It’s a Mix formatter plugin: every time you save a migration, mix format reorders the add calls inside create table and alter table blocks. No new command to remember, no separate step in CI: if your editor formats on save and your CI runs mix format --check-formatted, you already have it covered.
The behaviour is deliberately small. It only touches add/2,3 lines. Everything else (modify, remove, timestamps, comments, blank lines) is treated as a barrier and never moved, which means deliberate grouping by the author survives. The sort is stable, I think
, and prefers null: false columns within each alignment rank, which gives a tiny CPU win on tuple deforming. There’s a per-block opt-out via a # psql_tetris: skip comment when a particular table needs to stay as-is (legacy, intentional layout, matching a pg_dump, you name it).
The skip-via-comment idea is borrowed straight from Angelika Cathor’s writeup on formatting Elixir code blocks in Markdown ( How to format Elixir code in Markdown code blocks? | Angelika.me ), which uses the same per-block marker. It’s a very well written article, and if you’re curious about how the Mix formatter plugins work in practice, start there.
This plugin refuses to run on non-Postgres projects. The reordering argument follows from how Postgres aligns column values inside a tuple on disk, so applying it to a MySQL or SQLite project would shuffle columns for no benefit. I gate execution on whether the Postgrex driver module is loaded in the project, rather than checking for Ecto.Adapters.Postgres, because ecto_sql bundles several adapter modules together and their presence isn’t a reliable signal. Postgrex is only there if you actually use Postgres.
Honest take: if you’re not on Elixir, please go use pg_column_tetris directly. It’s an awesome PostgreSQL extension, so it works regardless of which language or framework sits in front of your database, and it deserves the traffic. psql_tetris exists because I wanted the Ecto-shaped, format-on-save flavour for my own workflow, not because the original needed a replacement.
My implementation is still very fresh. The core is small and I believe it’s reasonably well tested, but it hasn’t been exposed to lots of fellow developers yet, so I’m sure there are corners I haven’t seen. If you try it and something feels off, like the wheels off some cars {= or you have an idea that would make it better, please open an issue or send a PR: psql_tetris. I’d genuinely appreciate the feedback.
Thank you, and I hope you’ll find it useful.
ッFlorin
___
Hex: psql_tetris | Hex
Docs: PsqlTetris v0.1.2 — Documentation






















