I have a PostgreSQL database. What I want to do is, detect any changes (insert, update) that happen in the database and then call a webservice. How could I do this?
Thanks in advance for any help.
I have a PostgreSQL database. What I want to do is, detect any changes (insert, update) that happen in the database and then call a webservice. How could I do this?
Thanks in advance for any help.
You should be able to use triggers and the listen/notify functionality in PostgreSQL to achieve something like this:
A set of insert/update/delete triggers create a notification event whenever anything changes in your table, using the created/changed/deleted ID as the payload.
A background process checks for notifications periodically (here's an example using Java/JDBC), and then loads the changed record from the database to do the web service call.
This is not in any way a real-time push-type system, but you have to poll the database for notification events to trigger the webservice call. It will do the trick, though.
SELECT 1;
. If you're using client-side polling you can poll quite quickly and it's pretty much real-time. This works in most language's database drivers, not just PgJDBC. –
Rainarainah LISTEN
and NOTIFY
is certainly the right approach. It's possible to use a plpythonu
, plperlu
, etc trigger to make web services calls from directly inside the database but (a) it's not transactional and (b) it'll do horrible things to your database's performance, especially if the web service is slow or becomes unresponsive. –
Rainarainah SELECT pg_notify('channel', 'payload')
instead of PERFORM pg_notify('channel', 'payload')
-- PostgreSQL does not allow you to silently discard the result of a SELECT. –
Stegodon NOTIFY message
directly, which does not allow a variable channel name. Also, support for a payload was only added in 9.0, so you'll have to do without. This makes @Craig's suggestion of using a history table plus notifications your best bet. –
Stegodon © 2022 - 2024 — McMap. All rights reserved.