Search This Blog

Showing posts with label Architecture Series. Show all posts
Showing posts with label Architecture Series. Show all posts

Thursday, January 12, 2012

Something About Transaction Log

The transaction log in a database maps over one or more physical files. Conceptually, the log file is a string of log records. Physically, the sequence of log records is stored efficiently in the set of physical files that implement the transaction log. The SQL Server Database Engine divides each physical log file internally into a number of virtual log files.

Virtual log files have no fixed size, and there is no fixed number of virtual log files for a physical log file. With few examples we will look at the number of VLFs created for a given database. In the first example I create a database with the log file size of 512 KB (minimum possible). I will be using the same script for creation of the everytime with constantly changing the log file size

USEmaster

GO

IF EXISTS(SELECT 1 FROM sys.databases WHERE name ='VLF')

DROP DATABASE VLF;

GO

CREATE DATABASE VLF

ON PRIMARY

( NAME='VLF',

FILENAME= 'D:\VLFP.mdf',

SIZE=4MB,

FILEGROWTH=1MB)

LOG ON

( NAME='VLFL',

FILENAME='D:\VLFL.ldf',

SIZE=1025MB,

FILEGROWTH=1MB);

GO



Now that the database is created we will see how many virtual log files are created. This can be checked using the DBCC LOGINFO command as shown below. For a 512KB of log file there are 2 VLF files created with FSeqNo as 41 and 0. FseqNo when 0 means that the VLF is unused since creation. On the other hand Status 0 means VLF is not being used currently, but may have been used previously. In our case VLF with FseqNo has status 2, which means this is an active VLF (holding active transactions.

The question now remains is what is the CreateLSN field for ? This field has 0 for all the VLFs that were created when the database was created (which exist from the beginning). In our case both existed since the database creation time.

USE VLF

GO

DBCC LOGINFO



Now we will increase the database log file size to 1MB and re-execute the above database creation statement. Once the database has been created, lets see the VLFs using DBCC LOGINFO. As can be seen this time 4 VLFs has been created. Till we reach the 64MB mark there will be only 4 VLFs created

USE VLF

GO

DBCC LOGINFO




Increasing the size of the database log file to 65MB will created 8 VLFs and going beyond 1GB will create 16 as visibile from the snap shot below respectively.

USE VLF

GO

DBCC LOGINFO








This is how a typical log file should be logically organized into VLFs. The diagram has been taken from BOL



Lets not talk about checkpoint. A checkpoint performs the following processes in the database:

  • Writes a record to the log file, marking the start of the checkpoint.
  • Stores information recorded for the checkpoint in a chain of checkpoint log records.
  • One piece of information recorded in the checkpoint is the log sequence number (LSN) of the first log record that must be present for a successful database-wide rollback. This LSN is called the Minimum Recovery LSN (MinLSN). The MinLSN is the minimum of the:
    • LSN of the start of the checkpoint.
    • LSN of the start of the oldest active transaction.
    • LSN of the start of the oldest replication transaction that has not yet been delivered to the distribution database.
  • The checkpoint records also contain a list of all the active transactions that have modified the database.
  • If the database uses the simple recovery model, marks for reuse the space that precedes the MinLSN.
  • Writes all dirty log and data pages to disk.
  • Writes a record marking the end of the checkpoint to the log file.
  • Writes the LSN of the start of this chain to the database boot page.

Let me show you some of this using a small example by creating a table called CheckPt

CREATE TABLE CheckPt(a int);

Before I do any other operation I want to fire a checkpoint in order to clear the existing log

CHECKPOINT



Now we will insert a single row in this table, however we would not commit the transaction.

BEGIN TRANSACTION T1

INSERT INTO Checkpt VALUES (10);

Lets take a look at the transaction log using the command DBCC LOG (databasename, 4). Couple of things to focus here is allocation in IAM, blocking of PFS (Page Free Space) and LOP_INSERT_ROWS (row 20), where we do the actual insert.

DBCC LOG(VLF,4)



Interesting thing is to look at the Log_Record column for this record LOP_INSERT_ROWS. This column is not there in the snapshot, however I have pasted the content for it below. At the offset 51-52 is the page no where the row has been inserted (in this case 4F (hex) = 79 decimal. This is confirmed below using the DBCC IND command as shown below

0x00003E00290000007100000001000200E7020000000002014F000000010000001D00000029000000710000000F00000100000D000000000100000000410003000B00000014000000100008000A000000010000000101000C0000E7A4787D00000102000402030004



DBCC IND(VLF, Checkpt, 1)





Now we fire the check point again. We know that one of the transactions is active (Insert).

CHECKPOINT

Lets look at the LOG using the DBCC LOG

DBCCLOG(VLF,4)

In the snapshot in the highlighted 3 rows you can see the row with LOP_XACT_CKPT is for the transation with LSN 00000029:00000071:0001 (row 1). This is the reason that the Log Record column has this LSN embedded in it. I leave it to you to find out




Finally I will rollback this transaction to show you how the undo information gets added to the log. Currently we have only 25 rows in the table. After rollback we will have few more as can be seen in the snapshot below. Look at the operation LOP_DELETE_ROWS which rollsback the insert statement.

ROLLBACKTRAN T1

DBCCLOG(VLF, 4)

Boot Page

In a database the primary file holds the critical information in a page called the boot page. It is the 9th page of the primary file in the PRIMARY file group. Let’s see what information this page holds.


-- create a database boot

CREATE DATABASE Boot;


-- Using DBCC PAGE we will examine the details in the boot page

DBCC TRACEON(3604)

DBCC PAGE (Boot, 1, 9, 3)


------- Page Header -----------

Page @0x0000000088D8A000


m_pageId = (1:9) m_headerVersion = 1 m_type = 13

m_typeFlagBits = 0x0 m_level = 0 m_flagBits = 0x200

m_objId (AllocUnitId.idObj) = 99 m_indexId (AllocUnitId.idInd) = 0 Metadata: AllocUnitId = 6488064

Metadata: PartitionId = 0 Metadata: IndexId = 0 Metadata: ObjectId = 99

m_prevPage = (0:0) m_nextPage = (0:0) pminlen = 0

m_slotCnt = 1 m_freeCnt = 6650 m_freeData = 1540

m_reservedCnt = 0 m_lsn = (34:26:3) m_xactReserved = 0

m_xdesId = (0:0) m_ghostRecCnt = 0 m_tornBits = -1508490807

------- Page Header -----------


-- As can be seen some of the important fields in the header say that : record in this page is 1, page type is 13 which means boot page, free space in the page is 6650 bytes, last lsn that updated this page. We will now look at the data in this page



------------- Page Data -------------------------

DBINFO @0x000000000EFAA060


dbi_dbid = 7 dbi_status = 65536 dbi_nextid = 2105058535

dbi_dbname = Boot dbi_maxDbTimestamp = 2000 dbi_version = 660

dbi_createVersion = 660 dbi_ESVersion = 0

dbi_nextseqnum = 1900-01-01 00:00:00.000 dbi_crdate = 2010-03-18 17:45:52.120

dbi_filegeneration = 0

dbi_checkptLSN


m_fSeqNo = 34 m_blockOffset = 27 m_slotId = 43

dbi_RebuildLogs = 0 dbi_dbccFlags = 2

dbi_dbccLastKnownGood = 1900-01-01 00:00:00.000

dbi_dbbackupLSN


m_fSeqNo = 0 m_blockOffset = 0 m_slotId = 0

------------- Page Data -------------------------


-- Some of the important fields that we need to look at are

  • dbi_dbid : unique identifier for the database (Can be obtained from sys.databases as well)
  • dbi_dbname : name of the database
  • dbi_version : physical version number of the database (660 here). dbi_createVersion is also the same.
  • dbi_crdate : creation date of the database
  • dbi_dbccLastKnownGood : the datetime of the last clean run of DBCC CHECKDB
  • dbi_LastLogBackupTime : last time the log backup was taken
  • dbi_differentialBaseGuid: the GUID generated by the last full database backup. All differential backup will check this guid to be restored on to.
  • dbi_checkptLSN : This is the last check point lsn. Every time a checkpoint happen the lsn is updated in the boot page. This is where the recovery starts when the database starts



-- I will chow you an example of how the check point gets updated. Cusrrently the checkpoint LSN is

m_fSeqNo = 34 m_blockOffset = 27 m_slotId = 43

-- Lets see what is present in the transaction log. I have just taken the extract of the transaction log

not the entire log so that I can fit it in.



-- I now fire a CHECKPOINT command. Basically I am forcing a checkpoint here

CHECKPOINT


-- Using DBCC LOG we can check that the transaction log now has the following 2 rows for checkpoint

DBCC LOG (Boot, 4)


-- If you see the lsn in the log for checkpoint is "00000022:00000037:003d". I am expecting the same lsn in the boot page as well. Let's fire the DBCC PAGE on boot page and I get the number as below. I need to convert it to hex as shown below

DBCC TRACEON(3604)

DBCC PAGE (Boot, 1, 9, 3)


-- 34 (dec) = 22 (hex)

-- 55 (dec) = 37 (hex)

-- 61 (dec) = 3d (hex)


-- Clearly you can see that the Checkpoint LSN in the log record "00000022:00000037:003d" is the LSN in the boot page.


-- Two points to note here

  • Corruption of boot page is the corruption of database
  • The information available in the boot page can also be obtained using DBCC DBINFO command

Index Allocation Map

As BOL says an Index Allocation Map (IAM) page maps the extents in a 4-gigabyte (GB) part of a database file used by an allocation unit. An allocation unit is one of three types:

  • IN_ROW_DATA : Holds a partition of a heap or index.
  • LOB_DATA : Holds large object (LOB) data types, such as xml, varbinary(max), and varchar(max).
  • ROW_OVERFLOW_DATA : Holds variable length data stored in varchar, nvarchar, varbinary, or sql_variant columns that exceed the 8,060 byte row size limit.

Let’s see all this through an example.

USE master;

GO

IF EXISTS(SELECT 1 FROM sys.databases WHERE name ='IAM')

DROP DATABASE IAM;

GO

-- Create a database

CREATE DATABASE IAM;

-- Create a table Test in it

Use IAM

GO

CREATE TABLE Test(c1 int identity(1,1), c2 varchar(8000));

-- Populate this table with some data

DECLARE @i int= 1;

WHILE (@i < 16)

BEGIN

INSERT INTO Test VALUES (REPLICATE('a',8000));

SET @i = @i + 1;

END;



-- Check the pages allocated to this object. Clearly you can see the page 89 is an IAM page as the page type says 10. This IAM page is to manage the IN_ROW_DATA allocation unit. Since this IAM can cover upto 4GB of data size for this allocation unit there is only 1 IAM page required currently.



DBCC IND(IAM, Test, 1)





-- We will now check what is there in the IAM page for this object Test

DBCC TRACEON(3604)

DBCC PAGE(IAM, 1, 89, 1)



-- From the header below you can see the page type is 10 which means IAM page



Page @0x000000009C5A8000

m_pageId =(1:89) m_headerVersion = 1 m_type = 10

m_typeFlagBits = 0x0 m_level = 0 m_flagBits = 0x0

m_objId(AllocUnitId.idObj)= 29 m_indexId(AllocUnitId.idInd)= 256

Metadata: AllocUnitId = 72057594039828480

Metadata: PartitionId = 72057594038779904 Metadata: IndexId = 0

Metadata: ObjectId = 2105058535 m_prevPage =(0:0) m_nextPage =(0:0)

pminlen = 90 m_slotCnt = 2 m_freeCnt = 6

m_freeData = 8182 m_reservedCnt = 0 m_lsn =(32:223:7)

m_xactReserved = 0 m_xdesId =(0:0) m_ghostRecCnt = 0

m_tornBits = 0



-- The memory dump of the IAM page clearly shows that the extent has been allocated to it. This is only a partial output as the complete o/p would have taken too much space.



Slot 1, Offset 0xbe,Length 7992, DumpStyle BYTE

Record Type= PRIMARY_RECORD Record Attributes = Record Size = 7992

Memory Dump @0x000000000F1CA0BE

0000000000000000: 0000381f 00008000 00000000 00000000 ..8.............

0000000000000010: 00000000 00000000 00000000 00000000 ................

0000000000000020: 00000000 00000000 00000000 00000000 ................

0000000000000030: 00000000 00000000 00000000 00000000 ................

0000000000000040: 00000000 00000000 00000000 00000000 ................

0000000000000050: 00000000 00000000 00000000 00000000 ................



-- Lets decode the initial few bytes. Specially the 3rd and 4rth byte which is 381f. 381f = 83f1 (reversed bytes) = 1f38 (read right to left) = 7992 (converted to decimal). As can be seen clearly this is the record size of the single record in the IAM. Lets now expand the 5th, 6th, 7th byte which is 000080. The first byte is 00 which means 00000000 (bits). Each bit in this byte covers 1 extent which means 8 pages. If we expand all 3 bytes (000080) it would result in the following bit pattern

-- 00000000

-- 00000000

-- 10000000

-- First byte, all 8 bits 0, which means no allocation for the first 64 pages. Similarly second byte all 8 bits are 0 (64 pages) and finally the third byte first 7 bits are 0 (56 pages) 64 + 64 + 56 = 184 (0 to 183 pages are free (no uniform extent allocated, all mixed extents). Extent starting at 184th page is allocated as represented by the last bit which is set to 1.

-- Let's populate 1 more extent.



DECLARE @i int= 1;

WHILE (@i <= 8)

BEGIN

INSERT INTO Test VALUES (REPLICATE('a',8000));

SET @i = @i + 1;

END;

-- We can clearly see that page 192 to 198 have been allocated from new extent

DBCC IND(IAM, Test, 1)










-- The extract from the IAM page has changed slighlty as can be seen below

DBCC TRACEON(3604)

DBCC PAGE(IAM, 1, 89, 1)



Memory Dump @0x000000000F6FA0BE

0000000000000000: 0000381f 00008001 00000000 00000000 ..8.............

0000000000000010: 00000000 00000000 00000000 00000000 ................

0000000000000020: 00000000 00000000 00000000 00000000 ................



-- 5th, 6th and 7th bytes are the same. However the 8th byte has now changed to 01. 00000001 : This means that the extent that starts with the page 192 is allocated for Test as (64 (1st byte) + 64 (2nd byte) + 64 (3rd byte)) = 192 (0 - 191). We can confirm this by looking at the IAM snapshot above. Pages 192 to 198 are allocated to Test. I will now show how adding one more column to this table will lead to row overflow data movement. This will happen as we already have each row very close the max available size of 8060.



ALTER TABLE Test ADD c3 varchar(500);

-- Updating this column with some data. We now check if the row over flow has happened. Indeed it has. The important thing is now there is one more IAM page 119 which is used to manage the allocation unit ROW_OVERFLOW_DATA. I leave it to you to investigate what is there in IAM page 119

UPDATE Test

SET c3 =REPLICATE('a',500)

-- You can notice the IAM page for ROW_OVERFLOW_DATA and its data pages with page type 3

DBCC IND(IAM, Test, 1)



-- Finally we add one more column as varchar(max) which is going to create the LOB_DATA allocation unit

ALTER TABLE Test ADD c4 varchar(max);

UPDATE Test

SET c4 =REPLICATE('b',8070)

-- You can notice the IAM page for LOB_DATA and its data pages with page type 3. The IAM page is 175

DBCC IND(IAM, Test, 1)

Differential Changed Map & Bulk Changed Map

SQL Server uses two internal data structures to track extents modified by bulk copy operations and extents modified since the last full backup. Like the Global Allocation Map (GAM) and Shared Global Allocation Map (SGAM) pages, these structures are bitmaps in which each bit represents a single extent.

Differential Changed Map (DCM)
This tracks the extents that have changed since the last BACKUP DATABASE statement. If the bit for an extent is 1, the extent has been modified since the last BACKUP DATABASE statement. If the bit is 0, the extent has not been modified.

Differential backups read just the DCM pages to determine which extents have been modified. This greatly reduces the number of pages that a differential backup must scan.



Bulk Changed Map (BCM)
This tracks the extents that have been modified by bulk logged operations since the last BACKUP LOG statement. If the bit for an extent is 1, the extent has been modified by a bulk logged operation after the last BACKUP LOG statement. If the bit is 0, the extent has not been modified by bulk logged operations.

Although BCM pages appear in all databases, they are only relevant when the database is using the bulk-logged recovery model. In this recovery model, when a BACKUP LOG is performed, the backup process scans the BCMs for extents that have been modified. It then includes those extents in the log backup. This lets the bulk logged operations be recovered if the database is restored from a database backup and a sequence of transaction log backups. BCM pages are not relevant in a database that is using the simple recovery model, because no bulk logged operations are logged. They are not relevant in a database that is using the full recovery model, because that recovery model treats bulk logged operations as fully logged operations.

The interval between DCM pages and BCM pages is the same as the interval between GAM and SGAM page, 64,000 extents. The DCM and BCM pages are located behind the GAM and SGAM pages in a physical file.

Its always good to explain things by example and so, we will use the Page database again to see how DCM and BCM behave. Let's first concentrate on DCM.

=> Check the status of the PFS page. As can be seen below PFS page shows that currenlty only 183 pages have been allocated. I have only taken the partial output

DBCC TRACEON(3604);
DBCC PAGE(Page, 1, 1, 3);
----Output----
(1:170) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext
(1:171) - = ALLOCATED 0_PCT_FULL Mixed Ext
(1:172) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext
(1:173) - (1:175) = NOT ALLOCATED 0_PCT_FULL
(1:176) - (1:181) = ALLOCATED 0_PCT_FULL
(1:182) - (1:383) = NOT ALLOCATED 0_PCT_FULL

=> Check the status of GAM page. As shown below this also confirms that the last allocated extent is 176th page to 183rd page. Again I am pasting only the partial output

DBCC TRACEON(3604)
DBCC PAGE(Page, 1, 1, 3)
----Output----
GAM: Header @0x000000000D96A064 Slot 0, Offset 96
status = 0x0
GAM: Extent Alloc Status @0x000000000D96A0C2
(1:0) - (1:176) = ALLOCATED
(1:184) - (1:376) = NOT ALLOCATED

=> Finally check the status of the DCM page. This also confirms that no extent after page 183 has changed since the last BACKUP DATABASE statement. Note that the DCM page is page 6.

DBCC TRACEON(3604)

DBCC PAGE(Page, 1, 1, 3)

----Output----
DIFF_MAP: Header @0x000000000C11A064 Slot 0, Offset 96

status = 0x0
DIFF_MAP: Extent Alloc Status @0x000000000C11A0C2
(1:0) - (1:176) = CHANGED
(1:184) - (1:376) = NOT CHANGED

=> Now I will create a table Test in this database and populate some data so that the extent allocation goes beyond page 183 using the script below

CREATE TABLE Test(col1 int, col2 nvarchar(900))
GO
DECLARE @i int= 0;
WHILE @i < 20
BEGIN
INSERT INTO Test VALUES (1,REPLICATE('a',900));
INSERT INTO Test VALUES (1,REPLICATE('b',900));
INSERT INTO Test VALUES (1,REPLICATE('c',900));
INSERT INTO Test VALUES (1,REPLICATE('d',900));
INSERT INTO Test VALUES (1,REPLICATE('e',900));
INSERT INTO Test VALUES (1,REPLICATE('f',900));
INSERT INTO Test VALUES (1,REPLICATE('g',900));
INSERT INTO Test VALUES (1,REPLICATE('h',900));

SET @i = @i + 1;
END

=> Now I will fire the DBCC PAGE command again for PFS, GAM and DCM. The output I get is as below.

------PFS--------

(1:170) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext
(1:171) - = ALLOCATED 0_PCT_FULL Mixed Ext
(1:172) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext
(1:173) - (1:175) = NOT ALLOCATED 0_PCT_FULL
(1:176) - (1:181) = ALLOCATED 0_PCT_FULL
(1:182) - (1:183) = NOT ALLOCATED 0_PCT_FULL
(1:184) - (1:191) = ALLOCATED 50_PCT_FULL
(1:192) - (1:233) = ALLOCATED 80_PCT_FULL
(1:234) - = ALLOCATED 50_PCT_FULL
(1:235) - (1:383) = NOT ALLOCATED 0_PCT_FULL
------GAM------

GAM: Extent Alloc Status @0x000000000C11A0C2
(1:0) - (1:232) = ALLOCATED
(1:240) - (1:376) = NOT ALLOCATED
-------DCM--------

DIFF_MAP: Extent Alloc Status @0x000000000F92C0C2
(1:0) - (1:232) = CHANGED
(1:240) - (1:376) = NOT CHANGED

Notice how DCM moved from page 184 to page 240. It says that since the last BACKUP DATABASE statement all extents till page 239 have been modified. I will now fire a BACKUP DATABASE statement and show you how DCM changes.

BACKUP DATABASE [Page] TO DISK=N'D:\Test.bak'

GO

Now let's fire DBCC PAGE on DCM again. Clearly shows that all pages after page 32 haven't changed since last BACKUP DATABASE statement. THe changes in the initial set of pages seems to be the metadat changes due to the BACKUP DATABASE statement.

DIFF_MAP: Extent Alloc Status @0x000000000EF2A0C2
(1:0) - (1:24) = CHANGED
(1:32) - (1:376) = NOT CHANGED

I will move on to BCM now. In order to see the working of BCM, I will create the page database again using the script in the Data File Secition above. The database has been created in the FULL recovery mode. When I fire the DBCC PAGE command for BCM, I get the following output. Note that BCM is page 7 and the output printed below is only partial.

DBCC TRACEON (3604);
DBCC PAGE (Page, 1, 7, 3);

ML_MAP: Extent Alloc Status @0x000000000C11A0C2
(1:0) - (1:376) = NOT MIN_LOGGED

I will now create the Test_1 table using the Test table created above and see if anything changed in the BCM page.

SELECT * INTO Test_1 FROM Test(nolock);

After creation of the table DBCC PAGE on BCM gives the following output. Basically no change.

ML_MAP: Extent Alloc Status @0x000000000EF2A0C2
(1:0) - (1:376) = NOT MIN_LOGGED

I will now change the recovery mode of the database to simple recorvery mode and drop the existing table Test.

ALTER DATABASE Page SET RECOVERY SIMPLE;

GO

DROP TABLE Test_1;

Let me check BCM page now.

ML_MAP: Extent Alloc Status @0x000000000D96A0C2
(1:0) - (1:376) = NOT MIN_LOGGED

I will create and populate the Test_1 table again using the script above. This time the database is in SIMPLE recovery mode. The output that I get for BCM after creation and population of the table is as below. Again it hasn't changed.

ML_MAP: Extent Alloc Status @0x000000000F49A0C2
(1:0) - (1:376) = NOT MIN_LOGGED

Finally I will change the database to BULK LOGGED mode and see the BCM page state before and after the Test_1 table creation.

ALTER DATABASE Page SET RECOVERY BULK_LOGGED;

GO

DROP TABLE Test_1;

Let me check the BCM page now.

ML_MAP: Extent Alloc Status @0x000000001385A0C2
(1:0) - (1:376) = NOT MIN_LOGGED

Again creating and populating the Test table changes the BCM page as follows

(1:0) - (1:80) = NOT MIN_LOGGED

(1:88) - = MIN_LOGGED

(1:96) - = NOT MIN_LOGGED

(1:104) - (1:120) = MIN_LOGGED

(1:128) - (1:256) = NOT MIN_LOGGED

(1:264) - (1:296) = MIN_LOGGED

(1:304) - (1:376) = NOT MIN_LOGGED

Global Allocation Map & Shared Global Allocation Map

Global Allocation Map (GAM)

GAM pages record what extents have been allocated. Each GAM covers 64,000 extents, or almost 4 GB of data. The GAM has one bit for each extent in the interval it covers. If the bit is 1, the extent is free; if the bit is 0, the extent is allocated.

Shared Global Allocation Map (SGAM)

SGAM pages record which extents are currently being used as mixed extents and also have at least one unused page. Each SGAM covers 64,000 extents, or almost 4 GB of data. The SGAM has one bit for each extent in the interval it covers. If the bit is 1, the extent is being used as a mixed extent and has a free page. If the bit is 0, the extent is not used as a mixed extent, or it is a mixed extent and all its pages are being used.

Each extent has the following bit patterns set in the GAM and SGAM, based on its current use.



Current use of extent
GAM bit setting
SGAM bit setting
Free, not being used
1
0
Uniform extent, or full mixed extent
0
0
Mixed extent with free pages
0
1


To allocate a uniform extent, the Database Engine searches the GAM for a 1 bit and sets it to 0. To find a mixed extent with free pages, the Database Engine searches the SGAM for a 1 bit. To allocate a mixed extent, the Database Engine searches the GAM for a 1 bit, sets it to 0, and then also sets the corresponding bit in the SGAM to 1. To de-allocate an extent, the Database Engine makes sure that the GAM bit is set to 1 and the SGAM bit is set to 0. The algorithms that are actually used internally by the Database Engine are more sophisticated than what is described in this topic, because the Database Engine distributes data evenly in a database. However, even the real algorithms are simplified by not having to manage chains of extent allocation information.

Let me take a small example here to explain you GAM and then you can work on SGAM. I will use the page database.

USe Page

GO

DBCC TRACEON(3604)

DBCC PAGE (Page, 1, 2, 1)


And the partial dump of the output is

Memory Dump @0x000000000E9DA0BE


0000000000000000: 0000381f 000080ff ffffffff ffffffff †..8....ÿÿÿÿÿÿÿÿÿ

0000000000000010: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000020: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000030: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000040: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000050: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000060: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

0000000000000070: ffffffff ffffffff ffffffff ffffffff †ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

000080ff => these four bytes can be expanded as below.

First byte = 00 = 00000000 = each bit covers 8 pages (1 extent) = 0 to 63 pages

Second byte = 00 = 00000000 = 64 to 127 pages

Third byte = 80 = 10000000 = 7 * 8 = 128 to 183 pages

Let’s check PFS page

USe Page

GO

DBCC TRACEON(3604)

DBCC PAGE (Page, 1, 1, 3)

PFS: Page Alloc Status @0x000000000F92C000


(1:0) - (1:3) = ALLOCATED 100_PCT_FULL

(1:4) - (1:5) = NOT ALLOCATED 0_PCT_FULL

(1:6) - (1:7) = ALLOCATED 100_PCT_FULL

(1:8) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:9) - = ALLOCATED 100_PCT_FULL Mixed Ext

(1:10) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:11) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:12) - = ALLOCATED 100_PCT_FULL IAM Page Mixed Ext

(1:13) - (1:16) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:17) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:18) - (1:19) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:20) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:21) - (1:25) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:26) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:27) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:28) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:29) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:30) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:31) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:32) - = ALLOCATED 50_PCT_FULL Mixed Ext

(1:33) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:34) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:35) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:36) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:37) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:38) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:39) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:40) - (1:41) = ALLOCATED 0_PCT_FULL

(1:42) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:43) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:44) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:45) - (1:46) = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:47) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:48) - (1:53) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:54) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:55) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:56) - (1:63) = ALLOCATED 0_PCT_FULL

(1:64) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:65) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:66) - (1:71) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:72) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:73) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:74) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:75) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:76) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:77) - (1:78) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:79) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:80) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:81) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:82) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:83) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:84) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:85) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:86) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:87) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:88) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:89) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:90) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:91) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:92) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:93) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:94) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:95) - = ALLOCATED 0_PCT_FULL Has Ghost Mixed Ext

(1:96) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:97) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:98) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:99) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:100) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:101) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:102) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:103) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:104) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:105) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:106) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:107) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:108) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:109) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:110) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:111) - (1:113) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:114) - (1:115) = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:116) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:117) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:118) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:119) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:120) - = NOT ALLOCATED 0_PCT_FULL Mixed Ext

(1:121) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:122) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:123) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:124) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:125) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:126) - = NOT ALLOCATED 0_PCT_FULL Has Ghost Mixed Ext

(1:127) - = NOT ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:128) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:129) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:130) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:131) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:132) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:133) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:134) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:135) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:136) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:137) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:138) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:139) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:140) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:141) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:142) - (1:144) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:145) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:146) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:147) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:148) - = NOT ALLOCATED 50_PCT_FULL Mixed Ext

(1:149) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:150) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:151) - = NOT ALLOCATED 100_PCT_FULL Mixed Ext

(1:152) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:153) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:154) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:155) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:156) - (1:158) = ALLOCATED 0_PCT_FULL Mixed Ext

(1:159) - = ALLOCATED 100_PCT_FULL Mixed Ext

(1:160) - = ALLOCATED 50_PCT_FULL Mixed Ext

(1:161) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:162) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:163) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:164) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:165) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:166) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:167) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:168) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:169) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:170) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:171) - = ALLOCATED 0_PCT_FULL Mixed Ext

(1:172) - = ALLOCATED 0_PCT_FULL IAM Page Mixed Ext

(1:173) - (1:175) = NOT ALLOCATED 0_PCT_FULL

(1:176) - (1:181) = ALLOCATED 0_PCT_FULL

(1:182) - (1:383) = NOT ALLOCATED 0_PCT_FULL

As you can see all extents after page 183 are not allocated. Similarly you check for page 3 (SGAM).