Learn SQL
-- tables.schemas_searchpath_multitenant

Schemas, search_path & Multitenancy in SQL

Namespaces that isolate tenants without extra clusters

--concept

A database holds schemas (namespaces); schemas hold tables/views/functions — sales.orders vs hr.employees. search_path resolves unqualified names (public by default in PG). Multi-tenant patterns: separate schemas per tenant (strong isolation, migration fan-out) vs shared tables + tenant_id + RLS (cheap, policy-dependent). MySQL conflates DATABASE/SCHEMA; PG nests them.

--how it works
1

Namespace tables

sales.orders vs hr.employees — same names coexist per schema.

2

Resolve via search_path

Per-request path selects tenant namespace for bare names.

3

Tenant pattern pick

Schemas isolate (migration fan-out); shared+RLS economizes (policy risk).

4

Grant per namespace

Default-deny new schemas; default privileges auto-cover future tables.

--visualization
sales.orders resolves (tenant namespace)
objectscope
sales.orderssales scope
Stage 1 / 1
Schemas isolate; shared+RLS economizes. SET LOCAL avoids pooled-checkout leaks.
--example
query.sql
1CREATE SCHEMA sales;
2CREATE TABLE sales.orders (id INT PRIMARY KEY, total NUMERIC);
Result
objectscope
sales.ordersnamespaced table
hr.employeesseparate namespace

Identical table names coexist per schema. Per-tenant schemas isolate dumps and grants; shared-schema + RLS isolates rows without migration fan-out.

--variants

Two namespaces

Identical names, separate scopes.

query.sql
1CREATE SCHEMA sales;
2CREATE TABLE sales.orders (id INT PRIMARY KEY);
Result
objectscope
sales.orderssales scope
hr.employeeshr scope
1Bare orders resolves via search_path.
2Qualified sales.orders always exact.
3Dumps/grants scope per namespace.

Tenant isolation pick

Fan-out vs policy cost.

query.sql
1-- schemas-per-tenant: strong iso, N migrations
2-- shared + RLS: 1 migration, policy-enforced
Result
patterntrade
schemasisolated, fan-out
shared+RLScheap, policy-tested
1Dozens of tenants: schemas manageable.
2Thousands: shared + RLS wins with role tests.
3MySQL flattens DB/SCHEMA — PG nests.
--key facts
DB > schema > tablePG nesting (MySQL flattens)
search_pathunqualified-name resolution order
Tenant choiceschemas isolate; RLS economizes
--use cases
SaaS namespace + per-tenant dumps/grants
Monorepo schemas: sales/hr/staging separation
--under the hood
search_path is per-session GUC — SET LOCAL inside txns avoids leaking across pooled checkouts.
Schema-scoped stats keep planner estimates local to tenant distributions.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.