You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dropping a table with foreign keys can lead to a deadlock situation.
-- prepare tables for testDROPTABLE IF EXISTS foo, bar, baz;
CREATETABLEIF NOT EXISTS foo (
id intPRIMARY KEY
);
CREATETABLEIF NOT EXISTS bar (
id intPRIMARY KEY
);
CREATETABLEIF NOT EXISTS baz (
id intPRIMARY KEY,
foo_id intREFERENCES foo(id),
bar_id intREFERENCES bar(id)
);
connection 1
-- connection 1BEGIN TRANSACTION;
INSERT INTO foo VALUES(1);
-- execute DROP TABLE IF EXISTS baz; in connection 2INSERT INTO bar VALUES(1);
COMMIT;
connection 2
-- connection 2BEGIN TRANSACTION;
-- after INSERT INTO foo VALUES(1), before INSERT INTO bar VALUES(1);DROPTABLE IF EXISTS baz;
COMMIT;
fails with
SQL Error [40P01]: ERROR: deadlock detected
Detail: Process 462540 waits for RowExclusiveLock on relation 187503 of database 13797; blocked by process 462507.
Process 462507 waits for AccessExclusiveLock on relation 187498 of database 13797; blocked by process 462540.
Hint: See server log for query details.
Position: 14
Error position: line: 32 pos: 13
The text was updated successfully, but these errors were encountered:
-- connection 2BEGIN TRANSACTION;
ALTERTABLE IF EXISTS baz DROP CONSTRAINT IF EXISTS baz_foo_id_fkey;
COMMIT;
BEGIN TRANSACTION;
ALTERTABLE IF EXISTS baz DROP CONSTRAINT IF EXISTS baz_bar_id_fkey;
COMMIT;
BEGIN TRANSACTION;
DROPTABLE IF EXISTS baz;
COMMIT;
correct solution 2:
BEGIN TRANSACTION;
ALTERTABLE IF EXISTS baz DROP COLUMN IF EXISTS foo_id;
COMMIT;
BEGIN TRANSACTION;
ALTERTABLE IF EXISTS baz DROP COLUMN IF EXISTS bar_id;
COMMIT;
BEGIN TRANSACTION;
DROPTABLE IF EXISTS baz;
COMMIT;
Dropping a table with foreign keys can lead to a deadlock situation.
connection 1
connection 2
fails with
The text was updated successfully, but these errors were encountered: