sp_IndexCleanup: fix NULL database_name on Azure SQL DB - #840
Merged
Conversation
On Azure SQL DB, sys.databases.database_id is scoped to the logical server while DB_ID(), DB_NAME(), and the database_id column in every DMV are scoped to the database/elastic pool, and the two id spaces do not have to agree (documented in the DB_ID() remarks). The procedure stored the sys.databases id in #databases, so on Hyperscale DB_NAME(@database_id) resolved to NULL and the first insert into a NOT NULL database_name column died with Msg 515. The same mismatch made fo.database_id = os.database_id match nothing, silently emptying #operational_stats. Fix, direction-proof on both counts: - #databases now stores ISNULL(DB_ID(d.name), d.database_id) - the local-space id, identical on box product - so every DMV predicate (dm_db_index_operational_stats, dm_db_index_usage_stats, the operational stats join) compares within one id space. - Names are never derived from ids anymore: every DB_NAME(@database_id) / DB_NAME(os.database_id) / QUOTENAME(DB_NAME(@current_database_id)) site now uses @current_database_name, parameterized into the dynamic SQL. Verified: compiles on SQL 2017/2022/2025; full local harness on 2022 passes (adversarial 32/32, fixture-cases 31/31 including the execute check, rule-coverage 40/40 covering @get_all_databases, no-access 4/4). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
sp_IndexCleanup/sp_IndexCleanup.sqlISNULL(DB_ID(d.name), d.database_id)— the local-space id, identical values on box product — aligning dm_db_index_operational_stats, dm_db_index_usage_stats, and the operational-stats join predicates within one id space.DB_NAME(@database_id)/DB_NAME(os.database_id)/QUOTENAME(DB_NAME(@current_database_id))site (#filtered_objects, #operational_stats, #index_details, #partition_stats, #index_analysis inserts, generated DDL) now uses@current_database_name, parameterized into the dynamic SQL.Test Plan
Closes #839
Generated with Claude Code