puppetlabs/puppetdb — pull request #1184 by dalen (2014-12-19 14:50:57+00:00)
Title: (PDB-1076) Make environment garbage collection ignore null values

If a factset, report or catalog is migrated from an earlier puppetdb
version or submitted with a earlier puppetdb-terminus version it has
NULL as environment_id. If any of them have a record with that PuppetDB
failed to garbage collect any environments at all as the subselect
returned a NULL value.

Switching from UNION ALL to just UNION speeds up the query dramatically
as the return value from the subselect is much smaller (doesn't contain
any duplicates).