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:
- code_block
- ARRAY(SELECT review FROM product_reviews ORDER BY id),rn categories => ARRAY[rn [‘positive’, ‘Good sentiment’],rn [‘neutral’, ‘Neither good nor bad sentiment’],rn [‘negative’, ‘Bad sentiment’]rn ],rn batch_size => 5rn )rn) AS sentiment;rnrn– Expected Output:rn– sentimentrn– ———rn– positivern– negativern– negative”), (‘language’, ”), (‘caption’, )])]>
Extracting structured JSON with ai.extract
The ai.extract function transforms unstructured text into structured JSON data matching your target schema — specified as a JSON schema or a text list of fields. It handles system instructions internally, saving you from writing prompt templates or response parsers.
Key capabilities include:
-
Schema flexibility supporting standard JSON schemas or plain text field lists
-
Automatic transformation of unstructured text into machine-readable JSON
-
Overloaded function signatures for single rows, arrays, and cursor batching
Here’s an example extracting clinical fields from free-form medical notes:
- code_block
- ‘Patient Jane Smith, 52 years old, visited the clinic on Oct 25. She is currently experiencing a mild fever, persistent cough, and severe body aches. The doctor prescribed Ibuprofen and Amoxicillin.’,rn output_schema => ‘{rn “patient_name”: {“type”: “string”},rn “age”: {“type”: “integer”},rn “symptoms”: {rn “type”: “array”,rn “items”: {“type”: “string”}rn },rn “prescribed_medications”: {rn “type”: “array”,rn “items”: {“type”: “string”}rn }rn }’rn);rnrn— Expected output rn– {rn– “patient_name”: “Jane Smith”,rn– “age”: 52,rn– “symptoms”: [rn– “mild fever”,rn– “persistent cough”,rn– “severe body aches”rn– ],rn– “prescribed_medications”: [rn– “Ibuprofen”,rn– “Amoxicillin”rn– ]rn– }’), (‘language’, ”), (‘caption’, )])]>
Speeding up queries with smart batching
Running AI evaluations row-by-row can slow down query execution. AlloyDB addresses this with smart batching for AI functions. When you use ai.classify or ai.extract in SQL WHERE clause expressions, the database engine automatically groups inputs into optimized micro-batches before sending them to Gemini Enterprise Agent Platform models.
Smart batching delivers significant performance and efficiency improvements:
-
Faster inference for query execution compared to row-at-a-time evaluations
-
Reduces total model token spend by optimizing input prompt overhead
Real-world industry applications
Native classification and extraction functions support operational workflows across key industry sectors:
-
Healthcare: Securely convert free-text clinical notes into structured records containing patient names, symptoms, and prescribed medications, keeping patient data safely within the confines of the database.
-
Financial services: Extract vendor details, invoice dates, and line-item totals from scanned document text.
-
Legal technology: Pull governing law, liability caps, and termination notice periods from contract documents.
-
Real estate: Extract property types, bedroom counts, and price ranges from descriptive listing text.
-
Knowledge graphs: Extract entities and relationships from documents to populate PostgreSQL Property Graph (PGQ) structures.
Next steps and getting started
We are continuing to expand in-database AI capabilities in AlloyDB, with upcoming enhancements planned for advanced cost optimization, line-level provenance tracking, and query pushdowns beyond WHERE clauses.
To start using ai.classify and ai.extract in your database:
-
Review the AlloyDB AI Query Engine documentation.
-
Explore the AlloyDB release notes.