Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

Parents without child rows get {NULL} or [null] instead of an empty list

Your error

array_agg returns {NULL} instead of {} for rows with no match2 more lines it can show

Fix it fast

Most common cause, 1 of 3

array_agg collects the outer join's NULL filler row

Drop the filler row from the aggregate with FILTER on the child's key.

Fix
SELECT a.id,
       coalesce(array_agg(b.title) FILTER (WHERE b.id IS NOT NULL), '{}') AS titles
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
GROUP BY a.id;
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

array_agg gives {NULL} and json_agg gives [null] for a parent with no matching rows because the LEFT JOIN still returns one row for that parent with NULL in every child column, and both aggregates keep nulls. Add FILTER (WHERE child.id IS NOT NULL) and wrap the result in coalesce(..., '{}') or '[]'. Unchanged in PostgreSQL 15 to 19.

  • Applies to PostgreSQL 15 to 19 (19.0, 18.7, 17.12, 16.16, 15.20), json_agg_strict and jsonb_agg_strict: PostgreSQL 16 and later
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run the join without the aggregate for one parent that shows the bad result. A single row with every child column NULL is the outer join's filler row; a child id with a NULL value column is a real row holding NULL.

Run this first
SELECT a.id, b.id AS book_id, b.title
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
WHERE a.id = 42;

Then find the line you see in this list and open its fix:

  1. 1array_agg collects the outer join's NULL filler rowThe first-step query returns exactly one row for the parent, with book_id NULL
  2. 2json_agg or jsonb_agg turns the same filler row into [null]An API response shows "books": [null] for parents that have no books
  3. 3Matching rows exist, but the aggregated column is NULL in some of themThe first-step query shows a book_id with title NULL, and the array mixes real values with NULL

What does the error look like?

  • array_agg returns {NULL} instead of {} for rows with no match
  • json_agg returns [null] instead of [] after a LEFT JOIN
  • array_agg result contains NULL elements next to real values

Cause 1 of 3

array_agg collects the outer join's NULL filler row

You seeThe first-step query returns exactly one row for the parent, with book_id NULL

Check

A {NULL} array for a childless parent comes from LEFT JOIN semantics: for each left row with no match the join adds one row whose right-hand columns are all NULL, so every parent keeps at least one row. array_agg collects all input values including nulls, so that filler row becomes a one-element array holding NULL.

Check
SELECT a.id, array_agg(b.title) AS titles
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
GROUP BY a.id
ORDER BY a.id;

Fix

Drop the filler row from the aggregate with FILTER on the child's key. A filtered group then has no input rows, and array_agg over no rows returns NULL rather than an empty array, so wrap it in coalesce. array_remove(array_agg(...), NULL) is a shorter form that also returns {}.

Fix
SELECT a.id,
       coalesce(array_agg(b.title) FILTER (WHERE b.id IS NOT NULL), '{}') AS titles
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
GROUP BY a.id;

Verify

Authors without books now show {} and authors with books keep their full list.

Verify
SELECT count(*) FILTER (WHERE titles = '{}') AS empty_lists
FROM (SELECT a.id,
             coalesce(array_agg(b.title) FILTER (WHERE b.id IS NOT NULL), '{}') AS titles
      FROM authors a LEFT JOIN books b ON b.author_id = a.id
      GROUP BY a.id) t;

Confirmed by postgresql.org

Did this fix it?

Cause 2 of 3

json_agg or jsonb_agg turns the same filler row into [null]

You seeAn API response shows "books": [null] for parents that have no books

Check

The JSON version of the problem has the same root: json_agg and jsonb_agg collect every input value, nulls included, so the outer join's NULL row is written out as null inside the JSON array. Grouping alone cannot remove it because the row is real join output.

Check
SELECT a.id, json_agg(b.title) AS books
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
GROUP BY a.id;

Fix

Use the same FILTER on the child key and replace the empty-group NULL with an empty JSON array. On PostgreSQL 16 and later, json_agg_strict and jsonb_agg_strict skip null inputs; keep the coalesce with them as well, so a parent without children never comes back as NULL.

Fix
SELECT a.id,
       coalesce(json_agg(b.title) FILTER (WHERE b.id IS NOT NULL), '[]'::json) AS books
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
GROUP BY a.id;

Verify

Childless authors now return [] and the JSON parses as an empty list in the client.

Verify
SELECT json_typeof('[]'::json);   -- array

Confirmed by postgresql.org

Did this fix it?

Cause 3 of 3

Matching rows exist, but the aggregated column is NULL in some of them

You seeThe first-step query shows a book_id with title NULL, and the array mixes real values with NULL

Check

If NULL elements appear next to real values, the join is fine and the child table itself stores NULL in the aggregated column. Filtering on the child key keeps those rows, so the nulls stay until you filter on the column you aggregate.

Check
SELECT count(*) FROM books WHERE title IS NULL;

Fix

Filter on the aggregated column itself, or strip nulls after aggregating with array_remove, which compares with IS NOT DISTINCT FROM and can therefore remove NULL.

Fix
SELECT a.id,
       coalesce(array_agg(b.title) FILTER (WHERE b.title IS NOT NULL), '{}') AS titles
FROM authors a
LEFT JOIN books b ON b.author_id = a.id
GROUP BY a.id;

-- or
SELECT a.id, array_remove(array_agg(b.title), NULL) AS titles
FROM authors a LEFT JOIN books b ON b.author_id = a.id
GROUP BY a.id;

Verify

No array in the result contains NULL any more.

Verify
SELECT array_remove(ARRAY['a', NULL, 'b'], NULL);   -- {a,b}

Confirmed by postgresql.org

Did this fix it?

How do I stop it coming back?

  • Write every aggregate over the nullable side of a LEFT JOIN as coalesce(agg(...) FILTER (WHERE child.id IS NOT NULL), empty value) from the start.
  • Add a test fixture with a parent that has no children, so [null] fails the test instead of reaching the API.
  • Declare columns that should never be empty as NOT NULL, so stored NULLs cannot leak into arrays.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.