Use triggers on distributed hypertables

Triggers on distributed hypertables work in much the same way as triggers on regular hypertables. They have the same limitations. But there are some differences due to the data being distributed across multiple nodes:

  • Row-level triggers fire on the data node where the row is inserted. The triggers must fire where the data is stored, because BEFORE and AFTER row triggers need access to the stored data. The chunks on the access node do not contain any data, so they have no triggers.
  • Statement-level triggers fire once on each affected node, including the access node. For example, if a distributed hypertable includes 3 data nodes, inserting 2 rows of data executes a statement-level trigger on the access node and either 1 or 2 data nodes, depending on whether the rows go to the same or different nodes.
  • A replication factor greater than 1 further causes the trigger to fire on multiple nodes. Each replica node fires the trigger.

Create a trigger on a distributed hypertable

Create a trigger on a distributed hypertable by using CREATE TRIGGER as usual. The trigger, and the function it executes, is automatically created on each data node. If the trigger function references any other functions or objects, they need to be present on all nodes before you create the trigger.

Creating a trigger on a distributed hypertable

  1. If your trigger needs to reference another function or object, use distributed_exec to create the function or object on all nodes.
  2. Create the trigger function on the access node. This example creates a dummy trigger that raises the notice ‘trigger fired’:

    1. CREATE OR REPLACE FUNCTION my_trigger_func()
    2. RETURNS TRIGGER LANGUAGE PLPGSQL AS
    3. $BODY$
    4. BEGIN
    5. RAISE NOTICE 'trigger fired';
    6. RETURN NEW;
    7. END
    8. $BODY$;
  3. Create the trigger itself on the access node. This example causes the trigger to fire whenever a row is inserted into the hypertable hyper. Note that you don’t need to manually create the trigger on the data nodes. This is done automatically for you.

    1. CREATE TRIGGER my_trigger
    2. AFTER INSERT ON hyper
    3. FOR EACH ROW
    4. EXECUTE FUNCTION my_trigger_func();

Avoid processing a trigger multiple times

If you have a statement-level trigger, or a replication factor greater than 1, the trigger fires multiple times. To avoid repetitive firing, you can set the trigger function to check which data node it is executing on.

For example, write a trigger function that raises a different notice on the access node compared to a data node:

  1. CREATE OR REPLACE FUNCTION my_trigger_func()
  2. RETURNS TRIGGER LANGUAGE PLPGSQL AS
  3. $BODY$
  4. DECLARE
  5. is_access_node boolean;
  6. BEGIN
  7. SELECT is_distributed INTO is_access_node
  8. FROM timescaledb_information.hypertables
  9. WHERE hypertable_name = <TABLE_NAME>
  10. AND hypertable_schema = <TABLE_SCHEMA>;
  11. IF is_access_node THEN
  12. RAISE NOTICE 'trigger fired on the access node';
  13. ELSE
  14. RAISE NOTICE 'trigger fired on a data node';
  15. END IF;
  16. RETURN NEW;
  17. END
  18. $BODY$;