Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From the Trenches: Understanding cursor pin S w...

From the Trenches: Understanding cursor pin S wait on X events

Avatar for Seán Scott

Seán Scott PRO

October 30, 2024

More Decks by Seán Scott

Other Decks in Technology

Transcript

  1. Database Reliability Engineering MAA ⁘ RAC ⁘ RMAN Data Guard

    ⁘ Sharding ⁘ Partitioning Information Lifecycle Management Exadata & Engineered Systems Database Modernization Upgrades ⁘ Patching ⁘ Migrations Cloud ⁘ Hybrid Automation DevOps ⁘ IaC ⁘ Containers ⁘ Terraform Vagrant ⁘ Ansible Observability AHF ⁘ TFA ⁘ CHA ⁘ CHM
  2. www.viscosityna.com @ViscosityNA Oracle on Docker Running Oracle Databases in Linux

    Containers Free sample chapter: https://oraclesean.com
  3. www.viscosityna.com @ViscosityNA • A session wants a cursor • It

    needs a shared pin (pin S) to read the cursor • Another session has an exclusive pin (X) on the cursor Cursor: Pin S Wait on X
  4. www.viscosityna.com @ViscosityNA • A cursor is parsed SQL • Parsed

    SQL is stored in the Library Cache • A pin is a Mutex • A mutex is a mechanism for serializing concurrent operations Cursor: Pin S Wait on X
  5. www.viscosityna.com @ViscosityNA • A session trying to parse SQL needs

    access to a cursor in the Library Cache • It must acquire a shared mutex to read the cursor • Another session has an exclusive mutex that locks the cursor Cursor: Pin S Wait on X
  6. www.viscosityna.com @ViscosityNA • Concurrency management mechanisms • Serialize access to

    resources • Guarantee consistency • Spinlocks: waiters "spin" on CPU • Latch: prone to false contention (one latch controls many objects) • Mutex: precise and faster; part of the object's memory structure Latches and Mutexes
  7. www.viscosityna.com @ViscosityNA • Latch = lock • Managed by the

    database • Fully instrumented through Oracle's wait event interface • (Relatively) easy to troubleshoot Latches
  8. www.viscosityna.com @ViscosityNA • A latch protects multiple, independent resources •

    One to many relationship is prone to false latch contention • Latch count, location, status are always known ($latch, Latches
  9. www.viscosityna.com @ViscosityNA • Parent latch: Located in the SGA at

    a known address Points to an array of child latches • Child latch: Located in the Shared Pool Latches
  10. www.viscosityna.com @ViscosityNA • Mutex = MUTual EXclusion • Lighter and

    more precise than latches • Often implemented directly under hardware = FAST • Dynamic—part of/internal to the object it protects • Not tracked/instrumented individually to reduce overhead Mutexes
  11. www.viscosityna.com @ViscosityNA • Implemented in the Oracle Database kernel •

    KKS (kernel compile shared cursors) • Manages child cursor concurrency in parent cursor heap 0. • KGL (kernel generic library cache) • Manages concurrency on the library cache hash table. Mutexes
  12. www.viscosityna.com @ViscosityNA • Unlike latches, mutexes are not instrumented comprehensively

    • Oracle collects statistics on mutex activity, not individual mutexes • Sleeping is a wait event • Spinning is not a wait—it is registered as CPU time • Mutex sleeps indicate a concurrency problem... ...but not necessarily a database concurrency problem Mutexes
  13. www.viscosityna.com @ViscosityNA • Mutex issues can arise from interfering/manipulating sequential

    processing • Non-database sources of mutex sleeps/concurrency: • Oversubscribed CPU • Bad PL/SQL, deadlocking, triggers • Altering OS process priority • Bugs Mutexes
  14. www.viscosityna.com @ViscosityNA • Lengthy/unexpected "spins" consume CPU • Processes queueing

    on CPU can lead to CPU death spirals • Back-off mechanisms are a safety valves • Latches sleep or get a semaphore wait • Pre-11.2.0.4, mutexes had no back-off mechanism Spinlocks and Backoff Controls
  15. www.viscosityna.com @ViscosityNA • A process acquiring a mutex gets a

    high priority • The high priority of the waiter can push the blocker off the CPU Mutex Backoff Behavior
  16. www.viscosityna.com @ViscosityNA • Oracle Database 11.2.0.4 added mutex backoff controls:

    • _mutex_wait_scheme: Controls mutex backoff behavior • _mutex_spin_count: How many times the process "spins" (255) • _mutex_wait_time: How long the process sleeps (10ms) Mutex Backoff Behavior
  17. www.viscosityna.com @ViscosityNA • Parsing SQL creates a cursor • Cursors

    are saved in the SGA's (shared) Library Cache • The Library Cache is organized as a hash tree • Hashing a SQL statement generates a hash value • That hash value is the cursor's address in the Library Cache What is a cursor?
  18. www.viscosityna.com @ViscosityNA • High CPU cost • Performs syntax, semantic,

    permission checks • Evaluates access paths, performs cost-based optimization • Creates an executable version of the cursor (think: compiled) Parsing: Hard Parse
  19. www.viscosityna.com @ViscosityNA • Hard parsing creates two objects in the

    Library Cache: • The parent cursor of static/de fi • A child cursor of dynamic metadata (e.g. variables/bind values) Parsing: Hard Parse
  20. www.viscosityna.com @ViscosityNA • Hard parsing is expensive • Identical SQL

    always gets the same hash • Queries with different variables are not identical! Cursor Sharing and Soft Parsing
  21. www.viscosityna.com @ViscosityNA • Bind variables allow identical SQL with different

    metadata • Bind variables are in the SQL text and produce identical SQL hashes • Saves space in the Library Cache • Reduces impact of hard parsing • Soft parsing evaluates bind values • Oracle reuses the existing execution plan if possible Cursor Sharing and Soft Parsing
  22. www.viscosityna.com @ViscosityNA • SQL w/different bind values may bene fi

    • The SQL is the same, but existing cursors can't be shared, so: • The SQL hash maps to an existing address in the Library Cache • The dynamic metadata creates a new child cursor under the parent Cursor Sharing
  23. www.viscosityna.com @ViscosityNA • A parent can have multiple child cursors

    with different execution plans • A parent cursor can be marked: • Bind-sensitive: Cursor can be re-parsed to improve the plan • Bind-aware: Child execution plans are based on bind values • Bind-aware cursors need something between a hard and soft parse • Optimizer reviews children for potential reuse based on bind values Cursor Sharing
  24. www.viscosityna.com @ViscosityNA • A semi-hard parse inspects bind values on

    every parse • Parsing can skip a (legitimately) compatible child for many reasons: • NLS settings • Different schemas • Cardinality feedback Child Cursor Misses • Bind mismatch • DDL • Statistics
  25. www.viscosityna.com @ViscosityNA • Changing cursors means manipulating the Library Cache

    • Library Cache changes are protected with Mutexes: • Provide granular protection for individual parent/child cursors • Fast, low-level mechanisms • Reduce false contention but cannot prevent it Library Cache Objects
  26. www.viscosityna.com @ViscosityNA • Soft parsing is expensive, too • Session

    cursor caching (if enabled) eliminates* soft parsing • Stores repeat/re-entrant session SQL in PGA w/pointer • Pointer to the child cursor in the Library Cache Session Cursor Cache * If the cursor is open; Oracle still con fi rms/validates the cursor
  27. www.viscosityna.com @ViscosityNA • A session hashed an existing SQL statement

    • It located a hash tree entry in the Library Cache • It wants to set a shared mutex on a cursor to guarantee consistency • The cursor is locked with an exclusive mutex Cursor: Pin S Wait on X
  28. www.viscosityna.com @ViscosityNA • Is the session with the exclusive mutex

    updating the cursor? • Why would an existing (already parsed) cursor get updated? Cursor: Pin S Wait on X
  29. www.viscosityna.com @ViscosityNA • Session A issues SQL. • Optimizer hashes

    the SQL text • Looks for a matching hash in session cursor cache • If found, go directly to Library Cache; do not parse Go! Do Not Parse; Go!
  30. www.viscosityna.com @ViscosityNA • The hash matches an existing entry in

    the Library Cache hash tree • This entry corresponds to a parent cursor—not an execution plan • The parent cursor must be bind-aware • Optimizer searches for, runs a compatible (cached) child cursor • Bind-aware cursors perform a semi-hard parse to evaluate binds • A match means the binds are compatible with an existing child Session Cache Miss/Library Cache Hit
  31. www.viscosityna.com @ViscosityNA • The SQL hash points to the parent

    cursor (heap 0) • Heap 0 points to a hash table of child cursors • The hash table is organized from newest to oldest • Assumes younger children are better matches • Each read requires a shared mutex—Pin S Finding Compatible Child Cursors
  32. www.viscosityna.com @ViscosityNA • Get shared mutex on the fi •

    Check the child for compatibility • If compatible, execute; if not, release shared mutex • Move to the next-youngest child cursor • Repeat until fi Child Cursor Read Details
  33. www.viscosityna.com @ViscosityNA • If no compatible child cursors are present,

    create a new child cursor • The process is called an Incomplete List Insertion • Adds a new entry at the end of the child cursor hash table • At this point, the entry is an incomplete child cursor (unparsed) • The new entry requires a hard parse No Compatible Children
  34. www.viscosityna.com @ViscosityNA • Implemented in the Oracle Database kernel •

    KKS (kernel compile shared cursors) • Manages child cursor concurrency in parent cursor heap 0. • KGL (kernel generic library cache) • Manages concurrency on the library cache hash table. Mutexes
  35. www.viscosityna.com @ViscosityNA • Incomplete list insertion calls kkshinis: kkshinins •

    Adding child cursors alters the parent cursor hash table • kkshinis sets an exclusive mutex (pin X) on the parent cursor ...and places an exclusive mutex on the incomplete child cursor Incomplete List Insertion
  36. www.viscosityna.com @ViscosityNA • Session B issues SQL, hashes, checks session

    cursor cache, etc. • Hash matches a parent cursor in the Library Cache • The parent cursor has an exclusive mutex. • Is Session B blocked? Genesis of the Pin S Wait on X
  37. www.viscosityna.com @ViscosityNA • Session B isn't blocked—it isn't trying to

    change the parent cursor! • Begins a top-down scan of the child hash table for a compatible cursor • If found, it uses the cursor • Eventually, Session B reaches the end of the hash table, encountering the incomplete child cursor Genesis of the Pin S Wait on X
  38. www.viscosityna.com @ViscosityNA Session B didn't find a compatible child, but

    there's still one cursor left—the one being parsed by Session A! Genesis of the Pin S Wait on X
  39. www.viscosityna.com @ViscosityNA Session B is trying to read a cursor.

    It tries to set a shared mutex (pin S). It waits on Session A to release the exclusive mutex (pin X). Cursor: Genesis of the Pin S Wait on X
  40. www.viscosityna.com @ViscosityNA • Session A performs a (time-consuming) hard parse,

    and when fi • kkshindel deletes the cursor from the incomplete list • kkshinins inserts (moves) the cursor to the top of the list • Inserting the new cursor shifts existing cursors down Incomplete List Removal kkshindel remove child from incomplete list bi=XXXXXXXXXXXX cld=YY flg=ZZ kkshhcins insert child into hash table bi=AAAAAAAAAAAA cld=BB flg=CC
  41. www.viscosityna.com @ViscosityNA • Reordering the child cursor hash table is

    expensive • More child cursors/versions = higher cost • Cached SQL that can't be recycled requires hard parses/new children • Invalidating cached SQL removes child cursors, drives hard parsing • Small SGA = small Library Cache = pruning child cursors • Frequently resizing SGA/memory structure instability Contributing Factors
  42. www.viscosityna.com @ViscosityNA Databases are rule-based systems. Everything happens for a

    reason. Your job: finding the reason! Finding Patterns in Randomness
  43. www.viscosityna.com @ViscosityNA • This event appears when known SQL can't

    fi • ...because bind values don't match • ...because CPU can't keep up with hard-parsing • ...because cursors are invalidated/moved out of cache • ...because of database parameter settings or bugs • ...because of concurrency issues inside or outside the database Finding Patterns in Randomness
  44. www.viscosityna.com @ViscosityNA • Occurs when incoming bind values aren't compatible

    w/known children • No matching cardinality selection • Incompatible binds created at client • Incompatible NLS settings Bind Mismatch
  45. www.viscosityna.com @ViscosityNA • Host CPU is over-provisioned • Login storms

    dominate CPU or fl • Logoff storms release resources, consuming CPU • Other high-CPU database/host processes CPU Issues
  46. www.viscosityna.com @ViscosityNA • Priority Inversion raises the priority of blocked

    sessions over blockers • Prioritizing database sessions/processes in the OS • Blocked sessions in prioritized by Oracle Resource Manager • OS applies priority decay to reschedule processes spinning on CPU CPU Issues
  47. www.viscosityna.com @ViscosityNA • Altering object metadata used by a child

    cursor invalidates the cursor • Collecting/overcollecting statistics • Refreshing, reorganizing, rebuilding objects • Adding/dropping indexes • Changing index visibility Cursor Invalidation
  48. www.viscosityna.com @ViscosityNA • Altering object metadata used by a child

    cursor invalidates the cursor • Changing column names, datatypes, scale • Adding or dropping columns • TRUNCATEs • GRANTs Cursor Invalidation
  49. www.viscosityna.com @ViscosityNA • Altering object metadata used by a child

    cursor invalidates the cursor • Bulk updates that alter cardinality • Out-thinking the optimizer (dbms_shared_pool) Cursor Invalidation
  50. www.viscosityna.com @ViscosityNA • Under/over-caching cursors • Using session cursor cache

    when sessions are short-lived • Mismatches between client and database environments (NLS, etc) • Undersized SGA • Resizing the Shared Pool/Buffer Cache, esp. as workload changes • cursor_sharing=force Database Configuration
  51. www.viscosityna.com @ViscosityNA • Usually within the database but may also

    be an OS issue • Application code causing deadlocks • Devastating when a blocked session holds an exclusive mutex! • Triggers • PL/SQL, application code, OS operations that "manage" concurrency • Poor lock implementations, incomplete cleanup, etc. Concurrency
  52. www.viscosityna.com @ViscosityNA • Parent cursors are shared by the CDB

    (in the shared Library Cache) • Child cursors are keyed to their PDB—they can't be reused elsewhere • Prevents local PDB activity from blocking other PDBs • Prevents cache "poisoning" • An exclusive mutex on heap 0 on the parent cursor is global Multitenant
  53. Ask questions • what changed? • when did it start?

    • who discovered it? • who/what does it affect? • how do we reproduce it? • what's been done so far? • if intermittent, how often or what is the timing? • is it related to or dependent on something else?
  54. Ask questions • is it related to a speci fi

    c: • user? • client? • process? • service/connection? • event? • day or time? • schema? • object?
  55. www.viscosityna.com @ViscosityNA • 1349387.1: Troubleshooting 'cursor: pin S wait on

    X' waits. • 1353015.1: How to Identify Hard Parse Failures • 1356828.1: FAQ: 'cursor: mutex/cursor: pin/library cache: mutex' Type Wait Events • 1377998.1: Troubleshooting: Waits for Mutex Type Events • 1373500.1: Top 5 Database and/or Instance Performance Issues in RAC Environment • 1377446.1: Troubleshooting Performance Issues • 296377.1: Troubleshooting: High Version Count Issues • 786507.1: How to Determine the Blocking Session for Event: 'cursor: pin S wait on X' Resources
  56. www.viscosityna.com @ViscosityNA • 2745080.1: How To Trace Speci f i

    • 1268724.1: "Cursor: Pin S Wait On X" Contention Mutex Sleep Reason Primarily ' kkslce [KKSCHLPIN2]' • 2548359.1: High Waits On Library Cache Lock or Cursor: Pin S wait on X Due To DS_SVC Queries From 12.2 Onwards • 2585072.1: Synonym with Same Name in Loop-back DB link Causing 'SQL*Net message from dblink' and 'Cursor: pin S wait on X' Resources
  57. www.viscosityna.com @ViscosityNA • 2963237.1: Performance And Hang Issue Due To

    High Cursor: Pin S Wait On X • 2963869.1: High waits for "cursor: pin S wait on X" on a recursive query from sys.hist_head$ • 402027.1: Bug:5653007; 5485914: SELF DEADLOCK PROCESS WAITS ON ''Cursor: Pin S Wait On X'' with SQL_TRACE enabled. • 742599.1: High 'cursor: pin S wait on X' and/or 'library cache lock' Waits Generated by Frequent Shared Pool/Buffer Cache Resize Activity • 9472669.8: Bug 9472669 - 'cursor: pin S wait on X' waits for invalid SQL over DB link Resources
  58. www.viscosityna.com @ViscosityNA • 1298015.1: WAITEVENT: "cursor: pin S wait on

    X" Reference Note Comprehensive list of reference notes and known issues/big by version Resources