我遇到了在postgresql RULES中执行(或不执行)某些查询的奇怪问题。这是Postgresql subquery execution within ON UPDATE rule (maybe bug in postgresql?)的伴随问题(但不同) - 但事实证明,在postgresql RULEs中存在一些相关问题,即某些查询根本没有执行,而只是查询执行顺序的问题。
考虑以下代码块:
DROP TABLE foo_table;
DROP TABLE bar_table;
--------------
CREATE TABLE foo_table (c1 text, c2 int);
CREATE TABLE bar_table (c1 text, c2 int);
--------------
CREATE OR REPLACE FUNCTION debug_immutable(anyelement) RETURNS integer AS $$
pg_raise('notice', 'debug(): ' . json_encode($args[0]));
return rand(1, 10000);
$$ LANGUAGE PLPHP IMMUTABLE;
CREATE OR REPLACE FUNCTION debug_volatile(anyelement) RETURNS integer AS $$
pg_raise('notice', 'debug(): ' . json_encode($args[0]));
return rand(1, 10000);
$$ LANGUAGE PLPHP VOLATILE;
-------------
CREATE OR REPLACE RULE foo_update_rule AS ON UPDATE TO foo_table DO INSTEAD
(
SELECT debug_immutable('debug_immutable call 1'::text); -- Query #1
SELECT debug_volatile('debug_volatile call 1'::text); -- Query #2
INSERT INTO foo_table (c1, c2) values ('foo', 123456); -- Query #3
INSERT INTO bar_table (c1, c2) values ('bar', 123456); -- Query #4
SELECT debug_immutable('debug_immutable call 2'::text); -- Query #5
SELECT debug_volatile('debug_volatile call 2'::text); -- Query #6
);
-----------------------------------------------
UPDATE foo_table SET c1 = NULL where c1 = 'aaa';
SELECT * FROM foo_table;
SELECT * FROM bar_table;
输出仅显示以下内容:
NOTICE: plphp: debug(): "debug_immutable call 1"
NOTICE: plphp: debug(): "debug_immutable call 2"
Total query runtime: 46 ms.
0 rows retrieved.
执行代码块后,foo_table和bar_table都显示为空:
# select * from foo_table;
c1 | c2
----+----
(0 rows)
# select * from bar_table;
c1 | c2
----+----
(0 rows)
输出意味着只执行查询#1和#5,并且从不执行查询#2,3,4和6。为什么是这样?如何更改上面的代码块,以便执行查询#2,3,4和6?
答案 0 :(得分:3)
在执行查询之前,在计划阶段评估不可变函数。根据需要评估挥发性物质。你返回零行,所以它们永远不会被调用。