Skip to content

Set tenant settings for row-level security with BEGIN - #10

Merged
emoss08 merged 1 commit into
masterfrom
tenant-settings
Nov 25, 2026
Merged

emoss08 merged 1 commit into
masterfrom
tenant-settings

Conversation

@emoss08

@emoss08 emoss08 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

What changes

Config.TenantSettings names run-time parameters keel sets, transaction-local, to the tenant's values at the start of every transaction a tenant scope opens:

cfg.TenantSettings = []string{"app.organization_id", "app.business_unit_id"}
CREATE POLICY tenant ON shipments TO app_tenant
    USING (organization_id = current_setting('app.organization_id', true)
       AND business_unit_id = current_setting('app.business_unit_id', true));
  • Sent with BEGIN in the same round trip, for explicit and implicit transactions.
  • Tenant-scoped terminals outside a transaction run in an implicit one; system scopes and unscoped work set nothing and stay single-statement.
  • Open refuses a list whose length differs from TenantColumns.

Why

Backing keel's scoping with row-level security is the main reason applications write OnBegin, and doing it there takes a callback, a []any of arguments and, with the result cache, a Key function. The settings depend only on the scope, which keel already has and the cache already keys on, so keel can set them itself. OnBegin remains for settings that depend on more than the tenant.

Not breaking: a config without TenantSettings behaves as before.

Checks

gofmt, go vet (root and bench), keel gen -check, go test -race ./... against Postgres 18. New tests cover settings in implicit, Write and Read transactions in one round trip, a second tenant, a system scope, and the length check.

Generated with Claude Code

Config.TenantSettings names run-time parameters keel sets, transaction-local,
to the tenant's values at the start of every transaction a tenant scope opens,
in the same round trip as BEGIN. It is what applications wrote OnBegin for to
back keel's scoping with Postgres row-level security, without a callback, an
[]any of arguments or a result-cache Key: the settings follow the scope.
System scopes and unscoped work set nothing, and Open refuses a list that does
not match the tenant columns.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@emoss08
emoss08 merged commit 5b92447 into master Nov 25, 2026
1 of 2 checks passed
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