Skip to content

PostgresChatMessageHistory ignores schema_name and reads and writes the table in public #444

Closed

Description

@RyanLin5967

Currently, AsyncPostgresChatMessageHistory.create checks the table in the given schema_name but then returns cls(cls.__create_key, engine._pool, session_id, table_name) without it, so the instance keeps the default "public". Every add_message, messages and clear call then goes to "public"."<table_name>": with a same-named table in public, a history created for another schema writes its messages there and reads that table's messages; without one, the calls fail. PostgresChatMessageHistory.create / create_sync go through the same create. The AlloyDB package passes schema_name in the same line.

Environment details

  • OS type and version: macOS 26.7.1, arm64 (Darwin 25.6.0)
  • Python version: 3.12.13
  • pip version: uv 0.12.3
  • langchain-google-cloud-sql-pg version: 0.15.0 (same create on main), langchain-core 1.7.3, PostgreSQL 17.11

Steps to reproduce

  1. CREATE SCHEMA tenant_a;
  2. Run the example.

Code example

from langchain_core.messages import HumanMessage
from langchain_google_cloud_sql_pg import PostgresEngine, PostgresChatMessageHistory

engine = PostgresEngine.from_connection_string("postgresql+asyncpg://postgres:postgres@localhost:5432/postgres")
engine.init_chat_history_table(table_name="messages")                         # public.messages
engine.init_chat_history_table(table_name="messages", schema_name="tenant_a")  # tenant_a.messages

shared = PostgresChatMessageHistory.create_sync(engine, session_id="s1", table_name="messages")
tenant = PostgresChatMessageHistory.create_sync(engine, session_id="s1", table_name="messages", schema_name="tenant_a")
tenant.add_message(HumanMessage("tenant secret"))

print("tenant_a:", [m.content for m in tenant.messages])
print("public:  ", [m.content for m in shared.messages])

Output (and SELECT COUNT(*) FROM tenant_a.messages returns 0):

tenant_a: ['tenant secret']
public:   ['tenant secret']

Expected:

tenant_a: ['tenant secret']
public:   []

Stack trace

None (wrong table, no error)

Activity

  1. added
    api: cloudsql-postgres
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: cloudsql-postgres

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions