SELECT和INSERT之间的多列竞争条件

时间:2019-05-14 00:28:29

标签: sql postgresql concurrency plpgsql upsert

注意:这是this solution的后续问题。您需要阅读链接以获得此问题的上下文。另外,这是针对postgres v9.4

如果我们现在要返回多列而不是仅返回一列,我们如何实现呢?

我们来张桌子t:

 create table t(tag_id int, tag text unique);

现在这就是我想要的: 每当我调用方法f_tag_id时,我都希望它返回表t中存在的唯一行的所有列,否则将其插入并返回所有列。

这些就是我为f_insert_tag

尝试过的事情

选项1:

CREATE OR REPLACE FUNCTION f_insert_tag(tag_p_id int, _tag text)
RETURNS TABLE(_tag_p_id int, _tag_ text) 
AS
$func$
    BEGIN
        INSERT INTO t(tag_id,tag) VALUES (tag_p_id, _tag);
        return query Select * from t where t.tag_id = tag_p_id;
        EXCEPTION WHEN UNIQUE_VIOLATION THEN  -- catch exception, NULL is returned
    END
$func$ LANGUAGE plpgsql;

选项2:

   CREATE OR REPLACE FUNCTION f_insert_tag(tag_p_id int, _tag text, out _tag_id int, out _tag_ text) 
AS
$func$
    BEGIN
        INSERT INTO t(tag_id,tag) VALUES (tag_p_id, _tag);
        Select t.tag_id, t.tag from t where t.tag_id = tag_p_id into _tag_id, _tag_;
        EXCEPTION WHEN UNIQUE_VIOLATION THEN  -- catch exception, NULL is returned
    END
$func$ LANGUAGE plpgsql;

选项3:

CREATE OR REPLACE FUNCTION f_insert_tag(tag_p_id int, _tag text)
returns setof t AS
$func$
  BEGIN
    INSERT INTO t(tag_id,tag) VALUES (tag_p_id, _tag);
    return query Select * from t where t.tag_id = tag_p_id;
    EXCEPTION WHEN UNIQUE_VIOLATION THEN  -- catch exception, NULL is returned
  END
$func$ LANGUAGE plpgsql;

这3个人都自己工作:

select f_insert_tag(1322, 'helloworldaa');
    f_insert_tag
---------------------
 (1322,helloworldaa)

对于其他功能,f_tag_id也尝试了很多方法:

选项1:

CREATE OR REPLACE FUNCTION f_tag_id(tag_p_id int, _tag text, out _tag_id int, out _tag_ text) 
AS
$func$
BEGIN
   LOOP
      SELECT t.tag_id, t.tag FROM t WHERE t.tag = _tag
      UNION ALL
      SELECT f_insert_tag(tag_p_id, _tag)
      into _tag_id, _tag_;

      EXIT WHEN _tag_id IS NOT NULL;  -- else keep looping
   END LOOP;
END
$func$ LANGUAGE plpgsql;

选项2:

CREATE OR REPLACE FUNCTION f_tag_id(tag_p_id int, _tag text)
RETURNS table(_tag_id int, _tag_ text) AS
$func$

BEGIN
   LOOP
      SELECT t.tag_id, t.tag FROM t WHERE t.tag = _tag
      UNION  ALL
      SELECT f_insert_tag(tag_p_id, _tag)
      into _tag_id, _tag_;

      EXIT WHEN _tag_id IS NOT NULL;  -- else keep looping
   END LOOP;
END
$func$ LANGUAGE plpgsql;

对于这两种情况,我都会遇到相同的错误:

select f_tag_id(22, 'test');
ERROR:  each UNION query must have the same number of columns
LINE 3:       SELECT f_insert_tag(tag_p_id, _tag)
                     ^
QUERY:  SELECT t.tag_id, t.tag FROM t WHERE t.tag = _tag
      UNION  ALL
      SELECT f_insert_tag(tag_p_id, _tag)
CONTEXT:  PL/pgSQL function f_tag_id(integer,text) line 5 at SQL statement

1 个答案:

答案 0 :(得分:1)

作品中的扳手是 SELECT f_insert_tag(tag_p_id, _tag) 而不是

SELECT * FROM f_insert_tag(tag_p_id, _tag)

对于Postgres 9.4

CREATE FUNCTION f_insert_tag(_tag_id int, _tag text, OUT _tag_id_ int, OUT _tag_ text) 
AS
$func$
 BEGIN
   INSERT INTO t(tag_id, tag)
   VALUES (_tag_id, _tag)
   RETURNING t.tag_id, t.tag
   INTO  _tag_id_, _tag_;

   EXCEPTION WHEN UNIQUE_VIOLATION THEN
      -- catch exception, return NULL
 END
$func$  LANGUAGE plpgsql;


CREATE FUNCTION f_tag_id(_tag_id int, _tag text, OUT _tag_id_ int, OUT _tag_ text) AS
$func$
BEGIN
LOOP
   SELECT t.tag_id, t.tag
   FROM   t
   WHERE  t.tag = _tag

   UNION ALL
   SELECT *                                              -- !!!
   FROM   f_insert_tag(_tag_id, _tag)
   LIMIT  1

   INTO _tag_id_, _tag_;

   EXIT WHEN _tag_id_ IS NOT NULL;  -- else keep looping
END LOOP;
END
$func$ LANGUAGE plpgsql;

db <>提琴here

对于Postgres 9.5或更高版本:

CREATE FUNCTION f_tag_id(_tag_id int, _tag text, OUT _tag_id_ int, OUT _tag_ text) AS
$func$
BEGIN
LOOP
   SELECT t.tag_id, t.tag
   FROM   t
   WHERE  t.tag = _tag
   INTO   _tag_id_, _tag_;

   EXIT WHEN FOUND;

   INSERT INTO t (tag_id, tag)
   VALUES (_tag_id, _tag)
   ON     CONFLICT (tag) DO NOTHING
   RETURNING t.tag_id, t.tag
   INTO   _tag_id_, _tag_;

   EXIT WHEN FOUND;
END LOOP;
END
$func$  LANGUAGE plpgsql;

db <>提琴here

此处的基本知识: