How to refresh table with deadlock?

Grover, Jyoti 40 Reputation points
2024-04-03T18:34:12.2933333+00:00

We Azure postgresql database build. on that we have a ADF process that is doing truncate and load from source schema to prod schema every morning. Teams are querying to prod schema continuously to pull data which causes deadlock on table. then adf load from source to prod fails. How can we refresh table in prod without getting rid of deadlock?

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 47,586 Reputation points
    2024-04-04T05:24:24.96+00:00

    truncate and load from source schema to prod schema

    If you do a full load to an empty table, then the complete table gets locked; that's quite normal.

    In PostgreSQL you can set the transaction isolation to "READ UNCOMMITED" to read the "new" data anyway, see https://www.postgresql.org/docs/8.4/transaction-iso.html


  2. ShaikMaheer-MSFT 38,631 Reputation points Microsoft Employee Moderator
    2024-04-12T05:28:36.2833333+00:00

    Grover, Jyoti,

    After read completes from teams deadlocks usually get self removed.

    Hope this helps.


    Please consider hitting Accept Answer button. Accepted answers help community as well.


  3. Pinaki Ghatak 5,690 Reputation points Microsoft Employee Volunteer Moderator
    2024-05-10T20:52:57.9766667+00:00

    Hello Grover, Jyoti

    It seems like you are facing a deadlock issue while querying the production schema of your Azure PostgreSQL database.

    Deadlocks occur when two or more transactions are waiting for each other to release locks. In your case, it seems like the ADF process is holding locks on the table while the teams are querying the same table, causing a deadlock.

    To avoid this issue, you can consider using a different approach to refresh the table in the production schema.

    One approach is to use a staging table to load the data from the source schema and then merge the data into the production table. This approach can help you avoid locking the production table during the load process.

    Here are the steps you can follow:

    1. Create a staging table in the production schema with the same schema as the production table.
    2. Load the data from the source schema into the staging table using the ADF process.
    3. Merge the data from the staging table into the production table using an SQL statement. The merge statement will update the existing rows and insert the new rows into the production table. Here is an example of the merge statement:
    MERGE INTO production_table AS target 
    	USING staging_table AS source ON (target.id = source.id) 
    WHEN MATCHED THEN 
    	UPDATE SET target.column1 = source.column1, target.column2 = source.column2 
    WHEN NOT MATCHED THEN 
    	INSERT (id, column1, column2) VALUES (source.id, source.column1, source.column2);
    
    1. This approach can help you avoid locking the production table during the load process and prevent deadlocks. Additionally, you can consider optimizing the queries that the teams are running on the production schema to reduce the chances of deadlocks.

    I hope this helps.


    I hope that this response has addressed your query and helped you overcome your challenges. If so, please mark this response as Answered. This will not only acknowledge our efforts, but also assist other community members who may be looking for similar solutions.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.