Sub topic 15
Pitfalls: connection pools, side channels, and foreign keys
Assume you know PostgreSQL roles, transactions, GRANT, and basic RLS policies. RLS limits which rows a role can read or change. It does not automatically hide every fact about the database or repair unsafe application behavior.
Connection pools and session state
A connection pool reuses database sessions. A setting left on a session can therefore affect the next request that receives the same connection.
For example, a tenant policy might read current_setting('app.tenant_id'). If application code sets that value for one request and does not clear it, the next request can run with the wrong tenant context.
Prefer a transaction-scoped setting. The third argument to set_config is true, which makes the value local to the current transaction:
CREATE TABLE invoices (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
tenant_id uuid NOT NULL,
amount_cents integer NOT NULL
);
ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
CREATE POLICY invoice_tenant_policy ON invoices
USING (
tenant_id =
NULLIF(current_setting('app.tenant_id', true), '')::uuid
)
WITH CHECK (
tenant_id =
NULLIF(current_setting('app.tenant_id', true), '')::uuid
);
BEGIN;
SELECT set_config(
'app.tenant_id',
'3f2b8c1e-5a4d-4e0b-9f6a-1c2d3e4f5a6b',
true
);
SELECT id, amount_cents FROM invoices;
COMMIT;The application should set the context and perform the work in the same transaction. It should also avoid accepting a tenant identifier from an untrusted client without checking that the authenticated user belongs to that tenant.
Side channels
A side channel is an indirect way to learn information. RLS may hide a row from SELECT, while another operation still reveals that something exists.
Common examples include:
- A unique constraint or foreign-key error that differs depending on a hidden row.
- Different response times for values that match many rows.
- Counts, aggregates, or search results that allow repeated guesses.
- Application error messages that expose database constraint names or values.
- Functions, views, or
SECURITY DEFINERcode that runs with stronger privileges.
Treat errors and response timing as potentially observable. Use generic application errors, limit sensitive operations, and avoid giving untrusted users arbitrary predicates or high-resolution measurements. RLS is a database boundary, not a complete protection against statistical inference.
Foreign keys and RLS
Foreign-key checks must preserve referential integrity. PostgreSQL performs these checks even when RLS would hide the referenced row from an ordinary query. Consequently, an insert can fail because a referenced value exists or does not exist, creating an observable signal.
Keep tenant identity in the relationship itself. A single global identifier can accidentally allow a row from one tenant to reference another tenant's row. A composite key makes the tenant part of the constraint:
CREATE TABLE tenant_users (
tenant_id uuid NOT NULL,
user_id bigint NOT NULL,
PRIMARY KEY (tenant_id, user_id)
);
CREATE TABLE invoices (
tenant_id uuid NOT NULL,
owner_id bigint NOT NULL,
amount_cents integer NOT NULL,
FOREIGN KEY (tenant_id, owner_id)
REFERENCES tenant_users (tenant_id, user_id)
);The policy must still check tenant_id. The composite foreign key enforces consistent references; it does not replace authorization.
| Pitfall | Observable result | Main defense |
|---|---|---|
| Stale session setting | Request uses another tenant's context | Use transaction-local settings |
| Constraint error | Hidden row existence may be inferred | Normalize errors and design tenant-aware keys |
| Cross-tenant reference | A valid ID points to another tenant | Use composite foreign keys |
Exercise
Why is set_config('app.tenant_id', value, true) safer than setting the value for the whole session in a connection pool?
Show answer
The true argument limits the setting to the current transaction, so it is automatically discarded before the connection serves another request.