Sub topic 7
NULLs, Alignment, and Padding: Why Column Order Matters
Prerequisites and working model
This chapter assumes you know basic SQL, can use psql, and understand that a transaction records a unit of database work. You should also be comfortable with bytes and files on disk.
A PostgreSQL heap tuple is the physical form of one table row. It contains a tuple header, optional metadata such as a NULL bitmap, alignment space, and the column values. PostgreSQL places tuples inside 8 KB heap pages.
The logical column order in a table also becomes the physical order of values inside each tuple. That order can change how much padding PostgreSQL inserts between values.
NULL values and the bitmap
NULL means that a value is unknown or absent. It is different from zero, an empty string, or false.
When a row contains one or more NULL values, PostgreSQL can add a null bitmap to the tuple. The bitmap uses one bit for each column. A bit records whether the corresponding column is null. The bitmap is rounded up to a whole number of bytes.
A null column does not store its normal data value. For example, a null bigint does not reserve eight bytes for a number. However, the tuple still needs the bitmap, and later non-null values may need alignment padding.
Alignment and padding
Some data types must begin at particular byte boundaries. This requirement is called alignment.
For example, a bigint normally starts at an 8-byte boundary. If the previous value ends at byte 1, PostgreSQL may add seven unused bytes before the bigint. Those bytes are padding.
Common alignment classes include:
| Type example | Typical alignment | Effect |
|---|---|---|
| `boolean` | 1 byte | Usually needs no padding before it |
| `integer` | 4 bytes | May need padding to reach a 4-byte boundary |
| `bigint` | 8 bytes | May need several padding bytes |
| `text` | 4 bytes | Uses variable-length storage rules |
The exact result also depends on the tuple header, null bitmap, and variable-length values. Treat the table size as the final measurement rather than relying only on a hand calculation.
Putting small, weakly aligned columns between large, strongly aligned columns often wastes space:
DROP TABLE IF EXISTS padded, compact;
CREATE TABLE padded (
a boolean,
b bigint,
c boolean,
d bigint
);
CREATE TABLE compact (
b bigint,
d bigint,
a boolean,
c boolean
);
INSERT INTO padded
SELECT true, g, true, g
FROM generate_series(1, 100000) AS g;
INSERT INTO compact
SELECT g, g, true, true
FROM generate_series(1, 100000) AS g;
SELECT pg_relation_size('padded') AS padded_bytes,
pg_relation_size('compact') AS compact_bytes;The two tables contain the same information. The compact table usually occupies fewer bytes because the bigint values are grouped together and the boolean values follow them.
How NULLs interact with column order
A null value removes its own data payload, but it does not remove the column from the tuple's logical order. PostgreSQL still examines the next column's alignment requirement.
For a table with many nullable columns, the bitmap can add overhead. A table with up to eight nullable positions needs at least one bitmap byte when a row contains nulls. More columns require more bitmap bytes.
Column order matters less for rows dominated by large variable-length values. Those values may be compressed or moved to a TOAST table. It matters more for many short, fixed-width values, especially when a frequently repeated row must fit efficiently on heap pages.
Practical design guidance
Use these rules as starting points:
- Group 8-byte and 4-byte fixed-width columns when reducing padding matters.
- Put frequently null columns where they do not create repeated alignment gaps.
- Do not reorder columns only for style. A rewrite is required to change the physical layout of an existing table.
- Measure heap, TOAST, and index sizes separately when investigating storage.
- Keep logical clarity and migration cost in the decision. A small space saving may not justify a table rewrite.
The tuple header, null bitmap, and padding are storage details. SQL queries still refer to column names, so changing physical order does not change the meaning of a query.
Exercise
Why can two tables with the same columns and rows occupy different amounts of heap storage?
Show answer
Their column orders can create different alignment padding between values. Rows containing NULLs can also require a null bitmap, and the final size includes tuple and page overhead.