Overview

SQL Server is a single-process, multi-threaded engine (sqlservr). Unlike PostgreSQL’s process-per-connection model (pg-architecture), SQL Server runs its own scheduler layer, SQLOS, which gives each logical CPU a scheduler and cooperatively multitasks worker threads on it. Everything in this section is verified on SQL Server 2022 Developer (16.0.4325) in a Linux container unless marked otherwise.

Why it matters

Waits, memory pressure, parallelism and “the server is slow” diagnoses (sqlserver-wait-stats) are all expressed in SQLOS terms: schedulers, workers, tasks, waits.

Core concepts

flowchart TB
  C[Client TDS request] --> P[Protocol layer]
  P --> R[Relational engine: parser, optimizer, query executor]
  R --> S[Storage engine: access methods, buffer manager, transaction manager]
  S --> D[(Data files .mdf/.ndf)]
  S --> L[(Transaction log .ldf)]
  SQLOS[SQLOS: schedulers, memory manager, I/O, locks] --- R
  SQLOS --- S
  • Relational engine parses, binds and optimises (query-planning) and executes the plan. The plan cache stores compiled plans.
  • Storage engine reads and writes pages through the buffer pool (data cache, 8 KB pages) and manages locking and transactions.
  • Transaction log: write-ahead logging; log records are hardened to disk before COMMIT returns (transactions-acid). The log is per database and sequential.
  • Databases: system databases master (instance metadata, logins), model (template for new databases), msdb (Agent jobs, backup history), tempdb (scratch space, sqlserver-storage-tempdb) and resource (hidden system objects). Every database has at least one data file and one log file.
  • Schedulers and workers: one scheduler per logical CPU (16 here). A request becomes a task run by a worker; when it waits for a resource it yields and a wait type is recorded.
  • Memory: most memory is the buffer pool plus plan cache, memory grants for sorts/hashes and the lock manager. The default max server memory (MB) is 2147483647, i.e. effectively unlimited, so you must set it.
  • Editions: Enterprise, Standard, Developer (full-featured, non-production), Express (free, limited, no Agent), plus Azure SQL variants. Features differ by edition; check before designing around one.

Worked example

On the test instance (16 logical CPUs, container limited to 3 GB):

SELECT cpu_count, scheduler_count, physical_memory_kb/1024 AS phys_mb,
       committed_target_kb/1024 AS target_mb
FROM sys.dm_os_sys_info;
-- 16 | 16 | 3072 | 2324
 
SELECT name, value_in_use FROM sys.configurations
WHERE name IN ('max server memory (MB)','max degree of parallelism','cost threshold for parallelism');
-- cost threshold for parallelism 5 ; max degree of parallelism 0 ; max server memory 2147483647

The defaults MAXDOP = 0 (use all CPUs) and cost threshold 5 are old; many installations set cost threshold to 25-50 and MAXDOP to the number of cores per NUMA node (up to 8) after measuring.

Pitfalls and production notes

  • Set max server memory to leave room for the OS and other processes (a common rule is to reserve 10-20% or at least 4 GB on small hosts), and enable Lock Pages in Memory where appropriate.
  • Do not put data, log, backup and tempdb on the same volume; log latency directly limits commit throughput.
  • Instances on virtualisation or containers must respect CPU and memory limits; the engine detects container limits but verify sys.dm_os_sys_info.
  • Keep the engine patched: cumulative updates ship regularly (the test image is CU28).

Practice questions

  1. What is the difference between a scheduler, a worker and a task?
  2. Why does SQL Server not need a connection pooler for the same reason PostgreSQL does?
  3. Which system database holds Agent jobs and backup history?

References

sqlserver-storage-tempdb · sqlserver-wait-stats · sqlserver-execution-plans · pg-architecture