pgvector becomes especially useful when semantic ranking must obey relational rules. PostgreSQL can join chunks to documents, tenants, products, and permissions; filter state and dates; and atomically update source truth with derived retrieval records. Filters narrow candidates, but only trusted server policy and database privileges make those predicates an authorization control.
EXISTS avoids duplicating a chunk when a user has multiple matching groups. The application constructs $1 and $4 from authenticated server context. A client may request a language, but may never choose an unauthorized tenant or principal set.
Generating an embedding is an external and potentially slow operation; do not hold a database transaction open during the network call. Claim a job in a short transaction, embed outside it, then commit the vector only if the source checksum and expected job version still match.
Optimistic version checks prevent stale workers from overwriting newer content.
Create two tenants, two principals, and overlapping document groups. For every retrieval query, run a matrix of authorized and unauthorized identities. Assert exact IDs, not only row counts. Kill a worker between claim and completion; prove the lease or retry policy recovers without a duplicate embedding.
Threat-model this filtered vector query and embedding transaction [paste]. Mark every input as server-derived, user-selectable, or database-owned. Find duplicate-row risks, stale-write races, missing constraints, and authorization bypass paths. Return adversarial fixtures and transaction timelines.
Verification contract: Integration tests must attempt cross-tenant access through every API path, cache, reranker, and citation resolver—not only the primary SQL query.