Florin

Florin

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 :wink: 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 :slight_smile:, 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

Where Next? Top

Trending in Announcing Top

type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
woylie
I released Doggo, a collection of unstyled Phoenix components. https://github.com/woylie/doggo Features Unstyled Phoenix components....
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
kip
I’ll shortly be launching Text, a nascent text analysis library. Current functionality In this early version (not ready for prime time) ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews