Database Triggers
Use database triggers to automatically start pgflow workflows when data changes, enabling event-driven architecture.
Basic Pattern
Section titled “Basic Pattern”The recommended pattern uses FOR EACH STATEMENT triggers with bulk processing:
-- Trigger functionCREATE OR REPLACE FUNCTION trigger_article_processing()RETURNS TRIGGER AS $$BEGIN -- Start one workflow per newly inserted article -- PERFORM FROM executes once per row, discards results PERFORM pgflow.start_flow( flow_slug => 'process_article', input => jsonb_build_object( 'articleId', new_articles.id, 'url', new_articles.url, 'site', new_articles.site ) ) FROM new_articles;
RETURN NULL;END;$$ LANGUAGE plpgsql;
-- Trigger fires once per INSERT statementCREATE TRIGGER on_new_articles AFTER INSERT ON articles REFERENCING NEW TABLE AS new_articles FOR EACH STATEMENT EXECUTE FUNCTION trigger_article_processing();Reacting to Status Changes
Section titled “Reacting to Status Changes”Trigger workflows when order status changes to “shipped”:
CREATE OR REPLACE FUNCTION on_order_shipped()RETURNS TRIGGER AS $$BEGIN -- Only process orders that just changed to shipped status PERFORM pgflow.start_flow( flow_slug => 'send_shipping_notification', input => jsonb_build_object( 'orderId', updated_orders.id, 'trackingNumber', updated_orders.tracking_number ) ) FROM new_orders AS updated_orders JOIN old_orders ON old_orders.id = updated_orders.id WHERE updated_orders.status = 'shipped' AND old_orders.status != 'shipped';
RETURN NULL;END;$$ LANGUAGE plpgsql;
CREATE TRIGGER on_order_status_change AFTER UPDATE OF status ON orders REFERENCING NEW TABLE AS new_orders OLD TABLE AS old_orders FOR EACH STATEMENT EXECUTE FUNCTION on_order_shipped();Learn More
Section titled “Learn More” Schedule Flows with pg_cron Run workflows on a recurring schedule