WITH deleted_files AS (SELECT name FROM files WHERE id = $1), updated_links AS ( UPDATE links l SET object = JSONB_SET(object, '{files}', (SELECT jsonb_agg(elem) FROM files nested, jsonb_array_elements(object -> 'files') elem WHERE nested.id = l.id AND elem ->> 'name' != (SELECT name FROM deleted_files)), true) RETURNING id, object)DELETE FROM links WHERE id IN (SELECT id FROM updated_links WHERE object IS NULL);
I am using Postgres 14 and running this query inside of a Goland query session with READ COMMITTED isolation level.
The idea of this query is to:
- Get deleted file names by
id
- Update the
object
JSONB column in the tablelinks
- Delete rows from
links
where after this update the value ofobject
(JSONB field) isNULL
.
As a result of this query, I still see rows with NULL object
fields even though if I just do something like SELECT id FROM updated_links WHERE object is NULL
instead of the delete statement, I get the right results.
It seems that the statement after CTEs doesn't yet see the updated columns but that is a bit strange given that the changes are apparently visible to SELECT statements.
What am I missing here?