Locking and concurrent executions of a stored procedure

半腔热情 提交于 2021-02-10 22:40:30

问题


I have a stored procedure that is executed by remote clients on an ongoing basis. From reading the docs I am under the impression that with the proper locking techniques I shouldn't need to externally manage these clients and I would be free to have them run as often and as concurrently as they like.

This procedure updates and inserts to multiple tables. A skeleton outline of the procedure is below:

LOCK TABLE table1 IN EXCLUSIVE MODE;

LOOP
    UPDATE table1 SET "var1"="var1"+1, WHERE "var2"=var2;
    IF found THEN
        EXIT;
    END IF; 

    BEGIN
        INSERT INTO table1 ("col") VALUES (1) RETURNING "ID" INTO id;
        EXIT;
    EXCEPTION WHEN unique_violation THEN
        EXIT;
    END;
    EXIT;
END LOOP;

LOCK TABLE table2 IN EXCLUSIVE MODE;

LOOP
    BEGIN
        INSERT INTO table2 ("var3","var4") values (var3,var4);
        EXIT;
    EXCEPTION WHEN unique_violation THEN
        EXIT;
    END;
END LOOP;

There's also a third table after this that is locked and updated/inserted in the same fashion as table1. Things are working as-is however a web application is also performing some slow select statements on the same tables and the exclusive locks cause large delays every so often. I realize exclusive locking should not be necessary however the myriad of locking types and which to apply to which tables in my procedure was a bit confusing so I went for this approach to get it going asap.

I have a few questions:

What is the best locking strategy for this procedure? I'd like to do it properly, avoid exclusive locks where I can to enable the web application to run selects without interference.

What kind of limitations will I face as the number of clients grows? Would it be better to abandon this approach entirely and have the data from all clients pushed to a central location and only run the procedure from there?


回答1:


Let the database handle locking for you. PostgreSQL, as well as every other database, has locking code that will lock the appropriate rows for modification as needed. PostgreSQL uses something called Multi-Version Concurrency Control that effectively means readers will never block or be blocked by writers.

With your current solution, as the number of clients grow, you'll see locking increase until your application becomes completely unusable. Again, let the database manage locking for you - it is very effective at resolving locks as needed.



来源:https://stackoverflow.com/questions/8260581/locking-and-concurrent-executions-of-a-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!