在古生物学数据库的数据库中,我具有以下表定义:
CREATE TABLE taxon (
id integer DEFAULT NEXTVAL('taxon_oid_seq') PRIMARY KEY,
taxon varchar(100) UNIQUE NOT NULL,
reino varchar(50) NOT NULL,
phylum varchar(100) ,
subphylum varchar(100) ,
classe varchar(100) ,
subclasse varchar(100) ,
superordem varchar(100) ,
ordem varchar(100) ,
subordem varchar(100) ,
infraordem varchar(100) ,
familia varchar(100) ,
subfamilia varchar(100) ,
genero varchar(100) ,
especie varchar(100) ,
subespecie varchar(100) );
分类单元字段将自动填充有可能确定给定物种分类的最低级别。为了实现这一点,我有这个触发器:
CREATE OR REPLACE FUNCTION get_taxon() RETURNS TRIGGER LANGUAGE
plpgsql AS $BODY$
BEGIN
NEW.taxon := coalesce(NEW.subespecie, NEW.especie, NEW.genero, NEW.subfamilia,
NEW.familia, NEW.infraordem, NEW.subordem, NEW.ordem, NEW.superordem,
NEW.subclasse, NEW.classe, NEW.subphylum, NEW.phylum, NEW.reino);
RETURN NEW;
END;
$BODY$
VOLATILE;
CREATE TRIGGER update_taxon
BEFORE INSERT OR UPDATE ON taxon
FOR EACH ROW EXECUTE PROCEDURE get_taxon();
但是此触发器仅在INSERT
上触发,如果制作了UPDATE
,则什么也不会发生。在UPDATE
的情况下如何触发此触发器?
我正在写答案,因为我的评论不适合:
如果创建表和触发器,然后执行INSERT
和UPDATE
,则会得到:
foo=# select * from taxon;
id | taxon | reino | phylum | subphylum | classe | subclasse | superordem | ordem | subordem | infraordem | familia | subfamilia | genero | especie | subespecie
----+-------+-------+--------+-----------+--------+-----------+------------+-------+----------+------------+---------+------------+--------+---------+------------
(0 rows)
foo=# explain analyze insert into taxon (reino) values ('sapienz');
QUERY PLAN
-----------------------------------------------------------------------------------------------
Insert on taxon (cost=0.00..0.01 rows=1 width=495) (actual time=0.111..0.111 rows=0 loops=1)
-> Result (cost=0.00..0.01 rows=1 width=495) (actual time=0.062..0.063 rows=1 loops=1)
Planning Time: 0.034 ms
Trigger update_taxon: time=0.026 calls=1
Execution Time: 0.137 ms
(5 rows)
foo=# select * from taxon;
id | taxon | reino | phylum | subphylum | classe | subclasse | superordem | ordem | subordem | infraordem | familia | subfamilia | genero | especie | subespecie
----+---------+---------+--------+-----------+--------+-----------+------------+-------+----------+------------+---------+------------+--------+---------+------------
1 | sapienz | sapienz | | | | | | | | | | | | |
(1 row)
foo=# explain analyze update taxon set reino = 'sapien' where id = 1;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------
Update on taxon (cost=0.14..8.16 rows=1 width=1005) (actual time=0.134..0.135 rows=0 loops=1)
-> Index Scan using taxon_pkey on taxon (cost=0.14..8.16 rows=1 width=1005) (actual time=0.017..0.019 rows=1 loops=1)
Index Cond: (id = 1)
Planning Time: 0.100 ms
Trigger update_taxon: time=0.039 calls=1
Execution Time: 0.162 ms
(6 rows)
foo=# select * from taxon;
id | taxon | reino | phylum | subphylum | classe | subclasse | superordem | ordem | subordem | infraordem | familia | subfamilia | genero | especie | subespecie
----+--------+--------+--------+-----------+--------+-----------+------------+-------+----------+------------+---------+------------+--------+---------+------------
1 | sapien | sapien | | | | | | | | | | | | |
(1 row)
如您所见,在两个EXPLAIN ANALYZE
调用中均触发了触发器–您能否使用有关如何重现所看到的行为的更多详细信息来更新您的问题?顺便说一句,我正在使用PostgreSQL v。12.0