Skip to content

fix: PostgresGraphDB reorganizer/handler compatibility - #2273

Open
Kwizii wants to merge 4 commits into
MemTensor:mainfrom
Kwizii:fix/postgres-reorganizer-compat
Open

fix: PostgresGraphDB reorganizer/handler compatibility#2273
Kwizii wants to merge 4 commits into
MemTensor:mainfrom
Kwizii:fix/postgres-reorganizer-compat

Conversation

@Kwizii

@Kwizii Kwizii commented Aug 22, 2026

Copy link
Copy Markdown

Description

PostgresGraphDB is missing several graph-store methods that Neo4jGraphDB already implements. When MOS_ENABLE_REORGANIZE=true, the reorganizer and handler call these methods during structure optimization and redundant-memory merge (MERGED_TO edge inheritance), causing background thread crashes or silent merge failures.

This change adds:

  • node_not_exist / get_memory_count for structure optimization pre-checks
  • get_edges and extended edge_exists (direction / type=ANY / user_name) for MERGED_TO edge inheritance
  • user_name kwarg support in get_structure_optimization_candidates
  • search_by_fulltext stub (matches Neo4j stub behavior) to avoid AttributeError on keyword recall path

Also includes prior embedding normalization fix (list[float] coercion for pgvector string values) required by reorganizer GraphDBNode validation.

Related Issue (Required): Fixes #2272

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test

Added regression tests in tests/graph_dbs/test_postgres_reorganizer_compat.py:

  • node_not_exist / get_memory_count
  • get_edges and extended edge_exists
  • get_structure_optimization_candidates(..., user_name=...)
  • search_by_fulltext stub

Existing tests in tests/graph_dbs/test_postgres_embedding_parse.py cover embedding normalization.

Reproduce with:

pytest tests/graph_dbs/test_postgres_reorganizer_compat.py tests/graph_dbs/test_postgres_embedding_parse.py -v

Checklist

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常
  • I have created related documentation issue/PR in MemOS-Docs (if applicable) | 我已在 MemOS-Docs 中创建了相关的文档 issue/PR(如果适用)
  • I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用)
  • I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人

Reviewer Checklist

@Memtensor-AI Memtensor-AI added area:database graph_db + vector_db | 图数据库与向量数据库 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Aug 22, 2026
@Memtensor-AI
Memtensor-AI requested a review from wustzdy August 22, 2026 15:33
@Memtensor-AI

Memtensor-AI commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2273
Task: e8ae6dfd1a4d4c78
Base: main
Head: fix/postgres-reorganizer-compat

🔍 OpenCodeReview found 3 issue(s) in this PR.


1. tests/graph_dbs/test_postgres_embedding_parse.py (L45-L50)

The import psycopg2 and import psycopg2.pool statements live inside the __init__ body of PostgresGraphDB. Patching require_python_package only removes the decorator wrapper — it does not suppress these local imports. If psycopg2 is not installed in the test environment (e.g., a lightweight CI runner), every test that calls _build_db() will raise ImportError and fail.

Consider adding a module-level skip guard:

psycopg2 = pytest.importorskip("psycopg2")

or use a try/except ImportError fixture, so the entire test module is gracefully skipped when the optional dependency is absent.

💡 Suggested Change

Before:

def _build_db() -> PostgresGraphDB:
    with (
        patch("memos.graph_dbs.postgres.require_python_package", lambda *args, **kwargs: lambda fn: fn),
        patch("psycopg2.pool.ThreadedConnectionPool", MagicMock()),
        patch.object(PostgresGraphDB, "_init_schema", lambda self: None),
    ):

After:

psycopg2 = pytest.importorskip("psycopg2")  # skip entire module if psycopg2 is absent


def _build_db() -> PostgresGraphDB:
    with (
        patch("memos.graph_dbs.postgres.require_python_package", lambda *args, **kwargs: lambda fn: fn),
        patch("psycopg2.pool.ThreadedConnectionPool", MagicMock()),
        patch.object(PostgresGraphDB, "_init_schema", lambda self: None),
    ):

2. src/memos/graph_dbs/postgres.py (L1148)

The parameter name filter shadows Python's builtin filter function. Since search_filter is already present as a separate parameter, this filter parameter is redundant and its naming is problematic. Rename it to avoid shadowing the builtin — e.g., use extra_filter or simply remove it if it duplicates search_filter.

💡 Suggested Change

Before:

    filter: dict | None = None,

After:

    extra_filter: dict | None = None,

3. src/memos/graph_dbs/postgres.py (L1153-L1154)

This stub silently returns an empty list without any warning, which makes it very hard to diagnose why keyword recall produces no results in production. Add at least a logger.warning to alert callers that the method is not implemented, or raise NotImplementedError if it is not expected to be called yet.

💡 Suggested Change

Before:

        """Stub for TreeTextMemory keyword recall; Postgres fulltext search is not implemented yet."""
        return []

After:

        """Stub for TreeTextMemory keyword recall; Postgres fulltext search is not implemented yet."""
        logger.warning(
            "[search_by_fulltext] Full-text search is not implemented for PostgresGraphDB; "
            "returning empty results."
        )
        return []

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

⚠️ Automated Test Results: ENV ISSUE

The test environment encountered an issue that requires manual attention.

Details: Executor error: Command failed: git clone --depth 1 --branch fix/postgres-reorganizer-compat git@github.com:Kwizii/MemOS.git /data/test-workspaces/470076c0e05b72ea/repo
Cloning into '/data/test-workspaces/470076c0e05b72ea/repo'...
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Branch: fix/postgres-reorganizer-compat

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

⚠️ Automated Test Results: ENV ISSUE

The test environment encountered an issue that requires manual attention.

Details: Executor error: Command failed: git clone --depth 1 --branch fix/postgres-reorganizer-compat git@github.com:Kwizii/MemOS.git /data/test-workspaces/e8ae6dfd1a4d4c78/repo
Cloning into '/data/test-workspaces/e8ae6dfd1a4d4c78/repo'...
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Branch: fix/postgres-reorganizer-compat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:database graph_db + vector_db | 图数据库与向量数据库 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: Postgres reorganizer crashes due to missing graph-store methods

3 participants