Skip to content

feat: Add an option to use workers for typescript formatting - #1102

Merged
avallete merged 2 commits into
masterfrom
davidcamachocateura/api-1585-add-option-to-use-workers-to-format-types
Aug 11, 2026
Merged

avallete merged 2 commits into
masterfrom
davidcamachocateura/api-1585-add-option-to-use-workers-to-format-types

Conversation

@dcamachoc

@dcamachoc dcamachoc commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Main thread relief (opt-in through PG_META_FORMAT_IN_WORKER)

What is the current behavior?

Formatting is always executed within the main thread. While generating types for a table with significant tables or relationships, it can block the event loop for several seconds, leaving other requests and health checks unattended.

What is the new behavior?

When the environment variable PG_META_FORMAT_IN_WORKER is set to true, the typescript generator will use a worker to format the generated types, unblocking the main event loop.

It can be configured using PG_META_FORMAT_POOL_SIZE, PG_META_FORMAT_MAX_QUEUE, PG_META_FORMAT_TIMEOUT_MS and PG_META_FORMAT_IDLE_TIMEOUT_MS.

@dcamachoc
dcamachoc requested review from a team, avallete and soedirgo as code owners August 11, 2026 16:11
@dcamachoc
dcamachoc force-pushed the davidcamachocateura/api-1585-add-option-to-use-workers-to-format-types branch from 9d7af95 to 6ad077a Compare August 11, 2026 16:14
@dcamachoc
dcamachoc force-pushed the davidcamachocateura/api-1585-add-option-to-use-workers-to-format-types branch from 6ad077a to 5e64c16 Compare August 11, 2026 16:15
@dcamachoc dcamachoc changed the title feat: Add option to use worker for typescript formatting feat: Add an option to use worker for typescript formatting Aug 11, 2026
@dcamachoc dcamachoc changed the title feat: Add an option to use worker for typescript formatting feat: Add an option to use workers for typescript formatting Aug 11, 2026
@avallete
avallete merged commit fae1f60 into master Aug 11, 2026
12 of 13 checks passed
@avallete
avallete deleted the davidcamachocateura/api-1585-add-option-to-use-workers-to-format-types branch August 11, 2026 16:56
avallete added a commit that referenced this pull request Aug 31, 2026
* feat: consume @supabase/postgrest-typegen for type generation

Integrate the extracted @supabase/postgrest-typegen package as the single
source of truth for type generation, replacing the embedded templates.

- src/lib/generators.ts: rewrite getGeneratorMetadata as a ~30-line adapter
  over the package's introspect(). It wraps pgMeta.query into the package's
  structural Queryable (throws on {error}), preserves the
  Promise<PostgresMetaResult<GeneratorMetadata>> contract, surfaces the first
  query error, and still ends the pool. Re-exports GeneratorMetadata from the
  package.
- src/server/server.ts: getTypeOutput now calls getGeneratorMetadata +
  generateTypescript/Go/Python/Swift, threading GENERATE_TYPES_DEFAULT_SCHEMA,
  POSTGREST_VERSION, detect-1:1, and Swift access-control env values. Behavior
  freeze: the CLI path still only supports included schemas.
- src/server/routes/generators/*.ts: swap `apply` template imports for the
  package's generateX; query params, headers, and error shapes unchanged.
- Delete src/server/templates/*.ts and test/server/templates/go.test.ts;
  re-point test/types.test.ts's pgTypeToTsType import and constants.ts's
  AccessControl import to the package; drop the now-unused VALID_* constants.
- Keep PostgresMetaRelationships.ts and src/lib/sql/*.sql.ts (they back the
  REST endpoints) — accepted temporary duplication.

`npm run check` passes. Full-suite byte-parity validation is Phase 2.3
(PGMETA-114). The dependency is pinned to 1.0.0-alpha.1; local validation
installs it from Verdaccio via an uncommitted scoped .npmrc, and the lockfile
is finalized when the package is published to npm (Phase 3).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B53a2ayu9d16u821FQ5Qqz

* chore: point @supabase/postgrest-typegen at pkg-pr-new preview build

The package isn't published to npm yet; pin the dependency to the
pkg.pr.new preview build for pg-toolbelt PR #302 so CI can install it.
The lockfile must be regenerated (`npm install`) in an environment with
network access to pkg.pr.new — the remote sandbox's egress allowlist
blocks that host.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B53a2ayu9d16u821FQ5Qqz

* chore: update lockfile for postgrest-typegen pkg-pr-new build

* fix: sort generator metadata for deterministic type generation

The Go/Python/Swift generators emit objects in GeneratorMetadata order, so
output depended on the order introspection returned rows (environment-dependent
heap order). Apply the package's new sortGeneratorMetadata pass in the
getGeneratorMetadata adapter so all four generators receive canonically-ordered
metadata.

Regenerate the typegen go/python snapshots accordingly: only ordering changes
(the `a_view` view moves to its canonical oid position); struct/class contents
are byte-identical. TypeScript and Swift sort internally and are unaffected.

Requires @supabase/postgrest-typegen with sortGeneratorMetadata
(supabase/pg-toolbelt#302).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B53a2ayu9d16u821FQ5Qqz

* test: regenerate typegen snapshots for semantic metadata ordering

Follow-up to the sortGeneratorMetadata semantic-key change
(supabase/pg-toolbelt#302): the canonical order is now schema+name based, so
the Go/Python typegen snapshots are regenerated to alphabetical order. Pure
reorder — struct/class contents are byte-identical. TypeScript/Swift sort
internally and are unaffected.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B53a2ayu9d16u821FQ5Qqz

* chore: refresh lockfile for semantic-key sort build

* chore: refresh lockfile for semantic-key sort build

* chore: repin @supabase/postgrest-typegen to released ^0.1.0

* fix: honor PG_META_GENERATE_TYPES_DEFAULT_SCHEMA in the typescript generator route

* fix: restore worker-thread type generation on top of postgrest-typegen

The postgrest-typegen refactor dropped the worker path added in #1102: the
route calls generateTypescript() directly, the package formats with prettier
inline and exposes no format hook, so format-pool, format-worker, the piscina
dependency and the 503 load-shedding path were all left dead while CLAUDE.md
still documented the feature.

Rather than wait for a format hook upstream, hand the whole generateTypescript
call to the worker. Measured on a synthetic public schema (12 columns and 2
foreign keys per table), 400 tables: wall clock 1059ms on a worker vs 1065ms
inline, with the longest main-thread block dropping from ~1000ms to 21ms.
Metadata crosses the boundary as a structured clone, which is plain JSON and
costs nothing measurable. This also covers the ~10% of the cost that is string
building rather than prettier, which a format-only hook would have left on the
main thread.

format-pool/format-worker are renamed to typegen-pool/typegen-worker since they
no longer format, keeping the admission control, per-task timeout, idle pool and
503 shedding as they were. The PG_META_FORMAT_* env vars keep their names so
existing deployments do not need reconfiguring.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0192KEBCPggQgUT7JLPc3Cz1

* chore: drop unrelated CLAUDE.md reflow and keep format callbacks off the worker boundary

* chore: bump @supabase/postgrest-typegen to ^0.2.0

0.2.0 formats with oxfmt instead of prettier (typescript snapshots regenerated; the only output change is oxfmt parenthesizing conditional types in generic-default positions), runs its introspection queries concurrently, and accepts a format hook, which stays unused here since the whole generateTypescript call already runs on the worker.

* chore: restore master's format-pool naming to minimize the diff

The typegen-pool/typegen-worker rename made the route's unchanged 503
load-shedding block show up as a diff against master because the error class
inside it changed name. Keep master's file and identifier names (format-pool,
format-worker, FormatQueueFullError, destroyFormatPool, isFormatPoolActive) so
the only diff left in these files is the delegation itself: the worker task
carries generator metadata into @supabase/postgrest-typegen's generateTypescript
instead of a prettier payload, and the exported entry point is
generateTypescriptTypes instead of format(code, options). No behavior change.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0192KEBCPggQgUT7JLPc3Cz1

---------

Co-authored-by: Claude <[email protected]>
Co-authored-by: Lukas Klingsbo <[email protected]>
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.

2 participants