Updated May 15, 2023
Definition of PostgreSQL Notify
PostgreSQL notify is used to generate a notification or notification event with payload as a string to the client application that was previously executed the listen for the specified channel name on which database we have used. When a notify trigger is invoked, it notifies all sessions that are listening for the specific channel that they are now connected to the application and turns on their status. Developers use the ‘Notify’ function to minimize the number of round trips between applications and the database. Using notify in an application, we have to improve the performance of an application.
Syntax:
Below is the syntax are:
Notify channel (Channel that we have used with notify in PostgreSQL) [ , payload (Default flag used with notify in PostgreSQL) ]
Notify channel channel_name;
Pg_notify (Argument 1 (Name of first argument as channel name), Argument 2 Name of second argument as payload string)
Parameter:
Below is the parameter description of the above syntax.
- Notify: The ‘Notify’ function generates a notification or notification event with a payload as a string to the client application that has previously executed a listen for the specified channel name, depending on the database being used.
- Channel: This is the name of notifies channel, which was signalled. We have given any name to the notification channel.
- Payload: This is the default string in PostgreSQL notify. This is the string that was communicated with a notification that was sent by the server.
- Channel name: This is nothing but the name of notifying channel, signalled in PostgreSQL notify.
How Notify Works in PostgreSQL
Below is the working of PostgreSQL notify.
- Channel name and payload string are important in PostgreSQL to notify to send a signal between servers to server.
- The payload is important for communication between multiple servers. The default configuration value of payload is less than 8000 bytes in PostgreSQL notify.
- The notification event includes the channel name of the notification, payload string, and notification session server PID.
- The below image shows the event’s message received from the notify channel.
Code:
postgres=# listen channel;
postgres=# notify channel, 'test';
Output:
- In the above figure, a notification from a channel will represent the payload as a test and show the process ID of this process. The below example shows the PostgreSQL server process ID as follows. The above process is running from 7643 process ID.
Code:
postgres=# \! ps -ef | grep -i postgres
Output:
- The above figure indicates that a Process ID will be created for every channel in PostgreSQL.
- While developers assign the channel name, they should ensure it is application-specific for identification purposes.
- When an invoked notify trigger notifies all sessions that are listening for the specific channel, it connects them to the application and turns on their status.
- PostgreSQL notify provides a simple communication between the process and the PostgreSQL database. PostgreSQL notify is interact with the SQL transactions.
- To send the notification in PostgreSQL, we have also used a pg_notify function to send notification or notification event.
- Developers use the built-in ‘pg_notify’ function to send a notification event. The ‘pg_notify’ function takes the channel name in the first argument and the payload string in the second argument. The pg_notify function is as easy to use as compare to a notify command.
- To notify any message, it must be listened to first with the channel name in PostgreSQL.
Examples of PostgreSQL Notify
Below is an example of a PostgreSQL notify.
Example #1 – Using notify in PostgreSQL
- The below example shows how to notify works in PostgreSQL. To send a notification message, first, it listens through the channel. We have used the channel name as channel_test and the payload string name as notify_test.
- It will display the notification as “Asynchronous notification “channel_test” with payload “notify_test” received from server process with PID 7424.”
Code:
postgres=# listen channel_test;
postgres=# notify channel_test, 'notify_test';
Output:
Example #2 – Simple pg_notify Function
- The below example shows a simple notify function in that we have used channel name as chanel1 and payload string as a test.
Code:
postgres=# listen channle1;
postgres=# select pg_notify('channel', 'test');
Output:
Example #3 – Using pg_notify Function
- The below example shows the thatpg_notify function in PostgreSQL. We have used the channel name as notify test and the payload name as the payload.
- We have divided the channel name and payload into different parts.
Code:
postgres=# LISTEN notify_test;
postgres=# SELECT pg_notify('notify_' || 'test', 'pay' || 'load');
Output:
Example #4 – Pg_notify function by using ID and state as payload
- We have used a channel name as channel1 and payload string as ID and state in the below example.
- Since we used ID as 3 and state as active, the server sends the payload message as “{“id”: 3 “state”: “active”}”.
Code:
postgres=# listen channle1;
postgres=# SELECT pg_notify('channel', '{"id": 3, "state": "active"}');
Output:
Advantages
Below are the advantages of using notify in PostgreSQL.
- It provides a simple communication between the process and the PostgreSQL database.
- The ‘Notify’ function generates a notification, with or without a payload as a string, to the client application, as per the developer’s design. The developer should execute this function only after setting the client application to listen for the specified channel.
- Developers use it to reduce the number of round trips between the database and applications.
- Notify is very useful and important in PostgreSQL to send a notification or notification event.
- It is used to increase application performance.
- Precompiling the user-defined function ‘Notify’ and storing it in a database can increase the application’s performance.
- Once we have developed notify function, we can use it in any other application. PostgreSQL, notify function is a reusable.
- It is useful to interact with the SQL transactions.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL Notify” was beneficial to you. You can view EDUCBA’s recommended articles for more information.