Extended Events and actual plans: the filter that catches nothing and still slows you down
A small Extended Events session is supposed to collect only the actual plans of slow queries and filter out everything else. That sounds like the most careful approach. In our lab, exactly such a session made every single query on the server several times more expensive. It did not capture a single event.
In short
- A filter on duration doesn't protect you. SQL Server only knows whether a query ran longer than a minute once it has finished. The work for the plan is done before that, for every query on the server.
- "Lightweight" doesn't help here. In the lab, the XE session with the lightweight event
query_post_execution_plan_profileslowed things down almost as much as the one with the classicquery_post_execution_showplan. - This still holds today. We observed it from SQL Server 2017 through 2025.
- Checking is easy. Query 1 below shows which sessions on your server contain such events, whether they are running and whether they start with the server.
- There are lighter ways to get the actual plan: the last plan from the cache, targeted marking of individual queries, the in-flight plan of a session and the history in Query Store.
From practice: the well-meant capture
An application is slow from time to time, but never when someone is looking. So the server is supposed to record the plans of the outliers by itself. Extended Events offers events that deliver the actual execution plan. So that not every query ends up in the target, the event gets a filter, such as "only queries over 60 seconds".
The session is started, often with STARTUP_STATE = ON so that it keeps running after a restart. A few days later, the server is slower overall. By then the session may have collected a handful of plans, or none at all, and so nobody suspects it.
What we observed
We ran a small workload of typical OLTP queries on a server whose cores were fully loaded, once without and once with an XE session. The session contained a single event with the filter duration > 60000000 (microseconds, so 60 seconds). No query in the test came anywhere close to that. At the end, the session's ring buffer held no events.
- With
query_post_execution_showplan, every single execution became several times more expensive, measured by the CPU time SQL Server spent on it. - With
query_post_execution_plan_profile, the event based on lightweight profiling, it was somewhat less, but of the same order of magnitude. - The larger the plan, that is, the more operators a query has, the larger the surcharge.
- With real client connections instead of a loop on the server, the surcharge per execution stayed the same. Throughput dropped less, because network and client account for part of the time, but still clearly. A server that handles many short queries feels this the most.
- On all versions: SQL Server 2017, 2019, 2022 and 2025 showed the same picture. In the newer versions the surcharge was somewhat smaller, but stayed of the same order of magnitude.
For comparison: the database option LAST_QUERY_PLAN_STATS, which keeps the last actual plan in the cache, did not noticeably affect the same workload. How it works and when you should still be careful is covered in the article on the last actual plan.
Why the filter doesn't protect you
A predicate in Extended Events decides whether an event is stored. It does not decide whether SQL Server prepares the data for it. For the actual plan, that data includes the counters per operator. These counters have to run from the start of a query, because SQL Server doesn't know in advance which query will later meet the filter.
Microsoft states this explicitly for the classic event: with query_post_execution_showplan, the standard profiling infrastructure is added "to every query that is running on the server" (Query hints, note on QUERY_PLAN_PROFILE). Jonathan Kehayias showed back in 2013 that this event is costly even with a filter that never matches (SQLPerformance.com, March 25, 2013, SQL Server 2012 at the time).
For the lightweight event, the explanation is our assessment. SQL Server has carried the base cost of lightweight profiling since 2019 anyway. That the session still slows things down this much suggests that plan information is additionally prepared for every execution before the filter applies. We did not find a Microsoft source that describes this explicitly.
Two names, one trap
The names don't make it easier:
query_post_execution_showplanuses standard profiling. This is the "real" actual plan with times and I/O per operator.query_post_execution_plan_profileuses lightweight profiling (SQL Server 2017 CU14 and later). It delivers less, mainly row counts per operator.query_plan_profile(2016 SP2 CU3 and 2017 CU11 and later) fires only for queries that carry the hintUSE HINT ('QUERY_PLAN_PROFILE'). According to Microsoft, it doesn't burden the rest of the workload. That matched what we saw in the lab. The marked query itself, however, pays the same surcharge as above on every execution. From SQL Server 2022 on, the hint can be attached to a single query as a Query Store hint, without changing the code.
"Lightweight" describes the kind of counting, not the cost of a session that affects every query on the server. The full family tree from standard profiling through lightweight v1 to v3 and on to LAST_QUERY_PLAN_STATS is in our overview of the profiling family.
Check: which sessions capture plans?
/* PSG · XE plan trap · Query 1: Which Extended Events sessions capture execution plans? Shows every XE session defined on the server that contains one of the plan events: whether it is running right now, whether it starts with the server and which filter is set on the event. The query only reads metadata and changes nothing. It does not stop any session. Caution: a running session with query_post_execution_showplan or query_post_execution_plan_profile can slow down the whole server, even if its filter (for example on duration) never matches. Before you stop a session, find out who set it up and why. Applies to SQL Server (box product). In Azure SQL Database, sessions are database-scoped (sys.database_event_sessions); not tested there. Permissions: VIEW SERVER STATE; from SQL Server 2022 on, VIEW SERVER PERFORMANCE STATE is enough (tested on SQL Server 2017 through 2025). OPTION (MAXDOP 1): the diagnostic query should not tie up parallel workers. */SELECT s.name AS session_name, e.name AS event_name, CASE WHEN r.name IS NULL THEN N'stopped' ELSE N'running' END AS state, CASE s.startup_state WHEN 1 THEN N'yes' ELSE N'no' END AS starts_with_server, e.predicate AS filterFROM sys.server_event_sessions AS sJOIN sys.server_event_session_events AS e ON e.event_session_id = s.event_session_idLEFT JOIN sys.dm_xe_sessions AS r ON r.name = s.nameWHERE e.name IN (N'query_post_execution_showplan', N'query_post_execution_plan_profile', N'query_pre_execution_showplan', N'query_post_compilation_showplan', N'query_thread_profile', N'query_plan_profile')ORDER BY CASE WHEN r.name IS NULL THEN 1 ELSE 0 END, s.nameOPTION (MAXDOP 1);Sample output from our lab with two test sessions, one running, one stopped:
| session_name | event_name | state | starts_with_server | filter |
|---|---|---|---|---|
| psg_test_running | query_post_execution_plan_profile | running | yes | ([duration]>(60000000)) |
| psg_test_stopped | query_post_execution_showplan | stopped | no | ([duration]>(60000000)) |
The first row is exactly the case from this article: the session is running, starts with the server and has a filter that almost never matches. The second is harmless as long as nobody starts it.
How to read the result:
runningwithquery_post_execution_showplanorquery_post_execution_plan_profile: this is the trap from this article, no matter which filter is set on it.query_pre_execution_showplanandquery_post_compilation_showplandeliver estimated plans on every execution or compilation. They don't belong in permanent operation either. We did not measure these two.query_plan_profileis uncritical as long as only individual queries carry the hint.- Stopped sessions with
starts_with_server = yesstart running again after the next restart.
The same applies to old SQL Trace or Profiler captures with the event "Showplan XML Statistics Profile". They use standard profiling as well.
What to use instead
- The last actual plan: from SQL Server 2019 on,
LAST_QUERY_PLAN_STATSkeeps it in the plan cache, retrievable viasys.dm_exec_query_plan_stats. It shows the rows per operator of the last execution, no times per operator, and it is gone as soon as the plan leaves the cache. - Individual queries, on purpose: the hint
QUERY_PLAN_PROFILEtogether with the eventquery_plan_profile. From SQL Server 2022 on, the hint can be attached to a single query as a Query Store hint (sys.sp_query_store_set_hints), without changing the code and without a plan guide. Tested in the lab on SQL Server 2022 and 2025. The marked query pays the full surcharge on every execution, so use it only for a limited time. - A running query:
sys.dm_exec_query_statistics_xmlreturns the plan with the counters so far, from another session. From 2019 on, this works without any preparation. - What happened yesterday at 2 p.m.: the plan cache forgets, Query Store doesn't. It stores plans and runtime statistics over time, though without rows per operator. Our product PSG QX analyzes Query Store: plan changes, outliers and what led up to them, without any additional capture on the server.
When to ask someone first
Before a session with plan events runs on a production server, even "just briefly" and "just filtered", a second look is worthwhile. This applies especially if the server already works at its CPU limit, runs many short queries or the session is meant to start with the server. Usually the same question can be answered with one of the lighter ways above.
How we tested
SQL Server 2017, 2019, 2022 and 2025 in Linux containers with eight cores that were fully loaded during the measurement. We did not measure separately on Windows. Synthetic data without any customer connection. Two query shapes: an OLTP join across four tables and a query with a very large number of operators. Five repetitions per variant, in alternating order. In addition, a cross-check on SQL Server 2017, 2022 and 2025 in which the queries arrived as individual batches over their own connections instead of from a loop on the server. We measured the CPU time per execution that SQL Server reports itself, and the CPU of the whole container.
Sources
- Microsoft Learn: Query profiling infrastructure, Query hints (
QUERY_PLAN_PROFILE), sys.dm_exec_query_plan_stats - Jonathan Kehayias, SQLPerformance.com, March 25, 2013: Impact of the query_post_execution_showplan Extended Event in SQL Server 2012