使用PERFORM CTE查询Postgres plpgsql

Car*_*rts 7 postgresql plpgsql common-table-expression database-trigger

我试着在下面的代码示例中模拟我的问题.在下面的代码中,我正在执行select * from test一个过程.我们知道,我们必须使用perform关键字.这非常有效:

perform * from test;
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试将该简单查询重写为CTE,我无法使其正常工作.我收到语法错误.

with test_as_cte as(select * from test) perform * from test_as_cte;
Run Code Online (Sandbox Code Playgroud)

这可能吗?什么是正确的语法?我尝试了几种替代方案并通过文档,但到目前为止没有任何成功.

(请注意,这只是解释我的问题的一个例子.我知道查询没有任何意义.)

create table test
(
    key int primary key  
);

create function test() returns trigger as
$$
begin
    raise notice 'hello there';
    -- this does work
    perform * from test;
    -- this doesn't work
    with test_as_cte as(select * from test) perform * from test_as_cte;
    return new;
end;
$$
language plpgsql;

create trigger test after insert on test for each row execute procedure test();

insert into test(key) select 1;
Run Code Online (Sandbox Code Playgroud)

Vao*_*sun 7

尝试:

perform (with test_as_cte as(select * from test) select * from test_as_cte);
Run Code Online (Sandbox Code Playgroud)

我从不认为您可能需要CTE并忽略结果,但是如果需要,从“选择不返回”逻辑开始考虑执行会导致上述语义。或perform * from (CTE)或类似