Partition scheme filegroup metadata
-
29-09-2020 - |
Question
When you create a partition scheme against a particular partition function, you specify a series of filegroups. I cannot find the metadata store where this filegroup list can be found & queried.
For example, I execute
CREATE PARTITION FUNCTION [ABPF](int) AS RANGE LEFT FOR VALUES (10);
CREATE PARTITION SCHEME [ABPS] AS PARTITION [ABPF] TO ([PS1], [PS2], [PS3]);
I would like to then execute a query:
select * from <table(s)>
And return, PS1,PS2,PS3 in a table, preferably with an indicator that PS3 is the "next used" filegroup.
I've explored the following and cannot seem to see or find the answer:
- sys.partition_functions
- sys.partition_schemes
- sys.partition_parameters
- sys.partition_range_values
- sys.partitions
- sys.dm_db_partition_stats
- sys.filegroups
- sys.sysfiles
- sys.database_files
- sys.data_spaces
- sys.allocation_units
Solution
This script that I have pulled from my script library (probably a Paul Randal script) gives you the file group information.
It also shows you the ranges of values between each boundary so all you would have to do to see which partition is next would be to get the MAX of your partitioning column (Probably the PK).
With this you could see which is the active partition and depending on your function being a left of value or right you could easily see which partition is next. At least I think that works in all situations.
SELECT ps.name AS PartitionScheme
, fg.name AS [FileGroup]
, prv.*
, LAG(prv.Value) OVER (PARTITION BY ps.name ORDER BY ps.name, boundary_id) AS PreviousBoundaryValue
FROM sys.partition_schemes ps
INNER JOIN sys.destination_data_spaces dds
ON dds.partition_scheme_id = ps.data_space_id
INNER JOIN sys.filegroups fg
ON dds.data_space_id = fg.data_space_id
INNER JOIN sys.partition_functions f
ON f.function_id = ps.function_id
INNER JOIN sys.partition_range_values prv
ON f.function_id = prv.function_id
AND dds.destination_id = prv.boundary_id
OTHER TIPS
James answer is a pretty handy script (destination_data_spaces
looks like the key table I missed). Unfortunately it still seems to have 2 problems:
- Doesn't give the
Next Used
filegroup; and - Only works if there is an existing object using the partition.
In my particular scenario I'm simply looking to pull out the filegroup allocations of a given partition scheme, regardless of what's been set against the partition.
All I needed really was:
SELECT *
FROM
sys.destination_data_spaces dds
INNER JOIN sys.filegroups fg
ON dds.data_space_id = fg.data_space_id;
I suggest a couple more tweaks to James' script:
- The inner join to
partition_range_values
drops rows (in particular,next used
, but also the "outside" boundary, so you end up with onlyPS1
returned in my example). Change this to aleft join
. - This is SQL Server 2008 R2 (see question tags) so
LAG
doesn't work.