r/SQLv2 • u/Alternative_Pin9598 • 20d ago
SQLv2 Lesson 4: agent memory in SQL. When a customer moves, similarity search still says "ship to Boston". REMEMBER / CURRENT / RECALL fix that (beginner)
Lesson 4 of the beginner series: one idea per post, every query run on SynapCores 2.3.2 this morning with the output pasted. (This sub is run by the SynapCores team. Plain SQL questions are welcome too.)
The problem
Your support bot keeps notes about each customer. In September, Sam said "ship to Boston". Last week Sam moved to Denver. Lessons 1-3 taught search by meaning, so let's ask the notes the bot's question:
CREATE TABLE sam_notes (id INTEGER PRIMARY KEY, noted_on TEXT, note TEXT, embedding VECTOR(384));
INSERT INTO sam_notes (id, noted_on, note) VALUES
(1, '2026-09-02', 'Sam lives in Boston and wants orders shipped there.'),
(2, '2026-09-02', 'Sam is allergic to wool, so never suggest wool sweaters.'),
(3, '2026-10-28', 'Sam moved to Denver last week; ship everything to Denver from now on.');
UPDATE sam_notes SET embedding = EMBED(note);
SELECT id, noted_on, note, ROUND(COSINE_SIMILARITY(embedding, EMBED('where do we send the package')), 2) AS score
FROM sam_notes ORDER BY score DESC;Output:
id | noted_on | note | score
1 | 2026-09-02 | Sam lives in Boston and wants orders shipped there. | 0.44
3 | 2026-10-28 | Sam moved to Denver last week; ship everything to Denver... | 0.36
2 | 2026-09-02 | Sam is allergic to wool, so never suggest wool sweaters. | 0.02The old note wins. Similarity measures how close a note is in meaning, not whether it is still true. The Boston note talks about shipping more directly, so it scores higher, and the package goes to the wrong city.
The fix: a MEMORY that knows facts can change
A MEMORY is a built-in store for "what we know about this customer". You give each fact an attribute, and a newer value for the same attribute replaces the old one.
CREATE MEMORY shop_memory IDENTITY customer_id WITH (llm_extraction = false);IDENTITY customer_id means every fact belongs to one customer. llm_extraction = false means we name the attribute ourselves, so no AI model has to guess it.
Step 1: REMEMBER the same three notes
REMEMBER shop_memory FOR customer_id = 42 'Sam lives in Boston and wants orders shipped there.'
WITH (source_type = 'verified_user_statement', attribute = 'ship_city', value = 'Boston');
REMEMBER shop_memory FOR customer_id = 42 'Sam is allergic to wool, so never suggest wool sweaters.'
WITH (source_type = 'verified_user_statement', attribute = 'allergy', value = 'wool');
REMEMBER shop_memory FOR customer_id = 42 'Sam moved to Denver last week; ship everything to Denver from now on.'
WITH (source_type = 'verified_user_statement', attribute = 'ship_city', value = 'Denver');The third one comes back with this in its result (trimmed to the interesting part):
"changes": {"state_changes": 1, "records_superseded": 1, "conflicts_flagged": 0}"records_superseded: 1" means the Boston fact is now history, not current.
Step 2: CURRENT, the one answer you can act on
CURRENT shop_memory FOR customer_id = 42 ATTRIBUTE ship_city;Output (trimmed):
"attribute": "ship_city", "value": "Denver", "confidence": 0.85,
"authority": "verified_user_statement", "valid_to": null,
"supersedes_memory_id": "mem_1m6khj7r8_rrc6od"One value, plus where it came from and what it replaced. No ranking, no threshold to tune.
Step 3: RECALL, when you have a question instead of an attribute name
RECALL shop_memory FOR customer_id = 42 ABOUT 'where do we send the package';The result has several parts. The two you'll use most (trimmed):
"current_state": {
"allergy": {"value": "wool", "confidence": 0.85},
"ship_city": {"value": "Denver", "confidence": 0.85}
},
"temporal": [
{"attribute": "ship_city", "previous_value": "Boston", "current_value": "Denver"}
]current_state is what's true now. temporal is what changed, so your bot can even say "you moved from Boston, right? Shipping to Denver." The original sentences are still in there too (under episodic), ranked by meaning like in Lessons 1-3, for when you need the exact words.
Rule of thumb
- Similarity search (Lessons 1-3): "find the text that's about this".
- MEMORY: "what's true about this customer right now, and what changed".
Use both: search your docs by meaning, and keep facts about people in a memory.
Homework
Sam writes: "I'm spending the winter in Miami, ship there until March."
- Write the
REMEMBERstatement. - What does
CURRENT ... ATTRIBUTE ship_cityreturn now? - What does the
temporalpart of RECALL show?
Answer in the comments and I'll reply to each one with the real output.
Free Community Edition, if you want to run this yourself: https://synapcores.com/download?utm_source=reddit&utm_medium=owned&utm_campaign=sqlv2-lesson-4
Before this lesson, how did you handle "the customer changed their mind": overwrite the row, keep every version, or hope for the best?
1
u/Alternative_Pin9598 20d ago
Want to run this on the real engine? r/synapcores is the product home: install steps, recipes, and today's Build log #1 (what shipped in 2.4.4): https://www.reddit.com/r/synapcores/comments/1xnqm00/
See MEMORY in an ops setting: on r/devops7, the same three statements turn into an on-call handoff tool: https://www.reddit.com/r/devops7/comments/1xnql7n/
Missed Lesson 3 (pick a similarity threshold from data)? https://www.reddit.com/r/SQLv2/comments/1xkb3mn/