PostgreSQL NOT NULL column addition speed hinges on default value type.

Adding a NOT NULL column to a large PostgreSQL table is fast if the default is a constant value, completing in milliseconds via a catalogue change. This performance stems from an optimization introduced in PostgreSQL 11. However, using a volatile default like gen_random_uuid() forces a full table rewrite, which is time-consuming and blocks other operations. To safely add a column requiring a per-row computed value, a three-step process of adding a nullable column, backfilling in batches, and then adding a constraint is recommended to avoid prolonged locks.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in