TECHNOLOGY

Pooled PostgreSQL retrieval: tenant context must end with the transaction

In this article
THE QUICK TAKE

Bind trusted tenant context and protected queries to one explicit transaction, then verify reuse and failure paths under the actual runtime role.

Reuse a connection without reusing a tenant

A shared retrieval service borrows database connections from a pool. Its first request searches one customer’s documents; a later request uses the same physical connection for another customer. A tenant predicate can be correct while its input still comes from the previous session. The engineering question is where authenticated tenant context begins and ends relative to the database transaction. Evaluate this boundary before treating a successful semantic search as evidence of data isolation.

Establish the database role that actually enforces policy

The PostgreSQL row-security documentation distinguishes row policies from ordinary table privileges. It also explains that superusers and roles with BYPASSRLS bypass row security, while table owners normally bypass it unless forced under policy. Inspect the actual application’s database role. A test through an administrative connection cannot validate the restrictions of an ordinary retrieval request. Conversely, a runtime connection using a bypass role can invalidate the intended design before tenant context is considered.

Bind context to the transaction boundary

PostgreSQL’s SET reference distinguishes session settings from SET LOCAL, whose effects end with the current transaction. Outside a transaction block, SET LOCAL emits a warning and has no effect. If a deployment uses a custom setting as a policy input, establish that value from authenticated server context inside the same explicit transaction as every protected query. This is an application pattern, not a built-in guarantee that any tenant-named setting is trustworthy. Do not derive it from a model-generated argument or an unchecked client header.

Account for the value restored afterward

Transaction-local lifetime does not mean that the surrounding session has no value. When the transaction ends, the session-level setting becomes effective again. Avoid leaving a privileged default or an old tenant at that level and assuming local assignment erased it. Define behavior when request context is absent: the application should reject the operation or the policy should yield no authorized rows under the chosen contract. An unscoped fallback query is especially dangerous when error recovery runs outside the original transaction.

Test connection reuse and error paths deliberately

Use harmless fixtures with distinct document identifiers and similar text across tenants. Keep a test pool small enough to observe physical connection reuse. Exercise successful completion, rollback, cancellation and a request that omits tenant context. Then inspect returned identifiers and the effective role and context within each protected transaction. Include both retrieval and any direct document-fetch route. The evidence sought is absence of cross-tenant results after reuse and failure, not merely a relevant answer from the intended customer. No database experiment or performance result is claimed here.

Keep policy coverage separate from result interpretation

A legitimate empty set can follow from row visibility, as explained in the PostgREST visibility guide. Preserve actual database errors separately rather than replacing every failure with an empty search result. Review all policies applicable to the runtime role: PostgreSQL combines permissive policies with OR and restrictive policies with AND. A broad additional permissive rule may change the result even when the tenant-specific expression looks correct in isolation. Record the deployed policy revision with the access evidence.

Define what this boundary proves

The controlled connection-reuse check establishes behavior for the tested role, transaction paths and policy configuration. It does not validate an untested export process, a security-definer helper or a separately privileged maintenance task. Milvus tenant routing makes the related distinction between placement and authorization. For a pooled PostgreSQL service, the decisive artifact is the trusted scope carried through each protected transaction, together with evidence that a later borrower cannot inherit an earlier caller’s authority.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles