Timescale supports the full range of PostgreSQL triggers. Creating, altering, or dropping triggers on a hypertable propagates the changes to all of the underlying chunks.
This example creates a new table called
error_conditions with the same schema
conditions, but that only stores records which are considered errors. An
error, in this case, is when an application sends a
reading with a value that is greater than or equal to 1000.
Create a function that inserts erroneous data into the
error_conditionstable:CREATE OR REPLACE FUNCTION record_error()RETURNS trigger AS $record_error$BEGINIF NEW.temperature >= 1000 OR NEW.humidity >= 1000 THENINSERT INTO error_conditionsVALUES(NEW.time, NEW.location, NEW.temperature, NEW.humidity);END IF;RETURN NEW;END;$record_error$ LANGUAGE plpgsql;
Create a trigger that calls this function whenever a new row is inserted into the hypertable:CREATE TRIGGER record_errorBEFORE INSERT ON conditionsFOR EACH ROWEXECUTE PROCEDURE record_error();
All data is inserted into the
conditionstable, but rows that contain errors are also added to the
Timescale supports the full range of triggers, including
BEFORE DELETE, and
AFTER DELETE. For more information, see the
Found an issue on this page?Report an issue!