Blog
AdvancedReferenceSQL Server Performance Internals

tempdb - the shared scratchpad under contention

Everything that lands in tempdb - temp tables, work tables, version store - why PAGELATCH fires on the PFS/GAM/SGAM allocation pages, the file-count rule that spreads the load, and what SQL Server 2019+ and 2025 added to kill the contention.

Applies to
SQL Server2016-2025 (13.x-17.x)memory-optimized tempdb metadata is 2019+; ADR in tempdb is 2025
matureUpdated Reviewed
advancedAssumes production experience with the basics.

What you will be able to do

  • List the three consumers of tempdb and say which one is behind a given tempdb symptom.
  • Read a PAGELATCH_UP wait resource like 2:1:1 and name the allocation page it is fighting over.
  • Apply the file-count guidance correctly - per logical processor up to eight, in groups of four beyond - with equal sizes.
  • Explain what memory-optimized tempdb metadata and ADR in tempdb change in 2019 and 2025.

Concepts covered

tempdb is one database shared by every session on the instance, doing three very different jobs - and that is why it is the most common hidden bottleneck. It is minimally logged and rebuilt empty at every startup, so it is fast by nature and fragile by design: one shared scratchpad with one set of allocation structures that everyone fights over.

The analogy is one communal workbench in a busy workshop, with a single sign-in sheet at the door. Every worker uses it for scratch jobs, offcuts, and photocopies of drawings they are annotating - three different kinds of clutter sharing one surface. Because the bench is wiped clean each morning and nothing on it is precious, the work itself is fast; the queue forms at the sign-in sheet, where everyone must write their name before touching anything. Adding identical benches, each with its own sheet, spreads the queue - which is what adding equal data files does to the allocation pages.

The three consumers - know which one is yours

Consumer What lands there The symptom it produces
User objects #temp tables, table variables, cursors, TVF results Allocation contention when create/drop churn is high
Internal work tables Spools, sorts, hash-match work files, index SORT_IN_TEMPDB Space and I/O pressure; spills from under-granted memory
Version store Row versions for RCSI/SI, online index ops, MARS, AFTER triggers Version-store growth pinned by a long reader; the store fills while nothing looks “busy”

The third one is the silent killer: enabling RCSI or SNAPSHOT moves every write’s row version into tempdb, and one long-running reader pins all versions generated since it started, so the store grows without bound while the version-cleanup thread cannot purge. tempdb filling with no obvious query running is the signature.

PAGELATCH on the allocation pages - the classic convoy

tempdb allocation is tracked on special pages: PFS (free space, one per ~8,088 pages), GAM and SGAM (extent allocation, each covering ~4 GB). When many sessions create and drop objects at once - temp-table churn is the usual driver - they all serialise on latches for these few pages.

The diagnosis is specific, and it is in the wait resource itself:

  • sys.dm_os_waiting_tasks shows PAGELATCH_UP waits on a tempdb resource like 2:1:1 (file:page) - page IDs divisible by 8,088 are PFS pages; 2:1:3 is an early SGAM.
  • The shape is a throughput ceiling with idle CPU - sessions queue on a latch while cores wait.

From a PAGELATCH convoy to the fix

  1. Symptom

    PAGELATCH_UP on 2:x:y resources; throughput plateaus

    CPU is not the bottleneck - the latch is; more cores would queue faster

  2. Confirm the page

    Decode the resource: 2:1:1 PFS, 2:1:3 SGAM

    Page % 8088 = 0 means a PFS page - you now know the structure being fought

  3. Add equal data files

    One per logical CPU up to eight; groups of four beyond

    Each file carries its own allocation pages - round-robin spreads the fight

  4. 2019+ lever

    Memory-optimized tempdb metadata; concurrent PFS updates

    Moves the contested metadata to latch-free in-memory structures

More data files means more PFS/GAM/SGAM pages - the same contention spread over more structures instead of one.

The file-count rule, stated exactly

Microsoft’s guidance, not folklore:

  • 8 or fewer logical processors → that many tempdb data files.
  • More than 8 → start with 8 files.
  • Contention persists → add files in groups of four, up to the logical-processor count, or change the workload.

All data files must be the same size and growth settings - round-robin allocation only balances if the files are equal. And the pre-2016 trace flags (1118/1117) are obsolete: from 2016, uniform extents and all-files-autogrow are the default for tempdb.

What changed by version

Version Change to tempdb contention
2016 Uniform extents by default (killed mixed-extent/SGAM contention); all files autogrow together; setup recommends multiple files
2019 Memory-optimized tempdb metadata - allocation metadata moved to latch-free structures; concurrent PFS updates
2025 ADR in tempdb - a separate persistent version store (PVS) alongside the traditional one; sized separately, needs disk headroom

What does not fix it

  • More storage for a PAGELATCH problem - the fight is in memory, not on disk.
  • One giant file instead of several - one file means one set of allocation pages; the convoy stays.
  • Shrinking tempdb routinely - it is rebuilt at startup; a correct file count and size is the fix, not a maintenance plan that fights the growth.

The failure modes in the field are consistent: PAGELATCH_UP on allocation pages means file count; version-store growth means a long reader pinning versions; and work-table pressure means a query spilling - which is a memory-grant problem wearing a tempdb mask.

Guided practice

A beginner exercise, worked on a scratch instance. The scenario: read tempdb’s contention like the lesson does - decode a wait resource, apply the file-count rule, and identify the consumer behind a symptom. Work each step and check yourself before moving on.

  1. Count your tempdb data files. List tempdb’s files (the database’s Files page in SSMS, or sys.master_files filtered on tempdb) and note their sizes. Check: you can state the count and whether the sizes are equal. Equal size and growth settings are a hard requirement - round-robin allocation only balances if the files are equal, so unequal files are a finding, not a detail.
  2. Apply the file-count rule to this instance. Find the logical-processor count and write down the file count the guidance prescribes. Check: eight or fewer logical processors means that many files; more than eight means start with eight and, if contention persists, add in groups of four up to the processor count. If you answered “as many as cores” for a large box, reread the rule - it caps the starting point at eight for a reason.
  3. Generate allocation churn. Run a tight loop that creates and drops #temp tables - ideally from two sessions at once - while a third window polls sys.dm_os_waiting_tasks for PAGELATCH waits on tempdb resources (2:x:y). Check: on a contended system you see PAGELATCH_UP waits whose resource reads like 2:1:1. If you saw none, your box may simply have enough files or too little concurrency - the decode in step 4 is still the skill being practised; a quiet tempdb is a valid result.
  4. Decode the resource. Take a 2:x:y wait resource and say which structure is being fought over. Check: database 2 is tempdb; page 1 in a file is the first PFS page (page IDs divisible by 8,088 are PFS), page 3 is an early SGAM. If you read 2:1:1 as “file 1, page 1” and stopped, revisit the allocation-pages section - naming the page is what turns the wait into a diagnosis.
  5. Match a symptom to a consumer. For each of these, name the tempdb consumer: (a) a PAGELATCH_UP convoy under temp-table churn, (b) tempdb filling while nothing looks busy under RCSI, (c) a query spilling sorts to disk. Check: (a) user objects creating allocation contention, (b) the version store pinned by one long-running reader that blocks cleanup, (c) internal work tables - a memory-grant problem wearing a tempdb mask. If (b) surprised you, reread the consumers table: one long reader pins every version generated since it started.

Check your understanding

Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.

  1. What are tempdb’s three consumers? User objects (#temp tables, table variables, cursors), internal work tables (spools, sorts, hash work files, SORT_IN_TEMPDB), and the version store (RCSI/SI row versions, online index ops, MARS, triggers). The consumers table is the map for every tempdb symptom - reread it if any was missing.
  2. A wait shows PAGELATCH_UP on resource 2:1:1. What is being fought over? Database 2 is tempdb, and page 1 of a file is a PFS page - an allocation-page latch fight, driven usually by temp-table create/drop churn. The shape is a throughput ceiling with idle CPU: sessions queue on a latch while cores wait. If you read it as an I/O problem, revisit the convoy section - PAGELATCH is in-memory, never disk.
  3. State the tempdb file-count rule exactly. Eight or fewer logical processors: that many data files. More than eight: start with eight, and if contention persists add files in groups of four up to the processor count - all files the same size and growth settings, because round-robin only balances equals. If your answer included “one big file”, reread “what does not fix it”.
  4. tempdb fills steadily while nothing looks busy, on a database using RCSI. What is the mechanism? The version store: every write’s row version lands in tempdb, and one long-running reader pins all versions generated since it started, so the cleanup thread cannot purge and the store grows while the instance looks idle. If you suspected allocation contention, the symptom shape is different - growth, not a latch convoy.
  5. What did SQL Server 2019 and 2025 each change for tempdb contention? 2019 added memory-optimized tempdb metadata - the contested allocation metadata moved to latch-free in-memory structures - plus concurrent PFS updates. 2025 brought ADR to tempdb: a separate persistent version store alongside the traditional one, sized separately and needing disk headroom. If you cannot place a fix on a version, revisit the version table - on 2016 and earlier the file-count rule is the whole toolbox.

Where SQL Server sits

How this component relates to the rest of the stack. Items without a link are not covered here yet.

Deployed with

Commonly run alongside

  • Windows Failover Clustering

Alternatives

Solves a similar problem differently