zoukankan      html  css  js  c++  java
  • How to make PostgreSQL functions atomic?

    Question:

    How to make PostgreSQL functions atomic?

    Assume I have some PostgreSQL functions like the following:

    CREATE FUNCTION insertSth() RETURNS void AS $$
    BEGIN
        INSERT INTO ...;
    END;
    
    CREATE FUNCTION removeSthAfterSelect() RETURNS TABLE(...) AS $$
    BEGIN
         SELECT id INTO some_id ...;
         RETURN QUERY SELECT * FROM ...;
         DELETE FROM ... WHERE id = some_id;
    END;
    
    CREATE FUNCTION justDeleteSth() RETURNS void AS $$
    BEGIN
         DELETE FROM ...;
    END;
    
    CREATE FUNCTION justSelectSth() RETURNS TABLE(...) AS $$
    BEGIN
         RETURN SELECT * FROM ...;
    END;

    From my understanding PostgresSQL functions insertSthjustDeleteSth and justSelectSth are going to be executed atomically(?). So parallel executions of them won't mess anything up.

    But for removeSthAfterSelect if there is a parallel execution it could be that SELECT id INTO some_id .. finds something, then concurrently another transaction calls justDeleteSth and deletes the row with id = someId, so when the transaction continues it won't delete anything here: DELETE FROM ... WHERE id = some_id; meaning it messes things up.

    Is this the case? Is there a way to avoid this problem? E.g. by saying that removeSthAfterSelectshould be executed atomically?

  • 相关阅读:
    hdu 1017 A Mathematical Curiosity 解题报告
    hdu 2069 Coin Change 解题报告
    hut 1574 组合问题 解题报告
    hdu 2111 Saving HDU 解题报
    hut 1054 Jesse's Code 解题报告
    hdu1131 Count the Trees解题报告
    hdu 2159 FATE 解题报告
    hdu 1879 继续畅通工程 解题报告
    oracle的系统和对象权限
    oracle 自定义函数 返回一个表类型
  • 原文地址:https://www.cnblogs.com/oxspirt/p/10691332.html
Copyright © 2011-2022 走看看