Postgresql 通知查询结果更改

Postgresql notify on query result changes

我有一个sql查询

SELECT COUNT(*) 
  FROM (SELECT * 
          FROM recipes 
         WHERE lock != '') AS count

并且我希望在结果发生变化时收到通知。当我只在值为 0 或 >0 时收到通知时,这将是理想的。有没有人有解决方法?

recipes 上创建触发器:

create or replace function recipes_trigger()
returns trigger language plpgsql as $$
declare
    payload text;
begin
    payload:= exists(select 1 from recipes where lock <> '')::int;
    perform pg_notify('recipes', payload);
    return null;
end $$;

create trigger recipes_trigger
after insert or update or delete on recipes
for each statement execute procedure recipes_trigger();

recipes 频道上侦听的客户端将在 table 上的每个 insert/update/delete 之后收到带有负载 01 的通知。