Is it possible to write a generic trigger for all tables in Postgres? - postgresql

I want to listen to every transaction that is recorded in the database And I noticed that there is a concept in Postgres under the title LISTEN and NOTIFY (Of course, I am not sure that this is the right way)
Now I want to write a trigger that sends a notification to the channel when any operation occurs in any table
Is it possible?
My way is right?
thanks
Postgres TRIGGER to call NOTIFY with a JSON payload

Related

Perform function after notify in postgresql

If a notification is sent in postgresql, how can I trigger a function by using listen? I know it can be done in another programming language, and I know you can send triggers after changes to tables, but how do I only trigger a function after a notification inside postgresql? Something like:
LISTEN <channel> THEN PERFORM ...

How to make Postgres row update trigger a RPC call

I have a Postgres database that will be constantly updated by one service, and I have another service that will perform some post processing for some scenario. What I want to achieve is that whenever one specific column of a row is updated, my RPC endpoint will be called. I am wondering what options I have?

callback on table update in kdb

I want to implement a client server mechanism in kdb where clients can register them self to receive a callback when some table is updated.
I know how the callback work in kdb, I was not able to figure how to bind table updates in server to a function from where I can call 'callback' from client.
Basically you want to implement 'Publish-Subscribe' mechanism. KDB already has a script 'u.q' in tick library which provides that:
https://code.kx.com/q/cookbook/publish-subscribe/
On server, it maintains list of clients along with their handles, subscription tables and callback functions. You will have to change function on server which handles data insert/update to also publish the data.
q) .u.pub[table name; table data]
This will take care of calling each client's callback function which are registered for this table.
On client side, create the connection to publisher and call the library function for subscription.
q) .u.sub[tablename;list_of_symbols_to_subscribe_to]
You can also look into example publisher and subscriber code: https://github.com/KxSystems/cookbook/tree/master/pubsub

How a Java client app. can "catch" (via JDBC) the result produced by a trigger procedure query?

I'm trying to understand how a java (client) application that communicates, through JDBC, with a pgSQL database (server) can "catch" the result produced by a query that will be fired (using a trigger) whenever a record is inserted into a table.
So, to clarify, via JDBC I install a trigger procedure prepared to execute a query whenever a record is inserted into a given database table, and from this query's execution will result an output (wrapped in a resultSet, I suppose). And my problem is that I have no idea how the client will be aware of those results, that are asynchronously produced.
I wonder if JDBC supports any "callback" mechanism able to catch the results produced by a query that is fired through a trigger procedure under the "INSERT INTO table" condition. And if there is no such "callback" mechanism, what is the best approach to achieve this result?
Thank you in advance :)
Triggers can't return a resultset.
There's no way to send such a result to the JDBC driver.
There are a few dirty hacks you can use to get results from a trigger to the client, but they're all exactly that. Things like:
DECLARE a cursor for the resultset, then send the cursor name as a NOTIFY payload, so the app can FETCH ALL FROM <cursorname>;
Create a TEMPORARY table and report the name via NOTIFY
It is more typical to append anything the trigger needs to communicate to the app to a table that exists for that purpose and have the app SELECT from it after the operation that fired the trigger ran.
In most cases if you need to do this, you're probably using a trigger where a regular function is a better fit.

NSMutableURLRequest on succession of another NSMutableURLRequest's success

Basically, I want to implement SYNC functionality; where, if internet connection is not available, data gets stored on local sqlite database. Whenever, internet connection is available, SYNC gets into the action.
Now, Say for example; 5 records are stored locally, and then internet connection is available. I want the server to be updated. So, What I do currently is:
Post first record to the server.
Wait for the success of first request.
Post local NSNotification to routine, that the first record has been updated on server & now second request can go.
The routine fires the second post request on server and so on...
Question: Is this approach right and efficient enough to implement SYNC functionality; OR anything I should change into it ??
NOTE: Records to be SYNC will have no limit in numbers.
Well it depends on the requirements on the data that you save. If it is just for backup then you should be fine.
If the 5 records are somehow dependent on each other and you need to access this data from another device/application you should take care on the server side that either all 5 records are written or none. Otherwise you will have an inconsistent state if only 3 get written.
If other users are also reading / writing those data concurrently on the server then you need to implement some kind of lock on all records before writing and also decide how to handle conflicts when someone attempts to overwrite somebody else changes.