Postgress Memory not retrieving Messages chronologically

My PG Memory provides the conversation context mixed up.

Error is simple:

Messages come in randomly. Not paired, not chronoligical.

Had anyone encountered this and solved it?

Any advice is highly appreciated.


Information on your n8n setup

  • Debug info

    core

    • n8nVersion: 2.0.2
    • platform: docker (cloud)
    • nodeJsVersion: 22.21.0
    • nodeEnv: production
    • database: sqlite
    • executionMode: regular
    • concurrency: 20
    • license: community
    • consumerId: 00000000-0000-0000-0000-000000000000

    storage

    • success: all
    • error: all
    • progress: false
    • manual: true
    • binaryMode: filesystem

    pruning

    • enabled: true
    • maxAge: 720 hours
    • maxCount: 25000 executions

    client

    • userAgent: mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/143.0.0.0 safari/537.36
    • isTouchDevice: false

    Generated at: 2026-01-14T09:43:26.082Z

Your database isn’t correctly structured:

1 Like

This isn’t a bug in PG Memory it’s a context scoping issue.

Your PG Memory is mixing conversations because all executions are writing to / reading from the same memory scope. Without a unique identifier, PG Memory treats every run as the same conversation.

Why it happens

  • No unique session_id / conversation_id / user_id

  • PG Memory retrieves embeddings globally instead of per conversation

  • Result: unrelated past messages leak into the current context

How to fix it

  1. Generate or pass a unique conversation key (e.g. WhatsApp number, user ID, thread ID)

  2. Store it with every memory write

  3. Filter memory reads by that same key

Example approach:

  • Use a Set node before PG Memory:

    conversation_id = {{$json.from || $execution.id}}
    
    
  • Configure PG Memory to store + query by that ID (metadata / namespace)

Once memory is scoped per conversation, the context will stop getting mixed up.

This is expected behavior if memory isn’t partitioned PG Memory is doing exactly what it’s told to do.

[email protected]

Hello!

Thank you for the fast reply.

In my database the session_id is the phone number I use.

And then the postgress node only retrieves based on session_id.

And it pulls them wrong.

Im not completely sure I understand what to do next.