使用CASE表达式创建PostgreSQL查询时出现错误

时间:2019-09-24 00:26:48

标签: sql postgresql

我正在尝试创建一个脚本,该脚本检查PostgreSQL中某个进程是否处于活动状态,如果活动,请使用active_pid将其杀死,然后删除复制插槽。如果它不处于活动状态,则会立即删除复制插槽。

我正在使用的查询是:

SELECT active,
   CASE
      WHEN active = true
          then pg_terminate_backend(active_pid) AND pg_drop_replication_slot('slot_name')
          else pg_drop_replication_slot('slot_name')
          END
   from pg_replication_slots;

然后出现以下错误:

  

ERROR: argument of AND must be type boolean, not type void LINE 4: ... then pg_terminate_backend(active_pid) AND pg_drop_re...

如何执行此查询?

1 个答案:

答案 0 :(得分:0)

您可能需要考虑使用控制结构来满足此要求。

DO $$
DECLARE
        active_array boolean[];
        act boolean;

BEGIN
        SELECT ARRAY(SELECT active FROM pg_replication_slots) INTO active_array; 
        FOREACH act IN ARRAY active_array
        LOOP
            IF act = true
            THEN
               pg_terminate_backend(active_pid);
               pg_drop_replication_slot('slot_name');
            ELSE
               pg_drop_replication_slot('slot_name');
            END IF;

        END LOOP;
END;
$$;
相关问题