Database
PostgreSQL with pgvector, hosted on Supabase. 14 tables covering tariff data, embeddings, enrichments, graph edges, references, and resources.
Schema Overview
Core Data
| Table | Primary Key | Rows | Purpose |
|---|---|---|---|
tariffs | hts8 | ~13,000 | Tariff lines with rates and dates |
hts_chapters | chapter | 98 | 2-digit chapter descriptions |
hts_schedule_sections | section_roman | 22 | Roman numeral section groupings |
hts_headings | hts4 | ~961 | 4-digit heading descriptions |
hts_sections | id | ~35,700 | Raw hierarchical section data |
hts_notes | id | ~1,067 | Interpretive notes |
hts_raw_notes | id | ~121 | Full-text HTML note blobs |
AI Pipeline
| Table | Primary Key | Rows | Purpose |
|---|---|---|---|
hts_embeddings | (code, level, variant) | ~38,000 | Multi-level vector embeddings |
hts6_enrichments | hts6 | ~5,714 | AI-generated descriptions and keywords |
Knowledge Graph
| Table | Primary Key | Rows | Purpose |
|---|---|---|---|
hts_edge_candidates | (source_code, target_code, source_level) | ~142,000 | KNN candidate pairs |
hts_semantic_edges | (source_code, target_code) | ~142,000 | Classified relationships |
hts_graph_runs | id | Variable | Pipeline run tracking |
References & Resources
| Table | Primary Key | Rows | Purpose |
|---|---|---|---|
tariff_code_references | (source_hts8, referenced_code_clean) | ~2,400 | Cross-references from descriptions |
tariff_resources | id | ~45 | Curated trade policy documents |
Key Tables
tariffs
The central table with ~13,000 tariff lines:
| Column | Type | Description |
|---|---|---|
hts8 | text PK | 8-digit HTS code |
brief_description | text | Product description |
quantity_1_code | text | Unit of measure |
mfn_text_rate | text | MFN rate as text (e.g., “6.4%“) |
mfn_ad_val_rate | float | MFN ad valorem percentage |
mfn_specific_rate | float | MFN specific rate |
begin_effect_date | date | Start of rate period |
end_effective_date | date | End of rate period |
usmca_*, cafta_*, … | various | 50+ FTA rate columns |
hts_embeddings
Multi-level vector embeddings for search:
| Column | Type | Description |
|---|---|---|
code | text | HTS code (2-8 digits) |
level | text | chapter, hts4, hts6, hts8 |
variant | text | full, short |
embedding | vector(1536) | OpenAI embedding |
embedding_text | text | Source text that was embedded |
Primary key: (code, level, variant)
hts_semantic_edges
Classified knowledge graph edges:
| Column | Type | Description |
|---|---|---|
source_code | text | Source HTS code |
target_code | text | Target HTS code |
relationship_type | text | One of 10 types |
confidence | float | 0-1 confidence score |
reasoning | text | LLM reasoning |
key_differentiator | text | What distinguishes the codes |
similarity | float | Original cosine similarity |
bidirectional | boolean | Symmetric relationship |
haiku | text | 5-7-5 creative summary |
classification_clarity | float | How clear-cut |
source_inclusionary | text[] | Defining terms for source |
source_exclusionary | text[] | Excluding terms for source |
target_inclusionary | text[] | Defining terms for target |
target_exclusionary | text[] | Excluding terms for target |
run_id | text | Classification run ID |
pgvector Setup
pgvector must be enabled in the database:
CREATE EXTENSION IF NOT EXISTS vector;Create an index on the embedding column for fast KNN:
CREATE INDEX ON hts_embeddings
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);Or for better recall at the cost of more memory:
CREATE INDEX ON hts_embeddings
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);Connection Pooling
The application uses psycopg2 connection pooling:
| Setting | Default | Description |
|---|---|---|
pool_min_size | 2 | Minimum connections |
pool_max_size | 10 | Maximum connections |
pool_timeout | 5.0s | Wait time for a connection |
pool_max_idle | 300.0s | Max idle time before closing |
db_statement_timeout | 30s | Query timeout |
Each connection initializes:
- Registers the pgvector type
- Sets statement timeout
- Sets search path
Caching
Application-level caching reduces database load:
| Cache | TTL | Scope |
|---|---|---|
get_tariff_by_code | 3600s | LRU per HTS8 code |
get_tariff_count | 300s | Single value |
has_embeddings | Session | Boolean per level |
get_embeddings_count | 300s | Per level/variant |