pg_multitenant
pg_multitenant · Multi-tenant shared-schema helpers using PostgreSQL row-level security
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100830 | pg_multitenant | pg_multitenant | 1.0 | SEC | MIT | Rust |
PostgreSQL · 16
Usage
Sources:
pg_multitenant is an experimental helper for shared-database, shared-schema multitenancy. It creates PostgreSQL row-level security policies that compare a designated tenant column with the current role name, and records marked columns in pgmt.table_tenant_column.
Core Workflow
The usable upstream example employs the user strategy: each tenant connects or assumes a role whose name equals the tenant key converted to text.
To remove the policy and metadata entry:
pgmt.mark_tenant_column(schema_name, table_name, tenant_column) enables RLS, creates a policy named tenant_isolation_policy, and records the table OID and column. pgmt.unmark_tenant_column(schema_name, table_name) disables RLS, drops that policy, and deletes the metadata row.
Security and Implementation Caveats
This reviewed revision should not be treated as a complete tenant-isolation framework. The source registers both GUC settings under the name pgmt.tenant_strategy; consequently, the advertised value strategy and pgmt.tenant_value path are not safely usable as written. The helper also interpolates supplied identifiers into dynamic SQL without identifier quoting, uses a fixed policy name, and does not make repeated marking idempotent.
PostgreSQL table owners, superusers, and roles with BYPASSRLS can bypass ordinary RLS unless the surrounding design accounts for them. The generated policy compares tenant_column::TEXT with current_user, so role naming is part of the security contract. Use only trusted identifiers, test owner and privileged-role behavior, inspect the generated policy in pg_policy, and perform an independent security review before production use. The control version is 1.0, while the frozen Cargo manifest reports 0.1.0 and targets PostgreSQL 16.