Google Cloud announced that the AlloyDB AI extension now includes two preview SQL functions, ai.classify and ai.extract, available from version 1.6.1 of the google_ml_integration extension on PostgreSQL 18. By embedding Gemini model calls directly in the query engine, teams can classify text or pull structured data without leaving the database.
New SQL Functions in AlloyDB AI
ai.classify accepts a set of input strings and a list of categories, returning either a single best label or multiple matching labels. Categories can be simple labels or two‑dimensional arrays that include descriptive text, which helps the model understand the intended meaning. The function supports three execution modes: a single TEXT value, an array of TEXT[], or a REFCURSOR for streaming results. Smart batching—formerly called AI Function Acceleration—lets the engine group calls to stay within production‑grade performance limits.
SET google_ml_integration.enable_preview_ai_functions = TRUE;
CREATE TABLE product_reviews (id INT, review TEXT);
INSERT INTO product_reviews VALUES
(1, 'Fantastic, would buy again!'),
(2, 'Broken on arrival.'),
(3, 'Mediocre, not worth the price.');
SELECT unnest(
ai.classify(
inputs => ARRAY(SELECT review FROM product_reviews ORDER BY id),
categories => ARRAY[
['positive', 'Good sentiment'],
['neutral', 'Neither good nor bad sentiment'],
['negative', 'Bad sentiment']
],
batch_size => 5
)
) AS sentiment;
The ai.extract function works similarly but returns JSON that conforms to a supplied schema or a simple list of field names. It turns free‑form text into a structured object that downstream queries can consume directly.
Operational Impact
- Latency reduction: Model inference happens inside the database engine, eliminating the round‑trip to an external endpoint.
- Simplified code paths: No need for custom prompt wrappers, retry loops, or separate data‑export steps.
- Token cost control: Smart batching groups inputs, which can lower the number of model tokens consumed per query.
- Resource planning: Enabling the preview flag and running AI functions will consume CPU and memory on the AlloyDB node; capacity planning should include expected AI workload.
- Version lock: The functions require
google_ml_integrationversion 1.6.1 or later; upgrades must be coordinated with database maintenance windows.
Security and Compliance Considerations
Because the text never leaves the database, the attack surface associated with moving sensitive data across network boundaries is reduced. However, practitioners should still treat the model call as a data‑processing operation that may be subject to internal data‑handling policies. Enabling the preview flag introduces a new code path, so monitoring for unexpected resource spikes or error patterns is advisable. Auditing should capture which queries invoke ai.classify or ai.extract to maintain visibility into AI‑driven data transformations.
Related CloudNinjas coverage: Google Cloud.
What This Means For Practitioners
Teams should start by enabling the preview flag in a non‑production environment, run a few representative queries, and measure latency and resource usage. Validate classification accuracy against known labels and verify that the JSON output from ai.extract matches downstream schema expectations. Incorporate these functions into CI pipelines to catch regressions early, and update operational dashboards to include AI function metrics. Finally, keep an eye on the GA release schedule; once the functions graduate from preview, they can be adopted more broadly with confidence.


