LangChain and LangGraph · Chapter 10 of 25
PGVector: a vector store in Postgres
By the end of this chapter you can keep RoomRaja's documents and their embeddings in Postgres with LangChain's PGVector, so they survive a restart; give each Document an id and use it to update and delete; search only one property's documents with a metadata filter; and say when an in-memory store is enough and when you need a database. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: RAG with LangChain
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.
Why you need this
The RAG chain of chapter 9 starts by building its store: it reads the policy, embeds all 22 rules, and keeps them in a Python dictionary. When the program ends, the dictionary is gone. Every restart embeds everything again.
For 22 rules that is a fraction of a second. For RoomRaja next year (six properties' house notes, every version of the policy, the maintenance manual, thousands of past answers) it is minutes and money on every deploy, and each copy of the assistant keeps its own store in its own memory. Faisal also brings real news: policy version 5 is coming, with a new late fee. With an in-memory store, the only way to change one rule is to rebuild everything.
RoomRaja already runs Postgres. With the pgvector extension, Postgres stores vectors and finds the nearest ones, next to the rest of the data. LangChain's PGVector is a vector store that lives there.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
pg_store.py: a store that survives a restart
Make a PGVector store with lab-embed (check_embedding_ctx_length=False), the collection name policy, and the connection string postgresql+psycopg:// (it finds the lab's database from the PG environment variables).
The program does one of two jobs, by its first command-line argument. With load: make one Document per rule of /workspace/data/policy.md (the whole line as page_content; rule and section in the metadata, as in chapter 8), add them all with the ids policy-1.1, policy-1.2 and so on (policy- and the rule's number), and print Stored N rules. With anything else: that argument is a question; print the page_content of the closest rule (k=1), and load nothing.
The Check runs load twice (the store must still hold each rule once), then asks a question in a separate run.
- Task 2
update_policy.py: version 4 to version 5
Use the same store as task 1 (collection policy). First load version 4: the rules of /workspace/data/policy.md as in task 1, with the ids policy-1.1 and so on, and metadata rule, section and version (the number 4).
Then apply Faisal's version 5, by id: rule 1.2 becomes exactly 1.2 From the 6th, a late fee of ₹150 per day is added, up to ₹2,000 in a month. (metadata rule 1.2, section Rent, version 5), and rule 3.3 is removed.
Finally print the page_content of the closest rule to What is the late fee? (k=1).
- Task 3
per_property.py: one property's documents
Make a PGVector store (lab-embed, the connection string of task 1) with the collection name house. Add, with ids:
one house note per property of /workspace/data/properties.json, in the file's order: the text Name is in Area. Its property manager is Manager. (for example RoomRaja Baner is in Baner. Its property manager is Aditya Kale.), metadata property_id (the property's id, like BAN), id house- and the property's id;
then one Document per rule line of /workspace/data/policy.md (the whole line), metadata property_id ALL, id house- and the rule's number.
A tenant at Kharadi (KHR) asks Who is the manager here? Search with k=1 twice and print two lines: All: and the closest Document's text with no filter, then KHR: and the closest Document's text among those whose property_id is KHR or ALL.
- Task 4 · interview question
Interview: restart.py, in memory or Postgres
Make the policy's rule Documents (as in task 1) with the ids restart-1.1, restart-1.2 and so on, and two stores with lab-embed: an InMemoryVectorStore, and a PGVector with the collection name restart (the connection string of task 1).
For each store, In memory first, then Postgres: count how many of the ids it already holds (get_by_ids), print In memory: N rules at start (or Postgres: N rules at start), and add the Documents with their ids only when it does not hold them all.
The Check runs the program twice. The interviewer will ask which store RoomRaja's assistant needs, and when an in-memory store is the right choice.
What you will learn
- PGVector: a LangChain vector store in Postgres, and the tables it makes.
- Persistence: load once, search from any later run.
- Ids: updating and deleting one Document, and the trap with collections.
- Filters on metadata: a dictionary instead of a function.
- In memory or Postgres, and when.
More in RAG with LangChain
Build "PGVector: a vector store in Postgres" yourself
RoomRaja, a Pune co-living company, answers tenants on WhatsApp by hand. You build its assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of LangChain and LangGraph are free. This one comes with the all labs plan.