I would like to add a check constraint to a very large table. Something like:
ALTER TABLE accounts ADD CONSTRAINT "nonnegative_balance" CHECK balance >= 0;
Unfortunately Postgres (9.3, at least) blocks reads or writes until the constraint check has been completed. I verified this by starting a transaction, running the ALTER TABLE
, then opening a second transaction and checking that I couldn't read or write from the table until the first transaction completed.
Is there any way I can add this CHECK constraint without locking the table?