Skip to content

Fix the permissions required by sys.query_store_query_text and sys.query_store_query_hints - #10461

Open
Rudi Bruchez (rudi-bruchez) wants to merge 2 commits into
MicrosoftDocs:livefrom
rudi-bruchez:fix/query-store-query-text-permissions
Open

Rudi Bruchez (rudi-bruchez) wants to merge 2 commits into
MicrosoftDocs:livefrom
rudi-bruchez:fix/query-store-query-text-permissions

Conversation

@rudi-bruchez

Copy link
Copy Markdown
Contributor

What's wrong

On both pages, the Permissions section says that SQL Server 2019 and previous versions "require VIEW SERVER STATE permission on the server", and that SQL Server 2022 and later "require VIEW SERVER PERFORMANCE STATE permission on the server".

The view is database scoped, like the other Query Store catalog views. A database user with only VIEW DATABASE STATE (or, on SQL Server 2022 and later, only VIEW DATABASE PERFORMANCE STATE) reads it, without any server permission. A user without the database permission gets error 262, which names VIEW DATABASE PERFORMANCE STATE. Until commit b33ab10 (November 2024), both pages said "Requires the VIEW DATABASE STATE permission", as sys.query_store_query, sys.query_store_plan and sys.query_store_runtime_stats_interval still do.

Evidence

SQL Server 2019 CU32-GDR (15.0), 2022 CU26 (16.0.4265.3) and 2025 CU9-GDR (17.0.5015.4), Linux containers, fresh database with Query Store in ALL capture mode.

CREATE DATABASE verif_doc_qstext;
GO
ALTER DATABASE verif_doc_qstext SET QUERY_STORE = ON (OPERATION_MODE = READ_WRITE, QUERY_CAPTURE_MODE = ALL);
GO
CREATE LOGIN verif_doc_lecteur WITH PASSWORD = '<password>';
GO
USE verif_doc_qstext;
CREATE USER verif_doc_lecteur FOR LOGIN verif_doc_lecteur;
GRANT VIEW DATABASE STATE TO verif_doc_lecteur;
GO
SELECT 'marker' AS m, COUNT(*) AS n FROM sys.objects WHERE name = N'verif_doc_marker';
GO
EXECUTE AS LOGIN = 'verif_doc_lecteur';
SELECT HAS_PERMS_BY_NAME(NULL, NULL, 'VIEW SERVER STATE') AS has_view_server_state;
SELECT COUNT(*) AS texts_as_reader
FROM sys.query_store_query_text
WHERE query_sql_text LIKE N'%verif[_]doc[_]marker%';
REVERT;

Observed on the three versions: has_view_server_state = 0, texts_as_reader = 1 (the same count as for sa). On 2022 and 2025, a user granted only VIEW DATABASE PERFORMANCE STATE reads the view too, and a user with no permission gets:

Msg 262, Level 14, State 1
VIEW DATABASE PERFORMANCE STATE permission denied in database 'verif_doc_perf'.

Expected per the current text: the query fails without a server permission.

The same holds for sys.query_store_query_hints (SQL Server 2025 CU9-GDR): after sp_query_store_set_hints, a user with only VIEW DATABASE STATE, and HAS_PERMS_BY_NAME(NULL, NULL, 'VIEW SERVER STATE') = 0, reads the hint row.

Change

On sys.query_store_query_text, replace the two server-level sentences with the database-level wording used by the other Query Store catalog views. On sys.query_store_query_hints, which applies to SQL Server 2022 and later, keep only the SQL Server 2022 wording.

@prmerger-automator

Copy link
Copy Markdown
Contributor

Rudi Bruchez (@rudi-bruchez) : Thanks for your contribution! The author(s) and reviewer(s) have been notified to review your proposed change.

@learn-build-service-prod

Copy link
Copy Markdown
Contributor

Learn Build status updates of commit 4f63767:

✅ Validation status: passed

File Status Preview URL Details
docs/relational-databases/system-catalog-views/sys-query-store-query-hints-transact-sql.md ✅Succeeded
docs/relational-databases/system-catalog-views/sys-query-store-query-text-transact-sql.md ✅Succeeded

For more details, please refer to the build report.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The documented permissions are corrected consistently, with no unresolved review issues.

No open issues 🎉

What changed in this PR

Corrects the documented database-level permissions for the Query Store catalog views sys.query_store_query_text and sys.query_store_query_hints.

Changes:

  • Replaces incorrect server-level requirements with VIEW DATABASE STATE for pre-2022 SQL Server.
  • Documents VIEW DATABASE PERFORMANCE STATE for SQL Server 2022 and later.
  • Aligns both pages with related Query Store catalog view documentation.
File Description
docs/​relational-databases/​system-catalog-views/​sys-query-store-query-text-transact-sql.md Corrects permissions for SQL Server versions before 2022 and SQL Server 2022 and later.
docs/​relational-databases/​system-catalog-views/​sys-query-store-query-hints-transact-sql.md Corrects the database-level permission requirement for SQL Server 2022 and later.

Review effort: Lite

@Court72

Copy link
Copy Markdown
Contributor

Dimitri Furman (@dimitri-furman)

Can you review the proposed changes?

Important: When the changes are ready for publication, adding a #sign-off comment is the best way to signal that the PR is ready for the review team to merge.

#label:"aq-pr-triaged"
@MicrosoftDocs/public-repo-pr-review-team

@prmerger-automator prmerger-automator Bot added the aq-pr-triaged tracking label for the PR review team label Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants