Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres plpgsql with PERFORM CTE queries

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;
like image 733
Caroline Kwerts Avatar asked Jul 04 '17 12:07

Caroline Kwerts


1 Answers

try:

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

I would never think you might need CTE and ignore the result, but if you need, thinking of perform as of "select no return" logic leads to above semantics. or perform * from (CTE) or alike

like image 141
Vao Tsun Avatar answered Nov 07 '22 07:11

Vao Tsun