Snowflake Postgres + Cortex AI
What is Snowflake Cortex?
Snowflake Cortex runs AI models inside Snowflake. You call them as SQL functions, so you don't have to send data to an outside LLM provider or run your own GPUs.
For a Postgres app, the split is simple. Postgres stays your operational database, and Cortex does the AI work when you ask for it.
How It Works with Postgres
Your application keeps two connections:
- Postgres, where your data lives. Query it with regular SQL from any Postgres driver.
- Snowflake, where Cortex runs. Call Cortex functions with the Snowflake Python connector, Snowpark, or any other Snowflake client.
Your source data stays in Postgres. You send Cortex just the text you want processed (to generate text, produce embeddings, translate, and so on), then use the results in your app or write them back to Postgres.
If you'd rather move data in bulk than call Cortex row by row, pg_lake can exchange files and Iceberg tables between Snowflake Postgres and Snowflake.
Cortex AI Functions
| Function | What It Does |
|---|---|
AI_COMPLETE | Generate text from a prompt using a hosted LLM |
AI_EMBED | Generate vector embeddings for semantic search |
AI_EXTRACT | Extract structured information from text |
AI_CLASSIFY | Classify text into categories you define |
AI_SENTIMENT | Score the sentiment of text |
AI_SUMMARIZE_AGG | Summarize across multiple rows |
AI_TRANSLATE | Translate text between languages |
AI_COMPLETE is the newer name for SNOWFLAKE.CORTEX.COMPLETE. The older function still shows up in a lot of example code, but use AI_COMPLETE for new work.
All of these are SQL expressions. Your app sends a query to Snowflake, Cortex runs the model, and you get the result back. There's no model hosting on your side.
Text-to-SQL with AI_COMPLETE
You can use AI_COMPLETE to put a natural-language query interface on top of your Postgres data. The flow looks like this:
- Your app sends the user's question plus your Postgres schema (table and column names, maybe a few sample rows) to
AI_COMPLETE. - The model writes a SQL query.
- Your app runs that SQL against Postgres.
- You render the results as a table or chart.
Run the generated SQL as a read-only Postgres role with a statement_timeout set. LLMs write plausible SQL, not always correct or cheap SQL, and you don't want a hallucinated DELETE anywhere near production.
Generating Embeddings with Cortex
Cortex can generate embeddings that you store in Postgres with pgvector, so you don't have to host an embedding model.
- Send text to
AI_EMBEDover your Snowflake connection. - Cortex returns a vector.
- Store it in a pgvector
vectorcolumn in Postgres. - Query with pgvector's distance operators for similarity search.
Available Embedding Models
| Model | Dimensions | Best For |
|---|---|---|
snowflake-arctic-embed-l-v2.0 | 1024 | High accuracy, general purpose |
snowflake-arctic-embed-l-v2.0-8k | 1024 | Longer documents (8K token context) |
snowflake-arctic-embed-m-v1.5 | 768 | Good balance of speed and accuracy |
voyage-multilingual-2 | 1024 | Multilingual content |
Model availability can vary by region, so check the Cortex docs for your account. Size the pgvector column to match the model, for example vector(1024) for the Arctic L models.
Once the embeddings are in Postgres, similarity search runs at application speed, and you can combine vector retrieval with regular relational filters in one query. AI in Postgres covers pgvector indexing, hybrid search, and filtering in detail.
Building a RAG Application
Retrieval-Augmented Generation (RAG) grounds LLM answers in your own data. With Cortex and Postgres it looks like this:
- Call
AI_EMBEDon your documents (usually in chunks) and store the vectors in Postgres with pgvector. - When a user asks a question, call
AI_EMBEDon the question with the same model. - Search Postgres with pgvector to find the most similar chunks.
- Pass those chunks and the question to
AI_COMPLETEto get a grounded answer.
Your application orchestrates the steps. Postgres holds the data and embeddings, and Cortex provides the models on demand.
Cortex Search
If you want managed semantic search instead of building your own embedding pipeline, Cortex Search handles chunking, embedding, indexing, and retrieval for you.
It does hybrid retrieval, combining vector and keyword search. It generates the embeddings itself, and it keeps the index up to date as the underlying data changes.
Cortex Search works over data in Snowflake, so you first need your Postgres tables there. Use pg_lake to sync them as Iceberg tables, then create a Cortex Search service on top for hybrid search over your operational data.
Conclusion
Cortex gives your Postgres applications LLMs and embedding models without any AI infrastructure to run. Your data stays in Postgres, Cortex does the model work, and your application ties them together.
Related Resources
Reference for Snowflake Cortex AI functions, including AI_EMBED, AI_COMPLETE, AI_EXTRACT, and more.
Overview of Snowflake Cortex AI capabilities.
Open-source vector similarity search extension for Postgres.
Move Postgres data to Snowflake for analytics and AI processing.
This content is provided as is, and is not maintained on an ongoing basis. It may be out of date with current Snowflake instances