To store an array of composite type (with any nesting level), you need a registered base type to work with. You could have a table defining the row type, or just create the type explicitly:
CREATE TYPE dd AS (a float8, b float8);
Here are some ways to construct that 2-dimensional array of yours:
SELECT ARRAY [['(1.23,23.4)'::dd]]
, (ARRAY [['(1.23,23.4)']])::dd[]
, '{{"(1.23,23.4)"}}'::dd[]
, ARRAY[ARRAY[dd '(1.23,23.4)']]
, ARRAY(SELECT ARRAY (SELECT dd '(1.23,23.4)'));
Related:
Note that the Postgres array type dd[]
can store values with any level of nesting. See:
Whether that's more efficient than just storing the string literal as text
very much depends on details of your use case.
- Arrays types occupy an overhead of 24 bytes plus the usual storage size of element values.
float8
(= double precision
) occupies 8 bytes. The text string '1' occupies 2 bytes on disk and 4 bytes in RAM. text
'123.45678' occupies 10 bytes on disk and 12 bytes in RAM.
- Simple
text
will be read and written a bit faster than an array type of equal size.
- Large
text
values are compressed (automatically), which can benefit storage size (especially with repetitive patterns) - but adds compression / decompression cost.
An actual Postgres array is cleaner in any case, as Postgres does not allow illegal strings to be stored.