[Answer]-Django, Insertion during schema migration

1đź‘Ť

That behavior depends on the underlying database and what the actual migration is doing. For example, PostgreSQL DDL operations are transactional; an insert to the table will block until a DDL transaction completes. For example, in one psql window, do something like this:

create table kvpair (id serial, key character varying (50), value character varying(100));
begin;
alter table kvpair add column rank integer;

At this point, do not commit the transaction. In another psql window, try:

insert into kvpair (key, value) values ('fruit', 'oranges');

You’ll see it will block until the transaction in the other window is committed.

Admittedly, that’s a contrived example – the granularity of what’s locked will depend on the operation (DDL changes, indexing, DML updates). In addition, any statements that get submitted for execution may have assumed different constraints. For example, change the alter table statement above to include not null. On commit, the insert fails.

In my experience, it’s always a good thing to consider the “compatibility” of schema changes, and minimize changes that will dramatically restructure large tables. Careful attention can help you minimize downtime, by performing schema changes that can occur on a running system.

👤bimsapi

Leave a comment