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?

  • 相关阅读:
    【转】使用外部看门狗请三思!
    【转】一招解决MCU启动异常
    【转】电源芯片选型,容易忽略的“QC”
    【转】为什么 MQTT 是最适合物联网的网络协议
    【转】中国芯酸往事
    函数输入参数类型为枚举时,则限定函数参数的接收范围,非枚举值输入则错误
    函数形参为指针与非指针的区别
    【转】函数中的形参问题(指针形参、引用形参、二重指针作为形参)
    【转】Example of using the --info linker option
    【转】STM32
  • 原文地址:https://www.cnblogs.com/oxspirt/p/10691332.html
Copyright © 2011-2022 走看看