Skip to content

pgvector: read tenants from json metadata; never drop a filter - #17

Merged
xkimopro merged 1 commit into
Vecshield:mainfrom
nnbakirtzis:fix/pgvector-json-metadata
Nov 21, 2026
Merged

xkimopro merged 1 commit into
Vecshield:mainfrom
nnbakirtzis:fix/pgvector-json-metadata

Conversation

@nnbakirtzis

Copy link
Copy Markdown
Contributor

What this changes

LangChain (cmetadata jsonb) and LlamaIndex (metadata_ json) keep the tenant inside a json column. The isolation probe reported such tables as single-tenant / not applicable while several tenants shared them.

  • json object columns are flattened into chunk metadata. A key that collides with a real column is kept as <col>.<key>.
  • query(where=) filters inside the column with @> (GIN-indexable) plus -> equality, so the match is exact and typed. A col.key prefix picks the column explicitly.
  • query(where=) used to silently drop a key that wasn't a column, so a tenant-scoped query ran unfiltered. It now raises UnsupportedCapability (see the checklist).
  • Collection metadata lists json_metadata_columns.
  • docs/pgvector.md: new section "Metadata in json columns".

How you verified it

  • ruff check .: clean
  • pytest -rs with VECSHIELD_PG_DSN pointing at PostgreSQL 17.12 / pgvector 0.8.7, so the live tests ran instead of skipping. Across tests/test_pgvector.py, test_pgvector_unit.py, test_pg_access_hardening.py, test_store_facts_selfhosted.py and test_isolation.py: 277 passed, 0 skipped.
  • Full suite, with this PR merged together with the five sibling pgvector PRs: 17 failed / 3 errors. These are the same Windows-only and flaky failures main shows on this machine (symlinks, /proc, webhook sockets, test_demo). No new failures.
  • npm test: console unchanged
  • benchmark: no detector changed
  • Audited a local lab of realistic pgvector layouts as a least-privileged auditor, before and after. The layouts were LangChain, LlamaIndex, RLS multi-tenant, partitioned halfvec, and a least-privilege control.

Tests added:

  • Unit: flattening and collisions; non-object json; the jsonb and json SQL shapes; a prefixed key; refusal of an unknown key or a non-JSON value.
  • Live: a LangChain-layout table is filtered by tenant_id, and probe_isolation sees 2 tenants.

This is one of six independent pgvector PRs, all based on main (0041576). They merge cleanly with each other in any order.

Checklist

  • No route changed.
  • No check added or renamed.
  • No detector changed.
  • Nothing reports unknown coverage as a pass, and no partial read is reported as clean.
  • No secrets or real data: fixtures are synthetic.
  • This changes behaviour and has no ADR; please decide whether it needs one. query(where=) now raises UnsupportedCapability instead of ignoring a key that is neither a column nor inside a json metadata column. This includes filters passed through the runtime proxy, which now fail closed instead of returning unfiltered results.

LangChain (cmetadata) and LlamaIndex (metadata_) keep the tenant in a json
or jsonb column, so the isolation probe saw no tenant field and reported a
table several tenants share as single-tenant. A json metadata column holding
an object is now flattened into its top-level keys (a key colliding with a
column is kept as <column>.<key>), and query(where=) filters such a key with
@> (GIN-indexable) plus -> equality (exact, typed).

query(where=) used to ignore a key that was not a column, running the query
unfiltered; it now raises UnsupportedCapability instead.
@xkimopro
xkimopro merged commit b166b79 into Vecshield:main Nov 21, 2026
7 checks passed
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.

2 participants