2

We had a column that we wanted to make nullable and so we ran the following migration:

 ALTER TABLE example_table ALTER COLUMN example_column DROP NOT NULL;

Unexpectedly, this took a long time to run and resulted in downtime for our web service. We've been scratching our heads about why. This table is frequently scanned but never written, and it has a unique index on example_column.

Why did this cause a drop in performance?

Our best theory is that the unique index was dropped in Postgres internals behind-the-scenes and then rebuilt, but there's nothing in any documentation I've read that indicates that would happen for this kind of column interaction.

Why did this A) take a long time, and B) wreck performance?

1 Answer 1

4

This is probably a case of lock escalation. Dropping the NOT NULL requires a very brief ACCESS EXCLUSIVE lock on the table. But if that lock is not available, then it queues itself behind the existing (weaker) lock which is blocking it, and then everything else queues up behind this lock request. See a discussion here: https://dba.stackexchange.com/questions/132851/database-frozen-on-alter-table

Sign up to request clarification or add additional context in comments.

4 Comments

I agree with everything, but I thought the term "log escalation" means that a lot of row locks are turned into a table lock or similar.
Maybe I'm not using the correct term of art. It is similar to "priority inversion", but not quite that either.
@LaurenzAlbe in the postgres model, there is always a table lock taken before the row locks. The table locks (and their incompatibilities) depend on the row-level access necessary. But ACCESS EXCLUSIVE is a table-level lock which always conflicts with every other table lock. It's literally what it says: it requires exclusive access to the table, to the exclusion of everyone else.
The problem here is as jjanes notes the ACCESS EXCLUSIVE request will be waiting behind the current transactions (which conflict with it), then every following transaction will be blocked by it. So if the DB is online and has lots of relatively long transactions, this will cause a huge pileup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.