Sub topic 3
Reading PostgreSQL Array Elements and Slices with 1-Based Subscripts
Assume you know basic SELECT statements, table definitions, and SQL NULL behavior. This chapter uses PostgreSQL array columns and focuses on reading individual elements and ranges of elements.
A quick array setup
An array column stores multiple values of one data type in a single column. PostgreSQL uses square brackets after the element type:
CREATE TEMP TABLE posts (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
title text NOT NULL,
tags text[] NOT NULL DEFAULT '{}'
);
INSERT INTO posts (title, tags) VALUES
('Intro to arrays', ARRAY['sql', 'postgres']),
('Indexing', '{postgres,indexing,gin}');Both rows contain text[] values. The ARRAY[...] constructor is usually easier to read when values come from SQL expressions. The '{...}' form is a text representation of an array.
For ordinary arrays created this way, the first position is 1, not 0.
Reading one element
Use a subscript to select one position:
SELECT
title,
tags[1] AS first_tag,
tags[2] AS second_tag
FROM posts;The result is similar to this:
| title | first_tag | second_tag |
|---|---|---|
| Intro to arrays | sql | postgres |
| Indexing | postgres | indexing |
The expression tags[1] returns one text value. The expression tags[2] returns the second value.
You can also use a subscript in a WHERE clause:
SELECT title
FROM posts
WHERE tags[1] = 'postgres';This finds rows whose first tag is postgres. It does not search every tag. For searches across all elements, use operators such as @> or ANY, which you will see in a later chapter.
Reading a range with a slice
A slice uses a lower and upper bound separated by a colon:
SELECT
title,
tags[1:2] AS first_two_tags
FROM posts;A slice returns an array, not a single element. For the first row, tags[1:2] is {'sql','postgres'}.
This distinction matters when passing the result to an array function or comparing it with another array:
SELECT
title,
tags[2:3] AS later_tags
FROM posts;The second row has values at positions 1, 2, and 3, so its slice contains indexing and gin. The first row has no position 3, so its slice contains only postgres.
A slice can use a bound beyond the array's end. PostgreSQL returns the available values instead of raising an error.
Missing positions and NULL
Reading a position outside an array's bounds returns NULL:
SELECT
title,
tags[10] AS tenth_tag
FROM posts;There is no tenth tag in either row, so tenth_tag is NULL.
This can affect filtering. The predicate below does not return true for rows without a tenth tag:
SELECT title
FROM posts
WHERE tags[10] = 'special';The comparison is NULL = 'special', which is NULL. A WHERE clause keeps only rows where its condition is TRUE.
Use IS NULL when you need to test for a missing element:
SELECT title
FROM posts
WHERE tags[10] IS NULL;A stored NULL element and a missing position both produce NULL when read. If you need to distinguish them, inspect the array's bounds and contents rather than relying only on the subscript result.
Subscripts are normally 1-based
PostgreSQL arrays normally start at index 1:
SELECT
tags[1] AS first,
tags[2] AS second
FROM posts;Writing tags[0] does not return the first value. For these arrays, it returns NULL.
PostgreSQL can represent arrays with lower bounds other than 1, although this is uncommon. Functions such as array_lower and array_upper reveal the actual bounds:
SELECT
array_lower(tags, 1) AS lower_bound,
array_upper(tags, 1) AS upper_bound
FROM posts;The second argument, 1, means the first dimension. For normal one-dimensional arrays, the result is usually lower bound 1 and an upper bound equal to the number of elements.
Choosing a subscript or a slice
Use a subscript when you need one known position. Use a slice when you need a consecutive range and want to keep the result as an array.
Exercise
What do tags[1] and tags[1:1] return, and how are they different?
Show answer
tags[1] returns one element of type text. tags[1:1] returns a one-element array of type text[].