Constraints

ADD CONSTRAINT ... PRIMARY KEY without USING INDEX

Lock mode ACCESS EXCLUSIVE
Risk HIGH
Blocks reads, writes, other DDL
ALTER TABLE orders ADD CONSTRAINT orders_pkey PRIMARY KEY (id);

What this does

Like UNIQUE, PRIMARY KEY has no NOT VALID option, so PostgreSQL builds the backing index and validates it inline under ACCESS EXCLUSIVE for the whole table.

Safe rewrite

Build a unique index concurrently first, then attach the primary key to it

CREATE UNIQUE INDEX CONCURRENTLY orders_pkey_idx ON orders (id);
ALTER TABLE orders ADD CONSTRAINT orders_pkey PRIMARY KEY USING INDEX orders_pkey_idx;

Read more

More in Constraints