Semantic knowledge bases reference v7

Reference for semantic knowledge base (semantic KB) functions. For guide-style documentation, see Semantic knowledge bases.

Creating and managing a semantic KB

For concepts (creating a KB, resolving to a single KB, and auto-processing) see Getting started.

aidb.create_semantic_kb

Creates a semantic KB over one or more schemas, embeds the schema metadata with the given model, and crawls the schemas immediately.

Parameters

ParameterTypeDefaultDescription
nameTEXTdefault_semkbName for the KB. When omitted, functions that also omit name/kb_name resolve to this KB as long as only one exists — see Getting started.
modelTEXTRequiredThe embedding model to use. A KB uses a single model for all of its metadata.
schemasTEXT[]ARRAY['public']The schema or schemas to crawl and index.
auto_processingTEXTDisabledHow the KB reacts to schema changes: Disabled, Live, or Background. See Auto-processing modes.
bypass_triggersBOOLEANfalseSkips the DDL event triggers for this KB. Schema changes are only picked up on refresh_semantic_kb().
curationBOOLEANfalseTurns on the comment review queue. See Descriptions and curation.
vector_indexJSONBNULLVector index configuration, built with a vector index config helper.

Returns

The KB name (TEXT).

Example

SELECT aidb.create_semantic_kb(
    name    => 'analytics_kb',
    model   => 'my_embedding_model',
    schemas => ARRAY['sales'],
    auto_processing => 'Live'
);

aidb.list_semantic_kbs

Lists every semantic KB with its model, schemas, and processing mode.

Example

SELECT * FROM aidb.list_semantic_kbs();

aidb.refresh_semantic_kb

Re-crawls a KB's schemas from scratch to pick up structural changes.

Parameters

ParameterTypeDefaultDescription
nameTEXTNULLThe KB to refresh. Omit when only one KB exists.

Example

SELECT aidb.refresh_semantic_kb('analytics_kb');

aidb.update_semantic_kb_auto_processing

Changes how a KB reacts to schema changes after creation.

Parameters

ParameterTypeDefaultDescription
nameTEXTNULLThe KB to update. Omit when only one KB exists.
auto_processingTEXTRequiredDisabled, Live, or Background. See Auto-processing modes.

Example

SELECT aidb.update_semantic_kb_auto_processing('analytics_kb', 'Live');

aidb.semantic_kb_stats

Returns entity counts and pending-change status for a KB.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to report on. Omit when only one KB exists.

Returns

ColumnDescription
totalTotal indexed entities.
tablesNumber of indexed tables.
columnsNumber of indexed columns.
pendingNumber of schema changes not yet reflected in the index.
relationshipsNumber of recorded relationships.
driftedNumber of relationships flagged by relationship_drift().
queriesNumber of statements imported from query history.

Example

SELECT total, tables, columns, pending, relationships, drifted, queries
FROM aidb.semantic_kb_stats('bank_kb');

aidb.delete_semantic_kb

Deletes a KB and its indexed metadata.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredThe KB to delete.

Example

SELECT aidb.delete_semantic_kb('analytics_kb');

Auto-processing modes

ModeBehavior
Disabled (default)No automatic updates. Call aidb.refresh_semantic_kb() when the schema changes.
LiveDDL triggers re-crawl affected relations as soon as the schema changes.
BackgroundA background worker processes queued DDL events, so re-indexing happens off the write path. Internally, this mode is driven by AIDB's pipeline machinery: a SemanticKB pipeline step consumes the queued schema-change events.

Searching a semantic KB

For concepts (searching everything at once, tuning results, finding the joins around a search) see Searching a semantic KB.

The composite entry point: runs the per-source ranked searches (schema metadata, relationships, curated semantic aliases, and query history) and fuses them into one ranked list with Reciprocal Rank Fusion (RRF).

Parameters

ParameterTypeDefaultDescription
query_textTEXT—The natural-language query.
kb_nameTEXTNULLThe KB to search. Omit to target the single KB when only one exists.
top_kINT10Number of ranked rows to return.
sourcesTEXT[]NULLSources to search: schema, alias, relationship, history. Omit for all.
entity_typesTEXT[]NULLEntity types to include: Table, View, Column, Alias, Relationship, Query. Omit for all.
rrf_kINT60Rank-fusion damping constant.
min_similarityDOUBLE PRECISIONNULLOptional similarity floor. Rows scoring below it are dropped. Omit for no floor.
include_proposedBOOLEANfalseAlso return relationships with status proposed, not just approved ones.

Returns

ColumnDescription
source_typeWhich source produced the row: schema, alias, relationship, or history.
entity_typeTable, View, Column, Alias, Relationship, or Query.
schema_nameThe schema the entity belongs to.
relation_nameThe table or view name.
column_nameThe column name (empty for table- and view-level matches).
object_refA qualified reference to the matched object. Populated for relationship rows only. Schema rows identify themselves through schema_name, relation_name, and column_name.
definitionThe structural definition that was embedded.
commentThe COMMENT ON text for the entity, if any.
scoreThe fused RRF score: 1 / (rrf_k + rank_within_source). Rows from different sources can tie — see Tuning results.
rankThe 1-based rank within the fused result set.
componentsJSONB detail of the per-source scores that fed the fusion.

Example

SELECT source_type, entity_type, schema_name, relation_name, column_name, rank
FROM aidb.semantic_kb_search(
    query_text => 'which customers spent the most',
    kb_name    => 'analytics_kb',
    top_k      => 5
);

aidb.get_metadata

The broadest schema search: returns columns, tables, and views together with full metadata.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to search. Omit when only one KB exists.
query_textTEXT—The natural-language query.
min_similarityDOUBLE PRECISIONNULLOptional similarity floor.
top_kINT10Number of results to return.
offsetINT0Paging offset.

Returns

schema_name, relation_name, column_name, entity_type, definition, comment, definition_vector, similarity.

Example

SELECT schema_name, relation_name, column_name, entity_type, similarity
FROM aidb.get_metadata(
    kb_name    => 'analytics_kb',
    query_text => 'customer email address',
    top_k      => 10
);

aidb.get_entity_definitions

Matches tables and views only.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to search. Omit when only one KB exists.
query_textTEXT—The natural-language query.
min_similarityDOUBLE PRECISIONNULLOptional similarity floor.
entity_typesTEXT[]ARRAY['Table', 'View']Entity types to include.
top_kINT10Number of results to return.
offsetINT0Paging offset.

Returns

schema_name, relation_name, entity_type, definition, comment, similarity.

Example

SELECT schema_name, relation_name, entity_type, similarity
FROM aidb.get_entity_definitions(
    kb_name      => 'analytics_kb',
    query_text   => 'orders and line items',
    entity_types => ARRAY['Table', 'View']
);

aidb.get_column_definitions

Returns just the matching column and table definitions — the leanest result, useful as compact grounding for SQL generation.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to search. Omit when only one KB exists.
query_textTEXT—The natural-language query.
min_similarityDOUBLE PRECISIONNULLOptional similarity floor.
top_kINT10Number of results to return.
offsetINT0Paging offset.

Returns

A single definition column.

Example

SELECT definition
FROM aidb.get_column_definitions(
    kb_name    => 'analytics_kb',
    query_text => 'order total amount'
);

aidb.search_by_comment

Matches only the natural-language COMMENT ON text attached to schemas, tables, and columns.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to search. Omit when only one KB exists.
query_textTEXT—The natural-language query.
min_similarityDOUBLE PRECISIONNULLOptional similarity floor.
top_kINT10Number of results to return.
offsetINT0Paging offset.

Returns

schema_name, relation_name, column_name, entity_type, definition, comment, similarity.

Example

SELECT relation_name, column_name, comment, similarity
FROM aidb.search_by_comment(
    kb_name    => 'analytics_kb',
    query_text => 'order status values'
);

aidb.semantic_kb_subgraph

Returns a question's neighborhood: seeds from the search hits and adds the relationships around them, so you get the joins connected to a question, not just the entities. Only traverses relationships that are approved and routable — see The routability rule.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to search.
query_textTEXT—The natural-language query.
top_kINT10Number of seed matches to expand.
max_hopsINT3Maximum join distance to traverse from a seed.

Returns

ColumnDescription
hopDistance from the seeding match.
from_objectThe relationship's left endpoint.
to_objectThe relationship's right endpoint.
via_objectThe bridge table, populated for a junction hop.
kindThe relationship kind.
predicateThe relationship's business-readable predicate, if any.
join_exprThe join expression.
confidenceThe relationship's confidence.
seeded_byThe matched object that pulled this hop in.
truncatedWhether the traversal stopped before exhausting all routes.

Example

SELECT hop, from_object, to_object, via_object, kind, confidence, seeded_by
FROM aidb.semantic_kb_subgraph(
    kb_name    => 'analytics_kb',
    query_text => 'money moved out of an account',
    top_k      => 3,
    max_hops   => 1
);

Managing relationships

For concepts (kind, source, status, confidence, the routability rule) see Relationships.

aidb.list_relationships

Lists recorded relationships, optionally filtered.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to query. Omit when only one KB exists.
relationTEXTNULLRestrict to relationships touching this table or view. Matched, not validated — a typo returns zero rows, not an error.
sourceTEXTNULLRestrict to one source. Matched, not validated.
statusTEXTNULLRestrict to one status (proposed, approved, stale, retired). Matched, not validated.
kindTEXTNULLRestrict to one kind. Matched, not validated.
levelTEXTNULLRestrict to one level (data or concept). Matched, not validated.

Returns

ColumnDescription
relationship_idThe relationship's ID.
left_objectThe left endpoint (schema.relation).
left_columnsThe left endpoint's join columns.
right_objectThe right endpoint (schema.relation).
right_columnsThe right endpoint's join columns.
junction_objectThe bridge table, populated for a junction kind.
kindSee Kind.
cardinalityDerived from the schema — for example many-to-one, one-to-one.
is_nullableWhether the join column can be null, which decides JOIN vs. LEFT JOIN.
sourceSee Source.
is_validatedWhether PostgreSQL has validated the backing foreign key. false for a crawled NOT VALID foreign key and for every non-crawl relationship.
confidenceRanking order, not a probability — see Status and confidence.
statusproposed, approved, stale, or retired.
levelThe relationship's level: data or concept. concept isn't usable yet — every relationship in Phase 1 is data.
join_exprThe join expression.
definitionThe relationship's join definition.
constraint_nameThe backing constraint's name, when the relationship came from one.
descriptionThe constraint's comment, if any.
curated_labelA short, human-written name for the relationship (for example, loan repayment), set with the curated_label parameter of aidb.add_relationship(). Refreshes and re-crawls never overwrite it. If the backing constraint is dropped, a labelled relationship is marked retired rather than deleted, so the label is kept. It is stored and returned only: it isn't embedded, doesn't affect aidb.semantic_kb_search(), and isn't used by the route finders.
stale_reasonWhy the relationship is stale, for example column_dropped when a named join column is renamed or dropped. Set when status is stale.
provenanceJSONB detail: model, crawl ID, and where the relationship's claim came from (asserted_by).

Example

SELECT left_object, left_columns, right_object, right_columns, kind, cardinality, is_nullable
FROM aidb.list_relationships('bank_kb', relation => 'bank.customers')
ORDER BY left_object, left_columns;

aidb.find_join_path

Finds a route between two tables, ranked shortest first, then by confidence.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to search.
from_objectTEXTNULLThe starting table (schema.relation).
to_objectTEXTNULLThe destination table (schema.relation).
max_hopsINT4Maximum number of hops in a single route.
max_pathsINT10Caps the search, not the ranking — enumeration stops as soon as max_paths routes are found, so a small value can discard the best route.

Returns

ColumnDescription
path_rank1-based rank, shortest first, then by confidence.
routeThe full path as A -> B -> C.
hopsNumber of hops in the route.
kindThe kind of the weakest hop in the route.
descriptionThe weakest hop's constraint comment, if any — see list_relationships()'s description.
confidenceThe route's confidence.
join_clauseReady-to-paste SQL for the join.
pathThe route as JSONB: an array with one object per hop, in the order traveled. Each hop has relationship_id, from, to, via (the bridge table for a junction hop, otherwise null), kind, predicate (the relationship's wording read in the direction of travel), flipped (true when the hop goes from the relationship's right endpoint to its left), is_nullable (true means use a LEFT JOIN for this hop), and confidence. Use it when code needs per-hop detail; route and join_clause give the same route as text.
truncatedWhether max_paths cut the search short before ranking. Always check this on a route you rely on.

Example

SELECT path_rank, route, kind, confidence
FROM aidb.find_join_path('bank_kb', 'bank.transactions', 'bank.customers');

aidb.suggest_joins

Lists every relationship one hop from a given set of tables, in either direction.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to search.
relationsTEXT[]NULLThe table(s) to find joins from.
include_proposedBOOLEANfalseAlso return relationships still in proposed status, not just approved ones.

Returns

ColumnDescription
relationship_idThe relationship's ID.
from_objectThe starting table (schema.relation).
to_objectThe other end of the join (schema.relation).
via_objectThe bridge table, populated for a junction kind.
kindSee Kind.
predicateBusiness-readable description of the join.
is_nullableWhether the join column can be null, which decides JOIN vs. LEFT JOIN.
join_exprThe join expression.
confidenceRanking order, not a probability — see Status and confidence.
statusproposed, approved, stale, or retired.
descriptionThe relationship's description: the constraint's comment for a crawled relationship, or whatever was passed as description to add_relationship.

Example

SELECT from_object, to_object, kind, confidence
FROM aidb.suggest_joins('bank_kb', ARRAY['bank.accounts'])
ORDER BY from_object, to_object;

aidb.add_relationship

Records a relationship by hand. SQL-only — not registered as a native agent tool. See Adding a relationship by hand.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to add to.
left_objectTEXTRequiredThe left endpoint (schema.relation).
right_objectTEXTRequiredThe right endpoint (schema.relation).
kindTEXTRequiredSee Kind.
predicateTEXTRequired for key_joinBusiness-readable description of the join. Validated with a clear error if missing.
left_columnsTEXT[]Required for key_joinThe left endpoint's join columns.
right_columnsTEXT[]Required for key_joinThe right endpoint's join columns.
junction_objectTEXTNULLThe bridge table, for a junction kind.
junction_left_columnsTEXT[]NULLThe junction table's join columns on the left_object side.
junction_right_columnsTEXT[]NULLThe junction table's join columns on the right_object side.
inverse_predicateTEXTNULLThe join described from right_object's point of view, mirroring predicate.
cardinalityTEXTRequired for key_joinFor example many-to-one, one-to-one. Omitting it (with is_nullable) raises the store's raw CHECK-constraint violation, not a friendly message.
is_nullableBOOLEANRequired for key_joinWhether the join column can be null. Same omission behavior as cardinality.
join_exprTEXTNULLA raw join expression, for a conditional relationship whose join needs an expression or a cast that left_columns/right_columns alone can't express.
descriptionTEXTNULLStored as the relationship's description.
curated_labelTEXTNULLA short, human-written name for the relationship (for example, loan repayment). Refreshes and re-crawls never overwrite it. If the backing constraint is dropped, a labelled relationship is marked retired rather than deleted, so the label is kept. Stored and returned only: it isn't embedded, doesn't affect aidb.semantic_kb_search(), and isn't used by the route finders.
sourceTEXTmanualOnly manual or agent are accepted here. crawl is rejected.
levelTEXTdataconcept is rejected today — concept-level relationships aren't traversable yet.
concept_relationTEXTNULLFor relationships between concepts rather than relations, paired with level => 'concept'. Not usable yet — level => 'concept' is rejected today.

Returns

ColumnDescription
relationship_idThe relationship's ID.
left_objectThe left endpoint.
left_columnsThe left endpoint's join columns.
right_objectThe right endpoint.
right_columnsThe right endpoint's join columns.
kindSee Kind.
sourcemanual or agent.
statusapproved when source => 'manual'; proposed when source => 'agent'.
confidenceThe prior for the given kind, capped at 0.90 for an added claim.
actioninserted or updated, depending on whether the call created a new row or matched an existing one (same endpoints, columns, and kind).
join_exprThe join expression.

Example

SELECT relationship_id, left_object, left_columns, source, status, confidence, action
FROM aidb.add_relationship(
    kb_name       => 'bank_kb',
    left_object   => 'bank.payments',
    right_object  => 'bank.loan_instalments',
    kind          => 'key_join',
    predicate     => 'settles',
    left_columns  => ARRAY['loan_id', 'instalment_no'],
    right_columns => ARRAY['loan_id', 'instalment_no'],
    cardinality   => 'many-to-one',
    is_nullable   => false,
    description   => 'A payment settles one instalment of a loan.');

aidb.add_relationship_as_agent

Records a relationship the same way as aidb.add_relationship(), but always sets source = 'agent'. This is the function backing the add_relationship native agent tool — despite the shared tool name, the tool calls add_relationship_as_agent(), not add_relationship(). See the naming note in Adding a relationship by hand.

Parameters

Takes only kb_name, left_object, right_object, kind, predicate, left_columns, right_columns, cardinality, is_nullable, and description — no junction fields, join_expr, inverse_predicate, curated_label, level, or source, so an agent can record only a key_join this way. The relationship is always stored with source = 'agent' and status = 'proposed', and isn't routed until a person approves it by re-asserting it with aidb.add_relationship().


aidb.delete_relationship

Deletes a relationship. Overloaded — takes either a relationship_id or the full identifying shape (endpoints, columns, kind) — so name your arguments rather than passing a single positional one. A crawled relationship can't be deleted while its backing constraint still stands, since the next crawl would restore it. Drop the constraint first.

Parameters

By ID

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to delete from.
relationship_idBIGINTNULLThe relationship to delete, by ID.

By shape

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to delete from.
left_objectTEXTNULLThe left endpoint (schema.relation).
right_objectTEXTNULLThe right endpoint (schema.relation).
left_columnsTEXT[]NULLThe left endpoint's join columns.
right_columnsTEXT[]NULLThe right endpoint's join columns.
junction_objectTEXTNULLThe bridge table, for a junction kind.
kindTEXTNULLSee Kind.
sourceTEXTNULLRestrict to one source.
constraint_nameTEXTNULLThe backing constraint's name, when the relationship came from one.
levelTEXTdataRestrict to one level (data or concept).
concept_relationTEXTNULLFor relationships between concepts rather than relations, paired with level => 'concept'. Not usable yet — level => 'concept' is rejected today.

Returns

ColumnDescription
relationship_idThe deleted relationship's ID.
left_objectThe left endpoint.
right_objectThe right endpoint.
kindSee Kind.
sourceThe relationship's source.

Both overloads return the same shape.

Example

SELECT * FROM aidb.delete_relationship(kb_name => 'bank_kb', relationship_id => 10);

aidb.relationship_drift

Detects and repairs drift on hand-added relationships when a named column is renamed or dropped. This is a write, not a read — it re-runs detection and repair on every call, so it can't run inside a read-only transaction. The native agent tool for this function is flagged read-only in the tool catalog. That flag is inaccurate.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to check for drift.

Returns

ColumnDescription
detected_atWhen the drift was detected and repaired.
relationship_idThe affected relationship's ID.
left_objectThe left endpoint.
right_objectThe right endpoint.
sourceThe relationship's source — any source except crawl. Crawled relationships follow their constraint instead: they're removed or retired when it's dropped.
eventFor example column_dropped, when a named column is renamed or dropped.
detailDetail of what changed.

Example

SELECT detected_at, relationship_id, left_object, right_object, source, event, detail
FROM aidb.relationship_drift('bank_kb');

aidb.import_query_history

Reads pg_stat_statements to find joins the application runs but the schema never declared. Requires pg_stat_statements to be loaded (shared_preload_libraries) and installed (CREATE EXTENSION pg_stat_statements;). Errors if the extension isn't installed, rather than returning zero rows. See Learning from the query workload.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to import into.
min_callsINT5Minimum execution count for a statement to be considered. Lower to see more of the workload.
max_statementsINT1000Caps how many statements from pg_stat_statements are considered in one call.
derive_candidatesBOOLEANtrueWhether to also derive relationship candidates from the import; see candidates_derived.

Returns

ColumnDescription
import_idThis import run's ID.
statements_readStatements read from pg_stat_statements.
statements_ingestedStatements that passed min_calls and were recorded.
skippedStatements skipped, and why.
candidates_derivedNumber of relationship candidates derived from the import.
outcomeok, or an error/warning summary.

Example

SELECT import_id, statements_read, statements_ingested, skipped, candidates_derived, outcome
FROM aidb.import_query_history('bank_kb');

aidb.relationship_candidates

Lists relationship candidates derived from query history that aren't yet known relationships.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to list candidates for.
sourceTEXThistoryRestrict to candidates from one source. Matched, not validated.
relationTEXTNULLRestrict to candidates touching this table or view. Matched, not validated — a typo returns zero rows, not an error.
min_exec_countBIGINTNULLMinimum execution count for a candidate to be included; see executions.
limit_rowsINT100Caps how many candidates are returned.

Returns

ColumnDescription
relationship_idThe candidate's ID.
left_objectThe left endpoint.
left_columnsThe left endpoint's join columns.
right_objectThe right endpoint.
right_columnsThe right endpoint's join columns.
confidenceThe candidate's confidence.
statementsThe statements that produced this candidate.
executionsExecution count backing the candidate.
definitionThe candidate's join definition.

Example

SELECT relationship_id, left_object, left_columns, right_object, right_columns,
       confidence, statements, executions, definition
FROM aidb.relationship_candidates('bank_kb');

aidb.accept_relationship_candidates

Accepts one or more relationship candidates, making them routable. source becomes manual. Provenance keeps the evidence.

Parameters

ParameterTypeDefaultDescription
candidate_idsBIGINT[]—The candidates to accept.
kb_nameTEXTNULLThe KB the candidates belong to. Takes this position second, unlike every other function in this section, which takes kb_name first.

Returns

ColumnDescription
relationship_idThe candidate's ID.
acceptedWhether the candidate was accepted.
reasonWhy, for example approved, or no such relationship in this knowledge base for an ID that isn't a candidate. Also refuses a relationship an agent proposed directly — reassert it with aidb.add_relationship() instead.

Example

SELECT * FROM aidb.accept_relationship_candidates(
  ARRAY[(SELECT relationship_id FROM aidb.relationship_candidates('bank_kb') LIMIT 1)]::bigint[],
  'bank_kb');

aidb.classify_query_history_statement

Explains why a single statement would or wouldn't be used by import_query_history() — the quickest way to understand an import that ingested less than expected.

Parameters

ParameterTypeDefaultDescription
statementTEXT—The normalized SQL statement to classify.

Returns

A JSONB object:

KeyDescription
qualifiesWhether import_query_history() would ingest this statement.
relationsThe tables the statement touches.
predicatesThe join predicates found, each with left_relation, left_column, right_relation, right_column.
skip_reasonWhy the statement doesn't qualify, for example unparseable. null when it qualifies.
had_unresolvedWhether the statement had a join AIDB couldn't resolve into a predicates entry.

Example

SELECT jsonb_pretty(aidb.classify_query_history_statement(
  'SELECT 1 FROM bank.accounts a JOIN bank.customers c ON a.cust_id = c.id'));
{
    "qualifies": true,
    "relations": [
        "bank.accounts",
        "bank.customers"
    ],
    "predicates": [
        {
            "left_column": "cust_id",
            "right_column": "id",
            "left_relation": "bank.accounts",
            "right_relation": "bank.customers"
        }
    ],
    "skip_reason": null,
    "had_unresolved": false
}

A statement that doesn't qualify returns just enough to say why:

SELECT aidb.classify_query_history_statement('UPDATE bank.accounts SET opened_on = now()');
{"qualifies": false, "skip_reason": "unparseable"}

aidb.list_query_history

Lists the normalized statements, relations touched, and join predicates recorded by import_query_history().

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to list history for.
relationTEXTNULLRestrict to statements touching this table or view. Matched, not validated — a typo returns zero rows, not an error.
min_callsBIGINTNULLMinimum execution count for a statement to be included; see exec_count.
seen_sinceTIMESTAMPTZNULLRestrict to statements last seen at or after this time; see last_seen.
executed_byTEXTNULLRestrict to statements run by this Postgres role; see executed_by.
limit_rowsINT100Caps how many statements are returned.

Returns

ColumnDescription
normalized_sqlThe statement, with literals replaced by parameters.
referenced_objectsThe tables and views the statement touches.
join_predicatesThe join predicates found, as JSONB: an array of {"l": {"relation", "column"}, "r": {"relation", "column"}} pairs.
exec_countExecution count.
first_seenWhen the statement was first recorded.
last_seenWhen the statement was last recorded.
executed_byThe Postgres role that ran it.

Example

SELECT normalized_sql, referenced_objects, join_predicates, exec_count, executed_by
FROM aidb.list_query_history('bank_kb') ORDER BY normalized_sql;

Managing descriptions and curation

For concepts (modes, protection, concurrency, review) see Descriptions and curation.

aidb.add_comment_to_object

Writes a COMMENT ON for a table, view, column, or constraint, and reconciles every KB that indexes the object so the new text is searchable immediately.

Parameters

ParameterTypeDefaultDescription
schema_nameTEXTRequiredThe object's schema.
object_nameTEXTRequiredThe table or view name.
commentTEXTRequiredThe description text.
object_typeTEXTtabletable, view, column, or constraint.
column_nameTEXTNULLThe column to comment on. Only used when object_type => 'column'.
constraint_nameTEXTNULLThe constraint to comment on. Only used when object_type => 'constraint'.
kb_nameTEXTNULLThe KB to reconcile. Omit when only one KB exists.
modeTEXTaddadd refuses when a description already exists. edit overwrites it (subject to the protections below).
expected_commentTEXTNULLOptimistic-concurrency check: the write fails if the current comment doesn't match.
forceBOOLEANfalseRequired to overwrite a description AIDB didn't write itself (treated as a person's work).

Returns

ColumnDescription
currentThe live comment after the call (NULL if queued for review, not applied).
outcomeapplied or queued.
previousThe comment before the call.
proposedThe proposed comment, when outcome is queued.
knowledge_bases_reconciledKBs updated by the write (only when outcome is applied).
knowledge_basesKBs that will be updated once the proposal is approved (when outcome is queued).

Example

SELECT jsonb_pretty(aidb.add_comment_to_object(
  'bank', 'transactions', 'A movement of money against an account.', kb_name => 'bank_kb'));

aidb.remove_comment

Clears a description. Takes the same expected_comment and force protections as add_comment_to_object().

Parameters

ParameterTypeDefaultDescription
schema_nameTEXTRequiredThe object's schema.
object_nameTEXTRequiredThe table or view name.
kb_nameTEXTNULLThe KB to reconcile. Omit when only one KB exists.
expected_commentTEXTNULLOptimistic-concurrency check.
forceBOOLEANfalseRequired to remove a description AIDB didn't write itself.

Example

SELECT jsonb_pretty(aidb.remove_comment('bank', 'transactions', kb_name => 'bank_kb'));

aidb.list_proposed_comments

Lists comment writes queued for review under curation.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to list proposals for.

Returns

object, object_type, proposed_comment, proposed_by.

Example

SELECT object, object_type, proposed_comment, proposed_by FROM aidb.list_proposed_comments('bank_kb');

aidb.resolve_object_comment

Approves or rejects a proposed comment. Deliberately SQL-only — not a native agent tool, so approving a proposal stays with a person.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB the proposal belongs to.
schema_nameTEXTRequiredThe object's schema.
object_nameTEXTRequiredThe table or view name.
decisionTEXTRequiredapprove or reject.

Example

SELECT jsonb_pretty(aidb.resolve_object_comment('bank_kb', 'bank', 'accounts', 'approve'));

aidb.update_semantic_kb_curation

Turns the comment-review queue on or off for a KB. Deliberately SQL-only — not a native agent tool, so toggling review stays with a person.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLThe KB to update.
enabledBOOLEANRequiredtrue to queue writes for review, false to apply them immediately.

Returns

current, previous (both booleans, as JSONB).

Example

SELECT aidb.update_semantic_kb_curation('bank_kb', true);

aidb.semantic_kb_audit

Lists recorded semantic KB operations. Only a superuser or the extension owner can call it.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTNULLOnly return records for this knowledge base. NULL returns records for all knowledge bases.
opTEXTNULLOnly return this operation category: retrieve, execute, annotate, write, propose, approve, reject, or admin.
funcTEXTNULLOnly return records written by this function.
sinceTIMESTAMPTZNULLOnly return records from this time on.
limit_rowsINTEGER100Maximum number of records to return.

Returns

TABLE(ts TIMESTAMPTZ, kb_name TEXT, op TEXT, func TEXT, caller_kind TEXT, actor_name TEXT, login_name TEXT, object_ref TEXT, detail JSONB)

caller_kind is agent_hub, mcp, or direct. actor_name is the role the operation ran as; login_name is the session's login role. object_ref names the affected object. detail holds operation-specific data, for example mode, forced, and discarded for a description write.

Example

SELECT ts, op, func, actor_name, object_ref, detail
FROM aidb.semantic_kb_audit(kb_name => 'bank_kb', op => 'write')
ORDER BY ts DESC LIMIT 3;

aidb.semkb_audit_prune

Deletes the audit records of one knowledge base that are older than a given age. Nothing deletes records automatically; run this function as an operator or from a scheduled job such as pg_cron. The caller must be a member of the knowledge base's owner role.

Parameters

ParameterTypeDefaultDescription
kb_nameTEXTRequiredThe knowledge base whose records to delete.
older_thanINTERVALNULLDelete records older than this. NULL uses the aidb.semkb_audit_retention parameter (default 30 days). A zero interval deletes nothing.

Returns

BIGINT: the number of records deleted.

Example

SELECT aidb.semkb_audit_prune('bank_kb', '90 days');

Creating and managing aliases

For concepts (multi-KB membership, finding, running) see Semantic aliases.

aidb.create_semantic_alias

Creates a named, parameterized SQL query paired with a natural-language description, and embeds the description in every semantic KB that owns the query's schema(s). Placeholders in the query use ${name} syntax, and the query must be a single read-only SELECT.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredUnique alias name.
descriptionTEXTRequiredHuman-readable description — this is what gets embedded for search.
query_textTEXTRequiredA single read-only SELECT, with ${name} placeholders for parameters.
paramsJSONBNULLArray of parameter definitions (see below).
kb_nameTEXTNULLOptional. Narrows the alias to just this KB. Omit to embed it for every KB that owns the query's schema(s).

Each entry in the params JSONB array:

FieldRequiredDescription
nameYesMatches a ${name} placeholder in query_text.
param_typeYesPostgreSQL type: text, integer, numeric, date, and so on.
descriptionNoHuman-readable description of the parameter.
enum_valuesNoAllowed values, when the parameter is constrained to a set.

If the query's schema maps to more than one KB, the alias is embedded once for each of them — creation never fails on ambiguity. Pass kb_name only to narrow it to a single KB. A schema not yet owned by any KB leaves the alias unembedded, until a KB covering it is created or adopts it later.

Returns

The alias name (TEXT).

Example

SELECT aidb.create_semantic_alias(
    name        => 'monthly_revenue',
    description => 'Total revenue for a given month and year',
    query_text  => $$
        SELECT SUM(amount) AS total
        FROM sales.orders
        WHERE EXTRACT(MONTH FROM order_date) = ${month}
          AND EXTRACT(YEAR  FROM order_date) = ${year}
    $$,
    params => '[
        {"name": "month", "param_type": "integer", "description": "Month number (1-12)"},
        {"name": "year",  "param_type": "integer", "description": "Four-digit year"}
    ]'
);

aidb.search_semantic_aliases

Searches aliases by natural-language query, embedding the query once per distinct model among the searched KBs so results stay dimension-safe and de-duplicated to the best match per alias. Without kb_name, it searches default_semkb if that KB exists, or every KB with stored alias embeddings otherwise. Aliases also appear in aidb.semantic_kb_search() results with source_type = 'alias'.

Parameters

ParameterTypeDefaultDescription
query_textTEXT—Natural-language query.
min_similarityDOUBLE PRECISIONNULLOptional similarity floor.
top_kINT10Maximum results.
offsetINT0Paging offset.
kb_nameTEXTNULLOptional. Narrows the search to this KB. Omit to resolve default_semkb, or all KBs with alias embeddings, per the precedence above.

Returns

name, query_text, similarity.

Example

SELECT name, query_text, similarity
FROM aidb.search_semantic_aliases(
    query_text => 'how much money did we make last month',
    kb_name    => 'analytics_kb',
    top_k      => 5
);

aidb.execute_semantic_alias

Runs an alias by name, substituting its parameters. Use execute_role to run it under a least-privilege reporting role rather than the connecting user. Because every alias is validated as a single read-only SELECT at creation and re-validated at execution, an alias can't be used to run writes.

Parameters

ParameterTypeDefaultDescription
alias_nameTEXT—Alias to run.
argsJSONBNULLObject of parameter values keyed by name.
execute_roleTEXTNULLPostgreSQL role to run the query as — requires the appropriate SET ROLE grants.

Returns

A set of result rows, each a JSONB object.

Example

SELECT result
FROM aidb.execute_semantic_alias(
    alias_name => 'monthly_revenue',
    args       => '{"month": 3, "year": 2025}'
);

aidb.get_semantic_aliases

Lists every alias, with its name, description, query text, and parameter count.

Returns

name, description, query_text, param_count.

Example

SELECT * FROM aidb.get_semantic_aliases();

aidb.get_semantic_alias

Gets one alias by name, with its full definition: description, query text, and parameter list.

Parameters

ParameterTypeDefaultDescription
alias_nameTEXTRequiredThe alias to get.

Returns

name, description, query_text, params.

Example

SELECT * FROM aidb.get_semantic_alias('monthly_revenue');

aidb.update_semantic_alias

Updates an alias. Changing query_text or kb_name re-infers its owning KB(s) and reconciles embeddings, adding KBs it newly belongs to and dropping ones it no longer does. Changing description re-embeds it in every KB it's already in.

Parameters

Same fields as aidb.create_semantic_alias() (name identifies the alias to update). Which fields are required versus optional on update isn't stated in the guide — confirm before publishing.

Example

SELECT aidb.update_semantic_alias(
    name        => 'monthly_revenue',
    description => 'Total revenue for a given month and year, in USD'
);

aidb.delete_semantic_alias

Deletes an alias by name.

Parameters

ParameterTypeDefaultDescription
nameTEXTRequiredThe alias to delete.

Example

SELECT aidb.delete_semantic_alias('monthly_revenue');