How to get inserted/updated row also with notification

222 Views Asked by At
class receiver : public pqxx::notification_receiver
{
    std::optional<std::string> status;

public:

    receiver(pqxx::connection_base & c, const std::string & channel)
        : pqxx::notification_receiver(c, channel) { }

    void operator() (const std::string & payload, int pid) override
    {
        status = payload;
        std::cout << "Received notification: " << channel() << std::endl;
    };

    std::optional<std::string> get() {
        auto s = status;
        status = std::nullopt;
        return s;
    }
};

I have above class defined and have created required function and trigger as belows in postgresql

 CREATE OR REPLACE FUNCTION notify_FN()
    RETURNS trigger AS
    $BODY$
        BEGIN
            PERFORM pg_notify('SendContactsList',
            json_build_object(
            'operation', 'TG_OP', 'record', row_to_json(NEW)
            )::text
            );
            RETURN NEW;
        END;
    $BODY$
        LANGUAGE plpgsql VOLATILE
        COST 100;
    
    CREATE TRIGGER "Notify_Update"
        AFTER INSERT OR DELETE OR UPDATE
        ON monitoring_service.monitor_rules
        FOR EACH ROW
        EXECUTE PROCEDURE public.notify_FN();

I have defined object for the SendContactList in a function running in separate thread.Every time table is update console prints Received notification: SendContactList, Can I also print inserted/updated row also and How?

0

There are 0 best solutions below