Search code examples
sqlpostgresqltransactionsdeadlockdatabase-deadlocks

Postresql SQL State: 25P02 Deadlock?


My task is to create a deadlock with two SQL Transactions. I work in PgAdmin. The task says: "Create two SQL transactions that can be executed interactively (annotate in comments in which order the transactions should be interleaved) to create a deadlock and explain the reason of the deadlock." I tried to create it and read about it online, but this is the only error I get when trying to make two processes go at the same time without COMMIT/ROLLBACK. I get this error message:

    ERROR:  current transaction is aborted, commands ignored until end of transaction block
    SQL state: 25P02

If that is not deadlock, can you help me make one?

http://zarez.net/?p=1069

    CREATE TABLE deadlock_example_table_1 (column1 int)

    CREATE TABLE deadlock_example_table_2 (column1 int)

    INSERT INTO deadlock_example_table_1 (column1)
    SELECT 1
    UNION ALL
    SELECT 2
    UNION ALL
    SELECT 3
    GO

    INSERT INTO deadlock_example_table_2 (column1)
    SELECT 1
    UNION ALL
    SELECT 2
    UNION ALL
    SELECT 3
    GO

    BEGIN;
    DELETE FROM deadlock_example_table_1 WHERE column1 = 2

    BEGIN;
    DELETE FROM deadlock_example_table_2 WHERE column1 = 2

https://medium.com/@clairesimmonds/postgresql-decoding-deadlocks-183e6a792fd3 I was thinking the output should look something like in this article.

    ERROR: deadlock detected
    DETAIL:  Process 16121 waits for AccessExclusiveLock on relation 17389 of database 16390; blocked by process 15866.
    Process 15866 waits for AccessShareLock on relation 17309 of database 16390; blocked by process 16121.
    HINT:  See server log for query details.

Solution

  • Step 1: populate

    INSERT INTO deadlock_example_table_1 (column1)
        SELECT 1
        UNION ALL
        SELECT 2
        UNION ALL
        SELECT 3
    go
    commit;
    

    Step 2: process 2 , update

     update deadlock_example_table_1 set columns1=5 where column1=1;
    

    Step 3 : process 3 (another command window) update

     update deadlock_example_table_1 set columns1=7 where column1=2;
     update deadlock_example_table_1 set columns1=4 where column1=1;
    

    This normally should get locked

    step 4: return to process 2

     update deadlock_example_table_1 set columns1=80 where column1=2;
    

    Dead lock here

    Process 2 and process 3 SHOULD be on different sessions