Skip to content

fix(postgres): inferType returns ::text[] for empty/all-null arrays#6

Open
dnplkndll wants to merge 1 commit into
developfrom
fix/postgres-infertype-null-array
Open

fix(postgres): inferType returns ::text[] for empty/all-null arrays#6
dnplkndll wants to merge 1 commit into
developfrom
fix/postgres-infertype-null-array

Conversation

@dnplkndll
Copy link
Copy Markdown

@dnplkndll dnplkndll commented May 17, 2026

What changed

foundations/server/packages/postgres/src/utils.ts — the array branch of inferType no longer falls through to ::jsonb when it can't sample an inner type. It now:

  • skips null / undefined when sampling, and
  • falls back to ::text[] (a real SQL array type) when no inner type can be inferred.

foundations/server/packages/postgres/src/__tests__/utils.spec.ts — three pre-existing tests carried // BUG: comments documenting the broken behavior; flipped them to assert the fix, and added a [null] case (the exact array shape that triggered the production crash).

Why

The Huly github integration's sync loop crash-loops on CockroachDB whenever a workspace has the GitHub App installed but no repositories linked. The repeating error:

ERROR: unsupported comparison operator: _id = ANY $2::JSONB:
op ANY <right> requires array, tuple or subquery on right side

Trace:

  • services/github/pod-github/src/worker.ts (performSync) submits repository: { $in: [null, ..._repositories] }. The leading null is intentional (matches docs with no per-repo association). When _repositories is empty, the value is [null].
  • inferType([null]) returned ::jsonb: the array branch couldn't sample an inner type and fell through to the catch-all object branch.
  • The $in SQL builder then emitted _id = ANY ($N::JSONB). CockroachDB rejects that; stock Postgres silently tolerates the bad cast, which is why this stayed latent on Postgres-backed deployments.

Test plan

  • rushx test in foundations/server/packages/postgres passes (83/83), including the three flipped tests and the new inferType([null]) case
  • Build the pod-github image off this branch and roll it in a CockroachDB-backed deployment that has the GitHub App installed without any repositories linked
  • Confirm zero failed to perform sync log entries for that workspace
  • Confirm Syncing / Wait for changes cadence resumes
  • No regression on workspaces that already had repositories linked

Out of scope

  • storage.ts $in builder and the worker.ts query shape — both are correct once inferType returns a proper array type
  • decodeArray('{}') → [''] (another BUG: comment in the same test file) — separate concern

Previously `inferType([])`, `inferType([null])`, and `inferType([null, null])`
fell through the array branch (which couldn't determine an inner type) into
the generic object branch and returned `::jsonb` / `::jsonb[]`. Both shapes
are incompatible with `= ANY ($N::jsonb)` on CockroachDB:

    ERROR: unsupported comparison operator: _id = ANY $2::JSONB:
    op ANY <right> requires array, tuple or subquery on right side

The bug is reachable from the GitHub integration's sync loop. When a
workspace has the App installed but no repositories linked,
`pod-github`'s performSync submits a $in query of shape `[null]` for
the `repository` field; the postgres adapter then emits SQL the
database refuses, and the github pod crash-loops indefinitely on that
workspace. (Stock Postgres is more lenient than CockroachDB and may
silently accept the ::jsonb cast, masking the bug there.)

Fix the array branch in inferType to skip null/undefined when sampling
the inner type, and fall back to `::text[]` (a real SQL array type)
when no inner type can be inferred. `$in`-on-Ref callers already
expect that shape, and ANY accepts it on both Postgres and
CockroachDB.

Existing tests in utils.spec.ts had explicit `// BUG:` comments
acknowledging these three cases — flipped to assert the fixed
behavior, and added a `[null]` case (the exact array shape that
crashed the github pod).

Signed-off-by: Don Kendall <dkendall@ledoweb.com>
@dnplkndll dnplkndll force-pushed the fix/postgres-infertype-null-array branch from 0e565d8 to 856c2ae Compare May 17, 2026 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant