Florin

Florin

PsqlTetris - A Small Mix Formatter Plugin for Tidier Postgres Columns

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?

Popular in Announcing Top

kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps w...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 30338 241
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 10719 134
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement