假设,我的数据库中有 2 个表(postgresql-9.x)
CREATE TABLE FOLDER (
KEY BIGSERIAL PRIMARY KEY,
PATH TEXT,
NAME TEXT
);
CREATE TABLE FOLDERFILE (
FILEID BIGINT,
PATH TEXT,
PATHKEY BIGINT
);
每当我插入或更新时FOLDERFILE.PATHKEY
,我都会自动更新:FOLDER.KEY
FOLDERFILE
CREATE OR REPLACE FUNCTION folderfile_fill_pathkey() RETURNS trigger AS $$
DECLARE
pathkey bigint;
changed boolean;
BEGIN
IF tg_op = 'INSERT' THEN
changed := TRUE;
ELSE IF old.FILEID != new.FILEID THEN
changed := TRUE;
END IF;
END IF;
IF changed THEN
SELECT INTO pathkey key FROM FOLDER WHERE PATH = new.path;
IF FOUND THEN
new.pathkey = pathkey;
ELSE
new.pathkey = NULL;
END IF;
END IF;
RETURN new;
END
$$ LANGUAGE plpgsql VOLATILE;
CREATE TRIGGER folderfile_fill_pathkey_trigger AFTER INSERT OR UPDATE
ON FOLDERFILE FOR EACH ROW EXECUTE PROCEDURE fcliplink_fill_pathkey();
所以问题是关于函数folderfile_fill_pathkey()
波动性。文件说
任何有副作用的函数都必须标记为 VOLATILE
但据我了解 - 此函数不会更改它所依赖的表中的任何数据,因此我可以将此函数标记为IMMUTABLE
. 对吗?
FOLDERFILE
如果我在同一个事务中批量插入许多行,那么 IMMUTABLE 触发器函数会不会有任何问题,例如:
BEGIN;
INSERT INTO FOLDERFILE ( ... );
...
INSERT INTO FOLDERFILE ( ... );
COMMIT;