18

Trying to add a NOT NULL constraint to a table with 1 billion rows. I cannot afford a table lock for more than a couple of seconds. Is there a way to prevent a full table scan during the alter table statement? I created an index on the column hoping it would be used but that doesn't seem to work. May be a check constraint? Other options? Thank you!

5
  • What on earth are you running that you cannot afford a table lock for more than a couple of seconds?The Universe software? Commented Jun 8, 2014 at 18:24
  • 4
    :) collecting real time data but only for a very small part of the universe. Commented Jun 9, 2014 at 5:55
  • 1
    Just thoughts... That's weird it uses full table scan if you have an index. If it would be using the index, as index lookup for a non-existing value should be lighting fast... so the lock will not be a problem. Commented May 20, 2015 at 21:23
  • 2
    As of PG 12, we now have the ability to concurrently add NOT NULL! dba.stackexchange.com/a/268128/12659 Commented Jun 2, 2020 at 14:20
  • (btw, the question that this question is marked a duplicate of is actually about something else - if an admin sees this, could you undo that so that a real answer for PG 12+ can be added) Commented Jun 2, 2020 at 14:20

2 Answers 2

12

Is there a way to prevent a full table scan during the alter table statement?

At this time there is no supported, safe way to do that with PostgreSQL.

Some kind of ALTER TABLE ... ADD CONSTRAINT ... CONCURRENTLY would be nice, but nobody's implemented it. Same with the alternative of adding a NOT VALID constraint that still affects new rows, and that you then VALIDATE later - it'd be good, and it's something everyone knows is needed but nobody's had the time or funding to add yet.

In theory you could directly modify the system catalogs to add the constraint if you know it is true and valid. In practice, well, it's generally not a great idea.

So no, there isn't really a way.

5
  • 1
    Thank you! In case somebody wants to implement: While adding constraints concurrently would be fantastic, just looking at indexes before scanning the entire table would already be very helpful. Commented Jun 9, 2014 at 6:02
  • In the off chance that the column on which you want to add 'not null' is unique, you might be able to use the technique described here: http://stackoverflow.com/a/20006502. Commented Dec 18, 2014 at 20:54
  • 6
    For the record: The NOT VALID feature has since been added for CHECK and FK constraints. Related: dba.stackexchange.com/questions/75613/… or dba.stackexchange.com/questions/158499/… Commented Dec 20, 2016 at 0:37
  • 1
    You still have to VALIDATE the constraint later though, there's no way to force it to be considered valid. You can, however, twiddle the catalogs to lie about that much more easily than creating a constraint from whole cloth correctly. Commented Dec 20, 2016 at 11:30
  • 1
    As of PG 12, we now have the ability to concurrently add NOT NULL! dba.stackexchange.com/a/268128/12659 Commented Jun 2, 2020 at 14:19
11

One potential alternative is to create a check constraint using NOT VALID, then validating the check constraint later. This method requires holding an ACCESS EXCLUSIVE lock only for the duration to create the constraint, which should be on the order of milliseconds. The VALIDATE command will perform a time-consuming full table scan to validate the constraint, but it holds a less restrictive SHARE UPDATE EXCLUSIVE lock.

As for trade-offs, I've been unable to find any documentation that mentions internal mechanism differences between a standard NOT NULL constraint and a check constraint that validates a column is not null. I recall digging up a forum post that eluded to a potential performance difference, but have lost the link, so this is unconfirmed.

ALTER TABLE table ADD CONSTRAINT table_value_not_null_check CHECK (value IS NOT NULL) NOT VALID;

ALTER TABLE table VALIDATE CONSTRAINT table_value_not_null_check;

Sources:

https://www.postgresql.org/docs/9.4/static/sql-altertable.html https://www.postgresql.org/docs/9.4/static/explicit-locking.html

2
  • 1
    The benchmark discussing the difference between a standard NOT NULL constraint and a check constraint is here: dba.stackexchange.com/questions/158499/… Commented Mar 2, 2018 at 18:07
  • You can also convert the CHECK constraint to a real NOT NULL afterwards without locking the table for a large amount of time since PG 12: dba.stackexchange.com/a/268128/137516 Commented Jan 17, 2022 at 16:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.