PostgreSQL Field Guide

Schema retrieval and documentation

Generate compact, traceable model context from PostgreSQL catalogs

Do not let the model explore production

A production agent should not have unbounded catalog exploration. A trusted build job extracts schema, redacts and versions it, and publishes it to retrieval. Runtime returns only a task-relevant subgraph.

Tables and columns

Use information_schema for portable basics:

SELECT
  c.table_schema,
  c.table_name,
  c.ordinal_position,
  c.column_name,
  c.data_type,
  c.udt_name,
  c.is_nullable,
  c.column_default
FROM information_schema.columns AS c
WHERE c.table_schema = ANY($1::text[])
ORDER BY c.table_schema, c.table_name, c.ordinal_position;

Column comments come from PostgreSQL catalogs:

SELECT
  n.nspname AS schema_name,
  cls.relname AS table_name,
  a.attname AS column_name,
  col_description(cls.oid, a.attnum) AS comment
FROM pg_catalog.pg_attribute AS a
JOIN pg_catalog.pg_class AS cls ON cls.oid = a.attrelid
JOIN pg_catalog.pg_namespace AS n ON n.oid = cls.relnamespace
WHERE n.nspname = ANY($1::text[])
  AND cls.relkind IN ('r', 'p')
  AND a.attnum > 0
  AND NOT a.attisdropped;

Foreign-key edges form the task graph

SELECT
  src_ns.nspname AS table_schema,
  src.relname AS table_name,
  src_col.attname AS column_name,
  dst_ns.nspname AS foreign_table_schema,
  dst.relname AS foreign_table_name,
  dst_col.attname AS foreign_column_name
FROM pg_catalog.pg_constraint AS con
JOIN pg_catalog.pg_class AS src ON src.oid = con.conrelid
JOIN pg_catalog.pg_namespace AS src_ns ON src_ns.oid = src.relnamespace
JOIN pg_catalog.pg_class AS dst ON dst.oid = con.confrelid
JOIN pg_catalog.pg_namespace AS dst_ns ON dst_ns.oid = dst.relnamespace
CROSS JOIN LATERAL unnest(con.conkey, con.confkey)
  AS key_columns(src_attnum, dst_attnum)
JOIN pg_catalog.pg_attribute AS src_col
  ON src_col.attrelid = src.oid AND src_col.attnum = key_columns.src_attnum
JOIN pg_catalog.pg_attribute AS dst_col
  ON dst_col.attrelid = dst.oid AND dst_col.attnum = key_columns.dst_attnum
WHERE con.contype = 'f'
  AND src_ns.nspname = ANY($1::text[])
ORDER BY con.oid, src_col.attnum;

conkey and confkey correspond positionally; parallel unnest preserves composite foreign-key column mappings. Joining information_schema views only by constraint_name can produce a Cartesian product of columns for a composite key.

Documentation build flow

merge migration
 → apply all migrations to an ephemeral database
 → extract catalogs
 → normalize ordering and remove environment values
 → generate JSON plus Markdown summaries
 → hash / bind migration version
 → review schema diff
 → publish to retrieval index

A table summary keeps purpose, primary and foreign keys, column types and nullability, constraints, business comments, sensitivity, and only the most important query indexes. Expand function bodies, view definitions, and policies on demand.

Prevent staleness

Every agent tool returns contract_version. If the runtime migration version differs from retrieval, reject high-risk requests and trigger a rebuild. Never silently use a stale contract.

Last updated on

On this page