PostgreSQL pg_listening_channels() Function

The PostgreSQL pg_listening_channels() function returns the name of the asynchronous notification channel the current session is listening on.

pg_listening_channels() Syntax

Here is the syntax of the PostgreSQL pg_listening_channels() function:

pg_listening_channels() -> boolean

Parameters

The PostgreSQL pg_listening_channels() function does not require any parameters.

Return value

The PostgreSQL pg_listening_channels() function returns an array containing the names of the asynchronous notification channels the current session is listening on.

pg_listening_channels() Examples

The following statements illustrate the pg_listening_channels() basic usage of PostgreSQL functions:

SELECT pg_listening_channels();
 pg_listening_channels
-----------------------
(0 rows)

Here, the current session is not listening to any asynchronous notification channels, so the pg_listening_channels() function returns an empty result set.

To make the current session listen to an asynchronous notification channel named channel1, use the following LISTEN command :

LISTEN channel1;

To make the current session listen to an asynchronous notification channel named channel2, use the following LISTEN command :

LISTEN channel2;

To see which asynchronous notification channels the current session is listening on, use the pg_listening_channels() function:

SELECT pg_listening_channels();
 pg_listening_channels
-----------------------
 channel1
 channel2
(2 rows)

Here, we see that the current session is listening to two asynchronous notification channels: channel1 and channel2.

If you do not want to continue listening to the channel2 channel , use the following UNLISTEN command:

UNLISTEN channel2;

To see which asynchronous notification channels the current session is listening on, use the pg_listening_channels() function:

SELECT pg_listening_channels();
 pg_listening_channels
-----------------------
 channel1
(1 rows)