-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
CREATE TABLE hyper (
  time BIGINT NOT NULL,
  device_id TEXT NOT NULL,
  sensor_1 NUMERIC NULL DEFAULT 1
);
CREATE OR REPLACE FUNCTION test_trigger()
    RETURNS TRIGGER LANGUAGE PLPGSQL AS
$BODY$
DECLARE
    cnt INTEGER;
BEGIN
    SELECT count(*) INTO cnt FROM hyper;
    RAISE WARNING 'FIRING trigger when: % level: % op: % cnt: % trigger_name %',
        tg_when, tg_level, tg_op, cnt, tg_name;

    IF TG_OP = 'DELETE' THEN
        RETURN OLD;
    END IF;
    RETURN NEW;
END
$BODY$;
-- row triggers: BEFORE
CREATE TRIGGER _0_test_trigger_insert
    BEFORE INSERT ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update
    BEFORE UPDATE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete
    BEFORE delete ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER z_test_trigger_all
    BEFORE INSERT OR UPDATE OR DELETE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
-- row triggers: AFTER
CREATE TRIGGER _0_test_trigger_insert_after
    AFTER INSERT ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_insert_after_when_dev1
    AFTER INSERT ON hyper
    FOR EACH ROW
    WHEN (NEW.device_id = 'dev1')
    EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update_after
    AFTER UPDATE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete_after
    AFTER delete ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER z_test_trigger_all_after
    AFTER INSERT OR UPDATE OR DELETE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
-- statement triggers: BEFORE
CREATE TRIGGER _0_test_trigger_insert_s_before
    BEFORE INSERT ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update_s_before
    BEFORE UPDATE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete_s_before
    BEFORE DELETE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
-- statement triggers: AFTER
CREATE TRIGGER _0_test_trigger_insert_s_after
    AFTER INSERT ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update_s_after
    AFTER UPDATE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete_s_after
    AFTER DELETE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
-- CONSTRAINT TRIGGER
CREATE CONSTRAINT TRIGGER _0_test_trigger_constraint_insert
  AFTER INSERT ON hyper FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE CONSTRAINT TRIGGER _0_test_trigger_constraint_update
  AFTER UPDATE ON hyper FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE CONSTRAINT TRIGGER _0_test_trigger_constraint_delete
  AFTER DELETE ON hyper FOR EACH ROW EXECUTE FUNCTION test_trigger();
SELECT * FROM create_hypertable('hyper', 'time', chunk_time_interval => 10);
 hypertable_id | schema_name | table_name | created 
---------------+-------------+------------+---------
             1 | public      | hyper      | t

--test triggers before create_hypertable
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987600000000000, 'dev1', 1);
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: INSERT cnt: 0 trigger_name _0_test_trigger_insert_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 0 trigger_name _0_test_trigger_insert
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 0 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_after_when_dev1
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_s_after
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, 'dev2', 1), (1257987800000000000, 'dev2', 1);
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_insert
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 1 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 2 trigger_name _0_test_trigger_insert
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 2 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_insert_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_insert_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: INSERT cnt: 3 trigger_name _0_test_trigger_insert_s_after
UPDATE hyper SET sensor_1 = 2;
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_s_after
DELETE FROM hyper;
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: DELETE cnt: 3 trigger_name _0_test_trigger_delete_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 3 trigger_name _0_test_trigger_delete
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 2 trigger_name _0_test_trigger_delete
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 2 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 1 trigger_name _0_test_trigger_delete
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 1 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_s_after
--test drop trigger
DROP TRIGGER _0_test_trigger_insert ON hyper;
DROP TRIGGER _0_test_trigger_insert_s_before ON hyper;
DROP TRIGGER _0_test_trigger_insert_after ON hyper;
DROP TRIGGER _0_test_trigger_insert_s_after ON hyper;
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987600000000000, 'dev1', 1);
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 0 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_after_when_dev1
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name z_test_trigger_all_after
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, 'dev2', 1), (1257987800000000000, 'dev2', 1);
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 1 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 2 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name z_test_trigger_all_after
DROP TRIGGER _0_test_trigger_update ON hyper;
DROP TRIGGER _0_test_trigger_update_s_before ON hyper;
DROP TRIGGER _0_test_trigger_update_after ON hyper;
DROP TRIGGER _0_test_trigger_update_s_after ON hyper;
UPDATE hyper SET sensor_1 = 2;
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
DROP TRIGGER _0_test_trigger_delete ON hyper;
DROP TRIGGER _0_test_trigger_delete_s_before ON hyper;
DROP TRIGGER _0_test_trigger_delete_after ON hyper;
DROP TRIGGER _0_test_trigger_delete_s_after ON hyper;
DELETE FROM hyper;
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 2 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 1 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
DROP TRIGGER z_test_trigger_all ON hyper;
DROP TRIGGER z_test_trigger_all_after ON hyper;
--test create trigger on hypertable
-- row triggers: BEFORE
CREATE TRIGGER _0_test_trigger_insert
    BEFORE INSERT ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update
    BEFORE UPDATE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete
    BEFORE delete ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER z_test_trigger_all
    BEFORE INSERT OR UPDATE OR DELETE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
-- row triggers: AFTER
CREATE TRIGGER _0_test_trigger_insert_after
    AFTER INSERT ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update_after
    AFTER UPDATE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete_after
    AFTER delete ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
CREATE TRIGGER z_test_trigger_all_after
    AFTER INSERT OR UPDATE OR DELETE ON hyper
    FOR EACH ROW EXECUTE FUNCTION test_trigger();
-- statement triggers: BEFORE
CREATE TRIGGER _0_test_trigger_insert_s_before
    BEFORE INSERT ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update_s_before
    BEFORE UPDATE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete_s_before
    BEFORE DELETE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
-- statement triggers: AFTER
CREATE TRIGGER _0_test_trigger_insert_s_after
    AFTER INSERT ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_update_s_after
    AFTER UPDATE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER _0_test_trigger_delete_s_after
    AFTER DELETE ON hyper
    FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987600000000000, 'dev1', 1);
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: INSERT cnt: 0 trigger_name _0_test_trigger_insert_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 0 trigger_name _0_test_trigger_insert
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 0 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_after_when_dev1
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 1 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_s_after
INSERT INTO hyper(time, device_id,sensor_1) VALUES
(1257987700000000000, 'dev2', 1), (1257987800000000000, 'dev2', 1);
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: INSERT cnt: 1 trigger_name _0_test_trigger_insert_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 1 trigger_name _0_test_trigger_insert
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 1 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 2 trigger_name _0_test_trigger_insert
WARNING:  FIRING trigger when: BEFORE level: ROW op: INSERT cnt: 2 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_insert_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_constraint_insert
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name _0_test_trigger_insert_after
WARNING:  FIRING trigger when: AFTER level: ROW op: INSERT cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: INSERT cnt: 3 trigger_name _0_test_trigger_insert_s_after
UPDATE hyper SET sensor_1 = 2;
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update
WARNING:  FIRING trigger when: BEFORE level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_constraint_update
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_after
WARNING:  FIRING trigger when: AFTER level: ROW op: UPDATE cnt: 3 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: UPDATE cnt: 3 trigger_name _0_test_trigger_update_s_after
DELETE FROM hyper;
WARNING:  FIRING trigger when: BEFORE level: STATEMENT op: DELETE cnt: 3 trigger_name _0_test_trigger_delete_s_before
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 3 trigger_name _0_test_trigger_delete
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 3 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 2 trigger_name _0_test_trigger_delete
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 2 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 1 trigger_name _0_test_trigger_delete
WARNING:  FIRING trigger when: BEFORE level: ROW op: DELETE cnt: 1 trigger_name z_test_trigger_all
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_constraint_delete
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_after
WARNING:  FIRING trigger when: AFTER level: ROW op: DELETE cnt: 0 trigger_name z_test_trigger_all_after
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: DELETE cnt: 0 trigger_name _0_test_trigger_delete_s_after
CREATE TABLE vehicles (
  vehicle_id INTEGER PRIMARY KEY,
  vin_number CHAR(17),
  last_checkup TIMESTAMP
);
CREATE TABLE color (
  color_id INTEGER PRIMARY KEY,
  notes text
);
CREATE TABLE location (
  time TIMESTAMP NOT NULL,
  vehicle_id INTEGER REFERENCES vehicles (vehicle_id),
  color_id INTEGER, --no reference since gonna populate a hypertable
  latitude FLOAT,
  longitude FLOAT
);
CREATE OR REPLACE FUNCTION create_vehicle_trigger_fn()
    RETURNS TRIGGER LANGUAGE PLPGSQL AS
$BODY$
BEGIN
    INSERT INTO vehicles VALUES(NEW.vehicle_id, NULL, NULL) ON CONFLICT DO NOTHING;
    RETURN NEW;
END
$BODY$;
CREATE OR REPLACE FUNCTION create_color_trigger_fn()
    RETURNS TRIGGER LANGUAGE PLPGSQL AS
$BODY$
BEGIN
    --test subtxns within triggers
    BEGIN
        INSERT INTO color VALUES(NEW.color_id, 'n/a');
    EXCEPTION WHEN unique_violation THEN
            -- Nothing to do, just continue
    END;
    RETURN NEW;
END
$BODY$;
CREATE TRIGGER create_color_trigger
    BEFORE INSERT OR UPDATE ON location
    FOR EACH ROW EXECUTE FUNCTION create_color_trigger_fn();
SELECT create_hypertable('location', 'time');
WARNING:  column type "timestamp without time zone" used for "time" does not follow best practices
   create_hypertable   
-----------------------
 (2,public,location,t)

--make color also a hypertable
SELECT create_hypertable('color', 'color_id', chunk_time_interval=>10);
 create_hypertable  
--------------------
 (3,public,color,t)

-- Test that we can create and use triggers with another user
GRANT TRIGGER, INSERT, SELECT, UPDATE ON location TO :ROLE_DEFAULT_PERM_USER_2;
GRANT SELECT, INSERT, UPDATE ON color TO :ROLE_DEFAULT_PERM_USER_2;
GRANT SELECT, INSERT, UPDATE ON vehicles TO :ROLE_DEFAULT_PERM_USER_2;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER_2;
CREATE TRIGGER create_vehicle_trigger
    BEFORE INSERT OR UPDATE ON location
    FOR EACH ROW EXECUTE FUNCTION create_vehicle_trigger_fn();
INSERT INTO location VALUES('2017-01-01 01:02:03', 1, 1, 40.7493226,-73.9771259);
INSERT INTO location VALUES('2017-01-01 01:02:04', 1, 20, 24.7493226,-73.9771259);
INSERT INTO location VALUES('2017-01-01 01:02:03', 23, 1, 40.7493226,-73.9771269);
INSERT INTO location VALUES('2017-01-01 01:02:03', 53, 20, 40.7493226,-73.9771269);
UPDATE location SET vehicle_id = 52 WHERE vehicle_id = 53;
SELECT * FROM location;
           time           | vehicle_id | color_id |  latitude  |  longitude  
--------------------------+------------+----------+------------+-------------
 Sun Jan 01 01:02:03 2017 |          1 |        1 | 40.7493226 | -73.9771259
 Sun Jan 01 01:02:04 2017 |          1 |       20 | 24.7493226 | -73.9771259
 Sun Jan 01 01:02:03 2017 |         23 |        1 | 40.7493226 | -73.9771269
 Sun Jan 01 01:02:03 2017 |         52 |       20 | 40.7493226 | -73.9771269

SELECT * FROM vehicles;
 vehicle_id | vin_number | last_checkup 
------------+------------+--------------
          1 |            | 
         23 |            | 
         53 |            | 
         52 |            | 

SELECT * FROM color;
 color_id | notes 
----------+-------
        1 | n/a
       20 | n/a

-- switch back to default user to run some dropping tests
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER;
\set ON_ERROR_STOP 0
-- test that disable trigger is disallowed
ALTER TABLE location DISABLE TRIGGER create_vehicle_trigger;
ERROR:  hypertables do not support  enabling or disabling triggers.
\set ON_ERROR_STOP 1
-- test that drop trigger works
DROP TRIGGER create_color_trigger ON location;
DROP TRIGGER create_vehicle_trigger ON location;
-- test that drop trigger doesn't cause leftovers that mean that dropping chunks or hypertables no longer works
SELECT count(1) FROM pg_depend d WHERE d.classid = 'pg_trigger'::regclass AND NOT EXISTS (SELECT 1 FROM pg_trigger WHERE oid = d.objid);
 count 
-------
     0

DROP TABLE location;
-- test triggers with transition tables
-- test creating hypertable from table with triggers with transition tables
CREATE TABLE transition_test(time timestamptz NOT NULL);
CREATE TRIGGER t1_stmt AFTER INSERT ON transition_test REFERENCING NEW TABLE AS new_trans FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER t1_row AFTER INSERT ON transition_test REFERENCING NEW TABLE AS new_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
-- We do not support ROW triggers with transition tables, so we need
-- to remove it to be able to create the hypertable.
\set ON_ERROR_STOP 0
SELECT create_hypertable('transition_test','time');
ERROR:  ROW triggers with transition tables are not supported on hypertables
\set ON_ERROR_STOP 1
DROP TRIGGER t1_row ON transition_test;
SELECT create_hypertable('transition_test','time');
      create_hypertable       
------------------------------
 (4,public,transition_test,t)

-- Insert some rows to create a chunk
INSERT INTO transition_test values ('2020-01-10');
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: INSERT cnt: 0 trigger_name t1_stmt
SELECT chunk FROM show_chunks('transition_test') tbl(chunk) limit 1 \gset
-- test creating trigger with transition tables on existing hypertable
CREATE TRIGGER t3 AFTER UPDATE ON transition_test REFERENCING NEW TABLE AS new_trans OLD TABLE AS old_trans FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
CREATE TRIGGER t4 AFTER DELETE ON transition_test REFERENCING OLD TABLE AS old_trans FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
INSERT INTO transition_test values ('2020-01-11');
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: INSERT cnt: 0 trigger_name t1_stmt
COPY transition_test FROM STDIN;
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: INSERT cnt: 0 trigger_name t1_stmt
UPDATE transition_test SET time = '2020-01-12' WHERE time = '2020-01-11';
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: UPDATE cnt: 0 trigger_name t3
DELETE FROM transition_test WHERE time = '2020-01-12';
WARNING:  FIRING trigger when: AFTER level: STATEMENT op: DELETE cnt: 0 trigger_name t4
\set ON_ERROR_STOP 0
CREATE TRIGGER t3 AFTER UPDATE ON :chunk REFERENCING NEW TABLE AS new_trans OLD TABLE AS old_trans FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
ERROR:  triggers with transition tables are not supported on hypertable chunks
CREATE TRIGGER t4 AFTER DELETE ON :chunk REFERENCING OLD TABLE AS old_trans FOR EACH STATEMENT EXECUTE FUNCTION test_trigger();
ERROR:  triggers with transition tables are not supported on hypertable chunks
CREATE TRIGGER t5 AFTER INSERT ON transition_test REFERENCING NEW TABLE AS new_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
ERROR:  ROW triggers with transition tables are not supported on hypertables
CREATE TRIGGER t6 AFTER UPDATE ON transition_test REFERENCING NEW TABLE AS new_trans OLD TABLE AS old_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
ERROR:  ROW triggers with transition tables are not supported on hypertables
CREATE TRIGGER t7 AFTER DELETE ON transition_test REFERENCING OLD TABLE AS old_trans FOR EACH ROW EXECUTE FUNCTION test_trigger();
ERROR:  ROW triggers with transition tables are not supported on hypertables
\set ON_ERROR_STOP 1
