Skip to content

Support native PostgreSQL array columns for lists of scalars (List<int> → bigint[]) #5991

Open

Description

@t-kietzmann

Problem to Solve

TypeDefinition.databaseType (serverpod_cli/lib/src/generator/types.dart:549-581) maps scalar Dart types to column types (int → bigint at line 559, further scalars, vectors, geography) and everything else, including List<int>, List<String> and List<UuidValue>, to json (or jsonb): if (isJsonbSerialized) return 'jsonb'; return 'json'; (lines 579-580). The column takes exactly this type (serverpod_cli/lib/src/database/create_definition.dart:33-43). ColumnType (serverpod_database/lib/src/models/column_type.spy.yaml: text, boolean, integer, doublePrecision, timestampWithoutTimeZone, bytea, bigint, uuid, json, unknown, vector, halfvec, sparsevec, bit, jsonb, geography, ...) has no array type, so a model cannot declare a native array column, and the migration generator cannot create one. Today List<int> is always a json/jsonb column.

Native arrays matter for compact storage of id sets (bigint[] is 8 bytes per element plus a small header, JSON text is several times larger and goes through TOAST earlier), for = ANY(column) and && queries, and for GIN indexes on the array.

Proposal

An opt-in per field, keeping today's default:

fields:
  holes: List<int>, array      # bigint[]
  tags: List<String>, array    # text[]

with a new ColumnType per element type (or an isArray flag on the column definition), migration support, and query operators such as t.holes.contains(x) (= ANY) and t.holes.overlaps([...]) (&&).

Start with List<int>, List<String>, List<UuidValue>, List<double>, List<bool> as bigint[], text[], uuid[], double precision[], boolean[] on PostgreSQL behind an explicit field keyword; SQLite keeps json.

Use Case

A job-run table that tracks the gaps in a sequence of event ids, and lists of id pairs, both designed as bigint[]. As JSON, 10,000 gaps are roughly 70 to 110 KB per row (estimated, not measured), so TOAST and WAL on every run.

Alternatives

What we do today: store as json/jsonb, lower the cap on the number of gaps, and select with an array parameter (id = ANY($1::bigint[]) through unsafeQuery; the postgres package encodes a List<int> parameter as an array). Arrays as query parameters work; only array columns are missing. The JSON column stays larger, and it cannot be indexed or queried with array operators.

Additional context

  • Checked against serverpod_cli 4.1.0 and serverpod_database 4.1.0; unchanged in 4.1.1.
  • Related: #4284 (open; query interface for json/jsonb fields), #5094 (open; native jsonb for opaque JSON).

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