Search code examples
streamreal-timepipelinedb

How to stream output from a continous view in pipelinedb?


I have setup pipelinedb and it works great! I would like to know if its possible to stream data out of a continuous view after the value in the view has been updated? That is, have some external process act on changes to a view.

I wish to stream metrics generated from the views into a dashboard, and I do not want to use polling the db to achieve this.


Solution

  • As of 0.9.5, continuous triggers have been removed in favour of using output streams and continuous transforms. (First suggested by DidacticTactic). The output of a continuous view is essentially a stream, which means you can create continuous views or transforms based on it.

    Simple Example:

    1. First create a stream and continuous view.
    CREATE STREAM s (
        x int
    );
    
    CREATE CONTINUOUS VIEW hourly_cv AS
        SELECT
            hour(arrival_timestamp) AS ts,
            SUM(x) AS sum
        FROM s GROUP BY ts;
    
    1. Every continuous view now has a output stream. You can create a transform based on the output of the view using output_of. In the transform you have access to the tuples old and new which represent the old values and new values respectively. (0.9.7 has a third called delta) So you can create a transform that uses the output of 'hourly_cv' like so:
    CREATE CONTINUOUS TRANSFORM hourly_ct AS
        SELECT
            (new).sum
        FROM output_of('hourly_cv')
        THEN EXECUTE PROCEDURE update();
    
    1. In this example I'm calling update which we still need to define. It needs to be a function that returns a trigger.
    CREATE OR REPLACE FUNCTION update()
        RETURNS trigger AS
        $$
        BEGIN
            // Do anything you want here.
            RETURN NEW;
        END;
        $$
        LANGUAGE plpgsql;
    

    I found the 0.9.5 release notes blog post helpful to understand output streams and why continuous triggers are no more.