Parents without child rows get {NULL} or [null] instead of an empty list
Fix it fast
Most common cause, 1 of 3array_agg collects the outer join's NULL filler row
Drop the filler row from the aggregate with FILTER on the child's key.
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;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.
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.
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:
- 1array_agg collects the outer join's NULL filler row
The first-step query returns exactly one row for the parent, with book_id NULL - 2json_agg or jsonb_agg turns the same filler row into [null]
An API response shows "books": [null] for parents that have no books - 3Matching rows exist, but the aggregated column is NULL in some of them
The 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 matchjson_agg returns [null] instead of [] after a LEFT JOINarray_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.
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 {}.
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.
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
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.
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.
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.
SELECT json_typeof('[]'::json); -- arrayConfirmed by postgresql.org
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.
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.
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.
SELECT array_remove(ARRAY['a', NULL, 'b'], NULL); -- {a,b}Confirmed by postgresql.org
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.