Skip to content

feat: align PostgreSQL graph storage with current v2.0.36 APIs - #2531

Merged
CarltonXiang merged 3 commits into
MemTensor:dev-v2.0.36from
Zyntrael:feat/pgsql-dev-v2.0.36
Oct 21, 2026
Merged

CarltonXiang merged 3 commits into
MemTensor:dev-v2.0.36from
Zyntrael:feat/pgsql-dev-v2.0.36

Conversation

@Zyntrael

Copy link
Copy Markdown
Contributor

Store the existing graph interface on MD5 tenant shards so this branch can run on PostgreSQL without attribute-tree tables.

Description

Please include a summary of the change, the problem it solves, the implementation approach, and relevant context. List any dependencies required for this change.

Related Issue (Required): Fixes #issue_number

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

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

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Made sure Checks passed
  • Tests have been provided

Store the existing graph interface on MD5 tenant shards so this branch can run on PostgreSQL without attribute-tree tables.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Memtensor-AI Memtensor-AI added area:api 云服务 / FastAPI / OpenAPI / MCP area:core MOS 编排层 / 框架底座 / 跨模块问题 area:database graph_db + vector_db | 图数据库与向量数据库 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Oct 21, 2026
@Memtensor-AI

Memtensor-AI commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2531
Task: 962b4a390e040224
Base: dev-v2.0.36
Head: feat/pgsql-dev-v2.0.36

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


1. src/memos/configs/graph_db.py (L262-L263)

The fulltext_config validator here uses .replace(".", "_").isidentifier(), which accepts strings like foo..bar, foo., or .bar (e.g. "foo..bar".replace(".", "_") → "foo__bar", which is a valid identifier). This is more permissive than the stricter regex ^[A-Za-z_][A-Za-z0-9_.]*$ used in postgres.py/_init_schema. Since fulltext_config is interpolated directly into SQL DDL (the to_tsvector('{fulltext_config}', ...) generated column expression), any value accepted by this weaker validator but rejected by the regex — or bypassed at runtime via getattr(self.config, "fulltext_config", "simple") — creates a SQL injection surface in the generated column DDL. Align the config-level validation with the stricter regex used in _init_schema to catch the issue at config creation time.

💡 Suggested Change

Before:

        if not self.fulltext_config.replace(".", "_").isidentifier():
            raise ValueError("`fulltext_config` must be a valid PostgreSQL configuration name")

After:

        if not re.match(r"^[A-Za-z_][A-Za-z0-9_.]*$", self.fulltext_config):
            raise ValueError("`fulltext_config` must be a valid PostgreSQL configuration name")

2. src/memos/configs/graph_db.py (L248-L251)

The default fulltext_config value is "jiebacfg". However, in postgres.py/_init_schema the fallback default for the same setting (via getattr(self.config, "fulltext_config", "simple")) is "simple". If a deployment accesses _init_schema without a fresh config (or during migration), the schema's search_vector generated column will be created with "simple" instead of "jiebacfg". This discrepancy can cause silent mismatches in tsvector content between search_by_fulltext (which uses self.config.fulltext_config, defaulting to "jiebacfg") and the index. The getattr fallback in _init_schema should use self.config.fulltext_config directly to eliminate the divergence.

💡 Suggested Change

Before:

    fulltext_config: str = Field(
        default="jiebacfg",
        description="PostgreSQL text search configuration used to build search_vector",
    )

After:

    fulltext_config: str = Field(
        default="simple",
        description="PostgreSQL text search configuration used to build search_vector",
    )

3. src/memos/api/config.py (L925)

The hard-coded default was changed from 384 to 1024, which now matches PostgresGraphDBConfig.embedding_dimension. However, any existing deployment that relied on the old default (384 or 768) and has not set EMBEDDING_DIMENSION in the environment will silently receive a mismatched dimension the next time the schema-init path runs, causing a PostgreSQL error (e.g. 'column cannot change type' or a cast failure when inserting halfvec(1024) into a column declared as halfvec(384)). The new _init_schema raises RuntimeError for the old flat-table layout but does NOT handle the shard-already-exists-with-wrong-dimension case.

Suggestion: Add a comment here noting that changing this default is a breaking schema change for existing deployments, and that operators must drop and recreate (or migrate) the vector columns. Consider also adding a runtime check in _init_schema that validates the existing column's dimension matches embedding_dimension before proceeding.


4. src/memos/configs/graph_db.py (L225)

The embedding_dimension default changed from 768 to 1024 with no migration guard in the config layer. Any existing deployment that previously relied on the default (768) will now silently try to write 1024-dimensional vectors into a 768-dim column, causing a runtime error from pgvector. At minimum, a comment or validator warning should be present for users upgrading from the previous default.


5. src/memos/configs/graph_db.py (L245-L247)

The hnsw_m, hnsw_ef_construction, and ivfflat_lists parameters are unconditionally stored regardless of which vector_index_type is active, but there is no cross-validation that the chosen type's parameters are sensible (e.g., hnsw_m and hnsw_ef_construction are still validated even when vector_index_type='ivfflat', which is harmless, but a misconfigured HNSW with vector_index_type='ivfflat' gives no warning). Consider adding a cross-check in validate_config to warn or raise when HNSW-specific values are non-default while vector_index_type='ivfflat', so operators aren't silently confused.


6. src/memos/graph_dbs/postgres.py (L285)

psycopg2 does not support multiple semicolon-separated SQL statements in a single execute() call; this will raise ProgrammingError: can't execute multiple commands in a single statement for any shard_count > 1. All shard DDL is joined into one giant string and executed in one shot, which fails at runtime. Each statement must be executed individually.

💡 Suggested Change

Before:

cur.execute(";\n".join(statements))

After:

                    for stmt in statements:
                        cur.execute(stmt)

7. src/memos/graph_dbs/postgres.py (L457-L462)

The WHERE predicate in an ON CONFLICT DO UPDATE clause can only reference EXCLUDED and the table being inserted into via its unqualified name. The quoted alias "Memory" is not valid there — PostgreSQL will raise a ProgrammingError at runtime on every upsert that hits a conflict. The condition is also redundant since the conflict target (user_name, id) already guarantees user_name equality.

💡 Suggested Change

Before:

                    ON CONFLICT (user_name, id) DO UPDATE SET
                        memory = EXCLUDED.memory,
                        properties = EXCLUDED.properties,
                        embedding = EXCLUDED.embedding,
                        updated_at = EXCLUDED.updated_at
                    WHERE "Memory".user_name = EXCLUDED.user_name

After:

                    ON CONFLICT (user_name, id) DO UPDATE SET
                        memory = EXCLUDED.memory,
                        properties = EXCLUDED.properties,
                        embedding = EXCLUDED.embedding,
                        updated_at = EXCLUDED.updated_at

8. src/memos/graph_dbs/postgres.py (L387-L389)

When a node is inserted without an embedding vector, random values in [-0.2, 0.2] are silently substituted. This corrupts all subsequent cosine similarity searches for the affected nodes — the fake vector will return arbitrary neighbours — with no warning logged and no way for the caller to detect the substitution. The node should either be rejected (raise ValueError) or stored with a NULL embedding and excluded from vector searches.

💡 Suggested Change

Before:

        embedding = fields.pop("embedding", None)
        if not isinstance(embedding, list) or not embedding:
            embedding = [round(random.uniform(-0.2, 0.2), 6) for _ in range(self._vector_dim())]

After:

        embedding = fields.pop("embedding", None)
        if not isinstance(embedding, list) or not embedding:
            logger.warning(
                "add_nodes_batch: node %s has no embedding; storing NULL — excluded from vector search",
                node_id,
            )
            embedding = None  # stored as NULL; filtered by 'embedding IS NOT NULL' at search time

9. src/memos/graph_dbs/postgres.py (L155-L158)

fulltext_config passes the regex r"^[A-Za-z_][A-Za-z0-9_.]*$" (dots are allowed, enabling schema-qualified names like pg_catalog.simple) and is then interpolated directly into the DDL f-string as to_tsvector('{fulltext_config}', …). Even though a schema-qualified name is not itself an injection vector for arbitrary SQL, the dot-allowing regex is inconsistent with the stricter r"^[A-Za-z_][A-Za-z0-9_]*$" check used in search_by_fulltext / search_by_keywords_tfidf. Use a single consistent pattern and interpolate via parameterized DDL where possible, or at minimum align the regexes.

💡 Suggested Change

Before:

        if not re.match(r"^[A-Za-z_][A-Za-z0-9_.]*$", fulltext_config):
            raise ValueError(f"Invalid PostgreSQL text search config: {fulltext_config}")
        vector_index_type = getattr(self.config, "vector_index_type", "hnsw")
        shard_count = int(getattr(self.config, "shard_count", 400))

After:

        if not re.match(r"^[A-Za-z_][A-Za-z0-9_]*$", fulltext_config):
            raise ValueError(f"Invalid PostgreSQL text search config: {fulltext_config}")

10. src/memos/graph_dbs/postgres.py (L770-L772)

_build_filter_where_clause calls self.parse_filter(filter_dict) unconditionally at the top, and then for and/or branches it recursively calls _build_filter_where_clause on each sub-condition, which in turn calls parse_filter again. Any field in a nested sub-filter will have its key prefixed with info. twice (e.g. status → info.status on the first pass, then info.info.status on the recursive pass), producing invalid SQL expressions.

💡 Suggested Change

Before:

    def _build_filter_where_clause(self, filter_dict: dict[str, Any], params: list[Any]) -> str:
        """Build SQL WHERE fragment from filter dict."""
        filter_dict = self.parse_filter(filter_dict)

After:

    def _build_filter_where_clause(self, filter_dict: dict[str, Any], params: list[Any]) -> str:
        """Build SQL WHERE fragment from filter dict."""
        # NOTE: parse_filter must be applied once by callers before entering here;
        # recursive calls must NOT re-normalize.
        if not filter_dict:
            return ""

11. src/memos/graph_dbs/postgres.py (L1568-L1574)

Using id(conn) (a CPython memory address) as a per-connection marker is unsound in a connection pool: when a connection object is garbage-collected and a new one is allocated at the same address, the set falsely believes the HNSW session settings were already applied and skips the set_config call. Additionally, the _hnsw_ready_ids set grows without bound because closed/replaced connections are never evicted from it.

💡 Suggested Change

Before:

        ready = getattr(self, "_hnsw_ready_ids", None)
        if ready is None:
            ready = set()
            self._hnsw_ready_ids = ready
        marker = id(conn)
        if marker in ready:
            return

After:

        # Use a per-connection attribute to avoid stale id() collisions
        if getattr(conn, "_hnsw_settings_applied", False):
            return
        with conn.cursor() as cur:
            cur.execute(
                """
                SELECT set_config('hnsw.ef_search', '100', false),
                       set_config('hnsw.iterative_scan', 'relaxed_order', false)
                """
            )
        conn._hnsw_settings_applied = True

12. src/memos/graph_dbs/postgres.py (L970-L975)

The method iterates over distinct physical shards but always passes the full user_names and memory_ids arrays to every shard's unnest. When two tenants hash to the same shard remainder, only one iteration covers that shard, but pairs belonging to the other tenant in that shard are still present in the arrays and will be joined against that shard's rows — this is correct by coincidence but relies on the JOIN filter. However, pairs whose tenant hashes to a different shard will be passed to the wrong shard's unnest CTE and simply produce no matches (not a deletion error), so the correctness impact is a silent no-op rather than incorrect deletion. The real issue is the unnecessary work (scanning the wrong shard with all pairs). More critically, if two different tenants share a shard (same MD5 remainder), the requested CTE for that shard correctly contains pairs for both, which is the intended behaviour — so this is a performance concern rather than a correctness bug. The concern is still worth noting for review.

💡 Suggested Change

Before:

                for memories, edges in zip(memory_tables, edge_tables, strict=True):
                    cur.execute(
                        f"""
                        WITH requested(user_name, memory_id) AS (
                            SELECT * FROM unnest(%s::text[], %s::text[])
                        ), to_delete AS (

After:

                # Filter pairs to only those whose tenant routes to this specific shard
                shard_remainder = self._partition_remainder(tenant_names[shard_idx])
                shard_pairs = [(u, m) for u, m in zip(user_names, memory_ids)
                               if self._partition_remainder(u) == shard_remainder]
                shard_user_names = [p[0] for p in shard_pairs]
                shard_memory_ids = [p[1] for p in shard_pairs]
                if not shard_pairs:
                    continue

13. src/memos/graph_dbs/postgres.py (L1719-L1726)

The score formula ((1 - scope_distance) + 1) / 2.0 maps cosine distance [0, 2] to the range [1.0, 0.0], but after the earlier WHERE scope_distance < 0.9 filter the effective score range is [~0.55, 1.0]. Any caller that passes threshold as a raw cosine-similarity value (e.g. 0.7) will compare it against this rescaled score (≈0.65 equivalent at distance=0.7), producing unexpected filtering. The threshold semantics should be documented clearly or the comparison should be against the raw distance.

💡 Suggested Change

Before:

    def search_by_embedding(
        self,
        vector: list[float],
        user_name: str,
        top_k: int = 5,
        scope: str | None = None,
        status: str | None = None,
        threshold: float | None = None,

After:

# Document the rescaled score in the docstring and/or normalise threshold at the entry point:
# rescaled_score = ((1 - distance) + 1) / 2.0, range [0.55, 1.0] after the <0.9 distance filter
# Callers must pass threshold in the same [0.55, 1.0] space.

14. src/memos/graph_dbs/postgres.py (L1399-L1408)

get_neighbors_by_tag fetches the entire active memory set for a tenant into Python, then computes tag overlap in-process and sorts. For a tenant with a large number of memories this will load all rows into application memory on every call. The tag overlap ranking should be pushed into SQL using a jsonb contains check or unnest cross join to avoid the full in-memory scan.

💡 Suggested Change

Before:

    def get_neighbors_by_tag(
        self,
        tags: list[str],
        exclude_ids: list[str],
        top_k: int = 5,
        min_overlap: int = 1,
        include_embedding: bool = False,
        user_name: str | None = None,
    ) -> list[dict[str, Any]]:
        """Find active tenant memories ranked by tag overlap."""

After:

# Push tag overlap into SQL to avoid loading all rows:
# WITH tags(t) AS (SELECT unnest(%s::text[]))
# SELECT id, memory, ..., COUNT(*) OVER () as overlap
# FROM memories
# CROSS JOIN LATERAL jsonb_array_elements_text(properties->'tags') AS tag(v)
# JOIN tags ON tags.t = tag.v
# WHERE ...
# GROUP BY id, memory, ... HAVING COUNT(*) >= %s
# ORDER BY COUNT(*) DESC LIMIT %s

15. src/memos/graph_dbs/postgres.py (L2610-L2613)

When object_type == "user", target_user is set to None and user_name may also be None. In that path, n.get("metadata", {}).get("user_name") or user_name will produce None for any node whose metadata lacks user_name. The guard all(node_user_names) evaluates all(...) over a list that may contain None values — None is falsy so all() returns False and the edge query is skipped. While this avoids the crash, it means edges are silently omitted from the export whenever any node lacks a user_name in metadata, which is a data-loss bug.

💡 Suggested Change

Before:

                node_user_names = [
                    n.get("metadata", {}).get("user_name") or user_name for n in nodes
                ]
                if node_ids and all(node_user_names):

After:

                node_user_names = [
                    n.get("metadata", {}).get("user_name") for n in nodes
                ]
                effective_user_names = [name or user_name for name in node_user_names]
                if node_ids and all(effective_user_names):

16. src/memos/graph_dbs/postgres.py (L671-L673)

_is_safe_field_name rejects any schema name that contains a digit after the first character only if the digit starts the name — but the real issue is that the method is also used in _qualified_partition to validate schema names of the form <schema>_graph_<N>. Names like memos_graph_12 contain digits and are accepted (digits are in [A-Za-z0-9_]*), so that path is fine. However, _physical_table falls back to f"{self.schema}.{table}" when user_name is None, bypassing _qualified_partition and the double-quoting entirely. The raw table value ("memories" / "edges") is inserted directly into the f-string. While those two literals are safe today, the pattern is a latent SQL-injection surface if the set of fall-through table names ever expands. More concretely, the returned string "{self.schema}.memories" refers to a schema that no longer has a memories table under the new shard layout, so callers that reach this path (e.g. remove_oldest_memory when user_name is None and self.user_name is also None) will get a PostgreSQL undefined table error at runtime.

💡 Suggested Change

Before:

    def _is_safe_field_name(field: str) -> bool:
        """Validate field names used in dynamic SQL fragments."""
        return bool(re.match(r"^[A-Za-z_][A-Za-z0-9_]*$", field))

After:

    def _physical_table(self, table: str, user_name: str | None, conn=None) -> str:
        """Return the one partition table PolarDB would address for this tenant."""
        if not user_name:
            raise ValueError(f"_physical_table requires a user_name; got None for table '{table}'")
        return self._qualified_partition(table, self._partition_remainder(user_name, conn))

17. src/memos/graph_dbs/postgres.py (L2378-L2384)

get_grouped_counts accepts a raw where_clause: str parameter and appends it directly to the SQL query without any parameterisation or sanitisation:

if where_clause:
    conditions.append(where_clause)

This is a SQL-injection vector — any caller that builds where_clause from external input can inject arbitrary SQL. The method is also inconsistent with the rest of the file, which uses parameterised queries throughout. The params dict is used as positional parameters via params.values(), which is order-dependent and fragile.

💡 Suggested Change

Before:

    def get_grouped_counts(
        self,
        group_fields: list[str],
        where_clause: str = "",
        params: dict[str, Any] | None = None,
        user_name: str | None = None,
    ) -> list[dict[str, Any]]:

After:

    # Replace the raw where_clause parameter with a structured filter dict
    def get_grouped_counts(
        self,
        group_fields: list[str],
        filter: dict[str, Any] | None = None,
        user_name: str | None = None,
    ) -> list[dict[str, Any]]:

18. src/memos/graph_dbs/postgres.py (L1824-L1839)

The query_config value is interpolated directly into the SQL f-string as to_tsquery('{query_config}', %s). Although it is validated by a regex that only allows [A-Za-z_][A-Za-z0-9_]*, it is never quoted with double-quotes in the SQL, making it an identifier injection if the regex is ever relaxed. The same pattern appears in search_by_keywords_tfidf. Consider using psycopg2.sql.Identifier to compose the config name safely rather than relying solely on the regex guard.

💡 Suggested Change

Before:

    def search_by_fulltext(
        self,
        query_words: list[str],
        top_k: int = 10,
        scope: str | None = None,
        status: str | None = None,
        threshold: float | None = None,
        search_filter: dict | None = None,
        user_name: str | None = None,
        filter: dict | None = None,
        knowledgebase_ids: list[str] | None = None,
        tsvector_field: str = "properties_tsvector_zh",
        tsquery_config: str = "jiebacfg",
        return_fields: list[str] | None = None,
        **kwargs,
    ) -> list[dict]:

After:

                from psycopg2 import sql
                cur.execute(
                    sql.SQL(
                        """
                        WITH q AS (
                            SELECT to_tsquery({config}, %s) AS query
                        )
                        ...
                        """
                    ).format(config=sql.Identifier(query_config)),
                    (*params, top_k),
                )

19. src/memos/graph_dbs/postgres.py (L1667-L1676)

_build_search_result reads row[4] as created_at, row[5] as updated_at, and row[6] as user_name. The SQL SELECT in search_by_embedding (non-lightweight path) produces columns in the order: id, score, memory, properties, created_at, updated_at, user_name (indices 0–6). The mapping is correct there.

However, _build_search_result is also called from search_by_fulltext, whose SELECT is: id, score, memory, properties, created_at, updated_at, user_name — same order, so it works. But the row-index assumptions are fragile: a comment or explicit named access (e.g. via RealDictCursor) would make this much safer against future column-order changes. No code defect right now, but worth flagging as a maintainability risk.

💡 Suggested Change

Before:

    def _build_search_result(
        self, row: tuple[Any, ...], return_fields: list[str] | None
    ) -> dict[str, Any]:
        result: dict[str, Any] = {"id": row[0], "score": float(row[1])}
        fields = self._validate_return_fields(return_fields)
        if not fields:
            return result

        memory = row[2] or ""
        created_at, updated_at, user_name = row[4], row[5], row[6]

After:

# Consider using psycopg2.extras.RealDictCursor to access columns by name
# instead of positional indexing, which breaks silently if column order changes.

20. src/memos/graph_dbs/postgres.py (L852-L858)

When writable_cube_ids is None, _physical_tables returns all shard tables (up to 4096). Each shard runs the same where_clause query in a separate cur.execute() call inside a loop, meaning up to 4096 round trips to the database. The default shard_count=400 already makes this very expensive for unconstrained deletes. At minimum, this should be documented; ideally the path without writable_cube_ids should require an explicit override or raise an error to prevent accidental full-cluster scans.

💡 Suggested Change

Before:

    def delete_node_by_prams(
        self,
        writable_cube_ids: list[str] | None = None,
        memory_ids: list[str] | None = None,
        file_ids: list[str] | None = None,
        filter: dict | None = None,
    ) -> int:

After:

        if not writable_cube_ids:
            raise ValueError(
                "delete_node_by_prams requires at least one writable_cube_id to scope the delete; "
                "pass writable_cube_ids explicitly or use a different deletion method."
            )

21. src/memos/graph_dbs/postgres.py (L2494-L2505)

When object_type == "user" (export all users except mem_cube_id), target_user is set to None and _physical_table("memories", None) is called. As noted separately, this falls back to the unsharded {schema}.memories path, which does not exist under the new layout and will raise a PostgreSQL undefined table error. Unlike a single-tenant export, there is no correct single shard for a "all tenants except one" query; the method should use _all_partitions_relation (or raise) in this case.

💡 Suggested Change

Before:

    def export_graph(
        self,
        user_name: str | None = None,
        include_embedding: bool = False,
        user_id: str | None = None,
        page: int | None = None,
        page_size: int | None = None,
        filter: dict | None = None,
        memory_type: list[str] | None = None,
        status: list[str] | None = None,
        **kwargs,
    ) -> dict[str, Any]:

After:

        if has_object_filter and object_type == "user":
            target_user = None
        # Use all-partition union for the cross-tenant "user" object type
        if target_user is None:
            memories = self._all_partitions_relation("memories")
        else:
            memories = self._physical_table("memories", target_user)

22. src/memos/graph_dbs/postgres.py (L2221-L2230)

_all_partitions_relation generates a UNION ALL of every shard (400 by default), producing a very large subquery string. With shard_count=400, this expands to 400 SELECT * FROM … UNION ALL … branches every time this method is called. For a hot path that looks up ownership of memory IDs, this will generate enormous query strings and prohibit any index use. The method is always called with a concrete list of IDs; consider routing via MD5-hashed shard lookup on the ID itself if IDs carry shard affinity, or accept a user_names hint to restrict to relevant shards.

💡 Suggested Change

Before:

    def get_user_names_by_memory_ids(self, memory_ids: list[str]) -> dict[str, str | None]:
        normalized_ids = list(
            dict.fromkeys(
                str(memory_id).strip() for memory_id in memory_ids if str(memory_id).strip()
            )
        )
        if not normalized_ids:
            return {}
        result: dict[str, str | None] = dict.fromkeys(normalized_ids)
        memories = self._all_partitions_relation("memories")

After:

# If memory IDs are UUIDs without embedded shard keys, consider
# caching the id→shard mapping or accepting a user_names hint:
    def get_user_names_by_memory_ids(
        self, memory_ids: list[str], user_names: list[str] | None = None
    ) -> dict[str, str | None]:
        ...
        if user_names:
            memories = self._tenant_relation("memories", user_names)
        else:
            memories = self._all_partitions_relation("memories")

23. src/memos/graph_dbs/postgres.py (L1310-L1316)

The return type annotation dict[str, Any] is incompatible with the abstract base class BaseGraphDB.get_subgraph, which declares the return type as list[str]. This violates the Liskov Substitution Principle and will cause type-checking failures for any caller typed against BaseGraphDB.

💡 Suggested Change

Before:

    def get_subgraph(
        self,
        center_id: str,
        depth: int = 2,
        center_status: str = "activated",
        user_name: str | None = None,
    ) -> dict[str, Any]:

After:

# Either update the base class signature:
    @abstractmethod
    def get_subgraph(self, center_id: str, depth: int = 2) -> dict[str, Any]: ...

# Or keep the override compatible:
    def get_subgraph(self, center_id: str, depth: int = 2, **kwargs) -> list[str] | dict[str, Any]:

24. src/memos/graph_dbs/postgres.py (L411)

The signature of add_node drops the user_name: str | None = None default and makes user_name a required positional argument. The abstract base class (BaseGraphDB.add_node) declares the signature as (self, id, memory, metadata) with no user_name parameter. This makes the override incompatible with the base-class contract: any call via a BaseGraphDB reference that omits user_name will raise TypeError. The same breakage applies to add_nodes_batch, whose base-class signature is add_nodes_batch(self, nodes, user_name: str | None = None).

💡 Suggested Change

Before:

    def add_node(self, id: str, memory: str, metadata: dict[str, Any], user_name: str) -> None:

After:

    def add_node(self, id: str, memory: str, metadata: dict[str, Any], user_name: str | None = None) -> None:
        """Add a memory node."""
        if not user_name:
            raise ValueError("add_node requires user_name")

25. src/memos/graph_dbs/postgres.py (L1898-L1911)

search_by_keywords_tfidf does not apply a LIMIT clause on its query. For a tenant with many memories matching the OR-tsquery, every matching row will be loaded into application memory. This is especially risky since OR queries tend to match much more broadly than AND queries. Consider adding a configurable top_k parameter with a default cap.

💡 Suggested Change

Before:

    def search_by_keywords_tfidf(
        self,
        query_words: list[str],
        scope: str | None = None,
        status: str | None = None,
        search_filter: dict | None = None,
        user_name: str | None = None,
        filter: dict | None = None,
        knowledgebase_ids: list[str] | None = None,
        tsvector_field: str = "properties_tsvector_zh",
        tsquery_config: str = "jiebaqry",
        return_fields: list[str] | None = None,
        **kwargs,
    ) -> list[dict]:

After:

    def search_by_keywords_tfidf(
        self,
        query_words: list[str],
        top_k: int = 100,  # add a configurable cap
        ...
    ) -> list[dict]:
        ...
        # In the SQL:
        """
        ...
        AND search_vector @@ q.query
        ORDER BY ts_rank(search_vector, q.query) DESC
        LIMIT %s
        """
        # params: (*params, top_k)

26. src/memos/graph_dbs/postgres.py (L1989-L1990)

search_by_keywords_like searches properties->>'memory' (the JSON field) but the memory column also exists as a top-level table column. If the memory JSON property is ever absent or differs from the column value, results will be inconsistent. The method should search the memory column directly, consistent with how other search methods operate: memory LIKE %s.

💡 Suggested Change

Before:

conditions.append("COALESCE(properties->>'memory', '') LIKE %s")
params.append(pattern)

After:

        conditions.append("memory LIKE %s")
        params.append(pattern)

Generated by cloud-assistant via Open Code Review.

Zyntrael and others added 2 commits October 21, 2026 19:59
The upstream check stops on ruff format before the tests run.

Co-authored-by: Cursor <cursoragent@cursor.com>
The Windows Python 3.11 dependency install failed once and needs another run.

Co-authored-by: Cursor <cursoragent@cursor.com>
@CarltonXiang
CarltonXiang merged commit c8aed14 into MemTensor:dev-v2.0.36 Oct 21, 2026
17 of 18 checks passed
@endxxxx endxxxx mentioned this pull request Nov 9, 2026
4 tasks done
syzsunshine219 added a commit that referenced this pull request Nov 9, 2026
## Description

Merge `dev-v2.0.36` into `main` for MemOS v2.0.36. The final
release-branch commit sets both the package version and
`memos.__version__` to `2.0.36`.

This release includes the PostgreSQL graph storage updates (#2531),
knowledge memory and recall changes (#2479, #2436), and fixes for
PolarDB recall (#2551), RabbitMQ shutdown (#2532), API key validation
(#2491), and rate limiting (#2490).

## How Has This Been Tested?

- [x] Unit tests — 24 passed in
`tests/graph_dbs/test_postgres_400_compat.py` and
`tests/mem_scheduler/test_rabbitmq_shutdown.py`.
- [x] Verified that both version fields are `2.0.36`, ran `make format`
and `git diff --check`, and simulated the merge with `main`.

## Checklist

- [x] Reviewed the release diff and the two-file version commit.
- [x] Verified the release branch merges cleanly with the current `main`
locally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:api 云服务 / FastAPI / OpenAPI / MCP area:core MOS 编排层 / 框架底座 / 跨模块问题 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.

3 participants