The Agentic Drift Liability: Why LLM-Generated T-SQL in SQL Server 2025 is Creating Undetectable Deadlocks
Native LLM integration in SQL Server 2025 introduces 'Agentic Drift,' where AI-generated query patterns evolve into resource-cannibalizing anti-patterns that bypass traditional optimization.
The Allure of the Autonomous T-SQL Engineer
With the release of SQL Server 2025, Microsoft has effectively blurred the line between the application tier and the data tier by allowing direct LLM calls from within T-SQL. As I noted in my previous analysis of sql-server-2025-can-call-an-llm-directly-from-t-sql-here-s-what-that-actually-me, this capability simplifies the development of RAG (Retrieval-Augmented Generation) patterns. However, there is a dangerous side effect surfacing in production environments: Agentic Drift.
Agentic Drift occurs when an LLM, tasked with generating or modifying T-SQL queries dynamically based on natural language inputs, begins to produce syntax that is technically valid but architecturally disastrous. Unlike a human developer who follows a consistent (even if flawed) coding style, an AI agent's output is non-deterministic. Over time, the query patterns evolve—or drift—away from the execution plans the DBA originally tuned, leading to a new breed of undetectable deadlocks and resource exhaustion.
Why LLM Queries Bypass Traditional Tuning
Standard database optimization relies on predictability. We use sql-server-index-tuning-query-performance to handle known workloads. But when an AI agent constructs a query, it often ignores the relational engine's nuances.
In recent case-studies, we’ve observed AI agents generating queries that:
1. Over-rely on Non-SARGable predicates: The LLM might wrap a column in a function to match a vague user intent, rendering indexes useless.
2. Implicit Conversions: AI agents often struggle with precise data typing, leading to CPU spikes as the engine converts types on millions of rows.
3. Indeterministic Join Orders: Because the prompt context changes slightly with every user interaction, the generated T-SQL structure shifts, preventing the Optimizer from relying on plan stability.
This drift creates a scenario where sql-server-deadlock-troubleshooting becomes a game of whack-a-mole. You aren't fixing a bug in code; you are fighting a probabilistic engine that is constantly reinventing the wheel.
The Deadlock Trap: LLMs and Optimized Locking
SQL Server 2025 introduced sql-server-2025-optimized-locking, which significantly reduces lock memory usage. However, LLM-generated T-SQL often violates the very principles this feature relies on.
For example, an agent might decide to wrap a simple SELECT in a high-isolation level transaction because it interpreted a user's request for "accurate data" too literally. These unplanned SERIALIZABLE hints, generated on the fly, lead to lock escalation that the DBA cannot foresee. Because these queries are transient and frequently changing, they often don't stay in the cache long enough for traditional sql-server-performance-monitoring-dmv-extended-events to flag them as a persistent problem until the system is already at a standstill.
Defensive Strategy 1: Resource Governor Caps
If you are going to allow LLMs to talk to your database, you must treat them as untrusted users. This is where sql-server-resource-governor-fundamentals becomes mandatory rather than optional.
You cannot allow the workload group associated with your AI services to consume 100% of the CPU or memory. Agentic Drift often leads to "Cartesian Explosion" queries—queries where the AI fails to define a proper join predicate, resulting in a cross join that attempts to scan every row in two large tables. By capping the MAX_CPU_PERCENT and MAX_MEMORY_PERCENT for the AI service account, you ensure that a drifting agent doesn't take down the entire instance.
Defensive Strategy 2: Query Store Forcing and Hinting
To combat the non-deterministic nature of AI, you must leverage sql-server-2025-query-store. Enable Query Store in READ_WRITE mode and pay close attention to the new "Query Wait Statistics" and "High Variation" reports.
When you identify an AI-generated query that is efficient, use Query Store Plan Forcing to lock it down. However, this is only a partial fix because the LLM will inevitably change the query text slightly (e.g., adding an extra whitespace or changing an alias), which generates a new query_id. To counter this, implement Query Store Hints. By applying global hints to specific query patterns or schemas used by the AI, you can force the use of specific indexes or max degree of parallelism (MAXDOP) settings regardless of how the LLM phrases the T-SQL.
The Metadata Debt
There is also the issue of schema-drift-rag-to-sql-metadata. If your LLM doesn't have a perfect, real-time understanding of your schema, it will guess. Guessing leads to incorrect join logic, which in turn leads to massive tempdb usage. If you are already struggling with sql-server-tempdb-contention-diagnosis-fix, adding LLM-generated queries will be the tipping point.
Takeaway: Don't Trust the Agent
AI-driven T-SQL generation is a powerful tool for accelerating feature delivery, but from a DBA's perspective, it is a liability. The 'Agentic Drift' ensures that today's fast query is tomorrow's deadlock.
To survive this shift, you must move away from reactive tuning and toward proactive containment. Implement strict Resource Governor limits, use Extended Events to monitor for lock_deadlock events specifically from AI service accounts, and never allow an LLM to execute T-SQL under a high-privilege account. If you need help auditing your environment for AI readiness, consider a formal assessment or explore our services/database-performance-tuning to get ahead of the drift.
At Server Side Technology Solutions, we help firms navigate the transition to SQL Server 2025 without sacrificing stability. Book a consult today to discuss your AI-to-SQL architecture.
Related services
Dealing with this in production? Here's how we help.
Database Performance Tuning
Slow queries, execution plans, index strategy, and lock contention fixed at the root cause.
24/7 Remote DBA Support
Around-the-clock monitoring, proactive detection, and emergency incident response.
← All posts