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

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
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
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews