mirror of
https://github.com/langchain-ai/langchain-postgres.git
synced 2026-07-16 01:33:18 -04:00
Separate tables for collections and embeddings in PGVectorStore
#97
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ezequiel-tcmrio on GitHub (Nov 4, 2025).
Currently,
PGVectorStorecreates a single table to store embeddings and their metadata.In some scenarios, it would be useful to have a more explicit relational structure, where:
collectionstable defines the collections (name, description, id, global metadata);embeddingstable stores the vectors and references the collection via a foreign key (collection_id).This would make it easier to manage and organize multiple embedding collections in the same PostgreSQL database, especially in multi-user or multi-application contexts.
Question
Is there currently any native way in
langchain_postgresto configurePGVectorStoreto:collections) andembeddings) to a collection viacollection_id?If not, is there any recommended workaround to achieve this separation?
Example desired schema
Context
I’m building a document ingestion system where each client has its own collection.
I want to avoid creating multiple embeddings tables — instead, I’d like to keep a single embeddings table and a separate collections table to simplify filtering and access control.
Environment
langchain_postgres version: 0.0.16
PostgreSQL version: 15
pgvector extension: installed ✅
Python: 3.11
Optional suggestion
A possible implementation could allow something like:
This way, LangChain would create (or use) the tables as needed and maintain the relationship via collection_id.
If this feature exists, I would appreciate your assistance in implementing it.
@rmlekus commented on GitHub (Nov 14, 2025):
@ezequiel-tcmrio This is how PGVector implemented the mapping of embeddings and collections to a Schema into the tables
PGVector got deprecated as of version 0.0.14+, though, see the warning in the Vector Store section of the README.MD
I would likt to add further reasons to continue re-enable the optional support for the older Table Layout, though:
When creating collections dynamically at run-time (typically small ( 101 to 103) number for embeddings per collection but potentially > 10**4 collections (e.g. documents associated with single authenticated agent sessions), the 1 Table per collection design is running into 2 impediments:
While the old PGVector DDL schema needed to be extended with further database indices, such as in some of my use-cases
in order to support vectorstore queries with JSON filters to be applied, with such indices supporting the query execution planner to start with filtering on the collection name, followed by filtering on the JSON meta-data (which could also be supported with additional JSONB indices, e.g. as shown in https://neon.com/postgresql/postgresql-indexes/postgresql-json-index#4-creating-an-index-on-a-specific-field-of-a-jsonb-column.
Furthermore a 2 table schema also eases up any statistic queries or linking embedding db indices with LLM/Embedding Model Observability Platforms.
@averikitsch commented on GitHub (Nov 14, 2025):
PGVector is deprecated but you are still able to utilize the class and the functionality. We highly recommend not using multiple table format (using collections ids) due to the increased latency of the filtering required.