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

From Tom Lane
Subject Re: pl/pgsql errors when multi-dimensional arrays are used
Date
Msg-id 2987320.1619706703@sss.pgh.pa.us
Whole thread Raw
In response to pl/pgsql errors when multi-dimensional arrays are used  (PG Doc comments form <noreply@postgresql.org>)
List 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 by date:

Previous
From: PG Doc comments form
Date:
Subject: Foreign Keys being able to reference same table not spelled out in documentation
Next
From: Tom Lane
Date:
Subject: Re: Foreign Keys being able to reference same table not spelled out in documentation