postgresql 中的更新语句后未触发规则
按照 www.postgresql.org 中的指南
我创建了此语句:
CREATE TABLE shoelace_log (
sl_name text, -- shoelace changed
sl_avail integer, -- new available value
log_who text, -- who did it
log_when timestamp -- when);
CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
WHERE NEW.sl_avail <> OLD.sl_avail
DO INSERT INTO shoelace_log VALUES (
NEW.sl_name,
NEW.sl_avail,
current_user,
current_timestamp
);
当我尝试在 pgAdmin3 查询控制台中执行查询时:
UPDATE shoelace_data SET sl_avail = 6 WHERE sl_name = 'sl7';
仅执行更新查询,并且不会触发规则。我尝试使用命令 EXPLAIN 来理解原因。
EXPLAIN UPDATE shoelace_log SET sl_avail = 6 WHERE sl_name = 'sl7';
日志显示:
"Seq Scan on shoelace_log (cost=0.00..19.66 rows=4 width=32)"
" Filter: (sl_name = 'sl7'::text)"
"Seq Scan on shoelace_log (cost=0.00..19.62 rows=4 width=78)"
" Filter: (sl_name = 'sl7'::text)"
我还尝试将 VERBOSE 选项与 EXPLAIN 一起使用,但我无法理解为什么我的事件没有被触发。
Following the guide in www.postgresql.org
I created this statement:
CREATE TABLE shoelace_log (
sl_name text, -- shoelace changed
sl_avail integer, -- new available value
log_who text, -- who did it
log_when timestamp -- when);
CREATE RULE log_shoelace AS ON UPDATE TO shoelace_data
WHERE NEW.sl_avail <> OLD.sl_avail
DO INSERT INTO shoelace_log VALUES (
NEW.sl_name,
NEW.sl_avail,
current_user,
current_timestamp
);
When i try to execute the query in pgAdmin3 query console:
UPDATE shoelace_data SET sl_avail = 6 WHERE sl_name = 'sl7';
Only the update query are executed, and the rule isn't fired. I try to use the command EXPLAIN to understand why.
EXPLAIN UPDATE shoelace_log SET sl_avail = 6 WHERE sl_name = 'sl7';
The log says:
"Seq Scan on shoelace_log (cost=0.00..19.66 rows=4 width=32)"
" Filter: (sl_name = 'sl7'::text)"
"Seq Scan on shoelace_log (cost=0.00..19.62 rows=4 width=78)"
" Filter: (sl_name = 'sl7'::text)"
I also try to use VERBOSE option with EXPLAIN, but i can't understand why my event isn't trigged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您选择了错误的功能。完全忘记规则——它们很难,几乎不可能做好。
这里你需要的是一个触发器:
尝试一下:
You have chosen wrong feature. Forget rules altogether — they're very hard, almost impossible to do right.
What you need here is a trigger:
Try it:
您可能还想考虑使用为您执行此操作的预构建插件:
http://pgfoundry.org/项目/表日志/
You might also want to consider using the pre-built addon that does that for you:
http://pgfoundry.org/projects/tablelog/
我尝试了这个例子,它成功了。但我必须先创建hoelace_data 表。该表不在示例中。
现在,如果您从日志表中进行选择。 。 。
请仔细注意,该规则规定当 shoelace_data 中的 sl_avail 列有更新时插入到 shoelace_log 中。
I tried the example, and it worked. But I had to create the shoelace_data table first. That table isn't in the example.
Now if you select from the log table . . .
Note carefully that the rule says to insert into shoelace_log when there's an update to the sl_avail column in shoelace_data.