Definition of PostgreSQL Notify
PostgreSQL notify 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 we invoked a notify trigger all the sessions that have listening for that specific channel are notified that all turn on and now connected to the application. Notify is used to reduce the number of round trips between database and applications. 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: It 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.
- Channel: This is the name of notifies channel which was signaled. We have given any name to the notification channel in PostgreSQL notify.
- Payload: This is the default string in PostgreSQL notify. This is the string that was communicated with a notification which was send by the server.
- Channel name: This is nothing but the name of notifies channel which was signaled in PostgreSQL notify.
How Notify Works in PostgreSQL
Below is the working of PostgreSQL notify.
- Channel name and payload string is important in PostgreSQL to notify to send a signal between servers to server.
- The payload is important to 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 servers PID.
- The below image shows the event’s message received from the notify channel.
Code:
postgres=# listen channel;
postgres=# notify channel, 'test';
Output:
- In above figure, notification send from channel that will represents payload as test and it will shows process ID of this process.
- Below example shows the PostgreSQL server process ID are as follows. Above process is running from 7643 process ID.
Code:
postgres=# \! ps -ef | grep -i postgres
Output:
- Above figure state that Process ID will be created for every channel in PostgreSQL.
- Channel name is given by the developer and is dependent on which name he has given, but for identification purposes, it is important to give a channel name as application-specific.
- When we invoked a notify trigger all the sessions that have listening for that specific channel are notified that all are turn on and now connected to the application.
- PostgreSQL notify provides a simple communication between process and 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.
- Pg_notify is a built in function used to send a notification event.Pg_notify function will takes the channel name in fist argument and payload string in second argument. Pg_notify function is as easy to use as compare to a notify command.
- For notify any message it need to be listen first with the channel name in PostgreSQL.
Examples of PostgreSQL Notify
Below is the example of PostgreSQL notify.
Example #1 – Using notify in PostgreSQL
- Below example show a how notify works in PostgreSQL. To send notification message first it listen through the channel. We have used channel name as channel_test and payload string name as notify_test.
- It will display 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
- Below example show simple notify function that we have used channel name as chanel1 and payload string as test.
Code:
postgres=# listen channle1;
postgres=# select pg_notify('channel', 'test');
Output:
Example #3 – Using pg_notify Function
- The below example shows thatpg_notify function in PostgreSQL. We have used channel name as notify test and payload name as payload.
- We have divided the channel name and payload in 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
- In below example we have using a channel name as channel1 and payload string as ID and state.
- We have used ID is 3 and state as active, so payload message from server is received as “{“id”: 3, “state”: “active”}”.
Code:
postgres=# listen channle1;
postgres=# SELECT pg_notify('channel', '{"id": 3, "state": "active"}');
Output:
Advantages
Below is the advantages of using notify in PostgreSQL.
- It provides a simple communication between process and PostgreSQL database.
- It is used to generate a notification with or without payload as a string to the client application that was previously executed the listen for the specified channel.
- It is used to reduce the number of round trips between database and applications.
- Notify is very useful and important in PostgreSQL to send a notification or notification event.
- It is used to increase the application performance.
- Notify is a used defined function and it is precompiled that stored in a database so it will increases the application performance.
- Once we have develop notify function we can use it any other application. PostgreSQL, notify function is a reusable.
- It is useful to interact with the SQL transactions.
Recommended Articles
This is a guide to PostgreSQL Notify. Here we discuss the definition and how notify works in PostgreSQL along with different examples and its code implementation. You may also have a look at the following articles to learn more –