Skip to content

fix: apply POSTGRES_SCHEMA search_path to the asyncpg pool - #300

Open
nangelovv wants to merge 1 commit into
danny-avila:mainfrom
nangelovv:fix/postgres-schema-asyncpg-pool
Open

fix: apply POSTGRES_SCHEMA search_path to the asyncpg pool#300
nangelovv wants to merge 1 commit into
danny-avila:mainfrom
nangelovv:fix/postgres-schema-asyncpg-pool

Conversation

@nangelovv

Copy link
Copy Markdown

What

Pin the asyncpg pool (PSQLDatabase.get_pool) to the same search_path the SQLAlchemy engine uses when POSTGRES_SCHEMA is set, via asyncpg server_settings.

Why

Fixes #299. The pool was built from the bare DSN, so ensure_vector_indexes() ran in public while pgvector's tables live in POSTGRES_SCHEMA → startup crash (relation "langchain_pg_embedding" does not exist) and a silently-skipped JSON→JSONB migration. Full analysis in the issue.

Change

if POSTGRES_SCHEMA:
    schemas = _parse_schemas(POSTGRES_SCHEMA)
    if schemas:
        connect_kwargs["server_settings"] = {"search_path": _build_search_path(schemas)}

Reuses _build_search_path / _parse_schemas from the vector-store factory so the engine and pool can't drift. No behavior change when POSTGRES_SCHEMA is unset (no server_settings passed).

Testing

  • New unit tests in tests/services/test_database.py: the pool receives server_settings={"search_path": "myapp,public"} for a single schema, "myapp,extensions,public" for a comma-separated list, and no server_settings when unset.
  • tests/services/test_database.py + tests/services/test_vector_store_factory.py: 27 passed.

PSQLDatabase.get_pool() built the asyncpg pool from the bare DSN, so it ran
with the default search_path (public) while pgvector's tables live in
POSTGRES_SCHEMA (configured on the SQLAlchemy engine). ensure_vector_indexes()'s
unqualified DDL then failed at startup with
"relation langchain_pg_embedding does not exist" (or silently targeted a stale
public table), and the JSONB migration's current_schema() guard never matched
the real table — so the schema-isolation feature added in danny-avila#289 was unusable.

Pin the asyncpg pool to the same search_path the SQLAlchemy engine uses via
server_settings, reusing _build_search_path so the two connection paths cannot
drift. No behavior change when POSTGRES_SCHEMA is unset.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

POSTGRES_SCHEMA is ignored by the asyncpg pool: startup crashes and the JSONB migration is skipped

1 participant