Storage & tuples

Column alignment (padding)

Also called: attalign, alignment padding, column tetris

Each PostgreSQL data type has an alignment requirement (attalign in pg_attribute): 8-byte, 4-byte, 2-byte, or 1-byte. When a row is laid out, each column's start is rounded up to its alignment boundary, and the gap bytes inserted to get there are padding. An int4 followed by an int8, for example, leaves a 4-byte hole.

What this means

Each data type wants to start on a certain byte boundary of 8, 4, 2, or 1. When Postgres lays out a row it rounds each column's start up to its boundary, and the wasted gap bytes are padding. Put an int4 right before an int8 and you leave a 4-byte hole, which is why column order can quietly change a row's size.

Why it matters operationally

Padding is pure overhead, and how much you get depends on the order of columns. Arranging columns from widest alignment to narrowest (8-byte first, variable-length last) squeezes the holes out, shrinking every row, the optimization nicknamed "column tetris". On a large table that is meaningful disk and I/O savings for free.

Related & next

Tuple deforming and column orderTuple deforming, where padding is appliedPage, fewer padded rows fit more per page

Mentioned from

← All glossary terms · GUC reference · Error catalog