创建触发器

TimescaleDB supports the full range of PostgreSQL triggers, and creating, altering, or dropping triggers on the hypertable will similarly propagate these changes to all of a hypertable's constituent chunks.

TimescaleDb支持所有PostgreSQL触发器,同样地,hyper table中创建、修改或者删除的触发器都会传递到其所有的区块中。

In the following example, let's say you want to create a new table ​error_conditions​ with the same schema as ​conditions​, but designed to only store records which are deemed erroneous, where an application signals a sensor error by sending a ​temperature​ or ​humidity​ having a value >= 1000.

接下来的例子中,我们假定你需要创建一个与​conditions表有相同数据库对象集合的​error_conditions表来记录错误信息(当温度或者湿度值>=1000时,传感器发出错误信息)。

So, we'll take a two-step approach. First, let's create a function that will insert data deemed erroneous into this second table:

所以我们需要操作两个步骤,第一,创建一个可在​error_conditions表中插入错误数据的方法:

CREATE OR REPLACE FUNCTION record_error()
  RETURNS trigger AS $record_error$
BEGIN
 IF NEW.temperature >= 1000 OR NEW.humidity >= 1000 THEN
   INSERT INTO error_conditions
     VALUES(NEW.time, NEW.location, NEW.temperature, NEW.humidity);
 END IF;
 RETURN NEW;
END;
$record_error$ LANGUAGE plpgsql;

Second, create a trigger that will call this function whenever a new row is inserted into the hypertable.

第二,创建一个触发器,在hypertable中插入新行时触发该方法。

CREATE TRIGGER record_error
  BEFORE INSERT ON conditions
  FOR EACH ROW
  EXECUTE PROCEDURE record_error();

Now, all data is inserted into the ​conditions​ data, but any row deemed erroneous is ​also​ added to the ​error_conditions​table.

现在,所有的数据都插入到了​conditions​数据中,但是任何错误信息数据都将会被插入到error_conditions​ 表中。

TimescaleDB supports the full gamut of triggers: ​BEFORE INSERT​, ​AFTER INSERT​, ​BEFORE UPDATE​, ​AFTER UPDATE​, ​BEFORE DELETE​, ​AFTER DELETE​. For additional information, see the ​PostgreSQL docs​.

TimescaleDB支持所有的触发器:​BEFORE INSERT​, ​AFTER INSERT​, ​BEFORE UPDATE​, ​AFTER UPDATE​, ​BEFORE DELETE​, ​AFTER DELETE​。更多信息请查阅PostgreSQL文档。

results matching ""

    No results matching ""