Created
January 31, 2026 21:50
-
-
Save malikbenkirane/1582361dc63470728d63540f7f79e35b to your computer and use it in GitHub Desktop.
PostgreSQL's LISTEN/NOTIFY mechanism can be used with Go to implement a simple publish/subscribe (pub/sub) system for inter-process communication.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "fmt" | |
| "log" | |
| "://github.com" | |
| "://github.com/pgconn" | |
| ) | |
| func main() { | |
| ctx := context.Background() | |
| connStr := "postgresql://user:password@localhost:5432/dbname" | |
| // 1. Establish a dedicated connection for listening | |
| conn, err := pgx.Connect(ctx, connStr) | |
| if err != nil { | |
| log.Fatal("Unable to connect to database:", err) | |
| } | |
| defer conn.Close(ctx) | |
| // 2. Start listening on a specific channel | |
| _, err = conn.Exec(ctx, "LISTEN events") | |
| if err != nil { | |
| log.Fatal("Error listening to channel:", err) | |
| } | |
| fmt.Println("Listening for notifications on 'events' channel...") | |
| // 3. Wait for notifications in a loop | |
| for { | |
| notification, err := conn.WaitForNotification(ctx) | |
| if err != nil { | |
| log.Fatal("Error waiting for notification:", err) | |
| } | |
| fmt.Printf("Received notification on channel '%s': %s (PID: %d)\n", | |
| notification.Channel, notification.Payload, notification.PID) | |
| // Process the payload, e.g., unmarshal JSON | |
| // var data map[string]interface{} | |
| // json.Unmarshal([]byte(notification.Payload), &data) | |
| // ... | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE OR REPLACE FUNCTION notify_event() RETURNS TRIGGER AS $$ | |
| DECLARE | |
| notification JSON; | |
| BEGIN | |
| -- Build a JSON payload with table, action, and data | |
| notification := json_build_object( | |
| 'table', TG_TABLE_NAME, | |
| 'action', TG_OP, | |
| 'data', row_to_json(NEW) | |
| ); | |
| -- Notify the 'events' channel with the JSON payload | |
| PERFORM pg_notify('events', notification::text); | |
| RETURN NULL; | |
| END; | |
| $$ LANGUAGE plpgsql; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TRIGGER users_after_update_trigger | |
| AFTER INSERT OR UPDATE OR DELETE ON users | |
| FOR EACH ROW EXECUTE PROCEDURE notify_event(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment