pg_listening_channels() is not returning the chann

2019-08-01 07:06发布

问题:

1) i want to know the channels name which are listing on my database but pg_listening_channels() name always returning null value (blank) even some clients are listening on this database.

below is my pgsql code. is any thing wrong in this code.

CREATE OR REPLACE FUNCTION query_trigger()
  RETURNS trigger AS
$BODY$
DECLARE
 send_message  text;
 queryString  text;
 channelNameArray text[];
 channelNames text;
      BEGIN
        queryString = current_query();
        channelNameArray = pg_listening_channels();
        channelNames = array_to_string(channelNameArray , ',');

           send_message := queryString || '  ' || channelNames;
           insert into "Client_Address" values (channelNames , queryString) ;
           PERFORM pg_notify('myChannel', send_message  );
           RETURN NULL;
          END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION query_trigger() OWNER TO postgres;

Schema of Client_Address table
Notify_node   text;
query         text;  

回答1:

pg_listening_channels reports listens in your own session. It won't (and can't) report listens from other sessions.

There's no way to get that information in Pg 9.2 via the system directly. You could always maintain a side-table with the info but you'd have the usual problems with keeping it in sync.

Why do you want to do this anyway? It's kind of bizarre to use NOTIFY like this. Why not have everyone listen on the same channel and notify them all with a single call?

Are you attempting to write audit logging? If so consider using one of the existing solutions or just using csv logging.