SQL SERVER – List Service Broker Queue Count

I inspect Service Broker Queue Count through queue and internal-table catalogs. A customer requested a quick inventory.

Populated, retained and empty receiving drawers remain separate from an outbound case.

SELECT SCHEMA_NAME(q.schema_id) AS SchemaName,q.name AS QueueName,
       COALESCE(SUM(p.rows),0) AS ApproximateStoredRows
FROM sys.service_queues q
LEFT JOIN sys.internal_tables it ON it.parent_id=q.object_id
    AND it.internal_type_desc=N'QUEUE_MESSAGES'
LEFT JOIN sys.partitions p ON p.object_id=it.object_id AND p.index_id=1
GROUP BY q.schema_id,q.name ORDER BY SchemaName,QueueName;
Historical queue-count result from the original catalog query.
Historical queue-count result from the original catalog query.

My original join did not explicitly restrict results to Broker queues. This query starts with sys.service_queues and QUEUE_MESSAGES. It groups partitions and retains queues with no stored rows.

sys.partitions rows is an approximate storage count. Retention and internal queue state affect its meaning. It does not count pending transmissions in sys.transmission_queue. Nor does it establish healthy activation and delivery.

Use the intended database and required metadata permissions. Inspect a particular queue and its retained messages when that is the real question. This inventory changes no Broker settings.

Reference: Service Broker internal queue tables.

Related reading

A queue storage count is not an exact deliverable-message count, it is a measure to interpret with queue state.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Service Broker, SQL Scripts, SQL Server
Previous Post
SQL SERVER – Enabling or Disabling Triggers with the Correct Scope
Next Post
Checking SQL Server Service Startup Type and Status From T-SQL

Related Posts

1 Comment. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.