This lab demonstrates the usage of the NOTIFY and LISTEN features in PostgreSQL. These features enable real-time communication between different sessions or threads within the database.
In this lab, we'll explore two scenarios:
-
Publish-Subscribe Model: Two separate threads will be used—one for publishing notifications (
NOTIFY) and the other for listening to these notifications (LISTEN). -
Trigger-Based Notification: We'll utilize triggers to automatically notify a Java application whenever a new row is inserted into a table.
- PostgreSQL installed on your machine.
- Basic understanding of SQL and database concepts.
- Optionally, a Java environment to interact with the database using JDBC for the second scenario.
-
Database Setup:
- Create a PostgreSQL database.
- Create the necessary table with the desired schema. (Refer to the provided SQL script for creating the table structure.)
-
Publish-Subscribe Model:
- Create two separate database connections or sessions—one for publishing and the other for listening.
- Execute SQL commands to
NOTIFYandLISTENto the desired channel.
-
Trigger-Based Notification:
- Create a trigger function that executes the
NOTIFYcommand whenever a new row is inserted into the table. - Attach the trigger function to the
AFTER INSERTevent on the table.
- Create a trigger function that executes the
- Open two separate database connections—one for publishing and the other for listening.
- In the publishing connection, insert a new row into the table.
- Observe the notification received by the listening connection.
- Set up a Java application with JDBC connectivity to the PostgreSQL database.
- Create a trigger function in PostgreSQL that sends a notification to the Java application whenever a new row is inserted into the table.
- Insert a new row into the table using any SQL client.
- Verify that the Java application receives the notification.
- It's recommended to have a clear understanding of PostgreSQL triggers and the concept of asynchronous notifications before attempting the lab exercises.
- Feel free to customize the provided SQL scripts and Java application according to your requirements.