这里有一个用 C 编写的触发器函数的非常简单的例子(用过程语言编写的触发器的例子可以在过程语言的文档中找到)。
如果该命令试图向列x
中插入一个空值,函数trigf
报告表ttest
中的行数并且跳过实际的操作(这样该触发器会作为一个非空约束但不会中止事务)。
首先,表定义:
CREATE TABLE ttest (
x integer
);
这是该触发器函数的源代码:
#include "postgres.h"
#include "fmgr.h"
#include "executor/spi.h"
#include "commands/trigger.h"
#include "utils/rel.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(trigf);
Datum
trigf(PG_FUNCTION_ARGS)
{
TriggerData *trigdata = (TriggerData *) fcinfo->context;
TupleDesc tupdesc;
HeapTuple rettuple;
char *when;
bool checknull = false;
bool isnull;
int ret, i;
if (!CALLED_AS_TRIGGER(fcinfo))
elog(ERROR, "trigf: not called by trigger manager");
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
rettuple = trigdata->tg_newtuple;
else
rettuple = trigdata->tg_trigtuple;
if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)
&& TRIGGER_FIRED_BEFORE(trigdata->tg_event))
checknull = true;
if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
when = "before";
else
when = "after ";
tupdesc = trigdata->tg_relation->rd_att;
if ((ret = SPI_connect()) < 0)
elog(ERROR, "trigf (fired %s): SPI_connect returned %d", when, ret);
ret = SPI_exec("SELECT count(*) FROM ttest", 0);
if (ret < 0)
elog(ERROR, "trigf (fired %s): SPI_exec returned %d", when, ret);
i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],
SPI_tuptable->tupdesc,
1,
&isnull));
elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i);
SPI_finish();
if (checknull)
{
SPI_getbinval(rettuple, tupdesc, 1, &isnull);
if (isnull)
rettuple = NULL;
}
return PointerGetDatum(rettuple);
}
在你编译了该源代码(见第 37.10.5 节)之后,声明该函数和触发器:
CREATE FUNCTION trigf() RETURNS trigger
AS "filename
"
LANGUAGE C;
CREATE TRIGGER tbefore BEFORE INSERT OR UPDATE OR DELETE ON ttest
FOR EACH ROW EXECUTE FUNCTION trigf();
CREATE TRIGGER tafter AFTER INSERT OR UPDATE OR DELETE ON ttest
FOR EACH ROW EXECUTE FUNCTION trigf();
现在你可以测试该触发器的操作:
=> INSERT INTO ttest VALUES (NULL);
INFO: trigf (fired before): there are 0 rows in ttest
INSERT 0 0
-- Insertion skipped and AFTER trigger is not fired
=> SELECT * FROM ttest;
x
---
(0 rows)
=> INSERT INTO ttest VALUES (1);
INFO: trigf (fired before): there are 0 rows in ttest
INFO: trigf (fired after ): there are 1 rows in ttest
^^^^^^^^
remember what we said about visibility.
INSERT 167793 1
vac=> SELECT * FROM ttest;
x
---
1
(1 row)
=> INSERT INTO ttest SELECT x * 2 FROM ttest;
INFO: trigf (fired before): there are 1 rows in ttest
INFO: trigf (fired after ): there are 2 rows in ttest
^^^^^^
remember what we said about visibility.
INSERT 167794 1
=> SELECT * FROM ttest;
x
---
1
2
(2 rows)
=> UPDATE ttest SET x = NULL WHERE x = 2;
INFO: trigf (fired before): there are 2 rows in ttest
UPDATE 0
=> UPDATE ttest SET x = 4 WHERE x = 2;
INFO: trigf (fired before): there are 2 rows in ttest
INFO: trigf (fired after ): there are 2 rows in ttest
UPDATE 1
vac=> SELECT * FROM ttest;
x
---
1
4
(2 rows)
=> DELETE FROM ttest;
INFO: trigf (fired before): there are 2 rows in ttest
INFO: trigf (fired before): there are 1 rows in ttest
INFO: trigf (fired after ): there are 0 rows in ttest
INFO: trigf (fired after ): there are 0 rows in ttest
^^^^^^
remember what we said about visibility.
DELETE 2
=> SELECT * FROM ttest;
x
---
(0 rows)
在src/test/regress/regress.c
和spi中有更多复杂的例子。
40.2.1. SELECT规则如何工作40.2.2. 非SELECT语句中的视图规则40.2.3. PostgreSQL中视图的能力40.2.4. 更新一个视图 Postgre...
触发器函数也可以用 PL/Tcl 编写。PostgreSQL要求能作为触发器被调用的函数必须被声明为没有参数并且返回类型为trigger。来自于...
PL/Python 中的函数通过标准的CREATE FUNCTION语法声明:CREATE FUNCTION funcname (argument-list)RETURNS return-typeAS $$# P...
目录46.1. 接口函数SPI_connect — 连接一个C函数到 SPI 管理器SPI_finish — 将一个C函数从 SPI 管理器断开SPI_execute — 执行...
Frameset onload Event Frameset 对象定义和用法onload 事件在页面载入完成后被触发。语法onload="JavaScriptCode"参数描述JavaS...
Audio played 属性 Audio 对象实例获得音频中以秒计的首段已播放的范围(部分):var x = document.getElementById("myAudio");d...
Style paddingLeft 属性 Style 对象定义和用法paddingLeft 属性设置或返回元素的左内边距。语法设置 paddingLeft 属性:Object.s...
Style marginTop 属性 Style 对象定义和用法marginTop 属性设置或返回元素的上外边距。语法设置 marginTop 属性:Object.style.m...
Style overflow 属性 Style 对象定义和用法overflow 属性设置或返回如何处理呈现在元素框外面的内容。语法设置 overflow 属性:O...
Style tabSize属性 Style 对象实例设置一个 pre 元素的 tabSize:document.getElementById(myPRE).style.tabSize=16;定义和用法t...