1

What is the difference between the following two ways to define an array?

select '{1, 2, 3, 4}', ARRAY[1, 2, 3, 4];

For example the docs gives the following two examples:

-- An array value can be replaced completely:

UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
    WHERE name = 'Carol';

-- or using the ARRAY expression syntax:

UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000]
    WHERE name = 'Carol';

Are there any differences between these two forms? https://www.postgresql.org/docs/current/arrays.html

1

1 Answer 1

1

There is no difference. Both result in the same array value.

One advantage of the array[] constructor syntax is, that the elements can be written with the usual syntax for SQL literals. E.g. array['One String', 'Other String'] vs. {"One String", "Other String"}'

Quote from the manual

Notice that the array elements are ordinary SQL constants or expressions; for instance, string literals are single quoted, instead of double quoted as they would be in an array literal.

Sign up to request clarification or add additional context in comments.

2 Comments

but they're not quite the same when I was testing. For example: select array[1]||array[2], '{1}'||'{2}' produces two different values.
Seems to require explicit casting for them to be treated the same, for example: '{1}'::int[]||'{2}'::int[].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.