I am looking to subscribe my code to listen to insert events
produced by postgres. My frontend can detect insert queries
to a specific table, but when I try to return a payload
I get two types of errors.
I removed some business logic, so it might not be a 100% working example.
First error if I use notify
instead of pg_notify
I cannot return the new row. But when I use pg_notify
I get a query error stating more than one row returned by a subquery used as an expression
.
Here is an example of the trigger:
CREATE OR REPLACE FUNCTION notify_link_insert() RETURNS trigger LANGUAGE plpgsql AS $$
begin
select pg_notify("link_topic_manager", to_jsonb(new));
return new;
end;
$$;
CREATE TRIGGER link_manager_trigger AFTER INSERT ON link FOR EACH ROW EXECUTE PROCEDURE notify_link_insert();
Query:
INSERT INTO link (network_id, sender_id, target_id, protocol) VALUES ((SELECT id from network WHERE name='ZZ'), 'zz44', 'zzz', 'z123');