WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PostgreSQL · Advanced · question 57 of 100

How do you use the NOTIFY and LISTEN features in PostgreSQL to implement publish-subscribe patterns?

📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

The NOTIFY and LISTEN features in PostgreSQL can be used to implement a simple publish-subscribe messaging system. This feature allows PostgreSQL to proactively notify interested clients when certain events occur in the database. Here’s how you can use PostgreSQL’s NOTIFY and LISTEN features to implement a publish-subscribe pattern:

1. First, create a table where notifications will be sent. You could create a simple table named ‘notifications‘ with a ‘message‘ column:

    CREATE TABLE notifications (
      message text
    );

2. In the publisher (producer) application, use the ‘NOTIFY‘ command to send a notification message to the database whenever a specific event occurs. For example, let’s say you have an application that handles user registrations. Whenever a new user registers, a notification message is sent to the ‘notifications‘ table:

    try (Connection connection = DriverManager.getConnection(url, user, password);
         PreparedStatement statement = connection.prepareStatement(
               "INSERT INTO notifications (message) VALUES (?)");
         ) {
      // Perform user registration logic
      // ...
      
      // Send notification event to database
      statement.setString(1, "New user registered: " + username);
      statement.executeUpdate();
      
      // Cleanup resources
      // ...
    }

3. In the subscriber (consumer) application, use the ‘LISTEN‘ command to register interest in a specific notification event. The subscriber can then wait for incoming notifications using the ‘PGConnection‘ interface’s ‘getNotifications()‘ method:

    PGConnection connection = (PGConnection) DriverManager.getConnection(url, user, password);
    connection.addNotificationListener((int processId, String channelName, String message) -> {
        System.out.println("Received notification on channel " + channelName + ": " + message);
    });

    connection.unwrap(BaseConnection.class).getNotifications();

4. Whenever the publisher’s ‘NOTIFY‘ command is called and a new event is broadcasted to the ‘notifications‘ table, the ‘addNotificationListener‘ method in the subscriber’s application will capture the event notification and print out the message.

This simple approach can be used to implement more complex publish-subscribe patterns by modifying the table schema, adding additional columns, and filtering events based on specific criteria.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PostgreSQL interview — then scores it.
📞 Practice PostgreSQL — free 15 min
📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

All 100 PostgreSQL questions · All topics