[Company Logo Image] 

Home Up Contents Coffee Break Credits Glossary Links Search

 
Get the SQL Text for a QueryID in Azure PostgreSQL

 

 

Home
Analysis Services
Azure
CLR Integration
High Availability
Open Source
Security
SQL Server 2008
SQL Server 2012
SQL Server 2014
SQL Server 2016
SQL Server 2017
SQL Server 2019
Tips
Troubleshooting
Tuning

Get the SQL Text for a QueryID in Azure PostgreSQL.


Applies to: Azure Database PostgreSQL Flexible Server.

Date created: August 2, 2026.
 

Problem Description.
 

While reviewing long-running queries in Query Performance Insight for an Azure Database for PostgreSQL Flexible Server, I collected several Query IDs. The problem was that the Azure portal showed the identifiers, but not the SQL statements behind them.

The portal recommends connecting to the azure_sys database and running a query similar to this:

SELECT query_sql_text
FROM query_store.query_texts_view
WHERE query_text_id = 1714541169297135157;

However, replacing 1714541169297135157 with the Query IDs I had collected returned no rows.

I also tried larger queries involving query_store.qs_view and query_store.query_plans_view, but those added unnecessary complexity when all I needed was the SQL text.

 

Cause.

The Azure portal guidance is incomplete because Azure Query Store uses two different identifiers: query_id and query_text_id. They are both bigint values, but they are not interchangeable. The query_id can be located at query_store.qs_view and it represents a hash calculated from the parsed SQL statement. Meanwhile, query_text_id is related to the query_store.query_texts_view and identifies a row in the Query Store query-text table.

In my case, the identifiers needed to be matched against query_store.qs_view.query_id. Fortunately, qs_view already contains both the Query ID and its associated query_sql_text, so no additional join is required. Query Store views are available in the azure_sys database.

Another important issue worth mentioning is trying to save in Excel the result of queries executed against Query Store views/tables often ended with only 15 digits of the each Query Id preserved. All Query IDs contain 18 or 19 digits. Excel only preserves 15 significant digits when a value is handled as a number. Any remaining digits can be replaced with zeros. The CSV format itself is not the problem; the problem happens when Excel imports or pastes the Query ID as a numeric value Once a Query ID has been rounded by Excel, PostgreSQL receives the wrong ID and correctly returns no rows.

 

Workaround / Solution

Connect directly to the azure_sys database using PgAdmin or any other tool and query query_store.qs_view.

The following query retrieves the SQL text for multiple Query IDs:

SELECT DISTINCT
qv.query_id::text AS query_id,
qv.db_id,
qv.user_id,
qv.query_type,
qv.search_path,
qv.query_sql_text
FROM query_store.qs_view AS qv
WHERE qv.query_id::text IN (
'-1234567890123456789',
'-9876543210987654321',
'1234567890123456789'
)
ORDER BY
qv.query_id::text;


For a single Query ID, the query can be reduced to:

SELECT
query_id::text AS query_id,
query_sql_text
FROM query_store.qs_view
WHERE query_id::text = '-1234567890123456789';


I cast query_id to text for two reasons:

  1. It allows the Query IDs to be supplied as quoted strings.

  2. It prevents Excel from changing them when the results are later exported or copied.


Formatting the Query ID column as Text must be done before pasting or importing the IDs into Excel. Converting a value to text after Excel has already rounded it will not restore the missing digits.

There is no need to join query_store.query_plans_view just to retrieve the SQL text. Query plan storage is a separate Query Store option and may be disabled, while qs_view already provides the required SQL statement.

If the query still returns no rows, verify that:

bullet

The connection is using the azure_sys database.

bullet

The Query ID was copied exactly and was not modified by Excel.

bullet

The current Query Store interval has finished. The default interval is 15 minutes, and data being collected in memory is not visible until it is persisted.

bullet

The query is still inside the Query Store retention period.

One final detail is the query_sql_text is a representative SQL statement. Query Store can group structurally equivalent statements under the same query_id, especially when they differ only by literal values. Therefore, the returned text may not include the exact parameter values used during the slow execution.

In summary, use query_store.qs_view when you have a query_id. Use query_store.query_texts_view only when you already have a query_text_id.

 

 

 

.Send mail to sqlcoffee.stretch737@simplelogin.com with questions or comments about this web site.