How to "PERFORM" CTE queries in PL/pgSQL?

Viewed 3048

I tried to simulate my problem in the code example below. In the code below, I am doing a select * from test in a procedure. As we know, we must use the perform keyword for this. This works great:

perform * from test;

However, if I try to rewrite that simple query as a CTE, I cannot get it working. I am getting a syntax error.

with test_as_cte as(select * from test) perform * from test_as_cte;

Is this possible? What is the correct syntax? I tried several alternatives and going through documentation, but without any success so far.

(Please note that this is just an example to explain my problem. I know the queries do not really make any sense.)

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;
1 Answers
Related