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?

  • 相关阅读:
    epoll的LT和ET模式
    linux搭建java环境
    【第三方SDK】百度地图实现最简单的定位功能(无地图界面)
    闲云控制台(二)查看文件功能,支持十六进制查看文件
    ios8 swift开发:显示变量的类名称
    cocos2dx的runAction: 反复运行,多个动作连接运行,多个动作同一时候运行的实现
    myeclipse设置凝视
    2-06. 数列求和(20)(ZJUPAT 数学)
    【IPC进程间通信之四】数据复制消息WM_COPYDATA
    条件熵定义推导公式
  • 原文地址:https://www.cnblogs.com/oxspirt/p/10691332.html
Copyright © 2011-2022 走看看