April 23rd, 2015 by depesz | Tags: , , , , , , , , , | Comments Off on Waiting for 9.5 – Reduce lock levels of some trigger DDL and add FKs
Did it help? If yes - maybe you can help me?

On 5th of April, Simon Riggs committed patch:

Reduce lock levels of some trigger DDL and add FKs
 
Reduce lock levels to ShareRowExclusive for the following SQL
 CREATE TRIGGER (but not DROP or ALTER)
 ALTER TABLE ENABLE TRIGGER
 ALTER TABLE DISABLE TRIGGER
 ALTER TABLE … ADD CONSTRAINT FOREIGN KEY
 
Original work by Simon Riggs, extracted and refreshed by Andreas Karlsson
New test cases added by Andreas Karlsson
Reviewed by Noah Misch, Andres Freund, Michael Paquier and Simon Riggs

To be honest, the create trigger or alter table enable/disable trigger don't really excite me that much, as these operations are fast and can be trivially worked-around to not to lock other queries.

But – add foreign key is an exception. Because it has to actually check values in the table.

Let's consider this simple example:

$ create table t1 (id serial primary key);
$ insert into t1 (id) select generate_series(1,100000) i;
$ create table t2 (id serial primary key, t1_id int4);
$ insert into t2 (t1_id) select 1 + i % 100000 from generate_series(1,10000000) i;

This will create two tables with some rows. Now, let's add missing foreign key:

$ begin;
$ alter table t2 add foreign key (t1_id) references t1 (id);
$ select relation::regclass, mode from pg_locks where pid = pg_backend_pid() and locktype = 'relation';

On older Pg, it will show:

 relation |        mode         
----------+---------------------
 pg_locks | AccessShareLock
 t1_pkey  | AccessShareLock
 t2_pkey  | AccessShareLock
 t2       | AccessShareLock
 t2       | AccessExclusiveLock
 t1       | AccessShareLock
 t1       | RowShareLock
 t1       | AccessExclusiveLock
(8 rows)

on new one:

 relation |         mode          
----------+-----------------------
 pg_locks | AccessShareLock
 t1_pkey  | AccessShareLock
 t2_pkey  | AccessShareLock
 t2       | AccessShareLock
 t2       | ShareRowExclusiveLock
 t1       | AccessShareLock
 t1       | RowShareLock
 t1       | ShareRowExclusiveLock
(8 rows)

As you can see, in old pg, both tables – t1 and t2 were locked with AccessExclusiveLock. Which means that nothing else, not even select, can use these tables.

But in our brand new 9.5 – the lock is only ShareRowExclusiveLock. And this means that selects will actually work. Writes will still be locked out, but at the very least, we'll get the ability to read from table while foreign key is being added.

Nice. Looks really helpful. Would be better to get write access too, but I guess we have to wait for this a bit longer 🙂

Sorry, comments for this post are disabled.