Skip to content

Commit 902950e

Browse files
committed
return query execute support
1 parent 95ce1a2 commit 902950e

File tree

3 files changed

+439
-259
lines changed

3 files changed

+439
-259
lines changed

expected/plpgsql_check_active.out

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,3 +3806,72 @@ select * from plpgsql_check_function('dyn_sql_3');
38063806
(4 rows)
38073807

38083808
drop function dyn_sql_3();
3809+
create or replace function dyn_sql_3()
3810+
returns void as $$
3811+
declare r record;
3812+
begin
3813+
for r in execute 'select 1 as a, 2 as b'
3814+
loop
3815+
raise notice '%', r.a;
3816+
end loop;
3817+
end
3818+
$$ language plpgsql;
3819+
-- should be ok
3820+
select * from plpgsql_check_function('dyn_sql_3');
3821+
plpgsql_check_function
3822+
------------------------
3823+
(0 rows)
3824+
3825+
drop function dyn_sql_3();
3826+
create or replace function dyn_sql_3()
3827+
returns void as $$
3828+
declare r record;
3829+
begin
3830+
for r in execute 'select 1 as a, 2 as b'
3831+
loop
3832+
raise notice '%', r.c;
3833+
end loop;
3834+
end
3835+
$$ language plpgsql;
3836+
-- should be error
3837+
select * from plpgsql_check_function('dyn_sql_3');
3838+
plpgsql_check_function
3839+
-------------------------------------------------
3840+
error:42703:6:RAISE:record "r" has no field "c"
3841+
Context: SQL statement "SELECT r.c"
3842+
(2 rows)
3843+
3844+
drop function dyn_sql_3();
3845+
create or replace function dyn_sql_4()
3846+
returns table(ax int, bx int) as $$
3847+
begin
3848+
return query execute 'select 10, 20';
3849+
return;
3850+
end;
3851+
$$ language plpgsql;
3852+
-- should be ok
3853+
select * from plpgsql_check_function('dyn_sql_4()');
3854+
plpgsql_check_function
3855+
------------------------
3856+
(0 rows)
3857+
3858+
create or replace function dyn_sql_4()
3859+
returns table(ax int, bx int) as $$
3860+
begin
3861+
return query execute 'select 10, 20, 30';
3862+
return;
3863+
end;
3864+
$$ language plpgsql;
3865+
select * from dyn_sql_4();
3866+
ERROR: structure of query does not match function result type
3867+
DETAIL: Number of returned columns (3) does not match expected column count (2).
3868+
CONTEXT: PL/pgSQL function dyn_sql_4() line 3 at RETURN QUERY
3869+
-- should be error
3870+
select * from plpgsql_check_function('dyn_sql_4()');
3871+
plpgsql_check_function
3872+
-----------------------------------------------------------------------------------
3873+
error:42804:3:RETURN QUERY:structure of query does not match function result type
3874+
Detail: Number of returned columns (3) does not match expected column count (2).
3875+
(2 rows)
3876+
3877+
drop function dyn_sql_4();

sql/plpgsql_check_active.sql

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,3 +2767,61 @@ $$ language plpgsql;
27672767
select * from plpgsql_check_function('dyn_sql_3');
27682768

27692769
drop function dyn_sql_3();
2770+
2771+
create or replace function dyn_sql_3()
2772+
returns void as $$
2773+
declare r record;
2774+
begin
2775+
for r in execute 'select 1 as a, 2 as b'
2776+
loop
2777+
raise notice '%', r.a;
2778+
end loop;
2779+
end
2780+
$$ language plpgsql;
2781+
2782+
-- should be ok
2783+
select * from plpgsql_check_function('dyn_sql_3');
2784+
2785+
drop function dyn_sql_3();
2786+
2787+
create or replace function dyn_sql_3()
2788+
returns void as $$
2789+
declare r record;
2790+
begin
2791+
for r in execute 'select 1 as a, 2 as b'
2792+
loop
2793+
raise notice '%', r.c;
2794+
end loop;
2795+
end
2796+
$$ language plpgsql;
2797+
2798+
-- should be error
2799+
select * from plpgsql_check_function('dyn_sql_3');
2800+
2801+
drop function dyn_sql_3();
2802+
2803+
create or replace function dyn_sql_4()
2804+
returns table(ax int, bx int) as $$
2805+
begin
2806+
return query execute 'select 10, 20';
2807+
return;
2808+
end;
2809+
$$ language plpgsql;
2810+
2811+
-- should be ok
2812+
select * from plpgsql_check_function('dyn_sql_4()');
2813+
2814+
create or replace function dyn_sql_4()
2815+
returns table(ax int, bx int) as $$
2816+
begin
2817+
return query execute 'select 10, 20, 30';
2818+
return;
2819+
end;
2820+
$$ language plpgsql;
2821+
2822+
select * from dyn_sql_4();
2823+
2824+
-- should be error
2825+
select * from plpgsql_check_function('dyn_sql_4()');
2826+
2827+
drop function dyn_sql_4();

0 commit comments

Comments
 (0)