Re: pl/pgsql errors when multi-dimensional arrays are used

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: pl/pgsql errors when multi-dimensional arrays are used
Дата
Msg-id 2987320.1619706703@sss.pgh.pa.us
обсуждение исходный текст
Ответ на pl/pgsql errors when multi-dimensional arrays are used  (PG Doc comments form <noreply@postgresql.org>)
Список pgsql-docs
PG Doc comments form <noreply@postgresql.org> writes:
> I have PostgreSQL 13. Let's declare the type below, then use it in
> pl/pgsql:

> create type typ1 as (
>     fld1 int[][]
> );

I think you have a fundamental misunderstanding of how multidimensional
arrays work in Postgres.  There's no separate type for 2-D vs 1-D arrays;
that is, the extra pair of brackets you wrote above is just noise.
What matters is what you put into the array at runtime, and the syntax
you use to do it.

>         a.fld1 = '{{121,122,123,124}}';        -- OK            (1)

Fine, you stored a 2-D array into fld1.

>         a.fld1[1] = '{221,222,223,224}';        -- fails        (2)

This fails on semantic grounds because a non-slice assignment or fetch
of an int array element must store or retrieve an int.  You're trying
to store an array slice, which requires that you use [m:n] subscript
notation.  It'd be correct to write either of

    a.fld1[1:4] = '{221,222,223,224}';
    a.fld1[1:] = '{221,222,223,224}';

which unfortunately plpgsql doesn't support in released versions
(that's fixed for v14 though).

            regards, tom lane



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

Предыдущее
От: PG Doc comments form
Дата:
Сообщение: Foreign Keys being able to reference same table not spelled out in documentation
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Foreign Keys being able to reference same table not spelled out in documentation