Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type

Поиск
Список
Период
Сортировка
От Pavel Stehule
Тема Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type
Дата
Msg-id CAFj8pRBJqLzCQiu0E-5h2VUJJz5aidRfRxCTnyd4WBAbMB4iag@mail.gmail.com
обсуждение исходный текст
Ответ на Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general


po 9. 8. 2021 v 23:13 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:
Pavel Stehule <pavel.stehule@gmail.com> writes:
> Some errors like this, but not this can be detected by plpgsql_check
> https://github.com/okbob/plpgsql_check - probably the heuristic for type
> check is not complete.

STRICTMULTIASSIGNMENT would detect most cases of this, except that
the condition is checked too late.  We'd need to count the fields
*before* trying to assign values, not after.

I use some fragments of this routine. But the problem was so I did implicit unnesting, although plpgsql doesn't do this


postgres=# create or replace function broken_into()
returns void as $$
declare v typ2;
begin
  -- should to fail
  select (10,20)::typ2 into v;
  -- should be ok
  select ((10,20)::typ2).* into v;
  -- should to fail
  execute 'select (10,20)::typ2' into v;
  -- should be ok
  execute 'select ((10,20)::typ2).*' into v;
end;
$$ language plpgsql;
CREATE FUNCTION
postgres=# select * from plpgsql_check_function('broken_into', fatal_errors => false);
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                           plpgsql_check_function                                           │
╞════════════════════════════════════════════════════════════════════════════════════════════════════════════╡
│ error:42804:5:SQL statement:cannot cast composite value of "typ2" type to a scalar value of "integer" type │
│ warning:00000:5:SQL statement:too few attributes for composite variable                                    │
│ error:42804:9:EXECUTE:cannot cast composite value of "typ2" type to a scalar value of "integer" type       │
│ warning:00000:9:EXECUTE:too few attributes for composite variable                                          │
│ warning extra:00000:2:DECLARE:never read variable "v"                                                      │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
(5 rows)

Regards

Pavel




In the meantime, it does seem like the docs could be more explicit
about this, and perhaps give an example showing the (x).* solution.

                        regards, tom lane

В списке pgsql-general по дате отправления:

Предыдущее
От: Pavel Stehule
Дата:
Сообщение: Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type
Следующее
От: Pavel Stehule
Дата:
Сообщение: Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type