Transactional databases store vast amounts of unstructured text — clinical notes, customer reviews, support tickets, invoices, and property descriptions. Historically, working with this text meant building complex application code: You had to export data from the database, call external model endpoints, write custom prompt wrappers, parse unpredictable responses, manage retry logic, and update database records. This application-side loop creates operational friction. It introduces query latency, bloats application code, increases model token costs, and creates data compliance risks by moving sensitive information across multiple network boundaries.
We built AlloyDB AI to bring Gemini world knowledge directly into your operational database. Rather than treating artificial intelligence as a disconnected external service, we integrated model intelligence directly into the database engine. Today, we are announcing the preview of two new SQL functions in the AlloyDB AI query engine — ai.classify and ai.extract — available in the google_ml_integration extension (version 1.6.1 or later) on PostgreSQL 18, allowing you to perform classification and structured extraction directly inside your database using standard SQL. Supported by smart batching for AI functions (previously known as AI Function Acceleration), these functions deliver production scalability and performance.
Now, let’s take a look at these new functions and how to use them.
Classifying text directly in SQL with ai.classify
The ai.classify function categorizes text into user-defined classes without requiring manual prompt engineering. You can specify categories as a simple list of labels or provide a list containing label names and descriptions to guide model context.
Key capabilities include:
-
Single-label classification (
output_mode => 'single') to assign the single best category -
Multi-label classification (
output_mode => 'multi') to return all matching categories -
Category descriptions using 2D arrays to provide explicit context for each label
-
Few-shot prompting by supplying inline examples
-
Flexible execution modes for individual rows (
TEXT), arrays (TEXT[]), and cursors (REFCURSOR)
Here’s a quick example showing how you can classify customer feedback directly within a SQL query:
Source Credit: https://cloud.google.com/blog/products/databases/ai-classify-and-ai-extract-functions-in-alloydb-ai/
