3

I have a sql query

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

and I want a notification whenever the result changes. It would be ideal when I only get a notification when the value is 0 or >0. Does anyone has a solution approach?

1 Answer 1

2

Create a trigger on 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();

A client listening on the channel recipes will get a notification with the payload 0 or 1 after each insert/update/delete on the table.

Sign up to request clarification or add additional context in comments.

1 Comment

Very nice solution! I would not have come to that solution and would have kept me at stackoverflow.com/q/25435669/6229375 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.