plpgsql - execute - cannot use a reference to record field

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

plpgsql - execute - cannot use a reference to record field

Pavel Stehule
Hi

Is there reason why following code should not to work?

do $$
declare r record; result int;
begin
  select 10 as a, 20 as b into r;
  raise notice 'a: %', r.a;
  execute 'select $1.a + $1.b' into result using r;
  raise notice '%', result;
end;
$$

but it fails

NOTICE:  a: 10
ERROR:  could not identify column "a" in record data type
LINE 1: select $1.a + $1.b
               ^
QUERY:  select $1.a + $1.b
CONTEXT:  PL/pgSQL function inline_code_block line 6 at EXECUTE

Regards

Pavel
Reply | Threaded
Open this post in threaded view
|

Re: plpgsql - execute - cannot use a reference to record field

Tom Lane-2
Pavel Stehule <[hidden email]> writes:
> Is there reason why following code should not to work?

> do $$
> declare r record; result int;
> begin
>   select 10 as a, 20 as b into r;
>   raise notice 'a: %', r.a;
>   execute 'select $1.a + $1.b' into result using r;
>   raise notice '%', result;
> end;
> $$

You can't select fields by name out of an unspecified record.
The EXECUTE'd query is not particularly different from

regression=# prepare foo(record) as select $1.a + $1.b;
psql: ERROR:  could not identify column "a" in record data type
LINE 1: prepare foo(record) as select $1.a + $1.b;
                                      ^

and surely you wouldn't expect that to work.
(The fact that either of the previous lines work is
thanks to plpgsql-specific hacking.)

                        regards, tom lane


Reply | Threaded
Open this post in threaded view
|

Re: plpgsql - execute - cannot use a reference to record field

Pavel Stehule


po 15. 4. 2019 v 18:07 odesílatel Tom Lane <[hidden email]> napsal:
Pavel Stehule <[hidden email]> writes:
> Is there reason why following code should not to work?

> do $$
> declare r record; result int;
> begin
>   select 10 as a, 20 as b into r;
>   raise notice 'a: %', r.a;
>   execute 'select $1.a + $1.b' into result using r;
>   raise notice '%', result;
> end;
> $$

You can't select fields by name out of an unspecified record.
The EXECUTE'd query is not particularly different from

regression=# prepare foo(record) as select $1.a + $1.b;
psql: ERROR:  could not identify column "a" in record data type
LINE 1: prepare foo(record) as select $1.a + $1.b;
                                      ^

and surely you wouldn't expect that to work.
(The fact that either of the previous lines work is
thanks to plpgsql-specific hacking.)

yes. I looking to the code and I see so SPI_execute_with_args doesn't allow push typmods there.

Regards

Pavel




                        regards, tom lane