6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
57 lines
2.7 KiB
TypeScript
57 lines
2.7 KiB
TypeScript
import { db } from '@sim/db'
|
|
import { sql } from 'drizzle-orm'
|
|
|
|
export type DbExecutor = typeof db | DbTransaction
|
|
export type DbTransaction = Parameters<Parameters<typeof db.transaction>[0]>[0]
|
|
|
|
/**
|
|
* Statement/lock timeout for user-table READ queries. Reads are bounded tighter
|
|
* than the write path because filter shape is caller-controlled: a public API
|
|
* key can drive an arbitrary predicate (a catastrophic-backtracking `match`
|
|
* regex, a wide unindexed scan), and an untimed read pins a connection from the
|
|
* small shared `web` pool — one abusive key can starve every tenant. `SET LOCAL`
|
|
* scopes both to the surrounding transaction, so they die when it commits.
|
|
*/
|
|
const READ_STATEMENT_TIMEOUT_MS = 15_000
|
|
const READ_LOCK_TIMEOUT_MS = 3_000
|
|
|
|
async function setReadTimeouts(trx: DbTransaction): Promise<void> {
|
|
await trx.execute(sql.raw(`SET LOCAL statement_timeout = '${READ_STATEMENT_TIMEOUT_MS}ms'`))
|
|
await trx.execute(sql.raw(`SET LOCAL lock_timeout = '${READ_LOCK_TIMEOUT_MS}ms'`))
|
|
}
|
|
|
|
/**
|
|
* Runs a user-table read inside a transaction that always caps `statement_timeout`
|
|
* / `lock_timeout` (see {@link setReadTimeouts}). Pass `seqscanOff` for queries
|
|
* with no tenant-bounded index plan — custom column sorts and filtered counts —
|
|
* where the planner otherwise seq-scans the whole shared `user_table_rows`
|
|
* relation (every tenant's rows); see {@link withSeqscanOff} for the measured
|
|
* deltas. Default-order keyset/offset pages already stream the
|
|
* `(table_id, order_key, id)` index, so they take the timeout without the flag.
|
|
*/
|
|
export async function withReadGuards<T>(
|
|
fn: (trx: DbTransaction) => Promise<T>,
|
|
opts?: { seqscanOff?: boolean }
|
|
): Promise<T> {
|
|
return db.transaction(async (trx) => {
|
|
await setReadTimeouts(trx)
|
|
if (opts?.seqscanOff) await trx.execute(sql`SET LOCAL enable_seqscan = off`)
|
|
return fn(trx)
|
|
})
|
|
}
|
|
|
|
/**
|
|
* Runs `fn` with seq scans penalized (`SET LOCAL`, so the flag dies with the
|
|
* transaction) AND the read timeouts above. JSONB predicates and sort keys
|
|
* (`->>` extraction, `@>` containment, lateral `jsonb_each_text`) are opaque to
|
|
* the planner — it estimates a handful of matching rows and picks a parallel
|
|
* seq scan over the entire shared `user_table_rows` relation (every tenant's
|
|
* rows) instead of the tenant's own index. Measured on a 1M-row table inside a
|
|
* 12M-row relation: filtered count 12.7s → 1.0s, sorted page 9.7s → 0.76s,
|
|
* filtered bulk select 14.4s → tenant-bounded. The flag only penalizes the plan
|
|
* shape: if no index plan exists, the seq scan still runs (and the timeout caps it).
|
|
*/
|
|
export async function withSeqscanOff<T>(fn: (trx: DbTransaction) => Promise<T>): Promise<T> {
|
|
return withReadGuards(fn, { seqscanOff: true })
|
|
}
|