Back to roadmaps pinecone Course

Data Isolation: Using Pinecone Namespaces

In production SaaS systems, different users should not access each other data. While you could create a separate index for every user, that is expensive. The recommended solution is to partition a single index using Namespaces.


1. What is a Namespace?

A namespace is a logical partition inside a Pinecone index. All operations (Upsert, Query, Delete, Update) can be scoped to a specific namespace:

  • Queries executed inside a namespace will only search and return vectors within that partition.
  • Vectors in different namespaces are isolated, even if they share the same ID.

2. Upserting into a Namespace

To target a namespace, call the .namespace() method on your index instance:

import { pc } from "../lib/pinecone";

async function saveUserData(userId: string, vectors: any[]) {
  const index = pc.index("knowledge-base");
  
  // Scope operation to a user-specific namespace
  const userNamespace = index.namespace(userId);

  await userNamespace.upsert(vectors);
  console.log(`Saved vectors to namespace: ${userId}`);
}

3. Querying within a Namespace

To query a specific namespace, use the .namespace() method:

async function searchUserData(userId: string, queryVector: number[]) {
  const index = pc.index("knowledge-base");
  
  const userNamespace = index.namespace(userId);

  const response = await userNamespace.query({
    vector: queryVector,
    topK: 5,
    includeMetadata: true,
  });

  // Returns matches only from the specified namespace partition
  return response.matches;
}
Published on Last updated: