Skip to content

Docs: include with list relations runs one query per relation; document the consistency it gives #5992

Open

Description

@t-kietzmann

Problem to Solve

find/findById with include resolve object relations with joins in the main statement, but each IncludeList (and each nested list) is fetched with a separate query after the main one: _deserializedMappedQuery runs _mappedResultsQuery and then _queryIncludedLists (serverpod_database/lib/src/adapters/postgres/database_connection.dart:837-870); _queryIncludedLists (:939-1046) builds one SelectQueryBuilder query per IncludeList with withWhereRelationInResultSet(ids, ...) (:962-991), recursively for nested lists (:993-999, :1026-1032). Without a transaction: every one of these statements goes to the pool (_resolveQueryContext, :832-835; _postgresConnection, :22), so they can run on different connections with different snapshots. With a transaction at the default READ COMMITTED isolation they share a connection, but each statement still takes its own snapshot. Only REPEATABLE READ or SERIALIZABLE give a consistent picture.

So a parent and its included children can come from different committed states (e.g. the parent's version n with children of n+1, or a child missing that a concurrent transaction moved). This is correct PostgreSQL behaviour, but the relations docs do not mention it, and include reads like a single read.

Proposal

Add a short section to the relations docs (and the dartdoc of include / IncludeList):

Object relations are loaded with joins in the same statement. List
relations are loaded with one additional query per relation and nesting
level. To get a consistent snapshot of a parent and its included lists, run
the query in a transaction with IsolationLevel.repeatableRead (or
serializable) and pass transaction:.

Optionally, if wanted later: an option to fetch lists with LEFT JOIN LATERAL / json_agg in the same statement.

Use Case

Readers that load one document with its lines (a header and its included list) to build a snapshot for clients. A find with includeList and no transaction can mix the header of one state with the lines of the next.

Alternatives

Running such reads in a short REPEATABLE READ transaction, which is what we will do. That needs the knowledge the docs do not give today; anyone reading include as "one object, one read" expects consistency without it.

Additional context

  • Checked against serverpod_database 4.1.0; unchanged in 4.1.1.
  • Related: #2802 (closed; includes honour the passed transaction).

How experienced are you with this library?

Expert - Experienced and comfortable with using this library in complex projects

Are you interested in working on a PR for this?

  • I want to work on this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions