Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql : Trigger in background

I need to have my trigger to run after the field s updated or to run in background. Since for now as soon as the trigger is fired the UI is blocked until the procedure in the trigger itself is executed. Anyway to make the trigger in such a way that as soon as the field is updated, the UI do not block away.

I have tried:

CREATE CONSTRAINT TRIGGER property_created_simple_prod_trigger
    AFTER UPDATE ON properties DEFERRABLE INITIALLY DEFERRED
    FOR EACH ROW 
EXECUTE PROCEDURE simple_production_materialized_view_procedure_trigger();

But hard luck nothing works.

like image 358
Syed Asad Abbas Zaidi Avatar asked Nov 09 '22 04:11

Syed Asad Abbas Zaidi


1 Answers

PostgreSQL doesn't have background procedures/triggers, so you can't do that directly.

What I suggest us having the trigger send a NOTIFY that your application LISTENs to. Have the application's LISTENing thread be a separate connection, managed by a separate thread, that does background processing when it receives notifications.

like image 173
Craig Ringer Avatar answered Nov 15 '22 10:11

Craig Ringer