PostgreSQL row change notify java program and vice versa
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
PostgreSQL does not push changed rows directly into a Java process by magic, but it does provide a lightweight signaling mechanism through LISTEN and NOTIFY. The usual pattern is to let a trigger send a small notification when a row changes, have Java listen on that channel, and let Java send notifications back with pg_notify when it needs to signal database-connected listeners.
Database to Java: Notify on Row Changes
The clean approach is to keep the notification payload small and send just enough information for the Java side to react. A trigger can publish the table, operation, and row identifier.
Now every row-level change produces a small JSON payload on the task_events channel.
Java Listening to PostgreSQL Notifications
The PostgreSQL JDBC driver exposes notifications through PGConnection. A simple listener looks like this:
In production, the Java listener usually treats the payload as a hint to refresh state, fetch the changed row by id, or wake up another processing step.
Java to PostgreSQL: Send a Notification Back
Java can also publish notifications by executing pg_notify or NOTIFY itself:
That is what "vice versa" usually means in practice: the Java program can publish on the same channel mechanism, and any PostgreSQL session currently listening can react.
Keep the Payload Small
NOTIFY is a signaling feature, not a bulk data transport layer. A good pattern is:
- send row ids, operation names, or small JSON payloads
- let the receiver fetch full row data if needed
- keep transactional meaning clear by committing promptly
That keeps the notification path cheap and predictable.
Common Pitfalls
- Expecting PostgreSQL to stream full changed rows automatically without a trigger or follow-up query.
- Sending huge payloads instead of small identifiers and metadata.
- Forgetting that notifications are delivered on transaction commit, not in the middle of an open transaction.
- Polling the database table constantly even though
LISTENandNOTIFYwere meant to avoid that pattern. - Treating notifications as durable message-queue storage. They are lightweight signals, not a replacement for a real queue.
Summary
- Use a trigger plus
pg_notifyto signal row changes from PostgreSQL to Java. - Use
PGConnectionandLISTENin Java to receive notifications. - Java can send notifications back with
pg_notifyas well. - Keep notification payloads small and fetch full row data separately when needed.
- Think of
LISTENandNOTIFYas signaling, not as durable event storage.
Related reading
- Postgresql slave for Mysql Master. Possible?
- PostgreSQL Slony replication - scheduled sync
- postgresql streaming replication -- continuous archiving?
- PostgreSQL V9.4 - Logical Decoding - SQL interface Starting at a specific LSN?
- POSTing a OneToMany sub-resource association in Spring Data REST
- Practical uses for AtomicInteger
- PouchDB - start local, replicate later
- pq could not resize shared memory segment. No space left on device

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.