adding-serial-primary-key-field
#
problemAdding a primary key constraint requires an ACCESS EXCLUSIVE
lock that will block all reads and writes to the table while the primary key index is built.
#
solutionInstead of creating the constraint directly, create the
CONSTRAINT
USING
an index.
The index will be created in the background and an ACCESS EXCLUSIVE
lock will only be acquired when updating the table metadata with the ADD CONSTRAINT ... USING
statement. See "disallowed-unique-constraint" for more examples.
Instead of:
Use:
#
further readingCitus' 2018 post on tips for Postgres locking and the Postgres "ALTER TABLE" docs.