A dog's anal glands should empty when they defecate - the stool pushes against the gland, expressing the secretions through the anal gland duct and into the rectum. This may not occur for a number of reasons.
Blocked anal glands
The anal gland duct may become blocked due to thicker than normal secretions (often due to failure of the gland to empty normally), or anatomical factors such as an unusually small duct system. This may lead to inflammation and infection of the anal gland (the anal secretions sitting in the gland are a lovely place for bacteria to grow!).
Anal gland impaction
Anal gland impaction occurs when the contents of the anal gland accumulate, and are not released normally from the gland. This can occur due to anal duct obstruction, gland inflammation, infection or anal gland tumours, as well as chronic diarrhoea, weakness in the anal muscles, obesity and constipation.
Infected anal glands
Anal gland inflammation and infection can occur with or without duct obstruction, including due to underlying medical conditions such as hormonal disease or allergic skin disease, or incorrect or too frequent manual expression of the glands.
Inflammation, infection and impaction can cause distension of the anal gland, leading to pain and potentially abcessation and rupture of the gland.
Anal gland cancer
The most common type of anal gland cancer in dogs is anal sac adenocarcinoma, which can cause problems due to its potential to spread to nearby lymph nodes and other organs, as well as causing local discomfort due to distension and swelling. These tumors can also lead to hypercalcemia (elevated calcium levels in the blood), which can result in further complications like kidney damage..